testnative.c 6.8 KB

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