SDL_nullframebuffer.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_DUMMY
  20. #include "../SDL_sysvideo.h"
  21. #include "SDL_nullframebuffer_c.h"
  22. #define DUMMY_SURFACE "_SDL_DummySurface"
  23. int SDL_DUMMY_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *format, void **pixels, int *pitch)
  24. {
  25. SDL_Surface *surface;
  26. const Uint32 surface_format = SDL_PIXELFORMAT_RGB888;
  27. int w, h;
  28. /* Free the old framebuffer surface */
  29. SDL_DUMMY_DestroyWindowFramebuffer(_this, window);
  30. /* Create a new one */
  31. SDL_GetWindowSizeInPixels(window, &w, &h);
  32. surface = SDL_CreateSurface(w, h, surface_format);
  33. if (surface == NULL) {
  34. return -1;
  35. }
  36. /* Save the info and return! */
  37. SDL_SetWindowData(window, DUMMY_SURFACE, surface);
  38. *format = surface_format;
  39. *pixels = surface->pixels;
  40. *pitch = surface->pitch;
  41. return 0;
  42. }
  43. int SDL_DUMMY_UpdateWindowFramebuffer(_THIS, SDL_Window *window, const SDL_Rect *rects, int numrects)
  44. {
  45. static int frame_number;
  46. SDL_Surface *surface;
  47. surface = (SDL_Surface *)SDL_GetWindowData(window, DUMMY_SURFACE);
  48. if (surface == NULL) {
  49. return SDL_SetError("Couldn't find dummy surface for window");
  50. }
  51. /* Send the data to the display */
  52. if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
  53. char file[128];
  54. (void)SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIu32 "-%8.8d.bmp",
  55. SDL_GetWindowID(window), ++frame_number);
  56. SDL_SaveBMP(surface, file);
  57. }
  58. return 0;
  59. }
  60. void SDL_DUMMY_DestroyWindowFramebuffer(_THIS, SDL_Window *window)
  61. {
  62. SDL_Surface *surface;
  63. surface = (SDL_Surface *)SDL_SetWindowData(window, DUMMY_SURFACE, NULL);
  64. SDL_DestroySurface(surface);
  65. }
  66. #endif /* SDL_VIDEO_DRIVER_DUMMY */