1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 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
22 #include "../../SDL_internal.h"
23
24 #if SDL_VIDEO_DRIVER_OFFSCREEN
25
26 #include "../SDL_sysvideo.h"
27 #include "SDL_offscreenframebuffer_c.h"
28
29
30 #define OFFSCREEN_SURFACE "_SDL_DummySurface"
31
SDL_OFFSCREEN_CreateWindowFramebuffer(_THIS,SDL_Window * window,Uint32 * format,void ** pixels,int * pitch)32 int SDL_OFFSCREEN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
33 {
34 SDL_Surface *surface;
35 const Uint32 surface_format = SDL_PIXELFORMAT_RGB888;
36 int w, h;
37 int bpp;
38 Uint32 Rmask, Gmask, Bmask, Amask;
39
40 /* Free the old framebuffer surface */
41 surface = (SDL_Surface *) SDL_GetWindowData(window, OFFSCREEN_SURFACE);
42 SDL_FreeSurface(surface);
43
44 /* Create a new one */
45 SDL_PixelFormatEnumToMasks(surface_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
46 SDL_GetWindowSize(window, &w, &h);
47 surface = SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
48 if (!surface) {
49 return -1;
50 }
51
52 /* Save the info and return! */
53 SDL_SetWindowData(window, OFFSCREEN_SURFACE, surface);
54 *format = surface_format;
55 *pixels = surface->pixels;
56 *pitch = surface->pitch;
57 return 0;
58 }
59
SDL_OFFSCREEN_UpdateWindowFramebuffer(_THIS,SDL_Window * window,const SDL_Rect * rects,int numrects)60 int SDL_OFFSCREEN_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
61 {
62 static int frame_number;
63 SDL_Surface *surface;
64
65 surface = (SDL_Surface *) SDL_GetWindowData(window, OFFSCREEN_SURFACE);
66 if (!surface) {
67 return SDL_SetError("Couldn't find offscreen surface for window");
68 }
69
70 /* Send the data to the display */
71 if (SDL_getenv("SDL_VIDEO_OFFSCREEN_SAVE_FRAMES")) {
72 char file[128];
73 SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
74 SDL_GetWindowID(window), ++frame_number);
75 SDL_SaveBMP(surface, file);
76 }
77 return 0;
78 }
79
SDL_OFFSCREEN_DestroyWindowFramebuffer(_THIS,SDL_Window * window)80 void SDL_OFFSCREEN_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
81 {
82 SDL_Surface *surface;
83
84 surface = (SDL_Surface *) SDL_SetWindowData(window, OFFSCREEN_SURFACE, NULL);
85 SDL_FreeSurface(surface);
86 }
87
88 #endif /* SDL_VIDEO_DRIVER_OFFSCREEN */
89
90 /* vi: set ts=4 sw=4 expandtab: */
91