[client] allow window resize event's before startup

This commit is contained in:
Geoffrey McRae 2018-07-19 23:32:42 +10:00
parent 9f8c20c3e7
commit eb6ee8ea46
3 changed files with 41 additions and 31 deletions

View File

@ -85,6 +85,7 @@ LG_RendererFormat;
typedef struct LG_RendererRect
{
bool valid;
int x;
int y;
unsigned int w;

View File

@ -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();

View File

@ -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;
}