🛠️🐜 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.

169 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 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 save_surface_to_bmp()
  42. {
  43. SDL_Surface *surface;
  44. Uint32 r_mask, g_mask, b_mask, a_mask;
  45. Uint32 pixel_format;
  46. char file[128];
  47. int bbp;
  48. pixel_format = SDL_GetWindowPixelFormat(window);
  49. SDL_PixelFormatEnumToMasks(pixel_format, &bbp, &r_mask, &g_mask, &b_mask, &a_mask);
  50. surface = SDL_CreateRGBSurface(0, width, height, bbp, r_mask, g_mask, b_mask, a_mask);
  51. SDL_RenderReadPixels(renderer, NULL, pixel_format, (void*)surface->pixels, surface->pitch);
  52. SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIs32 "-%8.8d.bmp",
  53. SDL_GetWindowID(window), ++frame_number);
  54. SDL_SaveBMP(surface, file);
  55. SDL_FreeSurface(surface);
  56. }
  57. void loop()
  58. {
  59. SDL_Event event;
  60. /* Check for events */
  61. while (SDL_PollEvent(&event)) {
  62. switch (event.type) {
  63. case SDL_QUIT:
  64. done = SDL_TRUE;
  65. break;
  66. }
  67. }
  68. draw();
  69. save_surface_to_bmp();
  70. #ifdef __EMSCRIPTEN__
  71. if (done) {
  72. emscripten_cancel_main_loop();
  73. }
  74. #endif
  75. }
  76. int main(int argc, char *argv[])
  77. {
  78. #ifndef __EMSCRIPTEN__
  79. Uint32 then, now, frames;
  80. #endif
  81. /* Enable standard application logging */
  82. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  83. /* Force the offscreen renderer, if it cannot be created then fail out */
  84. if (SDL_VideoInit("offscreen") < 0) {
  85. SDL_Log("Couldn't initialize the offscreen video driver: %s\n",
  86. SDL_GetError());
  87. return SDL_FALSE;
  88. }
  89. /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
  90. window = SDL_CreateWindow("Offscreen Test",
  91. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  92. width, height, 0);
  93. if (window == NULL) {
  94. SDL_Log("Couldn't create window: %s\n", SDL_GetError());
  95. return SDL_FALSE;
  96. }
  97. renderer = SDL_CreateRenderer(window, -1, 0);
  98. if (renderer == NULL) {
  99. SDL_Log("Couldn't create renderer: %s\n",
  100. SDL_GetError());
  101. return SDL_FALSE;
  102. }
  103. SDL_RenderClear(renderer);
  104. srand((unsigned int)time(NULL));
  105. #ifndef __EMSCRIPTEN__
  106. /* Main render loop */
  107. frames = 0;
  108. then = SDL_GetTicks();
  109. done = 0;
  110. #endif
  111. SDL_Log("Rendering %u frames offscreen\n", max_frames);
  112. #ifdef __EMSCRIPTEN__
  113. emscripten_set_main_loop(loop, 0, 1);
  114. #else
  115. while (!done && frames < max_frames) {
  116. ++frames;
  117. loop();
  118. /* Print out some timing information, along with remaining frames */
  119. if (frames % (max_frames / 10) == 0) {
  120. now = SDL_GetTicks();
  121. if (now > then) {
  122. double fps = ((double)frames * 1000) / (now - then);
  123. SDL_Log("Frames remaining: %" SDL_PRIu32 " rendering at %2.2f frames per second\n", max_frames - frames, fps);
  124. }
  125. }
  126. }
  127. #endif
  128. SDL_DestroyRenderer(renderer);
  129. SDL_DestroyWindow(window);
  130. SDL_Quit();
  131. return 0;
  132. }
  133. /* vi: set ts=4 sw=4 expandtab: */