Added timers to the game

This commit is contained in:
2025-01-06 09:21:35 -07:00
parent 52097b3861
commit 41a208846a
4 changed files with 100 additions and 15 deletions
+11 -4
View File
@@ -1,6 +1,8 @@
const sdl = @import("./sdl.zig").c;
const std = @import("std");
const GameState = @import("./game_state.zig").GameState;
const Offset = @import("./utils/offset.zig").Offset;
const FONT_SIZE = 24;
var font: ?*sdl.TTF_Font = null;
var text_texture: ?*sdl.SDL_Texture = null;
@@ -14,7 +16,8 @@ pub const GameText = struct {
h: u32,
pub fn loadFromRenderedText(text: []const u8, color: sdl.SDL_Color) !GameText {
const c_text: [*c]const u8 = @ptrCast(text);
const trunc_str = text[0..text.len];
const c_text: [*c]const u8 = @ptrCast(trunc_str);
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;
@@ -45,7 +48,11 @@ pub const GameText = struct {
return error.Unimplemented;
}
pub fn render(self: *GameText, game_state: *GameState) !void {
pub fn render(
self: *GameText,
game_state: *GameState,
offset: Offset,
) !void {
if (!text_init) {
return error.TextNotInitialized;
}
@@ -62,12 +69,12 @@ pub const GameText = struct {
};
const srcr = sdl.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(self.w), .h = @intCast(self.h) };
const destr = sdl.SDL_Rect{ .x = @intCast(game_state.SCREEN_WIDTH - self.w), .y = 10, .w = @intCast(self.w), .h = @intCast(self.h) };
const destr = sdl.SDL_Rect{ .x = @intCast(offset.x), .y = @intCast(offset.y), .w = @intCast(self.w), .h = @intCast(self.h) };
_ = sdl.SDL_RenderCopy(game_state.renderer, text_texture, &srcr, &destr);
}
pub fn initFont() !void {
const loaded_font = sdl.TTF_OpenFont("./assets/fonts/DepartureMonoNF-Regular.ttf", 24) orelse {
const loaded_font = sdl.TTF_OpenFont("./assets/fonts/DepartureMonoNF-Regular.ttf", FONT_SIZE) orelse {
sdl.SDL_Log("Error loading ttf font: %s\n", sdl.TTF_GetError());
return error.FailedToLoadFont;
};