diff options
Diffstat (limited to 'src/lua/modules/graphics')
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 56 | ||||
-rw-r--r-- | src/lua/modules/graphics/jsl.cpp | 164 | ||||
-rw-r--r-- | src/lua/modules/graphics/texture.cpp (renamed from src/lua/modules/graphics/image.cpp) | 25 |
3 files changed, 115 insertions, 130 deletions
diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 9c1d404..98a4fef 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -12,7 +12,7 @@ namespace lua using jin::filesystem::Filesystem; using jin::filesystem::Buffer; - typedef Texture Image; + typedef Texture Texture; static struct { @@ -70,21 +70,21 @@ namespace lua return 1; } - static int l_newImage(lua_State* L) + static int l_newTexture(lua_State* L) { Filesystem* fs = Filesystem::get(); const char* f = luax_checkstring(L, 1); if (!fs->exists(f)) { - printf("Error: no such image %s\n", f); + printf("Error: no such texture %s\n", f); exit(1); } Buffer b; fs->read(f, &b); - 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* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); + Texture* img = Texture::createTexture(b.data, b.size); + proxy->bind(new Ref<Texture>(img, JIN_GRAPHICS_TEXTURE)); return 1; } @@ -158,10 +158,10 @@ namespace lua float sx = luax_optnumber(L, 4, 1); float sy = luax_optnumber(L, 5, 1); float r = luax_optnumber(L, 6, 0); - if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE)) + if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Image>& tex = proxy->getRef<Image>(); + Ref<Texture>& tex = proxy->getRef<Texture>(); tex->draw(x, y, sx, sy, r); } else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) @@ -173,7 +173,7 @@ namespace lua else { /* wrong type */ - luax_typerror(L, 1, "image or canvas"); + luax_typerror(L, 1, "texture or canvas"); } return 0; } @@ -198,7 +198,7 @@ namespace lua return 0; } - static int l_getColor(lua_State * L) + static int l_palette(lua_State * L) { luax_pushnumber(L, context.curRenderColor.rgba.r); luax_pushnumber(L, context.curRenderColor.rgba.g); @@ -267,7 +267,7 @@ namespace lua else return RENDER_MODE::NONE; } - static int l_drawpoint(lua_State* L) + static int l_point(lua_State* L) { int x = luax_checknumber(L, 1); int y = luax_checknumber(L, 2); @@ -276,7 +276,7 @@ namespace lua return 0; } - static int l_drawLine(lua_State* L) + static int l_line(lua_State* L) { int x1 = luax_checknumber(L, 1); int y1 = luax_checknumber(L, 2); @@ -287,7 +287,7 @@ namespace lua return 0; } - static int l_drawRect(lua_State* L) + static int l_rect(lua_State* L) { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); @@ -308,7 +308,7 @@ namespace lua return 0; } - static int l_drawCircle(lua_State* L) + static int l_circle(lua_State* L) { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); @@ -328,7 +328,7 @@ namespace lua return 0; } - static int l_drawTriangle(lua_State* L) + static int l_triangle(lua_State* L) { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); @@ -354,7 +354,7 @@ namespace lua return 0; } - static int l_drawPolygon(lua_State* L) + static int l_polygon(lua_State* L) { const char* modestr = luax_checkstring(L, 1); int n = luax_checknumber(L, 2); @@ -410,7 +410,7 @@ namespace lua return 1; } - static int l_study(lua_State* L) + static int l_setFont(lua_State* L) { int n = luax_gettop(L); if (n == 0) @@ -466,7 +466,7 @@ namespace lua { "getSize", l_getSize }, { "getWidth", l_getWidth }, { "getHeight", l_getHeight }, - { "newImage", l_newImage }, + { "newTexture", l_newTexture }, { "newShader", l_newShader }, { "newCanvas", l_newCanvas }, { "newFont", l_newFont }, @@ -476,24 +476,24 @@ namespace lua { "clear", l_clear }, { "draw", l_draw }, { "setColor", l_setColor }, - { "palette", l_getColor }, + { "palette", l_palette }, { "present", l_present }, - { "setFont", l_study }, + { "setFont", l_setFont }, { "bindCanvas", l_bindCanvas }, { "unbindCanvas", l_unbindCanvas }, { "useShader", l_useShader }, { "unuseShader", l_unuseShader }, - { "point", l_drawpoint }, - { "line", l_drawLine }, - { "rect", l_drawRect }, - { "circle", l_drawCircle }, - { "triangle", l_drawTriangle }, - { "polygon", l_drawPolygon }, + { "point", l_point }, + { "line", l_line }, + { "rect", l_rect }, + { "circle", l_circle }, + { "triangle", l_triangle }, + { "polygon", l_polygon }, { "destroy", l_destroy }, { 0, 0 } }; - extern int luaopen_Image(lua_State* L); + extern int luaopen_Texture(lua_State* L); extern int luaopen_Font(lua_State* L); extern int luaopen_Canvas(lua_State* L); extern int luaopen_JSL(lua_State* L); @@ -501,7 +501,7 @@ namespace lua int luaopen_graphics(lua_State* L) { // register types - luaopen_Image(L); + luaopen_Texture(L); luaopen_Canvas(L); luaopen_Font(L); luaopen_JSL(L); diff --git a/src/lua/modules/graphics/jsl.cpp b/src/lua/modules/graphics/jsl.cpp index 58de46a..9714695 100644 --- a/src/lua/modules/graphics/jsl.cpp +++ b/src/lua/modules/graphics/jsl.cpp @@ -10,7 +10,6 @@ namespace lua using namespace jin::graphics; - typedef Texture Image; typedef Ref<JSLProgram>& JSLRef; static inline JSLRef checkJSLProgram(lua_State* L) @@ -32,93 +31,74 @@ namespace lua COLOR, }; - static VARIABLE_TYPE strToType(const char* str) + /** + * jsl:sendNumber("variable", 0.1) + */ + static int l_sendNumber (lua_State* L) { - std::string s = std::string(str); - if (s == "number") return NUMBER; - else if (s == "Image") return IMAGE; - else if (s == "Canvas") return CANVAS; - else if (s == "vec2") return VEC2; - else if (s == "vec3") return VEC3; - else if (s == "vec4") return VEC4; - else if (s == "Color") return COLOR; - else return INVALID; + JSLRef ref = checkJSLProgram(L); + const char* variable = luax_checkstring(L, 2); + float number = luax_checknumber(L, 3); + ref->sendFloat(variable, number); + return 0; } - /** - * Use send function send variables to JSL program. - */ - static int l_send(lua_State* L) + 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<Texture>& tex = proxy->getRef<Texture>(); + 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>& canvas = proxy->getRef<Canvas>(); + 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) { - JSLRef ref = checkJSLProgram(L); - // number Image Texel - const char* typestr = luax_checkstring(L, 2); - // variable name - const char* variable = luax_checkstring(L, 3); - if (typestr != nullptr) - { - int type = strToType(typestr); - switch (type) - { - case NUMBER: - { - float number = luax_checknumber(L, 4); - ref->sendFloat(variable, number); - break; - } - case IMAGE: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); - Ref<Image>& tex = proxy->getRef<Image>(); - ref->sendTexture(variable, tex.getObject()); - break; - } - case CANVAS: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); - Ref<Canvas>& canvas = proxy->getRef<Canvas>(); - ref->sendCanvas(variable, canvas.getObject()); - break; - } - case VEC2: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - ref->sendVec2(variable, x, y); - break; - } - case VEC3: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - float z = luax_checknumber(L, 6); - ref->sendVec3(variable, x, y, z); - break; - } - case VEC4: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - float z = luax_checknumber(L, 6); - float w = luax_checknumber(L, 7); - ref->sendVec4(variable, x, y, z, w); - break; - } - case COLOR: - { - color col; - col.rgba.r = luax_checkinteger(L, 4); - col.rgba.g = luax_checkinteger(L, 5); - col.rgba.b = luax_checkinteger(L, 6); - col.rgba.a = luax_checkinteger(L, 7); - ref->sendColor(variable, &col); - break; - } - default: - return 0; - } - } - return 1; + return l_sendVec4(L); } static int l_gc(lua_State* L) @@ -129,9 +109,15 @@ namespace lua } static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "send", l_send }, - { 0, 0 } + { "__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 } }; /** @@ -143,5 +129,5 @@ namespace lua return 0; } -} -}
\ No newline at end of file +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/image.cpp b/src/lua/modules/graphics/texture.cpp index 3caff16..5bc208a 100644 --- a/src/lua/modules/graphics/image.cpp +++ b/src/lua/modules/graphics/texture.cpp @@ -10,32 +10,31 @@ namespace lua using namespace jin::graphics; - typedef Texture Image; - typedef Ref<Image>& ImageRef; + typedef Ref<Texture>& TextureRef; - static inline ImageRef checkImage(lua_State* L) + static inline TextureRef checkTexture(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - return proxy->getRef<Image>(); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); + return proxy->getRef<Texture>(); } static int l_getWidth(lua_State* L) { - ImageRef ref = checkImage(L); + TextureRef ref = checkTexture(L); luax_pushnumber(L, ref->getWidth()); return 1; } static int l_getHeight(lua_State *L) { - ImageRef ref = checkImage(L); + TextureRef ref = checkTexture(L); luax_pushnumber(L, ref->getHeight()); return 1; } static int l_getPixel(lua_State* L) { - ImageRef ref = checkImage(L); + TextureRef ref = checkTexture(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); color c = ref->getPixel(x, y); @@ -48,7 +47,7 @@ namespace lua static int l_setAnchor(lua_State* L) { - ImageRef ref = checkImage(L); + TextureRef ref = checkTexture(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); ref->setAnchor(x, y); @@ -57,7 +56,7 @@ namespace lua static int l_getSize(lua_State* L) { - ImageRef ref = checkImage(L); + TextureRef ref = checkTexture(L); luax_pushnumber(L, ref->getWidth()); luax_pushnumber(L, ref->getHeight()); return 2; @@ -65,7 +64,7 @@ namespace lua static int l_gc(lua_State* L) { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); proxy->release(); return 0; } @@ -80,9 +79,9 @@ namespace lua { 0, 0 } }; - int luaopen_Image(lua_State* L) + int luaopen_Texture(lua_State* L) { - luax_newtype(L, JIN_GRAPHICS_IMAGE, f); + luax_newtype(L, JIN_GRAPHICS_TEXTURE, f); return 0; } |