testnative.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright (C) 1997-2025 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. /* Simple program: Create a native window and attach an SDL renderer */
  11. #include "testnative.h"
  12. #include <SDL3/SDL.h>
  13. #include <SDL3/SDL_main.h>
  14. #include <SDL3/SDL_test.h>
  15. #include "testutils.h"
  16. #include <stdlib.h>
  17. #define WINDOW_W 640
  18. #define WINDOW_H 480
  19. #define NUM_SPRITES 100
  20. #define MAX_SPEED 1
  21. static NativeWindowFactory *factories[] = {
  22. #ifdef TEST_NATIVE_WINDOWS
  23. &WindowsWindowFactory,
  24. #endif
  25. #ifdef TEST_NATIVE_WAYLAND
  26. &WaylandWindowFactory,
  27. #endif
  28. #ifdef TEST_NATIVE_X11
  29. &X11WindowFactory,
  30. #endif
  31. #ifdef TEST_NATIVE_COCOA
  32. &CocoaWindowFactory,
  33. #endif
  34. NULL
  35. };
  36. static NativeWindowFactory *factory = NULL;
  37. static void *native_window;
  38. static SDL_FRect *positions, *velocities;
  39. static SDLTest_CommonState *state;
  40. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  41. static void
  42. quit(int rc)
  43. {
  44. if (native_window && factory) {
  45. factory->DestroyNativeWindow(native_window);
  46. }
  47. SDL_Quit();
  48. SDLTest_CommonDestroyState(state);
  49. /* Let 'main()' return normally */
  50. if (rc != 0) {
  51. exit(rc);
  52. }
  53. }
  54. static void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
  55. {
  56. int i;
  57. SDL_Rect viewport;
  58. SDL_FRect *position, *velocity;
  59. /* Query the sizes */
  60. SDL_GetRenderViewport(renderer, &viewport);
  61. /* Draw a gray background */
  62. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  63. SDL_RenderClear(renderer);
  64. /* Move the sprite, bounce at the wall, and draw */
  65. for (i = 0; i < NUM_SPRITES; ++i) {
  66. position = &positions[i];
  67. velocity = &velocities[i];
  68. position->x += velocity->x;
  69. if ((position->x < 0) || (position->x >= (viewport.w - sprite->w))) {
  70. velocity->x = -velocity->x;
  71. position->x += velocity->x;
  72. }
  73. position->y += velocity->y;
  74. if ((position->y < 0) || (position->y >= (viewport.h - sprite->h))) {
  75. velocity->y = -velocity->y;
  76. position->y += velocity->y;
  77. }
  78. /* Blit the sprite onto the screen */
  79. SDL_RenderTexture(renderer, sprite, NULL, position);
  80. }
  81. /* Update the screen! */
  82. SDL_RenderPresent(renderer);
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. int i, done;
  87. const char *driver;
  88. SDL_PropertiesID props;
  89. SDL_Window *window;
  90. SDL_Renderer *renderer;
  91. SDL_Texture *sprite;
  92. int window_w, window_h;
  93. SDL_Event event;
  94. /* Initialize test framework */
  95. state = SDLTest_CommonCreateState(argv, 0);
  96. if (!state) {
  97. return 1;
  98. }
  99. /* Parse commandline */
  100. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  101. return 1;
  102. }
  103. if (!SDL_Init(SDL_INIT_VIDEO)) {
  104. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s",
  105. SDL_GetError());
  106. exit(1);
  107. }
  108. driver = SDL_GetCurrentVideoDriver();
  109. /* Find a native window driver and create a native window */
  110. for (i = 0; factories[i]; ++i) {
  111. if (SDL_strcmp(driver, factories[i]->tag) == 0) {
  112. factory = factories[i];
  113. break;
  114. }
  115. }
  116. if (!factory) {
  117. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver",
  118. driver);
  119. quit(2);
  120. }
  121. SDL_Log("Creating native window for %s driver", driver);
  122. native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
  123. if (!native_window) {
  124. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window");
  125. quit(3);
  126. }
  127. props = SDL_CreateProperties();
  128. SDL_SetPointerProperty(props, "sdl2-compat.external_window", native_window);
  129. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, true);
  130. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, WINDOW_W);
  131. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, WINDOW_H);
  132. window = SDL_CreateWindowWithProperties(props);
  133. SDL_DestroyProperties(props);
  134. if (!window) {
  135. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s", SDL_GetError());
  136. quit(4);
  137. }
  138. SDL_SetWindowTitle(window, "SDL Native Window Test");
  139. /* Create the renderer */
  140. renderer = SDL_CreateRenderer(window, NULL);
  141. if (!renderer) {
  142. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s", SDL_GetError());
  143. quit(5);
  144. }
  145. /* Clear the window, load the sprite and go! */
  146. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  147. SDL_RenderClear(renderer);
  148. sprite = LoadTexture(renderer, "icon.bmp", true, NULL, NULL);
  149. if (!sprite) {
  150. quit(6);
  151. }
  152. /* Allocate memory for the sprite info */
  153. SDL_GetWindowSize(window, &window_w, &window_h);
  154. positions = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*positions));
  155. velocities = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*velocities));
  156. if (!positions || !velocities) {
  157. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  158. quit(2);
  159. }
  160. for (i = 0; i < NUM_SPRITES; ++i) {
  161. positions[i].x = (float)SDL_rand(window_w - sprite->w);
  162. positions[i].y = (float)SDL_rand(window_h - sprite->h);
  163. positions[i].w = (float)sprite->w;
  164. positions[i].h = (float)sprite->h;
  165. velocities[i].x = 0.0f;
  166. velocities[i].y = 0.0f;
  167. while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
  168. velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
  169. velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
  170. }
  171. }
  172. /* Main render loop */
  173. done = 0;
  174. while (!done) {
  175. /* Check for events */
  176. while (SDL_PollEvent(&event)) {
  177. if (state->verbose & VERBOSE_EVENT) {
  178. if (((event.type != SDL_EVENT_MOUSE_MOTION) &&
  179. (event.type != SDL_EVENT_FINGER_MOTION)) ||
  180. (state->verbose & VERBOSE_MOTION)) {
  181. SDLTest_PrintEvent(&event);
  182. }
  183. }
  184. switch (event.type) {
  185. case SDL_EVENT_WINDOW_EXPOSED:
  186. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  187. SDL_RenderClear(renderer);
  188. break;
  189. case SDL_EVENT_QUIT:
  190. done = 1;
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. MoveSprites(renderer, sprite);
  197. }
  198. SDL_DestroyTexture(sprite);
  199. SDL_DestroyRenderer(renderer);
  200. SDL_DestroyWindow(window);
  201. SDL_free(positions);
  202. SDL_free(velocities);
  203. quit(0);
  204. return 0; /* to prevent compiler warning */
  205. }