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
|
// 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("GL/gl.h");
@cInclude("GL/glu.h");
@cInclude("lodepng.h");
});
const allocator = std.heap.c_allocator;
const cwd = std.fs.cwd;
const data_dir = @import("build_options").data_dir;
const free = std.c.free;
const sep = std.fs.path.sep;
const maxInt = std.math.maxInt;
const std = @import("std");
const span = std.mem.span;
const max_size = maxInt(usize); // don't judge me, take care of me
const texture_dir = data_dir ++ [_]u8{ sep } ++ "textures";
fn check(errorString: fn (c_uint) callconv(.C) [*c]const u8,
status: anytype) void {
if (status != 0)
@panic(span(errorString(@intCast(c_uint, status))));
}
pub export fn loadTexture(filename: [*:0]const u8) GLuint {
var dir = cwd().openDir(texture_dir, .{}) catch unreachable;
defer dir.close();
var 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;
}
|