g2bmx.bmx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. Strict
  2. Framework brl.basic
  3. Import brl.map
  4. Import brl.retro
  5. Import brl.system
  6. '
  7. ' Generates bmx source from glew.h
  8. '
  9. Global OutDir:String = "_out_"
  10. Local OutOpenGLName:String="opengl.bmx"
  11. Local OutGlewName:String="glew.bmx"
  12. Const LINES_PER_PAGE:Int = 500
  13. CreateDir(outDir, True)
  14. Global OutOpenGL:TStream = WriteStream(outDir + "/" + OutOpenGLName)
  15. Global OutGLew :TStream = WriteStream(outDir + "/" + OutGlewName)
  16. Global Out :TStream = OutOpenGL
  17. Local generator:TCodeGenerator = New TCodeGenerator
  18. generator.Generate()
  19. Type TCodeGenerator
  20. Const STAGE_COUNT:Int = 0
  21. Const STAGE_GENERATE:Int = 1
  22. Const TYPE_OPENGL:Int = 0
  23. Const TYPE_GLEW:Int = 1
  24. Field timestamp:String = CurrentDate()
  25. Field stage:Int
  26. Field generateType:Int
  27. Field curr:String
  28. Field Text:String
  29. Field glewSource:String[]
  30. Field totalLines:Int
  31. Field pageCount:Int
  32. Field page:Int = -1
  33. Field lineCount:Int
  34. Method Init()
  35. glewSource = LoadText("GL/glew.h").Split("~n")
  36. End Method
  37. Method Generate()
  38. Init()
  39. stage = STAGE_COUNT
  40. Process()
  41. pageCount = totalLines / LINES_PER_PAGE
  42. If totalLines Mod LINES_PER_PAGE > 0 Then
  43. pageCount :+ 1
  44. End If
  45. stage = STAGE_GENERATE
  46. MainGlew()
  47. HeaderOpenGL()
  48. NextGlew()
  49. Process()
  50. WriteLine OutOpenGL , ""
  51. WriteLine OutOpenGL , "End Extern"
  52. OutOpenGL.Close
  53. WriteLine OutGLew , ""
  54. WriteLine OutGLew , "End Extern"
  55. WriteLine OutGLew , ""
  56. WriteLine OutGLew , "Extern ~qC~q"
  57. WriteLine OutGLew , "Function glewInit()=~qGLenum glewInit()!~q"
  58. WriteLine OutGLew , "End Extern"
  59. OutGlew.Close
  60. End Method
  61. Method GenerateApi (chaine:String, bbDoc:String)
  62. Select stage
  63. Case STAGE_COUNT
  64. Select generateType
  65. Case TYPE_GLEW
  66. totalLines :+ 1
  67. Case TYPE_OPENGL
  68. If Instr ( chaine , "glViewport" )
  69. generateType = TYPE_GLEW
  70. End If
  71. End Select
  72. Case STAGE_GENERATE
  73. 'WriteLine Out,"rem"
  74. 'WriteLine Out,"bbdoc: "+bbDoc
  75. 'WriteLine Out,"endrem"
  76. WriteLine Out,chaine
  77. Select generateType
  78. Case TYPE_GLEW
  79. lineCount :+ 1
  80. If lineCount = LINES_PER_PAGE Then
  81. FooterGlew()
  82. NextGlew()
  83. Out = OutGLew
  84. lineCount = 0
  85. End If
  86. Case TYPE_OPENGL
  87. If Instr ( chaine , "glViewport" )
  88. Out = OutGLew
  89. generateType = TYPE_GLEW
  90. End If
  91. End Select
  92. End Select
  93. End Method
  94. Method NextGlew()
  95. page :+ 1
  96. OutGlew = WriteStream(outDir + "/" + GlewFile(page))
  97. HeaderGlew()
  98. End Method
  99. Method GlewFile:String(page:Int)
  100. Local s:String = "00" + page
  101. Return "glew" + s[s.length-2..] + ".bmx"
  102. End Method
  103. Method Process()
  104. generateType = TYPE_OPENGL
  105. Local funMap:TMap=New TMap
  106. Local constMap:TMap=New TMap
  107. Local delayedMap:TMap = New TMap
  108. For Text:String = EachIn glewSource
  109. bump
  110. If curr="GLAPI"
  111. bump
  112. Local ty:Tty = New Tty
  113. Local funty:String=gltype(ty)
  114. If funty<>"x" And curr="GLAPIENTRY"
  115. Local id:String=bump()
  116. If id[..2]="gl" And bump()="("
  117. Local p:Tproto = New TProto
  118. Local proto:String=glproto(p)
  119. If proto<>"x"
  120. GenerateApi ( "Function "+id+funty+"("+proto+")", "GL Function "+id+funty )
  121. EndIf
  122. EndIf
  123. EndIf
  124. Else If curr="#"
  125. If bump()="define"
  126. Local id:String=bump()
  127. If id[..11]="GL_VERSION_"
  128. Else If id[..3]="GL_"
  129. If Not constMap.ValueForKey(id)
  130. Local n:String=bump()
  131. If n[..2]="0x"
  132. GenerateApi ( "Const "+id+"=$"+n[2..], "GL Const "+id )
  133. Else If n.length
  134. If isdigit(n[0]) 'And n<>"1"
  135. GenerateApi ( "Const "+id+"="+n, "GL Const "+id )
  136. Else
  137. Local v:String = String(constMap.ValueForKey(n))
  138. If v Then
  139. If v[..2]="0x" Then
  140. GenerateApi ( "Const "+id+"=$" + v[2..], "GL Const "+id )
  141. Else
  142. GenerateApi ( "Const "+id+"=" + v, "GL Const "+id )
  143. End If
  144. Else
  145. delayedMap.Insert(id, n)
  146. End If
  147. End If
  148. EndIf
  149. constMap.Insert id,n
  150. EndIf
  151. Else If id[..5]="GLEW_"
  152. If bump()="GLEW_GET_VAR" And bump()="("
  153. Local sym:String=bump()
  154. If sym[..7]="__GLEW_" And bump()=")"
  155. If Not constMap.ValueForKey("GL_"+id[5..])
  156. GenerateApi ( "Global GL_"+id[5..]+":Byte=~q"+sym+"~q", "GL Global "+id )
  157. End If
  158. EndIf
  159. EndIf
  160. Else If id[..2]="gl"
  161. If bump()="GLEW_GET_FUN" And bump()="("
  162. Local sym:String=bump()
  163. If sym[..6]="__glew" And bump()=")"
  164. Local key:String="PFNGL"+sym[6..].ToUpper()+"PROC"
  165. Local val:Tproto=Tproto( funMap.ValueForKey( key ) )
  166. If val
  167. GenerateApi ( "Global "+id+val.proto+"=~q"+val.exproto.Replace("XXXXXXXX", sym).Trim()+"~q", "GL Global "+id+val.proto )
  168. Else
  169. DebugLog "***** "+sym+" *****"
  170. EndIf
  171. EndIf
  172. EndIf
  173. EndIf
  174. EndIf
  175. Else If curr="typedef"
  176. bump
  177. Local ty:Tty = New Tty
  178. Local funty:String=gltype(ty)
  179. If funty<>"x" And curr="(" And bump()="GLAPIENTRY" And bump()="*"
  180. Local id:String=bump()
  181. If id[..5]="PFNGL" And bump()=")" And bump()="("
  182. Local p:Tproto = New TProto
  183. Local proto:String=glproto(p)
  184. If proto<>"x"
  185. p.proto = funty+"("+proto+")"
  186. p.exproto = ty.exty + " XXXXXXXX(" + p.exproto + ")!"
  187. funMap.Insert id, p
  188. EndIf
  189. EndIf
  190. EndIf
  191. EndIf
  192. Next
  193. For Local id:String = EachIn delayedMap.Keys()
  194. Local v:String = String(delayedMap.ValueForKey(id))
  195. If v Then
  196. Local c:String = String(constMap.ValueForKey(v))
  197. If c Then
  198. If c[..2]="0x" Then
  199. GenerateApi ( "Const "+id+"=$" + c[2..], "GL Const "+id )
  200. Else
  201. GenerateApi ( "Const "+id+"=" + c, "GL Const "+id )
  202. End If
  203. End If
  204. End If
  205. Next
  206. End Method
  207. Method glproto:String(proto:Tproto)
  208. If bump()=")" Return ""
  209. Local err,argid
  210. Repeat
  211. Local ty:Tty = New Tty
  212. Local argty:String=gltype(ty)
  213. If argty="x" Return argty
  214. Local id:String
  215. If curr<>"," And curr<>")" And curr.length And (isalpha(curr[0]) Or curr[0]=Asc("_"))
  216. id=curr
  217. If bump()="["
  218. While bump()<>"]"
  219. Wend
  220. bump
  221. If Not argty argty="Byte"
  222. argty:+" Ptr"
  223. EndIf
  224. Else
  225. id="arg"+argid
  226. EndIf
  227. argid:+1
  228. If proto.proto Then
  229. proto.proto:+","
  230. proto.exproto:+","
  231. End If
  232. proto.proto:+id+"_"+argty
  233. proto.exproto :+ ty.exty
  234. If curr=")"
  235. bump
  236. If proto.proto="arg0_" proto.proto=""
  237. Return proto.proto
  238. EndIf
  239. If curr<>"," Return "x"
  240. bump
  241. Forever
  242. End Method
  243. Method gltype:String(ty:TTy)
  244. 'Local ty:String
  245. 'Local exty:String
  246. If curr="const"
  247. ty.exty :+ curr
  248. bump
  249. EndIf
  250. Select curr
  251. Case "void","GLvoid"
  252. ty.ty=""
  253. Case "char","GLbyte","GLubyte","GLchar","GLboolean","GLcharARB"
  254. ty.ty="Byte"
  255. Case "GLshort","GLushort","GLhalf"
  256. ty.ty="Short"
  257. Case "GLint","GLuint","GLenum","GLsizei","GLbitfield"
  258. ty.ty="Int"
  259. Case "GLintptr","GLsizeiptr","GLintptrARB","GLsizeiptrARB"
  260. ty.ty="Int"
  261. Case "GLhandleARB"
  262. ty.ty="Int"
  263. Case "GLint64EXT","GLuint64EXT", "GLint64", "GLuint64"
  264. ty.ty="Long"
  265. Case "GLfloat","GLclampf"
  266. ty.ty="Float"
  267. Case "GLdouble","GLclampd"
  268. ty.ty="Double"
  269. Default
  270. Return "x"
  271. End Select
  272. ty.exty :+ " " + curr
  273. Repeat
  274. bump
  275. If curr="const" Then
  276. ty.exty :+ " " + curr
  277. bump
  278. End If
  279. If curr<>"*" Exit
  280. ty.exty :+ curr
  281. If Not ty.ty ty.ty="Byte"
  282. ty.ty:+" Ptr"
  283. Forever
  284. If ty.ty ty.ty=":"+ty.ty
  285. Return ty.ty
  286. End Method
  287. Method isalpha( c )
  288. Return (c>=Asc("A") And c<=Asc("Z")) Or (c>=Asc("a") And c<=Asc("z"))
  289. End Method
  290. Method isdigit( c )
  291. Return c>=Asc("0") And c<=Asc("9")
  292. End Method
  293. Method isalnum( c )
  294. Return isalpha(c) Or isdigit(c)
  295. End Method
  296. Method isxdigit( c )
  297. Return (c>=Asc("A") And c<=Asc("F")) Or (c>=Asc("a") And c<=Asc("f")) Or isdigit(c)
  298. End Method
  299. Method bump:String()
  300. Local i=0
  301. While i<Text.length And Text[i]<=Asc(" ")
  302. i:+1
  303. Wend
  304. If i=Text.length
  305. curr=""
  306. Text=""
  307. Return curr
  308. EndIf
  309. Text=Text[i..]
  310. Local c=Text[0]
  311. i=1
  312. If isalpha(c) Or c=Asc("_")
  313. While i<Text.length And (isalnum( Text[i] ) Or Text[i]=Asc("_"))
  314. i:+1
  315. Wend
  316. Else If c>=Asc("0") And c<=Asc("9")
  317. If i<Text.length And c=Asc("0") And Text[i]=Asc("x")
  318. i:+1
  319. While i<Text.length And isxdigit(Text[i])
  320. i:+1
  321. Wend
  322. Else
  323. While i<Text.length And isdigit(Text[i])
  324. i:+1
  325. Wend
  326. EndIf
  327. EndIf
  328. curr=Text[..i]
  329. Text=Text[i..]
  330. Return curr
  331. End Method
  332. Method HeaderOpenGL()
  333. Warning(OutOpenGL)
  334. WriteLine OutOpenGL, "Strict"
  335. WriteLine OutOpenGL, "Rem"
  336. WriteLine OutOpenGL, "bbdoc: Graphics/OpenGL 1.1"
  337. WriteLine OutOpenGL, "End Rem"
  338. WriteLine OutOpenGL, "Module Pub.OpenGL"
  339. WriteLine OutOpenGL, ""
  340. WriteLine OutOpenGL, "ModuleInfo ~qVersion: 1.02~q"
  341. WriteLine OutOpenGL, "ModuleInfo ~qLicense: SGI Free Software License B~q"
  342. WriteLine OutOpenGL, "ModuleInfo ~qModserver: brl~q"
  343. WriteLine OutOpenGL, ""
  344. WriteLine OutOpenGL, "ModuleInfo ~qHistory: 1.02 Release~q"
  345. WriteLine OutOpenGL, "ModuleInfo ~qHistory: Restored GLU~q"
  346. WriteLine OutOpenGL, ""
  347. WriteLine OutOpenGL, "?MacOS"
  348. WriteLine OutOpenGL, "Import ~q-Framework AGL~q"
  349. WriteLine OutOpenGL, "Import ~q-Framework OpenGL~q"
  350. WriteLine OutOpenGL, "?Win32"
  351. WriteLine OutOpenGL, "Import ~q-lglu32~q"
  352. WriteLine OutOpenGL, "Import ~q-lopengl32~q"
  353. WriteLine OutOpenGL, "?Linux"
  354. WriteLine OutOpenGL, "Import ~q-lGL~q"
  355. WriteLine OutOpenGL, "Import ~q-lGLU~q"
  356. WriteLine OutOpenGL, "?"
  357. WriteLine OutOpenGL, ""
  358. WriteLine OutOpenGL, "Import ~qglu.bmx~q"
  359. WriteLine OutOpenGL, ""
  360. WriteLine OutOpenGL, "Extern ~qOs~q"
  361. WriteLine OutOpenGL, ""
  362. End Method
  363. Method HeaderGlew()
  364. Warning(OutGLew)
  365. WriteLine OutGLew, "Strict"
  366. WriteLine OutGLew, ""
  367. WriteLine OutGLew, "Import ~qGL/glew.h~q"
  368. WriteLine OutGLew, ""
  369. WriteLine OutGLew, "Extern ~qOs~q"
  370. WriteLine OutGLew, ""
  371. End Method
  372. Method FooterGlew()
  373. WriteLine OutGLew, ""
  374. WriteLine OutGLew, "End Extern"
  375. End Method
  376. Method MainGlew()
  377. Warning(OutGLew)
  378. WriteLine OutGLew, "Strict"
  379. WriteLine OutGLew, ""
  380. WriteLine OutGLew, "Rem"
  381. WriteLine OutGLew, "bbdoc: Graphics/Glew"
  382. WriteLine OutGLew, "End Rem"
  383. WriteLine OutGLew, "Module Pub.Glew"
  384. WriteLine OutGLew, ""
  385. WriteLine OutGLew, "ModuleInfo ~qVersion: 1.10~q"
  386. WriteLine OutGLew, "ModuleInfo ~qAuthor: Milan Ikits, Marcelo Magallon~q"
  387. WriteLine OutGLew, "ModuleInfo ~qLicense: SGI Free Software License B~q"
  388. WriteLine OutGLew, "ModuleInfo ~qCopyright: Milan Ikits, Marcelo Magallon~q"
  389. WriteLine OutGLew, "ModuleInfo ~qModserver: BRL~q"
  390. WriteLine OutGLew, ""
  391. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.10~q"
  392. WriteLine OutGLew, "ModuleInfo ~qHistory: Updated to glew 2.1.0~q"
  393. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.09~q"
  394. WriteLine OutGLew, "ModuleInfo ~qHistory: Improved debug compilation times.~q"
  395. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.08 Release~q"
  396. WriteLine OutGLew, "ModuleInfo ~qHistory: Updated To glew 1.11.0~q"
  397. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.07 Release~q"
  398. WriteLine OutGLew, "ModuleInfo ~qHistory: Updated To glew 1.5.1~q"
  399. WriteLine OutGLew, "ModuleInfo ~qHistory: Modified glew2bmx To handle a GLint64EXT, GLuint64EXT And char - UNTESTED!~q"
  400. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.06 Release~q"
  401. WriteLine OutGLew, "ModuleInfo ~qHistory: Rebuilt To fix MacOs ld errors~q"
  402. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.05 Release~q"
  403. WriteLine OutGLew, "ModuleInfo ~qHistory: Removed glew2bmx.exe!~q"
  404. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.04 Release~q"
  405. WriteLine OutGLew, "ModuleInfo ~qHistory: Updated To glew 1.3.4~q"
  406. WriteLine OutGLew, "ModuleInfo ~qHistory: 1.03 Release~q"
  407. WriteLine OutGLew, "ModuleInfo ~qHistory: Updated To GL2.0 compatible version 1.3.3~q"
  408. WriteLine OutGLew, ""
  409. WriteLine OutGLew, "' Note : When replacing glew.h, ensure to add the following ..."
  410. WriteLine OutGLew, "'"
  411. WriteLine OutGLew, "' /* BaH : ensure it's always static */"
  412. WriteLine OutGLew, "' #ifndef GLEW_STATIC"
  413. WriteLine OutGLew, "' #define GLEW_STATIC"
  414. WriteLine OutGLew, "' #endif"
  415. WriteLine OutGLew, ""
  416. WriteLine OutGLew, "?Not opengles And Not nx"
  417. WriteLine OutGLew, ""
  418. WriteLine OutGLew, "Import ~qglew.c~q"
  419. WriteLine OutGLew, ""
  420. For Local i:Int = 0 Until pageCount
  421. WriteLine OutGLew, "Import ~q" + GlewFile(i) + "~q"
  422. Next
  423. WriteLine OutGLew, ""
  424. WriteLine OutGLew, "?"
  425. OutGlew.Close
  426. End Method
  427. Method Warning(Out:TStream)
  428. WriteLine Out, "'"
  429. WriteLine Out, "' NOTE : Generated file. Do not edit. Your changes may be lost on the next update!"
  430. WriteLine Out, "' Generated by g2bmx on " + timestamp
  431. WriteLine Out, "'"
  432. End Method
  433. End Type
  434. Type Tty
  435. Field ty:String
  436. Field exty:String
  437. End Type
  438. Type Tproto
  439. Field proto:String
  440. Field exproto:String
  441. End Type