Add input buffer.

This commit is contained in:
KikooDX 2021-01-27 14:12:44 +01:00
parent 900586d374
commit 23fac64415
1 changed files with 29 additions and 15 deletions

View File

@ -5,6 +5,7 @@ const std = @import("std");
const assert = std.debug.assert;
const format = std.fmt.format;
const maxInt = std.math.maxInt;
const log = std.log.default.debug;
const Level = @import("level.zig");
const Vec2 = @import("vec2.zig");
@ -23,27 +24,40 @@ pub fn main() !void {
// Limit FPS for performance.
ray.SetTargetFPS(60);
// Create level
// Create level.
var level: Level = try Level.init(allocator, 128, 128);
defer level.deinit(allocator);
// Create camera
// Create camera.
var camera: Vec2 = Vec2.init(0, 0);
while (!ray.WindowShouldClose()) {
// Temp code: move camera with arrow keys.
if (ray.IsKeyDown(ray.KEY_LEFT) and camera.x > 0)
camera.x -= 1;
if (ray.IsKeyDown(ray.KEY_RIGHT) and
camera.x < comptime maxInt(Vec2.int_type))
camera.x += 1;
if (ray.IsKeyDown(ray.KEY_UP) and camera.y > 0)
camera.y -= 1;
if (ray.IsKeyDown(ray.KEY_DOWN) and
camera.y < comptime maxInt(Vec2.int_type))
camera.y += 1;
// 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()) {
// 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);
log("Key pressed: {}", .{key});
input_cursor += 1;
// Avoid writing out of memory.
if (input_cursor >= input_buffer_len)
input_cursor = input_buffer_len - 1;
key = ray.GetCharPressed();
}
// Put all draw code after this.
ray.BeginDrawing();
defer ray.EndDrawing();