| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |