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; }
extern "C" int luaopen_love_thread(lua_State *L) { if (instance == 0) { try { instance = new ThreadModule(); lua_getglobal(L, "love"); Thread *curthread = instance->getThread("main"); curthread->lock(); curthread->retain(); curthread->unlock(); luax_newtype(L, "Thread", THREAD_THREAD_T, (void*)curthread); lua_setfield(L, -2, "_curthread"); } catch (Exception & e) { return luaL_error(L, e.what()); } } else instance->retain(); WrappedModule w; w.module = instance; w.name = "thread"; w.flags = MODULE_T; w.functions = module_functions; w.types = types; return luax_register_module(L, w); }
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; }
int w_getRecordedData(lua_State * L) { love::sound::SoundData * sd = instance->getRecordedData(); if (!sd) lua_pushnil(L); else luax_newtype(L, "SoundData", SOUND_SOUND_DATA_T, (void*)sd); return 1; }
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_getChannel(lua_State *L) { std::string name = luax_checkstring(L, 1); Channel *c = instance->getChannel(name); luax_newtype(L, "Channel", THREAD_CHANNEL_T, (void *)c); return 1; }
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_newScreenshot(lua_State * L) { love::image::Image * image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T); love::image::ImageData * i = instance->newScreenshot(image); luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i); return 1; }
int w_newFontData(lua_State * L) { Rasterizer * r = luax_checkrasterizer(L, 1); FontData * f = instance->newFontData(r); luax_newtype(L, "FontData", FONT_FONT_DATA_T, (void*)f); return 1; }
int w_newFileData(lua_State * L) { if(!lua_isstring(L, 1)) return luaL_error(L, "String expected."); if(!lua_isstring(L, 2)) return luaL_error(L, "String expected."); size_t length = 0; const char * str = lua_tolstring(L, 1, &length); const char * filename = lua_tostring(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); 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, "Unrecognized FileData decoder: %s", decstr); } luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void*)t); return 1; }
int w_ChainShape_getChildEdge(lua_State *L) { ChainShape *c = luax_checkchainshape(L, 1); int index = luaL_checkint(L, 2) - 1; // Convert from 1-based index EdgeShape *e = c->getChildEdge(index); luax_newtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, e); return 1; }
int w_newParticleSystem(lua_State * L) { Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T); int size = luaL_checkint(L, 2); ParticleSystem * t = instance->newParticleSystem(image, size); luax_newtype(L, "ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_T, (void*)t); return 1; }
int w_Canvas_getImageData(lua_State * L) { Canvas * canvas = luax_checkcanvas(L, 1); love::image::Image * image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T); love::image::ImageData * img = canvas->getImageData( image ); luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)img); return 1; }
int w_SpriteBatch_getImage(lua_State *L) { SpriteBatch *t = luax_checkspritebatch(L, 1); Image *image = t->getImage(); image->retain(); luax_newtype(L, "Image", GRAPHICS_IMAGE_T, (void *)image); 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_newGlyphData(lua_State * L) { Rasterizer * r = luax_checkrasterizer(L, 1); unsigned short g = (unsigned short)luaL_checkint(L, 2); GlyphData * t = instance->newGlyphData(r, g); luax_newtype(L, "GlyphData", FONT_GLYPH_DATA_T, t); return 1; }
int w_newSpriteBatch(lua_State * L) { Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T); int size = luaL_optint(L, 2, 1000); int usage = luaL_optint(L, 3, SpriteBatch::USAGE_DYNAMIC); SpriteBatch * t = instance->newSpriteBatch(image, size, usage); luax_newtype(L, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T, (void*)t); return 1; }
int w_newEncodedImageData(lua_State * L) { ImageData * t = luax_checkimagedata(L, 1); const char * fmt = luaL_checkstring(L, 2); EncodedImageData::Format f; EncodedImageData::getConstant(fmt, f); EncodedImageData * eid = t->encode(f); luax_newtype(L, "EncodedImageData", IMAGE_ENCODED_IMAGE_DATA_T, (void*)eid); return 1; }
int w_stopRecording(lua_State * L) { if (luax_optboolean(L, 1, true)) { love::sound::SoundData * sd = instance->stopRecording(true); if (!sd) lua_pushnil(L); else luax_newtype(L, "SoundData", SOUND_SOUND_DATA_T, (void*)sd); return 1; } instance->stopRecording(false); return 0; }
int w_getFont(lua_State * L) { Font * f = instance->getFont(); if(f == 0) return 0; f->retain(); luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)f); return 1; }
int w_newGlyph(lua_State * L) { love::font::GlyphData * data = luax_checktype<love::font::GlyphData>(L, 1, "GlyphData", FONT_GLYPH_DATA_T); // Create the image. Glyph * t = new Glyph(data); t->load(); // Push the type. luax_newtype(L, "Glyph", GRAPHICS_GLYPH_T, (void*)t); 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 _wrap_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 = instance->newImageData(w, h); luax_newtype(L, "ImageData", LOVE_IMAGE_IMAGE_DATA_BITS, (void*)t); return 1; } // Convert to File, if necessary. if(lua_isstring(L, 1)) luax_strtofile(L, 1); // Case 2: String/File. love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", LOVE_FILESYSTEM_FILE_BITS); ImageData * t = instance->newImageData(file); luax_newtype(L, "ImageData", LOVE_IMAGE_IMAGE_DATA_BITS, (void*)t); return 1; }
int w_newFile(lua_State * L) { const char * filename = luaL_checkstring(L, 1); File * t; try { t = instance->newFile(filename); } catch(Exception e) { return luaL_error(L, e.what()); } luax_newtype(L, "File", FILESYSTEM_FILE_T, (void*)t); return 1; }
int w_newFileData(lua_State * L) { if(!lua_isstring(L, 1)) return luaL_error(L, "String expected."); if(!lua_isstring(L, 2)) return luaL_error(L, "String expected."); size_t length = 0; const char * str = lua_tolstring(L, 1, &length); const char * filename = lua_tostring(L, 2); FileData * t = instance->newFileData((void*)str, (int)length, filename); luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void*)t); return 1; }
int w_newQuad(lua_State * L) { int x = luaL_checkint(L, 1); int y = luaL_checkint(L, 2); int w = luaL_checkint(L, 3); int h = luaL_checkint(L, 4); int sw = luaL_checkint(L, 5); int sh = luaL_checkint(L, 6); Quad * frame = instance->newQuad(x, y, w, h, sw, sh); if (frame == 0) return luaL_error(L, "Could not create frame."); luax_newtype(L, "Quad", GRAPHICS_QUAD_T, (void*)frame); 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_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_getThreads(lua_State *L) { unsigned count = instance->getThreadCount(); Thread **list = new Thread*[count]; instance->getThreads(list); lua_newtable(L); for (unsigned int i = 0; i<count; i++) { // allow names containing \0 luax_pushstring(L, list[i]->getName()); luax_newtype(L, "Thread", THREAD_THREAD_T, (void*) list[i]); list[i]->lock(); list[i]->retain(); list[i]->unlock(); lua_settable(L, -3); } delete[] list; return 1; }