diff options
author | chai <chaifix@163.com> | 2018-05-19 12:05:57 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-05-19 12:05:57 +0800 |
commit | adfda73e1810973a40b7bedd9a8edc3e7ab89e3c (patch) | |
tree | 3ee6b9ad4f3c71b5cefc719c694e7a80f3df7088 /src/script | |
parent | 3ce3b10167a98f45614408a8042a10c686f3a9cc (diff) |
增加子系统基类
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/embed/boot.lua.h | 33 | ||||
-rw-r--r-- | src/script/embed/debug.lua.h | 72 | ||||
-rw-r--r-- | src/script/event/luaopen_event.cpp | 21 | ||||
-rw-r--r-- | src/script/graphics/luaopen_JSL.cpp | 98 | ||||
-rw-r--r-- | src/script/graphics/luaopen_graphics.cpp | 11 |
5 files changed, 123 insertions, 112 deletions
diff --git a/src/script/embed/boot.lua.h b/src/script/embed/boot.lua.h index 223f354..cb61468 100644 --- a/src/script/embed/boot.lua.h +++ b/src/script/embed/boot.lua.h @@ -1,23 +1,7 @@ /* boot.lua */ static const char* boot_lua = R"( -local function _onEvent(e) - -- update keyboard status - if e.type == "keydown" then - jin.keyboard.set(e.key, true) - elseif e.type == "keyup" then - jin.keyboard.set(e.key, false) - end - - -- call user onEvent function - if jin.core.onEvent then - jin.core.onEvent(e) - end -end - -------------------------------------------------- --- init file system -------------------------------------------------- +-- init filesystem jin._argv[2] = jin._argv[2] or '.' jin.filesystem.init() jin.filesystem.mount(jin._argv[2]) @@ -54,13 +38,26 @@ function jin.core.run() local study = jin.graphics.study local onDraw = jin.core.onDraw local onUpdate = jin.core.onUpdate + local onEvent = jin.core.onEvent local present = jin.graphics.present + local setkey = jin.keyboard.set local dstatus = jin.debug.status local drender = jin.debug.render local fps = conf.fps - + + local _onEvent = function (e) + if e.type == "keydown" then + setkey(e.key, true) + elseif e.type == "keyup" then + setkey(e.key, false) + end + if onEvent then + onEvent(e) + end + end + if load then load() end diff --git a/src/script/embed/debug.lua.h b/src/script/embed/debug.lua.h index 7ccc99d..79c95ba 100644 --- a/src/script/embed/debug.lua.h +++ b/src/script/embed/debug.lua.h @@ -33,27 +33,27 @@ local refresh = true function jin.debug.init() debug = true - panel = jin.graphics.Canvas(jin.graphics.size()) + panel = jin.graphics.Canvas(jin.graphics.size()) end -- set buffer size function jin.debug.size(c) - bsize = c + bsize = c end function jin.debug.print(msg) - if not debug then return end - - msg = tostring(msg) - local tp = type(msg) - if tp ~= "string" and tp ~= "number" then - msg = string.format("print failed, expect string or number but get a %s", tp) - end - + if not debug then return end + + msg = tostring(msg) + local tp = type(msg) + if tp ~= "string" and tp ~= "number" then + msg = string.format("print failed, expect string or number but get a %s", tp) + end + -- remove the first one (old msg) - if #buffer >= bsize then - table.remove(buffer, 1) - end + if #buffer >= bsize then + table.remove(buffer, 1) + end buffer[#buffer + 1] = msg refresh = true @@ -61,33 +61,33 @@ end -- clear debug buffer function jin.debug.clear() - buffer = {} + buffer = {} end local function getStrHeight(str, lheight) - local h = lheight - if #str == 0 then - h = 0 - end - for i = 1, #str do - local c = string.sub(str, i, i) - if c == '\n' then - h = h + lheight - end - end - return h + local h = lheight + if #str == 0 then + h = 0 + end + for i = 1, #str do + local c = string.sub(str, i, i) + if c == '\n' then + h = h + lheight + end + end + return h end local function getBgQuad() - local width, height = 0, 0 - for i = 1, #buffer do - local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) - height = height + h - if width < w then - width = w - end - end - return width, height + local width, height = 0, 0 + for i = 1, #buffer do + local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) + height = height + h + if width < w then + width = w + end + end + return width, height end -- render to screen @@ -115,7 +115,7 @@ function jin.debug.render() y = y - h jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) end - + jin.graphics.bind() refresh = false @@ -126,7 +126,7 @@ function jin.debug.render() end function jin.debug.status() - return debug + return debug end )";
\ No newline at end of file diff --git a/src/script/event/luaopen_event.cpp b/src/script/event/luaopen_event.cpp index 87882e7..16ecbad 100644 --- a/src/script/event/luaopen_event.cpp +++ b/src/script/event/luaopen_event.cpp @@ -12,6 +12,23 @@ namespace jin { namespace lua { + + static inline const char* buttonstr(int id) { + switch (id) { + case 1: return "left"; + case 2: return "middle"; + case 3: return "right"; + case 4: return "wheelup"; + case 5: return "wheeldown"; + default: return "?"; + } + } + + static inline const char* wheelstr(int dir) + { + + } + /** * Load event poll, return a iterator(a table). */ @@ -50,14 +67,14 @@ namespace lua case SDL_MOUSEBUTTONDOWN: luax_setfield_string(L, "type", "mousebuttondown"); - luax_setfield_string(L, "button", buttonStr(e.button.button)); + luax_setfield_string(L, "button", buttonstr(e.button.button)); luax_setfield_number(L, "x", e.button.x); luax_setfield_number(L, "y", e.button.y); break; case SDL_MOUSEBUTTONUP: luax_setfield_string(L, "type", "mousebuttonup"); - luax_setfield_string(L, "button", buttonStr(e.button.button)); + luax_setfield_string(L, "button", buttonstr(e.button.button)); luax_setfield_number(L, "x", e.button.x); luax_setfield_number(L, "y", e.button.y); break; diff --git a/src/script/graphics/luaopen_JSL.cpp b/src/script/graphics/luaopen_JSL.cpp index adb109c..d5db363 100644 --- a/src/script/graphics/luaopen_JSL.cpp +++ b/src/script/graphics/luaopen_JSL.cpp @@ -24,22 +24,22 @@ namespace lua NUMBER, IMAGE, CANVAS, - VEC2, - VEC3, - VEC4, - COLOR, + VEC2, + VEC3, + VEC4, + COLOR, }; static VARIABLE_TYPE strtotype(const char* str) { std::string s = std::string(str); - if (s == "number") return NUMBER; + 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 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; } @@ -72,48 +72,48 @@ namespace lua break; } case CANVAS: - { + { Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); Canvas* canvas = (Canvas*)proxy->object; - jsl->sendCanvas(variable, canvas); - break; - } - case VEC2: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - jsl->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); - jsl->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); - jsl->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); - jsl->sendColor(variable, &col); - break; - } - default: - return 0; + jsl->sendCanvas(variable, canvas); + break; + } + case VEC2: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + jsl->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); + jsl->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); + jsl->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); + jsl->sendColor(variable, &col); + break; + } + default: + return 0; } } return 1; diff --git a/src/script/graphics/luaopen_graphics.cpp b/src/script/graphics/luaopen_graphics.cpp index 6e78e36..948e28a 100644 --- a/src/script/graphics/luaopen_graphics.cpp +++ b/src/script/graphics/luaopen_graphics.cpp @@ -30,15 +30,12 @@ namespace lua static int l_init(lua_State* L) { Window* wnd = Window::get(); - Window::Setting setting; + WindowSetting setting; setting.width = luax_getfield_integer(L, 1, "width"); setting.height = luax_getfield_integer(L, 1, "height"); setting.title = luax_getfield_string(L, 1, "title"); setting.vsync = luax_getfield_bool(L, 1, "vsync"); - wnd->init(setting); - - // init success - luax_pushboolean(L, true); + luax_pushboolean(L, wnd->init(&setting)); return 1; } @@ -357,8 +354,8 @@ namespace lua int tn = luax_tableidxlen(L, 3); if (tn != n * 2) { - static char emsg[] = - "number of polygon vertices doesn't match " + static char* emsg = \ + "number of polygon vertices doesn't match " \ "provided n, expect %d numbers but get %d"; luax_error(L, emsg, n * 2, tn); return 1; |