app-linux.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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: app-linux.c,v 1.4 2005/02/08 18:42:48 tonic Exp $
  22. * $Revision: 1.4 $
  23. *
  24. * Parts of this source file is based on test/example code from
  25. * GLESonGL implementation by David Blythe. Here is copy of the
  26. * license notice from that source:
  27. *
  28. * Copyright (C) 2003 David Blythe All Rights Reserved.
  29. *
  30. * Permission is hereby granted, free of charge, to any person obtaining a
  31. * copy of this software and associated documentation files (the "Software"),
  32. * to deal in the Software without restriction, including without limitation
  33. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  34. * and/or sell copies of the Software, and to permit persons to whom the
  35. * Software is furnished to do so, subject to the following conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be included
  38. * in all copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  41. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  42. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  43. * DAVID BLYTHE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  44. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  45. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  46. */
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <sys/time.h>
  50. #include <X11/Xlib.h>
  51. #include <X11/Xutil.h>
  52. #include <X11/keysym.h>
  53. #include "importgl.h"
  54. #include "app.h"
  55. int gAppAlive = 1;
  56. static const char sAppName[] =
  57. "San Angeles Observation OpenGL ES version example (Linux)";
  58. static Display *sDisplay;
  59. static Window sWindow;
  60. static int sWindowWidth = WINDOW_DEFAULT_WIDTH;
  61. static int sWindowHeight = WINDOW_DEFAULT_HEIGHT;
  62. static EGLDisplay sEglDisplay = EGL_NO_DISPLAY;
  63. static EGLConfig sEglConfig;
  64. static EGLContext sEglContext = EGL_NO_CONTEXT;
  65. static EGLSurface sEglSurface = EGL_NO_SURFACE;
  66. static void checkGLErrors()
  67. {
  68. GLenum error = glGetError();
  69. if (error != GL_NO_ERROR)
  70. fprintf(stderr, "GL Error: 0x%04x\n", (int)error);
  71. }
  72. static void checkEGLErrors()
  73. {
  74. EGLint error = eglGetError();
  75. // GLESonGL seems to be returning 0 when there is no errors?
  76. if (error && error != EGL_SUCCESS)
  77. fprintf(stderr, "EGL Error: 0x%04x\n", (int)error);
  78. }
  79. // Initializes and opens both X11 display and OpenGL ES.
  80. static int initGraphics()
  81. {
  82. static const EGLint configAttribs[] =
  83. {
  84. #if (WINDOW_BPP == 16)
  85. EGL_RED_SIZE, 5,
  86. EGL_GREEN_SIZE, 5,
  87. EGL_BLUE_SIZE, 5,
  88. #elif (WINDOW_BPP == 32)
  89. EGL_RED_SIZE, 8,
  90. EGL_GREEN_SIZE, 8,
  91. EGL_BLUE_SIZE, 8,
  92. #else
  93. #error WINDOW_BPP must be 16 or 32
  94. #endif
  95. EGL_DEPTH_SIZE, 16,
  96. EGL_ALPHA_SIZE, EGL_DONT_CARE,
  97. EGL_STENCIL_SIZE, EGL_DONT_CARE,
  98. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  99. EGL_NONE
  100. };
  101. EGLBoolean success;
  102. EGLint numConfigs;
  103. EGLint majorVersion;
  104. EGLint minorVersion;
  105. int importGLResult;
  106. importGLResult = importGLInit();
  107. if (!importGLResult)
  108. return 0;
  109. sDisplay = XOpenDisplay(NULL);
  110. sEglDisplay = eglGetDisplay(sDisplay);
  111. success = eglInitialize(sEglDisplay, &majorVersion, &minorVersion);
  112. if (success != EGL_FALSE)
  113. success = eglGetConfigs(sEglDisplay, NULL, 0, &numConfigs);
  114. if (success != EGL_FALSE)
  115. success = eglChooseConfig(sEglDisplay, configAttribs,
  116. &sEglConfig, 1, &numConfigs);
  117. if (success != EGL_FALSE)
  118. {
  119. sEglContext = eglCreateContext(sEglDisplay, sEglConfig, NULL, NULL);
  120. if (sEglContext == EGL_NO_CONTEXT)
  121. success = EGL_FALSE;
  122. }
  123. if (success != EGL_FALSE)
  124. {
  125. XSetWindowAttributes swa;
  126. XVisualInfo *vi, tmp;
  127. XSizeHints sh;
  128. int n;
  129. EGLint vid;
  130. eglGetConfigAttrib(sEglDisplay, sEglConfig,
  131. EGL_NATIVE_VISUAL_ID, &vid);
  132. tmp.visualid = vid;
  133. vi = XGetVisualInfo(sDisplay, VisualIDMask, &tmp, &n);
  134. swa.colormap = XCreateColormap(sDisplay,
  135. RootWindow(sDisplay, vi->screen),
  136. vi->visual, AllocNone);
  137. sh.flags = PMinSize | PMaxSize;
  138. sh.min_width = sh.max_width = sWindowWidth;
  139. sh.min_height = sh.max_height = sWindowHeight;
  140. swa.border_pixel = 0;
  141. swa.event_mask = ExposureMask | StructureNotifyMask |
  142. KeyPressMask | ButtonPressMask | ButtonReleaseMask;
  143. sWindow = XCreateWindow(sDisplay, RootWindow(sDisplay, vi->screen),
  144. 0, 0, sWindowWidth, sWindowHeight,
  145. 0, vi->depth, InputOutput, vi->visual,
  146. CWBorderPixel | CWColormap | CWEventMask,
  147. &swa);
  148. XMapWindow(sDisplay, sWindow);
  149. XSetStandardProperties(sDisplay, sWindow, sAppName, sAppName,
  150. None, (void *)0, 0, &sh);
  151. }
  152. if (success != EGL_FALSE)
  153. {
  154. sEglSurface = eglCreateWindowSurface(sEglDisplay, sEglConfig,
  155. (NativeWindowType)sWindow, NULL);
  156. if (sEglSurface == EGL_NO_SURFACE)
  157. success = EGL_FALSE;
  158. }
  159. if (success != EGL_FALSE)
  160. success = eglMakeCurrent(sEglDisplay, sEglSurface,
  161. sEglSurface, sEglContext);
  162. if (success == EGL_FALSE)
  163. checkEGLErrors();
  164. return success != EGL_FALSE;
  165. }
  166. static void deinitGraphics()
  167. {
  168. eglMakeCurrent(sEglDisplay, NULL, NULL, NULL);
  169. eglDestroyContext(sEglDisplay, sEglContext);
  170. eglDestroySurface(sEglDisplay, sEglSurface);
  171. eglTerminate(sEglDisplay);
  172. importGLDeinit();
  173. }
  174. int main(int argc, char *argv[])
  175. {
  176. // not referenced:
  177. argc = argc;
  178. argv = argv;
  179. if (!initGraphics())
  180. {
  181. fprintf(stderr, "Graphics initialization failed.\n");
  182. return EXIT_FAILURE;
  183. }
  184. appInit();
  185. while (gAppAlive)
  186. {
  187. struct timeval timeNow;
  188. while (XPending(sDisplay))
  189. {
  190. XEvent ev;
  191. XNextEvent(sDisplay, &ev);
  192. switch (ev.type)
  193. {
  194. case KeyPress:
  195. {
  196. unsigned int keycode, keysym;
  197. keycode = ((XKeyEvent *)&ev)->keycode;
  198. keysym = XKeycodeToKeysym(sDisplay, keycode, 0);
  199. if (keysym == XK_Return || keysym == XK_Escape)
  200. gAppAlive = 0;
  201. }
  202. break;
  203. }
  204. }
  205. if (gAppAlive)
  206. {
  207. gettimeofday(&timeNow, NULL);
  208. appRender(timeNow.tv_sec * 1000 + timeNow.tv_usec / 1000,
  209. sWindowWidth, sWindowHeight);
  210. checkGLErrors();
  211. eglSwapBuffers(sEglDisplay, sEglSurface);
  212. checkEGLErrors();
  213. }
  214. }
  215. appDeinit();
  216. deinitGraphics();
  217. return EXIT_SUCCESS;
  218. }