diff --git a/build.zig b/build.zig index 7d2e679..98245ef 100644 --- a/build.zig +++ b/build.zig @@ -50,6 +50,12 @@ pub fn build(b: *std.Build) void { }); exe.root_module.addImport("app", app.module("app")); + const gtl = b.dependency("gtl", .{ + .target = target, + .optimize = optimize, + }); + exe.root_module.addImport("gtl", gtl.module("gtl")); + // // SYSTEM DEPENDENCIES // diff --git a/build.zig.zon b/build.zig.zon index 3152acc..b22a465 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -9,8 +9,12 @@ .dependencies = .{ .app = .{ - .url = "git+http://2.120.146.66:25569/abux/NYXA#93a4606c94f67b1f0a559c2efff3d8ff030b3300", - .hash = "app-0.0.0-AOV9_ea_AACRsUJ2lZqYiUkPed0op9qgFSWB6ihDSrll", + .url = "git+http://2.120.146.66:25569/abux/NYXA.git#4113f667319559c6bddb83c89042deb54e63b3de", + .hash = "app-0.0.0-AOV9_eK_AAA4ZmEGkvsFUM2xMTb0lYbzq4vSShMRnKZj", + }, + .gtl = .{ + .url = "git+http://2.120.146.66:25569/abux/GTL.git#e5cf30860977587188d810cc6524a5101312426c", + .hash = "gtl-0.0.0-e3QoJM0-AABINN7OX52K0SUL8XAqhRykEf4LTxf0wuat", }, }, diff --git a/src/assets/textures/player.png b/src/assets/textures/player.png index b2a6098..f5a8efd 100644 Binary files a/src/assets/textures/player.png and b/src/assets/textures/player.png differ diff --git a/src/component/material.zig b/src/component/material.zig index 2e7a0ab..aa9046a 100644 --- a/src/component/material.zig +++ b/src/component/material.zig @@ -1,9 +1,8 @@ -const Handle = @import("app").template.Handle; -const Texture = @import("../resource/textures.zig").Texture; +const Handle = @import("gtl").Handle; pub const Style = union(enum) { solid: @Vector(4, u8), - textured: Handle(Texture), + textured: Handle(undefined), }; style: Style, diff --git a/src/main.zig b/src/main.zig index fc54c5b..086642f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,7 +3,6 @@ const App = @import("app"); const plugin = @import("plugin/root.zig"); const resource = @import("resource/root.zig"); -const template = @import("template/root.zig"); pub fn main(init: std.process.Init) !void { // --- APP --- diff --git a/src/plugin/default.zig b/src/plugin/default.zig index b245ebf..40dfee0 100644 --- a/src/plugin/default.zig +++ b/src/plugin/default.zig @@ -19,7 +19,6 @@ fn build( ) !void { // --- LIFETIME --- try ctx.app.schedules.add(App.stage.init, system.core.init); - try ctx.app.schedules.add(App.stage.post_init, system.core.build); try ctx.app.schedules.add(App.stage.deinit, system.core.deinit); // --- FRAME --- @@ -27,11 +26,11 @@ fn build( try ctx.app.schedules.add(App.stage.end_frame, system.frame.endFrame); // --- UPDATE --- - try ctx.app.schedules.add(App.stage.inputs, system.frame.inputs); + try ctx.app.schedules.add(App.stage.inputs, system.inputs.pollEvents); try ctx.app.schedules.addMany(App.stage.update, &.{ system.frame.updateTimers, system.frame.updateTransforms, system.frame.cast, }); - try ctx.app.schedules.add(App.stage.draw, system.frame.draw); + try ctx.app.schedules.add(App.stage.draw, system.draw.draw); } diff --git a/src/resource/root.zig b/src/resource/root.zig index 1246d7e..6b2b330 100644 --- a/src/resource/root.zig +++ b/src/resource/root.zig @@ -3,6 +3,3 @@ pub const Window = @import("window.zig"); pub const Renderer = @import("renderer.zig"); pub const Frame = @import("frame.zig"); pub const Time = @import("time.zig"); - -// --- ASSETS --- -pub const Textures = @import("textures.zig"); diff --git a/src/resource/textures.zig b/src/resource/textures.zig deleted file mode 100644 index 049f283..0000000 --- a/src/resource/textures.zig +++ /dev/null @@ -1,132 +0,0 @@ -const std = @import("std"); -const sdl = @import("sdl"); -const Renderer = @import("../resource/renderer.zig"); -const Handle = @import("app").template.Handle; -const Storage = @import("../template/storage.zig").Storage; -const Self = @This(); - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub const Size = union(enum) { - source, - fixed: [2]u32, -}; - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub const Filter = enum { - linear, - nearest, -}; - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub const Texture = struct { - path: []const u8, - size: [2]u32 = .{ 0, 0 }, - filter: Filter = .nearest, -}; - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub const GPUTexture = struct { - raw: *sdl.SDL_Texture, -}; - -// -// FIELDS -// - -textures: Storage(Texture, GPUTexture), - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn init(alloc: std.mem.Allocator) Self { - return .{ - .textures = .init(alloc), - }; -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn deinit(self: *Self) void { - self.textures.deinit(); -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn load( - self: *Self, - name: []const u8, - path: []const u8, -) !Handle(Texture) { - return try self.textures.put(name, .{ - .path = path, - }); -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn get( - self: *Self, - value: Handle(Texture), -) ?*GPUTexture { - const record = self.textures.get(value) orelse return null; - return if (record.gpu) |*v| v else null; -} - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn build( - self: *Self, - renderer: *const Renderer, -) !void { - for (self.textures.items()) |*record| { - // --- SKIP IF BUILD --- - if (record.gpu != null) continue; - - // --- LOAD SURFACE --- - const surface = sdl.IMG_Load( - record.cpu.path.ptr, - ) orelse { - std.debug.print("\x1b[31mFailed to load texture:\x1b[0m {s}\n", .{sdl.SDL_GetError()}); - return error.Texture; - }; - defer sdl.SDL_DestroySurface(surface); - - // --- CREATE TEXTURE --- - const texture = sdl.SDL_CreateTextureFromSurface( - renderer.raw, - surface, - ) orelse return error.Texture; - - // --- SET FILTER --- - _ = sdl.SDL_SetTextureScaleMode(texture, switch (record.cpu.filter) { - .linear => sdl.SDL_SCALEMODE_LINEAR, - .nearest => sdl.SDL_SCALEMODE_PIXELART, - }); - - // --- ASSIGN CPU --- - record.cpu.size = .{ - @intCast(surface.*.w), - @intCast(surface.*.h), - }; - - // --- ASSIGN GPU --- - record.gpu = .{ - .raw = texture, - }; - } - - // --- DEBUG --- - var it = self.textures.lookup.iterator(); - while (it.next()) |entry| { - const record = self.textures.get(entry.key_ptr.*) orelse continue; - - std.debug.print("\x1b[35m┏━ TEXTURE\n", .{}); - std.debug.print("\x1b[35m┃\x1b[0m Name: \"{s}\"\n", .{entry.value_ptr.*}); - std.debug.print("\x1b[35m┃\x1b[0m Path: \"{s}\"\n", .{record.cpu.path}); - std.debug.print("\x1b[35m┃\x1b[0m Size: {}x{}\n", .{ record.cpu.size[0], record.cpu.size[1] }); - std.debug.print("\x1b[35m┗━\x1b[0m\n", .{}); - } -} diff --git a/src/system/core.zig b/src/system/core.zig index 72322ee..0cb1920 100644 --- a/src/system/core.zig +++ b/src/system/core.zig @@ -39,9 +39,6 @@ pub fn init(ctx: App.scheduler.Context) !void { }); try ctx.app.resources.set(resource.Frame{}); - // --- ASSETS --- - const textures = try ctx.app.resources.getOrSet(resource.Textures.init(ctx.app.alloc)); - // --- DEBUG --- std.debug.print("\x1b[35m┏━ WINDOW\n", .{}); std.debug.print("\x1b[35m┃\x1b[0m Title: \"{s}\"\n", .{window.title}); @@ -62,9 +59,9 @@ pub fn init(ctx: App.scheduler.Context) !void { component.Friction{}, component.Velocity{}, - component.Transform{ .size = .{ 200, 200 } }, + component.Transform{ .size = .{ 64, 64 } }, component.Material{ .style = .{ - .textured = try textures.load("Player", "src/assets/textures/player.png"), + .solid = .{ 200, 20, 20, 255 }, } }, component.CameraTarget{}, }); @@ -84,24 +81,9 @@ pub fn deinit(ctx: App.scheduler.Context) !void { const res = try ctx.app.resources.getMany(struct { window: *resource.Window, renderer: *resource.Renderer, - textures: *resource.Textures, }); - res.textures.deinit(); sdl.SDL_DestroyRenderer(res.renderer.raw); sdl.SDL_DestroyWindow(res.window.raw); sdl.SDL_Quit(); } - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn build(ctx: App.scheduler.Context) !void { - const res = try ctx.app.resources.getMany(struct { - renderer: *resource.Renderer, - textures: *resource.Textures, - }); - - try res.textures.build( - res.renderer, - ); -} diff --git a/src/system/draw.zig b/src/system/draw.zig new file mode 100644 index 0000000..dfa2dc2 --- /dev/null +++ b/src/system/draw.zig @@ -0,0 +1,66 @@ +const std = @import("std"); +const sdl = @import("sdl"); +const App = @import("app"); + +const component = @import("../component/root.zig"); +const resource = @import("../resource/root.zig"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn draw(ctx: App.scheduler.Context) !void { + const res = try ctx.app.resources.getMany(struct { + window: *resource.Window, + renderer: *resource.Renderer, + frame: *resource.Frame, + }); + + // --- SET CLEAR COLOR --- + _ = sdl.SDL_SetRenderDrawColor( + res.renderer.raw, + 10, + 10, + 10, + 255, + ); + + // --- DRAW --- + _ = sdl.SDL_RenderClear(res.renderer.raw); + defer _ = sdl.SDL_RenderPresent(res.renderer.raw); + + { // DRAW + var query = ctx.app.world.query(struct { + transform: *component.Transform, + material: *component.Material, + camera_target: ?*component.CameraTarget, + }, .{}); + while (query.next()) |e| { + const camera = if (res.frame.camera) |*v| v else continue; + + // --- SHAPE --- + var rect: sdl.SDL_FRect = .{ + .x = e.transform.pos[0] + camera.transform.pos[0], + .y = e.transform.pos[1] + camera.transform.pos[1], + .w = e.transform.size[0], + .h = e.transform.size[1], + }; + + // --- MATERIAL --- + switch (e.material.style) { + .solid => |albedo| { + _ = sdl.SDL_SetRenderDrawColor(res.renderer.raw, albedo[0], albedo[1], albedo[2], albedo[3]); + _ = sdl.SDL_RenderFillRect(res.renderer.raw, &rect); + }, + else => {}, + // .textured => |handle| { + // const gpu = res.textures.get(handle) orelse continue; + // _ = sdl.SDL_RenderTexture( + // res.renderer.raw, + // gpu.raw, + // null, + // &rect, + // ); + // }, + } + } + } +} diff --git a/src/system/frame.zig b/src/system/frame.zig index 6b6606e..3557452 100644 --- a/src/system/frame.zig +++ b/src/system/frame.zig @@ -5,39 +5,6 @@ const App = @import("app"); const component = @import("../component/root.zig"); const resource = @import("../resource/root.zig"); -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn inputs(ctx: App.scheduler.Context) !void { - const res = try ctx.app.resources.getMany(struct { - window: *resource.Window, - renderer: *resource.Renderer, - }); - _ = res; - - // --- POLL EVENTS --- - var raw_event: sdl.SDL_Event = undefined; - while (sdl.SDL_PollEvent(&raw_event)) { - switch (raw_event.type) { - sdl.SDL_EVENT_QUIT => try ctx.app.events.send(App.event.AppExit{}), - else => {}, - } - } - - const keyboard: [*c]const bool = sdl.SDL_GetKeyboardState(null); - - var query = ctx.app.world.query(struct { - player: *component.Player, - velocity: *component.Velocity, - }, .{}); - while (query.next()) |e| { - const speed: f32 = 100.0; - if (keyboard[sdl.SDL_SCANCODE_W]) e.velocity.value[1] = -speed; - if (keyboard[sdl.SDL_SCANCODE_S]) e.velocity.value[1] = speed; - if (keyboard[sdl.SDL_SCANCODE_A]) e.velocity.value[0] = -speed; - if (keyboard[sdl.SDL_SCANCODE_D]) e.velocity.value[0] = speed; - } -} - /// ---------------------------------------------------- /// ---------------------------------------------------- pub fn beginFrame(ctx: App.scheduler.Context) !void { @@ -215,63 +182,3 @@ pub fn cast(ctx: App.scheduler.Context) !void { e.caster.cooldown; } } - -/// ---------------------------------------------------- -/// ---------------------------------------------------- -pub fn draw(ctx: App.scheduler.Context) !void { - const res = try ctx.app.resources.getMany(struct { - window: *resource.Window, - renderer: *resource.Renderer, - textures: *resource.Textures, - frame: *resource.Frame, - }); - - // --- SET CLEAR COLOR --- - _ = sdl.SDL_SetRenderDrawColor( - res.renderer.raw, - 10, - 10, - 10, - 255, - ); - - // --- DRAW --- - _ = sdl.SDL_RenderClear(res.renderer.raw); - defer _ = sdl.SDL_RenderPresent(res.renderer.raw); - - { // DRAW - var query = ctx.app.world.query(struct { - transform: *component.Transform, - material: *component.Material, - camera_target: ?*component.CameraTarget, - }, .{}); - while (query.next()) |e| { - const camera = if (res.frame.camera) |*v| v else continue; - - // --- SHAPE --- - var rect: sdl.SDL_FRect = .{ - .x = e.transform.pos[0] + camera.transform.pos[0], - .y = e.transform.pos[1] + camera.transform.pos[1], - .w = e.transform.size[0], - .h = e.transform.size[1], - }; - - // --- MATERIAL --- - switch (e.material.style) { - .solid => |albedo| { - _ = sdl.SDL_SetRenderDrawColor(res.renderer.raw, albedo[0], albedo[1], albedo[2], albedo[3]); - _ = sdl.SDL_RenderFillRect(res.renderer.raw, &rect); - }, - .textured => |handle| { - const gpu = res.textures.get(handle) orelse continue; - _ = sdl.SDL_RenderTexture( - res.renderer.raw, - gpu.raw, - null, - &rect, - ); - }, - } - } - } -} diff --git a/src/system/inputs.zig b/src/system/inputs.zig new file mode 100644 index 0000000..250f4cb --- /dev/null +++ b/src/system/inputs.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const sdl = @import("sdl"); +const App = @import("app"); + +const component = @import("../component/root.zig"); +const resource = @import("../resource/root.zig"); + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn pollEvents(ctx: App.scheduler.Context) !void { + const res = try ctx.app.resources.getMany(struct { + window: *resource.Window, + renderer: *resource.Renderer, + }); + _ = res; + + // --- POLL EVENTS --- + var raw_event: sdl.SDL_Event = undefined; + while (sdl.SDL_PollEvent(&raw_event)) { + switch (raw_event.type) { + sdl.SDL_EVENT_QUIT => try ctx.app.events.send(App.event.AppExit{}), + else => {}, + } + } + + const keyboard: [*c]const bool = sdl.SDL_GetKeyboardState(null); + + var query = ctx.app.world.query(struct { + player: *component.Player, + velocity: *component.Velocity, + }, .{}); + while (query.next()) |e| { + const speed: f32 = 100.0; + if (keyboard[sdl.SDL_SCANCODE_W]) e.velocity.value[1] = -speed; + if (keyboard[sdl.SDL_SCANCODE_S]) e.velocity.value[1] = speed; + if (keyboard[sdl.SDL_SCANCODE_A]) e.velocity.value[0] = -speed; + if (keyboard[sdl.SDL_SCANCODE_D]) e.velocity.value[0] = speed; + } +} diff --git a/src/system/root.zig b/src/system/root.zig index 0329ef8..9ef80d5 100644 --- a/src/system/root.zig +++ b/src/system/root.zig @@ -1,2 +1,4 @@ pub const core = @import("core.zig"); pub const frame = @import("frame.zig"); +pub const inputs = @import("inputs.zig"); +pub const draw = @import("draw.zig"); diff --git a/src/template/root.zig b/src/template/root.zig deleted file mode 100644 index 883557a..0000000 --- a/src/template/root.zig +++ /dev/null @@ -1 +0,0 @@ -pub const Storage = @import("storage.zig").Storage; diff --git a/src/template/storage.zig b/src/template/storage.zig deleted file mode 100644 index 1932ed5..0000000 --- a/src/template/storage.zig +++ /dev/null @@ -1,85 +0,0 @@ -const std = @import("std"); -const App = @import("app"); -const Handle = App.template.Handle; - -pub fn Storage( - comptime CPU: type, - comptime GPU: type, -) type { - return struct { - const Self = @This(); - - /// ---------------------------------------------------- - /// ---------------------------------------------------- - pub const Record = struct { - cpu: CPU, - gpu: ?GPU = null, - }; - - // - // FIELDS - // - - data: std.ArrayList(Record), - lookup: std.AutoHashMap(Handle(CPU), []const u8), - alloc: std.mem.Allocator, - - /// ---------------------------------------------------- - /// ---------------------------------------------------- - pub fn init(alloc: std.mem.Allocator) Self { - return .{ - .data = .empty, - .lookup = .init(alloc), - .alloc = alloc, - }; - } - - /// ---------------------------------------------------- - /// ---------------------------------------------------- - pub fn deinit(self: *Self) void { - self.data.deinit(self.alloc); - self.lookup.deinit(); - } - - /// ---------------------------------------------------- - /// ---------------------------------------------------- - pub fn put( - self: *Self, - name: []const u8, - value: CPU, - ) !Handle(CPU) { - // --- HANDLE --- - const handle: Handle(CPU) = .{ - .idx = @intCast(self.data.items.len), - }; - - // --- ADD DATA --- - try self.data.append(self.alloc, .{ .cpu = value }); - errdefer _ = self.data.swapRemove(self.data.items.len - 1); - - // --- ADD LOOKUP --- - try self.lookup.put( - handle, - name, - ); - - return handle; - } - - /// ---------------------------------------------------- - /// ---------------------------------------------------- - pub fn get( - self: *Self, - handle: Handle(CPU), - ) ?*Record { - if (!self.lookup.contains(handle)) return null; - return &self.data.items[@intCast(handle.idx)]; - } - - /// ---------------------------------------------------- - /// ---------------------------------------------------- - pub fn items(self: *Self) []Record { - return self.data.items; - } - }; -}