1/* San Angeles Observation OpenGL ES version example
2 * Copyright 2004-2005 Jetro Lauha
3 * All rights reserved.
4 * Web: http://iki.fi/jetro/
5 *
6 * This source is free software; you can redistribute it and/or
7 * modify it under the terms of EITHER:
8 *   (1) The GNU Lesser General Public License as published by the Free
9 *       Software Foundation; either version 2.1 of the License, or (at
10 *       your option) any later version. The text of the GNU Lesser
11 *       General Public License is included with this source in the
12 *       file LICENSE-LGPL.txt.
13 *   (2) The BSD-style license that is included with this source in
14 *       the file LICENSE-BSD.txt.
15 *
16 * This source is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
19 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
20 *
21 * $Id: importgl.c,v 1.4 2005/02/08 18:42:55 tonic Exp $
22 * $Revision: 1.4 $
23 */
24
25#undef WIN32
26#undef LINUX
27#ifdef _MSC_VER
28// Desktop or mobile Win32 environment:
29#define WIN32
30#else
31// Linux environment:
32#define LINUX
33#endif
34
35#ifndef DISABLE_IMPORTGL
36
37#if defined(WIN32)
38#define WIN32_LEAN_AND_MEAN
39#include <windows.h>
40#include <tchar.h>
41static HMODULE sGLESDLL = NULL;
42#endif // WIN32
43
44#ifdef LINUX
45#include <stdlib.h>
46#include <dlfcn.h>
47static void *sGLESSO = NULL;
48#endif // LINUX
49
50#endif /* DISABLE_IMPORTGL */
51
52#define IMPORTGL_NO_FNPTR_DEFS
53#define IMPORTGL_API
54#define IMPORTGL_FNPTRINIT = NULL
55#include "importgl.h"
56
57
58/* Imports function pointers to selected function calls in OpenGL ES Common
59 * or Common Lite profile DLL or shared object. The function pointers are
60 * stored as global symbols with equivalent function name but prefixed with
61 * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
62 * with preprocessor macros (see importgl.h).
63 */
64int importGLInit()
65{
66    int result = 1;
67
68#ifndef DISABLE_IMPORTGL
69
70#undef IMPORT_FUNC
71
72#ifdef WIN32
73    sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
74    if (sGLESDLL == NULL)
75        sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
76    if (sGLESDLL == NULL)
77        return 0;   // Cannot find OpenGL ES Common or Common Lite DLL.
78
79    /* The following fetches address to each egl & gl function call
80     * and stores it to the related function pointer. Casting through
81     * void * results in warnings with VC warning level 4, which
82     * could be fixed by casting to the true type for each fetch.
83     */
84#define IMPORT_FUNC(funcName) do { \
85        void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
86        if (procAddress == NULL) result = 0; \
87        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
88#endif // WIN32
89
90#ifdef LINUX
91#ifdef ANDROID_NDK
92    sGLESSO = dlopen("libGLESv1_CM.so", RTLD_NOW);
93#else /* !ANDROID_NDK */
94    sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
95    if (sGLESSO == NULL)
96        sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
97#endif /* !ANDROID_NDK */
98    if (sGLESSO == NULL)
99        return 0;   // Cannot find OpenGL ES Common or Common Lite SO.
100
101#define IMPORT_FUNC(funcName) do { \
102        void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
103        if (procAddress == NULL) result = 0; \
104        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
105#endif // LINUX
106
107#ifndef ANDROID_NDK
108    IMPORT_FUNC(eglChooseConfig);
109    IMPORT_FUNC(eglCreateContext);
110    IMPORT_FUNC(eglCreateWindowSurface);
111    IMPORT_FUNC(eglDestroyContext);
112    IMPORT_FUNC(eglDestroySurface);
113    IMPORT_FUNC(eglGetConfigAttrib);
114    IMPORT_FUNC(eglGetConfigs);
115    IMPORT_FUNC(eglGetDisplay);
116    IMPORT_FUNC(eglGetError);
117    IMPORT_FUNC(eglInitialize);
118    IMPORT_FUNC(eglMakeCurrent);
119    IMPORT_FUNC(eglSwapBuffers);
120    IMPORT_FUNC(eglTerminate);
121#endif /* !ANDROID_NDK */
122
123    IMPORT_FUNC(glBlendFunc);
124    IMPORT_FUNC(glClear);
125    IMPORT_FUNC(glClearColorx);
126    IMPORT_FUNC(glColor4x);
127    IMPORT_FUNC(glColorPointer);
128    IMPORT_FUNC(glDisable);
129    IMPORT_FUNC(glDisableClientState);
130    IMPORT_FUNC(glDrawArrays);
131    IMPORT_FUNC(glEnable);
132    IMPORT_FUNC(glEnableClientState);
133    IMPORT_FUNC(glFrustumx);
134    IMPORT_FUNC(glGetError);
135    IMPORT_FUNC(glLightxv);
136    IMPORT_FUNC(glLoadIdentity);
137    IMPORT_FUNC(glMaterialx);
138    IMPORT_FUNC(glMaterialxv);
139    IMPORT_FUNC(glMatrixMode);
140    IMPORT_FUNC(glMultMatrixx);
141    IMPORT_FUNC(glNormalPointer);
142    IMPORT_FUNC(glPopMatrix);
143    IMPORT_FUNC(glPushMatrix);
144    IMPORT_FUNC(glRotatex);
145    IMPORT_FUNC(glScalex);
146    IMPORT_FUNC(glShadeModel);
147    IMPORT_FUNC(glTranslatex);
148    IMPORT_FUNC(glVertexPointer);
149    IMPORT_FUNC(glViewport);
150
151#endif /* DISABLE_IMPORTGL */
152
153    return result;
154}
155
156
157void importGLDeinit()
158{
159#ifndef DISABLE_IMPORTGL
160#ifdef WIN32
161    FreeLibrary(sGLESDLL);
162#endif
163
164#ifdef LINUX
165    dlclose(sGLESSO);
166#endif
167#endif /* DISABLE_IMPORTGL */
168}
169