From 6812699401bc0ad05d72d8af86090c54ec577cfe Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 7 Jul 2023 14:24:43 -0400 Subject: [PATCH] pull some calculations into a new UiGeometry class --- Program.cs | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/Program.cs b/Program.cs index fd63468..24cf759 100644 --- a/Program.cs +++ b/Program.cs @@ -182,6 +182,18 @@ public class Texture : IDisposable { } } +public class UiGeometry { + public static Vector2i MIN_WINDOW_SIZE = new Vector2i(640, 480); + + public readonly Vector2i WindowSize; + + public UiGeometry() : this(MIN_WINDOW_SIZE) {} + + public UiGeometry(Vector2i windowSize) { + WindowSize = windowSize; + } +} + public class Game : GameWindow { public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { } @@ -190,15 +202,10 @@ public class Game : GameWindow { static int thumbnailWidth = (int) 1.0 * thumbnailHeight * activeCamera.Resolution.X / activeCamera.Resolution.Y; static Texture TEXTURE_WHITE; - int windowWidth; - int windowHeight; - float[] vertices = { - // Position Texture coordinates - 0f, 0f, 0.0f, 0.0f, 0.0f, // top left - 2560f, 0f, 0.0f, 1.0f, 0.0f, // top right - 2560f, 1440f, 0.0f, 1.0f, 1.0f, // bottom right - 0f, 1440f, 0.0f, 0.0f, 1.0f, // bottom left - }; + UiGeometry geometry = new UiGeometry(); + + // four points of (x, y, z, tex_x, tex_y) + float[] vertices = new float[20]; uint[] indices = { 0, 1, 3, // first triangle @@ -208,7 +215,7 @@ public class Game : GameWindow { int VertexBufferObject; int ElementBufferObject; int VertexArrayObject; - List textures; + List textures = new List(); int textureIndex = 0; Shader shader; Matrix4 projection; @@ -275,7 +282,6 @@ public class Game : GameWindow { // Load textures from JPEGs. string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\06\27\"); - textures = new List(); foreach (string file in files) { if (file.ToLower().EndsWith(".jpg")) { Image image = Image.Load(file); @@ -298,17 +304,16 @@ public class Game : GameWindow { GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.ActiveTexture(TextureUnit.Texture0); - int maxPhotoWidth = windowWidth - thumbnailWidth; + int maxPhotoWidth = geometry.WindowSize.X - thumbnailWidth; Texture active = textures[textureIndex]; // TODO: handle the case where we need to letterbox vertically instead. - // TODO: pull these geometry calculations out into an object. - int photoWidth = (int) (1.0 * windowHeight / active.Height * active.Width); + int photoWidth = (int) (1.0 * geometry.WindowSize.Y / active.Height * active.Width); int letterboxWidth = (maxPhotoWidth - photoWidth) / 2; - DrawTexture(active, makeBox(letterboxWidth, 0, photoWidth, windowHeight)); + DrawTexture(active, makeBox(letterboxWidth, 0, photoWidth, geometry.WindowSize.Y)); for (int i = 0; i < textures.Count; i++) { - Box2i box = makeBox(windowWidth - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight); + Box2i box = makeBox(geometry.WindowSize.X - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight); DrawTexture(textures[i], box); if (i == textureIndex) { DrawBox(box, 5, Color4.Black); @@ -341,12 +346,12 @@ public class Game : GameWindow { protected override void OnResize(ResizeEventArgs e) { base.OnResize(e); Console.WriteLine($"OnResize: {e.Width}x{e.Height}"); - windowWidth = e.Width; - windowHeight = e.Height; - projection = Matrix4.CreateOrthographicOffCenter(0f, windowWidth, windowHeight, 0f, -1f, 1f); + geometry = new UiGeometry(e.Size); + + projection = Matrix4.CreateOrthographicOffCenter(0f, e.Width, e.Height, 0f, -1f, 1f); GL.UniformMatrix4(shader.GetUniformLocation("projection"), true, ref projection); - GL.Viewport(0, 0, windowWidth, windowHeight); + GL.Viewport(0, 0, e.Width, e.Height); } private void SetVertices(float left, float top, float width, float height) { @@ -402,7 +407,7 @@ static class Program { nwSettings.CurrentMonitor = bestMonitor.Handle; nwSettings.Location = new Vector2i(bestMonitor.WorkArea.Min.X + 1, bestMonitor.WorkArea.Min.Y + 31); nwSettings.Size = new Vector2i(bestMonitor.WorkArea.Size.X - 2, bestMonitor.WorkArea.Size.Y - 32); - nwSettings.MinimumSize = new Vector2i(640, 480); + nwSettings.MinimumSize = UiGeometry.MIN_WINDOW_SIZE; nwSettings.Title = "Totte"; // FIXME: nwSettings.Icon = ...