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; }
FileData *luax_getfiledata(lua_State *L, int idx) { FileData *data = nullptr; File *file = nullptr; if (lua_isstring(L, idx) || luax_istype(L, idx, FILESYSTEM_FILE_ID)) { file = luax_getfile(L, idx); file->retain(); } else if (luax_istype(L, idx, FILESYSTEM_FILE_DATA_ID)) { data = luax_checkfiledata(L, idx); data->retain(); } if (!data && !file) { luaL_argerror(L, idx, "filename, File, or FileData expected"); return nullptr; // Never reached. } if (file) { luax_catchexcept(L, [&]() { data = file->read(); }, [&](bool) { file->release(); } ); } return data; }
int w_newRasterizer(lua_State *L) { Rasterizer *t = NULL; try { if (luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) { love::image::ImageData *d = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T); const char *g = luaL_checkstring(L, 2); std::string glyphs(g); t = instance->newRasterizer(d, glyphs); } else if (luax_istype(L, 1, DATA_T)) { Data *d = luax_checkdata(L, 1); int size = luaL_checkint(L, 2); t = instance->newRasterizer(d, size); } } catch (love::Exception &e) { return luaL_error(L, "%s", e.what()); } luax_newtype(L, "Rasterizer", FONT_RASTERIZER_T, t); 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_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; }
static inline int w_SpriteBatch_add_or_set(lua_State *L, SpriteBatch *t, int startidx, int index) { Quad *quad = nullptr; if (luax_istype(L, startidx, GRAPHICS_QUAD_ID)) { quad = luax_totype<Quad>(L, startidx, GRAPHICS_QUAD_ID); startidx++; } else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1)) return luax_typerror(L, startidx, "Quad"); float x = (float) luaL_optnumber(L, startidx + 0, 0.0); float y = (float) luaL_optnumber(L, startidx + 1, 0.0); float a = (float) luaL_optnumber(L, startidx + 2, 0.0); float sx = (float) luaL_optnumber(L, startidx + 3, 1.0); float sy = (float) luaL_optnumber(L, startidx + 4, sx); float ox = (float) luaL_optnumber(L, startidx + 5, 0.0); float oy = (float) luaL_optnumber(L, startidx + 6, 0.0); float kx = (float) luaL_optnumber(L, startidx + 7, 0.0); float ky = (float) luaL_optnumber(L, startidx + 8, 0.0); luax_catchexcept(L, [&]() { if (quad) index = t->addq(quad, x, y, a, sx, sy, ox, oy, kx, ky, index); else index = t->add(x, y, a, sx, sy, ox, oy, kx, ky, index); }); return index; }
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_newSource1(lua_State * L) { Source * t = 0; if(luax_istype(L, 1, SOUND_SOUND_DATA_T)) t = instance->newSource(luax_totype<love::sound::SoundData>(L, 1, "SoundData", SOUND_SOUND_DATA_T)); else if(luax_istype(L, 1, SOUND_DECODER_T)) t = instance->newSource(luax_totype<love::sound::Decoder>(L, 1, "Decoder", SOUND_DECODER_T)); if(t) { luax_newtype(L, "Source", AUDIO_SOURCE_T, (void*)t); return 1; } else return luaL_error(L, "No matching overload"); return 0; }
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_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_newSoundData(lua_State *L) { SoundData *t = 0; if (lua_isnumber(L, 1)) { int samples = luaL_checkint(L, 1); int sampleRate = luaL_optint(L, 2, Decoder::DEFAULT_SAMPLE_RATE); int bits = luaL_optint(L, 3, Decoder::DEFAULT_BITS); int channels = luaL_optint(L, 4, Decoder::DEFAULT_CHANNELS); try { t = instance->newSoundData(samples, sampleRate, bits, channels); } catch(love::Exception &e) { return luaL_error(L, e.what()); } } // Must be string or decoder. else { // Convert to Decoder, if necessary. if (!luax_istype(L, 1, SOUND_DECODER_T)) { w_newDecoder(L); lua_replace(L, 1); } try { t = instance->newSoundData(luax_checkdecoder(L, 1)); } catch(love::Exception &e) { return luaL_error(L, e.what()); } } luax_newtype(L, "SoundData", SOUND_SOUND_DATA_T, (void *)t); 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_File_write(lua_State *L) { File *file = luax_checkfile(L, 1); bool result = false; if (lua_isstring(L, 2)) { try { size_t datasize = 0; const char *data = lua_tolstring(L, 2, &datasize); if (!lua_isnoneornil(L, 3)) datasize = luaL_checkinteger(L, 3); result = file->write(data, datasize); } catch (love::Exception &e) { return luax_ioError(L, "%s", e.what()); } } else if (luax_istype(L, 2, DATA_ID)) { try { love::Data *data = luax_totype<love::Data>(L, 2, DATA_ID); result = file->write(data, luaL_optinteger(L, 3, data->getSize())); } catch (love::Exception &e) { return luax_ioError(L, "%s", e.what()); } } else { return luaL_argerror(L, 2, "string or data expected"); } luax_pushboolean(L, result); 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_newTrueTypeRasterizer(lua_State *L) { Rasterizer *t = nullptr; TrueTypeRasterizer::Hinting hinting = TrueTypeRasterizer::HINTING_NORMAL; if (lua_type(L, 1) == LUA_TNUMBER || lua_isnone(L, 1)) { // First argument is a number: use the default TrueType font. int size = (int) luaL_optnumber(L, 1, 12); const char *hintstr = lua_isnoneornil(L, 2) ? nullptr : luaL_checkstring(L, 2); if (hintstr && !TrueTypeRasterizer::getConstant(hintstr, hinting)) return luaL_error(L, "Invalid TrueType font hinting mode: %s", hintstr); luax_catchexcept(L, [&](){ t = instance()->newTrueTypeRasterizer(size, hinting); }); } else { love::Data *d = nullptr; if (luax_istype(L, 1, DATA_ID)) d = luax_checkdata(L, 1); else d = filesystem::luax_getfiledata(L, 1); int size = (int) luaL_optnumber(L, 2, 12); const char *hintstr = lua_isnoneornil(L, 3) ? nullptr : luaL_checkstring(L, 3); if (hintstr && !TrueTypeRasterizer::getConstant(hintstr, hinting)) return luaL_error(L, "Invalid TrueType font hinting mode: %s", hintstr); luax_catchexcept(L, [&]() { t = instance()->newTrueTypeRasterizer(d, size, hinting); }, [&](bool) { d->release(); } ); } luax_pushtype(L, FONT_RASTERIZER_ID, t); t->release(); 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; }
static int w_write_or_append(lua_State *L, File::Mode mode) { const char *filename = luaL_checkstring(L, 1); const char *input = 0; size_t len = 0; if (luax_istype(L, 2, DATA_ID)) { love::Data *data = luax_totype<love::Data>(L, 2, DATA_ID); input = (const char *) data->getData(); len = data->getSize(); } else if (lua_isstring(L, 2)) input = lua_tolstring(L, 2, &len); else return luaL_argerror(L, 2, "string or Data expected"); // Get how much we should write. Length of string default. len = luaL_optinteger(L, 3, len); try { if (mode == File::MODE_APPEND) instance()->append(filename, (const void *) input, len); else instance()->write(filename, (const void *) input, len); } catch (love::Exception &e) { return luax_ioError(L, "%s", e.what()); } luax_pushboolean(L, true); 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; }
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"); }