Author Topic: [BMAX] shift pixmaps  (Read 4191 times)

0 Members and 1 Guest are viewing this topic.

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
[BMAX] shift pixmaps
« on: February 10, 2008 »
I just did some code to shift pixmaps as an example for someone on the blitzmax.com forum. Perhaps its of some use for those of you dabbling in bmax.

Code: [Select]
Graphics 800,600

Local pmap:TPixmap = LoadPixmap("img.png")
Local image:TImage = LoadImage("img.png",0)

Local time:Int = 0
Local utime:Int = 0

Local fps:Int = 0
Local fpstmp:Int = 0
Local update:Int = MilliSecs()

While Not KeyHit(KEY_ESCAPE)

Cls
DrawImage(image,0,0)
DrawText("Current FPS: "+fps,8,520)
DrawText("Last image update time in millisecs: "+utime,8,540)
Flip False

If KeyHit(KEY_LEFT) Then
time = MilliSecs()
image=shiftPmapLeft(pmap,32)
utime = MilliSecs()-time
End If
If KeyHit(KEY_RIGHT) Then
time = MilliSecs()
image=shiftPmapRight(pmap,32)
utime = MilliSecs()-time
End If
If KeyHit(KEY_UP) Then
time = MilliSecs()
image=shiftPmapUp(pmap,32)
utime = MilliSecs()-time
End If
If KeyHit(KEY_DOWN) Then
time = MilliSecs()
image=shiftPmapDown(pmap,32)
utime = MilliSecs()-time
End If

fpstmp :+ 1
If MilliSecs() > update+1000 Then
update = MilliSecs()
fps = fpstmp
fpstmp = 0
End If

Wend
End

Function shiftPmapLeft:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapWidth(pix)*4
Local moveSize:Int = (PixmapWidth(pix)-shift)*4
Local offset:Int = shift*4
For Local y:Int = 0 To PixmapHeight(pix)-1
MemCopy(pixPtr,pixPtr+offset,moveSize)
pixPtr :+ pitch
Next
Return LoadImage(pix,0)
End Function

Function shiftPmapRight:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapWidth(pix)*4
Local moveSize:Int = (PixmapWidth(pix)-shift)*4
Local offset:Int = shift*4
For Local y:Int = 0 To PixmapHeight(pix)-1
MemCopy(pixPtr+offset,pixPtr,moveSize)
pixPtr :+ pitch
Next
Return LoadImage(pix,0)
End Function

Function shiftPmapUp:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapWidth(pix)*4
For Local y:Int = 0 To PixmapHeight(pix)-1-shift
MemCopy(pixPtr,pixPtr+pitch*shift,pitch)
pixPtr :+ pitch
Next
Return LoadImage(pix,0)
End Function

Function shiftPmapDown:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
pixPtr :+ ((PixmapWidth(pix)*4)*(PixmapHeight(pix)-1))
Local pitch:Int = PixmapWidth(pix)*4
For Local y:Int = 0 To PixmapHeight(pix)-1-shift
MemCopy(pixPtr,pixPtr-(pitch*shift),pitch)
pixPtr :- pitch
Next
Return LoadImage(pix,0)
End Function

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [BMAX] shift pixmaps
« Reply #1 on: May 20, 2009 »
Sorry buddy But I just recently tried your code.

I have a properly named "img.png" file but upon pressing the buttons it exits the window. Left press is a special case that throws a message stating that there is an unhandled memory exception error. This looked interesting too.
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [BMAX] shift pixmaps
« Reply #2 on: May 20, 2009 »
For some unknown reason I must only have tested the code on 32bit images, and some of the code is hardcoded for the pixel data size of these, so it breaks on others like 24bit images. I have made a few changes and it should work on those as well now.

Code: [Select]
Graphics 800,600

Local pmap:TPixmap = LoadPixmap("img.png")
Local image:TImage = LoadImage("img.png",0)

Local time:Int = 0
Local utime:Int = 0

Local fps:Int = 0
Local fpstmp:Int = 0
Local update:Int = MilliSecs()

While Not KeyHit(KEY_ESCAPE)

Cls
DrawImage(image,0,0)
DrawText("Current FPS: "+fps,8,520)
DrawText("Last image update time in millisecs: "+utime,8,540)
Flip False

If KeyHit(KEY_LEFT) Then
time = MilliSecs()
image=shiftPmapLeft(pmap,32)
utime = MilliSecs()-time
End If
If KeyHit(KEY_RIGHT) Then
time = MilliSecs()
image=shiftPmapRight(pmap,32)
utime = MilliSecs()-time
End If
If KeyHit(KEY_UP) Then
time = MilliSecs()
image=shiftPmapUp(pmap,32)
utime = MilliSecs()-time
End If
If KeyHit(KEY_DOWN) Then
time = MilliSecs()
image=shiftPmapDown(pmap,32)
utime = MilliSecs()-time
End If

fpstmp :+ 1
If MilliSecs() > update+1000 Then
update = MilliSecs()
fps = fpstmp
fpstmp = 0
End If

Wend
End

Function shiftPmapLeft:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapPitch(pix)
Local pixSize:Int = pitch/PixmapWidth(pix)
Local moveSize:Int = (PixmapWidth(pix)-shift)*pixSize
Local offset:Int = shift*pixSize
For Local y:Int = 0 To PixmapHeight(pix)-1
MemCopy(pixPtr,pixPtr+offset,moveSize)
pixPtr :+ pitch
Next
Return LoadImage(pix,0)
End Function

Function shiftPmapRight:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapPitch(pix)
Local pixSize:Int = pitch/PixmapWidth(pix)
Local moveSize:Int = (PixmapWidth(pix)-shift)*pixSize
Local offset:Int = shift*pixSize
For Local y:Int = 0 To PixmapHeight(pix)-1
MemCopy(pixPtr+offset,pixPtr,moveSize)
pixPtr :+ pitch
Next
Return LoadImage(pix,0)
End Function

Function shiftPmapUp:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapPitch(pix)
Local pixSize:Int = pitch/PixmapWidth(pix)
For Local y:Int = 0 To PixmapHeight(pix)-1-shift
MemCopy(pixPtr,pixPtr+pitch*shift,pitch)
pixPtr :+ pitch
Next
Return LoadImage(pix,0)
End Function

Function shiftPmapDown:TImage(pix:TPixmap,shift:Int)
Local pixPtr:Byte Ptr = PixmapPixelPtr(pix)
Local pitch:Int = PixmapPitch(pix)
Local pixSize:Int = pitch/PixmapWidth(pix)
pixPtr :+ ((PixmapWidth(pix)*pixSize)*(PixmapHeight(pix)-1))
For Local y:Int = 0 To PixmapHeight(pix)-1-shift
MemCopy(pixPtr,pixPtr-(pitch*shift),pitch)
pixPtr :- pitch
Next
Return LoadImage(pix,0)
End Function

http://zac-interactive.dk/temp/shiftpixmap.zip

[EDIT] added link to exe
« Last Edit: May 20, 2009 by zawran »

Offline spitfire

  • Amiga 1200
  • ****
  • Posts: 275
  • Karma: 9
    • View Profile
Re: [BMAX] shift pixmaps
« Reply #3 on: May 20, 2009 »
Can u include an exe plz, cuz I dont have blitz.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [BMAX] shift pixmaps
« Reply #4 on: May 26, 2009 »
Very nice, I finally got back from vacation and tried it.
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: [BMAX] shift pixmaps
« Reply #5 on: May 27, 2009 »
Vacation, nice, unfortunately I have to wait until August before I have vacation time again :(

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: [BMAX] shift pixmaps
« Reply #6 on: May 27, 2009 »


I assume something like this could be done for dynamic fake low res displays? Writing from pixmap sprites to a small pixmap back buffer then drawing a scaled result? I'm thinking that with some screen wrapping you could do a dynamic sine distortion effect. I may be off however but you know your pixmap pointer stuff.  :kewl:
Challenge Trophies Won: