testviewport.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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: Check viewports */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "SDL_test.h"
  15. #include "SDL_test_common.h"
  16. static SDLTest_CommonState *state;
  17. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  18. static void
  19. quit(int rc)
  20. {
  21. SDLTest_CommonQuit(state);
  22. exit(rc);
  23. }
  24. void
  25. DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
  26. {
  27. SDL_Rect rect;
  28. /* Set the viewport */
  29. SDL_RenderSetViewport(renderer, &viewport);
  30. /* Draw a gray background */
  31. SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
  32. SDL_RenderClear(renderer);
  33. /* Test inside points */
  34. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
  35. SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
  36. SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
  37. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - 10);
  38. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + 10);
  39. /* Test horizontal and vertical lines */
  40. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  41. SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
  42. SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
  43. SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
  44. SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
  45. /* Test diagonal lines */
  46. SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
  47. SDL_RenderDrawLine(renderer, 0, 0,
  48. viewport.w-1, viewport.h-1);
  49. SDL_RenderDrawLine(renderer, viewport.w-1, 0,
  50. 0, viewport.h-1);
  51. /* Test outside points */
  52. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
  53. SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
  54. SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
  55. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - viewport.w);
  56. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + viewport.w);
  57. /* Add a box at the top */
  58. rect.w = 8;
  59. rect.h = 8;
  60. rect.x = (viewport.w - rect.w) / 2;
  61. rect.y = 0;
  62. SDL_RenderFillRect(renderer, &rect);
  63. }
  64. int
  65. main(int argc, char *argv[])
  66. {
  67. int i, j, done;
  68. SDL_Event event;
  69. Uint32 then, now, frames;
  70. SDL_Rect viewport;
  71. SDL_bool use_target = SDL_FALSE;
  72. /* Initialize test framework */
  73. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  74. if (!state) {
  75. return 1;
  76. }
  77. for (i = 1; i < argc;) {
  78. int consumed;
  79. consumed = SDLTest_CommonArg(state, i);
  80. if (consumed == 0) {
  81. consumed = -1;
  82. if (SDL_strcasecmp(argv[i], "--target") == 0) {
  83. use_target = SDL_TRUE;
  84. consumed = 1;
  85. }
  86. }
  87. if (consumed < 0) {
  88. SDL_Log("Usage: %s %s [--target]\n",
  89. argv[0], SDLTest_CommonUsage(state));
  90. quit(1);
  91. }
  92. i += consumed;
  93. }
  94. if (!SDLTest_CommonInit(state)) {
  95. quit(2);
  96. }
  97. if (use_target) {
  98. int w, h;
  99. for (i = 0; i < state->num_windows; ++i) {
  100. SDL_GetWindowSize(state->windows[i], &w, &h);
  101. state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
  102. SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
  103. }
  104. }
  105. for (i = 0; i < state->num_windows; ++i) {
  106. SDL_Renderer *renderer = state->renderers[i];
  107. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  108. SDL_RenderClear(renderer);
  109. }
  110. /* Main render loop */
  111. frames = 0;
  112. then = SDL_GetTicks();
  113. done = 0;
  114. j = 0;
  115. while (!done) {
  116. /* Check for events */
  117. ++frames;
  118. while (SDL_PollEvent(&event)) {
  119. SDLTest_CommonEvent(state, &event, &done);
  120. }
  121. /* Move a viewport box in steps around the screen */
  122. viewport.x = j * 100;
  123. viewport.y = viewport.x;
  124. viewport.w = 100 + j * 50;
  125. viewport.h = 100 + j * 50;
  126. j = (j + 1) % 4;
  127. SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
  128. for (i = 0; i < state->num_windows; ++i) {
  129. if (state->windows[i] == NULL)
  130. continue;
  131. /* Draw using viewport */
  132. DrawOnViewport(state->renderers[i], viewport);
  133. /* Update the screen! */
  134. if (use_target) {
  135. SDL_SetRenderTarget(state->renderers[i], NULL);
  136. SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
  137. SDL_RenderPresent(state->renderers[i]);
  138. SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
  139. } else {
  140. SDL_RenderPresent(state->renderers[i]);
  141. }
  142. }
  143. SDL_Delay(1000);
  144. }
  145. /* Print out some timing information */
  146. now = SDL_GetTicks();
  147. if (now > then) {
  148. double fps = ((double) frames * 1000) / (now - then);
  149. SDL_Log("%2.2f frames per second\n", fps);
  150. }
  151. quit(0);
  152. return 0;
  153. }
  154. /* vi: set ts=4 sw=4 expandtab: */