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

210 lines
6.1 KiB

  1. /*
  2. Copyright (C) 1997-2022 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 <stdio.h>
  12. #include <stdlib.h> /* for srand() */
  13. #include <time.h> /* for time() */
  14. #include "testnative.h"
  15. #include "testutils.h"
  16. #define WINDOW_W 640
  17. #define WINDOW_H 480
  18. #define NUM_SPRITES 100
  19. #define MAX_SPEED 1
  20. static NativeWindowFactory *factories[] = {
  21. #ifdef TEST_NATIVE_WINDOWS
  22. &WindowsWindowFactory,
  23. #endif
  24. #ifdef TEST_NATIVE_X11
  25. &X11WindowFactory,
  26. #endif
  27. #ifdef TEST_NATIVE_COCOA
  28. &CocoaWindowFactory,
  29. #endif
  30. #ifdef TEST_NATIVE_OS2
  31. &OS2WindowFactory,
  32. #endif
  33. NULL
  34. };
  35. static NativeWindowFactory *factory = NULL;
  36. static void *native_window;
  37. static SDL_Rect *positions, *velocities;
  38. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  39. static void
  40. quit(int rc)
  41. {
  42. SDL_VideoQuit();
  43. if (native_window) {
  44. factory->DestroyNativeWindow(native_window);
  45. }
  46. exit(rc);
  47. }
  48. void
  49. MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
  50. {
  51. int sprite_w, sprite_h;
  52. int i;
  53. SDL_Rect viewport;
  54. SDL_Rect *position, *velocity;
  55. /* Query the sizes */
  56. SDL_RenderGetViewport(renderer, &viewport);
  57. SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
  58. /* Draw a gray background */
  59. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  60. SDL_RenderClear(renderer);
  61. /* Move the sprite, bounce at the wall, and draw */
  62. for (i = 0; i < NUM_SPRITES; ++i) {
  63. position = &positions[i];
  64. velocity = &velocities[i];
  65. position->x += velocity->x;
  66. if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
  67. velocity->x = -velocity->x;
  68. position->x += velocity->x;
  69. }
  70. position->y += velocity->y;
  71. if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
  72. velocity->y = -velocity->y;
  73. position->y += velocity->y;
  74. }
  75. /* Blit the sprite onto the screen */
  76. SDL_RenderCopy(renderer, sprite, NULL, position);
  77. }
  78. /* Update the screen! */
  79. SDL_RenderPresent(renderer);
  80. }
  81. int
  82. main(int argc, char *argv[])
  83. {
  84. int i, done;
  85. const char *driver;
  86. SDL_Window *window;
  87. SDL_Renderer *renderer;
  88. SDL_Texture *sprite;
  89. int window_w, window_h;
  90. int sprite_w, sprite_h;
  91. SDL_Event event;
  92. /* Enable standard application logging */
  93. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  94. if (SDL_VideoInit(NULL) < 0) {
  95. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
  96. SDL_GetError());
  97. exit(1);
  98. }
  99. driver = SDL_GetCurrentVideoDriver();
  100. /* Find a native window driver and create a native window */
  101. for (i = 0; factories[i]; ++i) {
  102. if (SDL_strcmp(driver, factories[i]->tag) == 0) {
  103. factory = factories[i];
  104. break;
  105. }
  106. }
  107. if (!factory) {
  108. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
  109. driver);
  110. quit(2);
  111. }
  112. SDL_Log("Creating native window for %s driver\n", driver);
  113. native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
  114. if (!native_window) {
  115. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
  116. quit(3);
  117. }
  118. window = SDL_CreateWindowFrom(native_window);
  119. if (!window) {
  120. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
  121. quit(4);
  122. }
  123. SDL_SetWindowTitle(window, "SDL Native Window Test");
  124. /* Create the renderer */
  125. renderer = SDL_CreateRenderer(window, -1, 0);
  126. if (!renderer) {
  127. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  128. quit(5);
  129. }
  130. /* Clear the window, load the sprite and go! */
  131. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  132. SDL_RenderClear(renderer);
  133. sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
  134. if (!sprite) {
  135. quit(6);
  136. }
  137. /* Allocate memory for the sprite info */
  138. SDL_GetWindowSize(window, &window_w, &window_h);
  139. SDL_QueryTexture(sprite, NULL, NULL, &sprite_w, &sprite_h);
  140. positions = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
  141. velocities = (SDL_Rect *) SDL_malloc(NUM_SPRITES * sizeof(SDL_Rect));
  142. if (!positions || !velocities) {
  143. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
  144. quit(2);
  145. }
  146. srand((unsigned int)time(NULL));
  147. for (i = 0; i < NUM_SPRITES; ++i) {
  148. positions[i].x = rand() % (window_w - sprite_w);
  149. positions[i].y = rand() % (window_h - sprite_h);
  150. positions[i].w = sprite_w;
  151. positions[i].h = sprite_h;
  152. velocities[i].x = 0;
  153. velocities[i].y = 0;
  154. while (!velocities[i].x && !velocities[i].y) {
  155. velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  156. velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  157. }
  158. }
  159. /* Main render loop */
  160. done = 0;
  161. while (!done) {
  162. /* Check for events */
  163. while (SDL_PollEvent(&event)) {
  164. switch (event.type) {
  165. case SDL_WINDOWEVENT:
  166. switch (event.window.event) {
  167. case SDL_WINDOWEVENT_EXPOSED:
  168. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  169. SDL_RenderClear(renderer);
  170. break;
  171. }
  172. break;
  173. case SDL_QUIT:
  174. done = 1;
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. MoveSprites(renderer, sprite);
  181. }
  182. quit(0);
  183. return 0; /* to prevent compiler warning */
  184. }
  185. /* vi: set ts=4 sw=4 expandtab: */