mirror of https://git.sr.ht/~kikoodx/kble
Input system more DRY, add Tab (`\t`) support.
This commit is contained in:
parent
25335e94a6
commit
eec14bc99c
Binary file not shown.
30
src/main.zig
30
src/main.zig
|
@ -24,6 +24,7 @@ const ActionCat = actions.ActionCat;
|
|||
const Mode = @import("modes.zig").Mode;
|
||||
|
||||
const char_range = 255;
|
||||
const input_buffer_len = 255;
|
||||
|
||||
pub fn main() void {
|
||||
const level_path: [*:0]const u8 = if (std.os.argv.len > 1)
|
||||
|
@ -120,7 +121,6 @@ pub fn main() void {
|
|||
}
|
||||
|
||||
// Create input buffer.
|
||||
const input_buffer_len = 255;
|
||||
var input_buffer: [input_buffer_len]u32 = undefined;
|
||||
comptime {
|
||||
var i: u8 = 0;
|
||||
|
@ -132,28 +132,18 @@ pub fn main() void {
|
|||
|
||||
while (!ray.WindowShouldClose()) {
|
||||
{
|
||||
// TODO: apply DRY
|
||||
// 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;
|
||||
|
||||
add_key_to_buffer(&input_buffer, &input_cursor, key);
|
||||
key = ray.GetCharPressed();
|
||||
}
|
||||
// Check for special keys, not detected by GetCharPressed.
|
||||
if (ray.IsKeyDown(ray.KEY_ENTER)) {
|
||||
input_buffer[input_cursor] = '\n';
|
||||
input_cursor += 1;
|
||||
// Avoid writing out of memory.
|
||||
if (input_cursor >= input_buffer_len)
|
||||
input_cursor = input_buffer_len - 1;
|
||||
}
|
||||
if (ray.IsKeyPressed(ray.KEY_ENTER))
|
||||
add_key_to_buffer(&input_buffer, &input_cursor, '\n');
|
||||
if (ray.IsKeyPressed(ray.KEY_TAB))
|
||||
add_key_to_buffer(&input_buffer, &input_cursor, '\t');
|
||||
}
|
||||
|
||||
// Process buffer content.
|
||||
|
@ -274,3 +264,11 @@ pub fn main() void {
|
|||
mouse.draw(scale, camera);
|
||||
}
|
||||
}
|
||||
|
||||
fn add_key_to_buffer(input_buffer: *[input_buffer_len]u32, input_cursor: *u8, key: c_int) void {
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue