gfx: split text style entity components into a distinct module

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-15 02:42:07 -07:00
parent 2cf68adcc7
commit 5714a60108
5 changed files with 60 additions and 45 deletions

View file

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

View file

@ -10,6 +10,7 @@ const Game = @import("Game.zig");
pub const modules = .{
mach.Engine,
mach.gfx.Text,
mach.gfx.TextStyle,
Game,
};

View file

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

37
src/gfx/TextStyle.zig Normal file
View file

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

View file

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