Scrolling an Image
BlitzMax Forums/BlitzMax Programming/Scrolling an Image
| ||
I have several tiles in a tile based RPG and would like to animate them by scrolling them up, down, left, or right. So shift them x pixels in one direction and the pixels that fall of that direction move into the image on the other side. How can this be done. Thanks |
| ||
I use this for DX : I'm guessing the principle is the same for GL which might even be set to wrap by default in Bmax.<edit> GL doesn't wrap by default so you'd need to find the OGL equivalent to the WRAP parm. |
| ||
What I did is when a tile goes ofscreen on the right (just an example) that the same tile should be drawn left from the tile which is most left to fill up the empty space. I used the same trick for moving objects so that if a monster goes out of the screen on one side I just draw it twice temporary on the other side. Here is a tiny example which only checks if the object goes ofscreen on the right and if so it draws it twice : Strict Global width:Int=640 Global height:Int=480 Global posX:Int=320 Global posY:Int=240 Graphics width,height,0 While Not KeyHit(Key_Escape) Cls posX = posX + 1 If posX > width + 50 Then posX = posX - width DrawOval posX - 50,posY - 50,100,100 If posX > width - 50 DrawOval posX - 50 - width,posY - 50,100,100 EndIf Flip Wend End I think it is faster than trying to capture every pixel which goes ofscreen on one side and draw them on the other side. |
| ||
Thanks tonyg. I am on OS X so the DX stuff isnt't too helpful. Also not much into Open GL. Ideally I would just have a function that takes a TImage, a direction, and the number of pixels and does the roll for me. Mark, I appreciate the response but I didn't phrase my initial query very well. I am talking about scrolling the graphic tile itself, the TImage. More specifically in an Ultima IV type game where the water tiles continuously scroll, or the energy fields etc. My current implementation uses a source TImage that is a 3 x 3 matrix of the tile I wish to scroll. Then when I display the normal size tile on screen I just pull from an offset location in the 3 x 3 matrix. The result is identical but I was hoping for something more elegant. |
| ||
How about something like this:SuperStrict Graphics 640,480 SetClsColor 100,100,100 Cls Local img1:TImage = LoadImage("test.png") Local time:Int = MilliSecs() Local img2:TImage = offsetImage(img1,32,32) Print "processtime was: "+(MilliSecs()-time)+"ms" DrawImage(img1,0,0) DrawImage(img2,256,0) Flip WaitKey() End Function offsetImage:TImage(source:TImage,xOffset:Int,yOffset:Int) Local scrPix:TPixmap = LockImage(source) Local tmpPix:TPixmap = CopyPixmap(scrPix) Local scrPtr:Byte Ptr = PixmapPixelPtr(scrPix,0,0) Local tmpPtr:Byte Ptr = PixmapPixelPtr(tmpPix,0,0) Local pitch:Int = scrPix.width Local w:Int = scrPix.width Local h:Int = scrPix.height Local x:Int,y:Int Local tx:Int,ty:Int For x = 0 To w-1 For y = 0 To h-1 tx = xOffset+x ty = yOffset+y If tx => w Then tx :- w If ty => h Then ty :- h If tx < 0 Then tx :+ w If ty < 0 Then ty :+ h tmpPtr[x*4+y*4*pitch] = scrPtr[tx*4+ty*4*pitch] tmpPtr[x*4+y*4*pitch+1] = scrPtr[tx*4+ty*4*pitch+1] tmpPtr[x*4+y*4*pitch+2] = scrPtr[tx*4+ty*4*pitch+2] tmpPtr[x*4+y*4*pitch+3] = scrPtr[tx*4+ty*4*pitch+3] Next Next Return LoadImage(tmpPix) End Function |
| ||
Isn't it better to delcare your variables as close as possible to their first usage? |
| ||
zawran, I guess that looks good. I will try it tonight. I haven't delved into pixmaps at all but your code looks pretty straightforward. Thanks, Jim |
| ||
zawran, Just tested it and have my water tiles etc. scrolling nicely. Exactly what I wanted. Bonus is it will help me learn pixmaps. Thanks a million. Jim |
| ||
No problem, I am glad you found it useful. |
| ||
Mark, I appreciate the response but I didn't phrase my initial query very well. I am talking about scrolling the graphic tile itself, the TImage. Misunderstood you there then. Pixmap is one way to handle this stuff and @ zawran, nice example. It's easy to change the direction also if needed. I still would prefer myself to create an animation if the animation would repeat itself. I can imagine pixmap could use up much CPU when the whole screen is animated in a high resolution. |