kble/src/main.zig

83 lines
2.3 KiB
Zig
Raw Normal View History

2021-01-24 19:22:48 +01:00
const ray = @cImport({
@cInclude("raylib.h");
});
const std = @import("std");
const assert = std.debug.assert;
const format = std.fmt.format;
2021-01-27 14:12:44 +01:00
const log = std.log.default.debug;
const Level = @import("level.zig");
const Vec2 = @import("vec2.zig");
2021-01-27 17:23:45 +01:00
const movement = @import("movement.zig");
2021-01-24 19:22:48 +01:00
pub fn main() !void {
// Create allocator
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = &arena.allocator;
2021-01-24 19:22:48 +01:00
// Create window.
2021-01-24 19:22:48 +01:00
ray.SetConfigFlags(ray.FLAG_WINDOW_RESIZABLE);
ray.InitWindow(640, 480, "KBLE");
2021-01-24 19:22:48 +01:00
defer ray.CloseWindow();
// Limit FPS for performance.
2021-01-24 19:22:48 +01:00
ray.SetTargetFPS(60);
2021-01-27 14:12:44 +01:00
// Create level.
var level: Level = try Level.init(allocator, 128, 128);
defer level.deinit(allocator);
2021-01-27 14:12:44 +01:00
// Create camera.
var camera: Vec2 = Vec2.init(0, 0);
2021-01-27 14:12:44 +01:00
// Create input buffer (ASCII).
const input_buffer_len = 256;
var input_buffer: [input_buffer_len]u32 = undefined;
comptime {
var i: u16 = 0;
while (i < input_buffer_len) : (i += 1) {
input_buffer[i] = 0;
}
}
var input_cursor: u16 = 0;
while (!ray.WindowShouldClose()) {
2021-01-27 14:12:44 +01:00
// Get keyboard input.
var key = ray.GetCharPressed();
// Check if more characters have been pressed.
while (key != 0) {
// Add key to buffer.
input_buffer[input_cursor] = @intCast(u32, key);
input_cursor += 1;
// Avoid writing out of memory.
if (input_cursor >= input_buffer_len)
input_cursor = input_buffer_len - 1;
key = ray.GetCharPressed();
}
2021-01-27 17:23:45 +01:00
// Process buffer content.
// Read the buffer backwards. This is placeholder logic.
while (input_cursor > 0) {
input_cursor -= 1;
const action = input_buffer[input_cursor];
switch (action) {
'h' => movement.move_left(&camera, 1),
'j' => {},
'k' => {},
'l' => movement.move_right(&camera, 1),
else => log("No action mapped to this key.", .{}),
}
}
2021-01-24 19:22:48 +01:00
ray.BeginDrawing();
defer ray.EndDrawing();
ray.ClearBackground(ray.BLACK);
level.draw(camera);
ray.DrawFPS(0, 0);
2021-01-24 19:22:48 +01:00
}
}