testscale.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: Move N sprites around on the screen as fast as possible */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "SDL_test_common.h"
  15. #define WINDOW_WIDTH 640
  16. #define WINDOW_HEIGHT 480
  17. static SDLTest_CommonState *state;
  18. typedef struct {
  19. SDL_Window *window;
  20. SDL_Renderer *renderer;
  21. SDL_Texture *background;
  22. SDL_Texture *sprite;
  23. SDL_Rect sprite_rect;
  24. int scale_direction;
  25. } DrawState;
  26. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  27. static void
  28. quit(int rc)
  29. {
  30. SDLTest_CommonQuit(state);
  31. exit(rc);
  32. }
  33. SDL_Texture *
  34. LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
  35. {
  36. SDL_Surface *temp;
  37. SDL_Texture *texture;
  38. /* Load the sprite image */
  39. temp = SDL_LoadBMP(file);
  40. if (temp == NULL) {
  41. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  42. return NULL;
  43. }
  44. /* Set transparent pixel as the pixel at (0,0) */
  45. if (transparent) {
  46. if (temp->format->palette) {
  47. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  48. } else {
  49. switch (temp->format->BitsPerPixel) {
  50. case 15:
  51. SDL_SetColorKey(temp, SDL_TRUE,
  52. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  53. break;
  54. case 16:
  55. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  56. break;
  57. case 24:
  58. SDL_SetColorKey(temp, SDL_TRUE,
  59. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  60. break;
  61. case 32:
  62. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  63. break;
  64. }
  65. }
  66. }
  67. /* Create textures from the image */
  68. texture = SDL_CreateTextureFromSurface(renderer, temp);
  69. if (!texture) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  71. SDL_FreeSurface(temp);
  72. return NULL;
  73. }
  74. SDL_FreeSurface(temp);
  75. /* We're ready to roll. :) */
  76. return texture;
  77. }
  78. void
  79. Draw(DrawState *s)
  80. {
  81. SDL_Rect viewport;
  82. SDL_RenderGetViewport(s->renderer, &viewport);
  83. /* Draw the background */
  84. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  85. /* Scale and draw the sprite */
  86. s->sprite_rect.w += s->scale_direction;
  87. s->sprite_rect.h += s->scale_direction;
  88. if (s->scale_direction > 0) {
  89. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  90. s->scale_direction = -1;
  91. }
  92. } else {
  93. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  94. s->scale_direction = 1;
  95. }
  96. }
  97. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  98. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  99. SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
  100. /* Update the screen! */
  101. SDL_RenderPresent(s->renderer);
  102. }
  103. int
  104. main(int argc, char *argv[])
  105. {
  106. DrawState *drawstates;
  107. int i, done;
  108. SDL_Event event;
  109. int frames;
  110. Uint32 then, now;
  111. /* Enable standard application logging */
  112. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  113. /* Initialize test framework */
  114. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  115. if (!state) {
  116. return 1;
  117. }
  118. for (i = 1; i < argc;) {
  119. int consumed;
  120. consumed = SDLTest_CommonArg(state, i);
  121. if (consumed == 0) {
  122. SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
  123. return 1;
  124. }
  125. i += consumed;
  126. }
  127. if (!SDLTest_CommonInit(state)) {
  128. quit(2);
  129. }
  130. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  131. for (i = 0; i < state->num_windows; ++i) {
  132. DrawState *drawstate = &drawstates[i];
  133. drawstate->window = state->windows[i];
  134. drawstate->renderer = state->renderers[i];
  135. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
  136. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
  137. if (!drawstate->sprite || !drawstate->background) {
  138. quit(2);
  139. }
  140. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  141. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  142. drawstate->scale_direction = 1;
  143. }
  144. /* Main render loop */
  145. frames = 0;
  146. then = SDL_GetTicks();
  147. done = 0;
  148. while (!done) {
  149. /* Check for events */
  150. ++frames;
  151. while (SDL_PollEvent(&event)) {
  152. SDLTest_CommonEvent(state, &event, &done);
  153. }
  154. for (i = 0; i < state->num_windows; ++i) {
  155. if (state->windows[i] == NULL)
  156. continue;
  157. Draw(&drawstates[i]);
  158. }
  159. }
  160. /* Print out some timing information */
  161. now = SDL_GetTicks();
  162. if (now > then) {
  163. double fps = ((double) frames * 1000) / (now - then);
  164. SDL_Log("%2.2f frames per second\n", fps);
  165. }
  166. SDL_stack_free(drawstates);
  167. quit(0);
  168. return 0;
  169. }
  170. /* vi: set ts=4 sw=4 expandtab: */