]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/glwrap_funcs.c
7fc5adfddf815d108ca98d15565f1970e0248725
[gldbg.git] / flavors / gl / source / glwrap_funcs.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <dlfcn.h>
9 #include "arraysize.h"
10 #include "functions.h"
11 #include "glwrap.h"
12 #include "opengl.h"
13
14 int in_begin_block = 0;
15 GLenum cur_error = GL_NO_ERROR;
16
17 static void check_error()
18 {
19         GLenum (*orig_glGetError)() = 0;
20         GLenum code;
21
22         if(in_begin_block)
23                 return;
24
25         if(!orig_glGetError)
26                 orig_glGetError = glsym("glGetError");
27
28         code = orig_glGetError();
29         if(code!=GL_NO_ERROR)
30         {
31                 begin_packet(FUNC_GLDERROR);
32                 write_int(code);
33                 send_packet();
34
35                 if(cur_error==GL_NO_ERROR)
36                         cur_error = code;
37         }
38 }
39
40 void APIENTRY glBegin(GLenum mode)
41 {
42         static void (*orig)(GLenum);
43         if(!orig)
44                 orig = glsym("glBegin");
45         orig(mode);
46
47         begin_packet(FUNC_GLBEGIN);
48         write_int(mode);
49         send_packet();
50
51         in_begin_block = 1;
52 }
53
54 void APIENTRY glEnd()
55 {
56         static void (*orig)();
57         if(!orig)
58                 orig = glsym("glEnd");
59         orig();
60
61         begin_packet(FUNC_GLEND);
62         send_packet();
63
64         in_begin_block = 0;
65         check_error();
66 }
67
68 GLenum APIENTRY glGetError()
69 {
70         GLenum ret = GL_NO_ERROR;
71
72         if(in_begin_block)
73         {
74                 if(cur_error==GL_NO_ERROR)
75                         cur_error = GL_INVALID_OPERATION;
76         }
77         else
78         {
79                 ret = cur_error;
80                 cur_error = GL_NO_ERROR;
81         }
82
83         begin_packet(FUNC_GLGETERROR);
84         write_int(ret);
85         send_packet();
86
87         return ret;
88 }
89
90 void (*glXGetProcAddress(const GLubyte *procname))(void)
91 {
92         void *handle = 0;
93         void (*ret)() = 0;
94
95         if(glsym((const char *)procname))
96         {
97                 handle = dlopen(NULL, RTLD_LAZY);
98                 ret = dlsym(handle, (const char *)procname);
99         }
100
101         begin_packet(FUNC_GLXGETPROCADDRESS);
102         write_string((const char *)procname);
103         write_pointer(ret);
104         send_packet();
105
106         return ret;
107 }
108
109 void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress")));
110
111 #include "gensrc/glwrap.funcs"