🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
12 KiB

  1. /*
  2. Copyright (C) 1997-2019 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. #ifdef __MACOS__
  16. #define HAVE_OPENGL
  17. #endif
  18. #ifdef HAVE_OPENGL
  19. #include "SDL_opengl.h"
  20. typedef struct GL_Context
  21. {
  22. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  23. #include "../src/render/opengl/SDL_glfuncs.h"
  24. #undef SDL_PROC
  25. } GL_Context;
  26. /* Undefine this if you want a flat cube instead of a rainbow cube */
  27. #define SHADED_CUBE
  28. static SDLTest_CommonState *state;
  29. static SDL_GLContext context;
  30. static GL_Context ctx;
  31. static int LoadContext(GL_Context * data)
  32. {
  33. #if SDL_VIDEO_DRIVER_UIKIT
  34. #define __SDL_NOGETPROCADDR__
  35. #elif SDL_VIDEO_DRIVER_ANDROID
  36. #define __SDL_NOGETPROCADDR__
  37. #elif SDL_VIDEO_DRIVER_PANDORA
  38. #define __SDL_NOGETPROCADDR__
  39. #endif
  40. #if defined __SDL_NOGETPROCADDR__
  41. #define SDL_PROC(ret,func,params) data->func=func;
  42. #else
  43. #define SDL_PROC(ret,func,params) \
  44. do { \
  45. data->func = SDL_GL_GetProcAddress(#func); \
  46. if ( ! data->func ) { \
  47. return SDL_SetError("Couldn't load GL function %s: %s", #func, SDL_GetError()); \
  48. } \
  49. } while ( 0 );
  50. #endif /* __SDL_NOGETPROCADDR__ */
  51. #include "../src/render/opengl/SDL_glfuncs.h"
  52. #undef SDL_PROC
  53. return 0;
  54. }
  55. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  56. static void
  57. quit(int rc)
  58. {
  59. if (context) {
  60. /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
  61. SDL_GL_DeleteContext(context);
  62. }
  63. SDLTest_CommonQuit(state);
  64. exit(rc);
  65. }
  66. static void
  67. Render()
  68. {
  69. static float color[8][3] = {
  70. {1.0, 1.0, 0.0},
  71. {1.0, 0.0, 0.0},
  72. {0.0, 0.0, 0.0},
  73. {0.0, 1.0, 0.0},
  74. {0.0, 1.0, 1.0},
  75. {1.0, 1.0, 1.0},
  76. {1.0, 0.0, 1.0},
  77. {0.0, 0.0, 1.0}
  78. };
  79. static float cube[8][3] = {
  80. {0.5, 0.5, -0.5},
  81. {0.5, -0.5, -0.5},
  82. {-0.5, -0.5, -0.5},
  83. {-0.5, 0.5, -0.5},
  84. {-0.5, 0.5, 0.5},
  85. {0.5, 0.5, 0.5},
  86. {0.5, -0.5, 0.5},
  87. {-0.5, -0.5, 0.5}
  88. };
  89. /* Do our drawing, too. */
  90. ctx.glClearColor(0.0, 0.0, 0.0, 1.0);
  91. ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. ctx.glBegin(GL_QUADS);
  93. #ifdef SHADED_CUBE
  94. ctx.glColor3fv(color[0]);
  95. ctx.glVertex3fv(cube[0]);
  96. ctx.glColor3fv(color[1]);
  97. ctx.glVertex3fv(cube[1]);
  98. ctx.glColor3fv(color[2]);
  99. ctx.glVertex3fv(cube[2]);
  100. ctx.glColor3fv(color[3]);
  101. ctx.glVertex3fv(cube[3]);
  102. ctx.glColor3fv(color[3]);
  103. ctx.glVertex3fv(cube[3]);
  104. ctx.glColor3fv(color[4]);
  105. ctx.glVertex3fv(cube[4]);
  106. ctx.glColor3fv(color[7]);
  107. ctx.glVertex3fv(cube[7]);
  108. ctx.glColor3fv(color[2]);
  109. ctx.glVertex3fv(cube[2]);
  110. ctx.glColor3fv(color[0]);
  111. ctx.glVertex3fv(cube[0]);
  112. ctx.glColor3fv(color[5]);
  113. ctx.glVertex3fv(cube[5]);
  114. ctx.glColor3fv(color[6]);
  115. ctx.glVertex3fv(cube[6]);
  116. ctx.glColor3fv(color[1]);
  117. ctx.glVertex3fv(cube[1]);
  118. ctx.glColor3fv(color[5]);
  119. ctx.glVertex3fv(cube[5]);
  120. ctx.glColor3fv(color[4]);
  121. ctx.glVertex3fv(cube[4]);
  122. ctx.glColor3fv(color[7]);
  123. ctx.glVertex3fv(cube[7]);
  124. ctx.glColor3fv(color[6]);
  125. ctx.glVertex3fv(cube[6]);
  126. ctx.glColor3fv(color[5]);
  127. ctx.glVertex3fv(cube[5]);
  128. ctx.glColor3fv(color[0]);
  129. ctx.glVertex3fv(cube[0]);
  130. ctx.glColor3fv(color[3]);
  131. ctx.glVertex3fv(cube[3]);
  132. ctx.glColor3fv(color[4]);
  133. ctx.glVertex3fv(cube[4]);
  134. ctx.glColor3fv(color[6]);
  135. ctx.glVertex3fv(cube[6]);
  136. ctx.glColor3fv(color[1]);
  137. ctx.glVertex3fv(cube[1]);
  138. ctx.glColor3fv(color[2]);
  139. ctx.glVertex3fv(cube[2]);
  140. ctx.glColor3fv(color[7]);
  141. ctx.glVertex3fv(cube[7]);
  142. #else /* flat cube */
  143. ctx.glColor3f(1.0, 0.0, 0.0);
  144. ctx.glVertex3fv(cube[0]);
  145. ctx.glVertex3fv(cube[1]);
  146. ctx.glVertex3fv(cube[2]);
  147. ctx.glVertex3fv(cube[3]);
  148. ctx.glColor3f(0.0, 1.0, 0.0);
  149. ctx.glVertex3fv(cube[3]);
  150. ctx.glVertex3fv(cube[4]);
  151. ctx.glVertex3fv(cube[7]);
  152. ctx.glVertex3fv(cube[2]);
  153. ctx.glColor3f(0.0, 0.0, 1.0);
  154. ctx.glVertex3fv(cube[0]);
  155. ctx.glVertex3fv(cube[5]);
  156. ctx.glVertex3fv(cube[6]);
  157. ctx.glVertex3fv(cube[1]);
  158. ctx.glColor3f(0.0, 1.0, 1.0);
  159. ctx.glVertex3fv(cube[5]);
  160. ctx.glVertex3fv(cube[4]);
  161. ctx.glVertex3fv(cube[7]);
  162. ctx.glVertex3fv(cube[6]);
  163. ctx.glColor3f(1.0, 1.0, 0.0);
  164. ctx.glVertex3fv(cube[5]);
  165. ctx.glVertex3fv(cube[0]);
  166. ctx.glVertex3fv(cube[3]);
  167. ctx.glVertex3fv(cube[4]);
  168. ctx.glColor3f(1.0, 0.0, 1.0);
  169. ctx.glVertex3fv(cube[6]);
  170. ctx.glVertex3fv(cube[1]);
  171. ctx.glVertex3fv(cube[2]);
  172. ctx.glVertex3fv(cube[7]);
  173. #endif /* SHADED_CUBE */
  174. ctx.glEnd();
  175. ctx.glMatrixMode(GL_MODELVIEW);
  176. ctx.glRotatef(5.0, 1.0, 1.0, 1.0);
  177. }
  178. int
  179. main(int argc, char *argv[])
  180. {
  181. int fsaa, accel;
  182. int value;
  183. int i, done;
  184. SDL_DisplayMode mode;
  185. SDL_Event event;
  186. Uint32 then, now, frames;
  187. int status;
  188. int dw, dh;
  189. int swap_interval = 0;
  190. /* Enable standard application logging */
  191. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  192. /* Initialize parameters */
  193. fsaa = 0;
  194. accel = -1;
  195. /* Initialize test framework */
  196. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  197. if (!state) {
  198. return 1;
  199. }
  200. for (i = 1; i < argc;) {
  201. int consumed;
  202. consumed = SDLTest_CommonArg(state, i);
  203. if (consumed == 0) {
  204. if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i+1 < argc) {
  205. fsaa = atoi(argv[i+1]);
  206. consumed = 2;
  207. } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i+1 < argc) {
  208. accel = atoi(argv[i+1]);
  209. consumed = 2;
  210. } else {
  211. consumed = -1;
  212. }
  213. }
  214. if (consumed < 0) {
  215. static const char *options[] = { "[--fsaa n]", "[--accel n]", NULL };
  216. SDLTest_CommonLogUsage(state, argv[0], options);
  217. quit(1);
  218. }
  219. i += consumed;
  220. }
  221. /* Set OpenGL parameters */
  222. state->window_flags |= SDL_WINDOW_OPENGL;
  223. state->gl_red_size = 5;
  224. state->gl_green_size = 5;
  225. state->gl_blue_size = 5;
  226. state->gl_depth_size = 16;
  227. state->gl_double_buffer = 1;
  228. if (fsaa) {
  229. state->gl_multisamplebuffers = 1;
  230. state->gl_multisamplesamples = fsaa;
  231. }
  232. if (accel >= 0) {
  233. state->gl_accelerated = accel;
  234. }
  235. if (!SDLTest_CommonInit(state)) {
  236. quit(2);
  237. }
  238. /* Create OpenGL context */
  239. context = SDL_GL_CreateContext(state->windows[0]);
  240. if (!context) {
  241. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
  242. quit(2);
  243. }
  244. /* Important: call this *after* creating the context */
  245. if (LoadContext(&ctx) < 0) {
  246. SDL_Log("Could not load GL functions\n");
  247. quit(2);
  248. return 0;
  249. }
  250. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  251. /* try late-swap-tearing first. If not supported, try normal vsync. */
  252. if (SDL_GL_SetSwapInterval(-1) == 0) {
  253. swap_interval = -1;
  254. } else {
  255. SDL_GL_SetSwapInterval(1);
  256. swap_interval = 1;
  257. }
  258. } else {
  259. SDL_GL_SetSwapInterval(0); /* disable vsync. */
  260. swap_interval = 0;
  261. }
  262. SDL_GetCurrentDisplayMode(0, &mode);
  263. SDL_Log("Screen BPP : %d\n", SDL_BITSPERPIXEL(mode.format));
  264. SDL_Log("Swap Interval : %d\n", SDL_GL_GetSwapInterval());
  265. SDL_GetWindowSize(state->windows[0], &dw, &dh);
  266. SDL_Log("Window Size : %d,%d\n", dw, dh);
  267. SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
  268. SDL_Log("Draw Size : %d,%d\n", dw, dh);
  269. SDL_Log("\n");
  270. SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
  271. SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
  272. SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
  273. SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  274. SDL_Log("\n");
  275. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  276. if (!status) {
  277. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  278. } else {
  279. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
  280. }
  281. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  282. if (!status) {
  283. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  284. } else {
  285. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
  286. }
  287. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  288. if (!status) {
  289. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  290. } else {
  291. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
  292. }
  293. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  294. if (!status) {
  295. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
  296. } else {
  297. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
  298. }
  299. if (fsaa) {
  300. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  301. if (!status) {
  302. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  303. } else {
  304. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  305. SDL_GetError());
  306. }
  307. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  308. if (!status) {
  309. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  310. value);
  311. } else {
  312. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  313. SDL_GetError());
  314. }
  315. }
  316. if (accel >= 0) {
  317. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  318. if (!status) {
  319. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
  320. value);
  321. } else {
  322. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  323. SDL_GetError());
  324. }
  325. }
  326. /* Set rendering settings */
  327. ctx.glMatrixMode(GL_PROJECTION);
  328. ctx.glLoadIdentity();
  329. ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
  330. ctx.glMatrixMode(GL_MODELVIEW);
  331. ctx.glLoadIdentity();
  332. ctx.glEnable(GL_DEPTH_TEST);
  333. ctx.glDepthFunc(GL_LESS);
  334. ctx.glShadeModel(GL_SMOOTH);
  335. /* Main render loop */
  336. frames = 0;
  337. then = SDL_GetTicks();
  338. done = 0;
  339. while (!done) {
  340. SDL_bool update_swap_interval = SDL_FALSE;
  341. /* Check for events */
  342. ++frames;
  343. while (SDL_PollEvent(&event)) {
  344. SDLTest_CommonEvent(state, &event, &done);
  345. if (event.type == SDL_KEYDOWN) {
  346. if (event.key.keysym.sym == SDLK_o) {
  347. swap_interval--;
  348. update_swap_interval = SDL_TRUE;
  349. } else if (event.key.keysym.sym == SDLK_p) {
  350. swap_interval++;
  351. update_swap_interval = SDL_TRUE;
  352. }
  353. }
  354. }
  355. if (update_swap_interval) {
  356. SDL_Log("Swap interval to be set to %d\n", swap_interval);
  357. }
  358. for (i = 0; i < state->num_windows; ++i) {
  359. int w, h;
  360. if (state->windows[i] == NULL)
  361. continue;
  362. SDL_GL_MakeCurrent(state->windows[i], context);
  363. if (update_swap_interval) {
  364. SDL_GL_SetSwapInterval(swap_interval);
  365. }
  366. SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  367. ctx.glViewport(0, 0, w, h);
  368. Render();
  369. SDL_GL_SwapWindow(state->windows[i]);
  370. }
  371. }
  372. /* Print out some timing information */
  373. now = SDL_GetTicks();
  374. if (now > then) {
  375. SDL_Log("%2.2f frames per second\n",
  376. ((double) frames * 1000) / (now - then));
  377. }
  378. quit(0);
  379. return 0;
  380. }
  381. #else /* HAVE_OPENGL */
  382. int
  383. main(int argc, char *argv[])
  384. {
  385. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n");
  386. return 1;
  387. }
  388. #endif /* HAVE_OPENGL */