Debian Codegraph @

src/cdogs/grafx.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2019 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "grafx.h"
50
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <sys/types.h>
55
56 #include <SDL_events.h>
57 #include <SDL_mouse.h>
58
59 #include "blit.h"
60 #include "config.h"
61 #include "defs.h"
62 #include "draw/drawtools.h"
63 #include "files.h"
64 #include "font_utils.h"
65 #include "grafx_bg.h"
66 #include "log.h"
67 #include "palette.h"
68 #include "utils.h"
69
70 GraphicsDevice gGraphicsDevice;
71
72 void GraphicsInit(GraphicsDevice *device, Config *c)
73 {
74 memset(device, 0, sizeof *device);
75 GraphicsConfigSetFromConfig(&device->cachedConfig, c);
76 device->cachedConfig.RestartFlags = RESTART_ALL;
77 }
78
79 // Initialises the video subsystem.
80 // To prevent needless screen flickering, config is compared with cache
81 // to see if anything changed. If not, don't recreate the screen.
82 void GraphicsInitialize(GraphicsDevice *g)
83 {
84 if (g->IsInitialized && !g->cachedConfig.RestartFlags)
85 {
86 return;
87 }
88
89 if (!g->IsWindowInitialized)
90 {
91 char buf[CDOGS_PATH_MAX];
92 GetDataFilePath(buf, "graphics/cdogs_icon.bmp");
93 g->icon = LoadImgToSurface(buf);
94 g->IsWindowInitialized = true;
95 }
96
97 g->IsInitialized = false;
98
99 const int w = g->cachedConfig.Res.x;
100 const int h = g->cachedConfig.Res.y;
101
102 const bool initWindow = !!(g->cachedConfig.RestartFlags & RESTART_WINDOW);
103 const bool initTextures = !!(
104 g->cachedConfig.RestartFlags & (RESTART_WINDOW | RESTART_SCALE_MODE));
105 const bool initBrightness =
106 !!(g->cachedConfig.RestartFlags &
107 (RESTART_WINDOW | RESTART_SCALE_MODE | RESTART_BRIGHTNESS));
108
109 if (initWindow)
110 {
111 LOG(LM_GFX, LL_INFO, "graphics mode(%dx%d %dx%s)", w, h,
112 g->cachedConfig.ScaleFactor,
113 g->cachedConfig.Fullscreen ? " fullscreen" : "");
114
115 Uint32 windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
116 Rect2i windowDim = Rect2iNew(
117 svec2i(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED),
118 svec2i(
119 ConfigGetInt(&gConfig, "Graphics.WindowWidth"),
120 ConfigGetInt(&gConfig, "Graphics.WindowHeight")));
121 if (g->cachedConfig.Fullscreen)
122 {
123 windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
124 windowDim.Pos = svec2i_zero();
125 }
126 LOG(LM_GFX, LL_DEBUG, "destroying previous renderer");
127 WindowContextDestroy(&g->gameWindow);
128 WindowContextDestroy(&g->secondWindow);
129 SDL_FreeFormat(g->Format);
130
131 char title[32];
132 sprintf(
133 title, "C-Dogs SDL %s%s",
134 g->cachedConfig.IsEditor ? "Editor " : "", CDOGS_SDL_VERSION);
135 if (!WindowContextCreate(
136 &g->gameWindow, windowDim, windowFlags, title, g->icon,
137 svec2i(w, h)))
138 {
139 return;
140 }
141 if (g->cachedConfig.SecondWindow)
142 {
143 if (!WindowContextCreate(
144 &g->secondWindow, windowDim, windowFlags, title, g->icon,
145 svec2i(w, h)))
146 {
147 return;
148 }
149 WindowsAdjustPosition(&g->gameWindow, &g->secondWindow);
150 }
151
152 g->Format = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888);
153
154 // Need to reload textures due to them tied to the renderer (window)
155 PicManagerReloadTextures(&gPicManager);
156 FontLoadFromJSON(&gFont, "graphics/font.png", "graphics/font.json");
157 }
158
159 if (initTextures)
160 {
161 if (!initWindow)
162 {
163 WindowContextDestroyTextures(&g->gameWindow);
164 WindowContextDestroyTextures(&g->secondWindow);
165 WindowContextInitTextures(&g->gameWindow, svec2i(w, h));
166 WindowContextInitTextures(&g->secondWindow, svec2i(w, h));
167 }
168
169 GraphicsResetClip(g->gameWindow.renderer);
170
171 // Set render scale mode
172 const char *renderScaleQuality = "nearest";
173 switch ((ScaleMode)ConfigGetEnum(&gConfig, "Graphics.ScaleMode"))
174 {
175 case SCALE_MODE_NN:
176 renderScaleQuality = "nearest";
177 break;
178 case SCALE_MODE_BILINEAR:
179 renderScaleQuality = "linear";
180 break;
181 default:
182 CASSERT(false, "unknown scale mode");
183 break;
184 }
185 LOG(LM_GFX, LL_DEBUG, "setting scale quality %s", renderScaleQuality);
186 if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, renderScaleQuality))
187 {
188 LOG(LM_GFX, LL_WARN, "cannot set render quality hint: %s",
189 SDL_GetError());
190 }
191
192 CFREE(g->buf);
193 CCALLOC(g->buf, GraphicsGetMemSize(&g->cachedConfig));
194 g->bkgTgt = WindowContextCreateTexture(
195 &g->gameWindow, SDL_TEXTUREACCESS_TARGET, svec2i(w, h),
196 SDL_BLENDMODE_NONE, 255, true);
197 if (g->bkgTgt == NULL)
198 {
199 return;
200 }
201 g->bkg = WindowContextCreateTexture(
202 &g->gameWindow, SDL_TEXTUREACCESS_STATIC, svec2i(w, h),
203 SDL_BLENDMODE_BLEND, 255, true);
204 if (g->bkg == NULL)
205 {
206 return;
207 }
208 if (g->cachedConfig.SecondWindow)
209 {
210 g->bkgTgt2 = WindowContextCreateTexture(
211 &g->secondWindow, SDL_TEXTUREACCESS_TARGET, svec2i(w, h),
212 SDL_BLENDMODE_NONE, 255, true);
213 if (g->bkgTgt2 == NULL)
214 {
215 return;
216 }
217 g->bkg2 = WindowContextCreateTexture(
218 &g->secondWindow, SDL_TEXTUREACCESS_STATIC, svec2i(w, h),
219 SDL_BLENDMODE_BLEND, 255, true);
220 if (g->bkg2 == NULL)
221 {
222 return;
223 }
224 }
225
226 g->screen = WindowContextCreateTexture(
227 &g->gameWindow, SDL_TEXTUREACCESS_STREAMING, svec2i(w, h),
228 SDL_BLENDMODE_BLEND, 255, false);
229 if (g->screen == NULL)
230 {
231 return;
232 }
233
234 g->hud = WindowContextCreateTexture(
235 &g->gameWindow, SDL_TEXTUREACCESS_STREAMING, svec2i(w, h),
236 SDL_BLENDMODE_BLEND, 255, false);
237 if (g->hud == NULL)
238 {
239 return;
240 }
241 BlitClearBuf(g);
242 BlitUpdateFromBuf(g, g->hud);
243
244 if (g->cachedConfig.SecondWindow)
245 {
246 g->hud2 = WindowContextCreateTexture(
247 &g->secondWindow, SDL_TEXTUREACCESS_STREAMING, svec2i(w, h),
248 SDL_BLENDMODE_BLEND, 255, false);
249 if (g->hud2 == NULL)
250 {
251 return;
252 }
253 BlitClearBuf(g);
254 BlitUpdateFromBuf(g, g->hud2);
255 }
256 }
257
258 if (initBrightness)
259 {
260 if (!initWindow && !initTextures)
261 {
262 SDL_DestroyTexture(g->brightnessOverlay);
263 }
264
265 const int brightness = ConfigGetInt(&gConfig, "Graphics.Brightness");
266 // Alpha is approximately 50% max
267 const Uint8 alpha =
268 (Uint8)(brightness > 0 ? brightness : -brightness) * 13;
269 g->brightnessOverlay = WindowContextCreateTexture(
270 &g->gameWindow, SDL_TEXTUREACCESS_STATIC, svec2i(w, h),
271 SDL_BLENDMODE_BLEND, alpha, false);
272 if (g->brightnessOverlay == NULL)
273 {
274 return;
275 }
276 const color_t overlayColour = brightness > 0 ? colorWhite : colorBlack;
277 BlitFillBuf(g, overlayColour);
278 BlitUpdateFromBuf(g, g->brightnessOverlay);
279 g->cachedConfig.Brightness = brightness;
280 }
281
282 g->IsInitialized = true;
283 g->cachedConfig.Res.x = w;
284 g->cachedConfig.Res.y = h;
285 g->cachedConfig.RestartFlags = 0;
286 }
287
288 void GraphicsTerminate(GraphicsDevice *g)
289 {
290 WindowContextDestroy(&g->gameWindow);
291 WindowContextDestroy(&g->secondWindow);
292 SDL_FreeFormat(g->Format);
293 SDL_VideoQuit();
294 CFREE(g->buf);
295 }
296
297 int GraphicsGetScreenSize(GraphicsConfig *config)
298 {
299 return config->Res.x * config->Res.y;
300 }
301
302 int GraphicsGetMemSize(GraphicsConfig *config)
303 {
304 return GraphicsGetScreenSize(config) * sizeof(Uint32);
305 }
306
307 void GraphicsConfigSet(
308 GraphicsConfig *c, struct vec2i windowSize, const bool fullscreen,
309 const int scaleFactor, const ScaleMode scaleMode, const int brightness,
310 const bool secondWindow)
311 {
312 #define SET(_lhs, _rhs, _flag) \
313 if ((_lhs) != (_rhs)) \
314 { \
315 (_lhs) = (_rhs); \
316 c->RestartFlags |= (_flag); \
317 }
318 SET(c->Fullscreen, fullscreen, RESTART_WINDOW);
319 if (c->Fullscreen)
320 {
321 // Set to native resolution
322 SDL_DisplayMode dm;
323 if (SDL_GetCurrentDisplayMode(0, &dm) != 0)
324 {
325 LOG(LM_GFX, LL_WARN, "cannot get display mode: %s",
326 SDL_GetError());
327 }
328 else
329 {
330 windowSize = svec2i(dm.w, dm.h);
331 ConfigSetInt(&gConfig, "Graphics.WindowWidth", dm.w);
332 ConfigSetInt(&gConfig, "Graphics.WindowHeight", dm.h);
333 }
334 }
335 SET(c->ScaleFactor, scaleFactor, RESTART_SCALE_MODE);
336 SET(c->ScaleMode, scaleMode, RESTART_SCALE_MODE);
337 SET(c->Brightness, brightness, RESTART_BRIGHTNESS);
338 SET(c->SecondWindow, secondWindow, RESTART_WINDOW);
339 const struct vec2i res = svec2i_scale_divide(windowSize, scaleFactor);
340 if (!svec2i_is_equal(res, c->Res))
341 {
342 c->Res = res;
343 c->RestartFlags |= RESTART_SCALE_MODE;
344 }
345 }
346
347 void GraphicsConfigSetFromConfig(GraphicsConfig *gc, Config *c)
348 {
349 GraphicsConfigSet(
350 gc,
351 svec2i(
352 ConfigGetInt(c, "Graphics.WindowWidth"),
353 ConfigGetInt(c, "Graphics.WindowHeight")),
354 ConfigGetBool(c, "Graphics.Fullscreen"),
355 ConfigGetInt(c, "Graphics.ScaleFactor"),
356 (ScaleMode)ConfigGetEnum(c, "Graphics.ScaleMode"),
357 ConfigGetInt(c, "Graphics.Brightness"),
358 ConfigGetBool(c, "Graphics.SecondWindow"));
359 }
360
361 void GraphicsSetClip(SDL_Renderer *renderer, const Rect2i r)
362 {
363 const SDL_Rect rect = {r.Pos.x, r.Pos.y, r.Size.x, r.Size.y};
364 if (SDL_RenderSetClipRect(renderer, Rect2iIsZero(r) ? NULL : &rect) != 0)
365 {
366 LOG(LM_MAIN, LL_ERROR, "Could not set clip rect: %s", SDL_GetError());
367 }
368 }
369
370 Rect2i GraphicsGetClip(SDL_Renderer *renderer)
371 {
372 SDL_Rect rect;
373 SDL_RenderGetClipRect(renderer, &rect);
374 return Rect2iNew(svec2i(rect.x, rect.y), svec2i(rect.w, rect.h));
375 }
376
377 void GraphicsResetClip(SDL_Renderer *renderer)
378 {
379 if (SDL_RenderSetClipRect(renderer, NULL) != 0)
380 {
381 LOG(LM_MAIN, LL_ERROR, "Could not reset clip rect: %s",
382 SDL_GetError());
383 }
384 }