Implemented an ugly hack to silence a C warning about casting a void pointer to a...
[neverball] / share / glext.c
1 /*
2  * Copyright (C) 2003-2011 Neverball authors
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <SDL.h>
16
17 #include "glext.h"
18
19 /*---------------------------------------------------------------------------*/
20
21 #ifndef CONF_OPENGLES
22
23 PFNGLACTIVETEXTURE_PROC    glActiveTexture_;
24
25 PFNGLGENBUFFERS_PROC       glGenBuffers_;
26 PFNGLBINDBUFFER_PROC       glBindBuffer_;
27 PFNGLBUFFERDATA_PROC       glBufferData_;
28 PFNGLBUFFERSUBDATA_PROC    glBufferSubData_;
29 PFNGLDELETEBUFFERS_PROC    glDeleteBuffers_;
30 PFNGLISBUFFER_PROC         glIsBuffer_;
31
32 PFNGLPOINTPARAMETERFV_PROC glPointParameterfv_;
33
34 #endif
35
36 /*---------------------------------------------------------------------------*/
37
38 int glext_check(const char *needle)
39 {
40     const GLubyte *haystack, *c;
41
42     /* Search for the given string in the OpenGL extension strings. */
43
44     for (haystack = glGetString(GL_EXTENSIONS); *haystack; haystack++)
45     {
46         for (c = (const GLubyte *) needle; *c && *haystack; c++, haystack++)
47             if (*c != *haystack)
48                 break;
49
50         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
51             return 1;
52     }
53
54     return 0;
55 }
56
57 /*---------------------------------------------------------------------------*/
58
59 #define SDL_GL_GFPA(fun, str) do {     \
60     ptr = SDL_GL_GetProcAddress(str);  \
61     memcpy(&fun, &ptr, sizeof (void *)); \
62 } while(0)
63
64 /*---------------------------------------------------------------------------*/
65
66 void glext_init(void)
67 {
68 #ifndef CONF_OPENGLES
69
70     void *ptr;
71
72     if (glext_check("ARB_multitexture"))
73         SDL_GL_GFPA(glActiveTexture_, "glActiveTextureARB");
74
75     if (glext_check("ARB_vertex_buffer_object"))
76     {
77         SDL_GL_GFPA(glGenBuffers_,    "glGenBuffersARB");
78         SDL_GL_GFPA(glBindBuffer_,    "glBindBufferARB");
79         SDL_GL_GFPA(glBufferData_,    "glBufferDataARB");
80         SDL_GL_GFPA(glBufferSubData_, "glBufferSubDataARB");
81         SDL_GL_GFPA(glDeleteBuffers_, "glDeleteBuffersARB");
82         SDL_GL_GFPA(glIsBuffer_,      "glIsBufferARB");
83     }
84
85     if (glext_check("ARB_point_parameters"))
86         SDL_GL_GFPA(glPointParameterfv_, "glPointParameterfvARB");
87
88 #endif
89 }
90
91 /*---------------------------------------------------------------------------*/