testoffscreen.c 4.2 KB

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