testoffscreen.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (C) 1997-2025 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 <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. #include <SDL3/SDL_test.h>
  14. #include <SDL3/SDL_opengl.h>
  15. #ifdef SDL_PLATFORM_EMSCRIPTEN
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. static SDL_Renderer *renderer = NULL;
  19. static SDL_Window *window = NULL;
  20. static int done = false;
  21. static int frame_number = 0;
  22. static int width = 640;
  23. static int height = 480;
  24. static unsigned int max_frames = 200;
  25. static void draw(void)
  26. {
  27. SDL_FRect rect;
  28. SDL_SetRenderDrawColor(renderer, 0x10, 0x9A, 0xCE, 0xFF);
  29. SDL_RenderClear(renderer);
  30. /* Grow based on the frame just to show a difference per frame of the region */
  31. rect.x = 0.0f;
  32. rect.y = 0.0f;
  33. rect.w = (float)((frame_number * 2) % width);
  34. rect.h = (float)((frame_number * 2) % height);
  35. SDL_SetRenderDrawColor(renderer, 0xFF, 0x10, 0x21, 0xFF);
  36. SDL_RenderFillRect(renderer, &rect);
  37. SDL_RenderPresent(renderer);
  38. }
  39. static void save_surface_to_bmp(void)
  40. {
  41. SDL_Surface *surface;
  42. char file[128];
  43. surface = SDL_RenderReadPixels(renderer, NULL);
  44. (void)SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIs32 "-%8.8d.bmp",
  45. SDL_GetWindowID(window), ++frame_number);
  46. SDL_SaveBMP(surface, file);
  47. SDL_DestroySurface(surface);
  48. }
  49. static void loop(void)
  50. {
  51. SDL_Event event;
  52. /* Check for events */
  53. while (SDL_PollEvent(&event)) {
  54. switch (event.type) {
  55. case SDL_EVENT_QUIT:
  56. done = true;
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. draw();
  63. save_surface_to_bmp();
  64. #ifdef SDL_PLATFORM_EMSCRIPTEN
  65. if (done) {
  66. emscripten_cancel_main_loop();
  67. }
  68. #endif
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. #ifndef SDL_PLATFORM_EMSCRIPTEN
  73. Uint64 then, now;
  74. Uint32 frames;
  75. #endif
  76. SDLTest_CommonState *state;
  77. /* Initialize test framework */
  78. state = SDLTest_CommonCreateState(argv, 0);
  79. if (!state) {
  80. return 1;
  81. }
  82. /* Parse commandline */
  83. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  84. return 1;
  85. }
  86. /* Force the offscreen renderer, if it cannot be created then fail out */
  87. SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "offscreen");
  88. if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) {
  89. SDL_Log("Couldn't initialize the offscreen video driver: %s\n",
  90. SDL_GetError());
  91. return 1;
  92. }
  93. /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
  94. window = SDL_CreateWindow("Offscreen Test", width, height, 0);
  95. if (!window) {
  96. SDL_Log("Couldn't create window: %s\n", SDL_GetError());
  97. return 1;
  98. }
  99. renderer = SDL_CreateRenderer(window, NULL);
  100. if (!renderer) {
  101. SDL_Log("Couldn't create renderer: %s\n",
  102. SDL_GetError());
  103. return 1;
  104. }
  105. SDL_RenderClear(renderer);
  106. #ifndef SDL_PLATFORM_EMSCRIPTEN
  107. /* Main render loop */
  108. frames = 0;
  109. then = SDL_GetTicks();
  110. done = 0;
  111. #endif
  112. SDL_Log("Rendering %u frames offscreen\n", max_frames);
  113. #ifdef SDL_PLATFORM_EMSCRIPTEN
  114. emscripten_set_main_loop(loop, 0, 1);
  115. #else
  116. while (!done && frames < max_frames) {
  117. ++frames;
  118. loop();
  119. /* Print out some timing information, along with remaining frames */
  120. if (frames % (max_frames / 10) == 0) {
  121. now = SDL_GetTicks();
  122. if (now > then) {
  123. double fps = ((double)frames * 1000) / (now - then);
  124. SDL_Log("Frames remaining: %" SDL_PRIu32 " rendering at %2.2f frames per second\n", max_frames - frames, fps);
  125. }
  126. }
  127. }
  128. #endif
  129. SDL_DestroyRenderer(renderer);
  130. SDL_DestroyWindow(window);
  131. SDL_Quit();
  132. SDLTest_CommonDestroyState(state);
  133. return 0;
  134. }