| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | #ifdef SDL_VIDEO_DRIVER_OFFSCREEN |
| 24 | |
| 25 | #include "../SDL_sysvideo.h" |
| 26 | #include "../../SDL_properties_c.h" |
| 27 | #include "SDL_offscreenframebuffer_c.h" |
| 28 | |
| 29 | #define OFFSCREEN_SURFACE "SDL.internal.window.surface" |
| 30 | |
| 31 | |
| 32 | bool SDL_OFFSCREEN_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch) |
| 33 | { |
| 34 | SDL_Surface *surface; |
| 35 | const SDL_PixelFormat surface_format = SDL_PIXELFORMAT_XRGB8888; |
| 36 | int w, h; |
| 37 | |
| 38 | // Create a new framebuffer |
| 39 | SDL_GetWindowSizeInPixels(window, &w, &h); |
| 40 | surface = SDL_CreateSurface(w, h, surface_format); |
| 41 | if (!surface) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | // Save the info and return! |
| 46 | SDL_SetSurfaceProperty(SDL_GetWindowProperties(window), OFFSCREEN_SURFACE, surface); |
| 47 | *format = surface_format; |
| 48 | *pixels = surface->pixels; |
| 49 | *pitch = surface->pitch; |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool SDL_OFFSCREEN_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects) |
| 55 | { |
| 56 | static int frame_number; |
| 57 | SDL_Surface *surface; |
| 58 | |
| 59 | surface = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), OFFSCREEN_SURFACE, NULL); |
| 60 | if (!surface) { |
| 61 | return SDL_SetError("Couldn't find offscreen surface for window" ); |
| 62 | } |
| 63 | |
| 64 | // Send the data to the display |
| 65 | if (SDL_GetHintBoolean(SDL_HINT_VIDEO_OFFSCREEN_SAVE_FRAMES, false)) { |
| 66 | char file[128]; |
| 67 | (void)SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIu32 "-%8.8d.bmp" , |
| 68 | SDL_GetWindowID(window), ++frame_number); |
| 69 | SDL_SaveBMP(surface, file); |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void SDL_OFFSCREEN_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window) |
| 75 | { |
| 76 | SDL_ClearProperty(SDL_GetWindowProperties(window), OFFSCREEN_SURFACE); |
| 77 | } |
| 78 | |
| 79 | #endif // SDL_VIDEO_DRIVER_OFFSCREEN |
| 80 | |