Created
October 19, 2009 22:00
-
-
Save nilium/213767 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Strict | |
' Implements basic 2D drawing functionality (blending, textures, etc. handled elsewhere) | |
Type Render2D | |
Const RENDER2D_BUFFER_SIZE_BYTES:Int = 131072 '128kb | |
Field _vertbuffer:TBank, _vertstream:TBankStream | |
Field _texcoordbuffer:TBank, _texcoordstream:TBankStream | |
Field _colorbuffer:TBank, _colorstream:TBankStream | |
Field _index:Int = 0, _sets%=0 | |
Field _cr%=255,_cg%=255,_cb%=255,_ca%=255 | |
Field _arrindices:Int[], _arrcounts:Int[] | |
Field _lock%=0 | |
Method New() | |
_vertbuffer = TBank.Create(RENDER2D_BUFFER_SIZE_BYTES); _vertstream = TBankStream.Create(_vertbuffer) | |
_texcoordbuffer = TBank.Create(RENDER2D_BUFFER_SIZE_BYTES); _texcoordstream = TBankStream.Create(_texcoordbuffer) | |
_colorbuffer = TBank.Create(RENDER2D_BUFFER_SIZE_BYTES); _colorstream = TBankStream.Create(_colorbuffer) | |
_arrindices = New Int[512] | |
_arrcounts = New Int[512] | |
End Method | |
Method AddPolygonEx(points:Double[], texcoords:Double[], colors:Int[]) | |
Assert _lock=0 Else "Buffers are locked for rendering" | |
Assert colors.Length/4 = points.Length/2 And points.Length/2 = texcoords.Length/2 And .. | |
(points.Length Mod 2 = 0 And texcoords.Length Mod 2 = 0 And colors.Length Mod 4 = 0) .. | |
Else "Incorrect buffer sizes" | |
If _sets >= _arrindices.Length Then | |
_arrindices = _arrindices[.. _arrindices.Length*2] | |
_arrcounts = _arrcounts[.. _arrcounts.Length*2] | |
EndIf | |
_arrindices[_sets] = _index | |
_arrcounts[_sets] = points.Length/2 | |
_texcoordstream.WriteBytes(texcoords, texcoords.Length*8) | |
_vertstream.WriteBytes(points, points.Length*8) | |
For Local i:Int = 0 Until colors.Length | |
_colorstream.WriteByte(colors[i]) | |
Next | |
_sets :+ 1 | |
_index :+ points.Length/2 | |
End Method | |
Method AddRectangle(x!, y!, w!, h!, u0!=0, v0!=0, u1!=1, v1!=1) | |
Local udif!=u1-u0 | |
Local vdif!=v1-v0 | |
AddPolygonEx([x,y,x+w,y,x+w,y+h,x,y+h], [u0,v0,u0+udif,v0,u0+udif,v0+vdif,u0,v0+vdif], [_cr,_cg,_cb,_ca,_cr,_cg,_cb,_ca,_cr,_cg,_cb,_ca,_cr,_cg,_cb,_ca]) | |
End Method | |
Method SetAutoColor(r%,g%,b%,a%) | |
_cr=r&$FF | |
_cg=g&$FF | |
_cb=b&$FF | |
_ca=a&$FF | |
End Method | |
Method GetAutoColor(r% Var, g% Var, b% Var, a% Var) | |
r = _cr | |
g = _cg | |
b = _cb | |
a = _ca | |
End Method | |
Method LockBuffers() | |
If _lock = 0 Then | |
glVertexPointer(2, GL_DOUBLE, 0, _vertbuffer.Lock()) | |
glColorPointer(4, GL_UNSIGNED_BYTE, 0, _colorbuffer.Lock()) | |
glTexCoordPointer(2, GL_DOUBLE, 0, _texcoordbuffer.Lock()) | |
EndIf | |
_lock :+ 1 | |
End Method | |
Method UnlockBuffers() | |
Assert _lock > 0 Else "Unmatched unlock for buffers" | |
_lock :- 1 | |
If _lock = 0 Then | |
glVertexPointer(4, GL_FLOAT, 0, Null) | |
glColorPointer(4, GL_FLOAT, 0, Null) | |
glTexCoordPointer(4, GL_FLOAT, 0, Null) | |
_vertbuffer.Unlock() | |
_colorbuffer.Unlock() | |
_texcoordbuffer.Unlock() | |
EndIf | |
End Method | |
Method Render() | |
LockBuffers | |
glMultiDrawArrays(GL_POLYGON, _arrindices, _arrcounts, _sets) | |
UnlockBuffers | |
End Method | |
Method ResetBuffers() | |
Assert _lock = 0 Else "Buffers are locked for rendering" | |
_vertstream.Seek(0) | |
_texcoordstream.Seek(0) | |
_colorstream.Seek(0) | |
_index = 0 | |
_sets = 0 | |
End Method | |
End Type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment