From 50084b0b3451328a4dfe6db65c78a225e9c8f288 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 6 Sep 2018 19:57:40 +0800 Subject: +bitmap --- src/lua/modules/graphics/bitmap.cpp | 99 ++++++++++++++++++++++++++++ src/lua/modules/graphics/graphics.cpp | 66 +++++++++++-------- src/lua/modules/graphics/jsl.cpp | 120 ---------------------------------- src/lua/modules/graphics/shader.cpp | 120 ++++++++++++++++++++++++++++++++++ src/lua/modules/graphics/texture.cpp | 14 ---- 5 files changed, 259 insertions(+), 160 deletions(-) create mode 100644 src/lua/modules/graphics/bitmap.cpp delete mode 100644 src/lua/modules/graphics/jsl.cpp create mode 100644 src/lua/modules/graphics/shader.cpp (limited to 'src/lua/modules/graphics') 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& BitmapRef; + + static inline BitmapRef checkBitmap(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + return proxy->getRef(); + } + + 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, 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& refBitmap = p->getRef(); + 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(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/jsl.cpp deleted file mode 100644 index c2808ca..0000000 --- a/src/lua/modules/graphics/jsl.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#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& JSLRef; - - static inline JSLRef checkJSLProgram(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef(); - } - - /** - * jsl:sendNumber("variable", 0.1) - */ - static int l_sendNumber (lua_State* L) - { - JSLRef ref = checkJSLProgram(L); - const char* variable = luax_checkstring(L, 2); - float number = luax_checknumber(L, 3); - ref->sendFloat(variable, number); - return 0; - } - - static int l_sendTexture (lua_State* L) - { - JSLRef ref = checkJSLProgram(L); - const char* variable = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); - Ref& tex = proxy->getRef(); - ref->sendTexture(variable, tex.getObject()); - return 0; - } - - static int l_sendCanvas (lua_State* L) - { - JSLRef ref = checkJSLProgram(L); - const char* variable = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); - Ref& canvas = proxy->getRef(); - ref->sendCanvas(variable, canvas.getObject()); - return 0; - } - - static int l_sendVec2 (lua_State* L) - { - JSLRef ref = checkJSLProgram(L); - const char* variable = luax_checkstring(L, 2); - float x = luax_checknumber(L, 3); - float y = luax_checknumber(L, 4); - ref->sendVec2(variable, x, y); - return 0; - } - - static int l_sendVec3 (lua_State* L) - { - JSLRef ref = checkJSLProgram(L); - const char* variable = luax_checkstring(L, 2); - float x = luax_checknumber(L, 3); - float y = luax_checknumber(L, 4); - float z = luax_checknumber(L, 5); - ref->sendVec3(variable, x, y, z); - return 0; - } - - static int l_sendVec4 (lua_State* L) - { - JSLRef ref = checkJSLProgram(L); - const char* variable = luax_checkstring(L, 2); - float x = luax_checknumber(L, 3); - float y = luax_checknumber(L, 4); - float z = luax_checknumber(L, 5); - float w = luax_checknumber(L, 6); - ref->sendVec4(variable, x, y, z, w); - return 0; - } - - static int l_sendColor (lua_State* L) - { - return l_sendVec4(L); - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "sendNumber", l_sendNumber }, - { "sendTexture", l_sendTexture }, - { "sendCanvas", l_sendCanvas }, - { "sendVec2", l_sendVec2 }, - { "sendVec3", l_sendVec3 }, - { "sendVec4", l_sendVec4 }, - { "sendColor", l_sendColor }, - { 0, 0 } - }; - - /** - * JSL program - */ - int luaopen_JSL(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_SHADER, f); - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/graphics/shader.cpp b/src/lua/modules/graphics/shader.cpp new file mode 100644 index 0000000..c2808ca --- /dev/null +++ b/src/lua/modules/graphics/shader.cpp @@ -0,0 +1,120 @@ +#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& JSLRef; + + static inline JSLRef checkJSLProgram(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + return proxy->getRef(); + } + + /** + * jsl:sendNumber("variable", 0.1) + */ + static int l_sendNumber (lua_State* L) + { + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + float number = luax_checknumber(L, 3); + ref->sendFloat(variable, number); + return 0; + } + + static int l_sendTexture (lua_State* L) + { + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); + Ref& tex = proxy->getRef(); + ref->sendTexture(variable, tex.getObject()); + return 0; + } + + static int l_sendCanvas (lua_State* L) + { + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); + Ref& canvas = proxy->getRef(); + ref->sendCanvas(variable, canvas.getObject()); + return 0; + } + + static int l_sendVec2 (lua_State* L) + { + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + float x = luax_checknumber(L, 3); + float y = luax_checknumber(L, 4); + ref->sendVec2(variable, x, y); + return 0; + } + + static int l_sendVec3 (lua_State* L) + { + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + float x = luax_checknumber(L, 3); + float y = luax_checknumber(L, 4); + float z = luax_checknumber(L, 5); + ref->sendVec3(variable, x, y, z); + return 0; + } + + static int l_sendVec4 (lua_State* L) + { + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + float x = luax_checknumber(L, 3); + float y = luax_checknumber(L, 4); + float z = luax_checknumber(L, 5); + float w = luax_checknumber(L, 6); + ref->sendVec4(variable, x, y, z, w); + return 0; + } + + static int l_sendColor (lua_State* L) + { + return l_sendVec4(L); + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + proxy->release(); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "sendNumber", l_sendNumber }, + { "sendTexture", l_sendTexture }, + { "sendCanvas", l_sendCanvas }, + { "sendVec2", l_sendVec2 }, + { "sendVec3", l_sendVec3 }, + { "sendVec4", l_sendVec4 }, + { "sendColor", l_sendColor }, + { 0, 0 } + }; + + /** + * JSL program + */ + int luaopen_JSL(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_SHADER, f); + return 0; + } + +} // lua +} // jin \ No newline at end of file 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 } }; -- cgit v1.1-26-g67d0