Added setCharInputCallback, and minor cleanup

This commit is contained in:
2022-01-10 16:30:58 -07:00
parent ed6ab7c92a
commit ed2445e623
2 changed files with 62 additions and 28 deletions

View File

@ -3,10 +3,10 @@ const std = @import("std");
const min = std.math.min;
const max = std.math.max;
const Width = 800;
const Height = 600;
const AspectRatio = @intToFloat(comptime_float, Width) / @intToFloat(comptime_float, Height);
const CursorWidth = 25;
const buffer_width = 800;
const buffer_height = 600;
const aspect_ratio = @intToFloat(comptime_float, buffer_width) / @intToFloat(comptime_float, buffer_height);
const cursor_width = 25;
fn grey(value: u8) mfb.Rgb {
return mfb.Rgb{ .r = value, .g = value, .b = value };
@ -36,12 +36,14 @@ const State = struct {
alloc: std.mem.Allocator,
cursor_pos: Coords = undefined,
rectangle_pos: FCoords = FCoords{
.x = @intToFloat(f32, Width) / 2.0,
.y = @intToFloat(f32, Height) / 2.0,
.x = @intToFloat(f32, buffer_width) / 2.0,
.y = @intToFloat(f32, buffer_height) / 2.0,
},
rectangle_width: f32 = @intToFloat(f32, Width) / 3.0,
rectangle_height: f32 = @intToFloat(f32, Height) / 3.0,
rectangle_width: f32 = @intToFloat(f32, buffer_width) / 3.0,
rectangle_height: f32 = @intToFloat(f32, buffer_height) / 3.0,
paused: bool = false,
cheat_code: []const u8 = "rave",
cheat_code_progress: usize = 0,
pub fn init(alloc: std.mem.Allocator, period: f64) !State {
var timer = try alloc.create(mfb.Timer);
@ -58,15 +60,31 @@ const State = struct {
self.alloc.destroy(self.timer);
}
pub fn cheatInput(self: *State, char: u32) void {
if (self.isCheating()) {
return;
}
if (char == self.cheat_code[self.cheat_code_progress]) {
self.cheat_code_progress += 1;
} else {
self.cheat_code_progress = 0;
}
}
fn isCheating(self: State) bool {
return self.cheat_code_progress == self.cheat_code.len;
}
pub fn render(self: State, buf: Buffer) void {
var bgValue = @rem(self.timer.*.now(), self.period) / self.period * 512;
const period = if (!self.isCheating()) self.period else self.period / 10;
var bgValue = @rem(self.timer.*.now(), period) / period * 512;
if (bgValue >= 256) {
bgValue = 512 - bgValue;
}
const rectColor = if (self.active) mfb.Rgb{ .r = 0, .g = 255, .b = 0 } else mfb.Rgb{ .r = 255, .g = 0, .b = 0 };
buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = Width, .y = Height }, grey(@floatToInt(u8, bgValue)));
buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = buffer_width, .y = buffer_height }, grey(@floatToInt(u8, bgValue)));
buf.drawRectangle(
(FCoords{
.x = self.rectangle_pos.x - self.rectangle_width / 2,
@ -81,8 +99,8 @@ const State = struct {
// Draw cursor
buf.drawRectangle(
.{ .x = self.cursor_pos.x - CursorWidth / 2, .y = self.cursor_pos.y - CursorWidth / 2 },
.{ .x = self.cursor_pos.x + CursorWidth / 2, .y = self.cursor_pos.y + CursorWidth / 2 },
.{ .x = self.cursor_pos.x - cursor_width / 2, .y = self.cursor_pos.y - cursor_width / 2 },
.{ .x = self.cursor_pos.x + cursor_width / 2, .y = self.cursor_pos.y + cursor_width / 2 },
mfb.Rgb{ .r = 61, .g = 97, .b = 170 },
);
}
@ -146,11 +164,11 @@ fn handleResize(win: mfb.Window(State), width: i32, height: i32) callconv(.C) vo
var vp_width: i32 = width;
var vp_offset_x: i32 = 0;
var vp_offset_y: i32 = 0;
if (win_aspect_ratio > AspectRatio) {
vp_width = @floatToInt(i32, @intToFloat(f32, height) * AspectRatio);
if (win_aspect_ratio > aspect_ratio) {
vp_width = @floatToInt(i32, @intToFloat(f32, height) * aspect_ratio);
vp_offset_x = @divTrunc(width - vp_width, 2);
} else {
vp_height = @floatToInt(i32, @intToFloat(f32, width) / AspectRatio);
vp_height = @floatToInt(i32, @intToFloat(f32, width) / aspect_ratio);
vp_offset_y = @divTrunc(height - vp_height, 2);
}
const did_set = win.setViewport(
@ -205,6 +223,10 @@ fn handleKeyboard(win: mfb.Window(State), key: mfb.Key, _: mfb.KeyMod, is_presse
}
}
fn handleCharInput(win: mfb.Window(State), char: u32) callconv(.C) void {
win.getUserData().?.cheatInput(char);
}
pub fn main() !void {
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gp_allocator.allocator();
@ -212,20 +234,29 @@ pub fn main() !void {
var state = try State.init(alloc, 3);
defer state.deinit();
var win = mfb.Window(State).open("Hello minifb-zig", Width, Height, .{ .resizable = true, .alwaysOnTop = true }) catch unreachable;
const scale = win.getMonitorScale();
std.log.info("Monitor scale: {d} * {d}", .{ scale.x, scale.y });
var win = mfb.Window(State).open("Hello minifb-zig", buffer_width, buffer_height, .{ .resizable = true, .always_on_top = true }) catch unreachable;
mfb.setTargetFPS(30);
win.setUserData(&state);
const scale = win.getMonitorScale();
try std.io.getStdOut().writer().print(
\\Monitor scale: {d} * {d}
\\
\\Press spacebar to pause, escape to quit.
\\Try [Ctrl+][Shift+]scroll wheel to change the colored rectangle.
\\Type "{s}" to get crazy.
\\
, .{ scale.x, scale.y, state.cheat_code });
win.setActiveCallback(handleActive);
win.setResizeCallback(handleResize);
win.setMouseButtonCallback(handleMouseButton);
win.setMouseMoveCallback(handleMouseMove);
win.setMouseScrollCallback(handleMouseScroll);
win.setKeyboardCallback(handleKeyboard);
win.setCharInputCallback(handleCharInput);
var buf = try Buffer.init(alloc, Width, Height);
var buf = try Buffer.init(alloc, buffer_width, buffer_height);
defer buf.deinit();
state.render(buf);