From eab2c638550a4f09f44c2b693b9eaa2a50a92323 Mon Sep 17 00:00:00 2001 From: Ivory Date: Wed, 21 Aug 2024 17:40:04 -0400 Subject: [PATCH] create assets and split scene --- src/Scene.zig | 20 ++++++++++++++++++++ src/assets.zig | 24 ++++++++++++++++++++++++ src/engine.zig | 28 ++++++---------------------- src/main.zig | 3 +++ 4 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 src/Scene.zig create mode 100644 src/assets.zig diff --git a/src/Scene.zig b/src/Scene.zig new file mode 100644 index 0000000..721b390 --- /dev/null +++ b/src/Scene.zig @@ -0,0 +1,20 @@ +const assets = @import("assets.zig"); +const Recti = @import("geometry/Recti.zig"); +const Layer = @import("Layer.zig"); +const Color = @import("Color.zig"); +const std = @import("std"); + +const Scene = @This(); +// const DrawFunction = fn() void; +// const UpdateFunction = fn(f32) void; + +// draw: DrawFunction, +// update: + +pub fn draw() void { + assets.heart.draw(&Recti.from_xywh(120, 100, 32, 64), Layer.ENTITIES, Color.LIGHT_BLUE); + assets.heart.draw(&Recti.from_xywh(100, 100, 32, 64), Layer.FLOOR, Color.WHITE); + assets.heart.draw(&Recti.from_xywh(80, 100, 32, 64), Layer.ENTITIES, Color.INDIGO); +} + + diff --git a/src/assets.zig b/src/assets.zig new file mode 100644 index 0000000..4a58470 --- /dev/null +++ b/src/assets.zig @@ -0,0 +1,24 @@ +const Texture = @import("Texture.zig"); +const Sprite = @import("Sprite.zig"); +const Recti = @import("geometry/Recti.zig"); +const std = @import("std"); + +const Assets = struct { + tree: Sprite, + heart: Sprite, +}; + +var assets: Assets = undefined; +var texture: Texture = undefined; + +pub fn load() !void { + texture = try Texture.create("textures.png"); + + assets = .{ + .tree = Sprite.create(&texture, Recti.from_xywh(8*27, 8*4, 8, 16)), + .heart = Sprite.create(&texture, Recti.from_xywh(8*27, 8*4, 8, 16)), + }; +} + +pub const tree: *const Sprite = &assets.tree; +pub const heart: *const Sprite = &assets.heart; diff --git a/src/engine.zig b/src/engine.zig index 74c444f..bc442a8 100644 --- a/src/engine.zig +++ b/src/engine.zig @@ -6,6 +6,8 @@ const Texture = @import("Texture.zig"); const Sprite = @import("Sprite.zig"); const Layer = @import("Layer.zig"); const Color = @import("Color.zig"); +const Scene = @import("Scene.zig"); +const assets = @import("assets.zig"); const c = @cImport({ @cInclude("glad/glad.h"); @@ -66,9 +68,8 @@ pub fn run() !void { // compile shaders... try shaders.load(); - const texture = try Texture.create("textures.png"); - // texture.bind(c.GL_TEXTURE0); - const sprite = Sprite.create(&texture, Recti.from_xywh(8*27, 8*4, 8, 16)); + try assets.load(); + const clearBrightness: f32 = 0.09; c.glClearColor(clearBrightness, clearBrightness, clearBrightness, 1.0); @@ -89,28 +90,11 @@ pub fn run() !void { // run the main loop while (c.glfwWindowShouldClose(window) == 0) { shaders.set_projection_matrix(&projection); - - // std.debug.print("size: {d} x {d}\n", .{width, height}); - // c.glViewport(0, 0, width, height); c.glClear(c.GL_COLOR_BUFFER_BIT | c.GL_DEPTH_BUFFER_BIT); - - // c.glBegin(c.GL_TRIANGLES); - // - // c.glVertex2f(0, 1); - // // c.glVertexAttrib4f(1, 1, 0, 0, 1); - // c.glVertex2f(-1, -1); - // // c.glVertexAttrib4f(1, 0, 1, 0, 1); - // c.glVertex2f(1, -1); - // // c.glVertexAttrib4f(1, 0, 0, 1, 1); - // - // c.glEnd(); - sprite.draw(&Recti.from_xywh(120, 100, 32, 64), Layer.ENTITIES, Color.LIGHT_BLUE); - sprite.draw(&Recti.from_xywh(100, 100, 32, 64), Layer.FLOOR, Color.WHITE); - sprite.draw(&Recti.from_xywh(80, 100, 32, 64), Layer.ENTITIES, Color.INDIGO); - // draw.rect(&geometry.Rect.from_xywh(100, 100, 256, 256), draw.Layer.FLOOR); - + Scene.draw(); // game.render(); // game.update(1.0); + c.glfwSwapBuffers(window); c.glfwPollEvents(); } diff --git a/src/main.zig b/src/main.zig index efbc34a..3bdbdf2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,5 +3,8 @@ const std = @import("std"); const engine = @import("engine.zig"); pub fn main() !void { + + // engine.preload + try engine.run(); }