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

170 lines
4.2 KiB

  1. /*
  2. Copyright (C) 1997-2020 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 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%d-%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. Uint32 then, now, frames;
  83. /* Enable standard application logging */
  84. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  85. /* Force the offscreen renderer, if it cannot be created then fail out */
  86. if (SDL_VideoInit("offscreen") < 0) {
  87. SDL_Log("Couldn't initialize the offscreen video driver: %s\n",
  88. SDL_GetError());
  89. return SDL_FALSE;
  90. }
  91. /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
  92. window = SDL_CreateWindow("Offscreen Test",
  93. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  94. width, height, 0);
  95. if (!window) {
  96. SDL_Log("Couldn't create window: %s\n",
  97. SDL_GetError());
  98. return SDL_FALSE;
  99. }
  100. renderer = SDL_CreateRenderer(window, -1, 0);
  101. if (!renderer) {
  102. SDL_Log("Couldn't create renderer: %s\n",
  103. SDL_GetError());
  104. return SDL_FALSE;
  105. }
  106. SDL_RenderClear(renderer);
  107. srand((unsigned int)time(NULL));
  108. /* Main render loop */
  109. frames = 0;
  110. then = SDL_GetTicks();
  111. done = 0;
  112. SDL_Log("Rendering %i frames offscreen\n", max_frames);
  113. #ifdef __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: %i 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. return 0;
  133. }
  134. /* vi: set ts=4 sw=4 expandtab: */