Make the example more interesting
This commit is contained in:
parent
3778320a3e
commit
efba4456e7
109
src/main.zig
109
src/main.zig
@ -1,6 +1,7 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
const mfb = @import("minifb");
|
const mfb = @import("minifb");
|
||||||
|
const std = @import("std");
|
||||||
|
const min = std.math.min;
|
||||||
|
const max = std.math.max;
|
||||||
|
|
||||||
const Width = 800;
|
const Width = 800;
|
||||||
const Height = 600;
|
const Height = 600;
|
||||||
@ -9,38 +10,102 @@ fn grey(value: u8) u32 {
|
|||||||
return mfb.rgb(value, value, value);
|
return mfb.rgb(value, value, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Coords = struct {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
};
|
||||||
|
|
||||||
|
const State = struct {
|
||||||
|
bgValue: u8,
|
||||||
|
increasing: bool,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn render(buf: Buffer, state: State) void {
|
||||||
|
buf.drawRectangle(.{.x=0,.y=0}, .{.x=Width, .y=Height}, grey(state.bgValue));
|
||||||
|
buf.drawRectangle(.{.x=Width/3, .y=Height/3}, .{.x=2*Width/3, .y=2*Height/3}, mfb.rgb(255,0,0));
|
||||||
|
}
|
||||||
|
|
||||||
|
const Buffer = struct {
|
||||||
|
alloc: *std.mem.Allocator,
|
||||||
|
slice: []u32,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
|
||||||
|
pub fn init(alloc: *std.mem.Allocator, width: u32, height: u32) anyerror!Buffer {
|
||||||
|
const slice = try alloc.alloc(u32, width*height);
|
||||||
|
return Buffer {
|
||||||
|
.alloc=alloc,
|
||||||
|
.slice=slice,
|
||||||
|
.width=width,
|
||||||
|
.height=height,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: Buffer) void {
|
||||||
|
self.alloc.free(self.slice);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setPixel(self: Buffer, coords: Coords, color: u32) void {
|
||||||
|
const x = @intCast(u32, coords.x);
|
||||||
|
const y = @intCast(u32, coords.y);
|
||||||
|
self.slice[y*self.width + x] = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn drawRectangle(self: Buffer, a: Coords, b: Coords, color: u32) void {
|
||||||
|
var start = Coords{
|
||||||
|
.x=max(0, min(a.x, b.x)),
|
||||||
|
.y=max(0, min(a.y, b.y)),
|
||||||
|
};
|
||||||
|
var end = Coords{
|
||||||
|
.x=min(@intCast(i32, self.width), max(a.x, b.x)),
|
||||||
|
.y=min(@intCast(i32, self.height), max(a.y, b.y)),
|
||||||
|
};
|
||||||
|
|
||||||
|
var y = start.y;
|
||||||
|
while (y < end.y) {
|
||||||
|
var x = start.x;
|
||||||
|
while (x < end.x) {
|
||||||
|
self.setPixel(.{.x=x, .y=y}, color);
|
||||||
|
x += 1;
|
||||||
|
}
|
||||||
|
y += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
|
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
const alloc = &gp_allocator.allocator;
|
||||||
|
|
||||||
std.log.info("All your codebase are belong to us.", .{});
|
std.log.info("All your codebase are belong to us.", .{});
|
||||||
var win = mfb.Window.open("Hello minifb-zig", Width, Height) catch unreachable;
|
var win = mfb.Window.open("Hello minifb-zig", Width, Height) catch unreachable;
|
||||||
mfb.setTargetFPS(60);
|
mfb.setTargetFPS(60);
|
||||||
|
|
||||||
var color: u32 = grey(0);
|
var state = State {
|
||||||
const deltaColor = grey(1);
|
.increasing=true,
|
||||||
var increasing = true;
|
.bgValue=0,
|
||||||
var rawBuffer: [Width*Height]u32 = undefined;
|
};
|
||||||
const buffer = rawBuffer[0..rawBuffer.len];
|
var buf = try Buffer.init(alloc, Width, Height);
|
||||||
std.mem.set(u32, buffer, color);
|
defer buf.deinit();
|
||||||
|
|
||||||
|
render(buf, state);
|
||||||
while (win.waitSync()) {
|
while (win.waitSync()) {
|
||||||
if (increasing) {
|
if (state.increasing) {
|
||||||
if (color < grey(255)) {
|
if (state.bgValue < 255) {
|
||||||
color += deltaColor;
|
state.bgValue += 1;
|
||||||
} else {
|
} else {
|
||||||
increasing = false;
|
state.increasing = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (color > grey(0)) {
|
if (state.bgValue > 0) {
|
||||||
color -= deltaColor;
|
state.bgValue -= 1;
|
||||||
} else {
|
} else {
|
||||||
increasing = true;
|
state.increasing = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std.mem.set(u32, buffer, color);
|
|
||||||
var i: u32 = 0;
|
render(buf, state);
|
||||||
while (i < Width) {
|
// TODO Handle mfb state
|
||||||
buffer[i] = mfb.rgb(255,0,0);
|
_ = win.update(buf.slice) catch unreachable;
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
_ = win.update(buffer) catch unreachable;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user