🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.3 KiB

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