[bmx] unofficial glmax2d by slenkar [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : unofficial glmax2d
Author : slenkar
Posted : 1+ years ago

Description : A very simple (but almost feature complete)
version of glmax2d.
anyone can send me changes to the code and I will add them here.

Features:
1.You can place the image vertices where ever you want with the
setimagevertices command

changes this loaded image:

into this in the game:


2.draw tint text
use vertex colors to shade text

3.compatible with MaxGUI canvas and glsharecontexts

4.grabimage is a lot faster than normal glmax2d

5.hollow rect and hollow circle

6.adjust vertex colors of rectangles

7. parent an image to another
it will rotate and move with the parent image

This will be a library that takes advantage of cool OpenGL

features that so far have not been added to the official

max2d due to DirectX not supporting those functions.

If interest is shown in this library:
1.grabimagefast will be implemented -done
2.texture splatting/masking??
3.image parent-child -done
4.choose to upload texture to video ram or just ram??
5.delete image?

problems/issues
1.Due to the fact that you can place vertices anywhere the handle commands are kind of obsolete - (but are still there for rectangle images)
2.No imagecollides function, I have some collision code that I 'borrowed'.
3.Instead of using TTF files for fonts,bitmap fonts are used.
 a bitmap font creator program is also included.
The font's png and xml files must be in the same directory and have the same name.
4.You will need to fix glgraphics.bmx with this:
<a href="../Community/posts15bc.html?topic=73299" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=73299</a>


Code :
Code (blitzmax) Select
' Slenkars unofficial GL Graphics

Rem
' NOTE: open window with:
  InitGL width,height [,depth=0][,fps=30]
EndRem

Strict
Module keef.gldraw
Import bah.libxml
Import BRL.LinkedList
Import BRL.PngLoader
Import BRL.JpgLoader
Import Pub.OpenGL
Import BRL.GLGraphics
Import PUB.Glew

Const MASKBLEND=1
Const SOLIDBLEND=2
Const ALPHABLEND=3
Const LIGHTBLEND=4
Const SHADEBLEND=5

'flags for frames/images
Const MASKEDIMAGE= $1
Const FILTEREDIMAGE= $2
Const MIPMAPPEDIMAGE= $4
Const DYNAMICIMAGE= $8

Type font_char
Field x#,y#,width#,height#,id
Field vertex[9]
Field pixel_width#,pixel_height#
Field yoffset,xoffset,xadvance
EndType

Type image_font
Global list:TList=New TList
Method New()
list.AddLast Self
EndMethod
Field tex:TImage
Field image_width#
Field image_height#
Field name$
Field backgroundr
Field backgroundg
Field backgroundb
Field char_array:font_char[256]
Field height
Field font_height
EndType


Type TImage
Field num,height#,width#
Field handlex,handley
Field path$
Field vertex#[9]
Field animx#[]
Field animy#[]
Field cell_width#
Field cell_height#
Field real_cell_width#
Field real_cell_height#
Field cell_count
Field child_x,child_y
Field child_rotation
Field child_list:TList
Field child_frame
Field parent:TImage
Method ResetVerts()
Local iw#=width , ih#=height
If cell_count>0
iw=cell_width ; ih=cell_height
EndIf
vertex[0]=0 ; vertex[1]=0
vertex[2]=iw ; vertex[3]=0
vertex[4]=iw ; vertex[5]=ih
vertex[6]=0 ; vertex[7]=ih
End Method
EndType

Global rotation=0
Global scalex#=1.0
Global scaley#=1.0
Global colorr=255
Global colorg=255
Global colorb=255
Global maskcolorr=0
Global maskcolorg=0
Global maskcolorb=0
Global globalalpha#
Global imagefont:image_font
Global glzoom#

Function InitGL(xsize,ysize,screendepth=0,fps=30)
GLGraphics(xsize,ysize,ScreenDepth,fps,GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER) '| BGL_FULLSCREEN
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glClearColor(0, 0, 0, 0)
glOrtho (0, XSize, YSize, 0, 0, 1)
glDisable(GL_DEPTH_TEST)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0.375, 0.375, 0)
glEnable(GL_BLEND)
glEnable(GL_TEXTURE_2D)
glenable(GL_SCISSOR_TEST)
glscissor(0,0,xsize,ysize)
glblendfunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
SetAlpha 1.0
Cls ; Flip ; Cls
EndFunction


Function setfont(i:image_font)
imagefont=i
EndFunction


Function GetScale(var1 Var,var2 Var)
var1=scalex ; var2=scaley
EndFunction

Function GetRotation()
Return rotation
EndFunction

Global state_blend
Global state_boundtex
Global state_texenabled

Function EnableTex(name)
BindTex name
If state_texenabled Return
glEnable GL_TEXTURE_2D
state_texenabled=True
End Function

Function DisableTex()
If Not state_texenabled Return
glDisable GL_TEXTURE_2D
state_texenabled=False
End Function

Function BindTex( name )
If name=state_boundtex Return
glBindTexture GL_TEXTURE_2D,name
state_boundtex=name
End Function

Function SetZoom(zoom#)
glzoom=zoom
EndFunction

Function DrawImage(img:TImage,x,y,frame=0)
EnableTex img.num
gltranslatef(x,y,0)
glrotatef(rotation,0,0,1)
glscalef(scalex,scaley,1)
If img.cell_count=0
glBegin(GL_QUADS)
glTexCoord2f(0,0)
glVertex2f(img.vertex[0],img.vertex[1] )
glTexCoord2f(1,0)
glVertex2f(img.vertex[2], img.vertex[3])
glTexCoord2f(1,1)
glVertex2f(img.vertex[4], img.vertex[5])
glTexCoord2f(0,1)
glVertex2f(img.vertex[6], img.vertex[7])
glend
EndIf
If img.cell_count>0
Local i:TImage=TImage(img)
glBegin(GL_QUADS)
glTexCoord2f(i.animx[frame],i.animy[frame])
glVertex2f(i.vertex[0],i.vertex[1] )
glTexCoord2f(i.animx[frame]+i.cell_width,i.animy[frame])
glVertex2f(i.vertex[2], i.vertex[3])
glTexCoord2f(i.animx[frame]+i.cell_width,i.animy[frame]+i.cell_height)
glVertex2f(i.vertex[4], i.vertex[5])
glTexCoord2f(i.animx[frame],i.animy[frame]+i.cell_height)
glVertex2f(i.vertex[6], i.vertex[7])
glend
EndIf
If img.child_list<>Null
If img.child_list.isempty()=False
For Local j:TImage=EachIn img.child_list
DrawChildImage(j,x,y,rotation)
Next
EndIf
EndIf
glloadidentity()
gltranslatef(0.375,0.375,0)
EndFunction

Function DrawChildImage(j:TImage,parentx,parenty,parentrot)
glloadidentity()
gltranslatef(0.375,0.375,0)
gltranslatef(parentx,parenty,0)
glrotatef(parentrot,0,0,1)
gltranslatef(j.child_x,j.child_y,0)
glrotatef(j.child_rotation,0,0,1)
If j.cell_count=0
glBegin(GL_QUADS)
glTexCoord2f(0,0)
glVertex2f(j.vertex[0],j.vertex[1] )
glTexCoord2f(1,0)
glVertex2f(j.vertex[2], j.vertex[3])
glTexCoord2f(1,1)
glVertex2f(j.vertex[4], j.vertex[5])
glTexCoord2f(0,1)
glVertex2f(j.vertex[6],j.vertex[7])
glend
EndIf
If j.cell_count>0
glBegin(GL_QUADS)
glTexCoord2f(j.animx[j.child_frame],j.animy[j.child_frame])
glVertex2f(j.vertex[0],j.vertex[1] )
glTexCoord2f(j.animx[j.child_frame]+j.cell_width,j.animy[j.child_frame])
glVertex2f(j.vertex[2], j.vertex[3])
glTexCoord2f(j.animx[j.child_frame]+j.cell_width,j.animy[j.child_frame]+j.cell_height)
glVertex2f(j.vertex[4], j.vertex[5])
glTexCoord2f(j.animx[j.child_frame],j.animy[j.child_frame]+j.cell_height)
glVertex2f(j.vertex[6], j.vertex[7])
glend
EndIf
If j.child_list<>Null
If j.child_list.isempty()=False
For Local i:TImage=EachIn j.child_list
drawchildimage(i,j.child_X,j.child_y,j.child_rotation)
Next
EndIf
EndIf
EndFunction


Function DrawOval(x1,y1,width#,height#,solid=True)
If solid
DisableTex
glBegin(GL_TRIANGLE_FAN)
Else
glBegin(GL_LINE_LOOP)
EndIf
gltranslatef(x1,y1,0)
glrotatef(rotation,0,0,1)
width:*0.5 ; height:*0.5
For Local angle# = 0 To 360 Step 10
glVertex2f(x1+Sin(angle)*width,y1+Cos(angle)*height)
Next
glEnd()
gltranslatef(0.375,0.375,0)
EndFunction

Function DrawLine(x1,y1,x2,y2)
glBegin(GL_LINES)
glVertex2f(x1,y1) ; glVertex2f(x2,y2)
glEnd()
EndFunction

Function DrawRect(posx#,posy#,width#,height#,solid=True)
gltranslatef(posx,posy,0)
glrotatef(rotation,0,0,1)
If solid
DisableTex
glBegin(GL_QUADS)
Else
glBegin(GL_LINE_LOOP)
EndIf
glVertex2f 0,0 ; glVertex2f width,0
glVertex2f width,height ; glVertex2f 0,height
glEnd()
glloadidentity()
gltranslatef(0.375,0.375,0)
End Function

Function DrawShadeRect( posx#,posy#,width#,height#,r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4)
DisableTex
gltranslatef(posx,posy,0)
glrotatef(rotation,0,0,1)
glBegin(GL_QUADS)
SetColor (r1,g1,b1)
glVertex2f 0,0
SetColor (r2,g2,b2)
glVertex2f width,0
SetColor (r3,g3,b3)
glVertex2f width,height
SetColor (r4,g4,b4)
glVertex2f 0,height
glend
glloadidentity()
gltranslatef(0.375,0.375,0)
End Function

Function DrawTintText(txt$,x,y,tintR,tintG,tintB)
If tintR>255
tintR=10 ; tintG=10 ; tintB=10
EndIf
EnableTex imagefont.tex.num
Local colorrr=colorr
Local colorgg=colorg
Local colorbb=colorb
Local x_place=x
Local highest_char
For x=1 To txt.length
Local char$=txt[x-1..x]
Local f:font_char=imagefont.char_array[Asc(char)]
If f<>Null
If f.pixel_height>highest_char
highest_char=f.pixel_height
EndIf
EndIf
Next
For x=1 To txt.length
Local char$= txt[x-1..x]
Local f:font_char=imagefont.char_array[Asc(char)]
If f<>Null
'Local y_place=y+((highest_char-f.pixel_height)+f.yoffset)
Local y_place=y+(f.yoffset*scaley)
gltranslatef(x_place,y_place,0)
glrotatef(rotation,0,0,1)
glscalef(scalex,scaley,1)
glBegin(GL_QUADS)
SetColor(colorrr,colorgg,colorbb)
glTexCoord2f(f.x,f.y)
glVertex2f(f.vertex[0],f.vertex[1] )
glTexCoord2f(f.x+f.width,f.y)
glVertex2f(f.vertex[2], f.vertex[3])
SetColor(tintr,tintg,tintb)
glTexCoord2f(f.x+f.width,f.y+f.height)
glVertex2f(f.vertex[4], f.vertex[5])
glTexCoord2f(f.x,f.y+f.height)
glVertex2f(f.vertex[6], f.vertex[7])
glend
glloadidentity()
gltranslatef(0.375,0.375,0)
x_place:+((f.xadvance*scalex))
x_place=x_place+(-f.xoffset*scalex)
EndIf
Next
SetColor(colorrr,colorgg,colorbb)
EndFunction

Function DrawText(txt$,x,y)
If imagefont=Null
GLDrawText txt,x,y
Return
'RuntimeError "No font is set yet, use SetImageFont"
EndIf
EnableTex imagefont.tex.num
Local colorrr=colorr
Local colorgg=colorg
Local colorbb=colorb
Local x_place=x
Local highest_char
For x=1 To txt.length
Local char$=txt[x-1..x]
Local f:font_char=imagefont.char_array[Asc(char)]
If f<>Null
If f.pixel_height>highest_char
highest_char=f.pixel_height
EndIf
EndIf
Next
For x=1 To txt.length
Local char$= txt[x-1..x]
Local f:font_char=imagefont.char_array[Asc(char)]
If f<>Null
'Local y_place=y+((highest_char-f.pixel_height)+f.yoffset)
Local y_place=y+(f.yoffset*scaley)
gltranslatef(x_place,y_place,0)
glrotatef(rotation,0,0,1)
glscalef(scalex,scaley,1)
glBegin(GL_QUADS)
SetColor(colorrr,colorgg,colorbb)
glTexCoord2f(f.x,f.y)
glVertex2f(f.vertex[0],f.vertex[1] )
glTexCoord2f(f.x+f.width,f.y)
glVertex2f(f.vertex[2], f.vertex[3])
glTexCoord2f(f.x+f.width,f.y+f.height)
glVertex2f(f.vertex[4], f.vertex[5])
glTexCoord2f(f.x,f.y+f.height)
glVertex2f(f.vertex[6], f.vertex[7])
glEnd
glloadidentity()
gltranslatef(0.375,0.375,0)
x_place:+((f.xadvance*scalex))
x_place=x_place+(-f.xoffset*scalex)
EndIf
Next
SetColor(colorrr,colorgg,colorbb)
EndFunction

'Function GrabImage:TImage(i:TImage,x,y)
'glBindTexture GL_TEXTURE_2D,i.num
'glCopyTexSubImage2D GL_TEXTURE_2D,0,0,0,x,y,width,height
'glCopyTexsubImage2D(GL_TEXTURE_2D, 0,0, 0,x, GraphicsHeight()-i.height-y, i.width, i.height)
'End Function

Function GrabImage:TImage(x,y,width,height)
Local i:TImage=CreateImage(width,height)
glBindTexture GL_TEXTURE_2D,i.num
glCopyTexSubImage2D GL_TEXTURE_2D,0,0,0,x,GraphicsHeight()-i.height-y,width,height
Return i
End Function

Function CreateImage:TImage(width,height)
Local i:TImage=New TImage
i.width=Width
i.height=Height
i.ResetVerts()
glGenTextures 1,Varptr i.num
BindTex i.num
'set texture parameters
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST
Local mip_level
glTexImage2D GL_TEXTURE_2D,mip_level,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
Return i
EndFunction

Function LoadImage:TImage(url:String)
If FileType(url)=1
Local pix:TPixmap=LoadPixmap(url)
If pix=Null
RuntimeError "pixmap is null: "+url
EndIf
pix=MaskPixmap(pix,maskcolorr,maskcolorg,maskcolorb)
Local tex=GLTexFromPixmap(pix)
Local i:TImage=New TImage
i.num=tex ; i.path=url
i.width=PixmapWidth(pix)
i.height=PixmapHeight(pix)
i.ResetVerts()
Return i
Else
RuntimeError "Image not found: "+url
EndIf
EndFunction

Function LoadAnimImage:TImage(url:Object,cell_width,cell_height,first_cell,cell_count,flags=MASKEDIMAGE)
Local pix:TPixmap
If String(url)
Local ur:String=String(url)
If FileType(ur)=1
pix:TPixmap=LoadPixmap(ur)
Else
RuntimeError "Image not found: "+ur
EndIf
EndIf
If TPixmap(url)
Local ur:TPixmap=TPixmap(url)
pix=ur
EndIf
pix=MaskPixmap(pix,maskcolorr,maskcolorg,maskcolorb)
Local tex=GLTexFromPixmap(pix)
Local i:TImage=New TImage
i.num=tex
If String(url)
Local ur:String=String(url)
i.path=ur
EndIf
i.width=PixmapWidth(pix)
i.height=PixmapHeight(pix)
i.animx=i.animx[..cell_count]
i.animy=i.animy[..cell_count]
i.real_cell_width=cell_width
i.real_cell_height=cell_height
i.cell_count=cell_count
i.ResetVerts()
Local w_iter=i.width/cell_width
Local h_iter=i.height/cell_height
Local cell_iter=0
For Local y=0 To h_iter-1
For Local x=0 To w_iter-1
Local length=x*cell_width
Local height=y*cell_height
i.animx[cell_iter]=length/i.width
i.animy[cell_iter]=height/i.height
cell_iter:+1
Next
Next
i.cell_width=cell_width/i.width
i.cell_height=cell_height/i.height
Return i
EndFunction

Function ImageWidth(i:TImage)
If i.cell_count=0
Return i.width
Else
Return i.real_cell_width
EndIf
EndFunction

Function ImageHeight(i:TImage)
If i.cell_count=0
Return i.height
Else
Return i.real_cell_height
EndIf
EndFunction

Function ImageParent(child:TImage,parent:TImage)
If parent<>Null
If parent.child_list=Null
parent.child_list=New TList
EndIf
parent.child_list.addlast(child)
child.parent=parent
Else
child.parent.child_list.remove(child)
child.parent=Null
EndIf
EndFunction

Function SetImageFont(i:image_font)
imagefont=i
EndFunction


Function LoadImageFont:image_font(url:String,incstring$)
Local x,y
Local doc:TxmlDoc
Local node:TxmlNode
Local thisnode:TxmlNode
Local charnode:TxmlNode
Local filename$
Local docname$=incstring+url
doc = TxmlDoc.parseFile(docname)
If doc = Null
RuntimeError docname + " not found"
EndIf
node=doc.getrootelement()
Local i:image_font=New image_font
i.name=StripAll(url)
Local nodeList:TList = node.getChildren()
For thisnode = EachIn nodeList
If thisnode.getname()="img"
i.image_width=Float(Int(thisnode.getattribute("width")))
i.image_height=Float(Int(thisnode.getattribute("height")))
i.backgroundr=Int(thisnode.getattribute("backgroundr"))
i.backgroundg=Int(thisnode.getattribute("backgroundg"))
i.backgroundb=Int(thisnode.getattribute("backgroundb"))
i.font_height=Int(thisnode.getattribute("fontheight"))
SetMaskColor (i.backgroundr,i.backgroundg,i.backgroundb)
i.tex=LoadImage(StripExt(url)+".png")
EndIf
If thisnode.getName()="chars"
Local charlist:TList=thisnode.getchildren()
For charnode=EachIn charlist
Local fo:font_char=New font_char
fo.xoffset=Int(charnode.getattribute("xoffset"))
fo.yoffset=Int(charnode.getattribute("yoffset"))
fo.pixel_width=Float(charnode.getattribute("width"))
fo.pixel_height=Float(charnode.getattribute("height"))
fo.width=fo.pixel_width/i.image_width
fo.height=fo.pixel_height/i.image_height
fo.id=Int(charnode.getattribute("ascnum"))
fo.xadvance=Int(charnode.getattribute("xadvance"))
fo.x=Float(charnode.getattribute("x"))
fo.y=Float(charnode.getattribute("y"))
fo.x=fo.x/i.image_width
fo.y=fo.y/i.image_height
fo.vertex[0]=0
fo.vertex[1]=0
fo.vertex[2]=fo.pixel_width
fo.vertex[3]=0
fo.vertex[4]=fo.pixel_width
fo.vertex[5]=fo.pixel_height
fo.vertex[6]=0
fo.vertex[7]=fo.pixel_height
i.char_array[fo.id]=fo
Next
EndIf
Next
doc.free()
SetMaskColor (0,0,0)
Return i
End Function


Function MidHandleImage(i:TImage)
If i.cell_count>0
Local ij:TImage=TImage(i)
SetImageVertices(ij,-(ij.real_cell_width/2),-(ij.real_cell_height/2),(ij.real_cell_width/2),-(ij.real_cell_height/2),(ij.real_cell_width/2),(ij.real_cell_height/2),-(ij.real_cell_width/2),(ij.real_cell_height/2))
Else
SetImageVertices(i,-(i.width/2),-(i.height/2),(i.width/2),-(i.height/2),(i.width/2),(i.height/2),-(i.width/2),(i.height/2))
EndIf
EndFunction

Function SetAlpha( alpha# )
Local red#=colorr/255.0
Local green#=colorg/255.0
Local blue#=colorb/255.0
globalalpha=alpha
glColor4f(red,green,blue,alpha)
End Function

Function SetLineWidth( width# )
glLineWidth width
End Function

Function SetColor( red#,green#,blue#)
colorr=red ; colorg=green ; colorb=blue
glColor4f(red/255.0,green/255.0,blue/255.0,globalalpha#)
End Function

Function SetClsColor(red,green,blue )
red=Min(Max(red,0),255)
green=Min(Max(green,0),255)
blue=Min(Max(blue,0),255)
glClearColor red/255.0,green/255.0,blue/255.0,1.0
End Function

Function SetImageXWhenChild(i:TImage,x)
i.child_x=x
EndFunction

Function SetImageYWhenChild(i:TImage,y)
i.child_y=y
EndFunction

Function SetImageRotationWhenChild(i:TImage,x)
i.child_rotation=x
EndFunction


Function SetImageHandle(i:TImage,x,y)
i.handlex=x ; i.handley=y
EndFunction

Function SetScale(x:Float,y:Float)
scalex=x ; scaley=y
EndFunction

Function SetMaskColor(r,g,b)
maskcolorr=r ; maskcolorg=g ; maskcolorb=b
EndFunction

Function SetBlend( blend )
If blend=state_blend Return
state_blend=blend
Select blend
Case MASKBLEND
glDisable GL_BLEND
glEnable GL_ALPHA_TEST
glAlphaFunc GL_GEQUAL,.5
Case SOLIDBLEND
glDisable GL_BLEND
glDisable GL_ALPHA_TEST
Case ALPHABLEND
glEnable GL_BLEND
glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
glDisable GL_ALPHA_TEST
Case LIGHTBLEND
glEnable GL_BLEND
glBlendFunc GL_SRC_ALPHA,GL_ONE
glDisable GL_ALPHA_TEST
Case SHADEBLEND
glEnable GL_BLEND
glBlendFunc GL_DST_COLOR,GL_ZERO
glDisable GL_ALPHA_TEST
Default
glDisable GL_BLEND
glDisable GL_ALPHA_TEST
End Select
EndFunction

Function Plot(x,y)
DisableTex
glBegin(GL_POINTS)
glVertex2f(x,y)
glEnd()
EndFunction
 
Function SetViewport(x,y,width,height)
glviewport(x,y,width,height)
EndFunction

Function SetRotation(rot)
rotation=rot
EndFunction

Function TextHeight(str:String)
Return imagefont.font_height
EndFunction

Function TextWidth(str:String)
Local width
For Local x=1 To str.length
Local char$= str[x-1..x]
Local f:font_char=imagefont.char_array[Asc(char)]
If f<>Null
width:+(f.xadvance*scalex)
width:+(-f.xoffset*scalex)
EndIf
Next
Return width
EndFunction


Function Cls()
glClear(GL_COLOR_BUFFER_BIT)
EndFunction


Function SetImageVertices(i:TImage,x0#,y0#,x1#,y1#,x2#,y2#,x3#,y3#)
i.vertex=[x0,y0,x1,y1,x2,y2,x3,y3]
EndFunction


Comments :


slenkar(Posted 1+ years ago)

 here is the bitmap font generator program:
Strict

Import BaH.libxml
Graphics 800,600,0

For Local x=32 To 126
Print Chr(x)
Next

Global font:Timagefont=Timagefont.Load("c:/windows/fonts/arial.ttf",60,SMOOTHFONT)
'Local g:TFreeTypeGlyph=font.loadglyph(32)

Global img_array:TImage[127]


Global texturewidth=512
Global textureheight=512

SetClsColor(0,0,0)

Global charnode:TxmlNode
Global imgnode:TxmlNode
Global rootnode:TxmlNode
Global doc:TxmlDoc
Global grabbed

While Not KeyHit(KEY_ESCAPE)

Cls
Local width
Local y
SetColor (255,255,255)

If grabbed=False
doc:TxmlDoc = TxmlDoc.newdoc("1.0")
rootnode:TxmlNode=TxmlNode.newnode("imagefont")
doc.setrootelement(rootnode)
imgnode:TxmlNode=rootnode.addchild("img")
charnode:TxmlNode=rootnode.addchild("chars")

imgnode.addattribute("width",texturewidth)
imgnode.addattribute("height",textureheight)
imgnode.addattribute("backgroundr",0)
imgnode.addattribute("backgroundg",0)
imgnode.addattribute("backgroundb",0)
imgnode.addattribute("fontheight", font.height())
EndIf

For Local x=32 To 126

Local s$=Chr(x)
Local n=font.CharToGlyph(x)
Local g:TimageGlyph=font.LoadGlyph(n)
Local img:TImage=g._image

Local thisnode:TxmlNode=charnode.addchild("Char")
thisnode.addattribute("ascnum",x)
img_array:TImage[x]=g._image
thisnode.addattribute("xadvance",g._advance)
thisnode.addattribute("yoffset",g._y)
thisnode.addattribute("xoffset",g._x)
thisnode.addattribute("width",g._w)
thisnode.addattribute("height",g._h)



If img<>Null

DrawImage img,width,y+g._y

If grabbed=False
thisnode.addattribute("x",width)
thisnode.addattribute("y",y+g._y)
EndIf

width=width+g._advance+(-g._x)+1

If x+1<126
Local n2=font.CharToGlyph(x+1)
Local g2:TimageGlyph=font.LoadGlyph(n2)

If g2<>Null

Local img2:TImage=g2._image
If width+g2._advance+(-g._x)+1>texturewidth
width=0
y=y+font.height()
EndIf
EndIf

EndIf
EndIf

Next

Flip

If grabbed=False
If doc <> Null Then
Print doc.savefile("-")
doc.savefile("arial.xml")
End If
Local pixy:TPixmap=GrabPixmap(0,0,texturewidth,textureheight)
SavePixmapPNG(pixy,"arial.png")
grabbed=True
EndIf

Wend




slenkar(Posted 1+ years ago)

 here are collision functions that are compatible with the images in this library:Module keef.maths
Import brl.math

'Finds out if a point is on the left or right side of a segment
'Code by Dave 'Oddball' Williamson
Strict

Function PointInOval:Int(px#,py#,ox#,oy#,width#,height#)

Return((px - ox)^2 / width^2 + (py - oy)^2 / height^2 < 1)

End Function

Function TFormPolyToTFormPoly( p1_xy:Float[], p1_x:Float=0, p1_y:Float=0,..
p1_rot:Float=0, p1_scale_x:Float=1, p1_scale_y:Float=1,..
p1_handle_x:Float=0, p1_handle_y:Float=0,..
p1_origin_x:Float=0, p1_origin_y:Float=0,..
p2_xy:Float[], p2_x:Float=0, p2_y:Float=0,..
p2_rot:Float=0, p2_scale_x:Float=1, p2_scale_y:Float=1,..
p2_handle_x:Float=0, p2_handle_y:Float=0,..
p2_origin_x:Float=0, p2_origin_y:Float=0)

If p1_xy.length<6 Or (p1_xy.length&1) Return False
If p2_xy.length<6 Or (p2_xy.length&1) Return False

Local tform1_xy:Float[]=TFormPoly(p1_xy,p1_x,p1_y,p1_rot,p1_scale_x,p1_scale_y,..
p1_handle_x,p1_handle_y,p1_origin_x,p1_origin_y)

Local tform2_xy:Float[]=TFormPoly(p2_xy,p2_x,p2_y,p2_rot,p2_scale_x,p2_scale_y,..
p2_handle_x,p2_handle_y,p2_origin_x,p2_origin_y)

If PolyToPoly(tform1_xy,tform2_xy)
Return True
Else
Return False
EndIf

End Function

Function PolyToTFormPoly( p1_xy:Float[], p2_xy:Float[],..
p2_x:Float=0, p2_y:Float=0, rot:Float=0,..
scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

If p1_xy.length<6 Or (p1_xy.length&1) Return False
If p2_xy.length<6 Or (p2_xy.length&1) Return False

Local tform_xy:Float[]=TFormPoly(p2_xy,p2_x,p2_y,rot,scale_x,scale_y,..
handle_x,handle_y,origin_x,origin_y)

If PolyToPoly(p1_xy,tform_xy)
Return True
Else
Return False
EndIf

End Function

Function PolyToPoly( p1_xy:Float[], p2_xy:Float[] )

If p1_xy.length<6 Or (p1_xy.length&1) Return False
If p2_xy.length<6 Or (p2_xy.length&1) Return False

For Local i:Int=0 Until p1_xy.Length Step 2
If PointInPoly(p1_xy[i],p1_xy[i+1],p2_xy) Then Return True
Next
For Local i:Int=0 Until p2_xy.Length Step 2
If PointInPoly(p2_xy[i],p2_xy[i+1],p1_xy) Then Return True
Next

Local l1_x1:Float=p1_xy[p1_xy.Length-2]
Local l1_y1:Float=p1_xy[p1_xy.Length-1]
For Local i1:Int=0 Until p1_xy.Length Step 2
Local l1_x2=p1_xy[i1]
Local l1_y2=p1_xy[i1+1]

Local l2_x1:Float=p2_xy[p2_xy.Length-2]
Local l2_y1:Float=p2_xy[p2_xy.Length-1]
For Local i2:Int=0 Until p2_xy.Length Step 2
Local l2_x2=p2_xy[i2]
Local l2_y2=p2_xy[i2+1]

If LinesCross(l1_x1,l1_y1,l1_x2,l1_y2,l2_x1,l2_y1,l2_x2,l2_y2)
Return True
EndIf

l2_x1=l2_x2
l2_y1=l2_y2
Next
l1_x1=l1_x2
l1_y1=l1_y2
Next
Return False
End Function

Function CircleToTFormPoly( circle_x:Float, circle_y:Float, radius:Float,..
xy:Float[], poly_x:Float=0, poly_y:Float=0, rot:Float=0,..
scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

If xy.length<6 Or (xy.length&1) Return False

Local tform_xy:Float[]=TFormPoly(xy,poly_x,poly_y,rot,scale_x,scale_y,..
handle_x,handle_y,origin_x,origin_y)

Return CircleToPoly(circle_x,circle_y,radius,tform_xy)
End Function

Function CircleToPoly( circle_x:Float, circle_y:Float, radius:Float, xy:Float[] )

If xy.length<6 Or (xy.length&1) Return False

If PointInPoly(circle_x,circle_y,xy) Then Return True

Local x1:Float=xy[xy.Length-2]
Local y1:Float=xy[xy.Length-1]

For Local i:Int=0 Until Len xy Step 2
Local x2:Float=xy[i]
Local y2:Float=xy[i+1]

If LineToCircle(x1,y1,x2,y2,circle_x,circle_y,radius) Then Return True
x1=x2
y1=y2
Next

Return False
End Function

Function LineToTFormPoly( line_x1:Float, line_y1:Float, line_x2:Float, line_y2:Float,..
xy:Float[], poly_x:Float=0, poly_y:Float=0,..
rot:Float=0, scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

If xy.length<6 Or (xy.length&1) Return False

TFormGlobalToLocal(line_x1,line_y1,poly_x,poly_y,rot,scale_x,..
scale_y,handle_x,handle_y,origin_x,origin_y)

TFormGlobalToLocal(line_x2,line_y2,poly_x,poly_y,rot,scale_x,..
scale_y,handle_x,handle_y,origin_x,origin_y)

Return LineToPoly(line_x1,line_y1,line_x2,line_y2,xy)
End Function

Function LineToPoly( line_x1:Float, line_y1:Float, line_x2:Float, line_y2:Float, xy:Float[] )

If xy.length<6 Or (xy.length&1) Return False

If PointInPoly(line_x1,line_y1,xy) Then Return True

Local poly_x1:Float=xy[xy.Length-2]
Local poly_y1:Float=xy[xy.Length-1]

For Local i:Int=0 Until Len xy Step 2
Local poly_x2:Float=xy[i]
Local poly_y2:Float=xy[i+1]

If LinesCross(line_x1,line_y1,line_x2,line_y2,..
poly_x1,poly_y1,poly_x2,poly_y2) Then Return True
poly_x1=poly_x2
poly_y1=poly_y2
Next

Return False

End Function

Function PointInTFormPoly( point_x:Float, point_y:Float, xy:Float[],..
poly_x:Float=0, poly_y:Float=0, rot:Float=0,..
scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

If xy.length<6 Or (xy.length&1) Return False

TFormGlobalToLocal(point_x,point_y,poly_x,poly_y,rot,scale_x,..
scale_y,handle_x,handle_y,origin_x,origin_y)

Return PointInPoly(point_x,point_y,xy)

End Function

Function PointInPoly( point_x:Float, point_y:Float, xy:Float[] )

If xy.length<6 Or (xy.length&1) Return False

Local x1:Float=xy[xy.Length-2]
Local y1:Float=xy[xy.Length-1]
Local cur_quad:Int=GetQuad(point_x,point_y,x1,y1)
Local next_quad:Int
Local total:Int

For Local i=0 Until Len xy Step 2
Local x2:Float=xy[i]
Local y2:Float=xy[i+1]
next_quad=GetQuad(point_x,point_y,x2,y2)
Local diff:Int=next_quad-cur_quad

Select diff
Case 2,-2
If ( x2 - ( ((y2 - point_y) * (x1 - x2)) / (y1 - y2) ) )<point_x
diff=-diff
EndIf
Case 3
diff=-1
Case -3
diff=1
End Select

total:+diff
cur_quad=next_quad
x1=x2
y1=y2
Next

If Abs(total)=4 Then Return True Else Return False

End Function

Function TFormPoly:Float[]( xy:Float[], tform_x:Float=0, tform_y:Float=0, rot:Float=0,..
scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

If xy.length<6 Or (xy.length&1) Return Null

Local tform_xy:Float[]=xy[..]

For Local i=0 Until tform_xy.Length Step 2
TFormLocalToGlobal(tform_xy[i],tform_xy[i+1],tform_x,tform_y,rot,..
scale_x,scale_y,handle_x,handle_y,origin_x,origin_y)
Next

Return tform_xy
End Function

Function TFormGlobalToLocal( point_x:Float Var, point_y:Float Var,..
tform_x:Float=0, tform_y:Float=0, rot:Float=0,..
scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

point_x:-origin_x
point_y:-origin_y

point_x:-tform_x
point_y:-tform_y

Local mag:Float=Sqr(point_x*point_x+point_y*point_y)
Local ang:Float=ATan2(point_y,point_x)
point_x=Cos(ang-rot)*mag
point_y=Sin(ang-rot)*mag

point_x:/scale_x
point_y:/scale_y

point_x:+handle_x
point_y:+handle_y
End Function

Function TFormLocalToGlobal( point_x:Float Var, point_y:Float Var,..
tform_x:Float=0, tform_y:Float=0, rot:Float=0,..
scale_x:Float=1, scale_y:Float=1,..
handle_x:Float=0, handle_y:Float=0,..
origin_x:Float=0, origin_y:Float=0 )

point_x:-handle_x
point_y:-handle_y

point_x:*scale_x
point_y:*scale_y

Local mag:Float=Sqr(point_x*point_x+point_y*point_y)
Local ang:Float=ATan2(point_y,point_x)
point_x=Cos(ang+rot)*mag
point_y=Sin(ang+rot)*mag

point_x:+tform_x
point_y:+tform_y

point_x:+origin_x
point_y:+origin_y
End Function

'Adapted from Fredborg's code
Function LinesCross( x0:Float, y0:Float , x1:Float, y1:Float,..
x2:Float ,y2:Float, x3:Float, y3:Float )
 
Local n:Float=(y0-y2)*(x3-x2)-(x0-x2)*(y3-y2)
Local d:Float=(x1-x0)*(y3-y2)-(y1-y0)*(x3-x2)

If Abs(d) < 0.0001
' Lines are parallel!
Return False
Else
' Lines might cross!
Local Sn:Float=(y0-y2)*(x1-x0)-(x0-x2)*(y1-y0)

Local AB:Float=n/d
If AB>0.0 And AB<1.0
Local CD:Float=Sn/d
If CD>0.0 And CD<1.0
' Intersection Point
Local X=x0+AB*(x1-x0)
      Local Y=y0+AB*(y1-y0)
Return True
End If
End If

' Lines didn't cross, because the intersection was beyond the end points of the lines
EndIf

' Lines do Not cross!
Return False

End Function

'Adapted from TomToad's code
Function LineToCircle( x1:Float, y1:Float, x2:Float, y2:Float, px:Float, py:Float, r:Float )

Local sx:Float = x2-x1
Local sy:Float = y2-y1

Local q:Float = ((px-x1) * (x2-x1) + (py - y1) * (y2-y1)) / (sx*sx + sy*sy)

If q < 0.0 Then q = 0.0
If q > 1.0 Then q = 1.0

Local cx:Float=(1-q)*x1+q*x2
Local cy:Float=(1-q)*y1 + q*y2


If PointToPointDist(px,py,cx,cy) < r

Return True

Else

Return False

EndIf


End Function

Function PointToPointDist:Float( x1:Float, y1:Float, x2:Float, y2:Float )

Local dx:Float = x1-x2
Local dy:Float = y1-y2

Return Sqr(dx*dx + dy*dy)
End Function


Function GetQuad(axis_x:Float,axis_y:Float,vert_x:Float,vert_y:Float)
If vert_x<axis_x
If vert_y<axis_y
Return 1
Else
Return 4
EndIf
Else
If vert_y<axis_y
Return 2
Else
Return 3
EndIf
EndIf

End Function



slenkar(Posted 1+ years ago)

 usage:use initgl(800,600,0)instead of graphics(800,600,0)all other commands are the same.Setgraphicsdriver is not needed


plash(Posted 1+ years ago)

 What did you do to the formatting!?


ImaginaryHuman(Posted 1+ years ago)

 So basically the only thing it does different is lets you position the corners of image quads, and add a linear gradient to drawtext?


slenkar(Posted 1+ years ago)

 yes basically but with peoples help it could do a lot more.The code is very simple compared to the official glmax2d which helps greatly when someone wants to add more features.


Black3D(Posted 1+ years ago)

 Thanks Slenkar, I learned a lot from this code. :) [/i]