diff --git a/build.zig.zon b/build.zig.zon index b22a465..bad24b0 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -13,8 +13,8 @@ .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", + .url = "git+http://2.120.146.66:25569/abux/GTL.git#b87f315b57788ee81af592fc85532f751951966d", + .hash = "gtl-0.0.0-e3QoJJZIAAB7ni5BsH4mVVuBkVfEbJ4lPNb38Dlm-kUg", }, }, diff --git a/src/component/material.zig b/src/component/material.zig index aa9046a..dbd420a 100644 --- a/src/component/material.zig +++ b/src/component/material.zig @@ -1,8 +1,9 @@ const Handle = @import("gtl").Handle; +const Texture = @import("../resource/textures.zig").Texture; pub const Style = union(enum) { solid: @Vector(4, u8), - textured: Handle(undefined), + textured: Handle(Texture), }; style: Style, diff --git a/src/resource/root.zig b/src/resource/root.zig index 6b2b330..759a886 100644 --- a/src/resource/root.zig +++ b/src/resource/root.zig @@ -3,3 +3,4 @@ pub const Window = @import("window.zig"); pub const Renderer = @import("renderer.zig"); pub const Frame = @import("frame.zig"); pub const Time = @import("time.zig"); +pub const Textures = @import("textures.zig"); diff --git a/src/resource/textures.zig b/src/resource/textures.zig new file mode 100644 index 0000000..82635dc --- /dev/null +++ b/src/resource/textures.zig @@ -0,0 +1,69 @@ +// --- # --- +const std = @import("std"); +const gtl = @import("gtl"); +const sdl = @import("sdl"); +const Self = @This(); + +// +// +// + +pub const Player = struct {}; + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub const Texture = struct { id: u64 }; + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub const GPUTexture = struct { + path: []const u8, + raw: ?*sdl.SDL_Texture, + size: [2]i32 = .{ 0, 0 }, +}; + +// +// FIELDS +// + +data: gtl.KeyedSlotMap(Texture, GPUTexture), + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn init(alloc: std.mem.Allocator) Self { + return .{ + .data = .init(alloc), + }; +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn deinit(self: *Self) void { + for (self.data.items()) |entry| { + if (entry.value.raw) |raw| { + sdl.SDL_DestroyTexture(raw); + } + } + self.data.deinit(); +} + +/// ---------------------------------------------------- +/// ---------------------------------------------------- +pub fn load( + self: *Self, + comptime T: type, + path: []const u8, +) !gtl.Handle(Texture) { + // --- ID --- + const id = gtl.TypeID(T); + + // --- # --- + if (self.data.getHandle(.{ .id = id })) |h| { + return h; + } + + return try self.data.put(.{ .id = id }, .{ + .path = path, + .raw = null, + }); +} diff --git a/src/system/core.zig b/src/system/core.zig index 0cb1920..c7d6061 100644 --- a/src/system/core.zig +++ b/src/system/core.zig @@ -38,6 +38,7 @@ pub fn init(ctx: App.scheduler.Context) !void { .frequency = sdl.SDL_GetPerformanceFrequency(), }); try ctx.app.resources.set(resource.Frame{}); + const textures = try ctx.app.resources.getOrSet(resource.Textures.init(ctx.app.alloc)); // --- DEBUG --- std.debug.print("\x1b[35m┏━ WINDOW\n", .{}); @@ -61,7 +62,7 @@ pub fn init(ctx: App.scheduler.Context) !void { component.Velocity{}, component.Transform{ .size = .{ 64, 64 } }, component.Material{ .style = .{ - .solid = .{ 200, 20, 20, 255 }, + .textured = try textures.load(resource.Textures.Player, "src/assets/textures/player.png"), } }, component.CameraTarget{}, }); @@ -81,8 +82,10 @@ 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(); diff --git a/src/system/draw.zig b/src/system/draw.zig index dfa2dc2..a0d1324 100644 --- a/src/system/draw.zig +++ b/src/system/draw.zig @@ -12,6 +12,7 @@ pub fn draw(ctx: App.scheduler.Context) !void { window: *resource.Window, renderer: *resource.Renderer, frame: *resource.Frame, + textures: *resource.Textures, }); // --- SET CLEAR COLOR --- @@ -50,16 +51,17 @@ pub fn draw(ctx: App.scheduler.Context) !void { _ = 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, - // ); - // }, + .textured => |handle| { + const gpu = res.textures.data.get(handle) orelse continue; + const raw = if (gpu.raw) |*v| v else continue; + + _ = sdl.SDL_RenderTexture( + res.renderer.raw, + raw.*, + null, + &rect, + ); + }, } } } diff --git a/src/system/frame.zig b/src/system/frame.zig index 3557452..2297452 100644 --- a/src/system/frame.zig +++ b/src/system/frame.zig @@ -1,5 +1,6 @@ const std = @import("std"); const sdl = @import("sdl"); +const gtl = @import("gtl"); const App = @import("app"); const component = @import("../component/root.zig"); @@ -12,6 +13,9 @@ pub fn beginFrame(ctx: App.scheduler.Context) !void { window: *resource.Window, frame: *resource.Frame, time: *resource.Time, + + textures: *resource.Textures, + renderer: *resource.Renderer, }); // @@ -63,6 +67,43 @@ pub fn beginFrame(ctx: App.scheduler.Context) !void { }; } } + + { // BUILD TEXTURES + for (res.textures.data.items()) |*entry| { + // --- SKIP IF ALREADY BUILD --- + if (entry.value.raw != null) continue; + + // --- SURFACE --- + const surface = sdl.IMG_Load(entry.value.path.ptr) orelse { + gtl.log.err("Texture: {s}", .{sdl.SDL_GetError()}); + continue; + }; + defer sdl.SDL_DestroySurface(surface); + + // --- TEXTURE --- + const raw = sdl.SDL_CreateTextureFromSurface( + res.renderer.raw, + surface, + ) orelse return error.Texture; + + // --- SET TEXTURE FILTER --- + _ = sdl.SDL_SetTextureScaleMode( + entry.value.raw, + sdl.SDL_SCALEMODE_PIXELART, + ); + + // --- ASSIGN --- + entry.value.raw = raw; + entry.value.size = .{ raw.*.w, raw.*.h }; + + // --- DEBUG --- + std.debug.print("{s}┏━ TEXTURE{s}\n", .{ gtl.ansi.purple, gtl.ansi.reset }); + std.debug.print("{s}┃{s} Path: \"{s}\"\n", .{ gtl.ansi.purple, gtl.ansi.reset, entry.value.path }); + std.debug.print("{s}┃{s} Size: {}x{}\n", .{ gtl.ansi.purple, gtl.ansi.reset, raw.*.w, raw.*.h }); + std.debug.print("{s}┃{s} Filter: PixelArt\n", .{ gtl.ansi.purple, gtl.ansi.reset }); + std.debug.print("{s}┗━{s}\n", .{ gtl.ansi.purple, gtl.ansi.reset }); + } + } } /// ---------------------------------------------------- @@ -147,6 +188,7 @@ pub fn updateTimers(ctx: App.scheduler.Context) !void { pub fn cast(ctx: App.scheduler.Context) !void { const res = try ctx.app.resources.getMany(struct { time: *resource.Time, + textures: *resource.Textures, }); // --- QUERY --- @@ -172,7 +214,10 @@ pub fn cast(ctx: App.scheduler.Context) !void { .value = .{ 400, 0 }, }, component.Material{ - .style = .{ .solid = .{ 200, 20, 20, 255 } }, + .style = .{ + // .solid = .{ 200, 20, 20, 255 }, + .textured = try res.textures.load(resource.Textures.Player, "src/assets/textures/player.png"), + }, }, component.Projectile{}, });