aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cimport.zig5
-rw-r--r--src/config.zig2
-rw-r--r--src/main.zig11
-rw-r--r--src/misc.zig69
4 files changed, 46 insertions, 41 deletions
diff --git a/src/cimport.zig b/src/cimport.zig
index b5d66e9..88061db 100644
--- a/src/cimport.zig
+++ b/src/cimport.zig
@@ -1,4 +1,9 @@
usingnamespace @cImport({
+ @cInclude("AL/al.h");
+ @cInclude("GL/gl.h");
+ @cInclude("GL/glu.h");
+ @cInclude("lodepng.h");
+
@cInclude("Game.h");
@cInclude("Constants.h");
});
diff --git a/src/config.zig b/src/config.zig
index ecb199f..6622ef2 100644
--- a/src/config.zig
+++ b/src/config.zig
@@ -92,7 +92,7 @@ fn parseLevels(dir_path: []const u8, length: usize) ![*]Level {
else return error.InvalidData;
} else if (eql(u8, kv.key, "evil weapons")) {
var weapons = IntegerBitSet(8).initEmpty();
- var enums = tokenize(kv.value, " ");
+ var enums = tokenize(u8, kv.value, " ");
while (enums.next()) |weapon|
weapons.set(try parseInt(u3, weapon, 10));
levels[i].evil_weapons = weapons.mask;
diff --git a/src/main.zig b/src/main.zig
index 6cbf240..f861b19 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -37,38 +37,43 @@ var game: *c.Game = undefined;
var prng: DefaultPrng = undefined;
fn resizeWindow(window: gf.Window, width: c_int, height: c_int) void {
+ _ = window;
c.resizeWindow(game, width, height);
}
fn handleKey(window: gf.Window, key: gf.Key, scancode: c_int,
action: gf.KeyAction, mods: gf.Mods) void {
+ _ = window;
+ _ = scancode;
c.handleKey(game, @enumToInt(key), @enumToInt(action), mods.toInt());
}
fn look(window: gf.Window, xpos: f64, ypos: f64) void {
+ _ = window;
c.look(game, xpos, ypos);
}
fn click(window: gf.Window, button: gf.MouseButton,
action: gf.MouseButton.Action, mods: gf.Mods) void {
+ _ = window;
c.click(game, @enumToInt(button), @enumToInt(action), mods.toInt());
}
/// Return a floating point value evenly distributed in the range [0, 1).
export fn randFloat() f32 {
- return prng.random.float(f32);
+ return prng.random().float(f32);
}
/// Return a random integer i where at_least <= i <= at_most.
/// The results of this function may be biased.
export fn randInt(at_least: i32, at_most: i32) i32 {
- return prng.random.intRangeAtMostBiased(i32, at_least, at_most);
+ return prng.random().intRangeAtMostBiased(i32, at_least, at_most);
}
/// Return a random integer i where 0 <= i < less_than.
/// The results of this function may be biased.
export fn randUint(less_than: u32) u32 {
- return prng.random.uintLessThanBiased(u32, less_than);
+ return prng.random().uintLessThanBiased(u32, less_than);
}
pub fn main() !void {
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;
}