testoffscreen.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright (C) 1997-2022 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: picks the offscreen backend and renders each frame to a bmp */
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #ifdef __EMSCRIPTEN__
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. #include "SDL.h"
  17. #include "SDL_stdinc.h"
  18. #include "SDL_opengl.h"
  19. static SDL_Renderer *renderer = NULL;
  20. static SDL_Window *window = NULL;
  21. static int done = SDL_FALSE;
  22. static int frame_number = 0;
  23. static int width = 640;
  24. static int height = 480;
  25. static unsigned int max_frames = 200;
  26. void
  27. draw()
  28. {
  29. SDL_Rect Rect;
  30. SDL_SetRenderDrawColor(renderer, 0x10, 0x9A, 0xCE, 0xFF);
  31. SDL_RenderClear(renderer);
  32. /* Grow based on the frame just to show a difference per frame of the region */
  33. Rect.x = 0;
  34. Rect.y = 0;
  35. Rect.w = (frame_number * 2) % width;
  36. Rect.h = (frame_number * 2) % height;
  37. SDL_SetRenderDrawColor(renderer, 0xFF, 0x10, 0x21, 0xFF);
  38. SDL_RenderFillRect(renderer, &Rect);
  39. SDL_RenderPresent(renderer);
  40. }
  41. void
  42. save_surface_to_bmp()
  43. {
  44. SDL_Surface* surface;
  45. Uint32 r_mask, g_mask, b_mask, a_mask;
  46. Uint32 pixel_format;
  47. char file[128];
  48. int bbp;
  49. pixel_format = SDL_GetWindowPixelFormat(window);
  50. SDL_PixelFormatEnumToMasks(pixel_format, &bbp, &r_mask, &g_mask, &b_mask, &a_mask);
  51. surface = SDL_CreateRGBSurface(0, width, height, bbp, r_mask, g_mask, b_mask, a_mask);
  52. SDL_RenderReadPixels(renderer, NULL, pixel_format, (void*)surface->pixels, surface->pitch);
  53. SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIs32 "-%8.8d.bmp",
  54. SDL_GetWindowID(window), ++frame_number);
  55. SDL_SaveBMP(surface, file);
  56. SDL_FreeSurface(surface);
  57. }
  58. void
  59. loop()
  60. {
  61. SDL_Event event;
  62. /* Check for events */
  63. while (SDL_PollEvent(&event)) {
  64. switch (event.type) {
  65. case SDL_QUIT:
  66. done = SDL_TRUE;
  67. break;
  68. }
  69. }
  70. draw();
  71. save_surface_to_bmp();
  72. #ifdef __EMSCRIPTEN__
  73. if (done) {
  74. emscripten_cancel_main_loop();
  75. }
  76. #endif
  77. }
  78. int
  79. main(int argc, char *argv[])
  80. {
  81. #ifndef __EMSCRIPTEN__
  82. Uint32 then, now, frames;
  83. #endif
  84. /* Enable standard application logging */
  85. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  86. /* Force the offscreen renderer, if it cannot be created then fail out */
  87. if (SDL_VideoInit("offscreen") < 0) {
  88. SDL_Log("Couldn't initialize the offscreen video driver: %s\n",
  89. SDL_GetError());
  90. return SDL_FALSE;
  91. }
  92. /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
  93. window = SDL_CreateWindow("Offscreen Test",
  94. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  95. width, height, 0);
  96. if (!window) {
  97. SDL_Log("Couldn't create window: %s\n",
  98. SDL_GetError());
  99. return SDL_FALSE;
  100. }
  101. renderer = SDL_CreateRenderer(window, -1, 0);
  102. if (!renderer) {
  103. SDL_Log("Couldn't create renderer: %s\n",
  104. SDL_GetError());
  105. return SDL_FALSE;
  106. }
  107. SDL_RenderClear(renderer);
  108. srand((unsigned int)time(NULL));
  109. #ifndef __EMSCRIPTEN__
  110. /* Main render loop */
  111. frames = 0;
  112. then = SDL_GetTicks();
  113. done = 0;
  114. #endif
  115. SDL_Log("Rendering %u frames offscreen\n", max_frames);
  116. #ifdef __EMSCRIPTEN__
  117. emscripten_set_main_loop(loop, 0, 1);
  118. #else
  119. while (!done && frames < max_frames) {
  120. ++frames;
  121. loop();
  122. /* Print out some timing information, along with remaining frames */
  123. if (frames % (max_frames / 10) == 0) {
  124. now = SDL_GetTicks();
  125. if (now > then) {
  126. double fps = ((double) frames * 1000) / (now - then);
  127. SDL_Log("Frames remaining: %" SDL_PRIu32 " rendering at %2.2f frames per second\n", max_frames - frames, fps);
  128. }
  129. }
  130. }
  131. #endif
  132. SDL_DestroyRenderer(renderer);
  133. SDL_DestroyWindow(window);
  134. SDL_Quit();
  135. return 0;
  136. }
  137. /* vi: set ts=4 sw=4 expandtab: */