GenerateQGL.pl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/perl
  2. open(INPUT_FILE, ">/tmp/input-$$.h") || die "$!";
  3. print INPUT_FILE "#import <OpenGL/gl.h>\n";
  4. close INPUT_FILE;
  5. open(CPP, "cpp /tmp/input-$$.h|") || die "$!";
  6. print "/**** This file is autogenerated. Run GenerateQGL.pl to update it ****/\n\n";
  7. print "#ifdef QGL_LOG_GL_CALLS\n";
  8. print "extern unsigned int QGLLogGLCalls;\n";
  9. print "extern FILE *QGLDebugFile(void);\n";
  10. print "#endif\n\n";
  11. print "extern void QGLCheckError(const char *message);\n";
  12. print "extern unsigned int QGLBeginStarted;\n\n";
  13. print "// This has to be done to avoid infinite recursion between our glGetError wrapper and QGLCheckError()\n";
  14. print "static inline GLenum _glGetError(void) {\n";
  15. print " return glGetError();\n";
  16. print "}\n\n";
  17. @functionNames = ();
  18. while (<CPP>) {
  19. chop;
  20. /^extern/ || next;
  21. s/extern //;
  22. print "// $_\n";
  23. # This approach is necessary to deal with glGetString whos type isn't a single word
  24. ($type, $rest) = m/(.+)\s+(gl.*)/;
  25. # print "type='$type'\n";
  26. # print "rest='$rest'\n";
  27. ($name, $argString) = ($rest =~ m/(\w+).*\s*\((.*)\)/);
  28. $isVoid = ($type =~ m/void/);
  29. push(@functionNames, $name);
  30. # print "name=$name\n";
  31. # print "argString=$argString\n";
  32. # print "argCount=$#args\n";
  33. # Parse the argument list into two arrays, one of types and one of argument names
  34. if ($argString =~ m/^void$/) {
  35. @args = ();
  36. } else {
  37. @args = split(",", $argString);
  38. }
  39. @argTypes = ();
  40. @argNames = ();
  41. for $arg (@args) {
  42. ($argType, $argName) = ($arg =~ m/(.*[ \*])([_a-zA-Z0-9]+)/);
  43. $argType =~ s/^ *//;
  44. $argType =~ s/ *$//;
  45. push(@argTypes, $argType);
  46. push(@argNames, $argName);
  47. # print "argType='$argType'\n";
  48. # print "argName='$argName'\n";
  49. }
  50. print "static inline $type q$name($argString)\n";
  51. print "{\n";
  52. if (! $isVoid) {
  53. print " $type returnValue;\n";
  54. }
  55. print "#if !defined(NDEBUG) && defined(QGL_LOG_GL_CALLS)\n";
  56. print " if (QGLLogGLCalls)\n";
  57. print " fprintf(QGLDebugFile(), \"$name(";
  58. if ($#argTypes >= 0) {
  59. for ($i = 0; $i <= $#argTypes; $i++) {
  60. $argType = $argTypes[$i];
  61. $argName = $argNames[$i];
  62. $_ = $argType;
  63. if (/^GLenum$/ || /^GLuint$/ || /^GLbitfield$/) {
  64. print "$argName=%lu";
  65. } elsif (/^GLsizei$/ || /^GLint$/) {
  66. print "$argName=%ld";
  67. } elsif (/^GLfloat$/ || /^GLdouble$/ || /^GLclampf$/ || /^GLclampd$/) {
  68. print "$argName=%f";
  69. } elsif (/^GLbyte$/) {
  70. print "$argName=%d";
  71. } elsif (/^GLubyte$/) {
  72. print "$argName=%u";
  73. } elsif (/^GLshort$/) {
  74. print "$argName=%d";
  75. } elsif (/^GLushort$/) {
  76. print "$argName=%u";
  77. } elsif (/^GLboolean$/) {
  78. print "$argName=%u";
  79. } elsif (/\*$/) {
  80. # TJW -- Later we should look at the count specified in the function name, look at the basic type and print out an array. Or we could just special case them...
  81. print "$argName=%p";
  82. } else {
  83. print STDERR "Unknown type '$argType'\n";
  84. exit(1);
  85. }
  86. print ", " if ($i != $#argTypes);
  87. }
  88. } else {
  89. print "void";
  90. }
  91. print ")\\n\"";
  92. print ", " if $#argTypes >= 0;
  93. print join(", ", @argNames);
  94. print ");\n";
  95. print "#endif\n";
  96. if (! $isVoid) {
  97. print " returnValue = ";
  98. } else {
  99. print " ";
  100. }
  101. print "$name(" . join(", ", @argNames) . ");\n";
  102. print "#if !defined(NDEBUG) && defined(QGL_CHECK_GL_ERRORS)\n";
  103. if ($name eq "glBegin") {
  104. print " QGLBeginStarted++;\n";
  105. }
  106. if ($name eq "glEnd") {
  107. print " QGLBeginStarted--;\n";
  108. }
  109. print " if (!QGLBeginStarted)\n";
  110. print " QGLCheckError(\"$name\");\n";
  111. print "#endif\n";
  112. if (! $isVoid) {
  113. print " return returnValue;\n";
  114. }
  115. print "}\n\n";
  116. }
  117. print "// Prevent calls to the 'normal' GL functions\n";
  118. for $name (@functionNames) {
  119. print "#define $name CALL_THE_QGL_VERSION_OF_$name\n";
  120. }