Fixed issue that fonts weren't rescaled going fullscreen.

This commit is contained in:
Sander Schobers 2023-06-07 07:36:37 +02:00
parent 529a55859c
commit 959d385342
3 changed files with 17 additions and 17 deletions

View File

@ -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;
}

View File

@ -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) {

View File

@ -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();
}
};