Textures
This commit is contained in:
parent
21667faac9
commit
c8a735314e
7 changed files with 136 additions and 15 deletions
|
|
@ -13,8 +13,8 @@
|
||||||
.hash = "app-0.0.0-AOV9_eK_AAA4ZmEGkvsFUM2xMTb0lYbzq4vSShMRnKZj",
|
.hash = "app-0.0.0-AOV9_eK_AAA4ZmEGkvsFUM2xMTb0lYbzq4vSShMRnKZj",
|
||||||
},
|
},
|
||||||
.gtl = .{
|
.gtl = .{
|
||||||
.url = "git+http://2.120.146.66:25569/abux/GTL.git#e5cf30860977587188d810cc6524a5101312426c",
|
.url = "git+http://2.120.146.66:25569/abux/GTL.git#b87f315b57788ee81af592fc85532f751951966d",
|
||||||
.hash = "gtl-0.0.0-e3QoJM0-AABINN7OX52K0SUL8XAqhRykEf4LTxf0wuat",
|
.hash = "gtl-0.0.0-e3QoJJZIAAB7ni5BsH4mVVuBkVfEbJ4lPNb38Dlm-kUg",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
const Handle = @import("gtl").Handle;
|
const Handle = @import("gtl").Handle;
|
||||||
|
const Texture = @import("../resource/textures.zig").Texture;
|
||||||
|
|
||||||
pub const Style = union(enum) {
|
pub const Style = union(enum) {
|
||||||
solid: @Vector(4, u8),
|
solid: @Vector(4, u8),
|
||||||
textured: Handle(undefined),
|
textured: Handle(Texture),
|
||||||
};
|
};
|
||||||
|
|
||||||
style: Style,
|
style: Style,
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,4 @@ pub const Window = @import("window.zig");
|
||||||
pub const Renderer = @import("renderer.zig");
|
pub const Renderer = @import("renderer.zig");
|
||||||
pub const Frame = @import("frame.zig");
|
pub const Frame = @import("frame.zig");
|
||||||
pub const Time = @import("time.zig");
|
pub const Time = @import("time.zig");
|
||||||
|
pub const Textures = @import("textures.zig");
|
||||||
|
|
|
||||||
69
src/resource/textures.zig
Normal file
69
src/resource/textures.zig
Normal file
|
|
@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -38,6 +38,7 @@ pub fn init(ctx: App.scheduler.Context) !void {
|
||||||
.frequency = sdl.SDL_GetPerformanceFrequency(),
|
.frequency = sdl.SDL_GetPerformanceFrequency(),
|
||||||
});
|
});
|
||||||
try ctx.app.resources.set(resource.Frame{});
|
try ctx.app.resources.set(resource.Frame{});
|
||||||
|
const textures = try ctx.app.resources.getOrSet(resource.Textures.init(ctx.app.alloc));
|
||||||
|
|
||||||
// --- DEBUG ---
|
// --- DEBUG ---
|
||||||
std.debug.print("\x1b[35m┏━ WINDOW\n", .{});
|
std.debug.print("\x1b[35m┏━ WINDOW\n", .{});
|
||||||
|
|
@ -61,7 +62,7 @@ pub fn init(ctx: App.scheduler.Context) !void {
|
||||||
component.Velocity{},
|
component.Velocity{},
|
||||||
component.Transform{ .size = .{ 64, 64 } },
|
component.Transform{ .size = .{ 64, 64 } },
|
||||||
component.Material{ .style = .{
|
component.Material{ .style = .{
|
||||||
.solid = .{ 200, 20, 20, 255 },
|
.textured = try textures.load(resource.Textures.Player, "src/assets/textures/player.png"),
|
||||||
} },
|
} },
|
||||||
component.CameraTarget{},
|
component.CameraTarget{},
|
||||||
});
|
});
|
||||||
|
|
@ -81,8 +82,10 @@ pub fn deinit(ctx: App.scheduler.Context) !void {
|
||||||
const res = try ctx.app.resources.getMany(struct {
|
const res = try ctx.app.resources.getMany(struct {
|
||||||
window: *resource.Window,
|
window: *resource.Window,
|
||||||
renderer: *resource.Renderer,
|
renderer: *resource.Renderer,
|
||||||
|
textures: *resource.Textures,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
res.textures.deinit();
|
||||||
sdl.SDL_DestroyRenderer(res.renderer.raw);
|
sdl.SDL_DestroyRenderer(res.renderer.raw);
|
||||||
sdl.SDL_DestroyWindow(res.window.raw);
|
sdl.SDL_DestroyWindow(res.window.raw);
|
||||||
sdl.SDL_Quit();
|
sdl.SDL_Quit();
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ pub fn draw(ctx: App.scheduler.Context) !void {
|
||||||
window: *resource.Window,
|
window: *resource.Window,
|
||||||
renderer: *resource.Renderer,
|
renderer: *resource.Renderer,
|
||||||
frame: *resource.Frame,
|
frame: *resource.Frame,
|
||||||
|
textures: *resource.Textures,
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- SET CLEAR COLOR ---
|
// --- 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_SetRenderDrawColor(res.renderer.raw, albedo[0], albedo[1], albedo[2], albedo[3]);
|
||||||
_ = sdl.SDL_RenderFillRect(res.renderer.raw, &rect);
|
_ = sdl.SDL_RenderFillRect(res.renderer.raw, &rect);
|
||||||
},
|
},
|
||||||
else => {},
|
.textured => |handle| {
|
||||||
// .textured => |handle| {
|
const gpu = res.textures.data.get(handle) orelse continue;
|
||||||
// const gpu = res.textures.get(handle) orelse continue;
|
const raw = if (gpu.raw) |*v| v else continue;
|
||||||
// _ = sdl.SDL_RenderTexture(
|
|
||||||
// res.renderer.raw,
|
_ = sdl.SDL_RenderTexture(
|
||||||
// gpu.raw,
|
res.renderer.raw,
|
||||||
// null,
|
raw.*,
|
||||||
// &rect,
|
null,
|
||||||
// );
|
&rect,
|
||||||
// },
|
);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const sdl = @import("sdl");
|
const sdl = @import("sdl");
|
||||||
|
const gtl = @import("gtl");
|
||||||
const App = @import("app");
|
const App = @import("app");
|
||||||
|
|
||||||
const component = @import("../component/root.zig");
|
const component = @import("../component/root.zig");
|
||||||
|
|
@ -12,6 +13,9 @@ pub fn beginFrame(ctx: App.scheduler.Context) !void {
|
||||||
window: *resource.Window,
|
window: *resource.Window,
|
||||||
frame: *resource.Frame,
|
frame: *resource.Frame,
|
||||||
time: *resource.Time,
|
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 {
|
pub fn cast(ctx: App.scheduler.Context) !void {
|
||||||
const res = try ctx.app.resources.getMany(struct {
|
const res = try ctx.app.resources.getMany(struct {
|
||||||
time: *resource.Time,
|
time: *resource.Time,
|
||||||
|
textures: *resource.Textures,
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- QUERY ---
|
// --- QUERY ---
|
||||||
|
|
@ -172,7 +214,10 @@ pub fn cast(ctx: App.scheduler.Context) !void {
|
||||||
.value = .{ 400, 0 },
|
.value = .{ 400, 0 },
|
||||||
},
|
},
|
||||||
component.Material{
|
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{},
|
component.Projectile{},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue