From 36621e6be9604517c900adc0d97665e975c2b325 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 22 Aug 2018 13:58:13 +0800 Subject: *update --- src/lua/modules/audio/audio.cpp | 110 +++++ src/lua/modules/audio/luaopen_Source.cpp | 116 ----- src/lua/modules/audio/luaopen_audio.cpp | 110 ----- src/lua/modules/audio/source.cpp | 116 +++++ src/lua/modules/bit/bit.cpp | 84 ++++ src/lua/modules/bit/luaopen_bit.cpp | 84 ---- src/lua/modules/core/core.cpp | 44 ++ src/lua/modules/core/luaopen_core.cpp | 44 -- src/lua/modules/embed/boot.lua.h | 21 +- src/lua/modules/event/event.cpp | 106 +++++ src/lua/modules/event/luaopen_event.cpp | 106 ----- src/lua/modules/filesystem/filesystem.cpp | 137 ++++++ src/lua/modules/filesystem/luaopen_filesystem.cpp | 137 ------ src/lua/modules/graphics/canvas.cpp | 75 ++++ src/lua/modules/graphics/font.cpp | 49 ++ src/lua/modules/graphics/graphics.cpp | 516 ++++++++++++++++++++++ src/lua/modules/graphics/image.cpp | 89 ++++ src/lua/modules/graphics/jsl.cpp | 146 ++++++ src/lua/modules/graphics/luaopen_Canvas.cpp | 75 ---- src/lua/modules/graphics/luaopen_Font.cpp | 49 -- src/lua/modules/graphics/luaopen_Image.cpp | 89 ---- src/lua/modules/graphics/luaopen_JSL.cpp | 146 ------ src/lua/modules/graphics/luaopen_graphics.cpp | 516 ---------------------- src/lua/modules/jin.cpp | 104 +++++ src/lua/modules/jin.h | 42 ++ src/lua/modules/joypad/joypad.cpp | 21 + src/lua/modules/joypad/luaopen_joypad.cpp | 21 - src/lua/modules/keyboard/keyboard.cpp | 16 + src/lua/modules/keyboard/luaopen_keyboard.cpp | 16 - src/lua/modules/luaopen_jin.cpp | 104 ----- src/lua/modules/luaopen_jin.h | 42 -- src/lua/modules/math/luaopen_math.cpp | 30 -- src/lua/modules/math/math.cpp | 30 ++ src/lua/modules/mouse/luaopen_mouse.cpp | 31 -- src/lua/modules/mouse/mouse.cpp | 31 ++ src/lua/modules/net/Buffer.cpp | 136 ++++++ src/lua/modules/net/luaopen_Buffer.cpp | 136 ------ src/lua/modules/net/luaopen_Socket.cpp | 127 ------ src/lua/modules/net/luaopen_net.cpp | 85 ---- src/lua/modules/net/net.cpp | 85 ++++ src/lua/modules/net/socket.cpp | 127 ++++++ src/lua/modules/thread/Thread.cpp | 243 +++++++++- src/lua/modules/thread/luaopen_thread.cpp | 249 ----------- src/lua/modules/time/luaopen_time.cpp | 38 -- src/lua/modules/time/time.cpp | 38 ++ 45 files changed, 2350 insertions(+), 2367 deletions(-) create mode 100644 src/lua/modules/audio/audio.cpp delete mode 100644 src/lua/modules/audio/luaopen_Source.cpp delete mode 100644 src/lua/modules/audio/luaopen_audio.cpp create mode 100644 src/lua/modules/audio/source.cpp create mode 100644 src/lua/modules/bit/bit.cpp delete mode 100644 src/lua/modules/bit/luaopen_bit.cpp create mode 100644 src/lua/modules/core/core.cpp delete mode 100644 src/lua/modules/core/luaopen_core.cpp create mode 100644 src/lua/modules/event/event.cpp delete mode 100644 src/lua/modules/event/luaopen_event.cpp create mode 100644 src/lua/modules/filesystem/filesystem.cpp delete mode 100644 src/lua/modules/filesystem/luaopen_filesystem.cpp create mode 100644 src/lua/modules/graphics/canvas.cpp create mode 100644 src/lua/modules/graphics/font.cpp create mode 100644 src/lua/modules/graphics/graphics.cpp create mode 100644 src/lua/modules/graphics/image.cpp create mode 100644 src/lua/modules/graphics/jsl.cpp delete mode 100644 src/lua/modules/graphics/luaopen_Canvas.cpp delete mode 100644 src/lua/modules/graphics/luaopen_Font.cpp delete mode 100644 src/lua/modules/graphics/luaopen_Image.cpp delete mode 100644 src/lua/modules/graphics/luaopen_JSL.cpp delete mode 100644 src/lua/modules/graphics/luaopen_graphics.cpp create mode 100644 src/lua/modules/jin.cpp create mode 100644 src/lua/modules/jin.h create mode 100644 src/lua/modules/joypad/joypad.cpp delete mode 100644 src/lua/modules/joypad/luaopen_joypad.cpp create mode 100644 src/lua/modules/keyboard/keyboard.cpp delete mode 100644 src/lua/modules/keyboard/luaopen_keyboard.cpp delete mode 100644 src/lua/modules/luaopen_jin.cpp delete mode 100644 src/lua/modules/luaopen_jin.h delete mode 100644 src/lua/modules/math/luaopen_math.cpp create mode 100644 src/lua/modules/math/math.cpp delete mode 100644 src/lua/modules/mouse/luaopen_mouse.cpp create mode 100644 src/lua/modules/mouse/mouse.cpp create mode 100644 src/lua/modules/net/Buffer.cpp delete mode 100644 src/lua/modules/net/luaopen_Buffer.cpp delete mode 100644 src/lua/modules/net/luaopen_Socket.cpp delete mode 100644 src/lua/modules/net/luaopen_net.cpp create mode 100644 src/lua/modules/net/net.cpp create mode 100644 src/lua/modules/net/socket.cpp delete mode 100644 src/lua/modules/thread/luaopen_thread.cpp delete mode 100644 src/lua/modules/time/luaopen_time.cpp create mode 100644 src/lua/modules/time/time.cpp (limited to 'src/lua/modules') diff --git a/src/lua/modules/audio/audio.cpp b/src/lua/modules/audio/audio.cpp new file mode 100644 index 0000000..8e11b88 --- /dev/null +++ b/src/lua/modules/audio/audio.cpp @@ -0,0 +1,110 @@ +#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::audio; + using namespace jin::filesystem; + + typedef SDLAudio Audio; + typedef SDLSource Source; + + static int l_init(lua_State* L) + { + Audio::Setting setting; + setting.samplerate = 44100; + setting.samples = 44100; + if (!Audio::get()->init(&setting)) + { + luax_error(L, "could not init audio"); + luax_pushboolean(L, false); + return 1; + } + luax_pushboolean(L, true); + return 1; + } + + static int l_play(lua_State* L) + { + Audio::get()->play(); + return 0; + } + + static int l_stop(lua_State* L) + { + Audio::get()->stop(); + return 0; + } + + static int l_pause(lua_State* L) + { + Audio::get()->pause(); + return 0; + } + + static int l_resume(lua_State* L) + { + Audio::get()->resume(); + return 0; + } + + static int l_setVolume(lua_State* L) + { + float volume = luax_checknumber(L, 1); + Audio::get()->setVolume(volume); + return 0; + } + + static int l_newSource(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); + exit(1); + } + Buffer b; + fs->read(f, &b); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); + Source* src = Source::createSource(b.data, b.size); + proxy->bind(new Ref(src, JIN_AUDIO_SOURCE)); + return 1; + } + + static int l_destroy(lua_State* L) + { + Audio* audio = Audio::get(); + audio->quit(); + return 0; + } + + static const luaL_Reg f[] = { + { "init", l_init }, + { "play", l_play }, + { "stop", l_stop }, + { "pause", l_pause }, + { "resume", l_resume }, + { "setVolume", l_setVolume }, + { "newSource", l_newSource }, + { "destroy", l_destroy }, + { 0, 0 } + }; + + extern int luaopen_Source(lua_State* L); + + int luaopen_audio(lua_State* L) + { + luaopen_Source(L); + + luax_newlib(L, f); + + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/modules/audio/luaopen_Source.cpp b/src/lua/modules/audio/luaopen_Source.cpp deleted file mode 100644 index 1953121..0000000 --- a/src/lua/modules/audio/luaopen_Source.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include "libjin/jin.h" -#include "lua/modules/luax.h" -#include "lua/common/common.h" -#include "lua/modules/types.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::audio; - - typedef Ref& SourceRef; - - static inline SourceRef checkSource(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); - return proxy->getRef(); - } - - static int l_play(lua_State* L) - { - SourceRef ref = checkSource(L); - ref->play(); - return 0; - } - - static int l_stop(lua_State* L) - { - SourceRef ref = checkSource(L); - ref->stop(); - return 0; - } - - static int l_pause(lua_State* L) - { - SourceRef ref = checkSource(L); - ref->pause(); - return 0; - } - - static int l_rewind(lua_State* L) - { - SourceRef ref = checkSource(L); - ref->rewind(); - return 0; - } - - static int l_resume(lua_State* L) - { - SourceRef ref = checkSource(L); - ref->resume(); - return 0; - } - - static int l_isStop(lua_State* L) - { - SourceRef ref = checkSource(L); - bool isStop = ref->isStopped(); - luax_pushboolean(L, isStop); - return 1; - } - - static int l_isPaused(lua_State* L) - { - SourceRef ref = checkSource(L); - bool isPaused = ref->isPaused(); - luax_pushboolean(L, isPaused); - return 1; - } - - static int l_setVolume(lua_State* L) - { - SourceRef ref = checkSource(L); - float volume = luax_checknumber(L, 2); - ref->setVolume(volume); - return 0; - } - - static int l_setLoop(lua_State* L) - { - SourceRef ref = checkSource(L); - bool loop = luax_checkbool(L, 2); - ref->setLoop(loop); - return 0; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "play", l_play }, - { "stop", l_stop }, - { "pause", l_pause }, - { "resume", l_resume }, - { "rewind", l_rewind }, - { "isStop", l_isStop }, - { "isPaused", l_isPaused }, - { "setVolume", l_setVolume }, - { "setLoop", l_setLoop }, - { 0, 0 } - }; - - int luaopen_Source(lua_State* L) - { - luax_newtype(L, JIN_AUDIO_SOURCE, f); - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/audio/luaopen_audio.cpp b/src/lua/modules/audio/luaopen_audio.cpp deleted file mode 100644 index 8e11b88..0000000 --- a/src/lua/modules/audio/luaopen_audio.cpp +++ /dev/null @@ -1,110 +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::audio; - using namespace jin::filesystem; - - typedef SDLAudio Audio; - typedef SDLSource Source; - - static int l_init(lua_State* L) - { - Audio::Setting setting; - setting.samplerate = 44100; - setting.samples = 44100; - if (!Audio::get()->init(&setting)) - { - luax_error(L, "could not init audio"); - luax_pushboolean(L, false); - return 1; - } - luax_pushboolean(L, true); - return 1; - } - - static int l_play(lua_State* L) - { - Audio::get()->play(); - return 0; - } - - static int l_stop(lua_State* L) - { - Audio::get()->stop(); - return 0; - } - - static int l_pause(lua_State* L) - { - Audio::get()->pause(); - return 0; - } - - static int l_resume(lua_State* L) - { - Audio::get()->resume(); - return 0; - } - - static int l_setVolume(lua_State* L) - { - float volume = luax_checknumber(L, 1); - Audio::get()->setVolume(volume); - return 0; - } - - static int l_newSource(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); - exit(1); - } - Buffer b; - fs->read(f, &b); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); - Source* src = Source::createSource(b.data, b.size); - proxy->bind(new Ref(src, JIN_AUDIO_SOURCE)); - return 1; - } - - static int l_destroy(lua_State* L) - { - Audio* audio = Audio::get(); - audio->quit(); - return 0; - } - - static const luaL_Reg f[] = { - { "init", l_init }, - { "play", l_play }, - { "stop", l_stop }, - { "pause", l_pause }, - { "resume", l_resume }, - { "setVolume", l_setVolume }, - { "newSource", l_newSource }, - { "destroy", l_destroy }, - { 0, 0 } - }; - - extern int luaopen_Source(lua_State* L); - - int luaopen_audio(lua_State* L) - { - luaopen_Source(L); - - luax_newlib(L, f); - - return 1; - } -} -} \ No newline at end of file diff --git a/src/lua/modules/audio/source.cpp b/src/lua/modules/audio/source.cpp new file mode 100644 index 0000000..1953121 --- /dev/null +++ b/src/lua/modules/audio/source.cpp @@ -0,0 +1,116 @@ +#include "libjin/jin.h" +#include "lua/modules/luax.h" +#include "lua/common/common.h" +#include "lua/modules/types.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::audio; + + typedef Ref& SourceRef; + + static inline SourceRef checkSource(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); + return proxy->getRef(); + } + + static int l_play(lua_State* L) + { + SourceRef ref = checkSource(L); + ref->play(); + return 0; + } + + static int l_stop(lua_State* L) + { + SourceRef ref = checkSource(L); + ref->stop(); + return 0; + } + + static int l_pause(lua_State* L) + { + SourceRef ref = checkSource(L); + ref->pause(); + return 0; + } + + static int l_rewind(lua_State* L) + { + SourceRef ref = checkSource(L); + ref->rewind(); + return 0; + } + + static int l_resume(lua_State* L) + { + SourceRef ref = checkSource(L); + ref->resume(); + return 0; + } + + static int l_isStop(lua_State* L) + { + SourceRef ref = checkSource(L); + bool isStop = ref->isStopped(); + luax_pushboolean(L, isStop); + return 1; + } + + static int l_isPaused(lua_State* L) + { + SourceRef ref = checkSource(L); + bool isPaused = ref->isPaused(); + luax_pushboolean(L, isPaused); + return 1; + } + + static int l_setVolume(lua_State* L) + { + SourceRef ref = checkSource(L); + float volume = luax_checknumber(L, 2); + ref->setVolume(volume); + return 0; + } + + static int l_setLoop(lua_State* L) + { + SourceRef ref = checkSource(L); + bool loop = luax_checkbool(L, 2); + ref->setLoop(loop); + return 0; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); + proxy->release(); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "play", l_play }, + { "stop", l_stop }, + { "pause", l_pause }, + { "resume", l_resume }, + { "rewind", l_rewind }, + { "isStop", l_isStop }, + { "isPaused", l_isPaused }, + { "setVolume", l_setVolume }, + { "setLoop", l_setLoop }, + { 0, 0 } + }; + + int luaopen_Source(lua_State* L) + { + luax_newtype(L, JIN_AUDIO_SOURCE, f); + return 0; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/bit/bit.cpp b/src/lua/modules/bit/bit.cpp new file mode 100644 index 0000000..5b18125 --- /dev/null +++ b/src/lua/modules/bit/bit.cpp @@ -0,0 +1,84 @@ +#include "lua/modules/luax.h" +#include "libjin/jin.h" +#include + +namespace jin +{ +namespace lua +{ + + static int l_and(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a & b); + return 1; + } + + static int l_or(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a | b); + return 1; + } + + static int l_xor(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a ^ b); + return 1; + } + + static int l_not(lua_State* L) + { + int n = luax_checkinteger(L, 1); + luax_pushinteger(L, ~n); + return 1; + } + + static int l_lshift(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a << b); + return 1; + } + + static int l_rshift(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushinteger(L, a >> b); + return 1; + } + + static int l_include(lua_State* L) + { + int a = luax_checkinteger(L, 1); + int b = luax_checkinteger(L, 2); + luax_pushboolean(L, (a & b) == b); + return 1; + } + + static const luaL_Reg f[] = { + { "bAnd", l_and }, + { "bOr" , l_or }, + { "bXor", l_xor }, + { "bNot", l_not }, + { "bLs", l_lshift }, + { "bRs", l_rshift }, + { "bInc", l_include }, + { 0, 0 } + }; + + int luaopen_bit(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } + +} +} \ No newline at end of file diff --git a/src/lua/modules/bit/luaopen_bit.cpp b/src/lua/modules/bit/luaopen_bit.cpp deleted file mode 100644 index 5b18125..0000000 --- a/src/lua/modules/bit/luaopen_bit.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "lua/modules/luax.h" -#include "libjin/jin.h" -#include - -namespace jin -{ -namespace lua -{ - - static int l_and(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a & b); - return 1; - } - - static int l_or(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a | b); - return 1; - } - - static int l_xor(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a ^ b); - return 1; - } - - static int l_not(lua_State* L) - { - int n = luax_checkinteger(L, 1); - luax_pushinteger(L, ~n); - return 1; - } - - static int l_lshift(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a << b); - return 1; - } - - static int l_rshift(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushinteger(L, a >> b); - return 1; - } - - static int l_include(lua_State* L) - { - int a = luax_checkinteger(L, 1); - int b = luax_checkinteger(L, 2); - luax_pushboolean(L, (a & b) == b); - return 1; - } - - static const luaL_Reg f[] = { - { "bAnd", l_and }, - { "bOr" , l_or }, - { "bXor", l_xor }, - { "bNot", l_not }, - { "bLs", l_lshift }, - { "bRs", l_rshift }, - { "bInc", l_include }, - { 0, 0 } - }; - - int luaopen_bit(lua_State* L) - { - luax_newlib(L, f); - - return 1; - } - -} -} \ No newline at end of file diff --git a/src/lua/modules/core/core.cpp b/src/lua/modules/core/core.cpp new file mode 100644 index 0000000..0243554 --- /dev/null +++ b/src/lua/modules/core/core.cpp @@ -0,0 +1,44 @@ +#include "lua/modules/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + using namespace jin::core; + + static int l_running(lua_State* L) + { + static Game* game = Game::get(); + bool running = game->running(); + luax_pushboolean(L, running); + return 1; + } + + static int l_stop(lua_State* L) + { + Game::get()->stop(); + return 0; + } + + static int l_quit(lua_State* L) + { + Game::get()->quit(); + return 0; + } + + static const luaL_Reg f[] = { + {"running", l_running }, + {"stop", l_stop }, + {"quit", l_quit }, + {0, 0 } + }; + + int luaopen_core(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/modules/core/luaopen_core.cpp b/src/lua/modules/core/luaopen_core.cpp deleted file mode 100644 index 0243554..0000000 --- a/src/lua/modules/core/luaopen_core.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "lua/modules/luax.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - using namespace jin::core; - - static int l_running(lua_State* L) - { - static Game* game = Game::get(); - bool running = game->running(); - luax_pushboolean(L, running); - return 1; - } - - static int l_stop(lua_State* L) - { - Game::get()->stop(); - return 0; - } - - static int l_quit(lua_State* L) - { - Game::get()->quit(); - return 0; - } - - static const luaL_Reg f[] = { - {"running", l_running }, - {"stop", l_stop }, - {"quit", l_quit }, - {0, 0 } - }; - - int luaopen_core(lua_State* L) - { - luax_newlib(L, f); - - return 1; - } -} -} \ No newline at end of file diff --git a/src/lua/modules/embed/boot.lua.h b/src/lua/modules/embed/boot.lua.h index 5ce20a7..d84ca21 100644 --- a/src/lua/modules/embed/boot.lua.h +++ b/src/lua/modules/embed/boot.lua.h @@ -8,24 +8,22 @@ jin.filesystem.mount(jin._argv[2]) -- Config game ------------------------------------------------------------------------- -jin.conf = {} +jin.config = {} if jin.filesystem.exist("config.lua") then - jin.conf = require "config" + jin.config = require "config" end -jin.conf.width = jin.conf.width or 576 -jin.conf.height = jin.conf.height or 448 -jin.conf.FPS = jin.conf.FPS or 60 -jin.conf.vsync = jin.conf.vsync or false -jin.conf.title = jin.conf.title or ("jin v" .. jin.version()) -jin.conf.resizable = jin.conf.resizable or false -jin.conf.fullscreen = jin.conf.fullscreen or false -jin.conf.lockFPS = jin.conf.lockFPS or false +jin.config.width = jin.config.width or 576 +jin.config.height = jin.config.height or 448 +jin.config.vsync = jin.config.vsync or false +jin.config.title = jin.config.title or ("jin v" .. jin.version()) +jin.config.resizable = jin.config.resizable or false +jin.config.fullscreen = jin.config.fullscreen or false ------------------------------------------------------------------------- -- Initialize sub systems ------------------------------------------------------------------------- -jin.graphics.init(jin.conf) +jin.graphics.init(jin.config) jin.audio.init() ------------------------------------------------------------------------- @@ -53,7 +51,6 @@ function jin.core.run() local dt = 0 local previous = jin.time.second() local current = previous - -- TODO: 重写,事件处理事件应该高频率调用 while jin.core.running() do for _, e in pairs(jin.event.poll()) do if e.type == "keydown" then diff --git a/src/lua/modules/event/event.cpp b/src/lua/modules/event/event.cpp new file mode 100644 index 0000000..af9f8fc --- /dev/null +++ b/src/lua/modules/event/event.cpp @@ -0,0 +1,106 @@ +/** +* Event module +*/ +#include "lua/modules/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::input; + + /** + * Load event poll, return a iterator(a table). + */ + static int l_event_poll(lua_State *L) + { + // table to store events + luax_newtable(L); + static Event e; + int i = 1; + poll: + while (pollEvent(&e)) + { + // each event is a table + luax_newtable(L); + switch (e.type) + { + case EventType::QUIT: + luax_setfield_string(L, "type", "quit"); + break; + + case EventType::KEY_DOWN: + luax_setfield_string(L, "type", "keydown"); + luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); + break; + + case EventType::KEY_UP: + luax_setfield_string(L, "type", "keyup"); + luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); + break; + + case EventType::MOUSE_MOTION: + luax_setfield_string(L, "type", "mousemotion"); + luax_setfield_number(L, "x", e.motion.x); + luax_setfield_number(L, "y", e.motion.y); + break; + + case EventType::MOUSE_BUTTON_DOWN: + luax_setfield_string(L, "type", "mousebuttondown"); + luax_setfield_string(L, "button", getButtonName(e.button.button)); + luax_setfield_number(L, "x", e.button.x); + luax_setfield_number(L, "y", e.button.y); + break; + + case EventType::MOUSE_BUTTON_UP: + luax_setfield_string(L, "type", "mousebuttonup"); + luax_setfield_string(L, "button", getButtonName(e.button.button)); + luax_setfield_number(L, "x", e.button.x); + luax_setfield_number(L, "y", e.button.y); + break; + + case EventType::MOUSE_WHEEL: + luax_setfield_string(L, "type", "wheel"); + if(e.wheel.x == -1) + luax_setfield_string(L, "x", "left"); + else if(e.wheel.x == 1) + luax_setfield_string(L, "x", "right"); + else + luax_setfield_string(L, "x", "none"); + if (e.wheel.y == -1) + luax_setfield_string(L, "y", "near"); + else if (e.wheel.y == 1) + luax_setfield_string(L, "y", "far"); + else + luax_setfield_string(L, "y", "none"); + break; + + default: + /* ignore other events */ + luax_pop(L, 1); // pop table out + goto poll; + break; + } + luax_rawseti(L, -2, i++); + } + return 1; + } + + static const luaL_Reg f[] = { + { "poll", l_event_poll }, + { 0, 0 } + }; + + /** + * load event module + */ + int luaopen_event(lua_State* L) + { + luax_newlib(L, f); + return 1; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/event/luaopen_event.cpp b/src/lua/modules/event/luaopen_event.cpp deleted file mode 100644 index af9f8fc..0000000 --- a/src/lua/modules/event/luaopen_event.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/** -* Event module -*/ -#include "lua/modules/luax.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::input; - - /** - * Load event poll, return a iterator(a table). - */ - static int l_event_poll(lua_State *L) - { - // table to store events - luax_newtable(L); - static Event e; - int i = 1; - poll: - while (pollEvent(&e)) - { - // each event is a table - luax_newtable(L); - switch (e.type) - { - case EventType::QUIT: - luax_setfield_string(L, "type", "quit"); - break; - - case EventType::KEY_DOWN: - luax_setfield_string(L, "type", "keydown"); - luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); - break; - - case EventType::KEY_UP: - luax_setfield_string(L, "type", "keyup"); - luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); - break; - - case EventType::MOUSE_MOTION: - luax_setfield_string(L, "type", "mousemotion"); - luax_setfield_number(L, "x", e.motion.x); - luax_setfield_number(L, "y", e.motion.y); - break; - - case EventType::MOUSE_BUTTON_DOWN: - luax_setfield_string(L, "type", "mousebuttondown"); - luax_setfield_string(L, "button", getButtonName(e.button.button)); - luax_setfield_number(L, "x", e.button.x); - luax_setfield_number(L, "y", e.button.y); - break; - - case EventType::MOUSE_BUTTON_UP: - luax_setfield_string(L, "type", "mousebuttonup"); - luax_setfield_string(L, "button", getButtonName(e.button.button)); - luax_setfield_number(L, "x", e.button.x); - luax_setfield_number(L, "y", e.button.y); - break; - - case EventType::MOUSE_WHEEL: - luax_setfield_string(L, "type", "wheel"); - if(e.wheel.x == -1) - luax_setfield_string(L, "x", "left"); - else if(e.wheel.x == 1) - luax_setfield_string(L, "x", "right"); - else - luax_setfield_string(L, "x", "none"); - if (e.wheel.y == -1) - luax_setfield_string(L, "y", "near"); - else if (e.wheel.y == 1) - luax_setfield_string(L, "y", "far"); - else - luax_setfield_string(L, "y", "none"); - break; - - default: - /* ignore other events */ - luax_pop(L, 1); // pop table out - goto poll; - break; - } - luax_rawseti(L, -2, i++); - } - return 1; - } - - static const luaL_Reg f[] = { - { "poll", l_event_poll }, - { 0, 0 } - }; - - /** - * load event module - */ - int luaopen_event(lua_State* L) - { - luax_newlib(L, f); - return 1; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/filesystem/filesystem.cpp b/src/lua/modules/filesystem/filesystem.cpp new file mode 100644 index 0000000..55f4c06 --- /dev/null +++ b/src/lua/modules/filesystem/filesystem.cpp @@ -0,0 +1,137 @@ +#include "lua/modules/luax.h" +#include "libjin/jin.h" +#include + +using namespace jin::filesystem; + +namespace jin +{ +namespace lua +{ + + static struct + { + Filesystem* fs; + } context; + + static int l_init(lua_State* L) + { + context.fs = Filesystem::get(); + return 0; + } + + /** + * set current game root, like + * C:/jin/games/tank/ + */ + static int l_mount(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + context.fs->mount(path); + return 0; + } + + /** + * + */ + static int l_isDir(lua_State *L) + { + const char* path = luax_checkstring(L, 1); + int r = context.fs->isDir(path); + luax_pushboolean(L, r); + return 1; + } + + /** + * + */ + static int l_exist(lua_State * L) + { + const char* path = luax_checkstring(L, 1); + int r = context.fs->exists(path); + luax_pushboolean(L, r); + return 1; + } + + static int l_isdir(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + int r = context.fs->isDir(path); + luax_pushboolean(L, r); + return 1; + } + + // load but dont run it + static int loadf(lua_State* L) + { + const char* filename = lua_tostring(L, -1); + Buffer bf; + context.fs->read(filename, &bf); + luax_loadbuffer(L, (const char*)bf.data, bf.size, filename); + return 1; + } + + static int loader(lua_State* L) + { + const char * filename = lua_tostring(L, -1); + + std::string tmp(filename); + tmp += ".lua"; + + int size = tmp.size(); + + for (int i = 0; iexists(tmp.c_str())) + { + lua_pop(L, 1); + lua_pushstring(L, tmp.c_str()); + return loadf(L); + } + + tmp = filename; + size = tmp.size(); + for (int i = 0; iisDir(tmp.c_str())) + { + tmp += "/init.lua"; + if (context.fs->exists(tmp.c_str())) + { + lua_pop(L, 1); + lua_pushstring(L, tmp.c_str()); + return loadf(L); + } + } + + lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); + return 1; + } + + static const luaL_Reg f[] = { + { "init", l_init }, + { "mount", l_mount }, + { "isdir", l_isDir }, + { "exist", l_exist }, + { 0, 0 } + }; + + int luaopen_filesystem(lua_State* L) + { + luax_newlib(L, f); + luax_register_searcher(L, loader, 1); + return 0; + } + +} +} \ No newline at end of file diff --git a/src/lua/modules/filesystem/luaopen_filesystem.cpp b/src/lua/modules/filesystem/luaopen_filesystem.cpp deleted file mode 100644 index 55f4c06..0000000 --- a/src/lua/modules/filesystem/luaopen_filesystem.cpp +++ /dev/null @@ -1,137 +0,0 @@ -#include "lua/modules/luax.h" -#include "libjin/jin.h" -#include - -using namespace jin::filesystem; - -namespace jin -{ -namespace lua -{ - - static struct - { - Filesystem* fs; - } context; - - static int l_init(lua_State* L) - { - context.fs = Filesystem::get(); - return 0; - } - - /** - * set current game root, like - * C:/jin/games/tank/ - */ - static int l_mount(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - context.fs->mount(path); - return 0; - } - - /** - * - */ - static int l_isDir(lua_State *L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->isDir(path); - luax_pushboolean(L, r); - return 1; - } - - /** - * - */ - static int l_exist(lua_State * L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->exists(path); - luax_pushboolean(L, r); - return 1; - } - - static int l_isdir(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->isDir(path); - luax_pushboolean(L, r); - return 1; - } - - // load but dont run it - static int loadf(lua_State* L) - { - const char* filename = lua_tostring(L, -1); - Buffer bf; - context.fs->read(filename, &bf); - luax_loadbuffer(L, (const char*)bf.data, bf.size, filename); - return 1; - } - - static int loader(lua_State* L) - { - const char * filename = lua_tostring(L, -1); - - std::string tmp(filename); - tmp += ".lua"; - - int size = tmp.size(); - - for (int i = 0; iexists(tmp.c_str())) - { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - return loadf(L); - } - - tmp = filename; - size = tmp.size(); - for (int i = 0; iisDir(tmp.c_str())) - { - tmp += "/init.lua"; - if (context.fs->exists(tmp.c_str())) - { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - return loadf(L); - } - } - - lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); - return 1; - } - - static const luaL_Reg f[] = { - { "init", l_init }, - { "mount", l_mount }, - { "isdir", l_isDir }, - { "exist", l_exist }, - { 0, 0 } - }; - - int luaopen_filesystem(lua_State* L) - { - luax_newlib(L, f); - luax_register_searcher(L, loader, 1); - return 0; - } - -} -} \ No newline at end of file diff --git a/src/lua/modules/graphics/canvas.cpp b/src/lua/modules/graphics/canvas.cpp new file mode 100644 index 0000000..f42dfba --- /dev/null +++ b/src/lua/modules/graphics/canvas.cpp @@ -0,0 +1,75 @@ +#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& CanvasRef; + + static inline CanvasRef checkCanvas(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + return proxy->getRef(); + } + + static int l_getWidth(lua_State* L) + { + CanvasRef ref = checkCanvas(L); + luax_pushnumber(L, ref->getWidth()); + return 1; + } + + static int l_getHeight(lua_State* L) + { + CanvasRef ref = checkCanvas(L); + luax_pushnumber(L, ref->getHeight()); + return 1; + } + + static int l_getSize(lua_State* L) + { + CanvasRef ref = checkCanvas(L); + luax_pushnumber(L, ref->getWidth()); + luax_pushnumber(L, ref->getHeight()); + return 2; + } + + static int l_setAnchor(lua_State* L) + { + CanvasRef ref = checkCanvas(L); + int x = luax_checknumber(L, 1); + int y = luax_checknumber(L, 2); + ref->setAnchor(x, y); + return 0; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + proxy->release(); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { "setAnchor", l_setAnchor }, + { 0, 0 } + }; + + int luaopen_Canvas(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_CANVAS, f); + return 0; + } + +}// lua +}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/font.cpp b/src/lua/modules/graphics/font.cpp new file mode 100644 index 0000000..926f2cc --- /dev/null +++ b/src/lua/modules/graphics/font.cpp @@ -0,0 +1,49 @@ +#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; + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); + proxy->release(); + return 0; + } + + static int l_box(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Ref& ref = proxy->getRef(); + const char* text = luax_checkstring(L, 2); + int fheight = luax_checknumber(L, 3); + int spacing = luax_checknumber(L, 4); + int lheight = luax_checknumber(L, 5); + int w, h; + ref->box(text, fheight, lheight, spacing, &w, &h); + luax_pushnumber(L, w); + luax_pushnumber(L, h); + return 2; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "box", l_box }, + { 0, 0 } + }; + + int luaopen_Font(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_FONT, f); + + return 0; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp new file mode 100644 index 0000000..9c1d404 --- /dev/null +++ b/src/lua/modules/graphics/graphics.cpp @@ -0,0 +1,516 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "libjin/jin.h" +#include "lua/common/common.h" +#include "lua/modules/embed/graphics.lua.h" + +namespace jin +{ +namespace lua +{ + using namespace jin::graphics; + using jin::filesystem::Filesystem; + using jin::filesystem::Buffer; + + typedef Texture Image; + + static struct + { + color curRenderColor; + color curClearColor; + Font* curFont = nullptr; + Font* defaultFont = nullptr; + } context; + + static int l_init(lua_State* L) + { + Window* wnd = Window::get(); + Window::Setting 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"); + setting.fullscreen = luax_getfield_bool(L, 1, "fullscreen"); + setting.resizable= luax_getfield_bool(L, 1, "resizable"); + if (!wnd->init(&setting)) + { + luax_pushboolean(L, false); + return 1; + } + luax_pushboolean(L, true); + return 1; + } + + static int l_destroy(lua_State* L) + { + Window* wnd = Window::get(); + wnd->quit(); + return 0; + } + + static int l_getSize(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + luax_pushnumber(L, wnd->getH()); + return 2; + } + + static int l_getWidth(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + return 1; + } + + static int l_getHeight(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getH()); + return 1; + } + + static int l_newImage(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); + 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(img, JIN_GRAPHICS_IMAGE)); + return 1; + } + + static int l_newShader(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); + const char* program = luax_checkstring(L, 1); + JSLProgram* jsl = JSLProgram::createJSLProgram(program); + proxy->bind(new Ref(jsl, JIN_GRAPHICS_SHADER)); + return 1; + } + + static int l_newCanvas(lua_State* L) + { + int w = luax_checknumber(L, 1); + int h = luax_checknumber(L, 2); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy)); + Canvas* cvs = Canvas::createCanvas(w, h); + proxy->bind(new Ref(cvs, JIN_GRAPHICS_CANVAS)); + return 1; + } + + static int l_clear(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glClearColor(0, 0, 0, 1); + } + else + { + int r = luax_checknumber(L, 1); + int g = luax_checknumber(L, 2); + int b = luax_checknumber(L, 3); + int a = luax_checknumber(L, 4); + glClearColor(r / 255.f, g / 255.f, b / 255.f, a / 255.f); + } + glClear(GL_COLOR_BUFFER_BIT); + return 0; + } + + static int l_setClearColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glClearColor(1, 1, 1, 1); + 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); + + 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); + return 0; + } + + static int l_present(lua_State* L) + { + Window::get()->swapBuffers(); + return 0; + } + + static int l_draw(lua_State* L) + { + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + 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)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref& tex = proxy->getRef(); + tex->draw(x, y, sx, sy, r); + } + else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref& p = proxy->getRef(); + p->draw(x, y, sx, sy, r); + } + else + { + /* wrong type */ + luax_typerror(L, 1, "image or canvas"); + } + return 0; + } + + static int l_setColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glColor4f(1, 1, 1, 1); + 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.rgba.a = luax_checknumber(L, 4); + + 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); + return 0; + } + + static int l_getColor(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); + return 4; + } + + static int l_bindCanvas(lua_State* L) + { + if (luax_gettop(L) == 0) + { + // bind to default canvas + Canvas::unbind(); + return 0; + } + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + Ref& ref = proxy->getRef(); + ref->bind(); + return 0; + } + + static int l_unbindCanvas(lua_State* L) + { + Canvas::unbind(); + return 0; + } + + static int l_useShader(lua_State* L) + { + if (luax_gettop(L) == 0) + { + JSLProgram::unuse(); + return 0; + } + if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref& jsl = proxy->getRef(); + jsl->use(); + } + else + { + luax_typerror(L, 1, "JSL shader"); + } + return 0; + } + + static int l_unuseShader(lua_State* L) + { + JSLProgram::unuse(); + return 0; + } + + static int l_setBlend(lua_State* L) + { + + return 0; + } + + static RENDER_MODE strtomode(const char* str) + { + std::string s = std::string(str); + if (s == "fill") return RENDER_MODE::FILL; + else if (s == "line") return RENDER_MODE::LINE; + else return RENDER_MODE::NONE; + } + + static int l_drawpoint(lua_State* L) + { + int x = luax_checknumber(L, 1); + int y = luax_checknumber(L, 2); + jin::graphics::point(x, y); + + return 0; + } + + static int l_drawLine(lua_State* L) + { + int x1 = luax_checknumber(L, 1); + int y1 = luax_checknumber(L, 2); + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); + jin::graphics::line(x1, y1, x2, y2); + + return 0; + } + + static int l_drawRect(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + RENDER_MODE mode = strtomode(modestr); + if (mode != RENDER_MODE::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + int w = luax_checknumber(L, 4); + int h = luax_checknumber(L, 5); + rect(mode, x, y, w, h); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_drawCircle(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + RENDER_MODE mode = strtomode(modestr); + if (mode != RENDER_MODE::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + float r = luax_checknumber(L, 4); + circle(mode, x, y, r); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_drawTriangle(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + RENDER_MODE mode = strtomode(modestr); + if (mode != RENDER_MODE::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); + + int x3 = luax_checknumber(L, 5); + int y3 = luax_checknumber(L, 6); + + triangle(mode, x, y, x2, y2, x3, y3); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_drawPolygon(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + int n = luax_checknumber(L, 2); + RENDER_MODE mode = strtomode(modestr); + if (mode != RENDER_MODE::NONE) + { + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + int tn = luax_tableidxlen(L, 3); + if (tn != n * 2) + { + 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; + } + float* p = new float[2 * n]; + for (int i = 1; i <= 2 * n; ++i) + p[i - 1] = luax_rawgetnumber(L, 3, i); + polygon(mode, p, n); + delete[] p; + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_newFont(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Font* font = new Font(); + { + 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); + } + proxy->bind(new Ref(font, JIN_GRAPHICS_FONT)); + return 1; + } + + static int l_study(lua_State* L) + { + int n = luax_gettop(L); + if (n == 0) + { + if (context.defaultFont == 0) + { + #include "lua/resources/font.ttf.h" + // load default font + context.defaultFont = new Font(); + context.defaultFont->loadb(font_ttf); + } + context.curFont = context.defaultFont; + return 0; + } + Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); + context.curFont = font; + return 0; + } + + static int l_write(lua_State* L) + { + if (context.curFont == 0) + return 0; + + const char* text = luax_checkstring(L, 1); + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + + int fh = luax_optnumber(L, 4, 15); + int spacing = luax_optnumber(L, 5, 1); + int lh = luax_optnumber(L, 6, 18); + + context.curFont->render(text, x, y, fh, spacing, lh); + + return 0; + } + + static int l_box(lua_State* L) + { + const char* text = luax_checkstring(L, 1); + int fontheight = luax_checknumber(L, 2); + int spacing = luax_checknumber(L, 3); + int lineheight = luax_checknumber(L, 4); + int w, h; + context.curFont->box(text, fontheight, spacing, lineheight, &w, &h); + luax_pushnumber(L, w); + luax_pushnumber(L, h); + return 2; + } + + static const luaL_Reg f[] = { + { "init", l_init }, + { "getSize", l_getSize }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "newImage", l_newImage }, + { "newShader", l_newShader }, + { "newCanvas", l_newCanvas }, + { "newFont", l_newFont }, + { "box", l_box }, + { "write", l_write }, + { "setClearColor", l_setClearColor }, + { "clear", l_clear }, + { "draw", l_draw }, + { "setColor", l_setColor }, + { "palette", l_getColor }, + { "present", l_present }, + { "setFont", l_study }, + { "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 }, + { "destroy", l_destroy }, + { 0, 0 } + }; + + extern int luaopen_Image(lua_State* L); + extern int luaopen_Font(lua_State* L); + extern int luaopen_Canvas(lua_State* L); + extern int luaopen_JSL(lua_State* L); + + int luaopen_graphics(lua_State* L) + { + // register types + luaopen_Image(L); + luaopen_Canvas(L); + luaopen_Font(L); + luaopen_JSL(L); + + // load whole lib + luax_newlib(L, f); + + return 1; + } + +}// lua +}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/image.cpp b/src/lua/modules/graphics/image.cpp new file mode 100644 index 0000000..5824660 --- /dev/null +++ b/src/lua/modules/graphics/image.cpp @@ -0,0 +1,89 @@ +#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 Texture Image; + + static inline Ref& checkImage(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); + return proxy->getRef(); + } + + static int l_getWidth(lua_State* L) + { + Ref& ref = checkImage(L); + luax_pushnumber(L, ref->getWidth()); + return 1; + } + + static int l_getHeight(lua_State *L) + { + Ref& ref = checkImage(L); + luax_pushnumber(L, ref->getHeight()); + return 1; + } + + static int l_getPixel(lua_State* L) + { + Ref& ref = checkImage(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) + { + Ref& ref = checkImage(L); + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + ref->setAnchor(x, y); + return 0; + } + + static int l_getSize(lua_State* L) + { + Ref& ref = checkImage(L); + luax_pushnumber(L, ref->getWidth()); + luax_pushnumber(L, ref->getHeight()); + return 2; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); + proxy->release(); + return 0; + } + + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { "getPixel", l_getPixel }, + { "setAnchor", l_setAnchor }, + { 0, 0 } + }; + + int luaopen_Image(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_IMAGE, f); + return 0; + } + +}// graphics +}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/jsl.cpp b/src/lua/modules/graphics/jsl.cpp new file mode 100644 index 0000000..ccd9ebd --- /dev/null +++ b/src/lua/modules/graphics/jsl.cpp @@ -0,0 +1,146 @@ +#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 Texture Image; + + static inline Ref& checkJSLProgram(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + return proxy->getRef(); + } + + static enum VARIABLE_TYPE + { + INVALID = 0, + + NUMBER, + IMAGE, + CANVAS, + VEC2, + VEC3, + VEC4, + COLOR, + }; + + static VARIABLE_TYPE strToType(const char* str) + { + 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; + } + + /** + * Use send function send variables to JSL program. + */ + static int l_send(lua_State* L) + { + Ref& 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& tex = proxy->getRef(); + ref->sendTexture(variable, tex.getObject()); + break; + } + case CANVAS: + { + Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); + Ref& canvas = proxy->getRef(); + 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; + } + + 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 }, + { "send", l_send }, + { 0, 0 } + }; + + /** + * JSL program + */ + int luaopen_JSL(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_SHADER, f); + return 0; + } + +} +} \ No newline at end of file diff --git a/src/lua/modules/graphics/luaopen_Canvas.cpp b/src/lua/modules/graphics/luaopen_Canvas.cpp deleted file mode 100644 index f42dfba..0000000 --- a/src/lua/modules/graphics/luaopen_Canvas.cpp +++ /dev/null @@ -1,75 +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& CanvasRef; - - static inline CanvasRef checkCanvas(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - return proxy->getRef(); - } - - static int l_getWidth(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } - - static int l_getHeight(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } - - static int l_getSize(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getWidth()); - luax_pushnumber(L, ref->getHeight()); - return 2; - } - - static int l_setAnchor(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - ref->setAnchor(x, y); - return 0; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { "setAnchor", l_setAnchor }, - { 0, 0 } - }; - - int luaopen_Canvas(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_CANVAS, f); - return 0; - } - -}// lua -}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/luaopen_Font.cpp b/src/lua/modules/graphics/luaopen_Font.cpp deleted file mode 100644 index 926f2cc..0000000 --- a/src/lua/modules/graphics/luaopen_Font.cpp +++ /dev/null @@ -1,49 +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; - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); - proxy->release(); - return 0; - } - - static int l_box(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - Ref& ref = proxy->getRef(); - const char* text = luax_checkstring(L, 2); - int fheight = luax_checknumber(L, 3); - int spacing = luax_checknumber(L, 4); - int lheight = luax_checknumber(L, 5); - int w, h; - ref->box(text, fheight, lheight, spacing, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "box", l_box }, - { 0, 0 } - }; - - int luaopen_Font(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_FONT, f); - - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/graphics/luaopen_Image.cpp b/src/lua/modules/graphics/luaopen_Image.cpp deleted file mode 100644 index 5824660..0000000 --- a/src/lua/modules/graphics/luaopen_Image.cpp +++ /dev/null @@ -1,89 +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 Texture Image; - - static inline Ref& checkImage(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - return proxy->getRef(); - } - - static int l_getWidth(lua_State* L) - { - Ref& ref = checkImage(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } - - static int l_getHeight(lua_State *L) - { - Ref& ref = checkImage(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } - - static int l_getPixel(lua_State* L) - { - Ref& ref = checkImage(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) - { - Ref& ref = checkImage(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - ref->setAnchor(x, y); - return 0; - } - - static int l_getSize(lua_State* L) - { - Ref& ref = checkImage(L); - luax_pushnumber(L, ref->getWidth()); - luax_pushnumber(L, ref->getHeight()); - return 2; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_IMAGE); - proxy->release(); - return 0; - } - - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { "getPixel", l_getPixel }, - { "setAnchor", l_setAnchor }, - { 0, 0 } - }; - - int luaopen_Image(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_IMAGE, f); - return 0; - } - -}// graphics -}// jin \ No newline at end of file diff --git a/src/lua/modules/graphics/luaopen_JSL.cpp b/src/lua/modules/graphics/luaopen_JSL.cpp deleted file mode 100644 index ccd9ebd..0000000 --- a/src/lua/modules/graphics/luaopen_JSL.cpp +++ /dev/null @@ -1,146 +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 Texture Image; - - static inline Ref& checkJSLProgram(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef(); - } - - static enum VARIABLE_TYPE - { - INVALID = 0, - - NUMBER, - IMAGE, - CANVAS, - VEC2, - VEC3, - VEC4, - COLOR, - }; - - static VARIABLE_TYPE strToType(const char* str) - { - 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; - } - - /** - * Use send function send variables to JSL program. - */ - static int l_send(lua_State* L) - { - Ref& 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& tex = proxy->getRef(); - ref->sendTexture(variable, tex.getObject()); - break; - } - case CANVAS: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, JIN_GRAPHICS_CANVAS); - Ref& canvas = proxy->getRef(); - 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; - } - - 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 }, - { "send", l_send }, - { 0, 0 } - }; - - /** - * JSL program - */ - int luaopen_JSL(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_SHADER, f); - return 0; - } - -} -} \ No newline at end of file diff --git a/src/lua/modules/graphics/luaopen_graphics.cpp b/src/lua/modules/graphics/luaopen_graphics.cpp deleted file mode 100644 index 9c1d404..0000000 --- a/src/lua/modules/graphics/luaopen_graphics.cpp +++ /dev/null @@ -1,516 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "libjin/jin.h" -#include "lua/common/common.h" -#include "lua/modules/embed/graphics.lua.h" - -namespace jin -{ -namespace lua -{ - using namespace jin::graphics; - using jin::filesystem::Filesystem; - using jin::filesystem::Buffer; - - typedef Texture Image; - - static struct - { - color curRenderColor; - color curClearColor; - Font* curFont = nullptr; - Font* defaultFont = nullptr; - } context; - - static int l_init(lua_State* L) - { - Window* wnd = Window::get(); - Window::Setting 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"); - setting.fullscreen = luax_getfield_bool(L, 1, "fullscreen"); - setting.resizable= luax_getfield_bool(L, 1, "resizable"); - if (!wnd->init(&setting)) - { - luax_pushboolean(L, false); - return 1; - } - luax_pushboolean(L, true); - return 1; - } - - static int l_destroy(lua_State* L) - { - Window* wnd = Window::get(); - wnd->quit(); - return 0; - } - - static int l_getSize(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getW()); - luax_pushnumber(L, wnd->getH()); - return 2; - } - - static int l_getWidth(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getW()); - return 1; - } - - static int l_getHeight(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getH()); - return 1; - } - - static int l_newImage(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); - 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(img, JIN_GRAPHICS_IMAGE)); - return 1; - } - - static int l_newShader(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - const char* program = luax_checkstring(L, 1); - JSLProgram* jsl = JSLProgram::createJSLProgram(program); - proxy->bind(new Ref(jsl, JIN_GRAPHICS_SHADER)); - return 1; - } - - static int l_newCanvas(lua_State* L) - { - int w = luax_checknumber(L, 1); - int h = luax_checknumber(L, 2); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy)); - Canvas* cvs = Canvas::createCanvas(w, h); - proxy->bind(new Ref(cvs, JIN_GRAPHICS_CANVAS)); - return 1; - } - - static int l_clear(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(0, 0, 0, 1); - } - else - { - int r = luax_checknumber(L, 1); - int g = luax_checknumber(L, 2); - int b = luax_checknumber(L, 3); - int a = luax_checknumber(L, 4); - glClearColor(r / 255.f, g / 255.f, b / 255.f, a / 255.f); - } - glClear(GL_COLOR_BUFFER_BIT); - return 0; - } - - static int l_setClearColor(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(1, 1, 1, 1); - 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); - - 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); - return 0; - } - - static int l_present(lua_State* L) - { - Window::get()->swapBuffers(); - return 0; - } - - static int l_draw(lua_State* L) - { - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - 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)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& tex = proxy->getRef(); - tex->draw(x, y, sx, sy, r); - } - else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& p = proxy->getRef(); - p->draw(x, y, sx, sy, r); - } - else - { - /* wrong type */ - luax_typerror(L, 1, "image or canvas"); - } - return 0; - } - - static int l_setColor(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glColor4f(1, 1, 1, 1); - 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.rgba.a = luax_checknumber(L, 4); - - 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); - return 0; - } - - static int l_getColor(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); - return 4; - } - - static int l_bindCanvas(lua_State* L) - { - if (luax_gettop(L) == 0) - { - // bind to default canvas - Canvas::unbind(); - return 0; - } - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Ref& ref = proxy->getRef(); - ref->bind(); - return 0; - } - - static int l_unbindCanvas(lua_State* L) - { - Canvas::unbind(); - return 0; - } - - static int l_useShader(lua_State* L) - { - if (luax_gettop(L) == 0) - { - JSLProgram::unuse(); - return 0; - } - if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& jsl = proxy->getRef(); - jsl->use(); - } - else - { - luax_typerror(L, 1, "JSL shader"); - } - return 0; - } - - static int l_unuseShader(lua_State* L) - { - JSLProgram::unuse(); - return 0; - } - - static int l_setBlend(lua_State* L) - { - - return 0; - } - - static RENDER_MODE strtomode(const char* str) - { - std::string s = std::string(str); - if (s == "fill") return RENDER_MODE::FILL; - else if (s == "line") return RENDER_MODE::LINE; - else return RENDER_MODE::NONE; - } - - static int l_drawpoint(lua_State* L) - { - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - jin::graphics::point(x, y); - - return 0; - } - - static int l_drawLine(lua_State* L) - { - int x1 = luax_checknumber(L, 1); - int y1 = luax_checknumber(L, 2); - int x2 = luax_checknumber(L, 3); - int y2 = luax_checknumber(L, 4); - jin::graphics::line(x1, y1, x2, y2); - - return 0; - } - - static int l_drawRect(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - int w = luax_checknumber(L, 4); - int h = luax_checknumber(L, 5); - rect(mode, x, y, w, h); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_drawCircle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - float r = luax_checknumber(L, 4); - circle(mode, x, y, r); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_drawTriangle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int x2 = luax_checknumber(L, 3); - int y2 = luax_checknumber(L, 4); - - int x3 = luax_checknumber(L, 5); - int y3 = luax_checknumber(L, 6); - - triangle(mode, x, y, x2, y2, x3, y3); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_drawPolygon(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - int n = luax_checknumber(L, 2); - RENDER_MODE mode = strtomode(modestr); - if (mode != RENDER_MODE::NONE) - { - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - int tn = luax_tableidxlen(L, 3); - if (tn != n * 2) - { - 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; - } - float* p = new float[2 * n]; - for (int i = 1; i <= 2 * n; ++i) - p[i - 1] = luax_rawgetnumber(L, 3, i); - polygon(mode, p, n); - delete[] p; - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_newFont(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); - Font* font = new Font(); - { - 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); - } - proxy->bind(new Ref(font, JIN_GRAPHICS_FONT)); - return 1; - } - - static int l_study(lua_State* L) - { - int n = luax_gettop(L); - if (n == 0) - { - if (context.defaultFont == 0) - { - #include "lua/resources/font.ttf.h" - // load default font - context.defaultFont = new Font(); - context.defaultFont->loadb(font_ttf); - } - context.curFont = context.defaultFont; - return 0; - } - Font* font = (Font*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); - context.curFont = font; - return 0; - } - - static int l_write(lua_State* L) - { - if (context.curFont == 0) - return 0; - - const char* text = luax_checkstring(L, 1); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int fh = luax_optnumber(L, 4, 15); - int spacing = luax_optnumber(L, 5, 1); - int lh = luax_optnumber(L, 6, 18); - - context.curFont->render(text, x, y, fh, spacing, lh); - - return 0; - } - - static int l_box(lua_State* L) - { - const char* text = luax_checkstring(L, 1); - int fontheight = luax_checknumber(L, 2); - int spacing = luax_checknumber(L, 3); - int lineheight = luax_checknumber(L, 4); - int w, h; - context.curFont->box(text, fontheight, spacing, lineheight, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - - static const luaL_Reg f[] = { - { "init", l_init }, - { "getSize", l_getSize }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "newImage", l_newImage }, - { "newShader", l_newShader }, - { "newCanvas", l_newCanvas }, - { "newFont", l_newFont }, - { "box", l_box }, - { "write", l_write }, - { "setClearColor", l_setClearColor }, - { "clear", l_clear }, - { "draw", l_draw }, - { "setColor", l_setColor }, - { "palette", l_getColor }, - { "present", l_present }, - { "setFont", l_study }, - { "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 }, - { "destroy", l_destroy }, - { 0, 0 } - }; - - extern int luaopen_Image(lua_State* L); - extern int luaopen_Font(lua_State* L); - extern int luaopen_Canvas(lua_State* L); - extern int luaopen_JSL(lua_State* L); - - int luaopen_graphics(lua_State* L) - { - // register types - luaopen_Image(L); - luaopen_Canvas(L); - luaopen_Font(L); - luaopen_JSL(L); - - // load whole lib - luax_newlib(L, f); - - return 1; - } - -}// lua -}// jin \ No newline at end of file diff --git a/src/lua/modules/jin.cpp b/src/lua/modules/jin.cpp new file mode 100644 index 0000000..a25ec6c --- /dev/null +++ b/src/lua/modules/jin.cpp @@ -0,0 +1,104 @@ +#include "jin.h" +#include "lua/modules/luax.h" +#include "embed/embed.h" + +namespace jin +{ +namespace lua +{ + + extern int luaopen_core(lua_State* L); + extern int luaopen_graphics(lua_State* L); + extern int luaopen_audio(lua_State* L); + extern int luaopen_net(lua_State* L); + extern int luaopen_event(lua_State* L); + extern int luaopen_time(lua_State* L); + 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); + extern int luaopen_math(lua_State* L); + extern int luaopen_thread(lua_State* L); + extern int luaopen_bit(lua_State* L); + + static int l_getversion(lua_State* L) + { + luax_pushstring(L, VERSION); + return 1; + } + + static int l_getAuthor(lua_State* L) + { + luax_pushstring(L, AUTHOR); + return 1; + } + + static int l_getOS(lua_State* L) + { + #ifdef _WIN32 + luax_pushstring(L, "windows"); + #elif defined __unix__ + luax_pushstring(L, "unix"); + #elif defined __APPLE__ + luax_pushstring(L, "macos"); + #endif + return 1; + } + + static int l_revision(lua_State* L) + { + luax_pushnumber(L, REVISION); + return 1; + } + + static const luaL_Reg f[] = { + { "version", l_getversion }, + { "revision", l_revision }, + { "author", l_getAuthor }, + { "os", l_getOS }, + { 0, 0 } + }; + + // submodules + static const luaL_Reg mods[] = { + { "core", luaopen_core }, + { "event", luaopen_event }, + { "graphics", luaopen_graphics }, + { "time", luaopen_time }, + { "mouse", luaopen_mouse }, + { "keyboard", luaopen_keyboard }, + { "filesystem", luaopen_filesystem }, + { "net", luaopen_net }, + { "audio", luaopen_audio }, + { "joypad", luaopen_joypad }, + { "math", luaopen_math }, + { "thread", luaopen_thread }, + { "bit", luaopen_bit }, + { 0, 0 } + }; + + int luaopen_jin(lua_State* L) + { + // jin module is on top of the stack + luax_newlib(L, f); + + // set to global field + luax_justglobal(L, -1, MODULE_NAME); + + // register submodules + for (int i = 0; mods[i].name; ++i) + { + mods[i].func(L); + luax_setfield(L, -2, mods[i].name); + } + + return 1; + } + + void boot(lua_State* L) + { + jin::embed::boot(L); + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/jin.h b/src/lua/modules/jin.h new file mode 100644 index 0000000..07531b2 --- /dev/null +++ b/src/lua/modules/jin.h @@ -0,0 +1,42 @@ +/** +* Copyright (C) 2016~2018 chai +* +* Permission is hereby granted, free of charge, to any person obtaining a copyof +* this software and associated documentation files (the "Software"), to deal in +* the Software without restriction, including without limitation the rights to use, +* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the +* Software, and to permit persons to whom the Software is furnished to do so, +* subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS 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. +*/ + +#ifndef __JIN_M_JIN_H +#define __JIN_M_JIN_H +#include "luax.h" + +#define MODULE_NAME "jin" +#define VERSION "0.1.1" +#define REVISION 101 +#define AUTHOR "chai" + +namespace jin +{ +namespace lua +{ + + int luaopen_jin(lua_State* L); + void boot(lua_State* L); + +} // jin +} // lua + +#endif // __JIN_M_JIN_H \ No newline at end of file diff --git a/src/lua/modules/joypad/joypad.cpp b/src/lua/modules/joypad/joypad.cpp new file mode 100644 index 0000000..d305878 --- /dev/null +++ b/src/lua/modules/joypad/joypad.cpp @@ -0,0 +1,21 @@ +#include "libjin/jin.h" +#include "lua/modules/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/lua/modules/joypad/luaopen_joypad.cpp b/src/lua/modules/joypad/luaopen_joypad.cpp deleted file mode 100644 index d305878..0000000 --- a/src/lua/modules/joypad/luaopen_joypad.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "libjin/jin.h" -#include "lua/modules/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/lua/modules/keyboard/keyboard.cpp b/src/lua/modules/keyboard/keyboard.cpp new file mode 100644 index 0000000..9d27a4f --- /dev/null +++ b/src/lua/modules/keyboard/keyboard.cpp @@ -0,0 +1,16 @@ +#include "lua/modules/luax.h" +#include "lua/modules/embed/keyboard.lua.h" + +namespace jin +{ +namespace lua +{ + + int luaopen_keyboard(lua_State* L) + { + luax_newlib(L, 0); + return 1; + } + +} +} \ No newline at end of file diff --git a/src/lua/modules/keyboard/luaopen_keyboard.cpp b/src/lua/modules/keyboard/luaopen_keyboard.cpp deleted file mode 100644 index 9d27a4f..0000000 --- a/src/lua/modules/keyboard/luaopen_keyboard.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/embed/keyboard.lua.h" - -namespace jin -{ -namespace lua -{ - - int luaopen_keyboard(lua_State* L) - { - luax_newlib(L, 0); - return 1; - } - -} -} \ No newline at end of file diff --git a/src/lua/modules/luaopen_jin.cpp b/src/lua/modules/luaopen_jin.cpp deleted file mode 100644 index dbbac31..0000000 --- a/src/lua/modules/luaopen_jin.cpp +++ /dev/null @@ -1,104 +0,0 @@ -#include "luaopen_jin.h" -#include "lua/modules/luax.h" -#include "embed/embed.h" - -namespace jin -{ -namespace lua -{ - - extern int luaopen_core(lua_State* L); - extern int luaopen_graphics(lua_State* L); - extern int luaopen_audio(lua_State* L); - extern int luaopen_net(lua_State* L); - extern int luaopen_event(lua_State* L); - extern int luaopen_time(lua_State* L); - 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); - extern int luaopen_math(lua_State* L); - extern int luaopen_thread(lua_State* L); - extern int luaopen_bit(lua_State* L); - - static int l_getversion(lua_State* L) - { - luax_pushstring(L, VERSION); - return 1; - } - - static int l_getAuthor(lua_State* L) - { - luax_pushstring(L, AUTHOR); - return 1; - } - - static int l_getOS(lua_State* L) - { - #ifdef _WIN32 - luax_pushstring(L, "windows"); - #elif defined __unix__ - luax_pushstring(L, "unix"); - #elif defined __APPLE__ - luax_pushstring(L, "macos"); - #endif - return 1; - } - - static int l_revision(lua_State* L) - { - luax_pushnumber(L, REVISION); - return 1; - } - - static const luaL_Reg f[] = { - { "version", l_getversion }, - { "revision", l_revision }, - { "author", l_getAuthor }, - { "os", l_getOS }, - { 0, 0 } - }; - - // submodules - static const luaL_Reg mods[] = { - { "core", luaopen_core }, - { "event", luaopen_event }, - { "graphics", luaopen_graphics }, - { "time", luaopen_time }, - { "mouse", luaopen_mouse }, - { "keyboard", luaopen_keyboard }, - { "filesystem", luaopen_filesystem }, - { "net", luaopen_net }, - { "audio", luaopen_audio }, - { "joypad", luaopen_joypad }, - { "math", luaopen_math }, - { "thread", luaopen_thread }, - { "bit", luaopen_bit }, - { 0, 0 } - }; - - int luaopen_jin(lua_State* L) - { - // jin module is on top of the stack - luax_newlib(L, f); - - // set to global field - luax_justglobal(L, -1, MODULE_NAME); - - // register submodules - for (int i = 0; mods[i].name; ++i) - { - mods[i].func(L); - luax_setfield(L, -2, mods[i].name); - } - - return 1; - } - - void boot(lua_State* L) - { - jin::embed::boot(L); - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/luaopen_jin.h b/src/lua/modules/luaopen_jin.h deleted file mode 100644 index 07531b2..0000000 --- a/src/lua/modules/luaopen_jin.h +++ /dev/null @@ -1,42 +0,0 @@ -/** -* Copyright (C) 2016~2018 chai -* -* Permission is hereby granted, free of charge, to any person obtaining a copyof -* this software and associated documentation files (the "Software"), to deal in -* the Software without restriction, including without limitation the rights to use, -* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the -* Software, and to permit persons to whom the Software is furnished to do so, -* subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -* HOLDERS 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. -*/ - -#ifndef __JIN_M_JIN_H -#define __JIN_M_JIN_H -#include "luax.h" - -#define MODULE_NAME "jin" -#define VERSION "0.1.1" -#define REVISION 101 -#define AUTHOR "chai" - -namespace jin -{ -namespace lua -{ - - int luaopen_jin(lua_State* L); - void boot(lua_State* L); - -} // jin -} // lua - -#endif // __JIN_M_JIN_H \ No newline at end of file diff --git a/src/lua/modules/math/luaopen_math.cpp b/src/lua/modules/math/luaopen_math.cpp deleted file mode 100644 index fa59e04..0000000 --- a/src/lua/modules/math/luaopen_math.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "lua/modules/luax.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - static int l_mod(lua_State* L) - { - int n = luax_checkinteger(L, 1); - int m = luax_checkinteger(L, 2); - int mod = n % m; - luax_pushinteger(L, mod); - return 1; - } - - static const luaL_Reg f[] = { - { "mod", l_mod }, - { 0, 0 } - }; - - int luaopen_math(lua_State* L) - { - luax_newlib(L, f); - return 1; - } - -} -} \ No newline at end of file diff --git a/src/lua/modules/math/math.cpp b/src/lua/modules/math/math.cpp new file mode 100644 index 0000000..fa59e04 --- /dev/null +++ b/src/lua/modules/math/math.cpp @@ -0,0 +1,30 @@ +#include "lua/modules/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + static int l_mod(lua_State* L) + { + int n = luax_checkinteger(L, 1); + int m = luax_checkinteger(L, 2); + int mod = n % m; + luax_pushinteger(L, mod); + return 1; + } + + static const luaL_Reg f[] = { + { "mod", l_mod }, + { 0, 0 } + }; + + int luaopen_math(lua_State* L) + { + luax_newlib(L, f); + return 1; + } + +} +} \ No newline at end of file diff --git a/src/lua/modules/mouse/luaopen_mouse.cpp b/src/lua/modules/mouse/luaopen_mouse.cpp deleted file mode 100644 index f907abb..0000000 --- a/src/lua/modules/mouse/luaopen_mouse.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "lua/modules/luax.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - using namespace jin::input; - - static int l_pos(lua_State* L) - { - static Mouse* mouse = Mouse::get(); - int x, y; - mouse->getState(&x, &y); - luax_pushnumber(L, x); - luax_pushnumber(L, y); - return 2; - } - - static const luaL_Reg f[] = { - { "position", l_pos }, - { 0, 0 } - }; - - int luaopen_mouse(lua_State* L) - { - luax_newlib(L, f); - return 1; - } -} -} \ No newline at end of file diff --git a/src/lua/modules/mouse/mouse.cpp b/src/lua/modules/mouse/mouse.cpp new file mode 100644 index 0000000..f907abb --- /dev/null +++ b/src/lua/modules/mouse/mouse.cpp @@ -0,0 +1,31 @@ +#include "lua/modules/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + using namespace jin::input; + + static int l_pos(lua_State* L) + { + static Mouse* mouse = Mouse::get(); + int x, y; + mouse->getState(&x, &y); + luax_pushnumber(L, x); + luax_pushnumber(L, y); + return 2; + } + + static const luaL_Reg f[] = { + { "position", l_pos }, + { 0, 0 } + }; + + int luaopen_mouse(lua_State* L) + { + luax_newlib(L, f); + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/modules/net/Buffer.cpp b/src/lua/modules/net/Buffer.cpp new file mode 100644 index 0000000..3354518 --- /dev/null +++ b/src/lua/modules/net/Buffer.cpp @@ -0,0 +1,136 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" +#include "Buffer.h" + +namespace jin +{ +namespace lua +{ +namespace net +{ + + static inline Ref& checkNetBuffer(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + return proxy->getRef(); + } + + // net.Buffer:append(value) -> value_length + static int l_append(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + const int vp = 2; + if (luax_isintegerstrict(L, vp)) + { + int n = luax_checkinteger(L, vp); + int size = sizeof(n); + ref->append(&n, size); + luax_pushinteger(L, size); + return 1; + } + else if (luax_isfloatstrict(L, vp)) + { + float n = luax_checknumber(L, vp); + int size = sizeof(n); + ref->append(&n, size); + luax_pushinteger(L, size); + return 1; + } + else if (luax_isbooleanstrict(L, vp)) + { + bool n = luax_checkbool(L, vp); + int size = sizeof(n); + ref->append(&n, size); + luax_pushinteger(L, size); + return 1; + } + else if (luax_isstringstrict(L, vp)) + { + const char* str = luax_checkstring(L, vp); + int size = strlen(str) + 1; + ref->append(str, size); + luax_pushinteger(L, size); + return 1; + } + else + { + luax_typerror(L, vp, "number, bool or string"); + return 0; + } + } + + // net.Buffer:grabString(offset) -> string, length + static int l_grabString(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + const char* str = ref->grabString(&len, offset); + luax_pushstring(L, str); + luax_pushinteger(L, len); + return 2; + } + + // net.Buffer:grabInteger(offset) -> integer, length + static int l_grabInteger(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + int integer = ref->grabInteger(&len, offset); + luax_pushinteger(L, integer); + luax_pushinteger(L, len); + return 2; + } + + static int l_grabFloat(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + float floatv = ref->grabFloat(&len, offset); + luax_pushnumber(L, floatv); + luax_pushinteger(L, len); + return 2; + } + + static int l_grabBoolean(lua_State* L) + { + Ref& ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + int len; + bool boolean = ref->grabBoolean(&len, offset); + luax_pushboolean(L, boolean); + luax_pushinteger(L, len); + return 2; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + proxy->release(); + return 0; + } + + static const luaL_Reg netbuffer_function[] = { + { "__gc", l_gc }, + { "append", l_append }, + { "grabString", l_grabString }, + { "grabInteger", l_grabInteger }, + { "grabBoolean", l_grabBoolean }, + { "grabFloat", l_grabFloat }, + { 0, 0 } + }; + +} // net + + int luaopen_Buffer(lua_State* L) + { + luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function); + return 0; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/net/luaopen_Buffer.cpp b/src/lua/modules/net/luaopen_Buffer.cpp deleted file mode 100644 index 3354518..0000000 --- a/src/lua/modules/net/luaopen_Buffer.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" -#include "Buffer.h" - -namespace jin -{ -namespace lua -{ -namespace net -{ - - static inline Ref& checkNetBuffer(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - return proxy->getRef(); - } - - // net.Buffer:append(value) -> value_length - static int l_append(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - const int vp = 2; - if (luax_isintegerstrict(L, vp)) - { - int n = luax_checkinteger(L, vp); - int size = sizeof(n); - ref->append(&n, size); - luax_pushinteger(L, size); - return 1; - } - else if (luax_isfloatstrict(L, vp)) - { - float n = luax_checknumber(L, vp); - int size = sizeof(n); - ref->append(&n, size); - luax_pushinteger(L, size); - return 1; - } - else if (luax_isbooleanstrict(L, vp)) - { - bool n = luax_checkbool(L, vp); - int size = sizeof(n); - ref->append(&n, size); - luax_pushinteger(L, size); - return 1; - } - else if (luax_isstringstrict(L, vp)) - { - const char* str = luax_checkstring(L, vp); - int size = strlen(str) + 1; - ref->append(str, size); - luax_pushinteger(L, size); - return 1; - } - else - { - luax_typerror(L, vp, "number, bool or string"); - return 0; - } - } - - // net.Buffer:grabString(offset) -> string, length - static int l_grabString(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - const char* str = ref->grabString(&len, offset); - luax_pushstring(L, str); - luax_pushinteger(L, len); - return 2; - } - - // net.Buffer:grabInteger(offset) -> integer, length - static int l_grabInteger(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - int integer = ref->grabInteger(&len, offset); - luax_pushinteger(L, integer); - luax_pushinteger(L, len); - return 2; - } - - static int l_grabFloat(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - float floatv = ref->grabFloat(&len, offset); - luax_pushnumber(L, floatv); - luax_pushinteger(L, len); - return 2; - } - - static int l_grabBoolean(lua_State* L) - { - Ref& ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - int len; - bool boolean = ref->grabBoolean(&len, offset); - luax_pushboolean(L, boolean); - luax_pushinteger(L, len); - return 2; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - proxy->release(); - return 0; - } - - static const luaL_Reg netbuffer_function[] = { - { "__gc", l_gc }, - { "append", l_append }, - { "grabString", l_grabString }, - { "grabInteger", l_grabInteger }, - { "grabBoolean", l_grabBoolean }, - { "grabFloat", l_grabFloat }, - { 0, 0 } - }; - -} // net - - int luaopen_Buffer(lua_State* L) - { - luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function); - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/net/luaopen_Socket.cpp b/src/lua/modules/net/luaopen_Socket.cpp deleted file mode 100644 index 6d3fdfb..0000000 --- a/src/lua/modules/net/luaopen_Socket.cpp +++ /dev/null @@ -1,127 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "lua/common/common.h" -#include "libjin/jin.h" -#include "Buffer.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::net; - using namespace lua::net; - - const int BUFFER_SIZE = 1024; - - static inline Ref& checkSocket(lua_State* L, int pos = 1) - { - Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); - return proxy->getRef(); - } - - static inline Ref& checkNetBuffer(lua_State* L, int pos = 1) - { - Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); - return proxy->getRef(); - } - - // return net.Socket - static int l_accept(lua_State* L) - { - Ref& socket = checkSocket(L); - Socket* client = socket->accept(); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(new Ref(client, JIN_NETWORK_SOCKET)); - return 1; - } - - // return net.Buffer - static int l_receive(lua_State* L) - { - Ref& socket = checkSocket(L); - char buffer[BUFFER_SIZE] = {0}; - int size = socket->receive(buffer, BUFFER_SIZE); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); - net::Buffer* netBuffer = new net::Buffer(buffer, size); - proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); - return 1; - } - - // Socket:receiveFrom(address, port) - static int l_receiveFrom(lua_State* L) - { - Ref& socket = checkSocket(L); - int address = luax_checkinteger(L, 2); - int port = luax_checkinteger(L, 3); - char buffer[BUFFER_SIZE]; - int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port); - net::Buffer* netBuffer = new net::Buffer(buffer, size); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); - proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); - return 1; - } - - // Socket:send(net.Buffer) -> data_length - static int l_send(lua_State* L) - { - Ref& socket = checkSocket(L); - Ref& ref = checkNetBuffer(L, 2); - int len = socket->send(ref->buffer, ref->size); - luax_pushinteger(L, len); - return 1; - } - - // Socket:sendTo(address, port, net.Buffer) - static int l_sendTo(lua_State* L) - { - Ref& socket = checkSocket(L); - int address = luax_checkinteger(L, 2); - int port = luax_checkinteger(L, 3); - Ref& buffer = checkNetBuffer(L, 4); - socket->sendTo(buffer->buffer, buffer->size, address, port); - return 0; - } - - static int l_close(lua_State* L) - { - Ref& socket = checkSocket(L); - socket->close(); - return 0; - } - - static int l_configBlocking(lua_State* L) - { - Ref& socket = checkSocket(L); - bool blocking = luax_checkbool(L, 2); - socket->configureBlocking(blocking); - return 0; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); - proxy->release(); - return 0; - } - - static const luaL_Reg socket_function[] = { - { "__gc", l_gc }, - { "accept", l_accept }, - { "receive", l_receive }, - { "receiveFrom", l_receiveFrom }, - { "send", l_send }, - { "sendTo", l_sendTo }, - { "close", l_close }, - { "configBlocking", l_configBlocking }, - { 0, 0 } - }; - - int luaopen_Socket(lua_State* L) - { - luax_newtype(L, JIN_NETWORK_SOCKET, socket_function); - return 0; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/net/luaopen_net.cpp b/src/lua/modules/net/luaopen_net.cpp deleted file mode 100644 index a984920..0000000 --- a/src/lua/modules/net/luaopen_net.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "libjin/jin.h" -#include "lua/common/common.h" -#include "Buffer.h" - -namespace jin -{ -namespace lua -{ - using namespace jin::lua::net; - using namespace jin::net; - - static int l_initNetwork(lua_State* L) - { - jin::net::Net::get()->init(); - return 1; - } - - static int l_Socket(lua_State* L) - { - SocketInformation info = { 0 }; - { - const char* socketType = luax_checkstring(L, 1); - if (strcmp(socketType, "TCP") == 0) - info.type = SocketType::TCP; - else if (strcmp(socketType, "UDP") == 0) - info.type = SocketType::UDP; - else - { - luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP"); - return 0; - } - // type, port - if (luax_gettop(L) == 2) - { - info.port = luax_checkinteger(L, 2); - } - // type, address, port - else if (luax_gettop(L) == 3) - { - if (luax_isstringstrict(L, 2)) - info.address = tk_strtohl(luax_checkstring(L, 2)); - else if (luax_isintegerstrict(L, 2)) - info.address = luax_checkinteger(L, 2); - info.port = luax_checkinteger(L, 3); - } - } - Socket* socket = new Socket(info); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(new Ref(socket, JIN_NETWORK_SOCKET)); - return 1; - } - - static int l_Buffer(lua_State* L) - { - int size = luax_checkinteger(L, 1); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); - net::Buffer* buffer = new net::Buffer(size); - proxy->bind(new Ref(buffer, JIN_NETWORK_BUFFER)); - return 1; - } - - static const luaL_Reg f[] = { - { "init", l_initNetwork }, - { "newSocket", l_Socket }, - { "newBuffer", l_Buffer }, - { 0, 0 } - }; - - extern int luaopen_Socket(lua_State* L); - extern int luaopen_Buffer(lua_State* L); - - int luaopen_net(lua_State* L) - { - luaopen_Socket(L); - luaopen_Buffer(L); - - luax_newlib(L, f); - - return 1; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/net/net.cpp b/src/lua/modules/net/net.cpp new file mode 100644 index 0000000..a984920 --- /dev/null +++ b/src/lua/modules/net/net.cpp @@ -0,0 +1,85 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "libjin/jin.h" +#include "lua/common/common.h" +#include "Buffer.h" + +namespace jin +{ +namespace lua +{ + using namespace jin::lua::net; + using namespace jin::net; + + static int l_initNetwork(lua_State* L) + { + jin::net::Net::get()->init(); + return 1; + } + + static int l_Socket(lua_State* L) + { + SocketInformation info = { 0 }; + { + const char* socketType = luax_checkstring(L, 1); + if (strcmp(socketType, "TCP") == 0) + info.type = SocketType::TCP; + else if (strcmp(socketType, "UDP") == 0) + info.type = SocketType::UDP; + else + { + luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP"); + return 0; + } + // type, port + if (luax_gettop(L) == 2) + { + info.port = luax_checkinteger(L, 2); + } + // type, address, port + else if (luax_gettop(L) == 3) + { + if (luax_isstringstrict(L, 2)) + info.address = tk_strtohl(luax_checkstring(L, 2)); + else if (luax_isintegerstrict(L, 2)) + info.address = luax_checkinteger(L, 2); + info.port = luax_checkinteger(L, 3); + } + } + Socket* socket = new Socket(info); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref(socket, JIN_NETWORK_SOCKET)); + return 1; + } + + static int l_Buffer(lua_State* L) + { + int size = luax_checkinteger(L, 1); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + net::Buffer* buffer = new net::Buffer(size); + proxy->bind(new Ref(buffer, JIN_NETWORK_BUFFER)); + return 1; + } + + static const luaL_Reg f[] = { + { "init", l_initNetwork }, + { "newSocket", l_Socket }, + { "newBuffer", l_Buffer }, + { 0, 0 } + }; + + extern int luaopen_Socket(lua_State* L); + extern int luaopen_Buffer(lua_State* L); + + int luaopen_net(lua_State* L) + { + luaopen_Socket(L); + luaopen_Buffer(L); + + luax_newlib(L, f); + + return 1; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/net/socket.cpp b/src/lua/modules/net/socket.cpp new file mode 100644 index 0000000..6d3fdfb --- /dev/null +++ b/src/lua/modules/net/socket.cpp @@ -0,0 +1,127 @@ +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "lua/common/common.h" +#include "libjin/jin.h" +#include "Buffer.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::net; + using namespace lua::net; + + const int BUFFER_SIZE = 1024; + + static inline Ref& checkSocket(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); + return proxy->getRef(); + } + + static inline Ref& checkNetBuffer(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); + return proxy->getRef(); + } + + // return net.Socket + static int l_accept(lua_State* L) + { + Ref& socket = checkSocket(L); + Socket* client = socket->accept(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref(client, JIN_NETWORK_SOCKET)); + return 1; + } + + // return net.Buffer + static int l_receive(lua_State* L) + { + Ref& socket = checkSocket(L); + char buffer[BUFFER_SIZE] = {0}; + int size = socket->receive(buffer, BUFFER_SIZE); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + net::Buffer* netBuffer = new net::Buffer(buffer, size); + proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } + + // Socket:receiveFrom(address, port) + static int l_receiveFrom(lua_State* L) + { + Ref& socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + char buffer[BUFFER_SIZE]; + int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port); + net::Buffer* netBuffer = new net::Buffer(buffer, size); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } + + // Socket:send(net.Buffer) -> data_length + static int l_send(lua_State* L) + { + Ref& socket = checkSocket(L); + Ref& ref = checkNetBuffer(L, 2); + int len = socket->send(ref->buffer, ref->size); + luax_pushinteger(L, len); + return 1; + } + + // Socket:sendTo(address, port, net.Buffer) + static int l_sendTo(lua_State* L) + { + Ref& socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + Ref& buffer = checkNetBuffer(L, 4); + socket->sendTo(buffer->buffer, buffer->size, address, port); + return 0; + } + + static int l_close(lua_State* L) + { + Ref& socket = checkSocket(L); + socket->close(); + return 0; + } + + static int l_configBlocking(lua_State* L) + { + Ref& socket = checkSocket(L); + bool blocking = luax_checkbool(L, 2); + socket->configureBlocking(blocking); + return 0; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET); + proxy->release(); + return 0; + } + + static const luaL_Reg socket_function[] = { + { "__gc", l_gc }, + { "accept", l_accept }, + { "receive", l_receive }, + { "receiveFrom", l_receiveFrom }, + { "send", l_send }, + { "sendTo", l_sendTo }, + { "close", l_close }, + { "configBlocking", l_configBlocking }, + { 0, 0 } + }; + + int luaopen_Socket(lua_State* L) + { + luax_newtype(L, JIN_NETWORK_SOCKET, socket_function); + return 0; + } + +} // lua +} // jin \ No newline at end of file diff --git a/src/lua/modules/thread/Thread.cpp b/src/lua/modules/thread/Thread.cpp index 8df3255..5c9cb1f 100644 --- a/src/lua/modules/thread/Thread.cpp +++ b/src/lua/modules/thread/Thread.cpp @@ -1,14 +1,249 @@ -#include "Thread.h" +#include "lua/modules/luax.h" +#include "lua/modules/types.h" +#include "libjin/jin.h" +#include "lua/modules/jin.h" +#include "lua/common/common.h" +#include "thread.h" namespace jin { namespace lua { -namespace thread -{ + using thread::Thread; + + int luaopen_thread(lua_State* L); + + static inline Ref& checkThread(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + return proxy->getRef(); + } + + static int threadRunner(void* t) + { + Ref& ref = *(Ref*)t; + lua_State* L = lua_open(); + luax_openlibs(L); + luaopen_jin(L); + luax_getglobal(L, MODULE_NAME); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + ref.retain(); + proxy->bind(&ref); + luax_setfield(L, -2, "_curThread"); + luax_dostring(L, ref->code.c_str()); + luax_close(L); + return 0; + } + + static int l_thread_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); + return 0; + } + + static int l_start(lua_State* L) + { + Ref& ref = checkThread(L); + bool result = ref->start(&ref); + luax_pushboolean(L, result); + return 1; + } + + static int l_wait(lua_State* L) + { + Ref& ref = checkThread(L); + ref->wait(); + return 0; + } + + static int l_send(lua_State* L) + { + Ref& ref = checkThread(L); + int slot = luax_checkinteger(L, 2); + const int vp = 3; + if (luax_isnumberstrict(L, vp)) + { + float real = luax_checknumber(L, vp); + ref->send(slot, real); + } + else if (luax_isbooleanstrict(L, vp)) + { + bool bol = luax_checkbool(L, vp); + ref->send(slot, bol); + } + else if (luax_isstringstrict(L, vp)) + { + const char* str = luax_checkstring(L, vp); + ref->send(slot, str); + } + else if (luax_isuserdata(L, vp)) + { + void* p = luax_touserdata(L, vp); + ref->send(slot, p); + } + else if (luax_islightuserdata(L, vp)) + { + void* p = luax_tolightuserdata(L, vp); + ref->send(slot, p); + } + return 0; + } + + static int l_receive(lua_State* L) + { + Ref& ref = checkThread(L); + int slot = luax_checkinteger(L, 2); + bool result = ref->receive(slot); + luax_pushboolean(L, result); + return 1; + } + + static int l_fetch(lua_State* L) + { + Ref& ref = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = ref->fetch(slot); + switch (v.type) + { + case thread::Thread::Variant::INTERGER: + luax_pushinteger(L, v.integer); + break; + + case thread::Thread::Variant::BOOLEAN: + luax_pushboolean(L, v.boolean); + break; + + case thread::Thread::Variant::CSTRING: + luax_pushstring(L, v.cstring); + break; + + case thread::Thread::Variant::REAL: + luax_pushnumber(L, v.real); + break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy)); + p->reference->retain(); + proxy->bind(p->reference); + break; + + } + return 1; + } + + static int l_demand(lua_State* L) + { + Ref& ref = checkThread(L); + int slot = luax_checkinteger(L, 2); + Thread::Variant v = ref->demand(slot); + switch (v.type) + { + case thread::Thread::Variant::INTERGER: + luax_pushinteger(L, v.integer); + break; + + case thread::Thread::Variant::BOOLEAN: + luax_pushboolean(L, v.boolean); + break; + + case thread::Thread::Variant::CSTRING: + luax_pushstring(L, v.cstring); + break; + + case thread::Thread::Variant::REAL: + luax_pushnumber(L, v.real); + break; + + case thread::Thread::Variant::POINTER: + Proxy* p = (Proxy*)v.pointer; + Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy)); + p->reference->retain(); + proxy->bind(p->reference); + break; + + } + return 1; + } + + static int l_remove(lua_State* L) + { + Ref& ref = checkThread(L); + int slot = luax_checkinteger(L, 2); + ref->remove(slot); + return 0; + } + + static int l_getName(lua_State* L) + { + Ref& ref = checkThread(L); + const char* name = ref->getName(); + luax_pushstring(L, name); + return 1; + } + + static int l_isRunning(lua_State* L) + { + Ref& ref = checkThread(L); + bool running = ref->isRunning(); + luax_pushboolean(L, running); + return 1; + } + + static const luaL_Reg thread_function[] = { + { "__gc", l_thread_gc }, + { "start", l_start }, + { "wait", l_wait }, + { "send", l_send }, + { "receive", l_receive }, + { "fetch", l_fetch }, + { "demand", l_demand }, + { "remove", l_remove }, + { "getName", l_getName }, + { "isRunning", l_isRunning }, + { 0, 0 } + }; + + static int luaopen_Thread(lua_State* L) + { + luax_newtype(L, JIN_THREAD_THREAD, thread_function); + + return 0; + } + + static int l_newThread(lua_State* L) + { + const char* name = luax_checkstring(L, 1); + const char* code = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + Thread* thread = new Thread(name, code, threadRunner); + proxy->bind(new Ref(thread, JIN_THREAD_THREAD)); + return 1; + } + + static int l_getThread(lua_State* L) + { + luax_getglobal(L, MODULE_NAME); + luax_getfield(L, -1, "_curThread"); + return 1; + } + + static const luaL_Reg f[] = { + { "newThread", l_newThread }, + { "getThread", l_getThread }, + { 0, 0 } + }; + + int luaopen_thread(lua_State* L) + { + luaopen_Thread(L); + + luax_newlib(L, f); + return 1; + } -} // thread } // lua } // jin \ No newline at end of file diff --git a/src/lua/modules/thread/luaopen_thread.cpp b/src/lua/modules/thread/luaopen_thread.cpp deleted file mode 100644 index d581f54..0000000 --- a/src/lua/modules/thread/luaopen_thread.cpp +++ /dev/null @@ -1,249 +0,0 @@ -#include "lua/modules/luax.h" -#include "lua/modules/types.h" -#include "libjin/jin.h" -#include "lua/modules/luaopen_jin.h" -#include "lua/common/common.h" -#include "Thread.h" - -namespace jin -{ -namespace lua -{ - - using thread::Thread; - - int luaopen_thread(lua_State* L); - - static inline Ref& checkThread(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - return proxy->getRef(); - } - - static int threadRunner(void* t) - { - Ref& ref = *(Ref*)t; - lua_State* L = lua_open(); - luax_openlibs(L); - luaopen_jin(L); - luax_getglobal(L, MODULE_NAME); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); - ref.retain(); - proxy->bind(&ref); - luax_setfield(L, -2, "_curThread"); - luax_dostring(L, ref->code.c_str()); - luax_close(L); - return 0; - } - - static int l_thread_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - proxy->release(); - return 0; - } - - static int l_start(lua_State* L) - { - Ref& ref = checkThread(L); - bool result = ref->start(&ref); - luax_pushboolean(L, result); - return 1; - } - - static int l_wait(lua_State* L) - { - Ref& ref = checkThread(L); - ref->wait(); - return 0; - } - - static int l_send(lua_State* L) - { - Ref& ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - const int vp = 3; - if (luax_isnumberstrict(L, vp)) - { - float real = luax_checknumber(L, vp); - ref->send(slot, real); - } - else if (luax_isbooleanstrict(L, vp)) - { - bool bol = luax_checkbool(L, vp); - ref->send(slot, bol); - } - else if (luax_isstringstrict(L, vp)) - { - const char* str = luax_checkstring(L, vp); - ref->send(slot, str); - } - else if (luax_isuserdata(L, vp)) - { - void* p = luax_touserdata(L, vp); - ref->send(slot, p); - } - else if (luax_islightuserdata(L, vp)) - { - void* p = luax_tolightuserdata(L, vp); - ref->send(slot, p); - } - return 0; - } - - static int l_receive(lua_State* L) - { - Ref& ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - bool result = ref->receive(slot); - luax_pushboolean(L, result); - return 1; - } - - static int l_fetch(lua_State* L) - { - Ref& ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = ref->fetch(slot); - switch (v.type) - { - case thread::Thread::Variant::INTERGER: - luax_pushinteger(L, v.integer); - break; - - case thread::Thread::Variant::BOOLEAN: - luax_pushboolean(L, v.boolean); - break; - - case thread::Thread::Variant::CSTRING: - luax_pushstring(L, v.cstring); - break; - - case thread::Thread::Variant::REAL: - luax_pushnumber(L, v.real); - break; - - case thread::Thread::Variant::POINTER: - Proxy* p = (Proxy*)v.pointer; - Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy)); - p->reference->retain(); - proxy->bind(p->reference); - break; - - } - return 1; - } - - static int l_demand(lua_State* L) - { - Ref& ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = ref->demand(slot); - switch (v.type) - { - case thread::Thread::Variant::INTERGER: - luax_pushinteger(L, v.integer); - break; - - case thread::Thread::Variant::BOOLEAN: - luax_pushboolean(L, v.boolean); - break; - - case thread::Thread::Variant::CSTRING: - luax_pushstring(L, v.cstring); - break; - - case thread::Thread::Variant::REAL: - luax_pushnumber(L, v.real); - break; - - case thread::Thread::Variant::POINTER: - Proxy* p = (Proxy*)v.pointer; - Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy)); - p->reference->retain(); - proxy->bind(p->reference); - break; - - } - return 1; - } - - static int l_remove(lua_State* L) - { - Ref& ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - ref->remove(slot); - return 0; - } - - static int l_getName(lua_State* L) - { - Ref& ref = checkThread(L); - const char* name = ref->getName(); - luax_pushstring(L, name); - return 1; - } - - static int l_isRunning(lua_State* L) - { - Ref& ref = checkThread(L); - bool running = ref->isRunning(); - luax_pushboolean(L, running); - return 1; - } - - static const luaL_Reg thread_function[] = { - { "__gc", l_thread_gc }, - { "start", l_start }, - { "wait", l_wait }, - { "send", l_send }, - { "receive", l_receive }, - { "fetch", l_fetch }, - { "demand", l_demand }, - { "remove", l_remove }, - { "getName", l_getName }, - { "isRunning", l_isRunning }, - { 0, 0 } - }; - - static int luaopen_Thread(lua_State* L) - { - luax_newtype(L, JIN_THREAD_THREAD, thread_function); - - return 0; - } - - static int l_newThread(lua_State* L) - { - const char* name = luax_checkstring(L, 1); - const char* code = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); - Thread* thread = new Thread(name, code, threadRunner); - proxy->bind(new Ref(thread, JIN_THREAD_THREAD)); - return 1; - } - - static int l_getThread(lua_State* L) - { - luax_getglobal(L, MODULE_NAME); - luax_getfield(L, -1, "_curThread"); - return 1; - } - - static const luaL_Reg f[] = { - { "newThread", l_newThread }, - { "getThread", l_getThread }, - { 0, 0 } - }; - - int luaopen_thread(lua_State* L) - { - luaopen_Thread(L); - - luax_newlib(L, f); - - return 1; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/time/luaopen_time.cpp b/src/lua/modules/time/luaopen_time.cpp deleted file mode 100644 index 39743b4..0000000 --- a/src/lua/modules/time/luaopen_time.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "lua/modules/luax.h" -#include -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::time; - - static int l_sec(lua_State* L) - { - luax_pushnumber(L, getSecond()); - return 1; - } - - static int l_sleep(lua_State* L) - { - double sec = luax_checknumber(L, 1); - sleep(sec * 1000.0f); - return 0; - } - - static const luaL_Reg f[] = { - { "second", l_sec }, - { "sleep", l_sleep }, - { 0, 0 }, - }; - - int luaopen_time(lua_State* L) - { - luax_newlib(L, f); - return 1; - } - -} // lua -} // jin \ No newline at end of file diff --git a/src/lua/modules/time/time.cpp b/src/lua/modules/time/time.cpp new file mode 100644 index 0000000..39743b4 --- /dev/null +++ b/src/lua/modules/time/time.cpp @@ -0,0 +1,38 @@ +#include "lua/modules/luax.h" +#include +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::time; + + static int l_sec(lua_State* L) + { + luax_pushnumber(L, getSecond()); + return 1; + } + + static int l_sleep(lua_State* L) + { + double sec = luax_checknumber(L, 1); + sleep(sec * 1000.0f); + return 0; + } + + static const luaL_Reg f[] = { + { "second", l_sec }, + { "sleep", l_sleep }, + { 0, 0 }, + }; + + int luaopen_time(lua_State* L) + { + luax_newlib(L, f); + return 1; + } + +} // lua +} // jin \ No newline at end of file -- cgit v1.1-26-g67d0