We doing global variables now

This commit is contained in:
duck
2025-07-07 21:11:55 +05:00
parent b2be9e3708
commit 7e162221ef
14 changed files with 521 additions and 1445 deletions

View File

@@ -1,155 +1,106 @@
const std = @import("std");
const builtin = @import("builtin");
const sdl = @import("sdl");
const err = @import("error.zig");
const Mouse = @import("mouse.zig");
const Keyboard = @import("keyboard.zig");
const debug_scene = @import("debug_scene.zig");
const Graph = @import("graph.zig");
const Graphics = @import("graphics.zig");
pub const Graphics = @import("graphics.zig");
pub const World = @import("world.zig");
// TODO:
// - Do something about deallocating `Resource`s when `Graph` fails
pub const RunInfo = struct { running: bool };
pub const Mouse = @import("mouse.zig");
pub const Keyboard = @import("keyboard.zig");
pub const Time = struct {
const Time = struct {
delta: f32,
now: sdl.Time,
};
alloc: std.mem.Allocator,
graph: Graph,
var alloc: std.mem.Allocator = undefined;
const Self = @This();
pub fn init(alloc: std.mem.Allocator) GameError!Self {
var graph = try Graph.init(alloc);
errdefer graph.deinit();
var running: bool = false;
var time: Time = .{ .delta = 0, .now = 0 };
pub var keyboard: Keyboard = .{};
pub var mouse: Mouse = .{};
const graphics = try Graphics.create();
var controller = try graph.getController();
controller.addResource(graphics);
controller.addResource(Mouse{
.buttons = .{},
.x = 0.0,
.y = 0.0,
.dx = 0.0,
.dy = 0.0,
});
controller.addResource(Keyboard{});
controller.addResource(Time{
.delta = 0.0,
.now = 0,
});
controller.queue(debug_scene.init);
try graph.freeController(controller);
defer graph.reset();
try graph.runAllSystems();
return Self{
.alloc = alloc,
.graph = graph,
};
const Game = @This();
pub fn init(game_alloc: std.mem.Allocator) void {
Game.alloc = game_alloc;
Game.running = false;
Game.time = Time{ .now = 0, .delta = 0 };
Game.keyboard = .{};
Game.mouse = .{ .x = 0, .y = 0, .dx = 0, .dy = 0 };
Graphics.create();
World.initDebug();
}
pub fn run(self: *Self) GameError!void {
{
var controller = try self.graph.getController();
controller.addResource(RunInfo{ .running = true });
try self.graph.freeController(controller);
}
while (true) {
if (!self.graph.getResource(RunInfo).?.running) {
break;
}
pub fn run() void {
Game.running = true;
while (Game.running) {
var current_time: sdl.Time = undefined;
if (sdl.GetCurrentTime(&current_time)) {
const time = self.graph.getResource(Time).?;
if (time.now != 0) {
time.delta = @as(f32, @floatFromInt(current_time - time.now)) * 0.000000001;
if (Game.time.now != 0) {
Game.time.delta = @as(f32, @floatFromInt(current_time - Game.time.now)) * 0.000000001;
}
time.now = current_time;
} else return error.SdlError;
Game.time.now = current_time;
} else err.sdl();
var controller = try self.graph.getController();
controller.queue(.{
processEvents,
debug_scene.updateReal,
beginDraw,
endDraw,
Graph.Controller.Option.ordered,
});
try self.graph.freeController(controller);
defer self.graph.reset();
try self.graph.runAllSystems();
Game.processEvents();
World.updateReal(Game.time.delta);
if (Game.beginDraw()) {
World.draw(Game.time.delta);
Game.endDraw();
}
}
}
fn beginDraw(graphics: *Graphics, controller: *Graph.Controller) GameError!void {
if (try graphics.beginDraw()) {
controller.queue(debug_scene.draw);
}
fn beginDraw() bool {
return Graphics.beginDraw();
}
fn endDraw(graphics: *Graphics) GameError!void {
try graphics.endDraw();
fn endDraw() void {
Graphics.endDraw();
}
fn clean(graphics: *Graphics) !void {
graphics.destroy();
// TODO: Also remove the resource
}
fn processEvents(
graphics: *Graphics,
run_info: *RunInfo,
mouse: *Mouse,
keyboard: *Keyboard,
) GameError!void {
mouse.dx = 0.0;
mouse.dy = 0.0;
keyboard.keys.reset();
fn processEvents() void {
Game.mouse.dx = 0.0;
Game.mouse.dy = 0.0;
Game.keyboard.keys.reset();
sdl.PumpEvents();
while (true) {
var buffer: [16]sdl.Event = undefined;
const count: usize = @intCast(sdl.PeepEvents(&buffer, buffer.len, sdl.GETEVENT, sdl.EVENT_FIRST, sdl.EVENT_LAST));
if (count == -1) return GameError.SdlError;
if (count == -1) err.sdl();
for (buffer[0..count]) |event| {
switch (event.type) {
sdl.EVENT_QUIT => {
run_info.running = false;
Game.running = false;
},
sdl.EVENT_WINDOW_RESIZED => {
if (event.window.windowID != sdl.GetWindowID(graphics.window)) continue;
graphics.resize(@intCast(event.window.data1), @intCast(event.window.data2));
if (event.window.windowID != Graphics.windowId()) continue;
Graphics.resize(@intCast(event.window.data1), @intCast(event.window.data2));
},
sdl.EVENT_MOUSE_MOTION => {
if (event.motion.windowID != sdl.GetWindowID(graphics.window)) continue;
mouse.x = event.motion.x;
mouse.y = event.motion.y;
mouse.dx += event.motion.xrel;
mouse.dy += event.motion.yrel;
if (event.motion.windowID != Graphics.windowId()) continue;
Game.mouse.x = event.motion.x;
Game.mouse.y = event.motion.y;
Game.mouse.dx += event.motion.xrel;
Game.mouse.dy += event.motion.yrel;
},
sdl.EVENT_KEY_DOWN => {
if (event.key.windowID != sdl.GetWindowID(graphics.window)) continue;
keyboard.keys.press(event.key.scancode);
if (event.key.windowID != Graphics.windowId()) continue;
Game.keyboard.keys.press(event.key.scancode);
},
sdl.EVENT_KEY_UP => {
if (event.key.windowID != sdl.GetWindowID(graphics.window)) continue;
keyboard.keys.release(event.key.scancode);
if (event.key.windowID != Graphics.windowId()) continue;
Game.keyboard.keys.release(event.key.scancode);
},
sdl.EVENT_MOUSE_BUTTON_DOWN => {
if (event.button.windowID != sdl.GetWindowID(graphics.window)) continue;
mouse.buttons.press(event.button.button);
if (event.button.windowID != Graphics.windowId()) continue;
Game.mouse.buttons.press(event.button.button);
},
sdl.EVENT_MOUSE_BUTTON_UP => {
if (event.button.windowID != sdl.GetWindowID(graphics.window)) continue;
mouse.buttons.release(event.button.button);
if (event.button.windowID != Graphics.windowId()) continue;
Game.mouse.buttons.release(event.button.button);
},
else => {},
}
@@ -161,25 +112,8 @@ fn processEvents(
sdl.FlushEvents(sdl.EVENT_FIRST, sdl.EVENT_LAST);
}
pub fn deinit(self: *Self) void {
var controller = self.graph.getController() catch unreachable;
controller.queue(.{
debug_scene.deinit,
clean,
Graph.Controller.Option.ordered,
});
self.graph.freeController(controller) catch unreachable;
self.graph.runAllSystems() catch unreachable;
self.graph.deinit();
pub fn deinit() void {
World.deinit();
Graphics.destroy();
sdl.Quit();
}
pub const GameError = error{
SdlError,
OSError,
OutOfMemory,
MissingResource,
SystemDeadlock,
};