2021-01-29 12:32:38 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Copyright (c) 2021 KikooDX
|
|
|
|
// This file is part of [KBLE](https://sr.ht/~kikoodx/kble), which is
|
|
|
|
// MIT licensed. The MIT license requires this copyright notice to be
|
|
|
|
// included in all copies and substantial portions of the software.
|
2021-01-24 19:22:48 +01:00
|
|
|
const ray = @cImport({
|
|
|
|
@cInclude("raylib.h");
|
|
|
|
});
|
2021-01-26 17:44:40 +01:00
|
|
|
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;
|
2021-01-28 16:12:41 +01:00
|
|
|
const maxInt = std.math.maxInt;
|
2021-01-26 17:44:40 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-26 13:10:16 +01:00
|
|
|
pub fn main() !void {
|
|
|
|
// Create allocator
|
2021-01-26 23:38:41 +01:00
|
|
|
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
|
|
|
|
2021-01-26 17:44:40 +01:00
|
|
|
// Create window.
|
2021-01-24 19:22:48 +01:00
|
|
|
ray.SetConfigFlags(ray.FLAG_WINDOW_RESIZABLE);
|
2021-01-26 13:10:16 +01:00
|
|
|
ray.InitWindow(640, 480, "KBLE");
|
2021-01-24 19:22:48 +01:00
|
|
|
defer ray.CloseWindow();
|
|
|
|
|
2021-01-26 17:44:40 +01:00
|
|
|
// 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.
|
2021-01-28 16:52:49 +01:00
|
|
|
var level: Level = try Level.init(allocator, 16, 16);
|
2021-01-26 13:10:16 +01:00
|
|
|
defer level.deinit(allocator);
|
|
|
|
|
2021-01-27 14:12:44 +01:00
|
|
|
// Create camera.
|
2021-01-26 17:44:40 +01:00
|
|
|
var camera: Vec2 = Vec2.init(0, 0);
|
|
|
|
|
2021-01-28 16:12:41 +01:00
|
|
|
// Scale, used by drawing code.
|
|
|
|
const scale_type = u16;
|
2021-01-28 17:06:03 +01:00
|
|
|
const scale_default = 16;
|
|
|
|
var scale: scale_type = scale_default;
|
2021-01-28 16:12:41 +01:00
|
|
|
|
2021-01-28 13:46:02 +01:00
|
|
|
// Create cursor.
|
|
|
|
var cursor: Vec2 = Vec2.init(0, 0);
|
|
|
|
|
2021-01-27 14:12:44 +01:00
|
|
|
// Create input buffer (ASCII).
|
2021-01-27 19:26:12 +01:00
|
|
|
const input_buffer_len = 255;
|
2021-01-27 14:12:44 +01:00
|
|
|
var input_buffer: [input_buffer_len]u32 = undefined;
|
|
|
|
comptime {
|
2021-01-27 19:26:12 +01:00
|
|
|
var i: u8 = 0;
|
2021-01-27 14:12:44 +01:00
|
|
|
while (i < input_buffer_len) : (i += 1) {
|
|
|
|
input_buffer[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 19:26:12 +01:00
|
|
|
var input_cursor: u8 = 0;
|
2021-01-27 14:12:44 +01:00
|
|
|
|
2021-01-26 13:10:16 +01:00
|
|
|
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];
|
|
|
|
|
2021-01-28 16:12:41 +01:00
|
|
|
var selection_update: movement.SelectionUpdate = undefined;
|
|
|
|
|
2021-01-29 12:32:38 +01:00
|
|
|
// TODO: move to functions.
|
2021-01-28 16:12:41 +01:00
|
|
|
switch (action) {
|
|
|
|
// Movement.
|
|
|
|
'h' => selection_update = movement.move_left(&cursor, 1, true),
|
2021-01-28 16:52:49 +01:00
|
|
|
'j' => selection_update = movement.move_down(&cursor, 1, true),
|
|
|
|
'k' => selection_update = movement.move_up(&cursor, 1, true),
|
2021-01-28 16:12:41 +01:00
|
|
|
'l' => selection_update = movement.move_right(&cursor, 1, true),
|
|
|
|
'H' => selection_update = movement.move_left(&cursor, 1, false),
|
2021-01-28 16:52:49 +01:00
|
|
|
'J' => selection_update = movement.move_down(&cursor, 1, false),
|
|
|
|
'K' => selection_update = movement.move_up(&cursor, 1, false),
|
2021-01-28 16:12:41 +01:00
|
|
|
'L' => selection_update = movement.move_right(&cursor, 1, false),
|
2021-01-28 16:52:49 +01:00
|
|
|
// Diagonals and rectangle selection.
|
|
|
|
'y' => selection_update = movement.move_up_left(&cursor, 1, true),
|
|
|
|
'u' => selection_update = movement.move_up_right(&cursor, 1, true),
|
|
|
|
'b' => selection_update = movement.move_down_left(&cursor, 1, true),
|
|
|
|
'n' => selection_update = movement.move_down_right(&cursor, 1, true),
|
|
|
|
'Y' => selection_update = movement.move_up_left(&cursor, 1, false),
|
|
|
|
'U' => selection_update = movement.move_up_right(&cursor, 1, false),
|
|
|
|
'B' => selection_update = movement.move_down_left(&cursor, 1, false),
|
|
|
|
'N' => selection_update = movement.move_down_right(&cursor, 1, false),
|
2021-01-28 17:06:03 +01:00
|
|
|
// Actions.
|
|
|
|
'd' => level.action_delete(),
|
2021-01-28 16:52:49 +01:00
|
|
|
// Zoom (pog feature).
|
2021-01-28 16:12:41 +01:00
|
|
|
'-' => if (scale > 3) {
|
|
|
|
scale -= 1;
|
|
|
|
},
|
|
|
|
'+' => if (scale < comptime maxInt(scale_type)) {
|
|
|
|
scale += 1;
|
|
|
|
},
|
|
|
|
// Reset zoom.
|
2021-01-28 17:06:03 +01:00
|
|
|
'0' => scale = scale_default,
|
2021-01-28 16:52:49 +01:00
|
|
|
else => log("No action mapped to key {}.", .{action}),
|
2021-01-28 16:12:41 +01:00
|
|
|
}
|
2021-01-28 13:46:02 +01:00
|
|
|
|
|
|
|
if (selection_update.exclusive != undefined)
|
|
|
|
level.apply_selection_update(selection_update);
|
2021-01-27 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-24 19:22:48 +01:00
|
|
|
ray.BeginDrawing();
|
|
|
|
defer ray.EndDrawing();
|
|
|
|
|
2021-01-26 17:44:40 +01:00
|
|
|
ray.ClearBackground(ray.BLACK);
|
2021-01-28 16:12:41 +01:00
|
|
|
level.draw(scale, camera);
|
|
|
|
level.draw_selection(scale, camera);
|
2021-01-28 13:46:02 +01:00
|
|
|
//ray.DrawFPS(0, 0);
|
2021-01-24 19:22:48 +01:00
|
|
|
}
|
|
|
|
}
|