Centered text

This commit is contained in:
2026-03-12 22:37:24 +01:00
parent c1a6e9ca9a
commit c59ed511c0
2 changed files with 40 additions and 21 deletions
+13
View File
@@ -794,8 +794,21 @@ fn registry_handle_global(
}
}
fn registryHandleGlobalRemove(
data: ?*anyopaque,
registry: ?*c.wl_registry,
name: u32,
) callconv(.c) void {
// var wl_state: *WaylandState = @ptrCast(@alignCast(data));
_ = data;
_ = registry;
_ = name;
}
const registry_listener: c.wl_registry_listener = .{
.global = registry_handle_global,
.global_remove = registryHandleGlobalRemove,
};
pub fn main() u8 {
+27 -21
View File
@@ -692,7 +692,9 @@ fn indexFont(font: Image, c: u8) V2 {
fn drawString(buffer: platform.OffscreenBuffer, image: Image, pos: V2, str: []const u8) void {
var current_pos = pos;
const scale = 0.5;
const font_size = 32;
const scale = 0.25;
const font_width_scaled = scale * font_size;
for (str) |c| {
const char_image_pos = indexFont(image, c);
@@ -700,17 +702,17 @@ fn drawString(buffer: platform.OffscreenBuffer, image: Image, pos: V2, str: []co
const pos_y: i32 = @intFromFloat(char_image_pos.y);
const idx: usize = @intCast(pos_y * image.stride + (pos_x * image.components));
const ptr = image.data.ptr + idx;
const size: usize = @intCast(32 * image.stride);
const size: usize = @intCast(font_size * image.stride);
const char_image: Image = .{
.width = 32,
.height = 32,
.width = font_size,
.height = font_size,
.stride = image.stride,
.components = image.components,
.data = ptr[0..size],
};
drawImage(buffer, char_image, current_pos, scale);
current_pos.x += 16;
current_pos.x += font_width_scaled;
}
}
@@ -911,12 +913,18 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
}
state.board = .{
.pos = V2.zero,
.pos = mid,
.width = 7,
.height = 7,
.tile_size = V2.init(64, 64),
.tile_size = V2.init(32, 32),
.tiles = tiles,
};
const board_size = V2.initI32(
@intCast(state.board.width * TILE_SIZE),
@intCast(state.board.height * TILE_SIZE),
);
const half = board_size.mul(0.5);
state.board.pos = state.board.pos.sub(half);
memory.initialized = true;
}
@@ -1035,29 +1043,21 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
// Render
fillRect(buffer, 0, 0, buffer.width, buffer.height, 0xFFFFFFFF);
// {
// const camera_pos = V2.init(20.0, 20.0).add(state.camera.pos);
// drawImage(buffer, state.font_image, camera_pos, 1.0);
// }
// Draw board
{
const board = state.board;
// const board_size = V2.init_int(
// @intCast(board.width * TILE_SIZE),
// @intCast(board.height * TILE_SIZE),
// );
// const half = board_size.mul(0.5);
for (board.tiles, 0..) |row, i| {
for (row, 0..) |tile, j| {
const tile_pos = board.getTilePos(i, j);
const camera_pos = tile_pos.add(state.camera.pos);
const pos_x: i32 = @intFromFloat(camera_pos.x);
const pos_y: i32 = @intFromFloat(camera_pos.y);
const width: i32 = @intFromFloat(board.tile_size.x);
const height: i32 = @intFromFloat(board.tile_size.y);
const str_width = @divFloor(32 * tile.label.len, 4);
const str_height = @divFloor(32, 4);
const str_size = V2.initUSize(str_width, str_height);
const string_pos = tile_pos.add(board.tile_size.mul(0.5)).sub(str_size.mul(0.5));
var color: u32 = 0xFF000000;
if (tile.blocked) {
color = 0xFFFFFFFF;
@@ -1065,9 +1065,15 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
color = 0xFF00FF00;
}
const camera_pos = tile_pos.add(state.camera.pos);
const pos_x: i32 = @intFromFloat(camera_pos.x);
const pos_y: i32 = @intFromFloat(camera_pos.y);
fillRect(buffer, pos_x, pos_y, width, height, color);
drawRect(buffer, camera_pos, board.tile_size, 0xFFFFFFFF, 2);
drawString(buffer, state.font_image, camera_pos, tile.label);
const string_camera_pos = string_pos.add(state.camera.pos);
drawString(buffer, state.font_image, string_camera_pos, tile.label);
}
}
}