Fixed extra junk after ms string with bufPrintZ

This commit is contained in:
Nathan Anderson 2025-01-06 09:33:38 -07:00
parent 41a208846a
commit 5680b1a5a5
2 changed files with 4 additions and 5 deletions

View File

@ -107,7 +107,7 @@ pub fn main() !void {
var buf: [8]u8 = undefined;
const timer_ms = timer.getTicks();
// const timer_base = std.math.log10(timer_ms) + 3;
const time_str: []const u8 = try std.fmt.bufPrint(&buf, "{d}ms", .{timer_ms});
const time_str: [*:0]const u8 = try std.fmt.bufPrintZ(&buf, "{d}ms", .{timer_ms});
var time_text = try GameText.loadFromRenderedText(time_str, RGBAColor.whiteSmoke().tosdl());
try time_text.render(
&game_state,

View File

@ -9,15 +9,14 @@ var text_texture: ?*sdl.SDL_Texture = null;
var text_init = false;
pub const GameText = struct {
text: []const u8,
text: [*:0]const u8,
color: sdl.SDL_Color,
surface: *sdl.SDL_Surface,
w: u32,
h: u32,
pub fn loadFromRenderedText(text: []const u8, color: sdl.SDL_Color) !GameText {
const trunc_str = text[0..text.len];
const c_text: [*c]const u8 = @ptrCast(trunc_str);
pub fn loadFromRenderedText(text: [*:0]const u8, color: sdl.SDL_Color) !GameText {
const c_text: [*c]const u8 = @ptrCast(text);
const text_surface: [*c]sdl.SDL_Surface = sdl.TTF_RenderText_Solid(font.?, c_text, color) orelse {
sdl.SDL_Log("Error loading text surface: %s\n", sdl.SDL_GetError());
return error.FailedToRenderSurface;