testgles.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include "SDL_test_common.h"
  15. #if defined(__IPHONEOS__) || defined(__ANDROID__)
  16. #define HAVE_OPENGLES
  17. #endif
  18. #ifdef HAVE_OPENGLES
  19. #include "SDL_opengles.h"
  20. static SDLTest_CommonState *state;
  21. static SDL_GLContext *context = NULL;
  22. static int depth = 16;
  23. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  24. static void
  25. quit(int rc)
  26. {
  27. int i;
  28. if (context != NULL) {
  29. for (i = 0; i < state->num_windows; i++) {
  30. if (context[i]) {
  31. SDL_GL_DeleteContext(context[i]);
  32. }
  33. }
  34. SDL_free(context);
  35. }
  36. SDLTest_CommonQuit(state);
  37. exit(rc);
  38. }
  39. static void
  40. Render()
  41. {
  42. static GLubyte color[8][4] = { {255, 0, 0, 0},
  43. {255, 0, 0, 255},
  44. {0, 255, 0, 255},
  45. {0, 255, 0, 255},
  46. {0, 255, 0, 255},
  47. {255, 255, 255, 255},
  48. {255, 0, 255, 255},
  49. {0, 0, 255, 255}
  50. };
  51. static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
  52. {0.5f, -0.5f, -0.5f},
  53. {-0.5f, -0.5f, -0.5f},
  54. {-0.5f, 0.5f, -0.5f},
  55. {-0.5f, 0.5f, 0.5f},
  56. {0.5f, 0.5f, 0.5f},
  57. {0.5f, -0.5f, 0.5f},
  58. {-0.5f, -0.5f, 0.5f}
  59. };
  60. static GLubyte indices[36] = { 0, 3, 4,
  61. 4, 5, 0,
  62. 0, 5, 6,
  63. 6, 1, 0,
  64. 6, 7, 2,
  65. 2, 1, 6,
  66. 7, 4, 3,
  67. 3, 2, 7,
  68. 5, 4, 7,
  69. 7, 6, 5,
  70. 2, 3, 1,
  71. 3, 0, 1
  72. };
  73. /* Do our drawing, too. */
  74. glClearColor(0.0, 0.0, 0.0, 1.0);
  75. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  76. /* Draw the cube */
  77. glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
  78. glEnableClientState(GL_COLOR_ARRAY);
  79. glVertexPointer(3, GL_FLOAT, 0, cube);
  80. glEnableClientState(GL_VERTEX_ARRAY);
  81. glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
  82. glMatrixMode(GL_MODELVIEW);
  83. glRotatef(5.0, 1.0, 1.0, 1.0);
  84. }
  85. int
  86. main(int argc, char *argv[])
  87. {
  88. int fsaa, accel;
  89. int value;
  90. int i, done;
  91. SDL_DisplayMode mode;
  92. SDL_Event event;
  93. Uint32 then, now, frames;
  94. int status;
  95. /* Enable standard application logging */
  96. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  97. /* Initialize parameters */
  98. fsaa = 0;
  99. accel = 0;
  100. /* Initialize test framework */
  101. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  102. if (!state) {
  103. return 1;
  104. }
  105. for (i = 1; i < argc;) {
  106. int consumed;
  107. consumed = SDLTest_CommonArg(state, i);
  108. if (consumed == 0) {
  109. if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
  110. ++fsaa;
  111. consumed = 1;
  112. } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
  113. ++accel;
  114. consumed = 1;
  115. } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
  116. i++;
  117. if (!argv[i]) {
  118. consumed = -1;
  119. } else {
  120. depth = SDL_atoi(argv[i]);
  121. consumed = 1;
  122. }
  123. } else {
  124. consumed = -1;
  125. }
  126. }
  127. if (consumed < 0) {
  128. SDL_Log("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
  129. SDLTest_CommonUsage(state));
  130. quit(1);
  131. }
  132. i += consumed;
  133. }
  134. /* Set OpenGL parameters */
  135. state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
  136. state->gl_red_size = 5;
  137. state->gl_green_size = 5;
  138. state->gl_blue_size = 5;
  139. state->gl_depth_size = depth;
  140. state->gl_major_version = 1;
  141. state->gl_minor_version = 1;
  142. state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  143. if (fsaa) {
  144. state->gl_multisamplebuffers=1;
  145. state->gl_multisamplesamples=fsaa;
  146. }
  147. if (accel) {
  148. state->gl_accelerated=1;
  149. }
  150. if (!SDLTest_CommonInit(state)) {
  151. quit(2);
  152. }
  153. context = SDL_calloc(state->num_windows, sizeof(context));
  154. if (context == NULL) {
  155. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
  156. quit(2);
  157. }
  158. /* Create OpenGL ES contexts */
  159. for (i = 0; i < state->num_windows; i++) {
  160. context[i] = SDL_GL_CreateContext(state->windows[i]);
  161. if (!context[i]) {
  162. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
  163. quit(2);
  164. }
  165. }
  166. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  167. SDL_GL_SetSwapInterval(1);
  168. } else {
  169. SDL_GL_SetSwapInterval(0);
  170. }
  171. SDL_GetCurrentDisplayMode(0, &mode);
  172. SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
  173. SDL_Log("\n");
  174. SDL_Log("Vendor : %s\n", glGetString(GL_VENDOR));
  175. SDL_Log("Renderer : %s\n", glGetString(GL_RENDERER));
  176. SDL_Log("Version : %s\n", glGetString(GL_VERSION));
  177. SDL_Log("Extensions : %s\n", glGetString(GL_EXTENSIONS));
  178. SDL_Log("\n");
  179. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  180. if (!status) {
  181. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  182. } else {
  183. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n",
  184. SDL_GetError());
  185. }
  186. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  187. if (!status) {
  188. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  189. } else {
  190. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
  191. SDL_GetError());
  192. }
  193. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  194. if (!status) {
  195. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  196. } else {
  197. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
  198. SDL_GetError());
  199. }
  200. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  201. if (!status) {
  202. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
  203. } else {
  204. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
  205. SDL_GetError());
  206. }
  207. if (fsaa) {
  208. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  209. if (!status) {
  210. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  211. } else {
  212. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  213. SDL_GetError());
  214. }
  215. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  216. if (!status) {
  217. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  218. value);
  219. } else {
  220. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  221. SDL_GetError());
  222. }
  223. }
  224. if (accel) {
  225. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  226. if (!status) {
  227. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
  228. } else {
  229. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  230. SDL_GetError());
  231. }
  232. }
  233. /* Set rendering settings for each context */
  234. for (i = 0; i < state->num_windows; ++i) {
  235. float aspectAdjust;
  236. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  237. if (status) {
  238. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  239. /* Continue for next window */
  240. continue;
  241. }
  242. aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h);
  243. glViewport(0, 0, state->window_w, state->window_h);
  244. glMatrixMode(GL_PROJECTION);
  245. glLoadIdentity();
  246. glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
  247. glMatrixMode(GL_MODELVIEW);
  248. glLoadIdentity();
  249. glEnable(GL_DEPTH_TEST);
  250. glDepthFunc(GL_LESS);
  251. glShadeModel(GL_SMOOTH);
  252. }
  253. /* Main render loop */
  254. frames = 0;
  255. then = SDL_GetTicks();
  256. done = 0;
  257. while (!done) {
  258. /* Check for events */
  259. ++frames;
  260. while (SDL_PollEvent(&event)) {
  261. switch (event.type) {
  262. case SDL_WINDOWEVENT:
  263. switch (event.window.event) {
  264. case SDL_WINDOWEVENT_RESIZED:
  265. for (i = 0; i < state->num_windows; ++i) {
  266. if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
  267. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  268. if (status) {
  269. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  270. break;
  271. }
  272. /* Change view port to the new window dimensions */
  273. glViewport(0, 0, event.window.data1, event.window.data2);
  274. /* Update window content */
  275. Render();
  276. SDL_GL_SwapWindow(state->windows[i]);
  277. break;
  278. }
  279. }
  280. break;
  281. }
  282. }
  283. SDLTest_CommonEvent(state, &event, &done);
  284. }
  285. for (i = 0; i < state->num_windows; ++i) {
  286. if (state->windows[i] == NULL)
  287. continue;
  288. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  289. if (status) {
  290. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  291. /* Continue for next window */
  292. continue;
  293. }
  294. Render();
  295. SDL_GL_SwapWindow(state->windows[i]);
  296. }
  297. }
  298. /* Print out some timing information */
  299. now = SDL_GetTicks();
  300. if (now > then) {
  301. SDL_Log("%2.2f frames per second\n",
  302. ((double) frames * 1000) / (now - then));
  303. }
  304. #if !defined(__ANDROID__)
  305. quit(0);
  306. #endif
  307. return 0;
  308. }
  309. #else /* HAVE_OPENGLES */
  310. int
  311. main(int argc, char *argv[])
  312. {
  313. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL ES support on this system\n");
  314. return 1;
  315. }
  316. #endif /* HAVE_OPENGLES */
  317. /* vi: set ts=4 sw=4 expandtab: */