Can't find the solution to this sprite problem

Blitz3D Forums/Blitz3D Programming/Can't find the solution to this sprite problem

Jeroen(Posted 2003) [#1]
Hi.

What my problem is that I want to chop images in 16 pieces, make a texture of it and put it on an entity.

1. Load in four images that will form my complete image.
2. Cut the big image into 16 pieces
3. These pieces need to be baked on a texture, and this
texture is placed on an entity

Unfortunately it seems all the images are the same, and do not represent the block it should be. Secondly, wouldn't there be a much more elegant way to position my entity relative to the camera position?

This is a mockup of what I am trying:



Graphics3D 640,480
SetBuffer BackBuffer()

treasureMapSize = 474

pieceSize = treasureMapSize / 4
blockSize = treasureMapSize / 2
 

Type kaartstukje
	Field entity
	Field texture
	Field id
End Type


plane=CreatePlane()
mycamera=CreateCamera()

kaart1 = LoadSprite ("kaart1.jpg",4)
kaart2 = LoadSprite ("kaart2.jpg",4)
kaart3 = LoadSprite ("kaart3.jpg",4)
kaart4 = LoadSprite ("kaart4.jpg",4)
palen = LoadSprite ("vb.png",2)
ScaleSprite palen,2,2

PositionEntity mycamera,1.7,2.7,0.97
RotateEntity mycamera,90,0,0

PositionEntity palen,1,0,1
PositionEntity kaart1,0,0,0
PositionEntity kaart2,2,0,0
PositionEntity kaart3,0,0,2
PositionEntity kaart4,2,0,2

RenderWorld
Flip
WaitKey


; this loop SHOULD do the following:
; chop the area from 0,0 to pieceSize,pieceSize in 16 tiles,
; place each tile (image) onto a texture, and place the texture on an entity (Sprite)

For y = 0 To 3
	For x = 0 To 3
		k.kaartstukje = New kaartstukje
		k\entity = CreateSprite()
		
		image = CreateImage(pieceSize,pieceSize) 
		CopyRect pieceSize*x,pieceSize*y,pieceSize ,pieceSize ,0,0,BackBuffer(),ImageBuffer(image)
		k\texture = CreateTexture(pieceSize,pieceSize) 	
		SetBuffer TextureBuffer(k\texture)
		DrawImage image,0,0
		SetBuffer BackBuffer()
		EntityTexture k\entity,k\texture
		FreeImage image
		
	Next
Next

; free the entities I don't need anymore
FreeEntity kaart1
FreeEntity kaart2
FreeEntity kaart3
FreeEntity kaart4
FreeEntity palen



For k.kaartstukje = Each kaartstukje
	PositionEntity k\entity,0,0,0
	RenderWorld
	Flip
	Delay(400)
Next

While Not KeyHit(1)
	RenderWorld
	Text 0,0,MouseX()
	Text 0,20,MouseY()
	Flip
	
Wend

End





Ross C(Posted 2003) [#2]
Try making you texture equal to 512 x 512 or any square number. Blitz will always scale the texture to the nearest square power i belive. I'll take a look at it later, i'm going out the now :D


Ethan3850(Posted 2003) [#3]
Hope this helps, if not sorry (I was in a hurry!!!)

What Ross C said is very important, and also unfortunately not all graphic cards support the same texture sizes.

[CODE]
RenderWorld
Flip
WaitKey
[/CODE]
You've flipped the buffers, so the sprites are now on the FrontBuffer(), not the BackBuffer()

[CODE]
; A tip(?)... ;-)

; Make your sprites parents of the camera, then you
; can position them relative to the ScreenWidth (in effect
; 1x1 texture pixels will equal 1x1 pixels on your monitor

kaart1 = LoadSprite ("kaart1.jpg",1+4+16+32,mycamera)
SpriteViewMode kaart1,3
;etc...

HandleSprite kaart1,-1,1
ScaleSprite kaart1,(0.5*ActualTextureWidthInPixels),(0.5*ActualHeightInPixels)
PositionEntity kaart1,2dScreenCoordsX-(GraphicsWidth()/2),-(2dScreenCoordsY-GraphicsHeight()/2),GraphicsWidth()

;...etc...


; this loop SHOULD do the following:

; chop the area from 0,0 to pieceSize,pieceSize in 16 tiles,

; place each tile (image) onto a texture, and place the texture on an entity (Sprite)

For y = 0 To 3
For x = 0 To 3
k.kaartstukje = New kaartstukje
k\entity = CreateSprite(Camera)

;Multiples of 16 - you can fake having different sizes
;by masking the Sprite's texture
k\texture=CreateTexture(pieceSize,pieceSize,1+16+32)

; There is no need to keep using SetBuffer().

CopyRect (x*pieceSize),(y*pieceSize),pieceSize,pieceSize,0,0,ImageBuffer(YourBuffer),TextureBuffer(k\texture)

DrawImage image,0,0
EntityTexture k\entity,k\texture

Next
Next

[/CODE]

Hope that helps, or at least gives you a couple of ideas - sorry I didn't have time to do something a bit more readable!


Jeroen(Posted 2003) [#4]
Hi Sainfohi and Ross,

Thank you very much for your code. It was indeed a problem caused by the texture size problem PLUS the lack of parenting to the camera. Those are nice ideas. Problem fixed!