🛠️🐜 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.

437 lines
13 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 main(int argc, char *argv[])
  179. {
  180. int fsaa, accel;
  181. int value;
  182. int i, done;
  183. SDL_DisplayMode mode;
  184. SDL_Event event;
  185. Uint32 then, now, frames;
  186. int status;
  187. int dw, dh;
  188. int swap_interval = 0;
  189. /* Enable standard application logging */
  190. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  191. /* Initialize parameters */
  192. fsaa = 0;
  193. accel = -1;
  194. /* Initialize test framework */
  195. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  196. if (state == NULL) {
  197. return 1;
  198. }
  199. for (i = 1; i < argc;) {
  200. int consumed;
  201. consumed = SDLTest_CommonArg(state, i);
  202. if (consumed == 0) {
  203. if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i + 1 < argc) {
  204. fsaa = SDL_atoi(argv[i + 1]);
  205. consumed = 2;
  206. } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i + 1 < argc) {
  207. accel = SDL_atoi(argv[i + 1]);
  208. consumed = 2;
  209. } else {
  210. consumed = -1;
  211. }
  212. }
  213. if (consumed < 0) {
  214. static const char *options[] = { "[--fsaa n]", "[--accel n]", NULL };
  215. SDLTest_CommonLogUsage(state, argv[0], options);
  216. quit(1);
  217. }
  218. i += consumed;
  219. }
  220. /* Set OpenGL parameters */
  221. state->window_flags |= SDL_WINDOW_OPENGL;
  222. state->gl_red_size = 5;
  223. state->gl_green_size = 5;
  224. state->gl_blue_size = 5;
  225. state->gl_depth_size = 16;
  226. state->gl_double_buffer = 1;
  227. if (fsaa) {
  228. state->gl_multisamplebuffers = 1;
  229. state->gl_multisamplesamples = fsaa;
  230. }
  231. if (accel >= 0) {
  232. state->gl_accelerated = accel;
  233. }
  234. if (!SDLTest_CommonInit(state)) {
  235. quit(2);
  236. }
  237. /* Create OpenGL context */
  238. context = SDL_GL_CreateContext(state->windows[0]);
  239. if (!context) {
  240. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
  241. quit(2);
  242. }
  243. /* Important: call this *after* creating the context */
  244. if (LoadContext(&ctx) < 0) {
  245. SDL_Log("Could not load GL functions\n");
  246. quit(2);
  247. return 0;
  248. }
  249. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  250. /* try late-swap-tearing first. If not supported, try normal vsync. */
  251. if (SDL_GL_SetSwapInterval(-1) == 0) {
  252. swap_interval = -1;
  253. } else {
  254. SDL_GL_SetSwapInterval(1);
  255. swap_interval = 1;
  256. }
  257. } else {
  258. SDL_GL_SetSwapInterval(0); /* disable vsync. */
  259. swap_interval = 0;
  260. }
  261. SDL_GetCurrentDisplayMode(0, &mode);
  262. SDL_Log("Screen BPP : %" SDL_PRIu32 "\n", SDL_BITSPERPIXEL(mode.format));
  263. SDL_Log("Swap Interval : %d\n", SDL_GL_GetSwapInterval());
  264. SDL_GetWindowSize(state->windows[0], &dw, &dh);
  265. SDL_Log("Window Size : %d,%d\n", dw, dh);
  266. SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
  267. SDL_Log("Draw Size : %d,%d\n", dw, dh);
  268. SDL_Log("\n");
  269. SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
  270. SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
  271. SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
  272. SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  273. SDL_Log("\n");
  274. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  275. if (!status) {
  276. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  277. } else {
  278. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
  279. }
  280. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  281. if (!status) {
  282. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  283. } else {
  284. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
  285. }
  286. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  287. if (!status) {
  288. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  289. } else {
  290. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
  291. }
  292. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  293. if (!status) {
  294. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
  295. } else {
  296. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
  297. }
  298. if (fsaa) {
  299. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  300. if (!status) {
  301. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  302. } else {
  303. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  304. SDL_GetError());
  305. }
  306. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  307. if (!status) {
  308. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  309. value);
  310. } else {
  311. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  312. SDL_GetError());
  313. }
  314. }
  315. if (accel >= 0) {
  316. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  317. if (!status) {
  318. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
  319. value);
  320. } else {
  321. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  322. SDL_GetError());
  323. }
  324. }
  325. /* Set rendering settings */
  326. ctx.glMatrixMode(GL_PROJECTION);
  327. ctx.glLoadIdentity();
  328. ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
  329. ctx.glMatrixMode(GL_MODELVIEW);
  330. ctx.glLoadIdentity();
  331. ctx.glEnable(GL_DEPTH_TEST);
  332. ctx.glDepthFunc(GL_LESS);
  333. ctx.glShadeModel(GL_SMOOTH);
  334. /* Main render loop */
  335. frames = 0;
  336. then = SDL_GetTicks();
  337. done = 0;
  338. while (!done) {
  339. SDL_bool update_swap_interval = SDL_FALSE;
  340. /* Check for events */
  341. ++frames;
  342. while (SDL_PollEvent(&event)) {
  343. SDLTest_CommonEvent(state, &event, &done);
  344. if (event.type == SDL_KEYDOWN) {
  345. if (event.key.keysym.sym == SDLK_o) {
  346. swap_interval--;
  347. update_swap_interval = SDL_TRUE;
  348. } else if (event.key.keysym.sym == SDLK_p) {
  349. swap_interval++;
  350. update_swap_interval = SDL_TRUE;
  351. }
  352. }
  353. }
  354. if (update_swap_interval) {
  355. SDL_Log("Swap interval to be set to %d\n", swap_interval);
  356. }
  357. for (i = 0; i < state->num_windows; ++i) {
  358. int w, h;
  359. if (state->windows[i] == NULL) {
  360. continue;
  361. }
  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 main(int argc, char *argv[])
  383. {
  384. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n");
  385. return 1;
  386. }
  387. #endif /* HAVE_OPENGL */