SDL_test_common.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * Common functions of SDL test framework.
  20. *
  21. * This code is a part of the SDL test library, not the main SDL library.
  22. */
  23. /* Ported from original test/common.h file. */
  24. #ifndef SDL_test_common_h_
  25. #define SDL_test_common_h_
  26. #include <SDL3/SDL.h>
  27. #ifdef SDL_PLATFORM_PSP
  28. #define DEFAULT_WINDOW_WIDTH 480
  29. #define DEFAULT_WINDOW_HEIGHT 272
  30. #elif defined(SDL_PLATFORM_VITA)
  31. #define DEFAULT_WINDOW_WIDTH 960
  32. #define DEFAULT_WINDOW_HEIGHT 544
  33. #else
  34. #define DEFAULT_WINDOW_WIDTH 640
  35. #define DEFAULT_WINDOW_HEIGHT 480
  36. #endif
  37. typedef Uint32 SDLTest_VerboseFlags;
  38. #define VERBOSE_VIDEO 0x00000001
  39. #define VERBOSE_MODES 0x00000002
  40. #define VERBOSE_RENDER 0x00000004
  41. #define VERBOSE_EVENT 0x00000008
  42. #define VERBOSE_AUDIO 0x00000010
  43. #define VERBOSE_MOTION 0x00000020
  44. /* !< Function pointer parsing one argument at argv[index], returning the number of parsed arguments,
  45. * or a negative value when the argument is invalid */
  46. typedef int (SDLCALL *SDLTest_ParseArgumentsFp)(void *data, char **argv, int index);
  47. /* !< Finalize the argument parser. */
  48. typedef void (SDLCALL *SDLTest_FinalizeArgumentParserFp)(void *arg);
  49. typedef struct SDLTest_ArgumentParser
  50. {
  51. /* !< Parse an argument. */
  52. SDLTest_ParseArgumentsFp parse_arguments;
  53. /* !< Finalize this argument parser. Called once before parsing the first argument. */
  54. SDLTest_FinalizeArgumentParserFp finalize;
  55. /* !< Null-terminated array of arguments. Printed when running with --help. */
  56. const char **usage;
  57. /* !< User data, passed to all callbacks. */
  58. void *data;
  59. /* !< Next argument parser. */
  60. struct SDLTest_ArgumentParser *next;
  61. } SDLTest_ArgumentParser;
  62. typedef struct
  63. {
  64. /* SDL init flags */
  65. char **argv;
  66. SDL_InitFlags flags;
  67. SDLTest_VerboseFlags verbose;
  68. /* Video info */
  69. const char *videodriver;
  70. int display_index;
  71. SDL_DisplayID displayID;
  72. const char *window_title;
  73. const char *window_icon;
  74. SDL_WindowFlags window_flags;
  75. bool flash_on_focus_loss;
  76. int window_x;
  77. int window_y;
  78. int window_w;
  79. int window_h;
  80. int window_minW;
  81. int window_minH;
  82. int window_maxW;
  83. int window_maxH;
  84. float window_min_aspect;
  85. float window_max_aspect;
  86. int logical_w;
  87. int logical_h;
  88. bool auto_scale_content;
  89. SDL_RendererLogicalPresentation logical_presentation;
  90. float scale;
  91. int depth;
  92. float refresh_rate;
  93. bool fill_usable_bounds;
  94. bool fullscreen_exclusive;
  95. SDL_DisplayMode fullscreen_mode;
  96. int num_windows;
  97. SDL_Window **windows;
  98. const char *gpudriver;
  99. /* Renderer info */
  100. const char *renderdriver;
  101. int render_vsync;
  102. bool skip_renderer;
  103. SDL_Renderer **renderers;
  104. SDL_Texture **targets;
  105. /* Audio info */
  106. const char *audiodriver;
  107. SDL_AudioFormat audio_format;
  108. int audio_channels;
  109. int audio_freq;
  110. SDL_AudioDeviceID audio_id;
  111. /* GL settings */
  112. int gl_red_size;
  113. int gl_green_size;
  114. int gl_blue_size;
  115. int gl_alpha_size;
  116. int gl_buffer_size;
  117. int gl_depth_size;
  118. int gl_stencil_size;
  119. int gl_double_buffer;
  120. int gl_accum_red_size;
  121. int gl_accum_green_size;
  122. int gl_accum_blue_size;
  123. int gl_accum_alpha_size;
  124. int gl_stereo;
  125. int gl_release_behavior;
  126. int gl_multisamplebuffers;
  127. int gl_multisamplesamples;
  128. int gl_retained_backing;
  129. int gl_accelerated;
  130. int gl_major_version;
  131. int gl_minor_version;
  132. int gl_debug;
  133. int gl_profile_mask;
  134. /* Mouse info */
  135. SDL_Rect confine;
  136. bool hide_cursor;
  137. /* Options info */
  138. SDLTest_ArgumentParser common_argparser;
  139. SDLTest_ArgumentParser video_argparser;
  140. SDLTest_ArgumentParser audio_argparser;
  141. SDLTest_ArgumentParser *argparser;
  142. } SDLTest_CommonState;
  143. #include <SDL3/SDL_begin_code.h>
  144. /* Set up for C function definitions, even when using C++ */
  145. #ifdef __cplusplus
  146. extern "C" {
  147. #endif
  148. /* Function prototypes */
  149. /**
  150. * Parse command line parameters and create common state.
  151. *
  152. * \param argv Array of command line parameters
  153. * \param flags Flags indicating which subsystem to initialize (i.e. SDL_INIT_VIDEO | SDL_INIT_AUDIO)
  154. *
  155. * \returns a newly allocated common state object.
  156. */
  157. SDLTest_CommonState *SDLCALL SDLTest_CommonCreateState(char **argv, SDL_InitFlags flags);
  158. /**
  159. * Free the common state object.
  160. *
  161. * You should call SDL_Quit() before calling this function.
  162. *
  163. * \param state The common state object to destroy
  164. */
  165. void SDLCALL SDLTest_CommonDestroyState(SDLTest_CommonState *state);
  166. /**
  167. * Process one common argument.
  168. *
  169. * \param state The common state describing the test window to create.
  170. * \param index The index of the argument to process in argv[].
  171. *
  172. * \returns the number of arguments processed (i.e. 1 for --fullscreen, 2 for --video [videodriver], or -1 on error.
  173. */
  174. int SDLCALL SDLTest_CommonArg(SDLTest_CommonState *state, int index);
  175. /**
  176. * Logs command line usage info.
  177. *
  178. * This logs the appropriate command line options for the subsystems in use
  179. * plus other common options, and then any application-specific options.
  180. * This uses the SDL_Log() function and splits up output to be friendly to
  181. * 80-character-wide terminals.
  182. *
  183. * \param state The common state describing the test window for the app.
  184. * \param argv0 argv[0], as passed to main/SDL_main.
  185. * \param options an array of strings for application specific options. The last element of the array should be NULL.
  186. */
  187. void SDLCALL SDLTest_CommonLogUsage(SDLTest_CommonState *state, const char *argv0, const char **options);
  188. /**
  189. * Open test window.
  190. *
  191. * \param state The common state describing the test window to create.
  192. *
  193. * \returns true if initialization succeeded, false otherwise
  194. */
  195. bool SDLCALL SDLTest_CommonInit(SDLTest_CommonState *state);
  196. /**
  197. * Easy argument handling when test app doesn't need any custom args.
  198. *
  199. * \param state The common state describing the test window to create.
  200. * \param argc argc, as supplied to SDL_main
  201. * \param argv argv, as supplied to SDL_main
  202. *
  203. * \returns false if app should quit, true otherwise.
  204. */
  205. bool SDLCALL SDLTest_CommonDefaultArgs(SDLTest_CommonState *state, int argc, char **argv);
  206. /**
  207. * Print the details of an event.
  208. *
  209. * This is automatically called by SDLTest_CommonEvent() as needed.
  210. *
  211. * \param event The event to print.
  212. */
  213. void SDLCALL SDLTest_PrintEvent(const SDL_Event *event);
  214. /**
  215. * Common event handler for test windows if you use a standard SDL_main.
  216. *
  217. * \param state The common state used to create test window.
  218. * \param event The event to handle.
  219. * \param done Flag indicating we are done.
  220. */
  221. void SDLCALL SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done);
  222. /**
  223. * Common event handler for test windows if you use SDL_AppEvent.
  224. *
  225. * This does _not_ free anything in `event`.
  226. *
  227. * \param state The common state used to create test window.
  228. * \param event The event to handle.
  229. * \returns Value suitable for returning from SDL_AppEvent().
  230. */
  231. SDL_AppResult SDLCALL SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event *event);
  232. /**
  233. * Close test window.
  234. *
  235. * \param state The common state used to create test window.
  236. *
  237. */
  238. void SDLCALL SDLTest_CommonQuit(SDLTest_CommonState *state);
  239. /**
  240. * Draws various window information (position, size, etc.) to the renderer.
  241. *
  242. * \param renderer The renderer to draw to.
  243. * \param window The window whose information should be displayed.
  244. * \param usedHeight Returns the height used, so the caller can draw more below.
  245. *
  246. */
  247. void SDLCALL SDLTest_CommonDrawWindowInfo(SDL_Renderer *renderer, SDL_Window *window, float *usedHeight);
  248. /* Ends C function definitions when using C++ */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #include <SDL3/SDL_close_code.h>
  253. #endif /* SDL_test_common_h_ */