int w_newSource(lua_State *L) { if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_ID) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_ID)) luax_convobj(L, 1, "sound", "newDecoder"); Source::Type stype = Source::TYPE_STREAM; const char *stypestr = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2); if (stypestr && !Source::getConstant(stypestr, stype)) return luaL_error(L, "Invalid source type: %s", stypestr); if (stype == Source::TYPE_STATIC && luax_istype(L, 1, SOUND_DECODER_ID)) luax_convobj(L, 1, "sound", "newSoundData"); Source *t = 0; luax_catchexcept(L, [&]() { if (luax_istype(L, 1, SOUND_SOUND_DATA_ID)) t = instance()->newSource(luax_totype<love::sound::SoundData>(L, 1, SOUND_SOUND_DATA_ID)); else if (luax_istype(L, 1, SOUND_DECODER_ID)) t = instance()->newSource(luax_totype<love::sound::Decoder>(L, 1, SOUND_DECODER_ID)); }); if (t) { luax_pushtype(L, AUDIO_SOURCE_ID, t); t->release(); return 1; } else return luax_typerror(L, 1, "Decoder or SoundData"); }
int w_newImageFont(lua_State * L) { // Convert to ImageData if necessary. if(lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, DATA_T)) luax_convobj(L, 1, "image", "newImageData"); // Convert to Rasterizer if necessary. if(luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) { int idxs[] = {1, 2}; luax_convobj(L, idxs, 2, "font", "newRasterizer"); } // Convert to FontData, if necessary. if(luax_istype(L, 1, FONT_RASTERIZER_T)) luax_convobj(L, 1, "font", "newFontData"); love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T); // Create the font. Font * font = instance->newFont(data); if(font == 0) return luaL_error(L, "Could not load font."); // Push the type. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font); return 1; }
int w_newImage(lua_State * L) { // Convert to File, if necessary. if(lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); // Convert to ImageData, if necessary. if(luax_istype(L, 1, FILESYSTEM_FILE_T)) luax_convobj(L, 1, "image", "newImageData"); love::image::ImageData * data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); // Create the image. Image * image = 0; try { image = instance->newImage(data); } catch (love::Exception & e) { luaL_error(L, e.what()); } if(image == 0) return luaL_error(L, "Could not load image."); // Push the type. luax_newtype(L, "Image", GRAPHICS_IMAGE_T, (void*)image); return 1; }
int w_ImageData_encode(lua_State *L) { std::string ext; const char *fmt; ImageData::Format format = ImageData::FORMAT_MAX_ENUM; ImageData *t = luax_checkimagedata(L, 1); if (lua_isstring(L, 2)) luax_convobj(L, 2, "filesystem", "newFile"); love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 2, "File", FILESYSTEM_FILE_T); if (lua_isnoneornil(L, 3)) { ext = file->getExtension(); fmt = ext.c_str(); ImageData::getConstant(fmt, format); } else { fmt = luaL_checkstring(L, 3); if (!ImageData::getConstant(fmt, format)) luaL_error(L, "Invalid image format."); } try { t->encode(file, format); } catch(love::Exception &e) { return luaL_error(L, e.what()); } return 0; }
int w_newThread(lua_State *L) { std::string name = luax_checkstring(L, 1); love::Data *data; if (lua_isstring(L, 2)) luax_convobj(L, 2, "filesystem", "newFile"); if (luax_istype(L, 2, FILESYSTEM_FILE_T)) { try { data = luax_checktype<love::filesystem::File>(L, 2, "File", FILESYSTEM_FILE_T)->read(); } catch (love::Exception & e) { return luaL_error(L, e.what()); } } else { data = luax_checktype<love::Data>(L, 2, "Data", DATA_T); data->retain(); } Thread *t = instance->newThread(name, data); // do not worry, file->read() returns retained data data->release(); if (!t) return luaL_error(L, "A thread with that name already exists."); luax_newtype(L, "Thread", THREAD_THREAD_T, (void*)t); return 1; }
int w_newFileData(lua_State *L) { // Single argument: treat as filepath or File. if (lua_gettop(L) == 1) { // We don't use luax_getfiledata because we want to use an ioError. if (lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); // Get FileData from the File. if (luax_istype(L, 1, FILESYSTEM_FILE_ID)) { File *file = luax_checkfile(L, 1); FileData *data = 0; try { data = file->read(); } catch (love::Exception &e) { return luax_ioError(L, "%s", e.what()); } luax_pushtype(L, FILESYSTEM_FILE_DATA_ID, data); data->release(); return 1; } else return luaL_argerror(L, 1, "filename or File expected"); } size_t length = 0; const char *str = luaL_checklstring(L, 1, &length); const char *filename = luaL_checkstring(L, 2); const char *decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0; FileData::Decoder decoder = FileData::FILE; if (decstr && !FileData::getConstant(decstr, decoder)) return luaL_error(L, "Invalid FileData decoder: %s", decstr); FileData *t = 0; switch (decoder) { case FileData::FILE: t = instance()->newFileData((void *)str, (int)length, filename); break; case FileData::BASE64: t = instance()->newFileData(str, filename); break; default: return luaL_error(L, "Invalid FileData decoder: %s", decstr); } luax_pushtype(L, FILESYSTEM_FILE_DATA_ID, t); t->release(); return 1; }
int w_setFont1(lua_State * L) { // The second parameter is an optional int. int size = luaL_optint(L, 2, 12); Font * font; // If the first parameter isn't a Font, create a new one if (!luax_istype(L, 1, GRAPHICS_FONT_T)) { lua_pushinteger(L, size); // push the size lua_insert(L, 2); // move it to its proper place // Convert to File, if necessary. if(lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); // Convert to Data, if necessary. if(luax_istype(L, 1, FILESYSTEM_FILE_T)) { love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); Data * d = f->read(); lua_remove(L, 1); // get rid of the file luax_newtype(L, "Data", DATA_T, (void*)d); lua_insert(L, 1); // put it at the bottom of the stack } // Convert to Rasterizer, if necessary. if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) { int idxs[] = {1, 2}; luax_convobj(L, idxs, 2, "font", "newRasterizer"); } // Convert to FontData, if necessary. if(luax_istype(L, 1, FONT_RASTERIZER_T)) luax_convobj(L, 1, "font", "newFontData"); love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T); // Create the font. font = instance->newFont(data); if(font == 0) return luaL_error(L, "Could not load font."); } else font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T); instance->setFont(font); return 0; }
int w_newImageData(lua_State * L) { // Case 1: Integers. if (lua_isnumber(L, 1)) { int w = luaL_checkint(L, 1); int h = luaL_checkint(L, 2); ImageData * t = 0; try { t = instance->newImageData(w, h); } catch (love::Exception & e) { return luaL_error(L, e.what()); } luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t); return 1; } // Case 2: Data if (luax_istype(L, 1, DATA_T)) { Data * d = luax_checktype<Data>(L, 1, "Data", DATA_T); ImageData * t = 0; try { t = instance->newImageData(d); } catch (love::Exception & e) { return luaL_error(L, e.what()); } luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t); return 1; } // Case 3: String/File. // Convert to File, if necessary. if (lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); ImageData * t = 0; try { t = instance->newImageData(file); } catch (love::Exception & e) { return luaL_error(L, e.what()); } luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void*)t); return 1; }
int w_newThread(lua_State *L) { std::string name = "Thread code"; love::Data *data = nullptr; if (lua_isstring(L, 1)) { size_t slen = 0; const char *str = lua_tolstring(L, 1, &slen); // Treat the string as Lua code if it's long or has a newline. if (slen >= 1024 || memchr(str, '\n', slen)) { // Construct a FileData from the string. lua_pushvalue(L, 1); lua_pushstring(L, "string"); int idxs[] = {lua_gettop(L) - 1, lua_gettop(L)}; luax_convobj(L, idxs, 2, "filesystem", "newFileData"); lua_pop(L, 1); lua_replace(L, 1); } else luax_convobj(L, 1, "filesystem", "newFileData"); } else if (luax_istype(L, 1, love::filesystem::File::type)) luax_convobj(L, 1, "filesystem", "newFileData"); if (luax_istype(L, 1, love::filesystem::FileData::type)) { love::filesystem::FileData *fdata = luax_checktype<love::filesystem::FileData>(L, 1); name = std::string("@") + fdata->getFilename(); data = fdata; } else { data = luax_checktype<love::Data>(L, 1); } LuaThread *t = instance()->newThread(name, data); luax_pushtype(L, t); t->release(); return 1; }
int w_newFont1(lua_State * L) { // Convert to File, if necessary. if(lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); // Convert to Data, if necessary. if(luax_istype(L, 1, FILESYSTEM_FILE_T)) { love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); Data * d = f->read(); lua_remove(L, 1); // get rid of the file luax_newtype(L, "Data", DATA_T, (void*)d); lua_insert(L, 1); // put it at the bottom of the stack } // Convert to Rasterizer, if necessary. if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) { int idxs[] = {1, 2}; luax_convobj(L, idxs, 2, "font", "newRasterizer"); } // Convert to FontData, if necessary. if(luax_istype(L, 1, FONT_RASTERIZER_T)) luax_convobj(L, 1, "font", "newFontData"); love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T); // Create the font. Font * font = instance->newFont(data); if(font == 0) return luaL_error(L, "Could not load font."); // Push the type. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font); return 1; }
int w_newImageData(lua_State *L) { // Case 1: Integers. if (lua_isnumber(L, 1)) { int w = luaL_checkint(L, 1); int h = luaL_checkint(L, 2); if (w <= 0 || h <= 0) return luaL_error(L, "Invalid image size."); ImageData *t = 0; try { t = instance->newImageData(w, h); } catch(love::Exception &e) { return luaL_error(L, "%s", e.what()); } luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)t); return 1; } // Case 2: File(Data). // Convert to FileData, if necessary. if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T)) luax_convobj(L, 1, "filesystem", "newFileData"); love::filesystem::FileData *data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); ImageData *t = 0; try { t = instance->newImageData(data); } catch (love::Exception &e) { return luaL_error(L, "%s", e.what()); } luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *) t); return 1; }
int w_isCompressed(lua_State *L) { // Convert to FileData, if necessary. if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T)) luax_convobj(L, 1, "filesystem", "newFileData"); love::filesystem::FileData *data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); bool compressed = false; try { compressed = instance->isCompressed(data); } catch (love::Exception &e) { return luaL_error(L, "%s", e.what()); } luax_pushboolean(L, compressed); return 1; }
int w_newCompressedData(lua_State *L) { // Convert to FileData, if necessary. if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T)) luax_convobj(L, 1, "filesystem", "newFileData"); love::filesystem::FileData *data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); CompressedData *t = 0; try { t = instance->newCompressedData(data); } catch(love::Exception &e) { return luaL_error(L, "%s", e.what()); } luax_newtype(L, "CompressedData", IMAGE_COMPRESSED_DATA_T, (void *) t); return 1; }
int w_newThread(lua_State *L) { std::string name = "Thread code"; love::Data *data; if (lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); if (luax_istype(L, 1, FILESYSTEM_FILE_T)) { try { love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); name = std::string("@") + file->getFilename(); data = file->read(); } catch (love::Exception & e) { return luaL_error(L, "%s", e.what()); } } else if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T)) { love::filesystem::FileData * fdata = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); name = std::string("@") + fdata->getFilename(); data = fdata; data->retain(); } else { data = luax_checktype<love::Data>(L, 1, "Data", DATA_T); data->retain(); } LuaThread *t = instance->newThread(name, data); // do not worry, file->read() returns retained data data->release(); luax_newtype(L, "Thread", THREAD_THREAD_T, (void *)t); return 1; }
int w_newDecoder(lua_State *L) { // Convert to File, if necessary. if (lua_isstring(L, 1)) luax_convobj(L, 1, "filesystem", "newFile"); love::filesystem::FileData *data; if (luax_istype(L, 1, FILESYSTEM_FILE_T)) { love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T); data = file->read(); } else { data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T); data->retain(); } int bufferSize = luaL_optint(L, 2, Decoder::DEFAULT_BUFFER_SIZE); try { Decoder *t = instance->newDecoder(data, bufferSize); data->release(); if (t == 0) return luaL_error(L, "Extension \"%s\" not supported.", data->getExtension().c_str()); luax_newtype(L, "Decoder", SOUND_DECODER_T, (void *)t); } catch(love::Exception &e) { data->release(); return luaL_error(L, e.what()); } return 1; }
int luax_filetodata(lua_State * L, int idx) { return luax_convobj(L, idx, "filesystem", "read"); }
int luax_strtofile(lua_State * L, int idx) { return luax_convobj(L, idx, "filesystem", "newFile"); }
static void convimagedata(lua_State *L, int idx) { if (lua_type(L, 1) == LUA_TSTRING || luax_istype(L, idx, FILESYSTEM_FILE_ID) || luax_istype(L, idx, FILESYSTEM_FILE_DATA_ID)) luax_convobj(L, idx, "image", "newImageData"); }