1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-08 23:23:38 +00:00
lovr/src/util.c
2016-09-13 17:02:23 -07:00

134 lines
3.3 KiB
C

#include "util.h"
#include <stdarg.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef __APPLE__
#include <unistd.h>
#else
#include <io.h>
#endif
void error(const char* format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
fputs("\n", stderr);
va_end(args);
exit(EXIT_FAILURE);
}
int fileExists(char* filename) {
return access(filename, 0) != -1;
}
char* loadFile(char* filename) {
struct stat info;
if (stat(filename, &info)) {
error("Could not stat '%s'", filename);
}
int size = (int)info.st_size;
char* buffer = malloc(size + 1);
int fd = open(filename, O_RDONLY);
if (fd < 0) {
error("Could not open '%s'", filename);
}
if (read(fd, buffer, size) < 0) {
error("Could not read '%s'", filename);
}
buffer[size] = '\0';
return buffer;
}
int luaPreloadModule(lua_State* L, const char* key, lua_CFunction f) {
lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
lua_pushcfunction(L, f);
lua_setfield(L, -2, key);
lua_pop(L, 2);
return 0;
}
void luaRegisterModule(lua_State* L, const char* name, const luaL_Reg* module) {
// Get reference to lovr
lua_getglobal(L, "lovr");
// Create a table and fill it with the module functions
lua_newtable(L);
luaL_register(L, NULL, module);
// lovr[name] = module
lua_setfield(L, -2, name);
// Pop lovr
lua_pop(L, 1);
}
void luaRegisterType(lua_State* L, const char* name, const luaL_Reg* functions, lua_CFunction gc) {
// Push metatable
luaL_newmetatable(L, name);
lua_getmetatable(L, -1);
// m.__index = m
lua_pushvalue(L, -1);
lua_setfield(L, -1, "__index");
// m.__gc = gc
if (gc) {
lua_pushcfunction(L, gc);
lua_setfield(L, -2, "__gc");
}
// m.name = name
lua_pushstring(L, name);
lua_setfield(L, -2, "name");
// Register class functions
if (functions) {
luaL_register(L, NULL, functions);
}
// Pop metatable
lua_pop(L, 1);
}
float* matrixMultiply(float* a, float* b) {
float a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
b00 = b[0], b01 = b[1], b02 = b[2], b03 = b[3],
b10 = b[4], b11 = b[5], b12 = b[6], b13 = b[7],
b20 = b[8], b21 = b[9], b22 = b[10], b23 = b[11],
b30 = b[12], b31 = b[13], b32 = b[14], b33 = b[15];
a[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
a[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
a[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
a[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
a[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
a[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
a[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
a[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
a[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
a[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
a[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
a[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
a[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
a[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
a[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
a[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
return a;
}