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

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