Debian Codegraph @

prboom2/src/SDL/i_sshot.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2006 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * Screenshot functions, moved out of i_video.c
31 *
32 *-----------------------------------------------------------------------------
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <stdlib.h>
40
41 #include "SDL.h"
42
43 #ifdef HAVE_LIBSDL2_IMAGE
44 #include <SDL_image.h>
45 #endif
46
47 #include "doomstat.h"
48 #include "doomdef.h"
49 #include "doomtype.h"
50 #include "v_video.h"
51 #include "i_video.h"
52 #include "z_zone.h"
53 #include "lprintf.h"
54
55 #include "dsda/gl/render_scale.h"
56
57 int renderW;
58 int renderH;
59
60 void I_UpdateRenderSize(void)
61 {
62 renderW = renderer_rect.w;
63 renderH = renderer_rect.h;
64 }
65
66 //
67 // I_ScreenShot // Modified to work with SDL2 resizeable window and fullscreen desktop - DTIED
68 //
69
70 int I_ScreenShot(const char *fname)
71 {
72 int result = -1;
73 unsigned char *pixels = I_GrabScreen();
74 SDL_Surface *screenshot = NULL;
75
76 if (pixels)
77 {
78 screenshot = SDL_CreateRGBSurfaceFrom(pixels, renderW, renderH, 24,
79 renderW * 3, 0x000000ff, 0x0000ff00, 0x00ff0000, 0);
80 }
81
82 if (screenshot)
83 {
84 #ifdef HAVE_LIBSDL2_IMAGE
85 result = IMG_SavePNG(screenshot, fname);
86 #else
87 result = SDL_SaveBMP(screenshot, fname);
88 #endif
89 SDL_FreeSurface(screenshot);
90 }
91 return result;
92 }
93
94 // NSM
95 // returns current screen contents as RGB24 (raw)
96 // returned pointer should be freed when done
97 //
98 // Modified to work with SDL2 resizeable window and fullscreen desktop - DTIED
99 //
100
101 unsigned char *I_GrabScreen(void)
102 {
103 static unsigned char *pixels = NULL;
104 static int pixels_size = 0;
105 int size;
106
107 I_UpdateRenderSize();
108
109 if (V_IsOpenGLMode())
110 {
111 return gld_ReadScreen();
112 }
113
114 size = renderW * renderH * 3;
115 if (!pixels || size > pixels_size)
116 {
117 pixels_size = size;
118 pixels = (unsigned char*)Z_Realloc(pixels, size);
119 }
120
121 if (pixels && size)
122 {
123 SDL_Rect screen = { 0, 0, renderW, renderH };
124 SDL_RenderReadPixels(sdl_renderer, &screen, SDL_PIXELFORMAT_RGB24, pixels, renderW * 3);
125 }
126
127 return pixels;
128 }