gfx: split text style entity components into a distinct module
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
2cf68adcc7
commit
5714a60108
5 changed files with 60 additions and 45 deletions
|
|
@ -70,6 +70,7 @@ const text2: []const []const u8 = &.{"!$?😊"};
|
||||||
fn init(
|
fn init(
|
||||||
engine: *mach.Engine.Mod,
|
engine: *mach.Engine.Mod,
|
||||||
text_mod: *Text.Mod,
|
text_mod: *Text.Mod,
|
||||||
|
text_style: *gfx.TextStyle.Mod,
|
||||||
game: *Mod,
|
game: *Mod,
|
||||||
) !void {
|
) !void {
|
||||||
// The Mach .core is where we set window options, etc.
|
// 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
|
// TODO: a better way to initialize entities with default values
|
||||||
const style1 = try engine.newEntity();
|
const style1 = try engine.newEntity();
|
||||||
try text_mod.set(style1, .font_name, "Roboto Medium"); // TODO
|
try text_style.set(style1, .font_name, "Roboto Medium"); // TODO
|
||||||
try text_mod.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt
|
try text_style.set(style1, .font_size, 48 * gfx.px_per_pt); // 48pt
|
||||||
try text_mod.set(style1, .font_weight, gfx.font_weight_normal);
|
try text_style.set(style1, .font_weight, gfx.font_weight_normal);
|
||||||
try text_mod.set(style1, .italic, false);
|
try text_style.set(style1, .italic, false);
|
||||||
try text_mod.set(style1, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
try text_style.set(style1, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
||||||
|
|
||||||
const style2 = try engine.newEntity();
|
const style2 = try engine.newEntity();
|
||||||
try text_mod.set(style2, .font_name, "Roboto Medium"); // TODO
|
try text_style.set(style2, .font_name, "Roboto Medium"); // TODO
|
||||||
try text_mod.set(style2, .font_size, 48 * gfx.px_per_pt); // 48pt
|
try text_style.set(style2, .font_size, 48 * gfx.px_per_pt); // 48pt
|
||||||
try text_mod.set(style2, .font_weight, gfx.font_weight_normal);
|
try text_style.set(style2, .font_weight, gfx.font_weight_normal);
|
||||||
try text_mod.set(style2, .italic, true);
|
try text_style.set(style2, .italic, true);
|
||||||
try text_mod.set(style2, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
try text_style.set(style2, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
||||||
|
|
||||||
const style3 = try engine.newEntity();
|
const style3 = try engine.newEntity();
|
||||||
try text_mod.set(style3, .font_name, "Roboto Medium"); // TODO
|
try text_style.set(style3, .font_name, "Roboto Medium"); // TODO
|
||||||
try text_mod.set(style3, .font_size, 48 * gfx.px_per_pt); // 48pt
|
try text_style.set(style3, .font_size, 48 * gfx.px_per_pt); // 48pt
|
||||||
try text_mod.set(style3, .font_weight, gfx.font_weight_bold);
|
try text_style.set(style3, .font_weight, gfx.font_weight_bold);
|
||||||
try text_mod.set(style3, .italic, false);
|
try text_style.set(style3, .italic, false);
|
||||||
try text_mod.set(style3, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
try text_style.set(style3, .color, vec4(0.6, 1.0, 0.6, 1.0));
|
||||||
|
|
||||||
// Create some text
|
// Create some text
|
||||||
const player = try engine.newEntity();
|
const player = try engine.newEntity();
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ const Game = @import("Game.zig");
|
||||||
pub const modules = .{
|
pub const modules = .{
|
||||||
mach.Engine,
|
mach.Engine,
|
||||||
mach.gfx.Text,
|
mach.gfx.Text,
|
||||||
|
mach.gfx.TextStyle,
|
||||||
Game,
|
Game,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,31 +53,6 @@ pub const components = .{
|
||||||
\\
|
\\
|
||||||
\\ Expected to match the length of the text component.
|
\\ 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 = .{
|
pub const global_events = .{
|
||||||
|
|
@ -420,16 +395,16 @@ fn updated(
|
||||||
|
|
||||||
for (segments, styles) |segment, style| {
|
for (segments, styles) |segment, style| {
|
||||||
// Load a font
|
// 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
|
_ = font_name; // TODO: actually use font name
|
||||||
const font_bytes = @import("font-assets").fira_sans_regular_ttf;
|
const font_bytes = @import("font-assets").fira_sans_regular_ttf;
|
||||||
var font = try gfx.Font.initBytes(font_bytes);
|
var font = try gfx.Font.initBytes(font_bytes);
|
||||||
defer font.deinit(text_mod.state().allocator);
|
defer font.deinit(text_mod.state().allocator);
|
||||||
|
|
||||||
const font_size = engine.entities.getComponent(style, .mach_gfx_text, .font_size).?;
|
const font_size = engine.entities.getComponent(style, .mach_gfx_text_style, .font_size).?;
|
||||||
const font_weight = engine.entities.getComponent(style, .mach_gfx_text, .font_weight);
|
const font_weight = engine.entities.getComponent(style, .mach_gfx_text_style, .font_weight);
|
||||||
const italic = engine.entities.getComponent(style, .mach_gfx_text, .italic);
|
const italic = engine.entities.getComponent(style, .mach_gfx_text_style, .italic);
|
||||||
const color = engine.entities.getComponent(style, .mach_gfx_text, .color);
|
const color = engine.entities.getComponent(style, .mach_gfx_text_style, .color);
|
||||||
// TODO: actually apply these
|
// TODO: actually apply these
|
||||||
_ = font_weight;
|
_ = font_weight;
|
||||||
_ = italic;
|
_ = italic;
|
||||||
|
|
|
||||||
37
src/gfx/TextStyle.zig
Normal file
37
src/gfx/TextStyle.zig
Normal 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
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
@ -4,6 +4,7 @@ pub const Atlas = @import("atlas/Atlas.zig");
|
||||||
// ECS modules
|
// ECS modules
|
||||||
pub const Sprite = @import("Sprite.zig");
|
pub const Sprite = @import("Sprite.zig");
|
||||||
pub const Text = @import("Text.zig");
|
pub const Text = @import("Text.zig");
|
||||||
|
pub const TextStyle = @import("TextStyle.zig");
|
||||||
|
|
||||||
// Fonts
|
// Fonts
|
||||||
pub const Font = @import("font/main.zig").Font;
|
pub const Font = @import("font/main.zig").Font;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue