static int l_filesystem_append(lua_State* state) { const char* filename = l_tools_toStringOrError(state, 1); const char* data = l_tools_toStringOrError(state, 2); filesystem_append(filename, data); return 2; }
static int l_filesystem_contain(lua_State* state) { const char* a = l_tools_toStringOrError(state, 1); const char* b = l_tools_toStringOrError(state, 2); if ( filesystem_contain(a, b)) lua_pushboolean(state, 1); else lua_pushboolean(state, 0); return 1; }
static int l_filesystem_compare(lua_State* state) { const char* a = l_tools_toStringOrError(state, 1); const char* b = l_tools_toStringOrError(state, 2); int l = l_tools_toNumberOrError(state, 3); if ( filesystem_compare(a, b, l)) lua_pushboolean(state, 1); else lua_pushboolean(state, 0); return 1; }
static int l_graphics_Shader_getExternVariable(lua_State* state) { l_assertType(state, 1, l_graphics_isShader); l_graphics_Shader const* shader = l_graphics_toShader(state, 1); char const* name = l_tools_toStringOrError(state, 2); graphics_ShaderUniformInfo const* info = graphics_Shader_getUniform(&shader->shader, name); if(!info) { goto errout; } graphics_ShaderUniformType type = graphics_shader_toMotorType(info->type); if(type == graphics_ShaderUniformType_none) { goto errout; } l_tools_pushEnum(state, type, l_graphics_ShaderUniformType); lua_pushnumber(state, graphics_shader_toMotorComponents(info->type)); lua_pushnumber(state, info->elements); return 3; errout: lua_pushnil(state); lua_pushnil(state); lua_pushnil(state); return 3; }
static int l_audio_newSource(lua_State *state) { char const* filename = l_tools_toStringOrError(state, 1); audio_SourceType type = audio_SourceType_stream; if(!lua_isnoneornil(state, 2)) { type = l_tools_toEnumOrError(state, 2, l_audio_SourceType); } // TODO handle load errors switch(type) { case audio_SourceType_static: { audio_StaticSource *src = lua_newuserdata(state, sizeof(audio_StaticSource)); audio_loadStatic(src, filename); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.staticMT); break; } case audio_SourceType_stream: { audio_StreamSource *src = lua_newuserdata(state, sizeof(audio_StreamSource)); audio_loadStream(src, filename); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.streamMT); break; } } lua_setmetatable(state, -2); return 1; }
static int l_geometry_rectangle(lua_State* state) { const char* type = l_tools_toStringOrError(state, 1); float x = l_tools_toNumberOrError(state, 2); float y = l_tools_toNumberOrError(state, 3); float w = l_tools_toNumberOrError(state, 4); float h = l_tools_toNumberOrError(state, 5); float r = 0; if (lua_tonumber(state, 6)) r = luaL_checknumber(state, 6); float sx = 1; if (lua_tonumber(state, 7)) sx = luaL_checknumber(state, 7); float sy = 1; if (lua_tonumber(state, 8)) sy = luaL_checknumber(state, 8); float ox = 0; if (lua_tonumber(state, 9)) ox = luaL_checknumber(state, 9); float oy = 0; if (lua_tonumber(state, 10)) oy = luaL_checknumber(state, 10); if (strncmp(type,"line",4) == 0) graphics_geometry_fillRectangle(0, x, y, w, h, r, sx, sy, ox, oy); else if(strncmp(type,"fill", 4) == 0) graphics_geometry_fillRectangle(1, x, y, w, h, r, sx, sy, ox, oy); else { luaL_argerror(state,1,"expected string"); lua_error(state); } return 1; }
static int l_audio_newSource(lua_State *state) { char const* filename = l_tools_toStringOrError(state, 1); const char* type = luaL_optstring(state, 2, "static"); int err; if (strcmp(type,"stream") == 0) { audio_StreamSource *src = lua_newuserdata(state, sizeof(audio_StreamSource)); err = audio_loadStream(src, filename); moduleData.audio_type = audio_type_stream; } else if (strcmp(type, "static") == 0) { audio_StaticSource *src = lua_newuserdata(state, sizeof(audio_StaticSource)); err = audio_loadStatic(src, filename); moduleData.audio_type = audio_type_static; } lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.audioDataMT); if(err == -1){ lua_pushstring(state, "Could not load sound file: "); lua_pushstring(state, lua_tostring(state, 1)); lua_pushstring(state, ", reason: unknow file type"); lua_concat(state, 3); return lua_error(state); } else if(err == 0){ lua_pushstring(state, "Could not load sound file: "); lua_pushstring(state, lua_tostring(state, 1)); lua_pushstring(state, ", reason: file does not exist"); lua_concat(state, 3); return lua_error(state); } lua_setmetatable(state, -2); return 1; }
static int l_filesystem_enumerate(lua_State* state) { int n = lua_gettop(state); const char * dir = l_tools_toStringOrError(state, 1); int error = PHYSFS_mount(dir, "/", 1); if (error != 1) return printf("%s %s %s \n", "Error: Directory or archive named : ", dir ," does not exist"); char **rc = PHYSFS_enumerateFiles("/"); char **i; int index = 1; if( n != 1 ) return luaL_error(state, "Function s a single parameter."); lua_newtable(state); for (i = rc; *i != 0; i++) { lua_pushinteger(state, index); lua_pushstring(state, *i); lua_settable(state, -3); index++; } PHYSFS_freeList(rc); return 1; }
static int l_filesystem_getRealDirectory(lua_State* state) { if (!PHYSFS_isInit()) return 0; const char *dir = PHYSFS_getRealDir(l_tools_toStringOrError(state,1)); lua_pushstring(state, dir); return 1; }
static int l_filesystem_setSource(lua_State* state) { const char* p = l_tools_toStringOrError(state, 1); int a = PHYSFS_addToSearchPath(p, 1); if(!a) printf("%s %s \n", "Couldn't set source", p); return 1; }
static int l_graphics_Font_getWrap(lua_State* state) { l_assertType(state, 1, l_graphics_isFont); graphics_Font* font = l_graphics_toFont(state, 1); char const* line = l_tools_toStringOrError(state, 2); int width = l_tools_toNumberOrError(state, 3); lua_pushinteger(state, graphics_Font_getWrap(font, line, width, NULL)); return 1; }
static int l_filesystem_isFile(lua_State* state) { if (!PHYSFS_isInit()) return 0; const char* name = l_tools_toStringOrError(state, 1); if(!PHYSFS_isDirectory(name) && PHYSFS_exists(name)) lua_pushboolean(state, 1); else lua_pushboolean(state, 0); return 1; }
static int l_graphics_Shader_send(lua_State *state) { l_assertType(state, 1, l_graphics_isShader); l_graphics_Shader* shader = l_graphics_toShader(state, 1); char const* name = l_tools_toStringOrError(state, 2); graphics_ShaderUniformInfo const* info = graphics_Shader_getUniform(&shader->shader, name); switch(info->type) { case GL_INT: sendIntegers(state, shader, info); break; case GL_FLOAT: sendFloats(state, shader, info); break; case GL_BOOL: sendBooleans(state, shader, info); break; case GL_INT_VEC2: case GL_INT_VEC3: case GL_INT_VEC4: sendIntegerVectors(state, shader, info); break; case GL_FLOAT_VEC2: case GL_FLOAT_VEC3: case GL_FLOAT_VEC4: sendFloatVectors(state, shader, info); break; case GL_BOOL_VEC2: case GL_BOOL_VEC3: case GL_BOOL_VEC4: sendBooleanVectors(state, shader, info); break; case GL_FLOAT_MAT2: case GL_FLOAT_MAT3: case GL_FLOAT_MAT4: sendFloatMatrices(state, shader, info); break; case GL_SAMPLER_2D: sendSamplers(state, shader, info); break; }; return 0; }
static int l_filesystem_load(lua_State* state) { char const* filename = l_tools_toStringOrError(state, 1); char* data = NULL; int len = filesystem_read(filename, &data); if(len < 0) { lua_pushstring(state, "could not read file"); return lua_error(state); } luaL_loadstring(state, data); free(data); return 1; }
static int l_geometry_circle(lua_State* state) { const char* type = l_tools_toStringOrError(state, 1); float x = l_tools_toNumberOrError(state, 2); float y = l_tools_toNumberOrError(state, 3); float radius = l_tools_toNumberOrError(state, 4); float segments = l_tools_toNumberOrError(state, 5); if (strncmp(type,"line",4) == 0) graphics_geometry_drawCircle(x, y, radius, segments); else if(strncmp(type,"fill", 4) == 0) graphics_geometry_fillCircle(x, y, radius, segments); return 1; }
static int l_filesystem_read(lua_State* state) { // TODO implement max length char const* filename = l_tools_toStringOrError(state, 1); char* data = NULL; int len = filesystem_read(filename, &data); if(len < 0) { lua_pushstring(state, "could not read file"); return lua_error(state); } lua_pushstring(state, data); free(data); lua_pushnumber(state, len); return 2; }
int static l_graphics_newShader(lua_State* state) { char const* vertexSrc = l_tools_toStringOrError(state, 1); char const* fragmentSrc = NULL; char * loadedFile1 = NULL; char * loadedFile2 = NULL; if(lua_isstring(state, 2)) { fragmentSrc = lua_tostring(state, 2); if(!isVertexShader(vertexSrc)) { // TODO int loadedFile1Size = filesystem_read(vertexSrc, &loadedFile1); (void) loadedFile1Size; if(!loadedFile1 || !isVertexShader(loadedFile1)) { free(loadedFile1); lua_pushstring(state, "input 1 is not a valid vertex shader"); return lua_error(state); } vertexSrc = loadedFile1; } if(!isSingleFragmentShader(fragmentSrc)) { // TODO int loadedFile2Size = filesystem_read(fragmentSrc, &loadedFile2); (void)loadedFile2Size; if(!loadedFile2 || !isSingleFragmentShader(loadedFile2)) { free(loadedFile1); free(loadedFile2); lua_pushstring(state, "input 2 is not a valid fragment shader"); return lua_error(state); } fragmentSrc = loadedFile2; } } else { if(isVertexShader(vertexSrc)) { // nothing required } else if(isSingleFragmentShader(vertexSrc)) { fragmentSrc = vertexSrc; vertexSrc = NULL; } else { // TODO int loadedFile1Size = filesystem_read(vertexSrc, &loadedFile1); (void) loadedFile1Size; if(!loadedFile1) { lua_pushstring(state, "could not open file"); return lua_error(state); } if(isSingleFragmentShader(loadedFile1)) { fragmentSrc = loadedFile1; vertexSrc = NULL; } else if(isVertexShader(loadedFile1)) { vertexSrc = loadedFile1; fragmentSrc = NULL; } else { free(loadedFile1); lua_pushstring(state, "input is not a valid shader"); return lua_error(state); } } } l_graphics_Shader * shader = lua_newuserdata(state, sizeof(l_graphics_Shader)); graphics_ShaderCompileStatus status = graphics_Shader_new(&shader->shader, vertexSrc, fragmentSrc); if(status != graphics_ShaderCompileStatus_okay) { pushShaderInfoLog(state, &shader->shader); return lua_error(state); } lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.shaderMT); lua_setmetatable(state, -2); free(loadedFile1); free(loadedFile2); int const textureUnits = shader->shader.textureUnitCount; shader->referencedTextures = malloc(textureUnits * sizeof(int)); for(int i = 0; i < textureUnits; ++i) { shader->referencedTextures[i] = LUA_NOREF; } return 1; }
static int l_filesystem_exists(lua_State* state) { const char* filename = l_tools_toStringOrError(state, 1); return filesystem_exists(filename); }
static int l_system_setClipboardText(lua_State* state) { const char* text = l_tools_toStringOrError(state, 1); system_setClipboardText(text); return 0; }
static int l_geometry_vertex(lua_State* state) { const char* type = l_tools_toStringOrError(state, 1); float x = l_tools_toNumberOrError(state, 2); float y = l_tools_toNumberOrError(state, 3); int _i = 0; int count = 1; if (lua_tonumber(state, 5)) count = luaL_checknumber(state, 5); int dataSize = sizeof(int) * count; if(moduleData.currentDataSize < dataSize){ free(moduleData.vertices); moduleData.vertices = (int*)malloc(dataSize); moduleData.currentDataSize = dataSize; } if(moduleData.vertices == 0) printf("Error: Could not allocate memory for l_geometry_vertex \n"); //Check if we need to draw points of lines if (lua_istable(state, 4)) { //in this case draw lines // Push another reference to the table on top of the stack (so we know // where it is, and this function can work for negative, positive and // pseudo indices lua_pushvalue(state, 4); // stack now contains: -1 => table lua_pushnil(state); // stack now contains: -1 => nil; -2 => table while (lua_next(state, -2)) { // stack now contains: -1 => value; -2 => key; -3 => table // copy the key so that lua_tostring does not modify the original lua_pushvalue(state, -2); // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table const char *key = lua_tostring(state, -1); const char * value = lua_tostring(state, -2); int v = atoi(value); int i = atoi(key); //Put the key and the value of the table into an array moduleData.vertices[_i] = v; // second insert the value of the key _i ++; // pop value + copy of key, leaving original key lua_pop(state, 2); // stack now contains: -1 => key; -2 => table } // stack now contains: -1 => table (when lua_next returns 0 it pops the key // but does not push anything.) // Pop table lua_pop(state, 1); // Stack is now the same as it was on entry to this function } if (strncmp(type,"line",4) == 0) graphics_geometry_vertex(0,x,y,moduleData.vertices,count); else if (strncmp(type, "fill",4) == 0) graphics_geometry_vertex(1,x,y,moduleData.vertices,count); //free(vertices); return 1; }
static int l_filesystem_remove(lua_State* state) { char const* file = l_tools_toStringOrError(state, 1); filesystem_remove(file); return 0; }