diff options
author | chai <chaifix@163.com> | 2018-05-18 14:39:38 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-05-18 14:39:38 +0800 |
commit | 1b773ad2c250e09c09c065eb3eec64bfebde09ca (patch) | |
tree | b3f1f367694d5f86cc0caa7f4eea2e6a1d3424c5 /src | |
parent | 91a592da979827b1735901388dba8712e6e3ecf3 (diff) |
修改userdata创建方式
Diffstat (limited to 'src')
-rw-r--r-- | src/3rdparty/luax/luax.h | 5 | ||||
-rw-r--r-- | src/3rdparty/stb/stb_vorbis.c | 2 | ||||
-rw-r--r-- | src/libjin/audio/source.h | 11 | ||||
-rw-r--r-- | src/libjin/render/canvas.cpp | 18 | ||||
-rw-r--r-- | src/libjin/render/canvas.h | 6 | ||||
-rw-r--r-- | src/libjin/render/drawable.cpp | 13 | ||||
-rw-r--r-- | src/libjin/render/drawable.h | 7 | ||||
-rw-r--r-- | src/libjin/render/image.cpp | 18 | ||||
-rw-r--r-- | src/libjin/render/image.h | 21 | ||||
-rw-r--r-- | src/libjin/render/jsl.cpp | 54 | ||||
-rw-r--r-- | src/libjin/render/jsl.h | 44 | ||||
-rw-r--r-- | src/libjin/tilemap/tilemap.h | 14 | ||||
-rw-r--r-- | src/script/audio/luaopen_Sound.cpp | 1 | ||||
-rw-r--r-- | src/script/audio/luaopen_audio.cpp | 5 | ||||
-rw-r--r-- | src/script/graphics/luaopen_Canvas.cpp | 9 | ||||
-rw-r--r-- | src/script/graphics/luaopen_Image.cpp | 9 | ||||
-rw-r--r-- | src/script/graphics/luaopen_JSL.cpp | 19 | ||||
-rw-r--r-- | src/script/graphics/luaopen_graphics.cpp | 43 | ||||
-rw-r--r-- | src/script/joypad/joypad.h | 14 | ||||
-rw-r--r-- | src/script/joypad/luaopen_joypad.cpp | 21 | ||||
-rw-r--r-- | src/script/luaopen_jin.cpp | 4 | ||||
-rw-r--r-- | src/script/luaopen_types.h | 12 |
22 files changed, 228 insertions, 122 deletions
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h index 3816430..49036b9 100644 --- a/src/3rdparty/luax/luax.h +++ b/src/3rdparty/luax/luax.h @@ -158,11 +158,6 @@ inline void* luax_newinstance(lua_State* L, const char* tname, int size) { void* p = lua_newuserdata(L, size); - //u->data = data; - //u->flags = flags; - //u->own = own; - - // luaL_getmetatable(L, tname) or luaL_newmetatable(L, tname); lua_setmetatable(L, -2); diff --git a/src/3rdparty/stb/stb_vorbis.c b/src/3rdparty/stb/stb_vorbis.c index 3d338f0..873eda6 100644 --- a/src/3rdparty/stb/stb_vorbis.c +++ b/src/3rdparty/stb/stb_vorbis.c @@ -5516,4 +5516,4 @@ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ -*/
\ No newline at end of file +*/ diff --git a/src/libjin/audio/source.h b/src/libjin/audio/source.h index 8b4b888..ad9a8ba 100644 --- a/src/libjin/audio/source.h +++ b/src/libjin/audio/source.h @@ -10,6 +10,17 @@ namespace audio class Source { + public: + void play(); + void stop(); + void pause(); + void resume(); + void rewind(); + void isStopped() const; + void isPaused() const; + void isFinished() const; + void setPitch(float pitch); + void setVolume(float volume); }; diff --git a/src/libjin/render/canvas.cpp b/src/libjin/render/canvas.cpp index 376c076..89d0d77 100644 --- a/src/libjin/render/canvas.cpp +++ b/src/libjin/render/canvas.cpp @@ -6,26 +6,26 @@ namespace jin { namespace render { - Canvas::Canvas() :Drawable() + Canvas::Canvas(int w, int h) + : Drawable(w, h) { + init(); } Canvas::~Canvas() { } - // no canvas has binded shared GLint Canvas::cur = -1; - bool Canvas::init(int w, int h) + bool Canvas::init() { - Drawable::init(w, h); - Drawable::setVertices( + setVertices( new float [DRAWABLE_V_SIZE] { 0, 0, - 0, (float)h, - (float)w, (float)h, - (float)w, 0, + 0, (float)height, + (float)width, (float)height, + (float)width, 0, }, new float [DRAWABLE_V_SIZE] { 0, 1, @@ -47,7 +47,7 @@ namespace render glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); diff --git a/src/libjin/render/canvas.h b/src/libjin/render/canvas.h index 6f81ed6..e65c0c1 100644 --- a/src/libjin/render/canvas.h +++ b/src/libjin/render/canvas.h @@ -9,11 +9,9 @@ namespace render { public: - Canvas(); + Canvas(int w, int h); ~Canvas(); - bool init(int w, int h); - void bind(); static void unbind(); @@ -26,6 +24,8 @@ namespace render // current binded fbo static GLint cur; + + bool init(); }; } } diff --git a/src/libjin/render/drawable.cpp b/src/libjin/render/drawable.cpp index 1a4cc43..c0d40a1 100644 --- a/src/libjin/render/drawable.cpp +++ b/src/libjin/render/drawable.cpp @@ -6,7 +6,7 @@ namespace jin { namespace render { - Drawable::Drawable():texture(0), width(0), height(0), ancx(0), ancy(0), textCoord(0), vertCoord(0) + Drawable::Drawable(int w, int h):texture(0), width(w), height(h), ancx(0), ancy(0), textCoord(0), vertCoord(0) { } @@ -17,17 +17,6 @@ namespace render delete[] textCoord; } - void Drawable::init(int w, int h) - { - texture = 0; - width = w; - height = h; - ancx = 0; - ancy = 0; - textCoord = 0; - vertCoord = 0; - } - void Drawable::setVertices(float* v, float* t) { // render area diff --git a/src/libjin/render/drawable.h b/src/libjin/render/drawable.h index baf6c97..b8e985a 100644 --- a/src/libjin/render/drawable.h +++ b/src/libjin/render/drawable.h @@ -8,12 +8,9 @@ namespace render class Drawable { public: - - Drawable(); + Drawable(int w = 0, int h = 0); virtual ~Drawable(); - - void init(int w = 0, int h = 0); - + void setAnchor(int x, int y); void draw(int x, int y, float sx, float sy, float r); diff --git a/src/libjin/render/image.cpp b/src/libjin/render/image.cpp index b27b9a6..5de997a 100644 --- a/src/libjin/render/image.cpp +++ b/src/libjin/render/image.cpp @@ -1,23 +1,27 @@ #include "image.h" #include "3rdparty/stb/stb_image.h" #include "../utils/utils.h" + namespace jin { namespace render { - Image::Image(): Drawable(), pixels(0) + + Image::Image(const char* file) + : Drawable(), pixels(0) { + loadf(file); } - Image::~Image() + Image::Image(const char* buffer, size_t size) + : Drawable(), pixels(0) { - stbi_image_free(pixels); + loadb(buffer, size); } - void Image::init() + Image::~Image() { - Drawable::init(); - pixels = 0; + stbi_image_free(pixels); } color Image::getPixel(int x, int y) @@ -61,7 +65,7 @@ namespace render return true; } - bool Image::loadb(const char* b, int size) + bool Image::loadb(const char* b, size_t size) { // ʹstbi_load_from_memory unsigned char* imageData = stbi_load_from_memory((unsigned char *)b, size, &width, &height, NULL, STBI_rgb_alpha); diff --git a/src/libjin/render/image.h b/src/libjin/render/image.h index 7375fc4..58ca04f 100644 --- a/src/libjin/render/image.h +++ b/src/libjin/render/image.h @@ -9,24 +9,23 @@ namespace render { class Image: public Drawable { + public: - Image(); + + Image(const char* file); + Image(const char* buffer, size_t size); + ~Image(); - // just like Image() - void init(); - - // load from file - bool loadf(const char* f); - - // load from memory - bool loadb(const char* b, int size); - color getPixel(int x, int y); private: - + + bool loadf(const char* file); + bool loadb(const char* buffer, size_t size); + color* pixels; + }; } } diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp index 56cbc31..49bb18f 100644 --- a/src/libjin/render/jsl.cpp +++ b/src/libjin/render/jsl.cpp @@ -18,15 +18,31 @@ namespace render "gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n" "}\0"; - shared GLint JSLProgram::current_texture_unit = 0; - shared GLint JSLProgram::max_texture_units = 0; + shared GLint JSLProgram::currentTextureUnit = 0; + shared GLint JSLProgram::maxTextureUnits = -1; - shared JSLProgram* JSLProgram::current_JSL_program = nullptr; + shared JSLProgram* JSLProgram::currentJSLProgram = nullptr; - void JSLProgram::init(const char* program) + JSLProgram::JSLProgram(const char* program) { - if(max_texture_units == 0) - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units); + initialize(program); + } + + JSLProgram::~JSLProgram() + { + destroy(); + } + + void JSLProgram::destroy() + { + if (currentJSLProgram == this) + unuse(); + } + + void JSLProgram::initialize(const char* program) + { + if (maxTextureUnits == -1) + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); char* fs = (char*)alloca(strlen(program) + strlen(base_f)); sprintf(fs, base_f, program); @@ -39,28 +55,18 @@ namespace render glLinkProgram(pid); } - shared GLint JSLProgram::getTextureUnit(const std::string& name) + GLint JSLProgram::getTextureUnit(const std::string& name) { - if (++current_texture_unit >= max_texture_units) + std::map<std::string, GLint>::iterator texture_unit = texturePool.find(name); + if (texture_unit != texturePool.end()) + return texture_unit->second; + if (++currentTextureUnit >= maxTextureUnits) return 0; - return current_texture_unit; - } - - void JSLProgram::use() - { - glUseProgram(pid); - JSLProgram::current_JSL_program = this; - JSLProgram::current_texture_unit = 0; - } - - shared void JSLProgram::unuse() - { - glUseProgram(0); - JSLProgram::current_JSL_program = nullptr; - JSLProgram::current_texture_unit = 0; + texturePool[name] = currentTextureUnit; + return currentTextureUnit; } -#define checkJSL() if (current_JSL_program != this) return +#define checkJSL() if (currentJSLProgram != this) return void JSLProgram::sendFloat(const char* variable, float number) { diff --git a/src/libjin/render/jsl.h b/src/libjin/render/jsl.h index 80ef4dc..7183325 100644 --- a/src/libjin/render/jsl.h +++ b/src/libjin/render/jsl.h @@ -17,11 +17,22 @@ namespace render { public: - void init(const char* program); + JSLProgram(const char* program); + ~JSLProgram(); - void use(); - - static void unuse(); + inline void JSLProgram::use() + { + glUseProgram(pid); + JSLProgram::currentJSLProgram = this; + JSLProgram::currentTextureUnit = 0; + } + + static inline void JSLProgram::unuse() + { + glUseProgram(0); + JSLProgram::currentJSLProgram = nullptr; + JSLProgram::currentTextureUnit = 0; + } void sendFloat(const char* name, float number); void sendImage(const char* name, const Image* image); @@ -30,18 +41,27 @@ namespace render void sendVec4(const char* name, float x, float y, float z, float w); void sendCanvas(const char* name, const Canvas* canvas); void sendColor(const char* name, const color* col); - - private: - JSLProgram(); + static inline JSLProgram* getCurrentJSL() + { + return currentJSLProgram; + } + + private: GLuint pid; - - static JSLProgram* current_JSL_program; - static GLint current_texture_unit; - static GLint max_texture_units; - static GLint getTextureUnit(const std::string& name); + std::map<std::string, GLint> texturePool; + + static JSLProgram* currentJSLProgram; + static GLint currentTextureUnit; + static GLint maxTextureUnits; + + GLint getTextureUnit(const std::string& name); + + void initialize(const char* program); + void destroy(); + }; } diff --git a/src/libjin/tilemap/tilemap.h b/src/libjin/tilemap/tilemap.h new file mode 100644 index 0000000..27cbe51 --- /dev/null +++ b/src/libjin/tilemap/tilemap.h @@ -0,0 +1,14 @@ +#ifndef __JIN_TILEMAP_H +#define __JIN_TILEMAP_H + +namespace jin +{ +namespace tilemap +{ + + + +}// tilemap +}// jin + +#endif
\ No newline at end of file diff --git a/src/script/audio/luaopen_Sound.cpp b/src/script/audio/luaopen_Sound.cpp index f2767bd..d43147e 100644 --- a/src/script/audio/luaopen_Sound.cpp +++ b/src/script/audio/luaopen_Sound.cpp @@ -21,5 +21,6 @@ namespace lua return 1; } + } } diff --git a/src/script/audio/luaopen_audio.cpp b/src/script/audio/luaopen_audio.cpp index f170115..78d7a69 100644 --- a/src/script/audio/luaopen_audio.cpp +++ b/src/script/audio/luaopen_audio.cpp @@ -22,7 +22,7 @@ namespace lua return 0; } - + static const luaL_Reg f[] = { {"init", l_init}, {"Sound", l_newSound}, @@ -31,7 +31,8 @@ namespace lua int luaopen_audio(lua_State* L) { - + luax_newlib(L, f); + return 1; } } diff --git a/src/script/graphics/luaopen_Canvas.cpp b/src/script/graphics/luaopen_Canvas.cpp index f869882..a34e3b3 100644 --- a/src/script/graphics/luaopen_Canvas.cpp +++ b/src/script/graphics/luaopen_Canvas.cpp @@ -11,7 +11,10 @@ namespace lua static inline Canvas* checkCanvas(lua_State* L) { - return (Canvas*)luax_checktype(L, 1, TYPE_CANVAS); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + if (proxy != nullptr) + return (Canvas*)proxy->object; + return nullptr; } static int l_getWidth(lua_State* L) @@ -47,7 +50,9 @@ namespace lua static int l_gc(lua_State* L) { - + Canvas* canvas = checkCanvas(L); + if (canvas != nullptr) + delete canvas; return 0; } diff --git a/src/script/graphics/luaopen_Image.cpp b/src/script/graphics/luaopen_Image.cpp index 7b9b96a..9506ce4 100644 --- a/src/script/graphics/luaopen_Image.cpp +++ b/src/script/graphics/luaopen_Image.cpp @@ -11,7 +11,10 @@ namespace lua static inline Image* checkImage(lua_State* L) { - return (Image*)luax_checktype(L, 1, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); + if (proxy != 0 && proxy != nullptr) + return (Image*)proxy->object; + return nullptr; } static int l_getWidth(lua_State* L) @@ -60,7 +63,9 @@ namespace lua static int l_gc(lua_State* L) { - + Image* i = checkImage(L); + if (i != nullptr) + delete i; return 0; } diff --git a/src/script/graphics/luaopen_JSL.cpp b/src/script/graphics/luaopen_JSL.cpp index b5ba125..33afa2c 100644 --- a/src/script/graphics/luaopen_JSL.cpp +++ b/src/script/graphics/luaopen_JSL.cpp @@ -11,7 +11,10 @@ namespace lua static inline JSLProgram* checkJSLProgram(lua_State* L) { - return (JSLProgram*)luax_checktype(L, 1, TYPE_JSL); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); + if(proxy != nullptr) + return (JSLProgram*)proxy->object; + return nullptr; } static enum VARIABLE_TYPE @@ -63,13 +66,15 @@ namespace lua } case IMAGE: { - Image* img = (Image*)luax_checktype(L, 4, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); + Image* img = (Image*)proxy->object; jsl->sendImage(variable, img); break; } case CANVAS: { - Canvas* canvas = (Canvas*)luax_checktype(L, 4, TYPE_IMAGE); + Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); + Canvas* canvas = (Canvas*)proxy->object; jsl->sendCanvas(variable, canvas); break; } @@ -116,13 +121,17 @@ namespace lua static int l_gc(lua_State* L) { - + JSLProgram* jsl = checkJSLProgram(L); + if (jsl != nullptr && jsl != NULL) + { + delete jsl; + } return 0; } static const luaL_Reg f[] = { + {"__gc", l_gc }, {"send", l_send}, - {"__gc", l_gc}, {0, 0} }; diff --git a/src/script/graphics/luaopen_graphics.cpp b/src/script/graphics/luaopen_graphics.cpp index 80acc94..0d240fe 100644 --- a/src/script/graphics/luaopen_graphics.cpp +++ b/src/script/graphics/luaopen_graphics.cpp @@ -76,9 +76,6 @@ namespace lua */ static int l_newImage(lua_State* L) { - Image* img = (Image*)luax_newinstance(L, TYPE_IMAGE, sizeof(Image)); - // pseudo constructor - img->init(); Filesystem* fs = Filesystem::get(); const char* f = luax_checkstring(L, 1); if (!fs->exists(f)) @@ -87,8 +84,11 @@ namespace lua exit(1); } Buffer b; - fs->read(f, &b); - img->loadb((const char*)b.data, b.size); + fs->read(f, &b); + + Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy)); + Image* img = new Image((const char*)b.data, b.size); + proxy->bind(img); return 1; } @@ -98,10 +98,10 @@ namespace lua */ static int l_newShader(lua_State* L) { - JSLProgram* j = (JSLProgram*)luax_newinstance(L, TYPE_JSL, sizeof(JSLProgram)); - const char* modestr = luax_checkstring(L, 1); - j->init(modestr); - + Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_JSL, sizeof(JSLProgram)); + const char* program = luax_checkstring(L, 1); + JSLProgram* jsl = new JSLProgram(program); + proxy->bind(jsl); return 1; } @@ -111,10 +111,11 @@ namespace lua */ static int l_newCanvas(lua_State* L) { - Canvas* cvs = (Canvas*)luax_newinstance(L, TYPE_CANVAS, sizeof(Canvas)); int w = luax_checknumber(L, 1); int h = luax_checknumber(L, 2); - cvs->init(w, h); + Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_CANVAS, sizeof(Proxy)); + Canvas* cvs = new Canvas(w, h); + proxy->bind(cvs); return 1; } @@ -155,14 +156,14 @@ namespace lua float r = luax_optnumber(L, 6, 0); if (luax_istype(L, 1, TYPE_IMAGE)) { - /* is image */ - Image* p = (Image*)luax_toudata(L, 1); - p->draw(x, y, sx, sy, r); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Image* img = (Image*)proxy->object; + img->draw(x, y, sx, sy, r); } else if (luax_istype(L, 1, TYPE_CANVAS)) { - /* is canvas */ - Canvas* p = (Canvas*)luax_toudata(L, 1); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Canvas* p = (Canvas*)proxy->object; p->draw(x, y, sx, sy, r); } else @@ -212,7 +213,8 @@ namespace lua Canvas::unbind(); return 0; } - Canvas* c = (Canvas*)luax_checktype(L, 1, TYPE_CANVAS); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + Canvas* c = (Canvas*)proxy->object; c->bind(); return 0; } @@ -232,13 +234,12 @@ namespace lua } if (luax_istype(L, 1, TYPE_JSL)) { - /* is image */ - JSLProgram* jsl = (JSLProgram*)luax_toudata(L, 1); - jsl->use(); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + JSLProgram* jsl = (JSLProgram*)proxy->object; + jsl->use(); } else { - /* wrong type */ luax_typerror(L, 1, "JSL shader"); } return 0; diff --git a/src/script/joypad/joypad.h b/src/script/joypad/joypad.h new file mode 100644 index 0000000..e8d309b --- /dev/null +++ b/src/script/joypad/joypad.h @@ -0,0 +1,14 @@ +#ifndef __JIN_JOYPAD_H +#define __JIN_JOYPAD_H + +namespace jin +{ +namespace input +{ + + + +} +} + +#endif
\ No newline at end of file diff --git a/src/script/joypad/luaopen_joypad.cpp b/src/script/joypad/luaopen_joypad.cpp new file mode 100644 index 0000000..0166a57 --- /dev/null +++ b/src/script/joypad/luaopen_joypad.cpp @@ -0,0 +1,21 @@ +#include "libjin/jin.h" +#include "3rdparty/luax/luax.h" + +namespace jin +{ +namespace lua +{ + + static const luaL_Reg f[] = { + { 0, 0 } + }; + + int luaopen_joypad(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } + +} +}
\ No newline at end of file diff --git a/src/script/luaopen_jin.cpp b/src/script/luaopen_jin.cpp index d7262e1..dcf8830 100644 --- a/src/script/luaopen_jin.cpp +++ b/src/script/luaopen_jin.cpp @@ -18,6 +18,7 @@ namespace lua extern int luaopen_mouse(lua_State* L); extern int luaopen_keyboard(lua_State* L); extern int luaopen_filesystem(lua_State* L); + extern int luaopen_joypad(lua_State* L); static int l_getversion(lua_State* L) { @@ -60,7 +61,8 @@ namespace lua {"keyboard", luaopen_keyboard}, {"filesystem", luaopen_filesystem}, {"net", luaopen_net}, - //{"audio", luaopen_audio}, + {"audio", luaopen_audio}, + {"joypad", luaopen_joypad}, {0, 0} }; diff --git a/src/script/luaopen_types.h b/src/script/luaopen_types.h index 292d991..8c1f2a6 100644 --- a/src/script/luaopen_types.h +++ b/src/script/luaopen_types.h @@ -10,4 +10,16 @@ // audio module #define TYPE_SOUND "Sound" +class Proxy +{ +public: + inline void bind(void* obj) + { + if (obj != 0 && obj != nullptr) + object = obj; + } + + void* object; +}; + #endif |