Modules now reside in Lua and are treated (almost) like normal objects.

Also added a way of getting a pointer to abstract module instances internally.
This commit is contained in:
rude
2009-09-02 21:18:13 +02:00
parent bea504af2b
commit 623e701549
22 changed files with 247 additions and 176 deletions
-1
View File
@@ -22,7 +22,6 @@ AC_SEARCH_LIBS(
AC_MSG_ERROR([Can't LÖVE without Lua])
)
AC_SEARCH_LIBS([ilInit], [IL], [], AC_MSG_ERROR([Can't LÖVE without DevIL]))
AC_SEARCH_LIBS([iluInit], [ILU], [], AC_MSG_ERROR([Can't LÖVE without ILU]))
AC_SEARCH_LIBS([mng_initialize], [mng], [], AC_MSG_ERROR([DevIL needs MNG]))
AC_SEARCH_LIBS([TIFFOpen], [tiff], [], AC_MSG_ERROR([DevIL needs TIFF]))
AC_SEARCH_LIBS([FT_Load_Glyph], [freetype], [], AC_MSG_ERROR([Can't LÖVE without FreeType]))
+2 -3
View File
@@ -24,15 +24,14 @@
// LOVE
#include "runtime.h"
#include "Exception.h"
#include "Object.h"
namespace love
{
/**
* Abstract superclass for all modules.
*
* @author Anders Ruud
**/
class Module
class Module : public Object
{
public:
+30 -39
View File
@@ -37,23 +37,12 @@ namespace love
**/
static int w__gc(lua_State * L)
{
UserData * u = (UserData *)lua_touserdata(L, 1);
Proxy * u = (Proxy *)lua_touserdata(L, 1);
Object * t = (Object *)u->data;
t->release();
return 0;
}
/**
* Special garbage collector for Modules.
**/
static int w__Module_gc(lua_State * L)
{
UserData * u = (UserData *)lua_touserdata(L, 1);
Module * t = (Module *)u->data;
delete t;
return 0;
}
Reference * luax_refif(lua_State * L, int type)
{
Reference * r = 0;
@@ -115,38 +104,36 @@ namespace love
return 0;
}
int luax_register_gc(lua_State * L, Module * m)
int luax_register_module(lua_State * L, const WrappedModule & m)
{
luax_getregistry(L, REGISTRY_GC);
// Put a reference to the C++ module in Lua.
luax_getregistry(L, REGISTRY_MODULES);
UserData * u = (UserData *)lua_newuserdata(L, sizeof(UserData));
u->own = true;
u->data = m;
Proxy * p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
p->own = true;
p->data = m.module;
p->flags = m.flags;
luaL_newmetatable(L, m->getName());
lua_pushcfunction(L, w__Module_gc);
luaL_newmetatable(L, m.module->getName());
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, w__gc);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
lua_setfield(L, -2, m.name); // _modules[name] = proxy
lua_pop(L, 1);
lua_setfield(L, -2, m->getName());
lua_pop(L, 1); // Registry
return 0;
}
int luax_register_module(lua_State * L, const luaL_Reg * fn, const lua_CFunction * types, const LuaConstant * constants, const char * name)
{
// Gets the love table.
luax_insistglobal(L, "love");
// Install constants.
if(constants != 0)
if(m.constants != 0)
{
for(int i = 0; constants[i].name != 0; i++)
for(int i = 0; m.constants[i].name != 0; i++)
{
lua_pushinteger(L, constants[i].value);
lua_setfield(L, -2, constants[i].name);
lua_pushinteger(L, m.constants[i].value);
lua_setfield(L, -2, m.constants[i].name);
}
}
@@ -154,14 +141,14 @@ namespace love
lua_newtable(L);
// Register all the functions.
luaL_register(L, 0, fn);
luaL_register(L, 0, m.functions);
// Register types.
if(types != 0)
for(const lua_CFunction * t = types; *t != 0; t++)
if(m.types != 0)
for(const lua_CFunction * t = m.types; *t != 0; t++)
(*t)(L);
lua_setfield(L, -2, name); // love.graphics = table
lua_setfield(L, -2, m.name); // love.graphics = table
lua_pop(L, 1); // love
return 0;
@@ -189,7 +176,9 @@ namespace love
lua_pushcfunction(L, w__gc);
lua_setfield(L, -2, "__gc");
luaL_register(L, 0, f);
if(f != 0)
luaL_register(L, 0, f);
lua_pop(L, 1); // Pops metatable.
return 0;
}
@@ -217,7 +206,7 @@ namespace love
void luax_newtype(lua_State * L, const char * name, bits flags, void * data, bool own)
{
UserData * u = (UserData *)lua_newuserdata(L, sizeof(UserData));
Proxy * u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
u->data = data;
u->flags = flags;
@@ -232,7 +221,7 @@ namespace love
if(lua_isuserdata(L, idx) == 0)
return false;
return ((((UserData *)lua_touserdata(L, idx))->flags & type) == type);
return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type);
}
int luax_getfunction(lua_State * L, const char * mod, const char * fn)
@@ -318,6 +307,8 @@ namespace love
{
case REGISTRY_GC:
return luax_insistlove(L, "_gc");
case REGISTRY_MODULES:
return luax_insistlove(L, "_modules");
default:
return luaL_error(L, "Attempted to use invalid registry.");
}
+62 -25
View File
@@ -44,31 +44,61 @@ namespace love
enum Registry
{
REGISTRY_GC = 1,
REGISTRY_MODULES,
};
/**
* This structure wraps all Lua-exposed objects. It exists in the
* Lua state as a full UserData (so we can catch __gc "events"),
* though the Object it refers to is light UserData in the sense
* Lua state as a full userdata (so we can catch __gc "events"),
* though the Object it refers to is light userdata in the sense
* that it is not allocated by the Lua VM.
**/
struct UserData
struct Proxy
{
bits flags; // Holds type information (see types.h).
// Holds type information (see types.h).
bits flags;
// The light userdata.
void * data;
bool own; // True if Lua should delete on GC.
// True if Lua should delete on GC.
bool own;
};
/**
* Used to store constants in arrays.
**/
struct LuaConstant
struct Constant
{
const char * name;
int value;
};
/**
* A Module with Lua wrapper functions and other data.
**/
struct WrappedModule
{
// The module containing the functions.
Module * module;
// The name for the table to put the functions in, without the 'love'-prefix.
const char * name;
// The type flags of this module.
love::bits flags;
// The functions of the module (last element {0,0}).
const luaL_Reg * functions;
// A list of functions which expose the types of the modules (last element 0).
const lua_CFunction * types;
// A list of constants (last element 0).
const Constant * constants;
};
/**
* Returns a reference to the top stack element (-1) if the value
* is of the specified type. If the value is incorrect, zero is returned.
@@ -134,22 +164,10 @@ namespace love
int luax_assert_function(lua_State * L, int idx);
/**
* Register a dummy UserData (full) for a module. This enables us to catch the
* call to __gc, and properly call the destructor on the Module in question.
* Register a module in the love table. The love table will be created if it does not exist.
* @param L The Lua state.
* @param module The Module to register the dummy UserData for.
**/
int luax_register_gc(lua_State * L, Module * module);
/**
* Register a module in the love table. The love table will created if it does not exist.
* @param L The Lua state.
* @param f The functions of the module (last element {0,0}).
* @param t A list of functions which expose the types of the modules (last element 0).
* @param c A list of constants (last element 0).
* @param name The name for the table to put the functions in, without the 'love'-prefix.
**/
int luax_register_module(lua_State * L, const luaL_Reg * f, const lua_CFunction * t, const LuaConstant * c, const char * name);
int luax_register_module(lua_State * L, const WrappedModule & m);
/**
* Inserts a module with 'name' into the package.preloaded table.
@@ -164,7 +182,7 @@ namespace love
* even from other modules.
* @param f The list of member functions for the type.
**/
int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f);
int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f = 0);
/**
* Register a new searcher function for package.loaders. This can for instance enable
@@ -189,7 +207,7 @@ namespace love
* @param L The Lua state.
* @param idx The index on the stack.
* @param type The type to check for.
* @return True if the value is UserData of the specified type, false otherwise.
* @return True if the value is Proxy of the specified type, false otherwise.
**/
bool luax_istype(lua_State * L, int idx, love::bits type);
@@ -258,11 +276,11 @@ namespace love
template <typename T>
T * luax_totype(lua_State * L, int idx, const char * name, love::bits type)
{
return (T*)(((UserData *)lua_touserdata(L, idx))->data);
return (T*)(((Proxy *)lua_touserdata(L, idx))->data);
}
/**
* Like luax_totype, but causes an error if the value at idx is not UserData,
* Like luax_totype, but causes an error if the value at idx is not Proxy,
* or is not the specified type.
* @param L The Lua state.
* @param idx The index on the stack.
@@ -275,7 +293,7 @@ namespace love
if(lua_isuserdata(L, idx) == 0)
luaL_error(L, "Incorrect parameter type: expected userdata.");
UserData * u = (UserData *)lua_touserdata(L, idx);
Proxy * u = (Proxy *)lua_touserdata(L, idx);
if((u->flags & type) != type)
luaL_error(L, "Incorrect parameter type: expected %s", name);
@@ -283,6 +301,25 @@ namespace love
return (T *)u->data;
}
template <typename T>
T * luax_getmodule(lua_State * L, const char * k, love::bits type)
{
luax_getregistry(L, REGISTRY_MODULES);
lua_getfield(L, -1, k);
if(!lua_isuserdata(L, -1))
luaL_error(L, "Tried to get nonexisting module %s.", k);
Proxy * u = (Proxy *)lua_touserdata(L, -1);
if((u->flags & type) != type)
luaL_error(L, "Incorrect module %s", k);
lua_pop(L, 2);
return (T*)u->data;
}
} // love
#endif // LOVE_RUNTIME_H
+13 -1
View File
@@ -26,11 +26,12 @@
namespace love
{
enum
enum Type
{
// Cross-module types.
OBJECT_ID = 0,
DATA_ID,
MODULE_ID,
// Filesystem.
FILESYSTEM_FILE_ID,
@@ -79,6 +80,11 @@ namespace love
PHYSICS_REVOLUTE_JOINT_ID,
PHYSICS_PULLEY_JOINT_ID,
PHYSICS_GEAR_JOINT_ID,
// The modules themselves. Only add abstracted modules here.
MODULE_FILESYSTEM_ID,
MODULE_IMAGE_ID,
MODULE_SOUND_ID,
// Count the number of bits needed.
BIT_SIZE
@@ -88,6 +94,7 @@ namespace love
const bits OBJECT_T = bits(1) << OBJECT_ID;
const bits DATA_T = (bits(1) << DATA_ID) | OBJECT_T;
const bits MODULE_T = (bits(1) << MODULE_ID) | OBJECT_T;
// Filesystem.
const bits FILESYSTEM_FILE_T = (bits(1) << FILESYSTEM_FILE_ID) | OBJECT_T;
@@ -136,6 +143,11 @@ namespace love
const bits PHYSICS_PULLEY_JOINT_T = (bits(1) << PHYSICS_PULLEY_JOINT_ID) | PHYSICS_JOINT_T;
const bits PHYSICS_GEAR_JOINT_T = (bits(1) << PHYSICS_GEAR_JOINT_ID) | PHYSICS_JOINT_T;
// Modules.
const bits MODULE_FILESYSTEM_T = (bits(1) << MODULE_FILESYSTEM_ID) | MODULE_T;
const bits MODULE_IMAGE_T = (bits(1) << MODULE_IMAGE_ID) | MODULE_T;
const bits MODULE_SOUND_T = (bits(1) << MODULE_SOUND_ID) | MODULE_T;
} // love
#endif // LOVE_TYPES_H
+9 -3
View File
@@ -266,9 +266,15 @@ namespace audio
if(instance == 0)
return luaL_error(L, "Could not open any audio module.");
luax_register_gc(L, instance);
return luax_register_module(L, functions, types, 0, "audio");
WrappedModule w;
w.module = instance;
w.name = "audio";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
w.constants = 0;
return luax_register_module(L, w);
}
} // audio
+10 -4
View File
@@ -72,7 +72,7 @@ namespace sdl
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "event_keypressed", Event::EVENT_KEYDOWN },
{ "event_keyreleased", Event::EVENT_KEYUP },
{ "event_mousepressed", Event::EVENT_MOUSEBUTTONDOWN },
@@ -97,9 +97,15 @@ namespace sdl
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, 0, constants, "event");
WrappedModule w;
w.module = instance;
w.name = "event";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = constants;
return luax_register_module(L, w);
}
} // sdl
@@ -252,7 +252,7 @@ namespace physfs
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "file_closed", File::CLOSED },
{ "file_read", File::READ },
{ "file_write", File::WRITE },
@@ -275,9 +275,15 @@ namespace physfs
}
}
luax_register_gc(L, instance);
WrappedModule w;
w.module = instance;
w.name = "filesystem";
w.flags = MODULE_FILESYSTEM_T;
w.functions = functions;
w.types = types;
w.constants = constants;
return luax_register_module(L, functions, types, constants, "filesystem");
return luax_register_module(L, w);
}
} // physfs
+9 -3
View File
@@ -80,9 +80,15 @@ namespace freetype
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, types, 0, "font");
WrappedModule w;
w.module = instance;
w.name = "font";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
w.constants = 0;
return luax_register_module(L, w);
}
} // freetype
+2 -61
View File
@@ -22,10 +22,6 @@
// LOVE
#include <common/config.h>
#include <image/devil/Image.h>
// IL
#include <IL/ilu.h>
namespace love
{
@@ -1177,70 +1173,15 @@ namespace opengl
return 0;
}
love::image::ImageData * Graphics::newScreenshot(lua_State * L)
love::image::ImageData * Graphics::newScreenshot(love::image::Image * image)
{
int w = getWidth();
int h = getHeight();
love::image::devil::Image * i = new love::image::devil::Image();
love::image::ImageData * img = i->newImageData(w, h);
GLubyte * pixels = new GLubyte[4*w*h];
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
try {
img = i->newImageData(w, h, pixels);
} catch (Exception & e) {
luaL_error(L, e.what());
}
// the screenshot is upside down, let's fix this
iluFlipImage();
delete [] pixels;
return img;
// TODO: update this.
/*
int w = getWidth();
int h = getHeight();
// Declare some storage.
GLubyte * pixels = new GLubyte[3*w*h];
GLubyte * screenshot = new GLubyte[3*w*h];
ILuint image;
// Read the pixels on the screen.
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
ilGenImages(1, &image);
ilBindImage(image);
ilTexImage(w, h, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, (ILvoid*)pixels);
delete [] pixels;
// Create the image.
ilSaveL(IL_BMP, screenshot, 3*w*h);
bool success = true;
if(file->open())
{
if(!file->write((const char *)screenshot, 3*w*h))
success = false;
file->close();
}
else
success = false;
delete [] screenshot;
ilDeleteImages(1, &image);
*/
return false;
return image->newImageData(w, h, (void*)pixels);
}
void Graphics::push()
+2 -1
View File
@@ -33,6 +33,7 @@
// LOVE
#include <graphics/Graphics.h>
#include <image/Image.h>
#include <image/ImageData.h>
#include "Image.h"
@@ -518,7 +519,7 @@ namespace opengl
* Creates a screenshot of the view and saves it to the default folder.
* @param file The file to write the screenshot to.
**/
love::image::ImageData * newScreenshot(lua_State * L);
love::image::ImageData * newScreenshot(love::image::Image * image);
void push();
void pop();
+12 -4
View File
@@ -479,7 +479,8 @@ namespace opengl
int w_newScreenshot(lua_State * L)
{
love::image::ImageData * i = instance->newScreenshot(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;
}
@@ -795,7 +796,7 @@ namespace opengl
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "align_left", Graphics::ALIGN_LEFT },
{ "align_right", Graphics::ALIGN_RIGHT },
@@ -860,8 +861,15 @@ namespace opengl
}
}
luax_register_gc(L, instance);
luax_register_module(L, functions, types, constants, "graphics");
WrappedModule w;
w.module = instance;
w.name = "graphics";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
w.constants = constants;
luax_register_module(L, w);
# include <scripts/graphics.lua.h>
+1 -1
View File
@@ -99,7 +99,7 @@ namespace devil
// Bind the image.
ilBindImage(image);
// Try to load the data.
bool success = ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data);
bool success = (ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data) == IL_TRUE);
int err = ilGetError();
if (err != IL_NO_ERROR){
switch (err) {
+9 -3
View File
@@ -92,9 +92,15 @@ namespace image
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, types, 0, "image");
WrappedModule w;
w.module = instance;
w.name = "image";
w.flags = MODULE_IMAGE_T;
w.functions = functions;
w.types = types;
w.constants = 0;
return luax_register_module(L, w);
}
} // image
+10 -3
View File
@@ -146,7 +146,7 @@ namespace sdl
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "joystick_axis_horizontal", Joystick::JOYSTICK_AXIS_HORIZONTAL },
{ "joystick_axis_vertical", Joystick::JOYSTICK_AXIS_VERITCAL },
@@ -176,9 +176,16 @@ namespace sdl
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, 0, constants, "joystick");
WrappedModule w;
w.module = instance;
w.name = "joystick";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = constants;
return luax_register_module(L, w);
}
} // sdl
+10 -4
View File
@@ -65,7 +65,7 @@ namespace sdl
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "key_unknown", 0 },
{ "key_first", 0 },
{ "key_backspace", 8 },
@@ -232,9 +232,15 @@ namespace sdl
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, 0, constants, "keyboard");
WrappedModule w;
w.module = instance;
w.name = "keyboard";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = constants;
return luax_register_module(L, w);
}
} // sdl
+10 -4
View File
@@ -105,7 +105,7 @@ namespace sdl
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "mouse_left", Mouse::MOUSE_LEFT },
{ "mouse_middle", Mouse::MOUSE_MIDDLE },
{ "mouse_right", Mouse::MOUSE_RIGHT },
@@ -129,9 +129,15 @@ namespace sdl
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, 0, constants, "mouse");
WrappedModule w;
w.module = instance;
w.name = "mouse";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = constants;
return luax_register_module(L, w);
}
} // sdl
+9 -3
View File
@@ -137,9 +137,15 @@ namespace tcc
luax_register_searcher(L, searcher);
luax_register_gc(L, instance);
return luax_register_module(L, functions, 0, 0, "native");
WrappedModule w;
w.module = instance;
w.name = "native";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = 0;
return luax_register_module(L, w);
}
} // tcc
+10 -2
View File
@@ -237,7 +237,7 @@ namespace box2d
};
// List of constants.
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "shape_circle", Shape::SHAPE_CIRCLE },
{ "shape_polygon", Shape::SHAPE_POLYGON },
@@ -264,7 +264,15 @@ namespace box2d
}
}
return luax_register_module(L, functions, types, constants, "physics");
WrappedModule w;
w.module = instance;
w.name = "physics";
w.flags = MODULE_T;
w.functions = functions;
w.types = types;
w.constants = constants;
return luax_register_module(L, w);
}
} // box2d
+10 -3
View File
@@ -62,7 +62,7 @@ namespace posix
{ 0, 0 }
};
static const LuaConstant constants[] = {
static const Constant constants[] = {
{ "signal_abrt", SIGABRT },
{ "signal_fpe", SIGFPE },
{ "signal_ill", SIGILL },
@@ -86,9 +86,16 @@ namespace posix
}
}
luax_register_gc(L, instance);
WrappedModule w;
w.module = instance;
w.name = "signal";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = constants;
return luax_register_module(L, functions, 0, constants, "signal");
return luax_register_module(L, w);
}
} // posix
+9 -2
View File
@@ -128,9 +128,16 @@ namespace sound
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, types, 0, "sound");
WrappedModule w;
w.module = instance;
w.name = "sound";
w.flags = MODULE_SOUND_T;
w.functions = functions;
w.types = types;
w.constants = 0;
return luax_register_module(L, w);
}
} // sound
+9 -3
View File
@@ -85,9 +85,15 @@ namespace sdl
}
}
luax_register_gc(L, instance);
return luax_register_module(L, functions, 0, 0, "timer");
WrappedModule w;
w.module = instance;
w.name = "timer";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = 0;
return luax_register_module(L, w);
}
} // sdl