Zig 0.14 => Zig 0.15, STB Image => SDL3 Image
This commit is contained in:
39
build.zig
39
build.zig
@@ -6,6 +6,10 @@ const SHADERS: []const []const u8 = &.{
|
||||
"data/shaders/basic.vert",
|
||||
"data/shaders/basic.frag",
|
||||
};
|
||||
const SDL_LIBS: []const []const u8 = &.{
|
||||
"SDL3",
|
||||
"SDL3_image",
|
||||
};
|
||||
|
||||
pub fn build(b: *Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
@@ -56,16 +60,16 @@ fn stepBuildClient(
|
||||
) *Build.Step.Compile {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "tabletop_client",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/client.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
|
||||
exe.root_module.addImport("sdl", sdl_module);
|
||||
exe.step.dependOn(sdl_step);
|
||||
|
||||
exe.addIncludePath(b.path("lib/clibs"));
|
||||
|
||||
return exe;
|
||||
}
|
||||
|
||||
@@ -76,9 +80,11 @@ fn stepBuildServer(
|
||||
) *Build.Step.Compile {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "tabletop_server",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/server.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
|
||||
return exe;
|
||||
@@ -93,16 +99,16 @@ fn stepBuildOffline(
|
||||
) *Build.Step.Compile {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "tabletop",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/offline.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
}),
|
||||
});
|
||||
|
||||
exe.root_module.addImport("sdl", sdl_module);
|
||||
exe.step.dependOn(sdl_step);
|
||||
|
||||
exe.addIncludePath(b.path("lib/clibs"));
|
||||
|
||||
return exe;
|
||||
}
|
||||
|
||||
@@ -112,11 +118,15 @@ fn stepBuildSdlTranslator(
|
||||
) *Build.Step.Compile {
|
||||
const sdl_translator = b.addExecutable(.{
|
||||
.name = "sdl_header_translator",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("utils/sdl_translator.zig"),
|
||||
.target = target,
|
||||
.optimize = .Debug,
|
||||
}),
|
||||
});
|
||||
sdl_translator.linkSystemLibrary("SDL3");
|
||||
for (SDL_LIBS) |lib| {
|
||||
sdl_translator.linkSystemLibrary(lib);
|
||||
}
|
||||
return sdl_translator;
|
||||
}
|
||||
|
||||
@@ -138,20 +148,25 @@ fn stepSdlModule(
|
||||
target: Build.ResolvedTarget,
|
||||
optimize: std.builtin.OptimizeMode,
|
||||
) struct { *Build.Module, *Build.Step } {
|
||||
const sdl_module = b.addModule("sdl", .{
|
||||
.root_source_file = b.path("lib/sdl.zig"),
|
||||
const translate_sdl_step, const sdl_rename = stepTranslateSdl(b, target);
|
||||
|
||||
const translate_c = b.addTranslateC(.{
|
||||
.root_source_file = b.path("lib/sdl.h"),
|
||||
.link_libc = true,
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
sdl_module.linkSystemLibrary("SDL3", .{});
|
||||
translate_c.addIncludePath(sdl_rename.dirname());
|
||||
const translate_module = translate_c.createModule();
|
||||
for (SDL_LIBS) |lib| {
|
||||
translate_module.linkSystemLibrary(lib, .{ .needed = true });
|
||||
}
|
||||
|
||||
const translate_step, const sdl_rename = stepTranslateSdl(b, target);
|
||||
sdl_module.addIncludePath(sdl_rename.dirname());
|
||||
translate_c.step.dependOn(translate_sdl_step);
|
||||
|
||||
return .{
|
||||
sdl_module,
|
||||
translate_step,
|
||||
translate_module,
|
||||
&translate_c.step,
|
||||
};
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
3
lib/sdl.h
Normal file
3
lib/sdl.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "SDL3/SDL.h"
|
||||
#include "SDL3_image/SDL_image.h"
|
||||
#include "sdl_rename.h"
|
@@ -1,4 +0,0 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cInclude("SDL3/SDL.h");
|
||||
@cInclude("sdl_rename.h");
|
||||
});
|
@@ -56,6 +56,7 @@ pub const LoadError = error{
|
||||
ParsingError,
|
||||
SdlError,
|
||||
FileTooBig,
|
||||
UnsupportedAsset,
|
||||
} || std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || std.json.ParseError(std.json.Scanner);
|
||||
|
||||
pub const AssetType = enum {
|
||||
@@ -123,7 +124,7 @@ pub fn AssetContainer(comptime T: type) type {
|
||||
self.last_state = self.asset_pointer.state;
|
||||
}
|
||||
if (self.last_state == .loaded) {
|
||||
self.data_pointer = @alignCast(@ptrCast(self.asset_pointer.data));
|
||||
self.data_pointer = @ptrCast(@alignCast(self.asset_pointer.data));
|
||||
return self.data_pointer;
|
||||
} else return null;
|
||||
},
|
||||
@@ -150,7 +151,7 @@ pub fn AssetContainer(comptime T: type) type {
|
||||
self.last_state = self.asset_pointer.state;
|
||||
}
|
||||
if (self.last_state == .loaded) {
|
||||
self.data_pointer = @alignCast(@ptrCast(self.asset_pointer.data));
|
||||
self.data_pointer = @ptrCast(@alignCast(self.asset_pointer.data));
|
||||
}
|
||||
continue :sw self.last_state;
|
||||
},
|
||||
@@ -298,8 +299,8 @@ fn makeLoader(comptime T: type, comptime func: *const fn ([]const u8, std.mem.Al
|
||||
fn makeUnloader(comptime T: type, comptime func: *const fn (T, std.mem.Allocator) void) *const fn (*AssetCell, std.mem.Allocator) void {
|
||||
const Container = struct {
|
||||
pub fn unloader(cell: *AssetCell, alloc: std.mem.Allocator) void {
|
||||
func(@as(*T, @alignCast(@ptrCast(cell.data))).*, alloc);
|
||||
alloc.destroy(@as(*T, @alignCast(@ptrCast(cell.data))));
|
||||
func(@as(*T, @ptrCast(@alignCast(cell.data))).*, alloc);
|
||||
alloc.destroy(@as(*T, @ptrCast(@alignCast(cell.data))));
|
||||
}
|
||||
};
|
||||
return Container.unloader;
|
||||
|
@@ -1,7 +1,6 @@
|
||||
const std = @import("std");
|
||||
const sdl = @import("sdl");
|
||||
const err = @import("../error.zig");
|
||||
const c = @import("../c.zig");
|
||||
const Assets = @import("../assets.zig");
|
||||
const Graphics = @import("../graphics.zig");
|
||||
|
||||
@@ -14,20 +13,15 @@ pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This()
|
||||
defer Assets.free(file);
|
||||
const data = (file.getSync() catch return error.DependencyError).bytes;
|
||||
|
||||
var width: u32 = undefined;
|
||||
var height: u32 = undefined;
|
||||
var channels: u32 = undefined;
|
||||
const image = c.stbi_load_from_memory(
|
||||
@ptrCast(data),
|
||||
@intCast(data.len),
|
||||
@ptrCast(&width),
|
||||
@ptrCast(&height),
|
||||
@ptrCast(&channels),
|
||||
4,
|
||||
);
|
||||
if (image == null) return error.ParsingError;
|
||||
defer c.stbi_image_free(image);
|
||||
const image_slice = image[0..@intCast(width * height * channels)];
|
||||
const image: *sdl.Surface = @ptrCast(sdl.IMG_Load_IO(sdl.IOFromConstMem(data.ptr, data.len), true) orelse return error.ParsingError);
|
||||
defer sdl.DestroySurface(image);
|
||||
const format = image.format;
|
||||
|
||||
const width: u32 = @intCast(image.w);
|
||||
const height: u32 = @intCast(image.h);
|
||||
const channels: u32 = 4;
|
||||
|
||||
const image_slice = @as([*]u8, @ptrCast(image.pixels))[0..@intCast(width * height * channels)];
|
||||
|
||||
if (width > 8192 or height > 8192) return error.FileTooBig;
|
||||
|
||||
@@ -63,6 +57,43 @@ pub fn load(path: []const u8, alloc: std.mem.Allocator) Assets.LoadError!@This()
|
||||
defer sdl.EndGPUCopyPass(copy_pass);
|
||||
|
||||
const map: [*]u8 = @ptrCast(sdl.MapGPUTransferBuffer(Graphics.device, transfer_buffer, false) orelse err.sdl());
|
||||
var pixel: u32 = rows_uploaded * width * bytes_per_pixel;
|
||||
var mapped: u32 = 0;
|
||||
while (pixel < (rows_uploaded + rows_to_upload) * width * bytes_per_pixel) {
|
||||
defer pixel += bytes_per_pixel;
|
||||
defer mapped += bytes_per_pixel;
|
||||
switch (format) {
|
||||
// Convert to RGBA8888
|
||||
sdl.PIXELFORMAT_ABGR8888 => {
|
||||
map[mapped + 0] = image_slice[pixel + 3];
|
||||
map[mapped + 1] = image_slice[pixel + 2];
|
||||
map[mapped + 2] = image_slice[pixel + 1];
|
||||
map[mapped + 3] = image_slice[pixel + 0];
|
||||
},
|
||||
sdl.PIXELFORMAT_ARGB8888 => {
|
||||
map[mapped + 0] = image_slice[pixel + 1];
|
||||
map[mapped + 1] = image_slice[pixel + 2];
|
||||
map[mapped + 2] = image_slice[pixel + 3];
|
||||
map[mapped + 3] = image_slice[pixel + 0];
|
||||
},
|
||||
sdl.PIXELFORMAT_RGBA8888 => {
|
||||
map[mapped + 0] = image_slice[pixel + 0];
|
||||
map[mapped + 1] = image_slice[pixel + 1];
|
||||
map[mapped + 2] = image_slice[pixel + 2];
|
||||
map[mapped + 3] = image_slice[pixel + 3];
|
||||
},
|
||||
sdl.PIXELFORMAT_BGRA8888 => {
|
||||
map[mapped + 0] = image_slice[pixel + 2];
|
||||
map[mapped + 1] = image_slice[pixel + 1];
|
||||
map[mapped + 2] = image_slice[pixel + 0];
|
||||
map[mapped + 3] = image_slice[pixel + 3];
|
||||
},
|
||||
else => {
|
||||
sdl.UnmapGPUTransferBuffer(Graphics.device, transfer_buffer);
|
||||
return error.UnsupportedAsset;
|
||||
},
|
||||
}
|
||||
}
|
||||
@memcpy(map, image_slice[(rows_uploaded * width * bytes_per_pixel)..((rows_uploaded + rows_to_upload) * width * bytes_per_pixel)]);
|
||||
sdl.UnmapGPUTransferBuffer(Graphics.device, transfer_buffer);
|
||||
|
||||
|
@@ -1,8 +0,0 @@
|
||||
pub usingnamespace @cImport({
|
||||
@cDefine("STBI_NO_GIF", "1");
|
||||
@cDefine("STBI_NO_JPEG", "1");
|
||||
@cDefine("STBI_NO_HDR", "1");
|
||||
@cDefine("STBI_NO_TGA", "1");
|
||||
@cDefine("STB_IMAGE_IMPLEMENTATION", "1");
|
||||
@cInclude("stb_image.h");
|
||||
});
|
@@ -14,7 +14,3 @@ const FileError = std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.Fil
|
||||
pub fn file(err: FileError, path: []const u8) noreturn {
|
||||
std.debug.panic("Error while reading \"{s}\": {any}", .{ path, err });
|
||||
}
|
||||
|
||||
pub fn stbi() noreturn {
|
||||
std.debug.panic("STBI error!\n", .{});
|
||||
}
|
||||
|
@@ -282,7 +282,7 @@ fn loadShader(path: []const u8, info: sdl.GPUShaderCreateInfo) *sdl.GPUShader {
|
||||
const file = std.fs.cwd().openFile(path, .{}) catch |e| err.file(e, path);
|
||||
defer file.close();
|
||||
|
||||
const code = file.readToEndAllocOptions(std.heap.c_allocator, std.math.maxInt(usize), null, 1, 0) catch |e| err.file(e, path);
|
||||
const code = file.readToEndAllocOptions(std.heap.c_allocator, std.math.maxInt(usize), null, .@"1", 0) catch |e| err.file(e, path);
|
||||
defer std.heap.c_allocator.free(code);
|
||||
|
||||
var updated_info = info;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
const std = @import("std");
|
||||
const sdl = @cImport({
|
||||
@cInclude("SDL3/SDL.h");
|
||||
@cInclude("SDL3_image/SDL_image.h");
|
||||
});
|
||||
|
||||
const PREFIX = "SDL_";
|
||||
@@ -23,12 +24,8 @@ pub fn main() !void {
|
||||
try std.fs.cwd().createFile(output, .{});
|
||||
defer file.close();
|
||||
|
||||
const writer = file.writer();
|
||||
|
||||
const stdout = std.io.getStdOut();
|
||||
defer stdout.close();
|
||||
|
||||
const out = stdout.writer();
|
||||
var writer_buffer: [4096]u8 = undefined;
|
||||
var writer = file.writer(&writer_buffer);
|
||||
|
||||
var renamed_count: usize = 0;
|
||||
for (@typeInfo(sdl).@"struct".decls) |decl| {
|
||||
@@ -36,7 +33,7 @@ pub fn main() !void {
|
||||
|
||||
const new_name: []const u8 = decl.name[PREFIX.len..];
|
||||
|
||||
try writer.print(
|
||||
try writer.interface.print(
|
||||
\\#define {1s} {0s}
|
||||
\\
|
||||
, .{ decl.name, new_name });
|
||||
@@ -45,5 +42,4 @@ pub fn main() !void {
|
||||
if (renamed_count == 0) {
|
||||
@panic("No SDL definitions renamed");
|
||||
}
|
||||
try out.print("[SDL Translator] {} SDL definitions renamed\n", .{renamed_count});
|
||||
}
|
||||
|
Reference in New Issue
Block a user