From 5714a60108f4de743a3b81d04f55c0899d829e71 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 15 Apr 2024 02:42:07 -0700 Subject: [PATCH] gfx: split text style entity components into a distinct module Signed-off-by: Stephen Gutekanst --- examples/text/Game.zig | 31 ++++++++++++++++--------------- examples/text/main.zig | 1 + src/gfx/Text.zig | 35 +++++------------------------------ src/gfx/TextStyle.zig | 37 +++++++++++++++++++++++++++++++++++++ src/gfx/main.zig | 1 + 5 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 src/gfx/TextStyle.zig diff --git a/examples/text/Game.zig b/examples/text/Game.zig index 14d16329..5dac36f8 100644 --- a/examples/text/Game.zig +++ b/examples/text/Game.zig @@ -70,6 +70,7 @@ const text2: []const []const u8 = &.{"!$?😊"}; fn init( engine: *mach.Engine.Mod, text_mod: *Text.Mod, + text_style: *gfx.TextStyle.Mod, game: *Mod, ) !void { // The Mach .core is where we set window options, etc. @@ -77,25 +78,25 @@ fn init( // TODO: a better way to initialize entities with default values const style1 = try engine.newEntity(); - try text_mod.set(style1, .font_name, "Roboto Medium"); // TODO - try text_mod.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt - try text_mod.set(style1, .font_weight, gfx.font_weight_normal); - try text_mod.set(style1, .italic, false); - try text_mod.set(style1, .color, vec4(0.6, 1.0, 0.6, 1.0)); + try text_style.set(style1, .font_name, "Roboto Medium"); // TODO + try text_style.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt + try text_style.set(style1, .font_weight, gfx.font_weight_normal); + try text_style.set(style1, .italic, false); + try text_style.set(style1, .color, vec4(0.6, 1.0, 0.6, 1.0)); const style2 = try engine.newEntity(); - try text_mod.set(style2, .font_name, "Roboto Medium"); // TODO - try text_mod.set(style2, .font_size, 48 * gfx.px_per_pt); // 48pt - try text_mod.set(style2, .font_weight, gfx.font_weight_normal); - try text_mod.set(style2, .italic, true); - try text_mod.set(style2, .color, vec4(0.6, 1.0, 0.6, 1.0)); + try text_style.set(style2, .font_name, "Roboto Medium"); // TODO + try text_style.set(style2, .font_size, 48 * gfx.px_per_pt); // 48pt + try text_style.set(style2, .font_weight, gfx.font_weight_normal); + try text_style.set(style2, .italic, true); + try text_style.set(style2, .color, vec4(0.6, 1.0, 0.6, 1.0)); const style3 = try engine.newEntity(); - try text_mod.set(style3, .font_name, "Roboto Medium"); // TODO - try text_mod.set(style3, .font_size, 48 * gfx.px_per_pt); // 48pt - try text_mod.set(style3, .font_weight, gfx.font_weight_bold); - try text_mod.set(style3, .italic, false); - try text_mod.set(style3, .color, vec4(0.6, 1.0, 0.6, 1.0)); + try text_style.set(style3, .font_name, "Roboto Medium"); // TODO + try text_style.set(style3, .font_size, 48 * gfx.px_per_pt); // 48pt + try text_style.set(style3, .font_weight, gfx.font_weight_bold); + try text_style.set(style3, .italic, false); + try text_style.set(style3, .color, vec4(0.6, 1.0, 0.6, 1.0)); // Create some text const player = try engine.newEntity(); diff --git a/examples/text/main.zig b/examples/text/main.zig index 8c1f912e..145f0500 100644 --- a/examples/text/main.zig +++ b/examples/text/main.zig @@ -10,6 +10,7 @@ const Game = @import("Game.zig"); pub const modules = .{ mach.Engine, mach.gfx.Text, + mach.gfx.TextStyle, Game, }; diff --git a/src/gfx/Text.zig b/src/gfx/Text.zig index 529f806c..abc994ae 100644 --- a/src/gfx/Text.zig +++ b/src/gfx/Text.zig @@ -53,31 +53,6 @@ pub const components = .{ \\ \\ Expected to match the length of the text component. }, - - // TODO: ship a default font - .font_name = .{ .type = []const u8, .description = - \\ Style component: desired font to render text with. - }, - - // e.g. 12 * mach.gfx.px_per_pt // 12pt - .font_size = .{ .type = f32, .description = - \\ Style component: font size in pixels - }, - - // e.g. mach.gfx.font_weight_normal - .font_weight = .{ .type = u16, .description = - \\ Style component: font weight - }, - - // e.g. false - .italic = .{ .type = bool, .description = - \\ Style component: italic text - }, - - // e.g. vec4(0, 0, 0, 1.0) - .color = .{ .type = Vec4, .description = - \\ Style component: fill color - }, }; pub const global_events = .{ @@ -420,16 +395,16 @@ fn updated( for (segments, styles) |segment, style| { // Load a font - const font_name = engine.entities.getComponent(style, .mach_gfx_text, .font_name).?; + const font_name = engine.entities.getComponent(style, .mach_gfx_text_style, .font_name).?; _ = font_name; // TODO: actually use font name const font_bytes = @import("font-assets").fira_sans_regular_ttf; var font = try gfx.Font.initBytes(font_bytes); defer font.deinit(text_mod.state().allocator); - const font_size = engine.entities.getComponent(style, .mach_gfx_text, .font_size).?; - const font_weight = engine.entities.getComponent(style, .mach_gfx_text, .font_weight); - const italic = engine.entities.getComponent(style, .mach_gfx_text, .italic); - const color = engine.entities.getComponent(style, .mach_gfx_text, .color); + const font_size = engine.entities.getComponent(style, .mach_gfx_text_style, .font_size).?; + const font_weight = engine.entities.getComponent(style, .mach_gfx_text_style, .font_weight); + const italic = engine.entities.getComponent(style, .mach_gfx_text_style, .italic); + const color = engine.entities.getComponent(style, .mach_gfx_text_style, .color); // TODO: actually apply these _ = font_weight; _ = italic; diff --git a/src/gfx/TextStyle.zig b/src/gfx/TextStyle.zig new file mode 100644 index 00000000..d771ec1d --- /dev/null +++ b/src/gfx/TextStyle.zig @@ -0,0 +1,37 @@ +const mach = @import("../main.zig"); +const math = mach.math; + +pub const name = .mach_gfx_text_style; +pub const Mod = mach.Mod(@This()); + +pub const components = .{ + // TODO: ship a default font + .font_name = .{ .type = []const u8, .description = + \\ Desired font to render text with. + \\ TODO(text): this is not currently implemented + }, + + // e.g. 12 * mach.gfx.px_per_pt // 12pt + .font_size = .{ .type = f32, .description = + \\ Font size in pixels + \\ TODO(text): this is not currently implemented + }, + + // e.g. mach.gfx.font_weight_normal + .font_weight = .{ .type = u16, .description = + \\ Font weight + \\ TODO(text): this is not currently implemented + }, + + // e.g. false + .italic = .{ .type = bool, .description = + \\ Italic text + \\ TODO(text): this is not currently implemented + }, + + // e.g. vec4(0, 0, 0, 1.0) + .color = .{ .type = math.Vec4, .description = + \\ Fill color + \\ TODO(text): this is not currently implemented + }, +}; diff --git a/src/gfx/main.zig b/src/gfx/main.zig index 49105d09..5b52b51b 100644 --- a/src/gfx/main.zig +++ b/src/gfx/main.zig @@ -4,6 +4,7 @@ pub const Atlas = @import("atlas/Atlas.zig"); // ECS modules pub const Sprite = @import("Sprite.zig"); pub const Text = @import("Text.zig"); +pub const TextStyle = @import("TextStyle.zig"); // Fonts pub const Font = @import("font/main.zig").Font;