Add text!

This commit is contained in:
2026-03-11 23:07:34 +01:00
parent 4a144ac9f9
commit c1a6e9ca9a
6 changed files with 8350 additions and 7 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

+2
View File
@@ -15,6 +15,8 @@ pub fn build(b: *std.Build) void {
.target = target,
});
mod.addIncludePath(b.path("src"));
const exe = b.addExecutable(.{
.name = "wl-main",
.use_llvm = llvm,
+1 -1
View File
@@ -679,7 +679,7 @@ fn wlKeyboardHandleKey(data: ?*anyopaque, keyboard: ?*c.wl_keyboard, serial: u32
const buf_end: usize = blk: {
var i: usize = 0;
while (buf[i] != 0) {
while (i < 32 and buf[i] != 0) {
i += 1;
}
break :blk i;
+6
View File
@@ -1,5 +1,11 @@
const std = @import("std");
pub const c = @cImport({
@cDefine("STB_IMAGE_IMPLEMENTATION", "");
@cDefine("STBI_ONLY_PNG", "");
@cInclude("stb_image.h");
});
pub fn kiloBytes(bytes: usize) usize {
return bytes * 1024;
}
+353 -6
View File
@@ -20,14 +20,14 @@ const V2 = struct {
};
}
fn init_int(x: i32, y: i32) V2 {
fn initI32(x: i32, y: i32) V2 {
return .{
.x = @floatFromInt(x),
.y = @floatFromInt(y),
};
}
fn init_usize(x: usize, y: usize) V2 {
fn initUSize(x: usize, y: usize) V2 {
return .{
.x = @floatFromInt(x),
.y = @floatFromInt(y),
@@ -86,6 +86,14 @@ const Camera = struct {
offset: V2,
};
const Image = struct {
width: i32,
height: i32,
stride: i32,
components: i32,
data: []u8,
};
const BoardTile = struct {
blocked: bool,
taken: bool,
@@ -195,6 +203,7 @@ const State = struct {
frame_count: i32,
mouse_pos: V2,
camera: Camera,
font_image: Image,
draw_bounding_rects: bool,
grabbed_brick_index: ?usize,
bricks: [8]Brick,
@@ -401,6 +410,310 @@ fn fillSquare(
fillRect(buffer, x, y, size, size, color);
}
fn drawImage(buffer: platform.OffscreenBuffer, image: Image, pos: V2, scale: f32) void {
const pixels: []u32 = @ptrCast(@alignCast(buffer.data));
const scale_factor: f32 = 1.0 / scale;
const buffer_width: usize = @intCast(buffer.width);
const image_width: f32 = @floatFromInt(image.width);
const image_height: f32 = @floatFromInt(image.height);
var min_x: i32 = @intFromFloat(pos.x);
var min_y: i32 = @intFromFloat(pos.y);
var max_x: i32 = @intFromFloat(pos.x + scale * @as(f32, @floatFromInt(image.width)));
var max_y: i32 = @intFromFloat(pos.y + scale * @as(f32, @floatFromInt(image.height)));
var image_offset_x: i32 = 0;
var image_offset_y: i32 = 0;
if (min_x < 0) {
image_offset_x = -min_x;
min_x = 0;
}
if (min_y < 0) {
image_offset_y = -min_y;
min_y = 0;
}
if (buffer.width < max_x) max_x = buffer.width;
if (buffer.height < max_y) max_y = buffer.height;
const start_x: usize = @intCast(min_x);
const start_y: usize = @intCast(min_y);
const end_x: usize = @intCast(max_x);
const end_y: usize = @intCast(max_y);
var image_x: i32 = image_offset_x * image.components;
var image_y: i32 = image_offset_y;
for (start_y..end_y) |y| {
for (start_x..end_x) |x| {
const idx: usize = y * buffer_width + x;
const current: u32 = pixels[idx];
const current_alpha = (0xFF000000 & current) >> 24;
// const current_red = (0x00FF0000 & current) >> 16;
// const current_green = (0x0000FF00 & current) >> 8;
// const current_blue = (0x000000FF & current) >> 0;
const x_f: f32 = @as(f32, @floatFromInt(image_x)) / @as(f32, @floatFromInt(image.width));
const xff: f32 = scale_factor * x_f;
const new_x: i32 = @as(i32, @intFromFloat(image_width * xff));
const y_f: f32 = @as(f32, @floatFromInt(image_y)) / @as(f32, @floatFromInt(image.height));
const yff: f32 = scale_factor * y_f;
const new_y: i32 = @as(i32, @intFromFloat(image_height * yff));
const image_idx: usize = @intCast(new_y * image.stride + new_x);
const image_pixel: [*]u8 = image.data.ptr + image_idx;
var image_alpha: u32 = 0xFF;
const image_red: u32 = image_pixel[0];
const image_green: u32 = image_pixel[1];
const image_blue: u32 = image_pixel[2];
if (image.components > 3) {
image_alpha = image_pixel[3];
}
if (image.components == 3 and image_red == 0 and image_green == 0 and image_blue == 0) {
image_alpha = 0;
}
// u32 red = lerp_color(image_alpha, current_red, image_red);
// u32 green = lerp_color(image_alpha, current_green, image_green);
// u32 blue = lerp_color(image_alpha, current_blue, image_blue);
const red = image_red;
const green = image_green;
const blue = image_blue;
pixels[idx] = (current_alpha << 24) | (red << 16) | (green << 8) | blue;
image_x += image.components;
}
image_x = image_offset_x * image.components;
image_y += 1;
}
}
fn indexFont(font: Image, c: u8) V2 {
var result = V2.zero;
_ = font;
var i: i32 = 0;
var j: i32 = 0;
switch (c) {
' ' => {
i = 15;
j = 1;
},
'(' => {
i = 7;
j = 2;
},
')' => {
i = 8;
j = 2;
},
',' => {
i = 11;
j = 2;
},
'-' => {
i = 12;
j = 2;
},
'.' => {
i = 13;
j = 2;
},
'0' => {
i = 15;
j = 2;
},
'1' => {
i = 0;
j = 3;
},
'2' => {
i = 1;
j = 3;
},
'3' => {
i = 2;
j = 3;
},
'4' => {
i = 3;
j = 3;
},
'5' => {
i = 4;
j = 3;
},
'6' => {
i = 5;
j = 3;
},
'7' => {
i = 6;
j = 3;
},
'8' => {
i = 7;
j = 3;
},
'9' => {
i = 8;
j = 3;
},
':' => {
i = 9;
j = 3;
},
'V' => {
i = 5;
j = 5;
},
'a' => {
i = 1;
j = 6;
},
'b' => {
i = 2;
j = 6;
},
'c' => {
i = 3;
j = 6;
},
'd' => {
i = 4;
j = 6;
},
'e' => {
i = 5;
j = 6;
},
'f' => {
i = 6;
j = 6;
},
'g' => {
i = 7;
j = 6;
},
'h' => {
i = 8;
j = 6;
},
'i' => {
i = 9;
j = 6;
},
'j' => {
i = 10;
j = 6;
},
'k' => {
i = 11;
j = 6;
},
'l' => {
i = 12;
j = 6;
},
'm' => {
i = 13;
j = 6;
},
'n' => {
i = 14;
j = 6;
},
'o' => {
i = 15;
j = 6;
},
'p' => {
i = 0;
j = 7;
},
'q' => {
i = 1;
j = 7;
},
'r' => {
i = 2;
j = 7;
},
's' => {
i = 3;
j = 7;
},
't' => {
i = 4;
j = 7;
},
'u' => {
i = 5;
j = 7;
},
'v' => {
i = 6;
j = 7;
},
'w' => {
i = 7;
j = 7;
},
'x' => {
i = 8;
j = 7;
},
'y' => {
i = 9;
j = 7;
},
'z' => {
i = 10;
j = 7;
},
else => {
},
}
result.x = @floatFromInt(i * 32);
result.y = @floatFromInt(j * 32);
return result;
}
fn drawString(buffer: platform.OffscreenBuffer, image: Image, pos: V2, str: []const u8) void {
var current_pos = pos;
const scale = 0.5;
for (str) |c| {
const char_image_pos = indexFont(image, c);
const pos_x: i32 = @intFromFloat(char_image_pos.x);
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 char_image: Image = .{
.width = 32,
.height = 32,
.stride = image.stride,
.components = image.components,
.data = ptr[0..size],
};
drawImage(buffer, char_image, current_pos, scale);
current_pos.x += 16;
}
}
fn keyPressed(key: *platform.Key) bool {
var result = false;
@@ -416,6 +729,29 @@ fn keyPressed(key: *platform.Key) bool {
return result;
}
fn loadPng(image_path: []const u8) ?Image {
var x: c_int = undefined;
var y: c_int = undefined;
var n: c_int = undefined;
const image_data: [*c]platform.c.stbi_uc = platform.c.stbi_load(@ptrCast(image_path), &x, &y, &n, 0);
if (image_data != 0) {
const stride = n * x;
const size: usize = @intCast(y * stride);
const ptr: []u8 = image_data[0..size];
return .{
.width = x,
.height = y,
.stride = stride,
.components = n,
.data = ptr,
};
} else {
return null;
}
}
pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBuffer, input: *platform.AppInput) void {
var state: *State = @ptrCast(@alignCast(memory.permanent_storage));
@@ -428,6 +764,11 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
state.grabbed_brick_index = null;
const font_image = loadPng("assets/font.png");
if (font_image) |img| {
state.font_image = img;
}
const indices = [4][4]?BoardIndex{
.{ null, null, null, null },
.{ null, null, null, null },
@@ -548,8 +889,8 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
};
const tile_names = [7][7]?[]const u8{
.{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", null },
.{ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", null },
.{ "jan", "feb", "mar", "apr", "may", "jun", null },
.{ "jul", "aug", "sep", "oct", "nov", "dec", null },
.{ "1", "2", "3", "4", "5", "6", "7" },
.{ "8", "9", "10", "11", "12", "13", "14" },
.{ "15", "16", "17", "18", "19", "20", "21" },
@@ -570,10 +911,10 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
}
state.board = .{
.pos = mid,
.pos = V2.zero,
.width = 7,
.height = 7,
.tile_size = V2.init(32, 32),
.tile_size = V2.init(64, 64),
.tiles = tiles,
};
@@ -694,6 +1035,11 @@ 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;
@@ -721,6 +1067,7 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
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);
}
}
}
+7988
View File
File diff suppressed because it is too large Load Diff