mirror of
https://github.com/love2d/love.git
synced 2026-08-18 19:54:22 +02:00
Updated the Lua wrapper code for modules to account for cases where the module's instance is removed from memory completely and recreated during the program's lifetime.
This commit is contained in:
@@ -40,6 +40,9 @@ class Font : public Module
|
||||
|
||||
public:
|
||||
|
||||
Font() { moduleType = M_FONT; }
|
||||
virtual ~Font() {}
|
||||
|
||||
virtual Rasterizer *newRasterizer(Data *data, int size) = 0;
|
||||
virtual Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
|
||||
virtual Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace font
|
||||
namespace freetype
|
||||
{
|
||||
|
||||
static Font *instance = nullptr;
|
||||
#define instance() (Module::getInstance<Font>(Module::M_FONT))
|
||||
|
||||
int w_newRasterizer(lua_State *L)
|
||||
{
|
||||
@@ -47,14 +47,14 @@ int w_newRasterizer(lua_State *L)
|
||||
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);
|
||||
luax_catchexcept(L, [&](){ t = instance->newRasterizer(d, glyphs); });
|
||||
luax_catchexcept(L, [&](){ t = instance()->newRasterizer(d, glyphs); });
|
||||
}
|
||||
else if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
|
||||
{
|
||||
love::filesystem::FileData *d = love::filesystem::luax_getfiledata(L, 1);
|
||||
int size = luaL_checkint(L, 2);
|
||||
luax_catchexcept(L,
|
||||
[&]() { t = instance->newRasterizer(d, size); },
|
||||
[&]() { t = instance()->newRasterizer(d, size); },
|
||||
[&]() { d->release(); }
|
||||
);
|
||||
}
|
||||
@@ -72,12 +72,12 @@ int w_newGlyphData(lua_State *L)
|
||||
if (lua_type(L, 2) == LUA_TSTRING)
|
||||
{
|
||||
std::string glyph = luax_checkstring(L, 2);
|
||||
luax_catchexcept(L, [&](){ t = instance->newGlyphData(r, glyph); });
|
||||
luax_catchexcept(L, [&](){ t = instance()->newGlyphData(r, glyph); });
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32 g = (uint32) luaL_checknumber(L, 2);
|
||||
t = instance->newGlyphData(r, g);
|
||||
t = instance()->newGlyphData(r, g);
|
||||
}
|
||||
|
||||
luax_pushtype(L, "GlyphData", FONT_GLYPH_DATA_T, t);
|
||||
@@ -101,9 +101,10 @@ static const lua_CFunction types[] =
|
||||
|
||||
extern "C" int luaopen_love_font(lua_State *L)
|
||||
{
|
||||
Font *instance = instance();
|
||||
if (instance == nullptr)
|
||||
{
|
||||
luax_catchexcept(L, [](){ instance = new Font(); });
|
||||
luax_catchexcept(L, [&](){ instance = new Font(); });
|
||||
}
|
||||
else
|
||||
instance->retain();
|
||||
|
||||
Reference in New Issue
Block a user