aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lua/audio/luaopen_Source.cpp2
-rw-r--r--src/lua/audio/luaopen_audio.cpp2
-rw-r--r--src/lua/common/Proxy.h12
-rw-r--r--src/lua/common/Reference.hpp21
-rw-r--r--src/lua/graphics/luaopen_graphics.cpp58
-rw-r--r--src/lua/luaopen_jin.cpp10
-rw-r--r--src/lua/luaopen_jin.h2
-rw-r--r--src/lua/net/luaopen_Socket.cpp6
-rw-r--r--src/lua/net/luaopen_net.cpp4
-rw-r--r--src/lua/thread/luaopen_Thread.cpp12
-rw-r--r--src/lua/thread/luaopen_thread.cpp12
11 files changed, 49 insertions, 92 deletions
diff --git a/src/lua/audio/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp
index 658adcb..aa71184 100644
--- a/src/lua/audio/luaopen_Source.cpp
+++ b/src/lua/audio/luaopen_Source.cpp
@@ -102,7 +102,7 @@ namespace lua
{ "isPaused", l_isPaused },
{ "setVolume", l_setVolume },
{ "setLoop", l_setLoop },
- {0, 0 }
+ { 0, 0 }
};
int luaopen_Source(lua_State* L)
diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp
index 731e4d9..03b33e6 100644
--- a/src/lua/audio/luaopen_audio.cpp
+++ b/src/lua/audio/luaopen_audio.cpp
@@ -72,7 +72,7 @@ namespace lua
fs->read(f, &b);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy));
Source* src = Source::createSource(b.data, b.size);
- proxy->bind(new Ref<Source>(src), JIN_AUDIO_SOURCE);
+ proxy->bind(new Ref<Source>(src, JIN_AUDIO_SOURCE));
return 1;
}
diff --git a/src/lua/common/Proxy.h b/src/lua/common/Proxy.h
index 60658ec..8323ead 100644
--- a/src/lua/common/Proxy.h
+++ b/src/lua/common/Proxy.h
@@ -11,12 +11,11 @@ namespace lua
class Proxy
{
public:
- void bind(Reference* ref, const char* t)
+ void bind(RefBase* ref)
{
if (ref == nullptr)
return;
reference = ref;
- type = t;
}
void release()
@@ -25,7 +24,6 @@ namespace lua
{
reference->release();
reference = nullptr;
- type = nullptr;
}
}
@@ -35,8 +33,12 @@ namespace lua
return *(Ref<T>*) reference;
}
- const char* type; // type name and metatable name
- Reference* reference; // acctual object binded
+ const char* getObjectType()
+ {
+ return reference->type;
+ }
+
+ RefBase* reference;
};
diff --git a/src/lua/common/Reference.hpp b/src/lua/common/Reference.hpp
index 7647a3f..feb96bb 100644
--- a/src/lua/common/Reference.hpp
+++ b/src/lua/common/Reference.hpp
@@ -6,7 +6,7 @@ namespace jin
namespace lua
{
- /*abstract*/class Reference
+ /*abstract*/class RefBase
{
public:
void retain()
@@ -20,29 +20,34 @@ namespace lua
delete this;
}
+ // object type string
+ const char* const type;
+
protected:
- Reference(void* obj)
+ RefBase(void* obj, const char* t)
: count(1)
, object(obj)
+ , type(t)
{
}
- Reference(const Reference&);
- virtual ~Reference()
+ RefBase(const RefBase&);
+
+ virtual ~RefBase()
{
}
void* object;
- int count;
+ int count;
};
template<class T>
- class Ref : public Reference
+ class Ref : public RefBase
{
public:
- Ref(T* obj)
- : Reference(obj)
+ Ref(T* obj, const char* type)
+ : RefBase(obj, type)
{
}
diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp
index b86b523..42e8f3c 100644
--- a/src/lua/graphics/luaopen_graphics.cpp
+++ b/src/lua/graphics/luaopen_graphics.cpp
@@ -13,10 +13,6 @@ namespace lua
typedef Texture Image;
- /**
- * jin.graphics context, storge some module
- * shared variables.
- */
static struct
{
color curRenderColor;
@@ -24,10 +20,6 @@ namespace lua
Font* defaultFont = nullptr;
} context;
- /**
- * Init video system.
- * jin.graphics.init(width, height, title)
- */
static int l_init(lua_State* L)
{
Window* wnd = Window::get();
@@ -54,9 +46,6 @@ namespace lua
return 0;
}
- /**
- * Get windows size.
- */
static int l_getSize(lua_State* L)
{
Window* wnd = Window::get();
@@ -65,9 +54,6 @@ namespace lua
return 2;
}
- /**
- * Create a image userdata and set metatable for it.
- */
static int l_newImage(lua_State* L)
{
Filesystem* fs = Filesystem::get();
@@ -82,35 +68,26 @@ namespace lua
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy));
Image* img = Image::createTexture(b.data, b.size);
- proxy->bind(new Ref<Image>(img), JIN_GRAPHICS_IMAGE);
+ proxy->bind(new Ref<Image>(img, JIN_GRAPHICS_IMAGE));
return 1;
}
- /**
- * Create a new JSL program.
- * graphics.Shader(program)
- */
static int l_newShader(lua_State* L)
{
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy));
const char* program = luax_checkstring(L, 1);
JSLProgram* jsl = JSLProgram::createJSLProgram(program);
- proxy->bind(new Ref<JSLProgram>(jsl), JIN_GRAPHICS_SHADER);
+ proxy->bind(new Ref<JSLProgram>(jsl, JIN_GRAPHICS_SHADER));
return 1;
}
- /**
- * Create a new Canvas, don't use it in loop, very slow.
- * jin.graphics.newCanvas(w, h)
- */
static int l_newCanvas(lua_State* L)
{
int w = luax_checknumber(L, 1);
int h = luax_checknumber(L, 2);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy));
Canvas* cvs = Canvas::createCanvas(w, h);
- Ref<Canvas>* ref = new Ref<Canvas>(cvs);
- proxy->bind(ref, JIN_GRAPHICS_CANVAS);
+ proxy->bind(new Ref<Canvas>(cvs, JIN_GRAPHICS_CANVAS));
return 1;
}
@@ -132,16 +109,12 @@ namespace lua
return 0;
}
- /**
- * Swap render buffers, present current buffer to front.
- */
static int l_present(lua_State* L)
{
Window::get()->swapBuffers();
return 0;
}
- // jin.graphics.draw(x, y, scalex, scaley, r)
static int l_draw(lua_State* L)
{
int x = luax_optnumber(L, 2, 0);
@@ -171,8 +144,6 @@ namespace lua
static int l_setColor(lua_State* L)
{
- // jin.graphics.color() to set back to default
- // render color
if (luax_gettop(L) == 0)
{
glColor4f(1, 1, 1, 1);
@@ -260,10 +231,6 @@ namespace lua
else return RENDER_MODE::NONE;
}
- /**
- * draw pixel to screen
- * jin.graphics.pixel(x, y)
- */
static int l_drawpoint(lua_State* L)
{
int x = luax_checknumber(L, 1);
@@ -351,10 +318,6 @@ namespace lua
return 0;
}
- /**
- * draw polygon.
- * jin.graphics.polygon(mode, n, {{}, {}, {}...})
- */
static int l_drawPolygon(lua_State* L)
{
const char* modestr = luax_checkstring(L, 1);
@@ -407,13 +370,10 @@ namespace lua
fs->read(path, &b);
font->loadb((const unsigned char*)b.data);
}
- proxy->bind(new Ref<Font>(font), JIN_GRAPHICS_FONT);
+ proxy->bind(new Ref<Font>(font, JIN_GRAPHICS_FONT));
return 1;
}
- /**
- * study font, 0 args for study default font.
- */
static int l_study(lua_State* L)
{
int n = luax_gettop(L);
@@ -434,10 +394,6 @@ namespace lua
return 0;
}
- /**
- * draw text with current font(after study). befor write, must
- * study a font.
- */
static int l_write(lua_State* L)
{
if (context.curFont == 0)
@@ -456,9 +412,6 @@ namespace lua
return 0;
}
- /**
- * get text bound box
- */
static int l_box(lua_State* L)
{
const char* text = luax_checkstring(L, 1);
@@ -502,11 +455,8 @@ namespace lua
};
extern int luaopen_Image(lua_State* L);
-
extern int luaopen_Font(lua_State* L);
-
extern int luaopen_Canvas(lua_State* L);
-
extern int luaopen_JSL(lua_State* L);
int luaopen_graphics(lua_State* L)
diff --git a/src/lua/luaopen_jin.cpp b/src/lua/luaopen_jin.cpp
index a271a77..ffb0f45 100644
--- a/src/lua/luaopen_jin.cpp
+++ b/src/lua/luaopen_jin.cpp
@@ -52,11 +52,11 @@ namespace lua
}
static const luaL_Reg f[] = {
- { "version", l_getversion },
- { "revision", l_revision },
- { "author", l_getAuthor },
- { "os", l_getOS },
- { 0, 0 }
+ { "version", l_getversion },
+ { "revision", l_revision },
+ { "author", l_getAuthor },
+ { "os", l_getOS },
+ { 0, 0 }
};
// submodules
diff --git a/src/lua/luaopen_jin.h b/src/lua/luaopen_jin.h
index 9cceb9e..8bd902e 100644
--- a/src/lua/luaopen_jin.h
+++ b/src/lua/luaopen_jin.h
@@ -25,7 +25,7 @@
#define MODULE_NAME "jin"
#define VERSION "0.1.1"
-#define REVISION 101
+#define REVISION 101
#define AUTHOR "chai"
namespace jin
diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp
index 6a6f1e1..def3d43 100644
--- a/src/lua/net/luaopen_Socket.cpp
+++ b/src/lua/net/luaopen_Socket.cpp
@@ -31,7 +31,7 @@ namespace lua
Ref<Socket>& socket = checkSocket(L);
Socket* client = socket->accept();
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
- proxy->bind(new Ref<Socket>(client), JIN_NETWORK_SOCKET);
+ proxy->bind(new Ref<Socket>(client, JIN_NETWORK_SOCKET));
return 1;
}
@@ -43,7 +43,7 @@ namespace lua
int size = socket->receive(buffer, BUFFER_SIZE);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
net::Buffer* netBuffer = new net::Buffer(buffer, size);
- proxy->bind(new Ref<Buffer>(netBuffer), JIN_NETWORK_BUFFER);
+ proxy->bind(new Ref<Buffer>(netBuffer, JIN_NETWORK_BUFFER));
return 1;
}
@@ -57,7 +57,7 @@ namespace lua
int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port);
net::Buffer* netBuffer = new net::Buffer(buffer, size);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
- proxy->bind(new Ref<Buffer>(netBuffer), JIN_NETWORK_BUFFER);
+ proxy->bind(new Ref<Buffer>(netBuffer, JIN_NETWORK_BUFFER));
return 1;
}
diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp
index 5457c07..600f479 100644
--- a/src/lua/net/luaopen_net.cpp
+++ b/src/lua/net/luaopen_net.cpp
@@ -48,7 +48,7 @@ namespace lua
}
Socket* socket = new Socket(info);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
- proxy->bind(new Ref<Socket>(socket), JIN_NETWORK_SOCKET);
+ proxy->bind(new Ref<Socket>(socket, JIN_NETWORK_SOCKET));
return 1;
}
@@ -58,7 +58,7 @@ namespace lua
int size = luax_checkinteger(L, 1);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
net::Buffer* buffer = new net::Buffer(size);
- proxy->bind(new Ref<Buffer>(buffer), JIN_NETWORK_BUFFER);
+ proxy->bind(new Ref<Buffer>(buffer, JIN_NETWORK_BUFFER));
return 1;
}
diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp
index 3277704..a427e30 100644
--- a/src/lua/thread/luaopen_Thread.cpp
+++ b/src/lua/thread/luaopen_Thread.cpp
@@ -28,7 +28,7 @@ namespace lua
luax_getglobal(L, MODULE_NAME);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
ref.retain();
- proxy->bind(&ref, JIN_THREAD_THREAD);
+ proxy->bind(&ref);
luax_setfield(L, -2, "_curThread");
luax_dostring(L, ref->code.c_str());
luax_close(L);
@@ -124,9 +124,9 @@ namespace lua
case thread::Thread::Variant::POINTER:
Proxy* p = (Proxy*)v.pointer;
- Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy));
p->reference->retain();
- proxy->bind(p->reference, p->type);
+ proxy->bind(p->reference);
break;
}
@@ -158,9 +158,9 @@ namespace lua
case thread::Thread::Variant::POINTER:
Proxy* p = (Proxy*)v.pointer;
- Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy));
p->reference->retain();
- proxy->bind(p->reference, p->type);
+ proxy->bind(p->reference);
break;
}
@@ -219,7 +219,7 @@ namespace lua
const char* code = luax_checkstring(L, 2);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
Thread* thread = new Thread(name, code, threadRunner);
- proxy->bind(new Ref<Thread>(thread), JIN_THREAD_THREAD);
+ proxy->bind(new Ref<Thread>(thread, JIN_THREAD_THREAD));
return 1;
}
diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp
index 3277704..a427e30 100644
--- a/src/lua/thread/luaopen_thread.cpp
+++ b/src/lua/thread/luaopen_thread.cpp
@@ -28,7 +28,7 @@ namespace lua
luax_getglobal(L, MODULE_NAME);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
ref.retain();
- proxy->bind(&ref, JIN_THREAD_THREAD);
+ proxy->bind(&ref);
luax_setfield(L, -2, "_curThread");
luax_dostring(L, ref->code.c_str());
luax_close(L);
@@ -124,9 +124,9 @@ namespace lua
case thread::Thread::Variant::POINTER:
Proxy* p = (Proxy*)v.pointer;
- Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy));
p->reference->retain();
- proxy->bind(p->reference, p->type);
+ proxy->bind(p->reference);
break;
}
@@ -158,9 +158,9 @@ namespace lua
case thread::Thread::Variant::POINTER:
Proxy* p = (Proxy*)v.pointer;
- Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy));
+ Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy));
p->reference->retain();
- proxy->bind(p->reference, p->type);
+ proxy->bind(p->reference);
break;
}
@@ -219,7 +219,7 @@ namespace lua
const char* code = luax_checkstring(L, 2);
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy));
Thread* thread = new Thread(name, code, threadRunner);
- proxy->bind(new Ref<Thread>(thread), JIN_THREAD_THREAD);
+ proxy->bind(new Ref<Thread>(thread, JIN_THREAD_THREAD));
return 1;
}