From 4b3478a9e9cc28c36e82fd8ce9aa76dcce02b414 Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Wed, 5 Oct 2022 19:11:40 +0200 Subject: [PATCH] made window responsive --- Program.cs | 2 +- src/Scene.cs | 11 ++++++++--- src/engine/Window.cs | 9 ++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Program.cs b/Program.cs index bde2e42..be2b45e 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,5 @@ var window = new Window("ASDLteroids", (int)(800 * Scene.SCALE), (int)(600 * Scene.SCALE)); var renderer = new Renderer(window); -var scene = new Scene(renderer); +var scene = new Scene(renderer, window); scene.Run(); diff --git a/src/Scene.cs b/src/Scene.cs index 763eeaf..95c697d 100644 --- a/src/Scene.cs +++ b/src/Scene.cs @@ -3,12 +3,13 @@ using static SDL2.SDL; class Scene { public const double SCALE = 2; - public const int SCREEN_WIDTH = (int)(800 * SCALE); - public const int SCREEN_HEIGHT = (int)(600 * SCALE); + public static int SCREEN_WIDTH = (int)(800 * SCALE); + public static int SCREEN_HEIGHT = (int)(600 * SCALE); private Ship ship; private UI ui; private Renderer renderer; private static Scene? instance; + private Window window; public Ufo ufo; public HashSet Shots; @@ -18,11 +19,12 @@ class Scene public AudioPlayer AudioPlayer; public bool running = true; - public Scene(Renderer renderer) + public Scene(Renderer renderer, Window window) { this.ship = new Ship(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); this.ufo = new Ufo(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); this.ui = new UI(); + this.window = window; this.Shots = new HashSet(); this.Asteroids = new HashSet(); this.renderer = renderer; @@ -50,6 +52,9 @@ class Scene DateTime start = DateTime.Now; while (true) { + var size = window.GetSize(); + SCREEN_WIDTH = size.width; + SCREEN_HEIGHT = size.height; start = DateTime.Now; pollEvents(); diff --git a/src/engine/Window.cs b/src/engine/Window.cs index 6131b6e..1f6c79c 100644 --- a/src/engine/Window.cs +++ b/src/engine/Window.cs @@ -6,7 +6,7 @@ class Window public Window(string title, int w, int h) { - window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WindowFlags.SDL_WINDOW_VULKAN); + window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WindowFlags.SDL_WINDOW_VULKAN | SDL_WindowFlags.SDL_WINDOW_RESIZABLE); } public Window(string title, int x, int y, int w, int h, SDL_WindowFlags flags) @@ -23,4 +23,11 @@ class Window { return this.window; } + + public (int width, int height) GetSize() + { + int w, h; + SDL_GetWindowSize(this.window, out w, out h); + return (w, h); + } }