Pixmaps

BlitzMax Forums/BlitzMax Beginners Area/Pixmaps

Can anyone explain how they work and how they differ from TImage?

I've only used TImages, however, getting into opengl it seems like to create a texture, Pixmaps are the easyest... Yet I'm having trouble trying to render a image:

SuperStrict
Framework pub.OpenGL
Import BRL.GLGraphics
Import BRL.Pixmap


Type TSprite
	Field img_tex:Int
	Field rot:Int

	Method InitImage:TSprite(img:TPixmap)
		img_tex = GLTexFromPixmap(img)
	End Method
	
	Method Render(x:Int,y:Int)
		glTranslatef(x, y, 0.0)
		glRotatef(rot, 0.0, 0.0, 1.0)
		glBindTexture(GL_TEXTURE_2D, img_tex)
		glBegin(GL_QUADS)
			glTexCoord2f(0.0, 0.0); glVertex2i(-10, -10)' top left
			glTexCoord2f(1.0, 0.0); glVertex2i( 10, -10)' top right
			glTexCoord2f(1.0, 1.0); glVertex2i( 10,  10)' bottom right
			glTexCoord2f(0.0, 1.0); glVertex2i(-10,  10)' bottom left
		glEnd()
	
		glLoadIdentity()
	End Method
End Type

'------------------------------------------------------------------------------

GLGraphics 1024, 768
InitalizeGL() 'initalize opengl stuff

Local x:Int = 300
Local y:Int = 300

Local mysprite:TSprite = LoadSprite("star.png")

'Main loop, quit on KEY_ESCAPE
While Not KeyDown(KEY_ESCAPE)
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) 'Cls but opengl
		
	If KeyDown(KEY_LEFT) x:-1
	If KeyDown(KEY_RIGHT) x:+1
	DrawSprite(mysprite, x, y)

	Flip 1
Wend
End

Function InitalizeGL()
	glShadeModel(GL_SMOOTH)
	glEnable(GL_TEXTURE_2D)
	glEnable(GL_BLEND)
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
	
	glViewport(0,0,1024,768)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	glOrtho(0.0,1024,768,0.0,-1.0,1.0)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
End Function

Function LoadSprite:TSprite(L:String)
	Local pm:TPixmap = LoadPixmap L
	If Not pm Return
	Sprite:TSprite = New TSprite
	Sprite.InitImage(pm)
	Return Sprite
End Function

Function DrawSprite(s:TSprite, x:Int, y:Int)
	If s = Null Return
	
	s.Render(x,y)
End Function


I'm getting some wierd errors trying to load the pixmap. Am I doing it right?

A pixmap is an area of system memory which describes the colour and alpha of each pixel in the image.
To draw a pixmap requires the data to be copied from system memory into video memory as a surface each time which is not quick.
When you load an image file it still creates a pixmap. However, when you drawimage the pixmap data is copied to a surface, displayed and the surface kept in video memory. The next time you drawimage it reuses that surface which saves the time to copy from system to video memory.
The downside is you cannot directly changes the pixels of an image.
This is done within the pixmap.
<edit>
As for your code, I am not sure what you're trying to do or why but this works (sort of)...
SuperStrict

Type TSprite
	Field img_tex:Int
	Field rot:Int

	Method InitImage:TSprite(img:TPixmap)
		img_tex = GLTexFromPixmap(img)
	End Method
	
	Method Render(x:Int,y:Int)
		glTranslatef(x, y, 0.0)
		glRotatef(rot, 0.0, 0.0, 1.0)
		glBindTexture(GL_TEXTURE_2D, img_tex)
		glBegin(GL_QUADS)
			glTexCoord2f(0.0, 0.0); glVertex2i(-10, -10)' top left
			glTexCoord2f(1.0, 0.0); glVertex2i( 10, -10)' top right
			glTexCoord2f(1.0, 1.0); glVertex2i( 10,  10)' bottom right
			glTexCoord2f(0.0, 1.0); glVertex2i(-10,  10)' bottom left
		glEnd()
	
		glLoadIdentity()
	End Method
End Type

'------------------------------------------------------------------------------

GLGraphics 1024, 768
InitalizeGL() 'initalize opengl stuff

Local x:Int = 300
Local y:Int = 300

Local mysprite:TSprite = LoadSprite("max.png")

'Main loop, quit on KEY_ESCAPE
While Not KeyDown(KEY_ESCAPE)
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) 'Cls but opengl
		
	If KeyDown(KEY_LEFT) x:-1
	If KeyDown(KEY_RIGHT) x:+1
	DrawSprite(mysprite, x, y)

	Flip 1
Wend
End

Function InitalizeGL()
	glShadeModel(GL_SMOOTH)
	glEnable(GL_TEXTURE_2D)
	glEnable(GL_BLEND)
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
	
	glViewport(0,0,1024,768)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	glOrtho(0.0,1024,768,0.0,-1.0,1.0)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
End Function

Function LoadSprite:TSprite(L:String)
	Local pm:TPixmap = LoadPixmap(L)
	If Not pm RuntimeError "No file found"
	Local Sprite:TSprite = New TSprite
	Sprite.InitImage(pm)
	Return Sprite
End Function

Function DrawSprite(s:TSprite, x:Int, y:Int)
	If s = Null Return
	
	s.Render(x,y)
End Function



cool, thanks!