aboutsummaryrefslogtreecommitdiffstats
path: root/code/macosx/GenerateQGL.pl
blob: 5fc8f967e06e51d70a74929bcb8a0d2a1b985018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl

open(INPUT_FILE, ">/tmp/input-$$.h") || die "$!";
print INPUT_FILE "#import <OpenGL/gl.h>\n";
close INPUT_FILE;
open(CPP, "cpp /tmp/input-$$.h|") || die "$!";

print "/**** This file is autogenerated.  Run GenerateQGL.pl to update it ****/\n\n";

print "#ifdef QGL_LOG_GL_CALLS\n";
print "extern unsigned int QGLLogGLCalls;\n";
print "extern FILE *QGLDebugFile(void);\n";
print "#endif\n\n";

print "extern void QGLCheckError(const char *message);\n";
print "extern unsigned int QGLBeginStarted;\n\n";
print "// This has to be done to avoid infinite recursion between our glGetError wrapper and QGLCheckError()\n";
print "static inline GLenum _glGetError(void) {\n";
print "    return glGetError();\n";
print "}\n\n";

@functionNames = ();

while (<CPP>) {
    chop;
    /^extern/ || next;
    s/extern //;
    print "// $_\n";

    # This approach is necessary to deal with glGetString whos type isn't a single word
    ($type, $rest) = m/(.+)\s+(gl.*)/;
#    print "type='$type'\n";
#    print "rest='$rest'\n";

    ($name, $argString) = ($rest =~ m/(\w+).*\s*\((.*)\)/);
    $isVoid = ($type =~ m/void/);
    push(@functionNames, $name);

#    print "name=$name\n";
#    print "argString=$argString\n";
#    print "argCount=$#args\n";

    # Parse the argument list into two arrays, one of types and one of argument names
    if ($argString =~ m/^void$/) {
        @args = ();
    } else {
        @args = split(",", $argString);
    }
    @argTypes = ();
    @argNames = ();
    for $arg (@args) {
        ($argType, $argName) = ($arg =~ m/(.*[ \*])([_a-zA-Z0-9]+)/);
        $argType =~ s/^ *//;
        $argType =~ s/ *$//;

        push(@argTypes, $argType);
        push(@argNames, $argName);
#        print "argType='$argType'\n";
#        print "argName='$argName'\n";
    }


    print "static inline $type q$name($argString)\n";
    print "{\n";

    if (! $isVoid) {
        print "    $type returnValue;\n";
    }

    print "#if !defined(NDEBUG) && defined(QGL_LOG_GL_CALLS)\n";
    print "    if (QGLLogGLCalls)\n";
    print "        fprintf(QGLDebugFile(), \"$name(";

    if ($#argTypes >= 0) {
        for ($i = 0; $i <= $#argTypes; $i++) {
            $argType = $argTypes[$i];
            $argName = $argNames[$i];
            $_ = $argType;
            if (/^GLenum$/ || /^GLuint$/ || /^GLbitfield$/) {
                print "$argName=%lu";
            } elsif (/^GLsizei$/ || /^GLint$/) {
                print "$argName=%ld";
            } elsif (/^GLfloat$/ || /^GLdouble$/ || /^GLclampf$/ || /^GLclampd$/) {
                print "$argName=%f";
            } elsif (/^GLbyte$/) {
                print "$argName=%d";
            } elsif (/^GLubyte$/) {
                print "$argName=%u";
            } elsif (/^GLshort$/) {
                print "$argName=%d";
            } elsif (/^GLushort$/) {
                print "$argName=%u";
            } elsif (/^GLboolean$/) {
                print "$argName=%u";
            } elsif (/\*$/) {
                # 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...
                print "$argName=%p";
            } else {
                print STDERR "Unknown type '$argType'\n";
                exit(1);
            }

            print ", " if ($i != $#argTypes);
        }
    } else {
        print "void";
    }

    print ")\\n\"";
    print ", " if $#argTypes >= 0;
    print join(", ", @argNames);
    print ");\n";
    print "#endif\n";

    if (! $isVoid) {
        print "    returnValue = ";
    } else {
        print "    ";
    }
    print "$name(" . join(", ", @argNames) . ");\n";

    print "#if !defined(NDEBUG) && defined(QGL_CHECK_GL_ERRORS)\n";
    if ($name eq "glBegin") {
        print "    QGLBeginStarted++;\n";
    }
    if ($name eq "glEnd") {
        print "    QGLBeginStarted--;\n";
    }
    print "    if (!QGLBeginStarted)\n";
    print "        QGLCheckError(\"$name\");\n";
    print "#endif\n";

    if (! $isVoid) {
        print "    return returnValue;\n";
    }
    
    print "}\n\n";
}


print "// Prevent calls to the 'normal' GL functions\n";
for $name (@functionNames) {
    print "#define $name CALL_THE_QGL_VERSION_OF_$name\n";
}