Post by Adam on Dec 7, 2009 14:41:46 GMT -5
incomplete tga image loader, works relatively well with uncompressed (type 2) tga files
still working on run-length compression for type 10 but i am having some problems with that.
simple use loadtga(filename$) just like you would use loadtex(filename$)
GLSprite Library
something similiar to tom's sprite library that i made to get it to work before i ported it to C++ (taking my favorite part of basic4glwith me )
'Sprite Setting Functions
GLSpriteSetInit(real,real)
GLNewSprite(Sprite handle)
GLBindSprite(Sprite handle)
GLSprSetPos(real,real)
GLSprSetPosv(real())
GLSprSetX(real)
GLSprSetY(real)
GLSprSetSize(real,real)
GLSprSetSizev(real())
GLSprSetXSize(real)
GLSprSetYSize(real)
GLSprSetVel(real,real)
GLSprSetVelv(real())
GLSprSetXVel(real)
GLSprSetYVel(real)
GLSprSetAngle(real)
GLSprSetSpin(real)
GLSprSetTexture(Texture handle)
GLSprSetTextures(Texture handle array)
GLSprAddTexture(Texture handle)
GLSprAddTextures(Texture handle array)
GLSprSetAnimSpeed(real)
GLSprSetAnimLoop(bool)
GLSprSetFrame(int)
GLSprSetFrameCount(int)
GLSprSetColor(real,real,real)
GLSprSetAlpha(real)
GLSrSetFlip(bool,bool)
GLSprSetXFlip(bool)
GLSprSetYFlip(bool)
GLCopySprite(Sprite handle)
'Returns
GLSpriteInit
GLSpriteAreaWidth
GLSpriteAreaHeight
GLSprPos
GLSprX
GLSprY
GLSprTop
GLSprBottom
GLSprLeft
GLSprRight
GLSprSize
GLSprXSize
GLSprYSize
GLSprVel
GLSprXVel
GLSprYVel
GLSprAngle
GLSprSpin
GLSprTexture
GLSprTextures
GLSprAnimSpeed
GLSprAnimLoop
GLSprFrame
GLSprFrameCount
GLSprColor
GLSprAlpha
GLSprAnimDone
GLSprFlip
GLSprXFlip
GLSprYFlip
'Animation
GLAnimateSprite(Sprite handle)
GLAnimateSprites()
GLAnimateSpriteFrames(Sprite handle)
'Drawing Routines
DrawTriangle(Sprite handle)
DrawQuad(Sprite handle)
DrawTriangleStrip(Sprite handle)
DrawTriangleStrips()
DrawPointSprite(Sprite handle)
and lastly a demo program i made that uses both of the previous files including 'sample.tga' from crazynate's art studio
still working on run-length compression for type 10 but i am having some problems with that.
simple use loadtga(filename$) just like you would use loadtex(filename$)
'tga image loader header
Struc TGA_Header
&pixeldata()
idFeild
colorMapType
imageType
colorMapSpec(4)
imageSpec(6)
texture
pixelfields
gltype
File
r,g,b,a
End Struc
Const GL_BGRA = 0x80E1
Const GL_BGR = 0x80E0
Dim TGA_Header tgaloadingtemp
Dim __tgaIncrement
Sub ReadPacket(&bytecounter, byte)
if val(left$(byte,1)) = 1 Then
byte = val(right$(byte,len(byte)-1))
tgaloadingtemp.b = readbyte(tgaloadingtemp.file)
tgaloadingtemp.g = readbyte(tgaloadingtemp.file)
tgaloadingtemp.r = readbyte(tgaloadingtemp.file)
tgaloadingtemp.a = readbyte(tgaloadingtemp.file)
for __tgaIncrement = bytecounter to byte step 4
tgaloadingtemp.pixeldata(__tgaIncrement ) = tgaloadingtemp.b
tgaloadingtemp.pixeldata(__tgaIncrement+1) = tgaloadingtemp.g
tgaloadingtemp.pixeldata(__tgaIncrement+2) = tgaloadingtemp.r
tgaloadingtemp.pixeldata(__tgaIncrement+3) = tgaloadingtemp.a
next
bytecounter = bytecounter + byte*4
endif
EndSub
Function LoadTGA(filename$)
tgaloadingtemp.file = openfileread(filename$)
if fileerror() <> "" then return 0 endif
tgaloadingtemp.idfeild = readbyte(tgaloadingtemp.file)
tgaloadingtemp.colormaptype = readbyte(tgaloadingtemp.file)
tgaloadingtemp.imagetype = readbyte(tgaloadingtemp.file)
for __tgaincrement = 0 to 4
tgaloadingtemp.colormapspec(__tgaIncrement) = readbyte(tgaloadingtemp.file)
next
for __tgaIncrement = 0 to 3
tgaloadingtemp.imageSpec(__tgaIncrement) = readword(tgaloadingtemp.file)
next
for __tgaIncrement = 0 to 1
tgaloadingtemp.imageSpec(__tgaIncrement+4) = readbyte(tgaloadingtemp.file)
next
for __tgaIncrement = 0 to tgaloadingtemp.idfeild-1
readbyte(tgaloadingtemp.file)
next
if tgaloadingtemp.imagespec(4) = 32 then
tgaloadingtemp.pixelfields = 4
tgaloadingtemp.gltype = GL_BGRA
elseif tgaloadingtemp.imagespec(4) = 24 then
tgaloadingtemp.pixelfields = 3
tgaloadingtemp.gltype = GL_BGR
endif
if tgaloadingtemp.imagespec(0) = 0 Then tgaloadingtemp.imagespec(0) = 1 Endif
alloc tgaloadingtemp.pixeldata, tgaloadingtemp.imagespec(2)*tgaloadingtemp.imagespec(3)*tgaloadingtemp.pixelfields-1
if tgaloadingtemp.imagetype = 2 then
IF tgaloadingtemp.imagespec(1) > 0 Then
for __tgaIncrement = arraymax(tgaloadingtemp.pixeldata) to 0 Step -4
tgaloadingtemp.pixeldata(__tgaIncrement-3) = readbyte(tgaloadingtemp.file)
tgaloadingtemp.pixeldata(__tgaIncrement-2) = readbyte(tgaloadingtemp.file)
tgaloadingtemp.pixeldata(__tgaIncrement-1) = readbyte(tgaloadingtemp.file)
tgaloadingtemp.pixeldata(__tgaIncrement ) = readbyte(tgaloadingtemp.file)
next
Else
for __tgaIncrement = 0 To arraymax(tgaloadingtemp.pixeldata)
tgaloadingtemp.pixeldata(__tgaIncrement) = readbyte(tgaloadingtemp.file)
next
Endif
elseif tgaloadingtemp.imagetype = 10 then
dim bytecounter
while bytecounter <= arraymax(tgaloadingtemp.pixeldata)
readpacket(bytecounter,readbyte(tgaloadingtemp.file))
wend
endif
tgaloadingtemp.texture = glgentexture()
glbindtexture(GL_TEXTURE_2D, tgaloadingtemp.texture)
glTexImage2D(GL_TEXTURE_2D, 0, tgaloadingtemp.pixelfields,tgaloadingtemp.imagespec(2),tgaloadingtemp.imagespec(3), 0, tgaloadingtemp.gltype, GL_Unsigned_Byte, tgaloadingtemp.pixeldata)
glTexParameterF(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterF(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
'glTexParameterF(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_Linear)
'glTexParameterF(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_linear)
return tgaloadingtemp.texture
end function
GLSprite Library
something similiar to tom's sprite library that i made to get it to work before i ported it to C++ (taking my favorite part of basic4glwith me )
'Sprite Setting Functions
GLSpriteSetInit(real,real)
GLNewSprite(Sprite handle)
GLBindSprite(Sprite handle)
GLSprSetPos(real,real)
GLSprSetPosv(real())
GLSprSetX(real)
GLSprSetY(real)
GLSprSetSize(real,real)
GLSprSetSizev(real())
GLSprSetXSize(real)
GLSprSetYSize(real)
GLSprSetVel(real,real)
GLSprSetVelv(real())
GLSprSetXVel(real)
GLSprSetYVel(real)
GLSprSetAngle(real)
GLSprSetSpin(real)
GLSprSetTexture(Texture handle)
GLSprSetTextures(Texture handle array)
GLSprAddTexture(Texture handle)
GLSprAddTextures(Texture handle array)
GLSprSetAnimSpeed(real)
GLSprSetAnimLoop(bool)
GLSprSetFrame(int)
GLSprSetFrameCount(int)
GLSprSetColor(real,real,real)
GLSprSetAlpha(real)
GLSrSetFlip(bool,bool)
GLSprSetXFlip(bool)
GLSprSetYFlip(bool)
GLCopySprite(Sprite handle)
'Returns
GLSpriteInit
GLSpriteAreaWidth
GLSpriteAreaHeight
GLSprPos
GLSprX
GLSprY
GLSprTop
GLSprBottom
GLSprLeft
GLSprRight
GLSprSize
GLSprXSize
GLSprYSize
GLSprVel
GLSprXVel
GLSprYVel
GLSprAngle
GLSprSpin
GLSprTexture
GLSprTextures
GLSprAnimSpeed
GLSprAnimLoop
GLSprFrame
GLSprFrameCount
GLSprColor
GLSprAlpha
GLSprAnimDone
GLSprFlip
GLSprXFlip
GLSprYFlip
'Animation
GLAnimateSprite(Sprite handle)
GLAnimateSprites()
GLAnimateSpriteFrames(Sprite handle)
'Drawing Routines
DrawTriangle(Sprite handle)
DrawQuad(Sprite handle)
DrawTriangleStrip(Sprite handle)
DrawTriangleStrips()
DrawPointSprite(Sprite handle)
'GL sprite library
Struc GLSprite
Pos#(1)
Size#(1)
Velocity#(1)
Angle#
Spin#
Texture(99)
Color#(3)
FrameCount
Frame#
AnimSpeed#
AnimLoop
Flip#(1)
End Struc
Struc GLSpritePointer
glsprite &handle
End Struc
Dim GLSprite &BoundSprite
Dim __GLSprIncrement
Dim __GLSpriteInit
Dim __GLSpriteAreaWidth#
Dim __GLSpriteAreaHeight#
Dim __SpriteCount
Dim GLSpritepointer __Sprites(3000)
'''''''''
''Declaration of hidden routines
Declare Sub _TextureHandling(GLSprite &Temp)
Declare Sub _Translate(GLSprite &Temp)
Declare Sub _CheckErr()
'''''''''
Sub GLSpriteSetInit(width#,height#)
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glOrtho (0, width#, height#, 0, 1, -1)
glMatrixMode (GL_MODELVIEW)
__GLSpriteInit = True
__GLSpriteAreaWidth# = width#
__GLSpriteAreaHeight#= height#
End Sub
Sub GLNewSprite(GLSprite &Temp)
&__sprites(__spritecount).handle = &temp
IF not __spriteCount > arraymax(__sprites) + 1 Then
__spriteCount = __SpriteCount + 1
endif
&BoundSprite = &Temp
Temp.AnimLoop = True
Temp.Flip# = vec2(1,1)
Temp.Color# = vec4(1,1,1,1)
Temp.Size# = Vec2(32,32)
End Sub
Sub GLBindSprite(GLSprite &Temp)
&BoundSprite = &Temp
End Sub
' Basic Sprite Functions
Sub GLSprSetPos(X#,Y#)
BoundSprite.Pos# = Vec2(X#,Y#)
End Sub
Sub GLSprSetPosv(cord#())
BoundSprite.Pos# = cord#
End Sub
Sub GLSprSetX(X#)
BoundSprite.Pos#(0) = X#
End Sub
Sub GLSprSetY(Y#)
BoundSprite.Pos#(1) = Y#
End Sub
Sub GLSprSetSize(X#,Y#)
BoundSprite.Size# = Vec2(X#,Y#)
End Sub
Sub GLSprSetSizev(Cord#())
BoundSprite.Size# = Cord#
End Sub
Sub GLSprSetXSize(X#)
BoundSprite.Size#(0) = X#
End Sub
Sub GLSprSetYSize(Y#)
BoundSprite.Size#(1) = Y#
End Sub
Sub GLSprSetVel(X#,Y#)
BoundSprite.Velocity# = Vec2(X#,Y#)
End Sub
Sub GLSprSetVelv(cord#())
BoundSprite.Velocity# = cord#
End Sub
Sub GLSprSetXVel(X#)
BoundSprite.Velocity#(0) = X#
End Sub
Sub GLSprSetYVel(Y#)
BoundSprite.Velocity#(1) = Y#
End Sub
Sub GLSprSetAngle(Ang#)
BoundSprite.Angle# = Ang#
End Sub
Sub GLSprSetSpin(Ang#)
BoundSprite.Spin# = Ang#
End Sub
Sub GLSprSetTexture(Tex)
BoundSprite.Texture(0) = Tex
BoundSprite.FrameCount = 0
BoundSprite.Frame# = 0
End Sub
Sub GLSprSetTextures(Tex())
BoundSprite.Frame# = 0
BoundSprite.FrameCount = ArrayMax(Tex)
For __GLSprIncrement = 0 To BoundSprite.FrameCount
BoundSprite.Texture(__GLSprIncrement) = Tex(__GLSprIncrement)
Next
End Sub
Sub GLSprAddTexture(Tex)
BoundSprite.FrameCount = BoundSprite.FrameCount + 1
BoundSprite.Texture(BoundSprite.FrameCount) = Tex
End Sub
Sub GLSprAddTextures(Tex())
Dim Base = BoundSprite.FrameCount+1
BoundSprite.FrameCount = BoundSprite.FrameCount + ArrayMax(Tex)+1
For __GLSprIncrement = Base To BoundSprite.FrameCount
BoundSprite.Texture(__GLSprIncrement) = Tex(__GLSprIncrement-base)
Next
End Sub
Sub GLSprSetAnimSpeed(Speed#)
BoundSprite.AnimSpeed# = Speed#
End Sub
Sub GLSprSetAnimLoop(AnimLoop)
BoundSprite.AnimLoop = AnimLoop
End Sub
Sub GLSprSetFrame(Frame)
BoundSprite.Frame# = Frame
End Sub
Sub GLSprSetFrameCount(FrameCount)
BoundSprite.FrameCount = FrameCount
End Sub
Sub GLSprSetColor(r#,g#,b#)
BoundSprite.Color#(0) = r#
BoundSprite.Color#(1) = g#
BoundSprite.Color#(2) = b#
End Sub
Sub GLSprSetAlpha(a#)
BoundSprite.Color#(3) = a#
End Sub
Sub GLSprSetXFlip(x)
IF x = True Then
BoundSprite.Flip#(0) = -1
Else
BoundSprite.Flip#(0) = 1
Endif
End Sub
Sub GLSprSetYFlip(Y)
IF Y = True Then
BoundSprite.Flip#(1) = -1
Else
BoundSprite.Flip#(1) = 1
Endif
End Sub
Sub GLSprSetFlip(x,y)
GLSprSetXFlip(x)
GLSprSetYFlip(y)
End Sub
Sub GLCopySprite(glsprite &temp)
boundSprite = temp
End Sub
' Return Functions
Function GLSpriteInit()
Return __GLSpriteInit
End Function
Function GLSpriteAreaWidth() as single
Return __GLSpriteAreaWidth#
End Function
Function GLSpriteAreaHeight() as single
Return __GLSpriteAreaHeight#
End Function
Function GLSprPos()() as single
Return BoundSprite.Pos#
End Function
Function GLSprX() as single
Return BoundSprite.Pos#(0)
End Function
Function GLSprY() as single
Return BoundSprite.Pos#(1)
End Function
Function GLSprTop() as single
Return BoundSprite.Pos#(1)-BoundSprite.Size#(1)/2
End Function
Function GLSprBottom() as single
Return BoundSprite.Pos#(1)+BoundSprite.Size#(1)/2
End Function
Function GLSprLeft() as single
Return BoundSprite.Pos#(0)-BoundSprite.Size#(0)/2
End Function
Function GLSprRight() as single
Return BoundSprite.Pos#(0)+BoundSprite.Size#(0)/2
End Function
Function GLSprSize()() as single
Return BoundSprite.Size#
End Function
Function GLSprXSize() as single
Return BoundSprite.Size#(0)
End Function
Function GLSprYSize() as single
Return BoundSprite.Size#(1)
End Function
Function GLSprVel()() as single
Return BoundSprite.Velocity#
End Function
Function GLSprXVel() as single
Return BoundSprite.Velocity#(0)
End Function
Function GLSprYVel() as single
Return BoundSprite.Velocity#(1)
End Function
Function GLSprAngle() as single
Return BoundSprite.Angle#
End Function
Function GLSprSpin() as single
Return BoundSprite.Spin#
End Function
Function GLSprTexture()
Return BoundSprite.Texture(0)
End Function
Function GLSprTextures() ()
Dim Temp(BoundSprite.FrameCount)
For __GLSprIncrement = 0 To BoundSprite.FrameCount
Temp(__GLSprIncrement) = BoundSprite.Texture(__GLSprIncrement)
Next
Return Temp
End Function
Function GLSprAnimSpeed() as single
Return BoundSprite.AnimSpeed#
End Function
Function GLSprAnimLoop()
Return BoundSprite.AnimLoop
End Function
Function GLSprFrame()
Return Int(BoundSprite.Frame#)
End Function
Function GLSprFrameCount()
Return GLSprFrameCount()
End Function
Function GLSprColor()() as single
Return vec3(BoundSprite.Color#(0),BoundSprite.Color#(1),BoundSprite.Color#(2))
End Function
Function GLSprAlpha() as single
Return BoundSprite.Color#(3)
End Function
Function GLSprAnimDone()
Return (Not BoundSprite.AnimLoop And Int(BoundSprite.Frame#) = BoundSprite.FrameCount)
End Function
Function GLSprXFlip()
Return (BoundSprite.Flip#(0) < 0)
End Function
Function GLSprYFlip()
Return (BoundSprite.Flip#(1) < 0)
End Function
Function GLSprFlip()() as single
Return Vec2(GLSprXFlip(),GLSpryFlip())
End Function
'Drawing Routines
Sub DrawTriangle(GLSprite &Temp)
_CheckErr()
GLPushMatrix()
_Translate(Temp)
_TextureHandling(Temp)
GLBegin(GL_Triangles)
GLTexCoord2F(0,0):GLVertex2F(-.5,.5)
GLTexCoord2F(0,1):GLVertex2F(-.5,-.5)
GLTexCoord2F(1,.5):GLVertex2F(.5,0)
GLEnd()
GLPopMatrix()
End Sub
Sub DrawQuad(GLSprite &Temp)
_CheckErr()
GLPushMatrix()
_Translate(Temp)
_TextureHandling(Temp)
GLBegin(GL_Quads)
GLTexCoord2F(0,0):GLVertex2F(-.5,.5)
GLTexCoord2F(0,1):GLVertex2F(-.5,-.5)
GLTexCoord2F(1,1):GLVertex2F(.5,-.5)
GLTexCoord2F(1,0):GLVertex2F(.5,.5)
GLEnd()
GLPopMatrix()
End Sub
Sub DrawTriangleStrip(GLSprite &Temp)
_CheckErr()
GLPushMatrix()
_Translate(Temp)
_TextureHandling(Temp)
GLBegin(GL_Triangle_Strip)
GLTexCoord2F(0,0):GLVertex2F(-.5,.5)
GLTexCoord2F(0,1):GLVertex2F(-.5,-.5)
GLTexCoord2F(1,0):GLVertex2F(.5,.5)
GLTexCoord2F(1,1):GLVertex2F(.5,-.5)
GLEnd()
GLPopMatrix()
End Sub
Sub DrawTriangleStrips()
For __GLSprIncrement = 0 To __SpriteCount-1
DrawTriangleStrip(__Sprites(__GLSprIncrement).handle)
Next
End Sub
Sub DrawPointSprite(GLSprite &Temp)
_CheckErr()
GLEnable(34913)
GLPushMatrix()
_Translate(Temp)
GLPointSize(Temp.Size#(0)*2)
_TextureHandling(Temp)
GLTexEnvI(34913, 34914, GL_TRUE)
GLBegin(GL_Points)
GLVertex2F(0,0)
GLEnd()
GLPopMatrix()
GLDisable(34913)
End Sub
'Animations
Sub GLAnimateSpriteFrames(GLSprite &Temp)
Temp.Frame# = Temp.Frame# + Temp.AnimSpeed#
IF Int(Temp.Frame#) > Temp.FrameCount Then
IF Temp.AnimLoop Then
Temp.Frame# = 0
Else
Temp.Frame# = Temp.FrameCount
Endif
ElseIF Int(Temp.Frame#) < 0 Then
IF Temp.AnimLoop Then
Temp.Frame# = Temp.FrameCount
Else
Temp.Frame# = 0
Endif
Endif
End Sub
Sub GLAnimateSprite(GLSprite &Temp)
Temp.Pos# = Temp.Pos# + Temp.Velocity#
Temp.Angle# = Temp.Angle# + Temp.Spin#
GLAnimateSpriteFrames(Temp)
End Sub
Sub GLAnimateSprites()
For __GLSprIncrement = 0 To __SpriteCount-1
GLAnimateSprite(__Sprites(__GLSprIncrement).handle)
Next
End Sub
''''''''''''''''''''''''''''''''''''
''Hidden Routines
Sub _TextureHandling(GLSprite &Temp)
IF Not Temp.Texture(0) = 0 Then
GLEnable(GL_TEXTURE_2D)
IF Temp.FrameCount > 0 Then
GLBindTexture(GL_TEXTURE_2D,Temp.Texture(Int(Temp.Frame#)))
Else
GLBindTexture(GL_TEXTURE_2D,Temp.Texture(0))
Endif
Else
GLDisable(GL_TEXTURE_2D)
Endif
GLColor4Fv(Temp.Color#)
End Sub
Sub _Translate(GLSprite &Temp)
GLTranslateF(Temp.Pos#(0),Temp.Pos#(1),0)
GLRotateF(Temp.Angle#,0,0,1)
GLScaleF(Temp.Flip#(0)*Temp.Size#(0),Temp.Flip#(1)*Temp.Size#(1),0)
End Sub
Sub _CheckErr()
IF Not __GLSpriteInit Then
Print "GLSprites not initialized"
DrawText()
SwapBuffers()
Sleep(500)
End
Endif
End Sub
and lastly a demo program i made that uses both of the previous files including 'sample.tga' from crazynate's art studio
Include TGAHeader.inc
Include GLSpriteLibrary.inc
textmode(text_overlaid)
dim glsprite sprite
dim glsprite sprite2
glspritesetinit(640,480)
glnewsprite(sprite)
glnewsprite(sprite2)
'glclear(GL_COLOR_BUFFER_BIT)
'GLEnable(GL_Texture_2D)
'glbindtexture(GL_TEXTURE_2D,LoadTGA("data\sample"))
'gltranslatef(0,0,-1)
'Glbegin(GL_Quads)
' GLTexCoord2F(0,0):GLVertex2F(-.5,.5)
' GLTexCoord2F(0,1):GLVertex2F(-.5,-.5)
' GLTexCoord2F(1,1):GLVertex2F(.5,-.5)
' GLTexCoord2F(1,0):GLVertex2F(.5,.5)
'GLEnd()
'Swapbuffers()
glalphafunc(gl_greater, .9)
glenable(gl_alpha_test)
glsprsettexture(LoadTGA("data\sample.tga"))
glsprsetpos(352,240)
glsprsetsize(64,64)
glsprsetspin(1)
glbindsprite(sprite)
glcopysprite(sprite2)
glsprsetx(glsprx()-64)
glsprsetspin(-glsprspin())
do
glclear(GL_COLOR_BUFFER_BIT)
drawtrianglestrips()
GLAnimateSprites()
swapbuffers()
loop