/FallingSandpaper/jni/importgl.c

http://thelements.googlecode.com/ · C · 168 lines · 111 code · 21 blank · 36 comment · 14 complexity · f1b77eb7cff8016478988e701c3bd918 MD5 · raw file

  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. #undef WIN32
  25. #undef LINUX
  26. #ifdef _MSC_VER
  27. // Desktop or mobile Win32 environment:
  28. #define WIN32
  29. #else
  30. // Linux environment:
  31. #define LINUX
  32. #endif
  33. #ifndef DISABLE_IMPORTGL
  34. #if defined(WIN32)
  35. #define WIN32_LEAN_AND_MEAN
  36. #include <windows.h>
  37. #include <tchar.h>
  38. static HMODULE sGLESDLL = NULL;
  39. #endif // WIN32
  40. #ifdef LINUX
  41. #include <stdlib.h>
  42. #include <dlfcn.h>
  43. static void *sGLESSO = NULL;
  44. #endif // LINUX
  45. #endif /* DISABLE_IMPORTGL */
  46. #define IMPORTGL_NO_FNPTR_DEFS
  47. #define IMPORTGL_API
  48. #define IMPORTGL_FNPTRINIT = NULL
  49. #include "importgl.h"
  50. /* Imports function pointers to selected function calls in OpenGL ES Common
  51. * or Common Lite profile DLL or shared object. The function pointers are
  52. * stored as global symbols with equivalent function name but prefixed with
  53. * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
  54. * with preprocessor macros (see importgl.h).
  55. */
  56. int importGLInit()
  57. {
  58. int result = 1;
  59. #ifndef DISABLE_IMPORTGL
  60. #undef IMPORT_FUNC
  61. #ifdef WIN32
  62. sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
  63. if (sGLESDLL == NULL)
  64. sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
  65. if (sGLESDLL == NULL)
  66. return 0; // Cannot find OpenGL ES Common or Common Lite DLL.
  67. /* The following fetches address to each egl & gl function call
  68. * and stores it to the related function pointer. Casting through
  69. * void * results in warnings with VC warning level 4, which
  70. * could be fixed by casting to the true type for each fetch.
  71. */
  72. #define IMPORT_FUNC(funcName) do { \
  73. void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
  74. if (procAddress == NULL) result = 0; \
  75. *((void **)&FNPTR(funcName)) = procAddress; } while (0)
  76. #endif // WIN32
  77. #ifdef LINUX
  78. #ifdef ANDROID_NDK
  79. sGLESSO = dlopen("libGLESv1_CM.so", RTLD_NOW);
  80. #else /* !ANDROID_NDK */
  81. sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
  82. if (sGLESSO == NULL)
  83. sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
  84. #endif /* !ANDROID_NDK */
  85. if (sGLESSO == NULL)
  86. return 0; // Cannot find OpenGL ES Common or Common Lite SO.
  87. #define IMPORT_FUNC(funcName) do { \
  88. void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
  89. if (procAddress == NULL) result = 0; \
  90. *((void **)&FNPTR(funcName)) = procAddress; } while (0)
  91. #endif // LINUX
  92. #ifndef ANDROID_NDK
  93. IMPORT_FUNC(eglChooseConfig);
  94. IMPORT_FUNC(eglCreateContext);
  95. IMPORT_FUNC(eglCreateWindowSurface);
  96. IMPORT_FUNC(eglDestroyContext);
  97. IMPORT_FUNC(eglDestroySurface);
  98. IMPORT_FUNC(eglGetConfigAttrib);
  99. IMPORT_FUNC(eglGetConfigs);
  100. IMPORT_FUNC(eglGetDisplay);
  101. IMPORT_FUNC(eglGetError);
  102. IMPORT_FUNC(eglInitialize);
  103. IMPORT_FUNC(eglMakeCurrent);
  104. IMPORT_FUNC(eglSwapBuffers);
  105. IMPORT_FUNC(eglTerminate);
  106. #endif /* !ANDROID_NDK */
  107. IMPORT_FUNC(glBlendFunc);
  108. IMPORT_FUNC(glClear);
  109. IMPORT_FUNC(glClearColorx);
  110. IMPORT_FUNC(glColor4x);
  111. IMPORT_FUNC(glColorPointer);
  112. IMPORT_FUNC(glDisable);
  113. IMPORT_FUNC(glDisableClientState);
  114. IMPORT_FUNC(glDrawArrays);
  115. IMPORT_FUNC(glEnable);
  116. IMPORT_FUNC(glEnableClientState);
  117. IMPORT_FUNC(glFrustumx);
  118. IMPORT_FUNC(glGetError);
  119. IMPORT_FUNC(glLightxv);
  120. IMPORT_FUNC(glLoadIdentity);
  121. IMPORT_FUNC(glMaterialx);
  122. IMPORT_FUNC(glMaterialxv);
  123. IMPORT_FUNC(glMatrixMode);
  124. IMPORT_FUNC(glMultMatrixx);
  125. IMPORT_FUNC(glNormalPointer);
  126. IMPORT_FUNC(glPopMatrix);
  127. IMPORT_FUNC(glPushMatrix);
  128. IMPORT_FUNC(glRotatex);
  129. IMPORT_FUNC(glScalex);
  130. IMPORT_FUNC(glShadeModel);
  131. IMPORT_FUNC(glTranslatex);
  132. IMPORT_FUNC(glVertexPointer);
  133. IMPORT_FUNC(glViewport);
  134. #endif /* DISABLE_IMPORTGL */
  135. return result;
  136. }
  137. void importGLDeinit()
  138. {
  139. #ifndef DISABLE_IMPORTGL
  140. #ifdef WIN32
  141. FreeLibrary(sGLESDLL);
  142. #endif
  143. #ifdef LINUX
  144. dlclose(sGLESSO);
  145. #endif
  146. #endif /* DISABLE_IMPORTGL */
  147. }