pull some calculations into a new UiGeometry class

This commit is contained in:
Colin McMillen 2023-07-07 14:24:43 -04:00
parent 6670b358c0
commit 6812699401

View File

@ -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 class Game : GameWindow {
public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { } 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 int thumbnailWidth = (int) 1.0 * thumbnailHeight * activeCamera.Resolution.X / activeCamera.Resolution.Y;
static Texture TEXTURE_WHITE; static Texture TEXTURE_WHITE;
int windowWidth; UiGeometry geometry = new UiGeometry();
int windowHeight;
float[] vertices = { // four points of (x, y, z, tex_x, tex_y)
// Position Texture coordinates float[] vertices = new float[20];
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
};
uint[] indices = { uint[] indices = {
0, 1, 3, // first triangle 0, 1, 3, // first triangle
@ -208,7 +215,7 @@ public class Game : GameWindow {
int VertexBufferObject; int VertexBufferObject;
int ElementBufferObject; int ElementBufferObject;
int VertexArrayObject; int VertexArrayObject;
List<Texture> textures; List<Texture> textures = new List<Texture>();
int textureIndex = 0; int textureIndex = 0;
Shader shader; Shader shader;
Matrix4 projection; Matrix4 projection;
@ -275,7 +282,6 @@ public class Game : GameWindow {
// Load textures from JPEGs. // Load textures from JPEGs.
string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\06\27\"); string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\06\27\");
textures = new List<Texture>();
foreach (string file in files) { foreach (string file in files) {
if (file.ToLower().EndsWith(".jpg")) { if (file.ToLower().EndsWith(".jpg")) {
Image<Rgba32> image = Image.Load<Rgba32>(file); Image<Rgba32> image = Image.Load<Rgba32>(file);
@ -298,17 +304,16 @@ public class Game : GameWindow {
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
int maxPhotoWidth = windowWidth - thumbnailWidth; int maxPhotoWidth = geometry.WindowSize.X - thumbnailWidth;
Texture active = textures[textureIndex]; Texture active = textures[textureIndex];
// TODO: handle the case where we need to letterbox vertically instead. // 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 * geometry.WindowSize.Y / active.Height * active.Width);
int photoWidth = (int) (1.0 * windowHeight / active.Height * active.Width);
int letterboxWidth = (maxPhotoWidth - photoWidth) / 2; 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++) { 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); DrawTexture(textures[i], box);
if (i == textureIndex) { if (i == textureIndex) {
DrawBox(box, 5, Color4.Black); DrawBox(box, 5, Color4.Black);
@ -341,12 +346,12 @@ public class Game : GameWindow {
protected override void OnResize(ResizeEventArgs e) { protected override void OnResize(ResizeEventArgs e) {
base.OnResize(e); base.OnResize(e);
Console.WriteLine($"OnResize: {e.Width}x{e.Height}"); 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.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) { private void SetVertices(float left, float top, float width, float height) {
@ -402,7 +407,7 @@ static class Program {
nwSettings.CurrentMonitor = bestMonitor.Handle; nwSettings.CurrentMonitor = bestMonitor.Handle;
nwSettings.Location = new Vector2i(bestMonitor.WorkArea.Min.X + 1, bestMonitor.WorkArea.Min.Y + 31); 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.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"; nwSettings.Title = "Totte";
// FIXME: nwSettings.Icon = ... // FIXME: nwSettings.Icon = ...