From eb6ee8ea46eb200e8b9ac68c8414014abebdc3ef Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 19 Jul 2018 23:32:42 +1000 Subject: [PATCH] [client] allow window resize event's before startup --- client/lg-renderer.h | 1 + client/main.c | 51 +++++++++++++++++++++------------------ client/renderers/opengl.c | 20 +++++++++------ 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/client/lg-renderer.h b/client/lg-renderer.h index 8605f308..827336a1 100644 --- a/client/lg-renderer.h +++ b/client/lg-renderer.h @@ -85,6 +85,7 @@ LG_RendererFormat; typedef struct LG_RendererRect { + bool valid; int x; int y; unsigned int w; diff --git a/client/main.c b/client/main.c index 310b7611..6751b875 100644 --- a/client/main.c +++ b/client/main.c @@ -53,6 +53,7 @@ struct AppState TTF_Font * font; TTF_Font * alertFont; + bool haveSrcSize; SDL_Point srcSize; LG_RendererRect dstRect; SDL_Point cursor; @@ -141,41 +142,42 @@ struct AppParams params = static inline void updatePositionInfo() { - if (!state.started) - return; - int w, h; SDL_GetWindowSize(state.window, &w, &h); - if (params.keepAspect) + if (state.haveSrcSize) { - const float srcAspect = (float)state.srcSize.y / (float)state.srcSize.x; - const float wndAspect = (float)h / (float)w; - if (wndAspect < srcAspect) + if (params.keepAspect) { - state.dstRect.w = (float)h / srcAspect; - state.dstRect.h = h; - state.dstRect.x = (w >> 1) - (state.dstRect.w >> 1); - state.dstRect.y = 0; + const float srcAspect = (float)state.srcSize.y / (float)state.srcSize.x; + const float wndAspect = (float)h / (float)w; + if (wndAspect < srcAspect) + { + state.dstRect.w = (float)h / srcAspect; + state.dstRect.h = h; + state.dstRect.x = (w >> 1) - (state.dstRect.w >> 1); + state.dstRect.y = 0; + } + else + { + state.dstRect.w = w; + state.dstRect.h = (float)w * srcAspect; + state.dstRect.x = 0; + state.dstRect.y = (h >> 1) - (state.dstRect.h >> 1); + } } else { - state.dstRect.w = w; - state.dstRect.h = (float)w * srcAspect; state.dstRect.x = 0; - state.dstRect.y = (h >> 1) - (state.dstRect.h >> 1); + state.dstRect.y = 0; + state.dstRect.w = w; + state.dstRect.h = h; } - } - else - { - state.dstRect.x = 0; - state.dstRect.y = 0; - state.dstRect.w = w; - state.dstRect.h = h; - } + state.dstRect.valid = true; - state.scaleX = (float)state.srcSize.y / (float)state.dstRect.h; - state.scaleY = (float)state.srcSize.x / (float)state.dstRect.w; + state.scaleX = (float)state.srcSize.y / (float)state.dstRect.h; + state.scaleY = (float)state.srcSize.x / (float)state.dstRect.w; + } if (state.lgr) state.lgr->on_resize(state.lgrData, w, h, state.dstRect); @@ -379,6 +381,7 @@ int frameThread(void * unused) { state.srcSize.x = header.width; state.srcSize.y = header.height; + state.haveSrcSize = true; if (params.autoResize) SDL_SetWindowSize(state.window, header.width, header.height); updatePositionInfo(); diff --git a/client/renderers/opengl.c b/client/renderers/opengl.c index 6f23dd97..3cb1f0aa 100644 --- a/client/renderers/opengl.c +++ b/client/renderers/opengl.c @@ -221,7 +221,9 @@ void opengl_on_resize(void * opaque, const int width, const int height, const LG this->window.x = width; this->window.y = height; - memcpy(&this->destRect, &destRect, sizeof(LG_RendererRect)); + + if (destRect.valid) + memcpy(&this->destRect, &destRect, sizeof(LG_RendererRect)); this->resizeWindow = true; } @@ -407,12 +409,16 @@ bool opengl_render(void * opaque, SDL_Window * window) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glTranslatef(this->destRect.x, this->destRect.y, 0.0f); - glScalef( - (float)this->destRect.w / (float)this->format.width, - (float)this->destRect.h / (float)this->format.height, - 1.0f - ); + + if (this->destRect.valid) + { + glTranslatef(this->destRect.x, this->destRect.y, 0.0f); + glScalef( + (float)this->destRect.w / (float)this->format.width, + (float)this->destRect.h / (float)this->format.height, + 1.0f + ); + } this->resizeWindow = false; }