diff options
Diffstat (limited to 'src/lua/modules/graphics')
-rw-r--r-- | src/lua/modules/graphics/bitmap.cpp | 99 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 66 | ||||
-rw-r--r-- | src/lua/modules/graphics/shader.cpp (renamed from src/lua/modules/graphics/jsl.cpp) | 0 | ||||
-rw-r--r-- | src/lua/modules/graphics/texture.cpp | 14 |
4 files changed, 139 insertions, 40 deletions
diff --git a/src/lua/modules/graphics/bitmap.cpp b/src/lua/modules/graphics/bitmap.cpp new file mode 100644 index 0000000..81c36f1 --- /dev/null +++ b/src/lua/modules/graphics/bitmap.cpp @@ -0,0 +1,99 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::graphics; + + typedef Ref<Bitmap>& BitmapRef; + + static inline BitmapRef checkBitmap(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + return proxy->getRef<Bitmap>(); + } + + static int l_gc(lua_State* L) + { + printf("collect bitmap\n"); + BitmapRef ref = checkBitmap(L); + ref.release(); + return 0; + } + + static int l_getWidth(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int w = ref->getWidth(); + luax_pushinteger(L, w); + return 1; + } + + static int l_getHeight(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int h = ref->getHeight(); + luax_pushinteger(L, h); + return 1; + } + + static int l_getSize(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int w = ref->getWidth(); + int h = ref->getHeight(); + luax_pushinteger(L, w); + luax_pushinteger(L, h); + return 2; + } + + static int l_getPixel(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + Color col = ref->getPixel(x, y); + luax_pushinteger(L, col.r); + luax_pushinteger(L, col.g); + luax_pushinteger(L, col.b); + luax_pushinteger(L, col.a); + return 4; + } + + static int l_setPixel(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + unsigned char r = luax_checkinteger(L, 4); + unsigned char g = luax_checkinteger(L, 5); + unsigned char b = luax_checkinteger(L, 6); + unsigned char a = luax_checkinteger(L, 7); + ref->setPixel(Color(r, g, b, a), x, y); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { "getPixel", l_getPixel }, + { "setPixel", l_setPixel }, + { 0, 0 } + }; + + int luaopen_Bitmap(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_BITMAP, f); + return 0; + } + + +} +}
\ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 62bfb26..909e70c 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -15,8 +15,8 @@ namespace lua static struct { - color curRenderColor; - color curClearColor; + Color curRenderColor; + Color curClearColor; Font* curFont = nullptr; Font* defaultFont = nullptr; } context; @@ -77,9 +77,9 @@ namespace lua return 1; } - static int l_newTexture(lua_State* L) + static int l_newBitmap(lua_State* L) { - Filesystem* fs = Filesystem::get(); + Filesystem* fs = Filesystem::get(); const char* f = luax_checkstring(L, 1); if (!fs->exists(f)) { @@ -88,9 +88,20 @@ namespace lua } Buffer b; fs->read(f, &b); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); + Bitmap* bitmap = Bitmap::createBitmap(b.data, b.size); + proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP)); + return 1; + } + /* jin.graphics.newTexture(bitmap) */ + static int l_newTexture(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + Ref<Bitmap>& refBitmap = p->getRef<Bitmap>(); + Bitmap* bitmap = refBitmap.getObject(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); - Texture* img = Texture::createTexture(b.data, b.size); + Texture* img = Texture::createTexture(bitmap); proxy->bind(new Ref<Texture>(img, JIN_GRAPHICS_TEXTURE)); return 1; } @@ -140,15 +151,15 @@ namespace lua return 0; } - context.curClearColor.rgba.r = luax_checknumber(L, 1); - context.curClearColor.rgba.g = luax_checknumber(L, 2); - context.curClearColor.rgba.b = luax_checknumber(L, 3); - context.curClearColor.rgba.a = luax_checknumber(L, 4); + context.curClearColor.r = luax_checknumber(L, 1); + context.curClearColor.g = luax_checknumber(L, 2); + context.curClearColor.b = luax_checknumber(L, 3); + context.curClearColor.a = luax_checknumber(L, 4); - glClearColor(context.curClearColor.rgba.r / 255.f, - context.curClearColor.rgba.g / 255.f, - context.curClearColor.rgba.b / 255.f, - context.curClearColor.rgba.a / 255.f); + glClearColor(context.curClearColor.r / 255.f, + context.curClearColor.g / 255.f, + context.curClearColor.b / 255.f, + context.curClearColor.a / 255.f); return 0; } @@ -192,27 +203,27 @@ namespace lua return 0; } - context.curRenderColor.rgba.r = luax_checknumber(L, 1); - context.curRenderColor.rgba.g = luax_checknumber(L, 2); - context.curRenderColor.rgba.b = luax_checknumber(L, 3); + context.curRenderColor.r = luax_checknumber(L, 1); + context.curRenderColor.g = luax_checknumber(L, 2); + context.curRenderColor.b = luax_checknumber(L, 3); if (luax_gettop(L) == 4) - context.curRenderColor.rgba.a = luax_checknumber(L, 4); + context.curRenderColor.a = luax_checknumber(L, 4); else - context.curClearColor.rgba.a = 255; + context.curClearColor.a = 255; - glColor4f(context.curRenderColor.rgba.r / 255.f, - context.curRenderColor.rgba.g / 255.f, - context.curRenderColor.rgba.b / 255.f, - context.curRenderColor.rgba.a / 255.f); + glColor4f(context.curRenderColor.r / 255.f, + context.curRenderColor.g / 255.f, + context.curRenderColor.b / 255.f, + context.curRenderColor.a / 255.f); return 0; } static int l_palette(lua_State * L) { - luax_pushnumber(L, context.curRenderColor.rgba.r); - luax_pushnumber(L, context.curRenderColor.rgba.g); - luax_pushnumber(L, context.curRenderColor.rgba.b); - luax_pushnumber(L, context.curRenderColor.rgba.a); + luax_pushnumber(L, context.curRenderColor.r); + luax_pushnumber(L, context.curRenderColor.g); + luax_pushnumber(L, context.curRenderColor.b); + luax_pushnumber(L, context.curRenderColor.a); return 4; } @@ -478,6 +489,7 @@ namespace lua { "getHeight", l_getHeight }, { "destroy", l_destroy }, /* creators */ + { "newBitmap", l_newBitmap }, { "newTexture", l_newTexture }, { "newShader", l_newShader }, { "newCanvas", l_newCanvas }, @@ -513,10 +525,12 @@ namespace lua extern int luaopen_Font(lua_State* L); extern int luaopen_Canvas(lua_State* L); extern int luaopen_JSL(lua_State* L); + extern int luaopen_Bitmap(lua_State* L); int luaopen_graphics(lua_State* L) { // register types + luaopen_Bitmap(L); luaopen_Texture(L); luaopen_Canvas(L); luaopen_Font(L); diff --git a/src/lua/modules/graphics/jsl.cpp b/src/lua/modules/graphics/shader.cpp index c2808ca..c2808ca 100644 --- a/src/lua/modules/graphics/jsl.cpp +++ b/src/lua/modules/graphics/shader.cpp diff --git a/src/lua/modules/graphics/texture.cpp b/src/lua/modules/graphics/texture.cpp index d1d187d..77a93b8 100644 --- a/src/lua/modules/graphics/texture.cpp +++ b/src/lua/modules/graphics/texture.cpp @@ -32,19 +32,6 @@ namespace lua return 1; } - static int l_getPixel(lua_State* L) - { - TextureRef ref = checkTexture(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - color c = ref->getPixel(x, y); - luax_pushnumber(L, c.rgba.r); - luax_pushnumber(L, c.rgba.g); - luax_pushnumber(L, c.rgba.b); - luax_pushnumber(L, c.rgba.a); - return 4; - } - static int l_setAnchor(lua_State* L) { TextureRef ref = checkTexture(L); @@ -74,7 +61,6 @@ namespace lua { "getWidth", l_getWidth }, { "getHeight", l_getHeight }, { "getSize", l_getSize }, - { "getPixel", l_getPixel }, { "setAnchor", l_setAnchor }, { 0, 0 } }; |