camelCase for function names
This commit is contained in:
14
src/game.zig
14
src/game.zig
@@ -16,7 +16,7 @@ pub fn init() GameError!Self {
|
||||
pub fn run(self: *Self) GameError!void {
|
||||
self.running = true;
|
||||
while (true) {
|
||||
try self.process_events();
|
||||
try self.processEvents();
|
||||
if (!self.running) {
|
||||
break;
|
||||
}
|
||||
@@ -30,19 +30,19 @@ fn update(self: *Self) GameError!void {
|
||||
}
|
||||
|
||||
fn draw(self: *Self) GameError!void {
|
||||
try self.graphics.begin_draw();
|
||||
try self.graphics.draw_debug();
|
||||
try self.graphics.end_draw();
|
||||
try self.graphics.beginDraw();
|
||||
try self.graphics.drawDebug();
|
||||
try self.graphics.endDraw();
|
||||
}
|
||||
|
||||
fn process_events(self: *Self) GameError!void {
|
||||
fn processEvents(self: *Self) GameError!void {
|
||||
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;
|
||||
for (buffer[0..count]) |event| {
|
||||
self.process_event(event);
|
||||
self.processEvent(event);
|
||||
}
|
||||
if (count < buffer.len) {
|
||||
break;
|
||||
@@ -51,7 +51,7 @@ fn process_events(self: *Self) GameError!void {
|
||||
sdl.FlushEvents(sdl.EVENT_FIRST, sdl.EVENT_LAST);
|
||||
}
|
||||
|
||||
fn process_event(self: *Self, event: sdl.Event) void {
|
||||
fn processEvent(self: *Self, event: sdl.Event) void {
|
||||
switch (event.type) {
|
||||
sdl.EVENT_QUIT => {
|
||||
self.running = false;
|
||||
|
@@ -70,22 +70,21 @@ pub fn deinit(self: *Self) void {
|
||||
self.controllers.deinit(self.alloc);
|
||||
}
|
||||
|
||||
fn enqueue_system(self: *Self, system: System) !void {
|
||||
fn enqueueSystem(self: *Self, system: System) !void {
|
||||
errdefer system.deinit(self.alloc);
|
||||
try self.system_queue.append(self.alloc, system);
|
||||
}
|
||||
|
||||
fn run_all_systems(self: *Self) GraphError!void {
|
||||
while (self.system_queue.items.len > 0) {
|
||||
const next_system = self.system_queue.pop();
|
||||
fn runAllSystems(self: *Self) GraphError!void {
|
||||
while (self.system_queue.pop()) |next_system| {
|
||||
defer next_system.deinit(self.alloc);
|
||||
|
||||
try self.run_system(next_system);
|
||||
try self.runSystem(next_system);
|
||||
}
|
||||
}
|
||||
|
||||
/// Does not deallocate the system
|
||||
fn run_system(self: *Self, system: System) GraphError!void {
|
||||
fn runSystem(self: *Self, system: System) GraphError!void {
|
||||
var buffer: [MAX_SYSTEM_REQUESTS]*anyopaque = undefined;
|
||||
var controller: ?Controller = null;
|
||||
errdefer if (controller) |*c| c.deinit();
|
||||
@@ -94,10 +93,10 @@ fn run_system(self: *Self, system: System) GraphError!void {
|
||||
for (system.requested_types) |request| {
|
||||
switch (request) {
|
||||
.resource => |resource| {
|
||||
buffer[buffer_len] = self.get_anyopaque_resource(resource) orelse return GraphError.MissingResource;
|
||||
buffer[buffer_len] = self.getAnyopaqueResource(resource) orelse return GraphError.MissingResource;
|
||||
},
|
||||
.controller => {
|
||||
controller = try self.get_controller();
|
||||
controller = try self.getController();
|
||||
buffer[buffer_len] = @ptrCast(&controller.?);
|
||||
},
|
||||
}
|
||||
@@ -107,44 +106,44 @@ fn run_system(self: *Self, system: System) GraphError!void {
|
||||
|
||||
if (controller) |c| {
|
||||
defer controller = null;
|
||||
try self.free_controller(c);
|
||||
try self.freeController(c);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_commands(self: *Self, commands: []const Controller.Command) !void {
|
||||
fn applyCommands(self: *Self, commands: []const Controller.Command) !void {
|
||||
for (commands) |command| {
|
||||
switch (command) {
|
||||
.add_resource => |r| try self.add_resource(r),
|
||||
.queue_system => |s| try self.enqueue_system(s),
|
||||
.add_resource => |r| try self.addResource(r),
|
||||
.queue_system => |s| try self.enqueueSystem(s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_controller(self: *Self) !Controller {
|
||||
if (self.controllers.popOrNull()) |c| {
|
||||
fn getController(self: *Self) !Controller {
|
||||
if (self.controllers.pop()) |c| {
|
||||
return c;
|
||||
}
|
||||
return Controller.create(self.alloc);
|
||||
}
|
||||
|
||||
/// Evaluates and clears the controller (even if errors out)
|
||||
fn free_controller(self: *Self, controller: Controller) !void {
|
||||
fn freeController(self: *Self, controller: Controller) !void {
|
||||
var c = controller;
|
||||
try self.apply_commands(c.commands());
|
||||
try self.applyCommands(c.commands());
|
||||
c.clear();
|
||||
try self.controllers.append(self.alloc, c);
|
||||
// TODO: Handle controller error state
|
||||
}
|
||||
|
||||
pub inline fn get_resource(self: *Self, comptime resource: type) ?*resource {
|
||||
utils.validate_resource(resource);
|
||||
if (get_anyopaque_resource(self, utils.hash_type(resource))) |ptr| {
|
||||
pub inline fn getResource(self: *Self, comptime resource: type) ?*resource {
|
||||
utils.validateResource(resource);
|
||||
if (getAnyopaqueResource(self, utils.hashType(resource))) |ptr| {
|
||||
return @alignCast(@ptrCast(ptr));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
fn get_anyopaque_resource(self: *Self, resource_hash: utils.Hash) ?*anyopaque {
|
||||
fn getAnyopaqueResource(self: *Self, resource_hash: utils.Hash) ?*anyopaque {
|
||||
if (self.resources.get(resource_hash)) |resource| {
|
||||
return resource.pointer;
|
||||
}
|
||||
@@ -152,7 +151,7 @@ fn get_anyopaque_resource(self: *Self, resource_hash: utils.Hash) ?*anyopaque {
|
||||
}
|
||||
|
||||
/// Discards any previous resource data, resource is assumed to be allocated with `self.alloc`
|
||||
pub inline fn add_resource(self: *Self, resource: Resource) !void {
|
||||
pub inline fn addResource(self: *Self, resource: Resource) !void {
|
||||
var previous = try self.resources.fetchPut(self.alloc, resource.hash, resource);
|
||||
if (previous) |*p| {
|
||||
p.value.deinit(self.alloc);
|
||||
@@ -169,35 +168,35 @@ test {
|
||||
const TestResource = struct {
|
||||
number: u32,
|
||||
|
||||
fn add_one(rsc: *@This()) void {
|
||||
fn addOne(rsc: *@This()) void {
|
||||
rsc.number += 1;
|
||||
}
|
||||
fn add_ten(rsc: *@This()) void {
|
||||
fn addTen(rsc: *@This()) void {
|
||||
rsc.number += 10;
|
||||
}
|
||||
fn add_eleven(cmd: *Controller) void {
|
||||
cmd.queue_system(add_ten);
|
||||
cmd.queue_system(add_one);
|
||||
fn addEleven(cmd: *Controller) void {
|
||||
cmd.queueSystem(addTen);
|
||||
cmd.queuesystem(addOne);
|
||||
}
|
||||
};
|
||||
|
||||
var graph = try Graph.init(std.testing.allocator);
|
||||
defer graph.deinit();
|
||||
|
||||
var controller = try graph.get_controller();
|
||||
controller.add_resource(TestResource{ .number = 100 });
|
||||
var controller = try graph.getController();
|
||||
controller.addResource(TestResource{ .number = 100 });
|
||||
|
||||
controller.queue_system(TestResource.add_one);
|
||||
controller.queue_system(TestResource.add_one);
|
||||
controller.queueSystem(TestResource.addOne);
|
||||
controller.queueSystem(TestResource.addOne);
|
||||
|
||||
controller.queue_system(TestResource.add_ten);
|
||||
controller.queueSystem(TestResource.addTen);
|
||||
|
||||
controller.queue_system(TestResource.add_eleven);
|
||||
controller.queueSystem(TestResource.addEleven);
|
||||
|
||||
try graph.free_controller(controller);
|
||||
try graph.freeController(controller);
|
||||
|
||||
try graph.run_all_systems();
|
||||
try graph.runAllSystems();
|
||||
|
||||
const result = graph.get_resource(TestResource);
|
||||
const result = graph.getResource(TestResource);
|
||||
try std.testing.expectEqual(result.?.number, 123);
|
||||
}
|
||||
|
@@ -56,32 +56,32 @@ pub const Controller = struct {
|
||||
}
|
||||
|
||||
/// Adds resource to the global storage, discarding any previously existing data
|
||||
pub inline fn add_resource(self: *Controller, resource: anytype) void {
|
||||
utils.validate_resource(@TypeOf(resource));
|
||||
pub inline fn addResource(self: *Controller, resource: anytype) void {
|
||||
utils.validateResource(@TypeOf(resource));
|
||||
|
||||
self.add_anyopaque_resource(
|
||||
self.addAnyopaqueResource(
|
||||
@ptrCast(&resource),
|
||||
utils.hash_type(@TypeOf(resource)),
|
||||
utils.hashType(@TypeOf(resource)),
|
||||
@sizeOf(@TypeOf(resource)),
|
||||
@alignOf(@TypeOf(resource)),
|
||||
) catch |err| self.fail(err);
|
||||
}
|
||||
|
||||
pub fn queue_system(self: *Controller, comptime function: anytype) void {
|
||||
utils.validate_system(function);
|
||||
pub fn queueSystem(self: *Controller, comptime function: anytype) void {
|
||||
utils.validateSystem(function);
|
||||
|
||||
self.queue_system_internal(function) catch |err| self.fail(err);
|
||||
self.queueSystemInternal(function) catch |err| self.fail(err);
|
||||
}
|
||||
|
||||
fn queue_system_internal(self: *Controller, comptime function: anytype) !void {
|
||||
var system = try System.from_function(function, self.alloc);
|
||||
fn queueSystemInternal(self: *Controller, comptime function: anytype) !void {
|
||||
var system = try System.fromFunction(function, self.alloc);
|
||||
errdefer system.deinit(self.alloc);
|
||||
|
||||
try self.command_buffer.append(self.alloc, .{ .queue_system = system });
|
||||
}
|
||||
|
||||
/// `previous_output` is expected to be aligned accordingly
|
||||
fn add_anyopaque_resource(
|
||||
fn addAnyopaqueResource(
|
||||
self: *Controller,
|
||||
resource: *const anyopaque,
|
||||
hash: utils.Hash,
|
||||
|
@@ -13,20 +13,20 @@ pub const Request = union(enum) {
|
||||
};
|
||||
|
||||
const Self = @This();
|
||||
pub fn from_function(comptime function: anytype, alloc: std.mem.Allocator) !Self {
|
||||
utils.validate_system(function);
|
||||
pub fn fromFunction(comptime function: anytype, alloc: std.mem.Allocator) !Self {
|
||||
utils.validateSystem(function);
|
||||
|
||||
var requests: [@typeInfo(@TypeOf(function)).Fn.params.len]Request = undefined;
|
||||
inline for (0.., @typeInfo(@TypeOf(function)).Fn.params) |i, param| {
|
||||
switch (@typeInfo(param.type.?).Pointer.child) {
|
||||
var requests: [@typeInfo(@TypeOf(function)).@"fn".params.len]Request = undefined;
|
||||
inline for (0.., @typeInfo(@TypeOf(function)).@"fn".params) |i, param| {
|
||||
switch (@typeInfo(param.type.?).pointer.child) {
|
||||
Controller => requests[i] = .controller,
|
||||
else => |resource_type| requests[i] = .{ .resource = utils.hash_type(resource_type) },
|
||||
else => |resource_type| requests[i] = .{ .resource = utils.hashType(resource_type) },
|
||||
}
|
||||
}
|
||||
|
||||
return Self{
|
||||
.requested_types = try alloc.dupe(Request, &requests),
|
||||
.function_runner = utils.generate_runner(function),
|
||||
.function_runner = utils.generateRunner(function),
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -5,11 +5,11 @@ const System = @import("system.zig");
|
||||
pub const Hash = u32;
|
||||
const HashAlgorithm = std.crypto.hash.blake2.Blake2s(@bitSizeOf(Hash));
|
||||
|
||||
pub inline fn hash_type(comptime h_type: type) Hash {
|
||||
return hash_string(@typeName(h_type));
|
||||
pub inline fn hashType(comptime h_type: type) Hash {
|
||||
return hashString(@typeName(h_type));
|
||||
}
|
||||
|
||||
pub fn hash_string(comptime name: []const u8) Hash {
|
||||
pub fn hashString(comptime name: []const u8) Hash {
|
||||
@setEvalBranchQuota(100000);
|
||||
var output: [@divExact(@bitSizeOf(Hash), 8)]u8 = undefined;
|
||||
|
||||
@@ -21,39 +21,39 @@ pub fn hash_string(comptime name: []const u8) Hash {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn validate_resource(comptime resource_type: type) void {
|
||||
pub fn validateResource(comptime resource_type: type) void {
|
||||
switch (@typeInfo(resource_type)) {
|
||||
.Struct, .Enum, .Union => return,
|
||||
.@"struct", .@"enum", .@"union" => return,
|
||||
else => @compileError("Invalid resource type \"" ++ @typeName(resource_type) ++ "\""),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate_system(comptime system: anytype) void {
|
||||
pub fn validateSystem(comptime system: anytype) void {
|
||||
const info = @typeInfo(@TypeOf(system));
|
||||
if (info != .Fn) @compileError("System can only be a function, got " ++ @typeName(system));
|
||||
if (info.Fn.return_type != void) @compileError("Systems are not allowed to return any value (" ++ @typeName(info.Fn.return_type.?) ++ " returned)");
|
||||
if (info.Fn.is_var_args) @compileError("System cannot be variadic");
|
||||
if (info.Fn.is_generic) @compileError("System cannot be generic");
|
||||
if (info != .@"fn") @compileError("System can only be a function, got " ++ @typeName(system));
|
||||
if (info.@"fn".return_type != void) @compileError("Systems are not allowed to return any value (" ++ @typeName(info.Fn.return_type.?) ++ " returned)");
|
||||
if (info.@"fn".is_var_args) @compileError("System cannot be variadic");
|
||||
if (info.@"fn".is_generic) @compileError("System cannot be generic");
|
||||
|
||||
const controller_requests: usize = 0;
|
||||
inline for (info.Fn.params) |param| {
|
||||
if (@typeInfo(param.type.?) != .Pointer) @compileError("Systems can only have pointer parameters");
|
||||
switch (@typeInfo(param.type.?).Pointer.child) {
|
||||
inline for (info.@"fn".params) |param| {
|
||||
if (@typeInfo(param.type.?) != .pointer) @compileError("Systems can only have pointer parameters");
|
||||
switch (@typeInfo(param.type.?).pointer.child) {
|
||||
Resource.Controller => {
|
||||
// controller_requests += 1;
|
||||
// _ = &controller_requests;
|
||||
},
|
||||
else => |t| validate_resource(t),
|
||||
else => |t| validateResource(t),
|
||||
}
|
||||
}
|
||||
if (controller_requests > 1) @compileError("A system cannot request controller more than once");
|
||||
}
|
||||
|
||||
pub fn generate_runner(comptime system: anytype) fn ([]const *anyopaque) void {
|
||||
pub fn generateRunner(comptime system: anytype) fn ([]const *anyopaque) void {
|
||||
const RunnerImpl = struct {
|
||||
fn runner(resources: []const *anyopaque) void {
|
||||
var args: std.meta.ArgsTuple(@TypeOf(system)) = undefined;
|
||||
inline for (0..@typeInfo(@TypeOf(system)).Fn.params.len) |index| {
|
||||
inline for (0..@typeInfo(@TypeOf(system)).@"fn".params.len) |index| {
|
||||
args[index] = @alignCast(@ptrCast(resources[index]));
|
||||
}
|
||||
@call(.always_inline, system, args);
|
||||
|
@@ -52,14 +52,14 @@ pub fn create() GameError!Self {
|
||||
if (!sdl.ClaimWindowForGPUDevice(device, window)) return GameError.SdlError;
|
||||
errdefer sdl.ReleaseWindowFromGPUDevice(device, window);
|
||||
|
||||
const shader_vert = try load_shader(
|
||||
const shader_vert = try loadShader(
|
||||
device,
|
||||
"data/shaders/basic.vert",
|
||||
sdl.GPU_SHADERSTAGE_VERTEX,
|
||||
);
|
||||
errdefer sdl.ReleaseGPUShader(device, shader_vert);
|
||||
|
||||
const shader_frag = try load_shader(
|
||||
const shader_frag = try loadShader(
|
||||
device,
|
||||
"data/shaders/basic.frag",
|
||||
sdl.GPU_SHADERSTAGE_FRAGMENT,
|
||||
@@ -115,10 +115,10 @@ pub fn create() GameError!Self {
|
||||
var window_height: c_int = 1;
|
||||
if (!sdl.GetWindowSizeInPixels(window, &window_width, &window_height)) return GameError.SdlError;
|
||||
|
||||
const depth_texture = try create_depth_texture(device, @intCast(window_width), @intCast(window_height));
|
||||
const depth_texture = try createDepthTexture(device, @intCast(window_width), @intCast(window_height));
|
||||
errdefer sdl.ReleaseGPUTexture(device, depth_texture);
|
||||
|
||||
const msaa_resolve = try create_texture(device, @intCast(window_width), @intCast(window_height), target_format);
|
||||
const msaa_resolve = try createTexture(device, @intCast(window_width), @intCast(window_height), target_format);
|
||||
errdefer sdl.ReleaseGPUTexture(device, msaa_resolve);
|
||||
|
||||
const pipeline = sdl.CreateGPUGraphicsPipeline(device, &.{
|
||||
@@ -209,15 +209,15 @@ pub fn destroy(self: *Self) void {
|
||||
sdl.DestroyGPUDevice(self.device);
|
||||
}
|
||||
|
||||
pub fn begin_draw(self: *Self) GameError!void {
|
||||
pub fn beginDraw(self: *Self) GameError!void {
|
||||
self.command_buffer = sdl.AcquireGPUCommandBuffer(self.device) orelse return GameError.SdlError;
|
||||
if (self.to_resize) |new_size| {
|
||||
try self.reset_textures(new_size[0], new_size[1]);
|
||||
try self.resetTextures(new_size[0], new_size[1]);
|
||||
self.to_resize = null;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_debug(self: *Self) GameError!void {
|
||||
pub fn drawDebug(self: *Self) GameError!void {
|
||||
var render_target: ?*sdl.GPUTexture = null;
|
||||
var width: u32 = 0;
|
||||
var height: u32 = 0;
|
||||
@@ -250,12 +250,12 @@ pub fn draw_debug(self: *Self) GameError!void {
|
||||
sdl.EndGPURenderPass(render_pass);
|
||||
}
|
||||
|
||||
pub fn end_draw(self: *Self) GameError!void {
|
||||
pub fn endDraw(self: *Self) GameError!void {
|
||||
defer self.command_buffer = null;
|
||||
if (!sdl.SubmitGPUCommandBuffer(self.command_buffer)) return GameError.SdlError;
|
||||
}
|
||||
|
||||
fn load_shader(device: *sdl.GPUDevice, path: []const u8, stage: c_uint) GameError!*sdl.GPUShader {
|
||||
fn loadShader(device: *sdl.GPUDevice, path: []const u8, stage: c_uint) GameError!*sdl.GPUShader {
|
||||
const file = std.fs.cwd().openFile(path, .{}) catch return GameError.OSError;
|
||||
defer file.close();
|
||||
|
||||
@@ -271,7 +271,7 @@ fn load_shader(device: *sdl.GPUDevice, path: []const u8, stage: c_uint) GameErro
|
||||
}) orelse return GameError.SdlError;
|
||||
}
|
||||
|
||||
fn create_depth_texture(device: *sdl.GPUDevice, width: u32, height: u32) GameError!*sdl.GPUTexture {
|
||||
fn createDepthTexture(device: *sdl.GPUDevice, width: u32, height: u32) GameError!*sdl.GPUTexture {
|
||||
return sdl.CreateGPUTexture(device, &.{
|
||||
.format = sdl.GPU_TEXTUREFORMAT_D16_UNORM,
|
||||
.layer_count_or_depth = 1,
|
||||
@@ -283,7 +283,7 @@ fn create_depth_texture(device: *sdl.GPUDevice, width: u32, height: u32) GameErr
|
||||
}) orelse return GameError.SdlError;
|
||||
}
|
||||
|
||||
fn create_texture(device: *sdl.GPUDevice, width: u32, height: u32, format: c_uint) GameError!*sdl.GPUTexture {
|
||||
fn createTexture(device: *sdl.GPUDevice, width: u32, height: u32, format: c_uint) GameError!*sdl.GPUTexture {
|
||||
return sdl.CreateGPUTexture(device, &.{
|
||||
.format = format,
|
||||
.layer_count_or_depth = 1,
|
||||
@@ -295,14 +295,14 @@ fn create_texture(device: *sdl.GPUDevice, width: u32, height: u32, format: c_uin
|
||||
}) orelse return GameError.SdlError;
|
||||
}
|
||||
|
||||
fn reset_textures(self: *Self, width: u32, height: u32) GameError!void {
|
||||
fn resetTextures(self: *Self, width: u32, height: u32) GameError!void {
|
||||
sdl.ReleaseGPUTexture(self.device, self.depth_texture);
|
||||
self.depth_texture = try create_depth_texture(self.device, width, height);
|
||||
self.depth_texture = try createDepthTexture(self.device, width, height);
|
||||
|
||||
const target_format = sdl.SDL_GetGPUSwapchainTextureFormat(self.device, self.window);
|
||||
|
||||
sdl.ReleaseGPUTexture(self.device, self.msaa_resolve);
|
||||
self.msaa_resolve = try create_texture(self.device, width, height, target_format);
|
||||
self.msaa_resolve = try createTexture(self.device, width, height, target_format);
|
||||
}
|
||||
|
||||
pub fn resize(self: *Self, width: u32, height: u32) void {
|
||||
|
@@ -2,14 +2,14 @@ const std = @import("std");
|
||||
const sdl = @import("sdl");
|
||||
const Game = @import("game.zig");
|
||||
|
||||
pub fn run_game() !void {
|
||||
pub fn runGame() !void {
|
||||
var game = try Game.init();
|
||||
defer game.deinit();
|
||||
try game.run();
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
run_game() catch |err| {
|
||||
runGame() catch |err| {
|
||||
switch (err) {
|
||||
error.SdlError => {
|
||||
std.debug.print("SDL Error:\n---\n{s}\n---\n", .{sdl.SDL_GetError()});
|
||||
|
Reference in New Issue
Block a user