Debian Codegraph @

similar/arch/sdl/gr.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 *
9 * SDL video functions.
10 *
11 */
12
13 #include <algorithm>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <math.h>
18 #include <SDL.h>
19 #include "gr.h"
20 #include "grdef.h"
21 #include "palette.h"
22 #include "game.h"
23 #include "console.h"
24 #include "u_mem.h"
25 #include "dxxerror.h"
26 #include "vers_id.h"
27 #include "gamefont.h"
28 #include "args.h"
29 #include "config.h"
30 #include "palette.h"
31
32 #include "compiler-range_for.h"
33 #include "d_range.h"
34 #include <memory>
35
36 using std::min;
37
38 namespace dcx {
39
40 static int sdl_video_flags = SDL_SWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
41 static SDL_Surface *screen, *canvas;
42 static int gr_installed;
43
44 void gr_flip()
45 {
46 SDL_BlitSurface(canvas, nullptr, screen, nullptr);
47 SDL_Flip(screen);
48 }
49
50 // returns possible (fullscreen) resolutions if any.
51 uint_fast32_t gr_list_modes(std::array<screen_mode, 50> &gsmodes)
52 {
53 int modesnum{0};
54 int sdl_check_flags = sdl_video_flags;
55
56 sdl_check_flags |= SDL_FULLSCREEN; // always use Fullscreen as lead.
57
58 SDL_Rect **const modes{SDL_ListModes(NULL, sdl_check_flags)};
59 if (modes == nullptr) // check if we get any modes - if not, return 0
60 return 0;
61
62 if (modes == reinterpret_cast<SDL_Rect**>(-1))
63 {
64 return 0; // can obviously use any resolution... strange!
65 }
66 else
67 {
68 for (int i = 0; modes[i]; ++i)
69 {
70 if (modes[i]->w > 0xFFF0 || modes[i]->h > 0xFFF0 // resolutions saved in 32bits. so skip bigger ones (unrealistic in 2010) (kreatordxx - made 0xFFF0 to kill warning)
71 || modes[i]->w < 320 || modes[i]->h < 200) // also skip everything smaller than 320x200
72 continue;
73 gsmodes[modesnum].width = modes[i]->w;
74 gsmodes[modesnum].height = modes[i]->h;
75 modesnum++;
76 if (modesnum >= gsmodes.size()) // that really seems to be enough big boy.
77 break;
78 }
79 return modesnum;
80 }
81 }
82
83 }
84
85 namespace dsx {
86
87 int gr_set_mode(screen_mode mode)
88 {
89 screen=NULL;
90
91 SDL_WM_SetCaption(DESCENT_VERSION, DXX_SDL_WINDOW_CAPTION);
92 SDL_WM_SetIcon( SDL_LoadBMP( DXX_SDL_WINDOW_ICON_BITMAP ), NULL );
93
94 const auto sdl_video_flags = ::sdl_video_flags;
95 const auto DbgBpp{CGameArg.DbgBpp};
96 if(SDL_VideoModeOK(SM_W(mode), SM_H(mode), DbgBpp, sdl_video_flags))
97 {
98 }
99 else
100 {
101 con_printf(CON_URGENT,"Cannot set %hux%hu. Fallback to 640x480", SM_W(mode), SM_H(mode));
102 mode.width = 640;
103 mode.height = 480;
104 Game_screen_mode = mode;
105 }
106 const unsigned w = SM_W(mode), h = SM_H(mode);
107 screen = SDL_SetVideoMode(w, h, DbgBpp, sdl_video_flags);
108
109 if (screen == NULL)
110 {
111 Error("Could not set %dx%dx%d video mode\n", w, h, DbgBpp);
112 exit(1);
113 }
114
115 canvas = SDL_CreateRGBSurface(sdl_video_flags, w, h, 8, 0, 0, 0, 0);
116 if (canvas == NULL)
117 {
118 Error("Could not create canvas surface\n");
119 exit(1);
120 }
121
122 *grd_curscreen = {};
123 grd_curscreen->sdl_surface = RAII_SDL_Surface(canvas);
124 grd_curscreen->set_screen_width_height(w, h);
125 grd_curscreen->sc_aspect = fixdiv(grd_curscreen->get_screen_width() * CGameCfg.AspectX, grd_curscreen->get_screen_height() * CGameCfg.AspectY);
126 gr_init_canvas(grd_curscreen->sc_canvas, reinterpret_cast<unsigned char *>(canvas->pixels), bm_mode::linear, w, h);
127 window_update_canvases();
128 gr_set_default_canvas();
129
130 SDL_ShowCursor(0);
131 gamefont_choose_game_font(w,h);
132 gr_palette_load(gr_palette);
133 gr_remap_color_fonts();
134
135 return 0;
136 }
137
138 }
139
140 namespace dcx {
141
142 int gr_check_fullscreen(void)
143 {
144 return !!(sdl_video_flags & SDL_FULLSCREEN);
145 }
146
147 void gr_toggle_fullscreen()
148 {
149 sdl_video_flags ^= SDL_FULLSCREEN;
150 const int WindowMode = !(sdl_video_flags & SDL_FULLSCREEN);
151 CGameCfg.WindowMode = WindowMode;
152 gr_remap_color_fonts();
153 SDL_WM_ToggleFullScreen(screen);
154 }
155
156 }
157
158 namespace dsx {
159
160 int gr_init()
161 {
162 // Only do this function once!
163 if (gr_installed==1)
164 return -1;
165
166 if (SDL_Init(SDL_INIT_VIDEO) < 0)
167 {
168 Error("SDL library video initialisation failed: %s.",SDL_GetError());
169 }
170
171 grd_curscreen = std::make_unique<grs_screen>();
172
173 if (!CGameCfg.WindowMode && !CGameArg.SysWindow)
174 sdl_video_flags|=SDL_FULLSCREEN;
175
176 if (CGameArg.SysNoBorders)
177 sdl_video_flags|=SDL_NOFRAME;
178
179 if (CGameArg.DbgSdlHWSurface)
180 sdl_video_flags|=SDL_HWSURFACE;
181
182 if (CGameArg.DbgSdlASyncBlit)
183 sdl_video_flags|=SDL_ASYNCBLIT;
184
185 // Set the mode.
186 grd_curscreen->sc_canvas.cv_fade_level = GR_FADE_OFF;
187 grd_curscreen->sc_canvas.cv_font = NULL;
188 grd_curscreen->sc_canvas.cv_font_fg_color = 0;
189 grd_curscreen->sc_canvas.cv_font_bg_color = 0;
190 gr_set_current_canvas( &grd_curscreen->sc_canvas );
191
192 gr_installed = 1;
193
194 return 0;
195 }
196
197 void gr_close()
198 {
199 if (gr_installed==1)
200 {
201 gr_installed = 0;
202 grd_curscreen.reset();
203 SDL_ShowCursor(1);
204 }
205 }
206
207 }
208
209 namespace dcx {
210
211 // Palette functions follow.
212 static int last_r=0, last_g=0, last_b=0;
213
214 void gr_palette_step_up( int r, int g, int b )
215 {
216 palette_array_t &p = gr_palette;
217 SDL_Palette *palette;
218
219 if ( (r==last_r) && (g==last_g) && (b==last_b) )
220 return;
221
222 last_r = r;
223 last_g = g;
224 last_b = b;
225
226 palette = canvas->format->palette;
227
228 if (palette == NULL)
229 return; // Display is not palettised
230
231 std::array<SDL_Color, 256> colors{};
232 range_for (const int i, xrange(256u))
233 {
234 const auto ir = static_cast<int>(p[i].r) + r + gr_palette_gamma;
235 colors[i].r = std::clamp(ir, 0, 63) * 4;
236 const auto ig = static_cast<int>(p[i].g) + g + gr_palette_gamma;
237 colors[i].g = std::clamp(ig, 0, 63) * 4;
238 const auto ib = static_cast<int>(p[i].b) + b + gr_palette_gamma;
239 colors[i].b = std::clamp(ib, 0, 63) * 4;
240 }
241 SDL_SetColors(canvas, colors.data(), 0, colors.size());
242 }
243
244 void gr_palette_load( palette_array_t &pal )
245 {
246 SDL_Palette *palette;
247 std::array<uint8_t, 64> gamma;
248
249 if (pal != gr_current_pal)
250 SDL_FillRect(canvas, NULL, SDL_MapRGB(canvas->format, 0, 0, 0));
251
252 copy_bound_palette(gr_current_pal, pal);
253
254 if (canvas == NULL)
255 return;
256
257 palette = canvas->format->palette;
258
259 if (palette == NULL)
260 return; // Display is not palettised
261
262 range_for (const int i, xrange(64u))
263 gamma[i] = static_cast<int>((pow((14.0 / 32.0), 1.0) * i) + 0.5);
264
265 std::array<SDL_Color, 256> colors{};
266 for (int i = 0, j = 0; j < 256; j++)
267 {
268 const auto c = gr_find_closest_color(gamma[gr_palette[j].r], gamma[gr_palette[j].g], gamma[gr_palette[j].b]);
269 gr_fade_table[(gr_fade_level{14})][j] = c;
270 colors[j].r = (min(gr_current_pal[i].r + gr_palette_gamma, 63)) * 4;
271 colors[j].g = (min(gr_current_pal[i].g + gr_palette_gamma, 63)) * 4;
272 colors[j].b = (min(gr_current_pal[i].b + gr_palette_gamma, 63)) * 4;
273 i++;
274 }
275
276 SDL_SetColors(canvas, colors.data(), 0, colors.size());
277 reset_computed_colors();
278 gr_remap_color_fonts();
279 }
280
281 void gr_palette_read(palette_array_t &pal)
282 {
283 SDL_Palette *palette;
284 unsigned i;
285
286 palette = canvas->format->palette;
287
288 if (palette == NULL)
289 return; // Display is not palettised
290
291 for (i = 0; i < 256; i++)
292 {
293 pal[i].r = palette->colors[i].r / 4;
294 pal[i].g = palette->colors[i].g / 4;
295 pal[i].b = palette->colors[i].b / 4;
296 }
297 }
298
299 }