From 4cd63997ce535d77f4c2c1106d2957d48a6d161e Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Tue, 20 Apr 2021 12:57:59 -0400 Subject: [PATCH] Specifying -1 in your window positions will make SDL center the window --- example/src/example.cpp | 2 +- platforms/sdl/main.cpp.in | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/example/src/example.cpp b/example/src/example.cpp index 2950f11..58bf690 100644 --- a/example/src/example.cpp +++ b/example/src/example.cpp @@ -9,7 +9,7 @@ void app_main(prism::engine* engine) { file::set_domain_path(file::Domain::App, "data"); file::set_domain_path(file::Domain::Internal, "{resource_dir}/shaders"); - platform::open_window("Example", {0, 0, 1280, 720}, WindowFlags::Resizable); + platform::open_window("Example", {-1, -1, 1280, 720}, WindowFlags::Resizable); } void ExampleApp::initialize_render() { diff --git a/platforms/sdl/main.cpp.in b/platforms/sdl/main.cpp.in index 47e5dff..3cd59af 100644 --- a/platforms/sdl/main.cpp.in +++ b/platforms/sdl/main.cpp.in @@ -89,7 +89,14 @@ int platform::open_window(const std::string_view title, const prism::Rectangle r if(flags == WindowFlags::Resizable) sdl_flags |= SDL_WINDOW_RESIZABLE; - win.window = SDL_CreateWindow(title.data(), rect.offset.x, rect.offset.y, rect.extent.width, rect.extent.height, sdl_flags); + int real_x = rect.offset.x; + int real_y = rect.offset.y; + if(rect.offset.x == -1 && rect.offset.y == -1) { + real_x = SDL_WINDOWPOS_CENTERED; + real_y = SDL_WINDOWPOS_CENTERED; + } + + win.window = SDL_CreateWindow(title.data(), real_x, real_y, rect.extent.width, rect.extent.height, sdl_flags); engine->add_window((void*)&win, win.identifier, rect.extent); app->initialize_render(); @@ -281,6 +288,8 @@ int main(int argc, char* argv[]) { auto window = get_window_by_sdl_id(event.window.windowID); if(window != nullptr) engine->resize(window->identifier, {static_cast(event.window.data1), static_cast(event.window.data2)}); + } else if(event.window.event == SDL_WINDOWEVENT_CLOSE) { + engine->quit(); } break; }