Compare commits

..

7 Commits

Author SHA1 Message Date
mid
82fcbb6fed Switch to "init" as main script 2025-02-09 22:24:49 +02:00
mid
130e0f1050 Automatically mark GPU skinning in shaders 2025-02-09 22:24:37 +02:00
mid
fff7690222 trimesh_desc_concat 2025-02-09 22:24:21 +02:00
mid
2bf947018c game.draw was broken for text 2025-02-09 22:23:54 +02:00
mid
6f2a27c03f Reset camfocus in game.cleanup 2025-02-09 22:23:33 +02:00
mid
8b359412f2 Secure module names 2025-02-09 22:22:59 +02:00
mid
2cae13dc2d UB fix 2025-02-09 22:22:32 +02:00
3 changed files with 105 additions and 9 deletions

View File

@ -214,7 +214,7 @@ static inline size_t game_nextid() {
} }
static inline void game_claimentid(size_t i) { static inline void game_claimentid(size_t i) {
Game.entities.freeIDs[i / 64] |= 1 << (i % 64); Game.entities.freeIDs[i / 64] |= 1UL << (i % 64);
} }
static inline int game_componentcomparator(const void *a, const void *b) { static inline int game_componentcomparator(const void *a, const void *b) {

View File

@ -1427,7 +1427,29 @@ static struct Module {
int ref; int ref;
} *modules; } *modules;
static int luaapi_require(lua_State *L) { static int luaapi_require(lua_State *L) {
const char *name = lua_tostring(L, 1); char *name = strdup(lua_tostring(L, 1));
size_t nameLen = strlen(name);
while(*name == '.' && nameLen) {
memmove(name, name + 1, --nameLen);
}
for(int i = 1; i < nameLen;) {
if(name[i] == '.' && name[i - 1] == '.') {
memmove(name + i + 1, name + i, --nameLen - i);
} else i++;
}
for(int i = 0; i < nameLen;) {
if(!isalpha(name[i]) && !isdigit(name[i]) && name[i] != '_' && name[i] != '-') {
memmove(name + i + 1, name + i, --nameLen - i);
} else i++;
}
if(nameLen == 0) {
free(name);
return 0;
}
for(size_t i = 0; i < moduleCount; i++) { for(size_t i = 0; i < moduleCount; i++) {
if(!strcmp(modules[i].name, name)) { if(!strcmp(modules[i].name, name)) {
@ -1469,6 +1491,7 @@ static int game_load(lua_State *L) {
static int dagame_cleanup(lua_State *L) { static int dagame_cleanup(lua_State *L) {
luaapi_cleanup(); luaapi_cleanup();
LuaapiCamFocus = false;
return 0; return 0;
} }
@ -1841,6 +1864,10 @@ static int dagame_draw(lua_State *L) {
} else { } else {
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, 1, "font");
struct k3Font *font = *(void**) lua_touserdata(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "align"); lua_getfield(L, 1, "align");
lua_getfield(L, 1, "r"); lua_getfield(L, 1, "r");
@ -1855,9 +1882,6 @@ static int dagame_draw(lua_State *L) {
lua_getfield(L, 1, "x"); lua_getfield(L, 1, "x");
lua_getfield(L, 1, "y"); lua_getfield(L, 1, "y");
lua_getfield(L, 1, "font");
struct k3Font *font = *(void**) lua_touserdata(L, 1);
struct k3RectF rect; struct k3RectF rect;
k3FontSz(font, lua_tonumber(L, -4), lua_tostring(L, -3), &rect); k3FontSz(font, lua_tonumber(L, -4), lua_tostring(L, -3), &rect);
@ -2263,6 +2287,67 @@ static int dagame_net_punch(lua_State *L) {
return 0; return 0;
} }
static int dagame_mdl_desc_append(lua_State *L) {
float offsetX = lua_tonumber(L, 3);
float offsetY = lua_tonumber(L, 4);
float offsetZ = lua_tonumber(L, 5);
lua_getfield(L, 1, "positions");
lua_getfield(L, 2, "positions");
lua_len(L, -2);
lua_len(L, -2);
size_t bigPositions = lua_tointeger(L, -2);
size_t smallPositions = lua_tointeger(L, -1);
lua_pop(L, 2);
for(size_t i = 1; i <= smallPositions; i += 3) {
lua_rawgeti(L, -1, i + 0);
lua_rawgeti(L, -2, i + 1);
lua_rawgeti(L, -3, i + 2);
float posX = lua_tonumber(L, -3) + offsetX;
float posY = lua_tonumber(L, -2) + offsetY;
float posZ = lua_tonumber(L, -1) + offsetZ;
lua_pop(L, 3);
lua_pushnumber(L, posX);
lua_rawseti(L, -3, bigPositions + i + 0);
lua_pushnumber(L, posY);
lua_rawseti(L, -3, bigPositions + i + 1);
lua_pushnumber(L, posZ);
lua_rawseti(L, -3, bigPositions + i + 2);
}
lua_pop(L, 2);
lua_getfield(L, 1, "indices");
lua_getfield(L, 2, "indices");
lua_len(L, -1);
size_t smallIndices = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_len(L, -2);
size_t bigIndices = lua_tointeger(L, -1);
lua_pop(L, 1);
bigPositions /= 3;
for(size_t i = 1; i <= smallIndices; i++) {
lua_rawgeti(L, -1, i);
size_t j = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pushinteger(L, j + bigPositions);
lua_rawseti(L, -3, ++bigIndices);
}
lua_pop(L, 2);
return 0;
}
#include<GLFW/glfw3.h> #include<GLFW/glfw3.h>
static int os_time(lua_State *L) { static int os_time(lua_State *L) {
lua_pushnumber(L, glfwGetTime() - LuaapiStartTime); lua_pushnumber(L, glfwGetTime() - LuaapiStartTime);
@ -2384,6 +2469,9 @@ void luaapi_init() {
lua_pushcfunction(L, dagame_mdl); lua_pushcfunction(L, dagame_mdl);
lua_setfield(L, -2, "mdl"); lua_setfield(L, -2, "mdl");
lua_pushcfunction(L, dagame_mdl_desc_append);
lua_setfield(L, -2, "trimesh_desc_concat");
lua_pushcfunction(L, dagame_trimesh); lua_pushcfunction(L, dagame_trimesh);
lua_setfield(L, -2, "trimesh"); lua_setfield(L, -2, "trimesh");
@ -2955,12 +3043,19 @@ static int parse_glsl_table(struct k3Mat *mat) {
} }
lua_pop(L, 1); lua_pop(L, 1);
char prefix[272]; char prefix[512];
snprintf(prefix, sizeof(prefix), "#version %lu\n%s", vsVer, defs); const char *prefixfmt;
if(k3IsSoftSkinning) {
prefixfmt = "#version %lu\n%s";
} else {
prefixfmt = "#version %lu\n#define k3_GPU_SKINNING\n%s";
}
snprintf(prefix, sizeof(prefix), prefixfmt, vsVer, defs);
char *vsSrc = read_full_file(prefix, "assets/mdl/", vsFile, ""); char *vsSrc = read_full_file(prefix, "assets/mdl/", vsFile, "");
snprintf(prefix, sizeof(prefix), "#version %lu\n%s", fsVer, defs); snprintf(prefix, sizeof(prefix), prefixfmt, fsVer, defs);
char *fsSrc = read_full_file(prefix, "assets/mdl/", fsFile, ""); char *fsSrc = read_full_file(prefix, "assets/mdl/", fsFile, "");
k3Log(k3_INFO, "Attempting to compile \"%s\" & \"%s\"...", vsFile, fsFile); k3Log(k3_INFO, "Attempting to compile \"%s\" & \"%s\"...", vsFile, fsFile);

View File

@ -351,6 +351,7 @@ static int eng_init() {
printf("GL vendor: %s\n", glGetString(GL_VENDOR)); printf("GL vendor: %s\n", glGetString(GL_VENDOR));
k3SetLogCallback(k4k3LogCallback); k3SetLogCallback(k4k3LogCallback);
k3Init(); k3Init();
k3BatchInit(); k3BatchInit();
@ -500,7 +501,7 @@ int main(int argc_, char **argv_) {
// INIT LEVEL // INIT LEVEL
game_init(); game_init();
luaapi_load(k4_get_arg("script") ? k4_get_arg("script") : "lvl1"); luaapi_load(k4_get_arg("script") ? k4_get_arg("script") : "init");
// //
LuaapiStartTime = glfwGetTime(); LuaapiStartTime = glfwGetTime();