From 959d385342020499970195dae79d845ac747d1cd Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 7 Jun 2023 07:36:37 +0200 Subject: [PATCH] Fixed issue that fonts weren't rescaled going fullscreen. --- src/context.zig | 11 ----------- src/main.zig | 5 ++--- src/renderer.zig | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/context.zig b/src/context.zig index caaf459..5055488 100644 --- a/src/context.zig +++ b/src/context.zig @@ -1,7 +1,6 @@ const std = @import("std"); const allegro = @import("allegro"); const engine = @import("engine.zig"); -const paths = @import("paths.zig"); const Palette = @import("palette.zig").Palette; const Scene = @import("scene.zig").Scene; const Renderer = @import("renderer.zig").Renderer; @@ -80,16 +79,6 @@ pub const Context = struct { self.events.destroy(); } - pub fn registerFonts(self: *Context) !void { - const viewport = self.renderer.viewport; - const fonts = &self.renderer.fonts; - try fonts.addFromFileTTF("default", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(32)); - try fonts.addFromFileTTF("small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(16)); - try fonts.addFromFileTTF("extra-small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(12)); - try fonts.addFromFileTTF("large", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(64)); - try fonts.addFromFileTTF("debug", paths.AssetsDir ++ "/fonts/Cabin-Regular.ttf", viewport.scaledInteger(16)); - } - pub fn quit(self: *Context) void { self.shouldQuit = true; } diff --git a/src/main.zig b/src/main.zig index 7f684e0..9bfe1d1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -61,7 +61,7 @@ pub fn main() !void { allegro.convertMemoryBitmaps(); - try context.registerFonts(); + try renderer.registerFonts(); try context.switchToScene(TitleScene, null); @@ -85,9 +85,8 @@ pub fn main() !void { switch (event.type) { allegro.c.ALLEGRO_EVENT_DISPLAY_CLOSE => context.quit(), allegro.c.ALLEGRO_EVENT_DISPLAY_RESIZE => { - context.renderer.resized(event.display.width, event.display.height); - try context.registerFonts(); _ = allegro.acknowledgeResize(allegro.Display{ .native = event.display.source.? }); + context.renderer.resized(); }, allegro.c.ALLEGRO_EVENT_KEY_CHAR => { switch (event.keyboard.keycode) { diff --git a/src/renderer.zig b/src/renderer.zig index 9d9c8ed..4ebe783 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -1,6 +1,7 @@ const std = @import("std"); const allegro = @import("allegro"); const engine = @import("engine.zig"); +const paths = @import("paths.zig"); const Context = @import("context.zig").Context; pub const Renderer = struct { @@ -109,8 +110,19 @@ pub const Renderer = struct { self.printText(font, color, screen.x, screen.y, alignment, fmt, args); } - pub fn resized(self: *Renderer, width: i32, height: i32) void { - self.viewport.update(width, height); + pub fn registerFonts(self: *Renderer) !void { + const viewport = self.viewport; + const fonts = &self.fonts; + try fonts.addFromFileTTF("default", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(32)); + try fonts.addFromFileTTF("small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(16)); + try fonts.addFromFileTTF("extra-small", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(12)); + try fonts.addFromFileTTF("large", paths.AssetsDir ++ "/fonts/Pixellari.ttf", viewport.scaledInteger(64)); + try fonts.addFromFileTTF("debug", paths.AssetsDir ++ "/fonts/Cabin-Regular.ttf", viewport.scaledInteger(16)); + } + + pub fn resized(self: *Renderer) void { + self.viewport.update(self.display.width(), self.display.height()); + self.registerFonts() catch {}; } pub fn takeScreenshot(self: Renderer) !void { @@ -121,6 +133,6 @@ pub const Renderer = struct { pub fn toggleFullScreen(self: *Renderer) void { var displayFlags = allegro.getDisplayFlags(self.display); _ = allegro.setDisplayFlag(self.display, allegro.NewDisplayFlags{ .FULLSCREEN_WINDOW = true }, !displayFlags.FULLSCREEN_WINDOW); - self.viewport.update(self.display.width(), self.display.height()); + self.resized(); } };