1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
// Miscellaneous functions
// Copyright (C) 2021 Nguyễn Gia Phong
//
// This file is part of Black Shades.
//
// Black Shades is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Black Shades is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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 al = @import("zeal");
const allocPrint = std.fmt.allocPrint;
const allocator = std.heap.c_allocator;
const assert = std.debug.assert;
const count = std.mem.count;
const cwd = std.fs.cwd;
const data_dir = @import("build_options").data_dir ++ [_]u8{ sep };
const endsWith = std.mem.endsWith;
const eql = std.mem.eql;
const free = std.c.free;
const join = std.fs.path.joinZ;
const maxInt = std.math.maxInt;
const parseFloat = std.fmt.parseFloat;
const parseInt = std.fmt.parseInt;
const sep = std.fs.path.sep;
const span = std.mem.span;
const startsWith = std.mem.startsWith;
const std = @import("std");
const tokenize = std.mem.tokenize;
// Don't judge me, take care of me!
const max_size = maxInt(usize);
/// Read given file to heap, allocated by C allocator.
fn readFile(dir: Dir, comptime fmt: []const u8, args: anytype) ![]const u8 {
const filename = try allocPrint(allocator, fmt, args);
defer allocator.free(filename);
return dir.readFileAlloc(allocator, filename, max_size);
}
const Frame = extern struct {
joints: [20]extern struct {
x: f32,
y: f32,
z: f32,
},
speed: f32,
};
export fn loadAnimation(name: [*:0]const u8) extern struct {
ptr: [*]Frame,
len: usize,
} {
var dir = cwd().openDir(data_dir ++ "animations", .{}) catch unreachable;
defer dir.close();
const anim_file = readFile(dir, "{s}{c}index.tsv", .{
name, sep, // $animation/index.tsv
}) catch unreachable;
defer allocator.free(anim_file);
const length = count(u8, anim_file, "\t") - 1;
var anim = tokenize(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");
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");
_ = joints.next().?; // ignore field names
for (frame.joints) |*joint| {
var coordinates = tokenize(joints.next().?, "\t");
joint.* = .{
.x = parseFloat(f32, coordinates.next().?) catch unreachable,
.y = parseFloat(f32, coordinates.next().?) catch unreachable,
.z = parseFloat(f32, coordinates.next().?) catch unreachable,
};
}
}
return .{ .ptr = frames.ptr, .len = frames.len };
}
/// Parse boolean values.
pub fn parseBool(s: []const u8) !bool {
if (eql(u8, s, "false"))
return false;
if (eql(u8, s, "true"))
return true;
return error.InvalidCharacter;
}
const Joint = extern struct {
label: i8,
x: f32,
y: f32,
z: f32,
length: f32,
model: u8,
visible: bool,
lower: bool,
parent: i8,
pub fn load(self: *Joint, row: []const u8) !void {
var values = tokenize(row, "\t");
self.label = try parseInt(i8, values.next().?, 10);
self.x = try parseFloat(f32, values.next().?);
self.y = try parseFloat(f32, values.next().?);
self.z = try parseFloat(f32, values.next().?);
self.length = try parseFloat(f32, values.next().?);
self.model = try parseInt(u8, values.next().?, 10);
self.visible = try parseBool(values.next().?);
self.lower = try parseBool(values.next().?);
self.parent = try parseInt(i8, values.next().?, 10);
}
};
/// Load joints in character's skeleton.
export fn loadJoints(joints: [*]Joint) void {
var tsv = tokenize(@embedFile("joints.tsv"), "\n");
_ = tsv.next().?; // ignore field names
var i = @as(u8, 0);
while (tsv.next()) |row| : (i += 1)
joints[i].load(row) catch unreachable;
}
const Vertex = extern struct {
x: f32, y: f32, z: f32,
};
const Face = extern struct {
// Only support triangles
v: [3]u16,
r: f32, g: f32, b: f32,
};
const OffIterator = struct {
token_iterator: TokenIterator,
pub fn init(buffer: []const u8) OffIterator {
var self = .{ .token_iterator = tokenize(buffer, "\n") };
if (!endsWith(u8, self.token_iterator.next().?, "OFF"))
self.token_iterator.reset();
return self;
}
pub fn next(self: *OffIterator) ?TokenIterator {
while (self.token_iterator.next()) |line| {
var words = tokenize(line, " ");
if (words.next()) |word| { // not empty
if (!startsWith(u8, word, "#")) { // not comment
words.reset();
return words;
}
}
}
return null;
}
};
/// Load model from given OFF file.
export fn loadModel(path: [*:0]const u8) extern struct {
vertices: extern struct {
ptr: [*]Vertex,
len: usize,
},
faces: extern struct {
ptr: [*]Face,
len: usize,
},
} {
var dir = cwd().openDir(data_dir ++ "models", .{}) catch unreachable;
defer dir.close();
const file = dir.readFileAlloc(allocator, span(path), max_size)
catch unreachable;
defer allocator.free(file);
var lines = OffIterator.init(file);
var counts = lines.next().?;
const vertex_count = parseInt(usize, counts.next().?, 10) catch unreachable;
const face_count = parseInt(usize, counts.next().?, 10) catch unreachable;
const vertices = allocator.alloc(Vertex, vertex_count) catch unreachable;
for (vertices) |*vertex| {
var numbers = lines.next().?;
vertex.* = .{
.x = parseFloat(f32, numbers.next().?) catch unreachable,
.y = parseFloat(f32, numbers.next().?) catch unreachable,
.z = parseFloat(f32, numbers.next().?) catch unreachable,
};
}
const faces = allocator.alloc(Face, face_count) catch unreachable;
for (faces) |*face| {
var numbers = lines.next().?;
assert(eql(u8, numbers.next().?, "3"));
face.* = .{
.v = .{
parseInt(u16, numbers.next().?, 10) catch unreachable,
parseInt(u16, numbers.next().?, 10) catch unreachable,
parseInt(u16, numbers.next().?, 10) catch unreachable,
},
.r = parseFloat(f32, numbers.next().?) catch unreachable,
.g = parseFloat(f32, numbers.next().?) catch unreachable,
.b = parseFloat(f32, numbers.next().?) catch unreachable,
};
}
return .{
.vertices = .{ .ptr = vertices.ptr , .len = vertices.len },
.faces = .{ .ptr = faces.ptr , .len = faces.len },
};
}
const Muscle = extern struct {
length: f32,
initlen: f32,
minlen: f32,
maxlen: f32,
flag: bool,
visible: bool,
parent1: i8,
parent2: i8,
pub fn load(self: *Muscle, row: []const u8) !void {
var values = tokenize(row, "\t");
self.length = try parseFloat(f32, values.next().?);
self.initlen = try parseFloat(f32, values.next().?);
self.minlen = try parseFloat(f32, values.next().?);
self.maxlen = try parseFloat(f32, values.next().?);
self.flag = try parseBool(values.next().?);
self.visible = try parseBool(values.next().?);
self.parent1 = try parseInt(i8, values.next().?, 10);
self.parent2 = try parseInt(i8, values.next().?, 10);
}
};
/// Load muscles in character's skeleton.
export fn loadMuscles(muscles: [*]Muscle) void {
var tsv = tokenize(@embedFile("muscles.tsv"), "\n");
_ = tsv.next().?; // ignore field names
var i = @as(u8, 0);
while (tsv.next()) |row| : (i += 1)
muscles[i].load(row) catch unreachable;
}
/// Load audio file into an OpenAL buffer and return it.
export fn loadSound(filename: [*:0]const u8) u32 {
const path = join(allocator, &.{ data_dir ++ "sounds", span(filename) })
catch unreachable;
defer allocator.free(path);
const audio = al.Audio.read(allocator, span(path)) catch unreachable;
defer audio.free();
const buffer = al.Buffer.init(audio) catch unreachable;
return buffer.reference;
}
fn check(errorString: fn (c_uint) callconv(.C) [*c]const u8,
status: anytype) void {
if (status != 0)
@panic(span(errorString(@intCast(c_uint, status))));
}
/// Load PNG file into an OpenGL buffer and return it.
export fn loadTexture(filename: [*:0]const u8) GLuint {
var dir = cwd().openDir(data_dir ++ "textures", .{}) catch unreachable;
defer dir.close();
const file = dir.readFileAlloc(allocator, span(filename), max_size)
catch unreachable;
defer allocator.free(file);
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));
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));
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
src.setPosition(.{ x / 32, y / 32, z / 32 }) catch unreachable;
src.play() catch unreachable;
}
|