Create image in memory

BlitzMax Forums/BlitzMax Programming/Create image in memory

TaskMaster(Posted 2007) [#1]
How could I draw from one image to another in memory?

I can do it using PixMaps and LockImage etc... But I was hoping there might be a faster method. Maybe some OpenGL command to change the backbuffer, or to write on a texture and copy it into a TImage afterwards?

Is there a way to make an image the target rather than the backbuffer, so I can use drawrect to draw on it?

Thanks for any hints pointing me in the right direction.


Perturbatio(Posted 2007) [#2]
apparently wglMakeCurrent can be used to use openGL drawing commands on a texture.

http://developer.nvidia.com/object/gdc_oglrtt.html


tonyg(Posted 2007) [#3]
pixmap.paste is a quick way of copying image1 (or a portion of it) into image2 but without alpha. It uses memcopy under the covers.
As for setting an image as the drawing target the it's back to looking for HighGfx and Indie's RTT modules.
Like streaming sounds, I still can't believe Mark hasn't added Render-To-texture/Imagebuffers.


Dreamora(Posted 2007) [#4]
Look for Indiepath Render2Texture.

Drawing directly Texture onto Texture is possible through pBuffer and other ways but you have to implement that on your own (if only opengl is of interest, Indiepaths module will work, just rip the DX part out thats broken due to the rewrite of the DX module in mid 1.24)

Reason is that Max2D is OpenGL 1.2, anything to draw onto is at least an Extension (-> pub.glew) and OpenGL 1.3


TaskMaster(Posted 2007) [#5]
Thanks for the tips guys. I don't mind it being OpenGL only.

I tried using pixmap.paste and it is sssllloooowwww...


tonyg(Posted 2007) [#6]
tried using pixmap.paste and it is sssllloooowwww...

Have you got the code you used as it shouldn't be thaaaatttttt slow.
Having said that it is a bit limited as it copies masked colours as well which 'blats' the target image.


TaskMaster(Posted 2007) [#7]
I am playing around with the topic GreyAlien had posted about.

With TileMaps, drawing all of the tiles to the screen at floating point coordinates you see glitches at the seams of the tiles. So, I tried pixmap pasting my tiles to a pixmap an drawing it to the screen.

Drawing 64 tiles 8x8 grid of 64pix X 48pix tiles.

With my work PC, kind of a lame video card...

I get 529 FPS drawing to the screen and about 60 fps drawing to a pixmap and drawing it to the screen.

Edit: Press P to change using or not using Pixmap.
Another edit: You can get the tile I used from here:

Here is the quick mock up:

SuperStrict

Global Tile:TImage
Global PTile:TPixmap
Global Map:TImage=CreateImage(640,480)
Global PMap:TPixmap

Global FPSCounter:Int
Global FPSTime:Int=MilliSecs()+1000
Global FPS:Int

Global bPix:Int

SetGraphicsDriver GLMax2DDriver()
Graphics (800,600,0,0)

Tile=LoadImage("Tile.png")
PTile=LockImage(Tile,0,True,False)

While Not KeyHit(KEY_ESCAPE)
	If KeyHit(KEY_P)	bPix=Not(bPix)
	If bPix=True
		PMap=LockImage(Map)
		For Local x:Int=1 To 8
			For Local y:Int=1 To 8
				PMap.Paste(PTile,x*64,y*48)
			Next
		Next
		UnlockImage(Map)
		DrawImage(Map,0,0)
	Else
		For Local x:Int=1 To 8
			For Local y:Int=1 To 8
				DrawImage(Tile,Float(x*64),Float(y*48))
			Next
		Next
	End If	
	FPSCounter:+1
 If FPSTime<MilliSecs() 'If it has been 1 second
  FPS=FPSCounter  'Set FPSNum to current count
  FPSTime=MilliSecs()+1000  'Set new time for next update
  FPSCounter=0  'Set counter back to 0
 End If
	DrawText("FPS: " + FPS,0,0)
	If bPix
		DrawText ("Using Pixmap",0,15)
	Else
		DrawText ("Not Using Pixmap",0,15)
	End If
	Flip;Cls
Wend
UnlockImage(Tile)




I will do it with Render2Texture when I have a spare moment and see how that works out.


TaskMaster(Posted 2007) [#8]
I gave this a shot using IndiePath's Render2Texture and I got about 220 FPS. Better than using PixMap, but not nearly as fast as just drawing the tiles right to the screen.

But it most definitely does solve the problem of seeing the seams between the tiles. It is a nice tile map scrolling around smoothly at floating point values.

Edit: I did a bit more testing. It runs that speed whether I draw a bunch of tiles to the texture or just one. So, that speed is just how quickly the texture is being handled and it make makes very little difference how many tiles I draw to the texture.


ninjarat(Posted 2007) [#9]
Have you seen the GLBlur sample that comes with BlitzMax? It's really fast and uses OpenGL render to texture.


TaskMaster(Posted 2007) [#10]
Thanks, I'll take a look at it.