From 7c2ec8d1665a8666f12697b8963e392e4a76eb36 Mon Sep 17 00:00:00 2001 From: Cong Date: Thu, 8 Feb 2018 22:50:10 +1100 Subject: [PATCH] Fix editor bg drawing (#500) --- src/cdogs/config_apply.c | 2 +- src/cdogs/grafx.c | 51 ++++++++++++++++++-------------------- src/cdogs/grafx.h | 2 +- src/cdogs/window_context.c | 17 ++++++++++--- src/cdogs/window_context.h | 2 ++ 5 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/cdogs/config_apply.c b/src/cdogs/config_apply.c index 81201d81c..ccc11b7b8 100644 --- a/src/cdogs/config_apply.c +++ b/src/cdogs/config_apply.c @@ -47,7 +47,7 @@ bool ConfigApply(Config *config) GraphicsConfigSetFromConfig(&gGraphicsDevice.cachedConfig, config); const bool makeBackground = gGraphicsDevice.cachedConfig.RestartFlags & - (RESTART_RESOLUTION | RESTART_SCALE_MODE); + (RESTART_WINDOW | RESTART_SCALE_MODE); GraphicsInitialize(&gGraphicsDevice); if (makeBackground) { diff --git a/src/cdogs/grafx.c b/src/cdogs/grafx.c index 1a5246a16..1cd0b19ec 100644 --- a/src/cdogs/grafx.c +++ b/src/cdogs/grafx.c @@ -146,6 +146,7 @@ void GraphicsInit(GraphicsDevice *device, Config *c) AddGraphicsMode(device, 400, 300); AddGraphicsMode(device, 640, 480); GraphicsConfigSetFromConfig(&device->cachedConfig, c); + device->cachedConfig.RestartFlags = RESTART_ALL; } static void AddSupportedGraphicsModes(GraphicsDevice *device) @@ -198,36 +199,30 @@ void GraphicsInitialize(GraphicsDevice *g) const int w = g->cachedConfig.Res.x; const int h = g->cachedConfig.Res.y; - const bool initRenderer = - !!(g->cachedConfig.RestartFlags & RESTART_RESOLUTION); + const bool initWindow = + !!(g->cachedConfig.RestartFlags & RESTART_WINDOW); const bool initTextures = !!(g->cachedConfig.RestartFlags & - (RESTART_RESOLUTION | RESTART_SCALE_MODE)); + (RESTART_WINDOW | RESTART_SCALE_MODE)); const bool initBrightness = !!(g->cachedConfig.RestartFlags & - (RESTART_RESOLUTION | RESTART_SCALE_MODE | RESTART_BRIGHTNESS)); + (RESTART_WINDOW | RESTART_SCALE_MODE | RESTART_BRIGHTNESS)); - if (initRenderer) + if (initWindow) { - Uint32 windowFlags = SDL_WINDOW_RESIZABLE; - if (g->cachedConfig.Fullscreen) - { - windowFlags |= SDL_WINDOW_FULLSCREEN; - } + LOG(LM_GFX, LL_INFO, "graphics mode(%dx%d %dx%s)", + w, h, g->cachedConfig.ScaleFactor, + g->cachedConfig.Fullscreen ? " fullscreen" : ""); - LOG(LM_GFX, LL_INFO, "graphics mode(%dx%d %dx)", - w, h, g->cachedConfig.ScaleFactor); - // Get the previous window's dimensions and recreate it + Uint32 windowFlags = SDL_WINDOW_RESIZABLE; Rect2i windowDim = Rect2iNew( svec2i(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED), svec2i_scale(svec2i(w, h), (float)g->cachedConfig.ScaleFactor) ); - if (g->gameWindow.window) + if (g->cachedConfig.Fullscreen) { - SDL_GetWindowPosition( - g->gameWindow.window, &windowDim.Pos.x, &windowDim.Pos.y); - SDL_GetWindowSize( - g->gameWindow.window, &windowDim.Size.x, &windowDim.Size.y); + windowFlags |= SDL_WINDOW_FULLSCREEN; + windowDim.Size = svec2i(w, h); } LOG(LM_GFX, LL_DEBUG, "destroying previous renderer"); WindowContextDestroy(&g->gameWindow); @@ -256,19 +251,21 @@ void GraphicsInitialize(GraphicsDevice *g) } g->Format = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888); - - GraphicsSetBlitClip( - g, 0, 0, g->cachedConfig.Res.x - 1, g->cachedConfig.Res.y - 1); } if (initTextures) { - if (!initRenderer) + if (!initWindow) { WindowContextDestroyTextures(&g->gameWindow); WindowContextDestroyTextures(&g->secondWindow); + WindowContextInitTextures(&g->gameWindow, svec2i(w, h)); + WindowContextInitTextures(&g->secondWindow, svec2i(w, h)); } + GraphicsSetBlitClip( + g, 0, 0, g->cachedConfig.Res.x - 1, g->cachedConfig.Res.y - 1); + // Set render scale mode const char *renderScaleQuality = "nearest"; switch ((ScaleMode)ConfigGetEnum(&gConfig, "Graphics.ScaleMode")) @@ -358,7 +355,7 @@ void GraphicsInitialize(GraphicsDevice *g) if (initBrightness) { - if (!initRenderer && !initTextures) + if (!initWindow && !initTextures) { SDL_DestroyTexture(g->brightnessOverlay); } @@ -416,7 +413,7 @@ void GraphicsConfigSet( if (!svec2i_is_equal(res, c->Res)) { c->Res = res; - c->RestartFlags |= RESTART_RESOLUTION; + c->RestartFlags |= RESTART_SCALE_MODE; } #define SET(_lhs, _rhs, _flag) \ if ((_lhs) != (_rhs)) \ @@ -424,11 +421,11 @@ void GraphicsConfigSet( (_lhs) = (_rhs); \ c->RestartFlags |= (_flag); \ } - SET(c->Fullscreen, fullscreen, RESTART_RESOLUTION); - SET(c->ScaleFactor, scaleFactor, RESTART_RESOLUTION); + SET(c->Fullscreen, fullscreen, RESTART_WINDOW); + SET(c->ScaleFactor, scaleFactor, RESTART_SCALE_MODE); SET(c->ScaleMode, scaleMode, RESTART_SCALE_MODE); SET(c->Brightness, brightness, RESTART_BRIGHTNESS); - SET(c->SecondWindow, secondWindow, RESTART_RESOLUTION); + SET(c->SecondWindow, secondWindow, RESTART_WINDOW); } void GraphicsConfigSetFromConfig(GraphicsConfig *gc, Config *c) diff --git a/src/cdogs/grafx.h b/src/cdogs/grafx.h index c40542916..77f693958 100644 --- a/src/cdogs/grafx.h +++ b/src/cdogs/grafx.h @@ -59,7 +59,7 @@ #include "window_context.h" -#define RESTART_RESOLUTION 1 +#define RESTART_WINDOW 1 #define RESTART_SCALE_MODE 2 #define RESTART_BRIGHTNESS 4 #define RESTART_ALL -1 diff --git a/src/cdogs/window_context.c b/src/cdogs/window_context.c index 2392428fc..cd78145c7 100644 --- a/src/cdogs/window_context.c +++ b/src/cdogs/window_context.c @@ -34,9 +34,6 @@ bool WindowContextCreate( const char *title, SDL_Surface *icon, const struct vec2i rendererLogicalSize) { - CArrayInit(&wc->texturesBkg, sizeof(SDL_Texture *)); - CArrayInit(&wc->textures, sizeof(SDL_Texture *)); - LOG(LM_GFX, LL_DEBUG, "creating window (%d, %d) %dx%d flags(%X)", windowDim.Pos.x, windowDim.Pos.y, windowDim.Size.x, windowDim.Size.y, windowFlags); @@ -58,8 +55,20 @@ bool WindowContextCreate( LOG(LM_GFX, LL_DEBUG, "setting icon"); SDL_SetWindowIcon(wc->window, icon); + if (!WindowContextInitTextures(wc, rendererLogicalSize)) + { + return false; + } + return true; +} +bool WindowContextInitTextures( + WindowContext *wc, const struct vec2i rendererLogicalSize) +{ + CArrayInit(&wc->texturesBkg, sizeof(SDL_Texture *)); + CArrayInit(&wc->textures, sizeof(SDL_Texture *)); + if (SDL_RenderSetLogicalSize( - wc->renderer, rendererLogicalSize.x, rendererLogicalSize.y) != 0) + wc->renderer, rendererLogicalSize.x, rendererLogicalSize.y) != 0) { LOG(LM_GFX, LL_ERROR, "cannot set renderer logical size: %s", SDL_GetError()); diff --git a/src/cdogs/window_context.h b/src/cdogs/window_context.h index 34bc18850..c4f3a8a90 100644 --- a/src/cdogs/window_context.h +++ b/src/cdogs/window_context.h @@ -43,6 +43,8 @@ bool WindowContextCreate( WindowContext *wc, const Rect2i windowDim, const int windowFlags, const char *title, SDL_Surface *icon, const struct vec2i rendererLogicalSize); +bool WindowContextInitTextures( + WindowContext *wc, const struct vec2i rendererLogicalSize); void WindowContextDestroy(WindowContext *wc); void WindowContextDestroyTextures(WindowContext *wc);