testdraw2.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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: draw as many random objects on the screen as possible */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "SDL_test_common.h"
  15. #define NUM_OBJECTS 100
  16. static SDLTest_CommonState *state;
  17. static int num_objects;
  18. static SDL_bool cycle_color;
  19. static SDL_bool cycle_alpha;
  20. static int cycle_direction = 1;
  21. static int current_alpha = 255;
  22. static int current_color = 255;
  23. static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  24. void
  25. DrawPoints(SDL_Renderer * renderer)
  26. {
  27. int i;
  28. int x, y;
  29. SDL_Rect viewport;
  30. /* Query the sizes */
  31. SDL_RenderGetViewport(renderer, &viewport);
  32. for (i = 0; i < num_objects * 4; ++i) {
  33. /* Cycle the color and alpha, if desired */
  34. if (cycle_color) {
  35. current_color += cycle_direction;
  36. if (current_color < 0) {
  37. current_color = 0;
  38. cycle_direction = -cycle_direction;
  39. }
  40. if (current_color > 255) {
  41. current_color = 255;
  42. cycle_direction = -cycle_direction;
  43. }
  44. }
  45. if (cycle_alpha) {
  46. current_alpha += cycle_direction;
  47. if (current_alpha < 0) {
  48. current_alpha = 0;
  49. cycle_direction = -cycle_direction;
  50. }
  51. if (current_alpha > 255) {
  52. current_alpha = 255;
  53. cycle_direction = -cycle_direction;
  54. }
  55. }
  56. SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
  57. (Uint8) current_color, (Uint8) current_alpha);
  58. x = rand() % viewport.w;
  59. y = rand() % viewport.h;
  60. SDL_RenderDrawPoint(renderer, x, y);
  61. }
  62. }
  63. void
  64. DrawLines(SDL_Renderer * renderer)
  65. {
  66. int i;
  67. int x1, y1, x2, y2;
  68. SDL_Rect viewport;
  69. /* Query the sizes */
  70. SDL_RenderGetViewport(renderer, &viewport);
  71. for (i = 0; i < num_objects; ++i) {
  72. /* Cycle the color and alpha, if desired */
  73. if (cycle_color) {
  74. current_color += cycle_direction;
  75. if (current_color < 0) {
  76. current_color = 0;
  77. cycle_direction = -cycle_direction;
  78. }
  79. if (current_color > 255) {
  80. current_color = 255;
  81. cycle_direction = -cycle_direction;
  82. }
  83. }
  84. if (cycle_alpha) {
  85. current_alpha += cycle_direction;
  86. if (current_alpha < 0) {
  87. current_alpha = 0;
  88. cycle_direction = -cycle_direction;
  89. }
  90. if (current_alpha > 255) {
  91. current_alpha = 255;
  92. cycle_direction = -cycle_direction;
  93. }
  94. }
  95. SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
  96. (Uint8) current_color, (Uint8) current_alpha);
  97. if (i == 0) {
  98. SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
  99. SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
  100. SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
  101. SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
  102. } else {
  103. x1 = (rand() % (viewport.w*2)) - viewport.w;
  104. x2 = (rand() % (viewport.w*2)) - viewport.w;
  105. y1 = (rand() % (viewport.h*2)) - viewport.h;
  106. y2 = (rand() % (viewport.h*2)) - viewport.h;
  107. SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  108. }
  109. }
  110. }
  111. void
  112. DrawRects(SDL_Renderer * renderer)
  113. {
  114. int i;
  115. SDL_Rect rect;
  116. SDL_Rect viewport;
  117. /* Query the sizes */
  118. SDL_RenderGetViewport(renderer, &viewport);
  119. for (i = 0; i < num_objects / 4; ++i) {
  120. /* Cycle the color and alpha, if desired */
  121. if (cycle_color) {
  122. current_color += cycle_direction;
  123. if (current_color < 0) {
  124. current_color = 0;
  125. cycle_direction = -cycle_direction;
  126. }
  127. if (current_color > 255) {
  128. current_color = 255;
  129. cycle_direction = -cycle_direction;
  130. }
  131. }
  132. if (cycle_alpha) {
  133. current_alpha += cycle_direction;
  134. if (current_alpha < 0) {
  135. current_alpha = 0;
  136. cycle_direction = -cycle_direction;
  137. }
  138. if (current_alpha > 255) {
  139. current_alpha = 255;
  140. cycle_direction = -cycle_direction;
  141. }
  142. }
  143. SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
  144. (Uint8) current_color, (Uint8) current_alpha);
  145. rect.w = rand() % (viewport.h / 2);
  146. rect.h = rand() % (viewport.h / 2);
  147. rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
  148. rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
  149. SDL_RenderFillRect(renderer, &rect);
  150. }
  151. }
  152. int
  153. main(int argc, char *argv[])
  154. {
  155. int i, done;
  156. SDL_Event event;
  157. Uint32 then, now, frames;
  158. /* Enable standard application logging */
  159. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  160. /* Initialize parameters */
  161. num_objects = NUM_OBJECTS;
  162. /* Initialize test framework */
  163. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  164. if (!state) {
  165. return 1;
  166. }
  167. for (i = 1; i < argc;) {
  168. int consumed;
  169. consumed = SDLTest_CommonArg(state, i);
  170. if (consumed == 0) {
  171. consumed = -1;
  172. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  173. if (argv[i + 1]) {
  174. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  175. blendMode = SDL_BLENDMODE_NONE;
  176. consumed = 2;
  177. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  178. blendMode = SDL_BLENDMODE_BLEND;
  179. consumed = 2;
  180. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  181. blendMode = SDL_BLENDMODE_ADD;
  182. consumed = 2;
  183. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  184. blendMode = SDL_BLENDMODE_MOD;
  185. consumed = 2;
  186. }
  187. }
  188. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  189. cycle_color = SDL_TRUE;
  190. consumed = 1;
  191. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  192. cycle_alpha = SDL_TRUE;
  193. consumed = 1;
  194. } else if (SDL_isdigit(*argv[i])) {
  195. num_objects = SDL_atoi(argv[i]);
  196. consumed = 1;
  197. }
  198. }
  199. if (consumed < 0) {
  200. SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
  201. argv[0], SDLTest_CommonUsage(state));
  202. return 1;
  203. }
  204. i += consumed;
  205. }
  206. if (!SDLTest_CommonInit(state)) {
  207. return 2;
  208. }
  209. /* Create the windows and initialize the renderers */
  210. for (i = 0; i < state->num_windows; ++i) {
  211. SDL_Renderer *renderer = state->renderers[i];
  212. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  213. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  214. SDL_RenderClear(renderer);
  215. }
  216. srand((unsigned int)time(NULL));
  217. /* Main render loop */
  218. frames = 0;
  219. then = SDL_GetTicks();
  220. done = 0;
  221. while (!done) {
  222. /* Check for events */
  223. ++frames;
  224. while (SDL_PollEvent(&event)) {
  225. SDLTest_CommonEvent(state, &event, &done);
  226. }
  227. for (i = 0; i < state->num_windows; ++i) {
  228. SDL_Renderer *renderer = state->renderers[i];
  229. if (state->windows[i] == NULL)
  230. continue;
  231. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  232. SDL_RenderClear(renderer);
  233. DrawRects(renderer);
  234. DrawLines(renderer);
  235. DrawPoints(renderer);
  236. SDL_RenderPresent(renderer);
  237. }
  238. }
  239. SDLTest_CommonQuit(state);
  240. /* Print out some timing information */
  241. now = SDL_GetTicks();
  242. if (now > then) {
  243. double fps = ((double) frames * 1000) / (now - then);
  244. SDL_Log("%2.2f frames per second\n", fps);
  245. }
  246. return 0;
  247. }
  248. /* vi: set ts=4 sw=4 expandtab: */