diff options
author | chai <chaifix@163.com> | 2018-08-14 14:56:47 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-14 14:56:47 +0800 |
commit | 5c9af043503f92852a1a765b6ecfbc1aea24d2e9 (patch) | |
tree | eb371092c4137a672e7bfc13dc56ee777623ebfe /src/lua/graphics | |
parent | 5162f84be0a4deb447c6ba1226722b049335d525 (diff) |
*update
Diffstat (limited to 'src/lua/graphics')
-rw-r--r-- | src/lua/graphics/Canvas.cpp | 24 | ||||
-rw-r--r-- | src/lua/graphics/Canvas.h | 59 | ||||
-rw-r--r-- | src/lua/graphics/Color.h | 19 | ||||
-rw-r--r-- | src/lua/graphics/Font.h | 52 | ||||
-rw-r--r-- | src/lua/graphics/Image.cpp | 26 | ||||
-rw-r--r-- | src/lua/graphics/Image.h | 59 | ||||
-rw-r--r-- | src/lua/graphics/JSL.cpp | 19 | ||||
-rw-r--r-- | src/lua/graphics/JSL.h | 90 | ||||
-rw-r--r-- | src/lua/graphics/graphics.h | 18 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_Canvas.cpp | 7 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_Font.cpp | 9 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_Image.cpp | 22 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_JSL.cpp | 16 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_graphics.cpp | 64 |
14 files changed, 433 insertions, 51 deletions
diff --git a/src/lua/graphics/Canvas.cpp b/src/lua/graphics/Canvas.cpp new file mode 100644 index 0000000..c4918f0 --- /dev/null +++ b/src/lua/graphics/Canvas.cpp @@ -0,0 +1,24 @@ +#include "Canvas.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + Canvas* Canvas::createCanvas(int w, int h) + { + Canvas* canvas = new Canvas(); + canvas->canvas = jin::graphics::Canvas::createCanvas(w, h); + return canvas; + } + + void Canvas::unbind() + { + jin::graphics::Canvas::unbind(); + } + +} +} +}
\ No newline at end of file diff --git a/src/lua/graphics/Canvas.h b/src/lua/graphics/Canvas.h new file mode 100644 index 0000000..d80ff79 --- /dev/null +++ b/src/lua/graphics/Canvas.h @@ -0,0 +1,59 @@ +#ifndef __JIN_LUA_GRAPHICS_CANVAS_H +#define __JIN_LUA_GRAPHICS_CANVAS_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class Canvas : public Object + { + public: + static Canvas* createCanvas(int w, int h); + + int getWidth() + { + return canvas->getWidth(); + } + int getHeight() + { + return canvas->getHeight(); + } + void setAnchor(int x, int y) + { + canvas->setAnchor(x, y); + } + inline const jin::graphics::Canvas* getRawCanvas() const + { + return canvas; + } + void bind() + { + canvas->bind(); + } + + void draw(int x, int y, float sx, float sy, float r) + { + canvas->draw(x, y, sx, sy, r); + } + + static void unbind(); + + private: + ~Canvas() + { + delete canvas; + } + jin::graphics::Canvas* canvas; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/Color.h b/src/lua/graphics/Color.h new file mode 100644 index 0000000..3621329 --- /dev/null +++ b/src/lua/graphics/Color.h @@ -0,0 +1,19 @@ +#ifndef __JIN_LUA_GRAPHICS_COLOR_H +#define __JIN_LUA_GRAPHICS_COLOR_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + typedef jin::graphics::color color; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/Font.h b/src/lua/graphics/Font.h new file mode 100644 index 0000000..b3b66a8 --- /dev/null +++ b/src/lua/graphics/Font.h @@ -0,0 +1,52 @@ +#ifndef __JIN_LUA_GRAPHICS_FONT_H +#define __JIN_LUA_GRAPHICS_FONT_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class Font : public Object + { + public: + Font() + { + font = new jin::graphics::Font(); + } + + void box(const char* str, int fheight, int spacing, int lheight, int* w, int * h) + { + font->box(str, fheight, spacing, lheight, w, h); + } + void loadb(const unsigned char* data) + { + font->loadb(data); + } + void render( + const char* text, // rendered text + float x, float y, // render position + int fheight, // font height + int spacing, // font spacing + int lheight) // line height + { + font->render(text, x, y, fheight, spacing, lheight); + } + private: + ~Font() + { + delete font; + } + + jin::graphics::Font* font; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/Image.cpp b/src/lua/graphics/Image.cpp new file mode 100644 index 0000000..43a83c4 --- /dev/null +++ b/src/lua/graphics/Image.cpp @@ -0,0 +1,26 @@ +#include "Image.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + Image* Image::createImage(const char* file) + { + Image* image = new Image(); + image->image = jin::graphics::Texture::createTexture(file); + return image; + } + + Image* Image::createImage(const void* mem, size_t size) + { + Image* image = new Image(); + image->image = jin::graphics::Texture::createTexture(mem, size); + return image; + } + +} +} +}
\ No newline at end of file diff --git a/src/lua/graphics/Image.h b/src/lua/graphics/Image.h new file mode 100644 index 0000000..689793a --- /dev/null +++ b/src/lua/graphics/Image.h @@ -0,0 +1,59 @@ +#ifndef __JIN_LUA_GRAPHICS_IMAGE_H +#define __JIN_LUA_GRAPHICS_IMAGE_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class Image : public Object + { + public: + static Image* createImage(const char* file); + static Image* createImage(const void* mem, size_t size); + + int getWidth() + { + return image->getWidth(); + } + int getHeight() + { + return image->getHeight(); + } + void setAnchor(int x, int y) + { + image->setAnchor(x, y); + } + jin::graphics::color getPixel(int x, int y) + { + return image->getPixel(x, y); + } + inline const jin::graphics::Texture* getRawImage() const + { + return image; + } + + void draw(int x, int y, float sx, float sy, float r) + { + image->draw(x, y, sx, sy, r); + } + + private: + ~Image() + { + delete image; + } + + jin::graphics::Texture* image; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/JSL.cpp b/src/lua/graphics/JSL.cpp new file mode 100644 index 0000000..9df19a9 --- /dev/null +++ b/src/lua/graphics/JSL.cpp @@ -0,0 +1,19 @@ +#include "JSL.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + JSLProgram* JSLProgram::currentJSLProgram = nullptr; + JSLProgram* JSLProgram::createJSLProgram(const char* program) + { + JSLProgram* jslprogram = new JSLProgram(); + jslprogram->jslprogram = jin::graphics::JSLProgram::createJSLProgram(program); + return jslprogram; + } +} +} +}
\ No newline at end of file diff --git a/src/lua/graphics/JSL.h b/src/lua/graphics/JSL.h new file mode 100644 index 0000000..828fac0 --- /dev/null +++ b/src/lua/graphics/JSL.h @@ -0,0 +1,90 @@ +#ifndef __JIN_LUA_GRAPHICS_JSL_H +#define __JIN_LUA_GRAPHICS_JSL_H +#include "libjin/jin.h" +#include "../luaopen_types.h" +#include "Image.h" +#include "Color.h" +#include "Canvas.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + class JSLProgram : public Object + { + public: + static JSLProgram* createJSLProgram(const char* program); + + inline void use() + { + currentJSLProgram = this; + jslprogram->use(); + } + + static inline void unuse() + { + currentJSLProgram = nullptr; + jin::graphics::JSLProgram::unuse(); + } + + void sendFloat(const char* name, float number) + { + jslprogram->sendFloat(name, number); + } + + void sendImage(const char* name, const Image* image) + { + jslprogram->sendTexture(name, image->getRawImage()); + } + + void sendVec2(const char* name, float x, float y) + { + jslprogram->sendVec2(name, x, y); + } + + void sendVec3(const char* name, float x, float y, float z) + { + jslprogram->sendVec3(name, x, y, z); + } + + void sendVec4(const char* name, float x, float y, float z, float w) + { + jslprogram->sendVec4(name, x, y, z, w); + } + + void sendCanvas(const char* name, const Canvas* canvas) + { + jslprogram->sendCanvas(name, canvas->getRawCanvas()); + } + + void sendColor(const char* name, const lua::graphics::color* col) + { + jslprogram->sendColor(name, col); + } + + static inline JSLProgram* getCurrentJSL() + { + return currentJSLProgram; + } + + private: + + static JSLProgram* currentJSLProgram; + + ~JSLProgram() + { + delete jslprogram; + } + + jin::graphics::JSLProgram * jslprogram; + + }; + +} // graphics +} // lua +} // jin + +#endif
\ No newline at end of file diff --git a/src/lua/graphics/graphics.h b/src/lua/graphics/graphics.h new file mode 100644 index 0000000..bd9bec1 --- /dev/null +++ b/src/lua/graphics/graphics.h @@ -0,0 +1,18 @@ +#ifndef __JIN_LUA_GRAPHICS_GRAPHICS_H +#define __JIN_LUA_GRAPHICS_GRAPHICS_H +#include "libjin/jin.h" +#include "../luaopen_types.h" + +namespace jin +{ +namespace lua +{ +namespace graphics +{ + + typedef jin::graphics::RENDER_MODE RENDER_MODE; + +} +} +} +#endif
\ No newline at end of file diff --git a/src/lua/graphics/luaopen_Canvas.cpp b/src/lua/graphics/luaopen_Canvas.cpp index 808a977..d08b181 100644 --- a/src/lua/graphics/luaopen_Canvas.cpp +++ b/src/lua/graphics/luaopen_Canvas.cpp @@ -1,13 +1,13 @@ #include "lua/luax.h" #include "lua/luaopen_types.h" -#include "libjin/jin.h" +#include "Canvas.h" namespace jin { namespace lua { - using namespace jin::graphics; + using namespace lua::graphics; static inline Canvas* checkCanvas(lua_State* L) { @@ -51,8 +51,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Canvas* canvas = (Canvas*)proxy->object; - delete canvas; + proxy->release(); return 0; } diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp index 3de2981..3643412 100644 --- a/src/lua/graphics/luaopen_Font.cpp +++ b/src/lua/graphics/luaopen_Font.cpp @@ -11,12 +11,15 @@ namespace lua static int l_gc(lua_State* L) { + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + proxy->release(); return 0; } static int l_box(lua_State* L) { - Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Font* font = (Font*)proxy->object; const char* text = luax_checkstring(L, 2); int fheight = luax_checknumber(L, 3); int spacing = luax_checknumber(L, 4); @@ -41,5 +44,5 @@ namespace lua return 0; } -} -}
\ No newline at end of file +} // lua +} // jin
\ No newline at end of file diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp index 0f97b2c..8d89a80 100644 --- a/src/lua/graphics/luaopen_Image.cpp +++ b/src/lua/graphics/luaopen_Image.cpp @@ -1,39 +1,40 @@ #include "lua/luax.h" -#include "libjin/jin.h" #include "lua/luaopen_types.h" +#include "Image.h" +#include "Color.h" namespace jin { namespace lua { - using namespace jin::graphics; + using namespace lua::graphics; - static inline Texture* checkTexture(lua_State* L) + static inline Image* checkImage(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); if (proxy != nullptr) - return (Texture*)proxy->object; + return (Image*)proxy->object; return nullptr; } static int l_getWidth(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); luax_pushnumber(L, i->getWidth()); return 1; } static int l_getHeight(lua_State *L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); luax_pushnumber(L, i->getHeight()); return 1; } static int l_getPixel(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); color c = i->getPixel(x, y); @@ -46,7 +47,7 @@ namespace lua static int l_setAnchor(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); i->setAnchor(x, y); @@ -55,7 +56,7 @@ namespace lua static int l_getSize(lua_State* L) { - Texture* i = checkTexture(L); + Image* i = checkImage(L); luax_pushnumber(L, i->getWidth()); luax_pushnumber(L, i->getHeight()); return 2; @@ -64,8 +65,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - Texture* img = (Texture*)proxy->object; - delete img; + proxy->release(); return 0; } diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp index 8d25178..774f2b6 100644 --- a/src/lua/graphics/luaopen_JSL.cpp +++ b/src/lua/graphics/luaopen_JSL.cpp @@ -1,13 +1,16 @@ #include "lua/luax.h" -#include "libjin/jin.h" #include "lua/luaopen_types.h" +#include "Image.h" +#include "JSL.h" +#include "Canvas.h" +#include "Color.h" namespace jin { namespace lua { - - using namespace jin::graphics; + + using namespace lua::graphics; static inline JSLProgram* checkJSLProgram(lua_State* L) { @@ -67,8 +70,8 @@ namespace lua case IMAGE: { Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_IMAGE); - Texture* tex = (Texture*)proxy->object; - jsl->sendTexture(variable, tex); + Image* tex = (Image*)proxy->object; + jsl->sendImage(variable, tex); break; } case CANVAS: @@ -122,8 +125,7 @@ namespace lua static int l_gc(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - JSLProgram* jsl = (JSLProgram*)proxy->object; - delete jsl; + proxy->release(); return 0; } diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp index da91c51..31c5719 100644 --- a/src/lua/graphics/luaopen_graphics.cpp +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -1,14 +1,23 @@ -#include "libjin/jin.h" #include "lua/luax.h" +#include "libjin/jin.h" #include "lua/luaopen_types.h" #include "lua/embed/graphics.lua.h" +#include "Canvas.h" +#include "Color.h" +#include "Font.h" +#include "Image.h" +#include "JSL.h" +#include "graphics.h" namespace jin { namespace lua { - using namespace jin::graphics; - using namespace jin::filesystem; + + using namespace lua::graphics; + using jin::graphics::Window; + using jin::filesystem::Filesystem; + using jin::filesystem::Buffer; /** * jin.graphics context, storge some module @@ -17,8 +26,8 @@ namespace lua static struct { color curRenderColor; - Font* curFont = 0; - Font* defaultFont = 0; + Font* curFont = nullptr; + Font* defaultFont = nullptr; } context; /** @@ -78,7 +87,7 @@ namespace lua fs->read(f, &b); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_IMAGE, sizeof(Proxy)); - Texture* img = Texture::createTexture(b.data, b.size); + Image* img = Image::createImage(b.data, b.size); proxy->bind(img, JIN_GRAPHICS_IMAGE); return 1; } @@ -148,7 +157,7 @@ namespace lua if (luax_istype(L, 1, JIN_GRAPHICS_IMAGE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Texture* tex = (Texture*)proxy->object; + Image* tex = (Image*)proxy->object; tex->draw(x, y, sx, sy, r); } else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) @@ -251,9 +260,9 @@ namespace lua static RENDER_MODE strtomode(const char* str) { std::string s = std::string(str); - if (s == "fill") return FILL; - else if (s == "line") return LINE; - else return NONE; + if (s == "fill") return RENDER_MODE::FILL; + else if (s == "line") return RENDER_MODE::LINE; + else return RENDER_MODE::NONE; } /** @@ -264,7 +273,7 @@ namespace lua { int x = luax_checknumber(L, 1); int y = luax_checknumber(L, 2); - point(x, y); + jin::graphics::point(x, y); return 0; } @@ -275,7 +284,7 @@ namespace lua int y1 = luax_checknumber(L, 2); int x2 = luax_checknumber(L, 3); int y2 = luax_checknumber(L, 4); - line(x1, y1, x2, y2); + jin::graphics::line(x1, y1, x2, y2); return 0; } @@ -284,7 +293,7 @@ namespace lua { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); @@ -305,7 +314,7 @@ namespace lua { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); @@ -325,7 +334,7 @@ namespace lua { const char* modestr = luax_checkstring(L, 1); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { int x = luax_checknumber(L, 2); int y = luax_checknumber(L, 3); @@ -356,7 +365,7 @@ namespace lua const char* modestr = luax_checkstring(L, 1); int n = luax_checknumber(L, 2); RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) + if (mode != RENDER_MODE::NONE) { if (!luax_istable(L, 3)) { @@ -389,18 +398,21 @@ namespace lua static int l_newFont(lua_State* L) { - Font* font = (Font*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Font)); - const char* path = luax_checkstring(L, 1); - Filesystem* fs = Filesystem::get(); - Buffer b = {}; - if (!fs->exists(path)) + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Font* font = new Font(); { - printf("Error: no such font %s\n", path); - exit(1); + const char* path = luax_checkstring(L, 1); + Filesystem* fs = Filesystem::get(); + Buffer b = {}; + if (!fs->exists(path)) + { + printf("Error: no such font %s\n", path); + exit(1); + } + fs->read(path, &b); + font->loadb((const unsigned char*)b.data); } - fs->read(path, &b); - font->loadb((const unsigned char*)b.data); - + proxy->bind(font, JIN_GRAPHICS_FONT); return 1; } |