Here is an example of a sprite/texture in opengl.
There are many on this forum who are opengl experts, and opengl can now have a completely new format.
Code: Select all
#include "GL/gl.bi"
#include "GL/glext.bi"
#include "fbgfx.bi"
Screenres 800,600,32
Dim As long refreshrate
Dim As String driver
Screeninfo ,,,,,refreshrate
Screenres 800, 600, 32,,2
Screeninfo ,,,,,,driver
Width 800\8,600\16 'full fonts on image
'Declare Function settimer Alias "timeBeginPeriod"(As Ulong=1) As Long
'Declare Function freetimer Alias "timeEndPeriod" (As Ulong=1) As Long
Sub setupgl
Dim As Integer xres,yres
Screeninfo xres,yres
glDisable (GL_DEPTH_TEST)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
glEnable (GL_BLEND)
glEnable (GL_LINE_SMOOTH)
glOrtho 0, xres, yres,0,-1, 1
glClearColor 0,0,200/255,1
End Sub
setupgl
Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) Export
glMatrixMode GL_PROJECTION 'save projection
glPushMatrix
glMatrixMode GL_MODELVIEW
glPushMatrix
glMatrixMode GL_PROJECTION 'make ortho
glLoadIdentity
glOrtho 0, xres, yres, 0,-1, 1
glMatrixMode GL_MODELVIEW
glLoadIdentity
#define Red(c) ((c) Shr 16 And 255)
#define Green(c) ((c) Shr 8 And 255)
#define Blue(c) ((c) And 255)
#define Alph(c) ((c) Shr 24)
glColor4ub Red(col),Green(col),Blue(col),alph(col)
glend
glpointsize(1.1*size)
glBegin (GL_POINTS)
Type D2
As Single x,y
End Type
Static As d2 cpt(),XY()
Static As Long runflag
If runflag=0 Then
Redim XY(128,127)
Redim cpt(1 To 64*2)
Screen 8
Width 640\8,200\16
Dim As Ulong Pointer img
Dim count As Long
For ch As Long=1 To 127
img=Imagecreate(640,200)
Draw String img,(1,1),Chr(ch)
For x As Long=1 To 8
For y As Long=1 To 16
If Point(x,y,img)<>0 Then
count=count+1
XY(count,ch)=Type<D2>(x,y)
End If
Next y
Next x
count=0
Imagedestroy img
Next ch
runflag=1
End If
If size=0 Then Exit Sub
Dim As D2 np,t
#macro Scale(p1,p2,d)
np.x=d*(p2.x-p1.x)+p1.x
np.y=d*(p2.y-p1.y)+p1.y
#endmacro
Dim As D2 c=Type<D2>(xpos,ypos)
Dim As Long dx=xpos,dy=ypos
For z6 As Long=1 To Len(text)
Var asci=text[z6-1]
For _x1 As Long=1 To 64*2
t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)
Scale(c,t,size)
cpt(_x1)=np
If XY(_x1,asci).x<>0 Then
If Abs(size)>0 Then
glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
End If
End If
Next _x1
dx=dx+8
Next z6
glend
glMatrixMode GL_PROJECTION 'restore
glPopMatrix
glMatrixMode GL_MODELVIEW
glPopMatrix
End Sub
Sub inittext Constructor
drawstring(0,0,"",0,0,0,0)
End Sub
Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
Static As Double timervalue,lastsleeptime,t3,frames
frames+=1
If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
If sleeptime<1 Then sleeptime=1
lastsleeptime=sleeptime
timervalue=Timer
Return sleeptime
End Function
Sub settexture( texture As gluint, image As Any Ptr)
glGenTextures(1, @texture)
glBindTexture( GL_TEXTURE_2D, texture )
glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub
Sub drawquad(x As Integer)
glBegin(GL_QUADS)
glTexCoord2f( 1,0 )
glVertex2f( x, 80) 'Top right of the quad
glTexCoord2f( 0,0 )
glVertex2f(x-63, 80) 'Top left of the quad
glTexCoord2f( 0,1 )
glVertex2f(x-63, 127+80) ' Bottom left of the quad
glTexCoord2f( 1,1 )
glVertex2f( x, 127+80) ' Bottom right of the quad
glend
End Sub
Dim As Any Ptr im=Imagecreate(128/2,128)
'draw stuff to image
For x As Long=0 To 128\2
For y As Long=0 To 128
Pset im,(x,y),Rgb(x*4,x*4 xor y*4,y*4)
Next y
Next x
Circle im,(32,64),20,Rgb(200,0,0),,,,f
Draw String im,(10,100),"Hello",Rgb(255,255,255)
Dim As gluint tex1
settexture(tex1,im)
Dim As Integer x = 0
Dim As Long fps
Do
glClear(GL_COLOR_BUFFER_BIT)
glEnable( GL_TEXTURE_2D )
drawquad(x)
gldisable( GL_TEXTURE_2D )
x += 1: If (x > 639) Then x = 0
drawstring(20,20,"framerate = " &fps,Rgb(0,200,0),1,800,600)
drawstring(20,40,"refreshrate = " &refreshrate,Rgb(0,200,0),1,800,600)
drawstring(20,60,"driver = " +driver,Rgb(0,200,0),1,800,600)
Flip
' settimer
Sleep regulate(refreshrate*3,fps)
' freetimer
Loop While Inkey = ""
Imagedestroy im
End