glsdlgraphics.bmx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. Strict
  2. Module SDL.GLSDLGraphics
  3. ModuleInfo "Version: 1.00"
  4. ModuleInfo "Author: Mark Sibly, Simon Armstrong, Bruce A Henderson"
  5. ModuleInfo "License: zlib/libpng"
  6. ModuleInfo "Copyright: Blitz Research Ltd"
  7. ModuleInfo "History: 1.00"
  8. ModuleInfo "History: Port to SDL backend, based on snippets from BRL.GLGraphics."
  9. ?Not opengles And Not nx
  10. Import SDL.SDLGraphics
  11. Import BRL.Pixmap
  12. Import Pub.Glew
  13. Import Pub.OpenGL
  14. Private
  15. Incbin "gldrawtextfont.bin"
  16. Global fontTex
  17. Global fontSeq
  18. Global ortho_mv![16],ortho_pj![16]
  19. Function BeginOrtho()
  20. Local vp[4]
  21. glPushAttrib GL_ENABLE_BIT|GL_TEXTURE_BIT|GL_TRANSFORM_BIT
  22. glGetIntegerv GL_VIEWPORT,vp
  23. glGetDoublev GL_MODELVIEW_MATRIX,ortho_mv
  24. glGetDoublev GL_PROJECTION_MATRIX,ortho_pj
  25. glMatrixMode GL_MODELVIEW
  26. glLoadIdentity
  27. glMatrixMode GL_PROJECTION
  28. glLoadIdentity
  29. glOrtho 0,vp[2],vp[3],0,-1,1
  30. glDisable GL_CULL_FACE
  31. glDisable GL_ALPHA_TEST
  32. glDisable GL_DEPTH_TEST
  33. End Function
  34. Function EndOrtho()
  35. glMatrixMode GL_PROJECTION
  36. glLoadMatrixd ortho_pj
  37. glMatrixMode GL_MODELVIEW
  38. glLoadMatrixd ortho_mv
  39. glPopAttrib
  40. End Function
  41. Public
  42. Rem
  43. bbdoc: Helper function to calculate nearest valid texture size
  44. about: This functions rounds @width and @height up to the nearest valid texture size
  45. End Rem
  46. Function GLAdjustTexSize( width Var,height Var )
  47. Function Pow2Size( n )
  48. Local t=1
  49. While t<n
  50. t:*2
  51. Wend
  52. Return t
  53. End Function
  54. width=Pow2Size( width )
  55. height=Pow2Size( height )
  56. Repeat
  57. Local t
  58. glTexImage2D GL_PROXY_TEXTURE_2D,0,4,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,Null
  59. glGetTexLevelParameteriv GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_WIDTH,Varptr t
  60. If t Return
  61. If width=1 And height=1 RuntimeError "Unable to calculate tex size"
  62. If width>1 width:/2
  63. If height>1 height:/2
  64. Forever
  65. End Function
  66. Rem
  67. bbdoc: Helper function to create a texture from a pixmap
  68. returns: Integer GL Texture name
  69. about: @pixmap is resized to a valid texture size before conversion.
  70. end rem
  71. Function GLTexFromPixmap( pixmap:TPixmap,mipmap=True )
  72. If pixmap.format<>PF_RGBA8888 pixmap=pixmap.Convert( PF_RGBA8888 )
  73. Local width=pixmap.width,height=pixmap.height
  74. GLAdjustTexSize width,height
  75. If width<>pixmap.width Or height<>pixmap.height pixmap=ResizePixmap( pixmap,width,height )
  76. Local old_name,old_row_len
  77. glGetIntegerv GL_TEXTURE_BINDING_2D,Varptr old_name
  78. glGetIntegerv GL_UNPACK_ROW_LENGTH,Varptr old_row_len
  79. Local name
  80. glGenTextures 1,Varptr name
  81. glBindtexture GL_TEXTURE_2D,name
  82. Local mip_level
  83. Repeat
  84. glPixelStorei GL_UNPACK_ROW_LENGTH,pixmap.pitch/BytesPerPixel[pixmap.format]
  85. glTexImage2D GL_TEXTURE_2D,mip_level,GL_RGBA8,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.pixels
  86. If Not mipmap Exit
  87. If width=1 And height=1 Exit
  88. If width>1 width:/2
  89. If height>1 height:/2
  90. pixmap=ResizePixmap( pixmap,width,height )
  91. mip_level:+1
  92. Forever
  93. glBindTexture GL_TEXTURE_2D,old_name
  94. glPixelStorei GL_UNPACK_ROW_LENGTH,old_row_len
  95. Return name
  96. End Function
  97. Rem
  98. bbdoc:Helper function to output a simple rectangle
  99. about:
  100. Draws a rectangle relative to top-left of current viewport.
  101. End Rem
  102. Function GLDrawRect( x,y,width,height )
  103. BeginOrtho
  104. glBegin GL_QUADS
  105. glVertex2i x,y
  106. glVertex2i x+width,y
  107. glVertex2i x+width,y+height
  108. glVertex2i x,y+height
  109. glEnd
  110. EndOrtho
  111. End Function
  112. Rem
  113. bbdoc: Helper function to output some simple 8x16 font text
  114. about:
  115. Draws text relative to top-left of current viewport.<br>
  116. <br>
  117. The font used is an internal fixed point 8x16 font.<br>
  118. <br>
  119. This function is intended for debugging purposes only - performance is unlikely to be stellar.
  120. End Rem
  121. Function GLDrawText( text:String,x,y )
  122. ' If fontSeq<>graphicsSeq
  123. If Not fontTex
  124. Local pixmap:TPixmap=TPixmap.Create( 1024,16,PF_RGBA8888 )
  125. Local p:Byte Ptr=IncbinPtr( "gldrawtextfont.bin" )
  126. For Local y=0 Until 16
  127. For Local x=0 Until 96
  128. Local b=p[x]
  129. For Local n=0 Until 8
  130. If b & (1 Shl n)
  131. pixmap.WritePixel x*8+n,y,~0
  132. Else
  133. pixmap.WritePixel x*8+n,y,0
  134. EndIf
  135. Next
  136. Next
  137. p:+96
  138. Next
  139. fontTex=GLTexFromPixmap( pixmap )
  140. fontSeq=graphicsSeq
  141. EndIf
  142. BeginOrtho
  143. glEnable GL_TEXTURE_2D
  144. glBindTexture GL_TEXTURE_2D,fontTex
  145. For Local i=0 Until text.length
  146. Local c=text[i]-32
  147. If c>=0 And c<96
  148. Const adv#=8/1024.0
  149. Local t#=c*adv;
  150. glBegin GL_QUADS
  151. glTexcoord2f t,0
  152. glVertex2f x,y
  153. glTexcoord2f t+adv,0
  154. glVertex2f x+8,y
  155. glTexcoord2f t+adv,1
  156. glVertex2f x+8,y+16
  157. glTexcoord2f t,1
  158. glVertex2f x,y+16
  159. glEnd
  160. EndIf
  161. x:+8
  162. Next
  163. EndOrtho
  164. End Function
  165. ?