Resizing of SDL2 windows

This commit is contained in:
Jeffrey Pfau 2014-07-09 00:03:05 -07:00
parent 548793e551
commit ecee71cfa1
3 changed files with 22 additions and 5 deletions

View File

@ -74,6 +74,7 @@ int main(int argc, char** argv) {
renderer.viewportHeight = graphicsOpts.height;
#if SDL_VERSION_ATLEAST(2, 0, 0)
renderer.events.fullscreen = graphicsOpts.fullscreen;
renderer.events.windowUpdated = 0;
#endif
if (!_GBASDLInit(&renderer)) {
@ -138,7 +139,7 @@ static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
#endif
#if SDL_VERSION_ATLEAST(2, 0, 0)
renderer->window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->events.fullscreen));
renderer->window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->events.fullscreen));
SDL_GL_CreateContext(renderer->window);
SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
renderer->events.window = renderer->window;
@ -199,15 +200,13 @@ static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer*
#endif
while (SDL_PollEvent(&event)) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
int fullscreen = renderer->events.fullscreen;
#endif
GBASDLHandleEvent(context, &renderer->events, &event);
#if SDL_VERSION_ATLEAST(2, 0, 0)
// Event handling can change the size of the screen
if (renderer->events.fullscreen != fullscreen) {
if (renderer->events.windowUpdated) {
SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
renderer->events.windowUpdated = 0;
}
#endif
}

View File

@ -107,6 +107,7 @@ static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents
case SDLK_f:
SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
sdlContext->fullscreen = !sdlContext->fullscreen;
sdlContext->windowUpdated = 1;
break;
#endif
case SDLK_p:
@ -205,6 +206,17 @@ static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyH
context->activeKeys |= key;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
(void) (context);
switch (event->event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
sdlContext->windowUpdated = 1;
break;
}
}
#endif
void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
switch (event->type) {
case SDL_QUIT:
@ -217,6 +229,11 @@ void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContex
ConditionWake(&context->stateCond);
MutexUnlock(&context->stateMutex);
break;
#if SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_WINDOWEVENT:
_GBASDLHandleWindowEvent(context, sdlContext, &event->window);
break;
#endif
case SDL_KEYDOWN:
case SDL_KEYUP:
_GBASDLHandleKeypress(context, sdlContext, &event->key);

View File

@ -12,6 +12,7 @@ struct GBASDLEvents {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_Window* window;
int fullscreen;
int windowUpdated;
#endif
};