Meh zoom implementation.

This commit is contained in:
KikooDX 2021-01-28 16:12:41 +01:00
parent 0b52b35238
commit 0519e630d8
2 changed files with 33 additions and 16 deletions

View File

@ -50,7 +50,7 @@ pub fn deinit(self: *Self, allocator: *std.mem.Allocator) void {
}
/// Draw level tiles from `offset` to fill the window.
pub fn draw(self: *Self, offset: Vec2) void {
pub fn draw(self: *Self, scale: u16, offset: Vec2) void {
// Pixel position (were we draw).
var x: Vec2.int_type = 0;
var y: Vec2.int_type = 0;
@ -65,18 +65,18 @@ pub fn draw(self: *Self, offset: Vec2) void {
1 => ray.WHITE,
else => ray.PURPLE, //unknown
};
ray.DrawPixel(x, y, color);
y += 1;
ray.DrawRectangle(x + 1, y + 1, scale - 2, scale - 2, color);
y += scale;
cy += 1;
}
y = 0;
x += 1;
x += scale;
cx += 1;
}
}
/// Draw selection from `offset` to fill the window.
pub fn draw_selection(self: *Self, offset: Vec2) void {
pub fn draw_selection(self: *Self, scale: u16, offset: Vec2) void {
// Pixel position (were we draw).
var x: Vec2.int_type = 0;
var y: Vec2.int_type = 0;
@ -86,12 +86,12 @@ pub fn draw_selection(self: *Self, offset: Vec2) void {
var cy: Vec2.int_type = offset.y;
while (cy < self.height) {
if (self.selection[cy * self.width + cx])
ray.DrawPixel(x, y, ray.GREEN);
y += 1;
ray.DrawRectangleLines(x, y, scale, scale, ray.GREEN);
y += scale;
cy += 1;
}
y = 0;
x += 1;
x += scale;
cx += 1;
}
}

View File

@ -5,6 +5,7 @@ const std = @import("std");
const assert = std.debug.assert;
const format = std.fmt.format;
const log = std.log.default.debug;
const maxInt = std.math.maxInt;
const Level = @import("level.zig");
const Vec2 = @import("vec2.zig");
@ -31,6 +32,10 @@ pub fn main() !void {
// Create camera.
var camera: Vec2 = Vec2.init(0, 0);
// Scale, used by drawing code.
const scale_type = u16;
var scale: scale_type = 4;
// Create cursor.
var cursor: Vec2 = Vec2.init(0, 0);
@ -66,16 +71,28 @@ pub fn main() !void {
input_cursor -= 1;
const action = input_buffer[input_cursor];
const selection_update: movement.SelectionUpdate = switch (action) {
'h' => movement.move_left(&cursor, 1, true),
'l' => movement.move_right(&cursor, 1, true),
'H' => movement.move_left(&cursor, 1, false),
'L' => movement.move_right(&cursor, 1, false),
var selection_update: movement.SelectionUpdate = undefined;
switch (action) {
// Movement.
'h' => selection_update = movement.move_left(&cursor, 1, true),
'l' => selection_update = movement.move_right(&cursor, 1, true),
'H' => selection_update = movement.move_left(&cursor, 1, false),
'L' => selection_update = movement.move_right(&cursor, 1, false),
// Zoom. TODO: move to functions.
'-' => if (scale > 3) {
scale -= 1;
},
'+' => if (scale < comptime maxInt(scale_type)) {
scale += 1;
},
// Reset zoom.
'0' => scale = 4,
else => {
log("No action mapped to key {}.", .{action});
break undefined;
},
};
}
if (selection_update.exclusive != undefined)
level.apply_selection_update(selection_update);
@ -85,8 +102,8 @@ pub fn main() !void {
defer ray.EndDrawing();
ray.ClearBackground(ray.BLACK);
level.draw(camera);
level.draw_selection(camera);
level.draw(scale, camera);
level.draw_selection(scale, camera);
//ray.DrawFPS(0, 0);
}
}