diff options
Diffstat (limited to 'src/misc.zig')
-rw-r--r-- | src/misc.zig | 69 |
1 files changed, 32 insertions, 37 deletions
diff --git a/src/misc.zig b/src/misc.zig index f61fc2d..eb09cc2 100644 --- a/src/misc.zig +++ b/src/misc.zig @@ -16,15 +16,8 @@ // You should have received a copy of the GNU General Public License // along with Black Shades. If not, see <https://www.gnu.org/licenses/>. -usingnamespace @cImport({ - @cInclude("AL/al.h"); - @cInclude("GL/gl.h"); - @cInclude("GL/glu.h"); - @cInclude("lodepng.h"); -}); - const Dir = std.fs.Dir; -const TokenIterator = std.mem.TokenIterator; +const TokenIterator = std.mem.TokenIterator(u8); const allocPrint = std.fmt.allocPrint; const allocator = std.heap.c_allocator; const assert = std.debug.assert; @@ -48,6 +41,7 @@ const gf = @import("gfz"); const ini = @import("ini"); const data_dir = @import("build_options").data_dir ++ [_]u8{ sep }; +const c = @import("cimport.zig"); /// Return whether the given keyboard key is pressed. export fn keyPress(key: c_int) bool { @@ -83,22 +77,22 @@ export fn loadAnimation(name: [*:0]const u8) extern struct { }) catch unreachable; defer allocator.free(anim_file); const length = count(u8, anim_file, "\t") - 1; - var anim = tokenize(anim_file, "\n"); + var anim = tokenize(u8, anim_file, "\n"); _ = anim.next().?; // ignore field names const frames = allocator.alloc(Frame, length) catch unreachable; for (frames) |*frame| { - var values = tokenize(anim.next().?, "\t"); + var values = tokenize(u8, anim.next().?, "\t"); const frame_file = readFile(dir, "{s}{c}frames{c}{s}.tsv", .{ name, sep, sep, values.next().?, // $animation/frames/$frame.tsv }) catch unreachable; defer allocator.free(frame_file); frame.speed = parseFloat(f32, values.next().?) catch unreachable; - var joints = tokenize(frame_file, "\n"); + var joints = tokenize(u8, frame_file, "\n"); _ = joints.next().?; // ignore field names for (frame.joints) |*joint| { - var coordinates = tokenize(joints.next().?, "\t"); + var coordinates = tokenize(u8, joints.next().?, "\t"); joint.* = .{ .x = parseFloat(f32, coordinates.next().?) catch unreachable, .y = parseFloat(f32, coordinates.next().?) catch unreachable, @@ -129,7 +123,7 @@ const Joint = extern struct { parent: i8, pub fn load(self: *Joint, row: []const u8) !void { - var values = tokenize(row, "\t"); + var values = tokenize(u8, row, "\t"); self.x = try parseFloat(f32, values.next().?); self.y = try parseFloat(f32, values.next().?); self.z = try parseFloat(f32, values.next().?); @@ -146,7 +140,7 @@ export fn loadJoints(joints: [*]Joint) void { const file = readFile(cwd(), data_dir ++ "joints.tsv", .{}) catch unreachable; defer allocator.free(file); - var tsv = tokenize(file, "\n"); + var tsv = tokenize(u8, file, "\n"); _ = tsv.next().?; // ignore field names var i = @as(u8, 0); while (tsv.next()) |row| : (i += 1) @@ -167,7 +161,7 @@ const OffIterator = struct { token_iterator: TokenIterator, pub fn init(buffer: []const u8) OffIterator { - var self = .{ .token_iterator = tokenize(buffer, "\n") }; + var self = .{ .token_iterator = tokenize(u8, buffer, "\n") }; if (!endsWith(u8, self.token_iterator.next().?, "OFF")) self.token_iterator.reset(); return self; @@ -175,7 +169,7 @@ const OffIterator = struct { pub fn next(self: *OffIterator) ?TokenIterator { while (self.token_iterator.next()) |line| { - var words = tokenize(line, " "); + var words = tokenize(u8, line, " "); if (words.next()) |word| { // not empty if (!startsWith(u8, word, "#")) { // not comment words.reset(); @@ -250,7 +244,7 @@ const Muscle = extern struct { parent2: i8, pub fn load(self: *Muscle, row: []const u8) !void { - var values = tokenize(row, "\t"); + var values = tokenize(u8, row, "\t"); self.length = try parseFloat(f32, values.next().?); self.initlen = try parseFloat(f32, values.next().?); self.minlen = try parseFloat(f32, values.next().?); @@ -267,7 +261,7 @@ export fn loadMuscles(muscles: [*]Muscle) void { const file = readFile(cwd(), data_dir ++ "muscles.tsv", .{}) catch unreachable; defer allocator.free(file); - var tsv = tokenize(file, "\n"); + var tsv = tokenize(u8, file, "\n"); _ = tsv.next().?; // ignore field names var i = @as(u8, 0); while (tsv.next()) |row| : (i += 1) @@ -322,7 +316,7 @@ fn check(errorString: fn (c_uint) callconv(.C) [*c]const u8, } /// Load PNG file into an OpenGL buffer and return it. -export fn loadTexture(filename: [*:0]const u8) GLuint { +export fn loadTexture(filename: [*:0]const u8) c.GLuint { const file = readFile(cwd(), data_dir ++ "textures{c}{s}", .{ sep, filename, }) catch unreachable; @@ -331,32 +325,33 @@ export fn loadTexture(filename: [*:0]const u8) GLuint { var data: [*c]u8 = undefined; var w: c_uint = undefined; var h: c_uint = undefined; - check(lodepng_error_text, - lodepng_decode32(&data, &w, &h, file.ptr, file.len)); + check(c.lodepng_error_text, + c.lodepng_decode32(&data, &w, &h, file.ptr, file.len)); defer free(data); - var texture: GLuint = undefined; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - defer glBindTexture(GL_TEXTURE_2D, 0); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - const width = @intCast(GLint, w); - const height = @intCast(GLint, h); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - check(gluErrorString, gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, - GL_RGBA, GL_UNSIGNED_BYTE, data)); + var texture: c.GLuint = undefined; + c.glGenTextures(1, &texture); + c.glBindTexture(c.GL_TEXTURE_2D, texture); + defer c.glBindTexture(c.GL_TEXTURE_2D, 0); + c.glTexEnvi(c.GL_TEXTURE_ENV, c.GL_TEXTURE_ENV_MODE, c.GL_MODULATE); + c.glTexParameteri(c.GL_TEXTURE_2D, c.GL_TEXTURE_MAG_FILTER, c.GL_LINEAR); + c.glTexParameteri(c.GL_TEXTURE_2D, c.GL_TEXTURE_MIN_FILTER, c.GL_LINEAR); + + const width = @intCast(c.GLint, w); + const height = @intCast(c.GLint, h); + c.glPixelStorei(c.GL_UNPACK_ALIGNMENT, 1); + c.glTexImage2D(c.GL_TEXTURE_2D, 0, 4, width, height, + 0, c.GL_RGBA, c.GL_UNSIGNED_BYTE, data); + check(c.gluErrorString, + c.gluBuild2DMipmaps(c.GL_TEXTURE_2D, 4, width, height, + c.GL_RGBA, c.GL_UNSIGNED_BYTE, data)); return texture; } /// Move sound source to given position and play it. export fn playSound(source: u32, x: f32, y: f32, z: f32) void { const src = al.Source{ .reference = source }; - _ = alGetError(); // TODO: remove when completely migrate to zeal + _ = c.alGetError(); // TODO: remove when completely migrate to zeal src.setPosition(.{ x / 32, y / 32, z / 32 }) catch unreachable; src.play() catch unreachable; } |