decouple engine and scene

stable
Ivory 2024-08-23 17:22:01 -04:00
parent 2ebfdb8db0
commit 05dfdca6c6
4 changed files with 31 additions and 11 deletions

View File

@ -1,3 +1,5 @@
const Engine = @This();
const std = @import("std");
const Recti = @import("geometry/Recti.zig");
const Matrix4f = @import("geometry/Matrix4f.zig");
@ -20,7 +22,13 @@ const HashMap = std.HashMap;
// render: fn () void,
// update: fn (f32) void,
// };
current_scene: Scene,
pub fn create(initial_scene: Scene) Engine {
return .{
.current_scene = initial_scene
};
}
fn errorCallback(err: c_int, desc_c: [*c]const u8) callconv(.C) void {
const desc: *const u8 = @ptrCast(desc_c);
@ -28,7 +36,7 @@ fn errorCallback(err: c_int, desc_c: [*c]const u8) callconv(.C) void {
}
// export fn run(game: *const IGame) !void {
pub fn run() !void {
pub fn run(self: *Engine) !void {
// in case of errors, set callback early!
// for some reason this returns an error function as well??
@ -87,19 +95,17 @@ pub fn run() !void {
const projection = Matrix4f.orthographic(0, @floatFromInt(width), @floatFromInt(height), 0, 0, 100);
var scene = Scene.new();
defer scene.destroy();
try scene.start();
// run the main loop
while (c.glfwWindowShouldClose(window) == 0) {
shaders.set_projection_matrix(&projection);
c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT);
scene.draw();
self.current_scene.draw();
// game.render();
// game.update(1.0);
c.glfwSwapBuffers(window);
c.glfwPollEvents();
self.current_scene.update(0.0042);
}
}

View File

@ -11,7 +11,7 @@ const ArrayList = std.ArrayList;
entities: ArrayList(Entity),
pub fn new() Scene {
pub fn create() Scene {
const e: ArrayList(Entity) = ArrayList(Entity).init(std.heap.page_allocator);
return .{
.entities = e

View File

@ -1,10 +1,11 @@
const std = @import("std");
const engine = @import("engine.zig");
const Engine = @import("Engine.zig");
// const assets = @import("assets.zig");
const scenes = @import("scenes.zig");
pub fn main() !void {
// engine.preload
// engine.preload(assets.load);
var engine = Engine.create(try scenes.game());
try engine.run();
}

13
src/scenes.zig 100644
View File

@ -0,0 +1,13 @@
const Scene = @import("Scene.zig");
const Terrain = @import("Terrain.zig");
pub fn game() !Scene {
var scene = Scene.create();
// first try is for allocating more memory in entities
// second try is for allocating terrain on the heap...
try scene.add(try Terrain.create());
return scene;
}