From 4c9c63894d00567249a726ebd93379926fd2c8e7 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 14 Jan 2019 13:47:50 +0100 Subject: [PATCH] Allow to resize the SDL window and propagate changes to the scene. --- doc/tutorial/02_cube.cpp | 2 +- src/platform/sdl/sdlapp.cpp | 4 ++-- src/platform/sdl/sdlinput.cpp | 5 ++++- src/scene.cpp | 41 +++++++++++++++++++++++++++-------- src/scene.h | 4 ++++ src/video.cpp | 5 +++++ src/video.h | 15 ++++++++----- 7 files changed, 57 insertions(+), 19 deletions(-) diff --git a/doc/tutorial/02_cube.cpp b/doc/tutorial/02_cube.cpp index 2820863f..1717d641 100644 --- a/doc/tutorial/02_cube.cpp +++ b/doc/tutorial/02_cube.cpp @@ -45,7 +45,7 @@ public: m_ready(false) { m_camera = new Camera(); - m_camera->SetProjection(mat4::perspective(radians(30.f), 960.f, 600.f, .1f, 1000.f)); + m_camera->SetProjection(mat4::perspective(radians(30.f), 640.f, 480.f, .1f, 1000.f)); m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f), vec3(0.f, -1.f, 0.f), vec3(0.f, 1.f, 0.f))); diff --git a/src/platform/sdl/sdlapp.cpp b/src/platform/sdl/sdlapp.cpp index feeb0727..94fe34cc 100644 --- a/src/platform/sdl/sdlapp.cpp +++ b/src/platform/sdl/sdlapp.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -69,7 +69,7 @@ SdlAppDisplay::SdlAppDisplay(char const *title, ivec2 res) * However, perfs warning have been may occur. */ SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); - int flags = SDL_WINDOW_OPENGL; + int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; if (window_size == ivec2(0)) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; data->m_window = SDL_CreateWindow(title, diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index 8cc8f524..d2efa1e6 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -332,6 +332,9 @@ void SdlInputData::Tick(float seconds) case SDL_WINDOWEVENT_FOCUS_LOST: m_mouse->SetKey(3, false); break; + case SDL_WINDOWEVENT_RESIZED: + Video::Resize(ivec2(event.window.data1, event.window.data2)); + break; } break; } diff --git a/src/scene.cpp b/src/scene.cpp index a484bf77..20599152 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -204,14 +204,15 @@ mutex SceneData::m_prim_mutex; * Public Scene class */ Scene::Scene(ivec2 size) - : data(new SceneData()) -{ - m_renderer = new Renderer(size); - - data->m_renderbuffer[0] = new Framebuffer(size); - data->m_renderbuffer[1] = new Framebuffer(size); - data->m_renderbuffer[2] = new Framebuffer(size); - data->m_renderbuffer[3] = new Framebuffer(size); + : m_size(size), + m_wanted_size(size), + data(new SceneData()), + m_renderer(new Renderer(size)) +{ + data->m_renderbuffer[0] = new Framebuffer(m_size); + data->m_renderbuffer[1] = new Framebuffer(m_size); + data->m_renderbuffer[2] = new Framebuffer(m_size); + data->m_renderbuffer[3] = new Framebuffer(m_size); data->m_pp.m_shader[0] = Shader::Create(LOLFX_RESOURCE_NAME(gpu_blit)); data->m_pp.m_shader[1] = Shader::Create(LOLFX_RESOURCE_NAME(gpu_postprocess)); data->m_pp.m_coord[0] = data->m_pp.m_shader[0]->GetAttribLocation(VertexUsage::Position, 0); @@ -232,7 +233,7 @@ Scene::Scene(ivec2 size) /* Create a default orthographic camera, in case the user doesn’t. */ data->m_default_cam = new Camera(); - mat4 proj = mat4::ortho(0.f, (float)size.x, 0.f, (float)size.y, -1000.f, 1000.f); + mat4 proj = mat4::ortho(0.f, (float)m_size.x, 0.f, (float)m_size.y, -1000.f, 1000.f); data->m_default_cam->SetProjection(proj); PushCamera(data->m_default_cam); @@ -248,6 +249,11 @@ Scene::Scene(ivec2 size) data->m_line_api.m_debug_mask = 1; } +void Scene::resize(ivec2 size) +{ + m_wanted_size = size; +} + //----------------------------------------------------------------------------- Scene::~Scene() { @@ -591,6 +597,23 @@ void Scene::pre_render(float) { gpu_marker("Pre Render"); + // Handle resize event + if (m_size != m_wanted_size) + { + m_size = m_wanted_size; + delete data->m_renderbuffer[0]; + delete data->m_renderbuffer[1]; + delete data->m_renderbuffer[2]; + delete data->m_renderbuffer[3]; + data->m_renderbuffer[0] = new Framebuffer(m_size); + data->m_renderbuffer[1] = new Framebuffer(m_size); + data->m_renderbuffer[2] = new Framebuffer(m_size); + data->m_renderbuffer[3] = new Framebuffer(m_size); + + mat4 proj = mat4::ortho(0.f, (float)m_size.x, 0.f, (float)m_size.y, -1000.f, 1000.f); + data->m_default_cam->SetProjection(proj); + } + /* First render into the offline buffer */ if (do_pp) { diff --git a/src/scene.h b/src/scene.h index f7b7b561..fb140c9c 100644 --- a/src/scene.h +++ b/src/scene.h @@ -250,6 +250,8 @@ public: void EnableDisplay(); void DisableDisplay(); + void resize(ivec2 size); + void pre_render(float seconds); void render(float seconds); void post_render(float seconds); @@ -259,6 +261,8 @@ private: void render_tiles(); void render_lines(float seconds); + ivec2 m_size, m_wanted_size; + SceneData *data; Renderer *m_renderer; }; diff --git a/src/video.cpp b/src/video.cpp index f6dd9bcd..4961baef 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -120,6 +120,11 @@ void Video::Capture(uint32_t *buffer) #endif } +void Video::Resize(ivec2 size) +{ + Scene::GetScene(0).resize(size); +} + ivec2 Video::GetSize() { return Scene::GetScene(0).get_renderer()->GetViewport().extent(); diff --git a/src/video.h b/src/video.h index 53ecce53..4d98efe7 100644 --- a/src/video.h +++ b/src/video.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2019 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -49,6 +51,7 @@ public: static void Setup(ivec2 size); static void Destroy(); + static void Resize(ivec2 size); static ivec2 GetSize(); static void SetDebugRenderMode(DebugRenderMode d);