diff options
author | chai <chaifix@163.com> | 2018-10-20 16:18:11 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-10-20 16:18:11 +0800 |
commit | cf68d9f46da1cf20503a7d738da1f43de916004d (patch) | |
tree | 5577e44d926deadb15ef5ca145e705eae14636fe | |
parent | e9410bd371fc68169c57d2c52382948a0b083da4 (diff) |
*格式化代码
47 files changed, 2528 insertions, 2521 deletions
diff --git a/bin/Jin.exe b/bin/Jin.exe Binary files differindex f1284dd..b44458d 100644 --- a/bin/Jin.exe +++ b/bin/Jin.exe diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex f1284dd..b44458d 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/src/libjin/Filesystem/je_asset_database.h b/src/libjin/Filesystem/je_asset_database.h index ef16263..b8f96f6 100644 --- a/src/libjin/Filesystem/je_asset_database.h +++ b/src/libjin/Filesystem/je_asset_database.h @@ -1,3 +1,6 @@ +#ifndef __JE_ASSET_DATABASE_H +#define __JE_ASSET_DATABASE_H + #include "je_buffer.h" #include "../3rdparty/smount/smount.h" @@ -28,4 +31,6 @@ namespace JinEngine }; } // namespace Filesystem -} // namespace JinEngine
\ No newline at end of file +} // namespace JinEngine + +#endif
\ No newline at end of file diff --git a/src/libjin/Graphics/Shader/je_base.shader.h b/src/libjin/Graphics/Shader/je_base.shader.h index 0616751..d6f9d7b 100644 --- a/src/libjin/Graphics/Shader/je_base.shader.h +++ b/src/libjin/Graphics/Shader/je_base.shader.h @@ -85,4 +85,4 @@ static const char* SHADER_MAIN_TEXTURE = "jin_MainTexture"; static const char* SHADER_VERTEX_COORDS = "jin_VertexCoords"; static const char* SHADER_TEXTURE_COORDS = "jin_TextureCoords"; -#endif
\ No newline at end of file +#endif // __JE_BASE_SHADER_H
\ No newline at end of file diff --git a/src/libjin/Graphics/je_bitmap.cpp b/src/libjin/Graphics/je_bitmap.cpp index c284d2a..5269606 100644 --- a/src/libjin/Graphics/je_bitmap.cpp +++ b/src/libjin/Graphics/je_bitmap.cpp @@ -147,5 +147,5 @@ namespace JinEngine return pixels; } - } -}
\ No newline at end of file + } // namespace Graphics +} // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/Graphics/je_bitmap.h b/src/libjin/Graphics/je_bitmap.h index eeb065e..8adc326 100644 --- a/src/libjin/Graphics/je_bitmap.h +++ b/src/libjin/Graphics/je_bitmap.h @@ -170,4 +170,5 @@ namespace JinEngine } // namespace JinEngine #endif + #endif
\ No newline at end of file diff --git a/src/libjin/Graphics/je_color.h b/src/libjin/Graphics/je_color.h index 01484ff..a3c7582 100644 --- a/src/libjin/Graphics/je_color.h +++ b/src/libjin/Graphics/je_color.h @@ -4,7 +4,7 @@ #ifndef __JE_COLOR_H #define __JE_COLOR_H #include "../core/je_configuration.h" -#if LIBJIN_MODULES_RENDER +#if defined(jin_graphics) #include "../utils/je_endian.h" @@ -18,8 +18,8 @@ namespace JinEngine class Color { public: - /* Built-in Colors */ - static const Color WHITE; + // Built-in colors + static const Color WHITE; static const Color BLACK; static const Color RED; static const Color GREEN; @@ -81,5 +81,6 @@ namespace JinEngine } // namespace Graphics } // namespace JinEngine -#endif // LIBJIN_MODULES_RENDER +#endif // jin_graphics + #endif // __JE_COLOR_H
\ No newline at end of file diff --git a/src/libjin/Graphics/je_drawable.h b/src/libjin/Graphics/je_drawable.h index 28fd28b..2547373 100644 --- a/src/libjin/Graphics/je_drawable.h +++ b/src/libjin/Graphics/je_drawable.h @@ -83,7 +83,7 @@ namespace JinEngine }; - } // namespace render + } // namespace Graphics } // namespace JinEngine #endif // LIBJIN_MODULES_RENDER diff --git a/src/libjin/Graphics/je_gl.cpp b/src/libjin/Graphics/je_gl.cpp index f27f7ba..9ef1460 100644 --- a/src/libjin/Graphics/je_gl.cpp +++ b/src/libjin/Graphics/je_gl.cpp @@ -8,5 +8,5 @@ namespace JinEngine OpenGL gl; - } -} + } // namespace Graphics +} // namespace JinEngine diff --git a/src/libjin/Graphics/je_gl.h b/src/libjin/Graphics/je_gl.h index e0bc81f..703fdc7 100644 --- a/src/libjin/Graphics/je_gl.h +++ b/src/libjin/Graphics/je_gl.h @@ -24,7 +24,7 @@ namespace JinEngine extern OpenGL gl; - } -} + } // namespace Graphics +} // namespace JinEngine #endif
\ No newline at end of file diff --git a/src/libjin/Graphics/je_image.cpp b/src/libjin/Graphics/je_image.cpp index 9c6f2ad..a905943 100644 --- a/src/libjin/Graphics/je_image.cpp +++ b/src/libjin/Graphics/je_image.cpp @@ -43,4 +43,4 @@ namespace JinEngine } } // namespace Graphics -} // namespace JinEngine +} // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/Graphics/je_mesh.cpp b/src/libjin/Graphics/je_mesh.cpp index de810e7..4ef5a6e 100644 --- a/src/libjin/Graphics/je_mesh.cpp +++ b/src/libjin/Graphics/je_mesh.cpp @@ -7,5 +7,5 @@ namespace JinEngine - } -}
\ No newline at end of file + } // namespace Graphics +} // namespace JinEngine
\ No newline at end of file diff --git a/src/libjin/Graphics/je_mesh.h b/src/libjin/Graphics/je_mesh.h index 29c8916..854b701 100644 --- a/src/libjin/Graphics/je_mesh.h +++ b/src/libjin/Graphics/je_mesh.h @@ -15,7 +15,7 @@ namespace JinEngine }; - } -} + } // namespace Graphics +} // namespace JinEngine #endif
\ No newline at end of file diff --git a/src/lua/common/Proxy.h b/src/lua/common/Proxy.h index 643dd48..1fa72e4 100644 --- a/src/lua/common/Proxy.h +++ b/src/lua/common/Proxy.h @@ -3,9 +3,9 @@ #include "Reference.hpp" -namespace jin +namespace JinEngine { -namespace lua +namespace Lua { class Proxy @@ -61,7 +61,7 @@ namespace lua }; -} // lua -} // jin +} // Lua +} // JinEngine #endif // __JIN_COMMON_PROXY_H
\ No newline at end of file diff --git a/src/lua/common/Reference.hpp b/src/lua/common/Reference.hpp index bd9dc24..618456d 100644 --- a/src/lua/common/Reference.hpp +++ b/src/lua/common/Reference.hpp @@ -1,9 +1,9 @@ #ifndef __JIN_COMMON_REFERENCE_H #define __JIN_COMMON_REFERENCE_H -namespace jin +namespace JinEngine { -namespace lua +namespace Lua { /*abstract*/class RefBase diff --git a/src/lua/common/error.h b/src/lua/common/error.h index 1d10faf..c254486 100644 --- a/src/lua/common/error.h +++ b/src/lua/common/error.h @@ -4,9 +4,9 @@ #include "../jin.h" #include <string.h> -namespace jin +namespace JinEngine { -namespace lua +namespace Lua { static const int FORMAT_MSG_BUFFER_SIZE = 2048; diff --git a/src/lua/embed/embed.h b/src/lua/embed/embed.h index 8d0ba85..cde2ba4 100644 --- a/src/lua/embed/embed.h +++ b/src/lua/embed/embed.h @@ -2,7 +2,7 @@ #define __JIN_LUA_EMBED_H #include <cstring> -namespace jin +namespace JinEngine { namespace embed { @@ -41,6 +41,6 @@ namespace embed } } // embed -} // jin +} // JinEngine #endif
\ No newline at end of file diff --git a/src/lua/jin.cpp b/src/lua/jin.cpp index e6d0a2a..fc1e443 100644 --- a/src/lua/jin.cpp +++ b/src/lua/jin.cpp @@ -2,9 +2,9 @@ #include "lua/modules/luax.h" #include "embed/embed.h" -namespace jin +namespace JinEngine { -namespace lua +namespace Lua { extern int luaopen_core(lua_State* L); @@ -102,8 +102,8 @@ namespace lua void boot(lua_State* L) { - jin::embed::boot(L); + JinEngine::embed::boot(L); } -} // lua -} // jin
\ No newline at end of file +} // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/jin.h b/src/lua/jin.h index fe8d6dd..d88eed5 100644 --- a/src/lua/jin.h +++ b/src/lua/jin.h @@ -13,15 +13,15 @@ #define REVISION 101 #define AUTHOR "chai" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - int luaopen_jin(lua_State* L); - void boot(lua_State* L); + int luaopen_jin(lua_State* L); + void boot(lua_State* L); -} // jin -} // lua + } // JinEngine +} // Lua #endif // __JIN_M_JIN_H
\ No newline at end of file diff --git a/src/lua/main.cpp b/src/lua/main.cpp index 1010b47..a17ed1c 100644 --- a/src/lua/main.cpp +++ b/src/lua/main.cpp @@ -8,7 +8,7 @@ #include "libjin/jin.h" #include <Windows.h> -using namespace jin::lua; +using namespace JinEngine::Lua; using namespace JinEngine::Filesystem; int main(int argc, char* argv[]) diff --git a/src/lua/modules/audio/audio.cpp b/src/lua/modules/audio/audio.cpp index becde12..8d8bd22 100644 --- a/src/lua/modules/audio/audio.cpp +++ b/src/lua/modules/audio/audio.cpp @@ -3,121 +3,121 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin +namespace JinEngine { -namespace lua -{ + namespace Lua + { - using namespace JinEngine::Audio; - using namespace JinEngine::Filesystem; + using namespace JinEngine::Audio; + using namespace JinEngine::Filesystem; - typedef SDLAudio Audio; - typedef SDLSource Source; + 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)) + static int l_init(lua_State* L) { - luax_error(L, "could not init audio"); - luax_pushboolean(L, false); + 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; } - 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) - { - AssetDatabase* fs = AssetDatabase::get(); - const char* f = luax_checkstring(L, 1); - Buffer b; - if (!fs->exists(f)) + + static int l_play(lua_State* L) + { + Audio::get()->play(); + return 0; + } + + static int l_stop(lua_State* L) { - error(L, "No such image %s", f); - goto fail; + Audio::get()->stop(); + return 0; } - if (!fs->read(f, &b)) + + 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) { - error(L, "Failed to read source file %s", f); - goto fail; + float volume = luax_checknumber(L, 1); + Audio::get()->setVolume(volume); + return 0; } - Source* src = Source::createSource(b.data, b.size); - if (src == nullptr) + + static int l_newSource(lua_State* L) { - error(L, "Failed to decode source file %s", f); - goto fail; + AssetDatabase* fs = AssetDatabase::get(); + const char* f = luax_checkstring(L, 1); + Buffer b; + if (!fs->exists(f)) + { + error(L, "No such image %s", f); + goto fail; + } + if (!fs->read(f, &b)) + { + error(L, "Failed to read source file %s", f); + goto fail; + } + Source* src = Source::createSource(b.data, b.size); + if (src == nullptr) + { + error(L, "Failed to decode source file %s", f); + goto fail; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); + proxy->bind(new Ref<Source>(src, JIN_AUDIO_SOURCE)); + return 1; + fail: + luax_pushnil(L); + return 1; } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); - proxy->bind(new Ref<Source>(src, JIN_AUDIO_SOURCE)); - return 1; - fail: - luax_pushnil(L); - 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; - } - -} // lua -} // jin
\ No newline at end of file + 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; + } + + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/audio/source.cpp b/src/lua/modules/audio/source.cpp index a001f92..e5b7953 100644 --- a/src/lua/modules/audio/source.cpp +++ b/src/lua/modules/audio/source.cpp @@ -3,114 +3,114 @@ #include "lua/common/common.h" #include "lua/modules/types.h" -namespace jin +namespace JinEngine { -namespace lua -{ - - using namespace JinEngine::Audio; - - typedef Ref<Source>& SourceRef; - - static inline SourceRef checkSource(lua_State* L) + namespace Lua { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); - return proxy->getRef<Source>(); - } - 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 } - }; + using namespace JinEngine::Audio; + + typedef Ref<Source>& SourceRef; + + static inline SourceRef checkSource(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); + return proxy->getRef<Source>(); + } + + 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 + int luaopen_Source(lua_State* L) + { + luax_newtype(L, JIN_AUDIO_SOURCE, f); + return 0; + } + + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/bit/bit.cpp b/src/lua/modules/bit/bit.cpp index c1ca840..2d0b644 100644 --- a/src/lua/modules/bit/bit.cpp +++ b/src/lua/modules/bit/bit.cpp @@ -2,83 +2,83 @@ #include "libjin/jin.h" #include <cstdlib> -namespace jin +namespace JinEngine { -namespace lua -{ - - static int l_and(lua_State* L) + namespace Lua { - 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_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_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_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_not(lua_State* L) - { - int n = luax_checkinteger(L, 1); - luax_pushinteger(L, ~n); - 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_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_not(lua_State* L) + { + int n = luax_checkinteger(L, 1); + luax_pushinteger(L, ~n); + 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_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_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 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 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 } - }; + 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; + } - int luaopen_bit(lua_State* L) - { - luax_newlib(L, f); + 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; - } + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/core/core.cpp b/src/lua/modules/core/core.cpp index b2b5766..da7b470 100644 --- a/src/lua/modules/core/core.cpp +++ b/src/lua/modules/core/core.cpp @@ -1,46 +1,46 @@ #include "lua/modules/luax.h" #include "libjin/jin.h" -namespace jin +namespace JinEngine { -namespace lua -{ - - using namespace JinEngine::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) + namespace Lua { - 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; - } -} // lua -} // jin
\ No newline at end of file + using namespace JinEngine::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; + } + + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/event/event.cpp b/src/lua/modules/event/event.cpp index 510afd7..7227516 100644 --- a/src/lua/modules/event/event.cpp +++ b/src/lua/modules/event/event.cpp @@ -4,127 +4,127 @@ #include "lua/modules/luax.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine; - using namespace JinEngine::Input; + using namespace JinEngine; + using namespace JinEngine::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)) + /** + * Load event poll, return a iterator(a table). + */ + static int l_event_poll(lua_State *L) { - /** - * TODO: ڴСıʱҪtransform - * - */ + /* table to store events */ luax_newtable(L); - switch (e.type) + static Event e; + int i = 1; + poll: + while (pollEvent(&e)) { - case EventType::QUIT: - luax_setfieldstring(L, "type", "Quit"); - break; + /** + * TODO: ڴСıʱҪtransform + * + */ + luax_newtable(L); + switch (e.type) + { + case EventType::QUIT: + luax_setfieldstring(L, "type", "Quit"); + break; - case EventType::KEY_DOWN: - case EventType::KEY_UP: - luax_setfieldstring(L, "type", e.type == EventType::KEY_DOWN ? "KeyDown" : "KeyUp"); - luax_setfieldstring(L, "key", getKeyName(e.key.keysym.sym)); - break; + case EventType::KEY_DOWN: + case EventType::KEY_UP: + luax_setfieldstring(L, "type", e.type == EventType::KEY_DOWN ? "KeyDown" : "KeyUp"); + luax_setfieldstring(L, "key", getKeyName(e.key.keysym.sym)); + break; - case EventType::MOUSE_MOTION: - luax_setfieldstring(L, "type", "MouseMotion"); - luax_setfieldnumber(L, "x", e.motion.x); - luax_setfieldnumber(L, "y", e.motion.y); - break; + case EventType::MOUSE_MOTION: + luax_setfieldstring(L, "type", "MouseMotion"); + luax_setfieldnumber(L, "x", e.motion.x); + luax_setfieldnumber(L, "y", e.motion.y); + break; - case EventType::MOUSE_BUTTON_DOWN: - case EventType::MOUSE_BUTTON_UP: - luax_setfieldstring(L, "type", e.type == EventType::MOUSE_BUTTON_DOWN ? "MouseButtonDown" : "MouseButtonUp"); - luax_setfieldstring(L, "button", getButtonName(e.button.button)); - luax_setfieldnumber(L, "x", e.button.x); - luax_setfieldnumber(L, "y", e.button.y); - break; + case EventType::MOUSE_BUTTON_DOWN: + case EventType::MOUSE_BUTTON_UP: + luax_setfieldstring(L, "type", e.type == EventType::MOUSE_BUTTON_DOWN ? "MouseButtonDown" : "MouseButtonUp"); + luax_setfieldstring(L, "button", getButtonName(e.button.button)); + luax_setfieldnumber(L, "x", e.button.x); + luax_setfieldnumber(L, "y", e.button.y); + break; - case EventType::MOUSE_WHEEL: - luax_setfieldstring(L, "type", "Wheel"); - if(e.wheel.x == -1) - luax_setfieldstring(L, "x", "Left"); - else if(e.wheel.x == 1) - luax_setfieldstring(L, "x", "Right"); - else - luax_setfieldstring(L, "x", "None"); - if (e.wheel.y == -1) - luax_setfieldstring(L, "y", "Near"); - else if (e.wheel.y == 1) - luax_setfieldstring(L, "y", "Far"); - else - luax_setfieldstring(L, "y", "None"); - break; + case EventType::MOUSE_WHEEL: + luax_setfieldstring(L, "type", "Wheel"); + if(e.wheel.x == -1) + luax_setfieldstring(L, "x", "Left"); + else if(e.wheel.x == 1) + luax_setfieldstring(L, "x", "Right"); + else + luax_setfieldstring(L, "x", "None"); + if (e.wheel.y == -1) + luax_setfieldstring(L, "y", "Near"); + else if (e.wheel.y == 1) + luax_setfieldstring(L, "y", "Far"); + else + luax_setfieldstring(L, "y", "None"); + break; - case EventType::JOYBUTTONDOWN: - case EventType::JOYBUTTONUP: - luax_setfieldstring(L, "type", e.type == EventType::JOYBUTTONDOWN ? "JoyButtonDown" : "JoyButtonUp"); - luax_setfieldinteger(L, "which", e.jbutton.which); - luax_setfieldstring(L, "button", Input::getJoyButtonName(e.jbutton.button)); - break; + case EventType::JOYBUTTONDOWN: + case EventType::JOYBUTTONUP: + luax_setfieldstring(L, "type", e.type == EventType::JOYBUTTONDOWN ? "JoyButtonDown" : "JoyButtonUp"); + luax_setfieldinteger(L, "which", e.jbutton.which); + luax_setfieldstring(L, "button", Input::getJoyButtonName(e.jbutton.button)); + break; - case EventType::JOYAXISMOTION: - luax_setfieldstring(L, "type", "JoyAxisMotion"); - luax_setfieldinteger(L, "which", e.jaxis.which); - luax_setfieldfstring(L, "axis", Input::getJoyAxisName(e.jaxis.axis)); - break; + case EventType::JOYAXISMOTION: + luax_setfieldstring(L, "type", "JoyAxisMotion"); + luax_setfieldinteger(L, "which", e.jaxis.which); + luax_setfieldfstring(L, "axis", Input::getJoyAxisName(e.jaxis.axis)); + break; - case EventType::JOYBALLMOTION: - case EventType::JOYHATMOTION: + case EventType::JOYBALLMOTION: + case EventType::JOYHATMOTION: - case EventType::JOYDEVICEADDED: - case EventType::JOYDEVICEREMOVED: - luax_setfieldfstring(L, "type", e.type == EventType::JOYDEVICEADDED ? "JoyDeviceAdded" : "JoyDeviceRemoved"); - luax_setfieldinteger(L, "which", e.jdevice.which); - break; + case EventType::JOYDEVICEADDED: + case EventType::JOYDEVICEREMOVED: + luax_setfieldfstring(L, "type", e.type == EventType::JOYDEVICEADDED ? "JoyDeviceAdded" : "JoyDeviceRemoved"); + luax_setfieldinteger(L, "which", e.jdevice.which); + break; - //https://stackoverflow.com/questions/50022316/what-is-sdl-joystick-and-what-is-sdl-gamecontroller-what-are-the-relationships - case EventType::CONTROLLERBUTTONDOWN: - case EventType::CONTROLLERBUTTONUP: + //https://stackoverflow.com/questions/50022316/what-is-sdl-joystick-and-what-is-sdl-gamecontroller-what-are-the-relationships + case EventType::CONTROLLERBUTTONDOWN: + case EventType::CONTROLLERBUTTONUP: - case EventType::CONTROLLERAXISMOTION: + case EventType::CONTROLLERAXISMOTION: - default: - /* ignore other events */ - luax_pop(L, 1); // pop table out - goto poll; - break; + default: + /* ignore other events */ + luax_pop(L, 1); // pop table out + goto poll; + break; + } + luax_rawseti(L, -2, i++); } - luax_rawseti(L, -2, i++); + return 1; } - return 1; - } - static const luaL_Reg f[] = { - { "poll", l_event_poll }, - { 0, 0 } - }; + 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; - } + /** + * load event module + */ + int luaopen_event(lua_State* L) + { + luax_newlib(L, f); + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/filesystem/filesystem.cpp b/src/lua/modules/filesystem/filesystem.cpp index 3cb413f..4919936 100644 --- a/src/lua/modules/filesystem/filesystem.cpp +++ b/src/lua/modules/filesystem/filesystem.cpp @@ -4,137 +4,137 @@ using namespace JinEngine::Filesystem; -namespace jin +namespace JinEngine { -namespace lua -{ - - static struct - { - AssetDatabase* fs; - } context; - - static int l_init(lua_State* L) - { - context.fs = AssetDatabase::get(); - return 0; - } - - static int l_mount(lua_State* L) + namespace Lua { - const char* path = luax_checkstring(L, 1); - context.fs->mount(path); - return 0; - } - 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; - } - - static int l_isFile(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->isFile(path); - luax_pushboolean(L, r); - return 1; - } + static struct + { + AssetDatabase* fs; + } context; - static int loadbuffer(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); + static int l_init(lua_State* L) + { + context.fs = AssetDatabase::get(); + return 0; + } - std::string tmp(filename); - tmp += ".lua"; + static int l_mount(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + context.fs->mount(path); + return 0; + } - int size = tmp.size(); + 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; + } - for (int i = 0; i<size - 4; ++i) + static int l_isDir(lua_State* L) { - if (tmp[i] == '.') - { - tmp[i] = '/'; - } + const char* path = luax_checkstring(L, 1); + int r = context.fs->isDir(path); + luax_pushboolean(L, r); + return 1; } - if (context.fs->exists(tmp.c_str())) + static int l_isFile(lua_State* L) { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - return loadbuffer(L); + const char* path = luax_checkstring(L, 1); + int r = context.fs->isFile(path); + luax_pushboolean(L, r); + return 1; } - tmp = filename; - size = tmp.size(); - for (int i = 0; i<size; ++i) + static int loadbuffer(lua_State* L) { - if (tmp[i] == '.') - tmp[i] = '/'; + 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; } - if (context.fs->isDir(tmp.c_str())) + static int loader(lua_State* L) { - tmp += "/init.lua"; + const char * filename = lua_tostring(L, -1); + + std::string tmp(filename); + tmp += ".lua"; + + int size = tmp.size(); + + for (int i = 0; i<size - 4; ++i) + { + if (tmp[i] == '.') + { + tmp[i] = '/'; + } + } + if (context.fs->exists(tmp.c_str())) { lua_pop(L, 1); lua_pushstring(L, tmp.c_str()); return loadbuffer(L); } + + tmp = filename; + size = tmp.size(); + for (int i = 0; i<size; ++i) + { + if (tmp[i] == '.') + tmp[i] = '/'; + } + + if (context.fs->isDir(tmp.c_str())) + { + tmp += "/init.lua"; + if (context.fs->exists(tmp.c_str())) + { + lua_pop(L, 1); + lua_pushstring(L, tmp.c_str()); + return loadbuffer(L); + } + } + + lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); + return 1; } - lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); - return 1; - } + static int l_read(lua_State* L) + { + AssetDatabase* fs = context.fs; + const char* file = luax_checkstring(L, 1); + unsigned int len; + Buffer buffer; + buffer.data = (char*)fs->read(file, &buffer.size); + luax_pushstring(L, (char*)buffer.data); + luax_pushinteger(L, buffer.size); + return 2; + } - static int l_read(lua_State* L) - { - AssetDatabase* fs = context.fs; - const char* file = luax_checkstring(L, 1); - unsigned int len; - Buffer buffer; - buffer.data = (char*)fs->read(file, &buffer.size); - luax_pushstring(L, (char*)buffer.data); - luax_pushinteger(L, buffer.size); - return 2; - } - - static const luaL_Reg f[] = { - { "init", l_init }, - { "mount", l_mount }, - { "isDirectory", l_isDir }, - { "isFile", l_isFile }, - { "exist", l_exist }, - { "read", l_read }, - { 0, 0 } - }; - - int luaopen_filesystem(lua_State* L) - { - luax_newlib(L, f); - luax_registersearcher(L, loader, 1); - return 0; - } + static const luaL_Reg f[] = { + { "init", l_init }, + { "mount", l_mount }, + { "isDirectory", l_isDir }, + { "isFile", l_isFile }, + { "exist", l_exist }, + { "read", l_read }, + { 0, 0 } + }; + + int luaopen_filesystem(lua_State* L) + { + luax_newlib(L, f); + luax_registersearcher(L, loader, 1); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/bitmap.cpp b/src/lua/modules/graphics/bitmap.cpp index 9a396fb..30ac807 100644 --- a/src/lua/modules/graphics/bitmap.cpp +++ b/src/lua/modules/graphics/bitmap.cpp @@ -3,111 +3,111 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - typedef Ref<Bitmap>& BitmapRef; + typedef Ref<Bitmap>& BitmapRef; - static inline BitmapRef checkBitmap(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - return proxy->getRef<Bitmap>(); - } + static inline BitmapRef checkBitmap(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + return proxy->getRef<Bitmap>(); + } - static int l_gc(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - ref.release(); - return 0; - } + static int l_gc(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + ref.release(); + return 0; + } - static int l_getWidth(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int w = ref->getWidth(); - luax_pushinteger(L, w); - return 1; - } + static int l_getWidth(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int w = ref->getWidth(); + luax_pushinteger(L, w); + return 1; + } - static int l_getHeight(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int h = ref->getHeight(); - luax_pushinteger(L, h); - return 1; - } + static int l_getHeight(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int h = ref->getHeight(); + luax_pushinteger(L, h); + return 1; + } - static int l_getSize(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int w = ref->getWidth(); - int h = ref->getHeight(); - luax_pushinteger(L, w); - luax_pushinteger(L, h); - return 2; - } + static int l_getSize(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int w = ref->getWidth(); + int h = ref->getHeight(); + luax_pushinteger(L, w); + luax_pushinteger(L, h); + return 2; + } - static int l_getPixel(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int x = luax_checkinteger(L, 2); - int y = luax_checkinteger(L, 3); - Color col = ref->getPixel(x, y); - luax_pushinteger(L, col.r); - luax_pushinteger(L, col.g); - luax_pushinteger(L, col.b); - luax_pushinteger(L, col.a); - return 4; - } + static int l_getPixel(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + Color col = ref->getPixel(x, y); + luax_pushinteger(L, col.r); + luax_pushinteger(L, col.g); + luax_pushinteger(L, col.b); + luax_pushinteger(L, col.a); + return 4; + } - static int l_setPixel(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - int x = luax_checkinteger(L, 2); - int y = luax_checkinteger(L, 3); - if (!luax_istable(L, 4)) + static int l_setPixel(lua_State* L) { - luax_typerror(L, 4, "table"); - return 1; + BitmapRef ref = checkBitmap(L); + int x = luax_checkinteger(L, 2); + int y = luax_checkinteger(L, 3); + if (!luax_istable(L, 4)) + { + luax_typerror(L, 4, "table"); + return 1; + } + unsigned int r = luax_rawgetnumber(L, 4, 1); + unsigned int g = luax_rawgetnumber(L, 4, 2); + unsigned int b = luax_rawgetnumber(L, 4, 3); + unsigned int a = luax_rawgetnumber(L, 4, 4); + ref->setPixel(Color(r, g, b, a), x, y); + return 0; } - unsigned int r = luax_rawgetnumber(L, 4, 1); - unsigned int g = luax_rawgetnumber(L, 4, 2); - unsigned int b = luax_rawgetnumber(L, 4, 3); - unsigned int a = luax_rawgetnumber(L, 4, 4); - ref->setPixel(Color(r, g, b, a), x, y); - return 0; - } - static int l_clone(lua_State* L) - { - BitmapRef ref = checkBitmap(L); - Bitmap* bitmap = ref.getObject(); - Bitmap* b = Bitmap::clone(bitmap); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); - proxy->bind(new Ref<Bitmap>(b, JIN_GRAPHICS_BITMAP)); - return 1; - } + static int l_clone(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + Bitmap* bitmap = ref.getObject(); + Bitmap* b = Bitmap::clone(bitmap); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); + proxy->bind(new Ref<Bitmap>(b, JIN_GRAPHICS_BITMAP)); + return 1; + } - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "getSize", l_getSize }, - { "getPixel", l_getPixel }, - { "setPixel", l_setPixel }, - { "clone", l_clone }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { "getPixel", l_getPixel }, + { "setPixel", l_setPixel }, + { "clone", l_clone }, + { 0, 0 } + }; - int luaopen_Bitmap(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_BITMAP, f); - return 0; - } + int luaopen_Bitmap(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_BITMAP, f); + return 0; + } -} // graphics -} // jin
\ No newline at end of file + } // graphics +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/canvas.cpp b/src/lua/modules/graphics/canvas.cpp index 4639179..506d782 100644 --- a/src/lua/modules/graphics/canvas.cpp +++ b/src/lua/modules/graphics/canvas.cpp @@ -3,73 +3,73 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - typedef Ref<Canvas>& CanvasRef; + typedef Ref<Canvas>& CanvasRef; - static inline CanvasRef checkCanvas(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - return proxy->getRef<Canvas>(); - } + static inline CanvasRef checkCanvas(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + return proxy->getRef<Canvas>(); + } - static int l_getWidth(lua_State* L) - { - CanvasRef ref = checkCanvas(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } + 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_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_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_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 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 } - }; + 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; - } + int luaopen_Canvas(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_CANVAS, f); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 9322765..573f319 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -6,797 +6,797 @@ #include "lua/modules/types.h" #include "lua/common/common.h" -namespace jin +namespace JinEngine { -namespace lua -{ - using namespace std; - using namespace JinEngine; - using namespace JinEngine::Graphics; - using JinEngine::Filesystem::AssetDatabase; - using JinEngine::Filesystem::Buffer; - -#include "../../resources/font.ttf.h" - - static struct - { - Color curRenderColor; - Color curClearColor; - Font* curFont = nullptr; - Font* defaultFont = nullptr; - } context; + namespace Lua + { + using namespace std; + using namespace JinEngine; + using namespace JinEngine::Graphics; + using JinEngine::Filesystem::AssetDatabase; + using JinEngine::Filesystem::Buffer; + + #include "../../resources/font.ttf.h" + + 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_getfieldinteger(L, 1, "width"); + setting.height = luax_getfieldinteger(L, 1, "height"); + setting.title = luax_getfieldstring(L, 1, "title"); + setting.vsync = luax_getfieldbool(L, 1, "vsync"); + setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen"); + setting.resizable = luax_getfieldbool(L, 1, "resizable"); + if (!wnd->init(&setting)) + { + luax_pushboolean(L, false); + return 1; + } + { + /* load default font */ + Bitmap* bitmap = Bitmap::createBitmap(default_font_bitmap, sizeof(default_font_bitmap)); + TextureFont* tf = TextureFont::createTextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight()); + context.defaultFont = tf; + delete bitmap; + } + context.curFont = context.defaultFont; - static int l_init(lua_State* L) - { - Window* wnd = Window::get(); - Window::Setting setting; - setting.width = luax_getfieldinteger(L, 1, "width"); - setting.height = luax_getfieldinteger(L, 1, "height"); - setting.title = luax_getfieldstring(L, 1, "title"); - setting.vsync = luax_getfieldbool(L, 1, "vsync"); - setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen"); - setting.resizable = luax_getfieldbool(L, 1, "resizable"); - if (!wnd->init(&setting)) - { - luax_pushboolean(L, false); + luax_pushboolean(L, true); return 1; } + + static int l_setTitle(lua_State* L) { - /* load default font */ - Bitmap* bitmap = Bitmap::createBitmap(default_font_bitmap, sizeof(default_font_bitmap)); - TextureFont* tf = TextureFont::createTextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight()); - context.defaultFont = tf; - delete bitmap; + Window* wnd = Window::get(); + const char* title = luax_checkstring(L, 1); + wnd->setTitle(title); + return 0; } - context.curFont = context.defaultFont; - - luax_pushboolean(L, true); - return 1; - } - - static int l_setTitle(lua_State* L) - { - Window* wnd = Window::get(); - const char* title = luax_checkstring(L, 1); - wnd->setTitle(title); - return 0; - } - - 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_newBitmap(lua_State* L) - { - Bitmap* bitmap = nullptr; - if (luax_gettop(L) == 2) - { - int w = luax_checkinteger(L, 1); - int h = luax_checkinteger(L, 2); - bitmap = Bitmap::createBitmap(w, h); - } - else if (luax_gettop(L) == 3) + static int l_destroy(lua_State* L) { - int w = luax_checkinteger(L, 1); - int h = luax_checkinteger(L, 2); - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - unsigned int r = luax_rawgetnumber(L, 3, 1); - unsigned int g = luax_rawgetnumber(L, 3, 2); - unsigned int b = luax_rawgetnumber(L, 3, 3); - unsigned int a = luax_rawgetnumber(L, 3, 4); - bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a)); + Window* wnd = Window::get(); + wnd->quit(); + return 0; } - else + + static int l_getSize(lua_State* L) { - const char* f = luax_checkstring(L, 1); - AssetDatabase* fs = AssetDatabase::get(); - if (!fs->exists(f)) - { - error(L, "No such image file %s", f); - goto fail; - } - Buffer b = {}; - if (!fs->read(f, &b)) - { - error(L, "Failed to read image %s", f); - goto fail; - } - bitmap = Bitmap::createBitmap(b.data, b.size); - //const Color* col = bitmap->getPixels(); - //ofstream o = ofstream("img.txt", ios_base::app); - //for (int i = 0; i < bitmap->getWidth() * bitmap->getHeight(); ++i) - //{ - // Color c = col[i]; - // o << (int)c.r << ','; - // o << (int)c.g << ','; - // o << (int)c.b << ','; - // o << (int)c.a << ','; - // if ((i + 1) % 10 == 0) - // o << endl; - //} - if (bitmap == nullptr) - { - error(L, "Failed to decode image file %s", f); - goto fail; - } - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); - proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP)); - return 1; - fail: - luax_pushnil(L); - return 1; - } - - /* jin.graphics.newTexture(bitmap) */ - static int l_newTexture(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - Ref<Bitmap>& refBitmap = p->getRef<Bitmap>(); - Bitmap* bitmap = refBitmap.getObject(); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); - Texture* tex = Texture::createTexture(bitmap); - proxy->bind(new Ref<Texture>(tex, JIN_GRAPHICS_TEXTURE)); - return 1; - } - - static int l_newShader(lua_State* L) - { - const char* program = luax_checkstring(L, 1); - Shader* jsl = Shader::createShader(program); - if (jsl == nullptr) + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + luax_pushnumber(L, wnd->getH()); + return 2; + } + + static int l_getWidth(lua_State* L) { - error(L, "Failed to compile shader"); - luax_pushnil(L); + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); return 1; } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - proxy->bind(new Ref<Shader>(jsl, JIN_GRAPHICS_SHADER)); - return 1; - } - static int l_newShaderf(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - AssetDatabase* fs = AssetDatabase::get(); - if (!fs->exists(path)) + static int l_getHeight(lua_State* L) { - error(L, "No such shader file %s\n", path); - luax_pushnil(L); + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getH()); return 1; } - Buffer b; - fs->read(path, &b); - Shader* jsl = Shader::createShader((char*)b.data); - if (jsl == nullptr) + + static int l_newBitmap(lua_State* L) { - error(L, "Failed to compile shader"); + Bitmap* bitmap = nullptr; + if (luax_gettop(L) == 2) + { + int w = luax_checkinteger(L, 1); + int h = luax_checkinteger(L, 2); + bitmap = Bitmap::createBitmap(w, h); + } + else if (luax_gettop(L) == 3) + { + int w = luax_checkinteger(L, 1); + int h = luax_checkinteger(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + unsigned int r = luax_rawgetnumber(L, 3, 1); + unsigned int g = luax_rawgetnumber(L, 3, 2); + unsigned int b = luax_rawgetnumber(L, 3, 3); + unsigned int a = luax_rawgetnumber(L, 3, 4); + bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a)); + } + else + { + const char* f = luax_checkstring(L, 1); + AssetDatabase* fs = AssetDatabase::get(); + if (!fs->exists(f)) + { + error(L, "No such image file %s", f); + goto fail; + } + Buffer b = {}; + if (!fs->read(f, &b)) + { + error(L, "Failed to read image %s", f); + goto fail; + } + bitmap = Bitmap::createBitmap(b.data, b.size); + //const Color* col = bitmap->getPixels(); + //ofstream o = ofstream("img.txt", ios_base::app); + //for (int i = 0; i < bitmap->getWidth() * bitmap->getHeight(); ++i) + //{ + // Color c = col[i]; + // o << (int)c.r << ','; + // o << (int)c.g << ','; + // o << (int)c.b << ','; + // o << (int)c.a << ','; + // if ((i + 1) % 10 == 0) + // o << endl; + //} + if (bitmap == nullptr) + { + error(L, "Failed to decode image file %s", f); + goto fail; + } + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); + proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP)); + return 1; + fail: luax_pushnil(L); return 1; } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - proxy->bind(new Ref<Shader>(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<Canvas>(cvs, JIN_GRAPHICS_CANVAS)); - return 1; - } - - static int l_clear(lua_State* L) - { - glClear(GL_COLOR_BUFFER_BIT); - return 0; - } - - static int l_setClearColor(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(0, 0, 0, 1); - return 0; - } - - context.curClearColor.r = luax_checknumber(L, 1); - context.curClearColor.g = luax_checknumber(L, 2); - context.curClearColor.b = luax_checknumber(L, 3); - context.curClearColor.a = luax_checknumber(L, 4); - - gl.setClearColor(context.curClearColor.r, - context.curClearColor.g, - context.curClearColor.b, - context.curClearColor.a); - return 0; - } - - static int l_present(lua_State* L) - { - Window::get()->swapBuffers(); - return 0; - } - - static void l_draw_texture(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) - return; - 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); - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Texture>& tex = proxy->getRef<Texture>(); - tex->draw(x, y, sx, sy, r); - } - - static void l_draw_canvas(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) - return; - 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); - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Canvas>& p = proxy->getRef<Canvas>(); - p->draw(x, y, sx, sy, r); - } - - /* jin.graphics.draw(text, font, x, y) */ - static void l_draw_text(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_TEXT)) - return; - Proxy* p = (Proxy*)luax_toudata(L, 1); - Text* text = p->getObject<Text>(); - int x = luax_optnumber(L, 3, 0); - int y = luax_optnumber(L, 4, 0); - int spacing = luax_optnumber(L, 6, 0); - Font* font = nullptr; - Proxy* p2 = (Proxy*)luax_toudata(L, 2); - if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT)) - { - TextureFont* tf = p2->getObject<TextureFont>(); - font = tf; - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) - { - TTF* ttf = p2->getObject<TTF>(); - font = ttf; - } - else - { - font = context.defaultFont; - } - int lineheight = luax_optnumber(L, 5, font->getFontSize()); - font->print(*text, x, y, lineheight, spacing); - } - - /* jin.graphics.draw(page, x, y) */ - static void l_draw_page(lua_State* L) - { - if (!luax_istype(L, 1, JIN_GRAPHICS_PAGE)) - return; - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - Proxy* p = (Proxy*)luax_toudata(L, 1); - Page* page = p->getObject<Page>(); - Font* font = page->font; - font->print(page, x, y); - } - - static int l_draw(lua_State* L) - { - if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) - l_draw_texture(L); - else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) - l_draw_canvas(L); - else if (luax_istype(L, 1, JIN_GRAPHICS_TEXT)) - l_draw_text(L); - else if (luax_istype(L, 1, JIN_GRAPHICS_PAGE)) - l_draw_page(L); - else - { - luax_typerror(L, 1, "texture or canvas"); + /* jin.graphics.newTexture(bitmap) */ + static int l_newTexture(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + Ref<Bitmap>& refBitmap = p->getRef<Bitmap>(); + Bitmap* bitmap = refBitmap.getObject(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); + Texture* tex = Texture::createTexture(bitmap); + proxy->bind(new Ref<Texture>(tex, JIN_GRAPHICS_TEXTURE)); return 1; } - return 0; - } - // draw(tex, quad, x, y, sx, sy, r, ax, ay) - static int l_drawq(lua_State* L) - { - if (!luax_istable(L, 2)) + static int l_newShader(lua_State* L) { - luax_typerror(L, 2, "table"); + const char* program = luax_checkstring(L, 1); + Shader* jsl = Shader::createShader(program); + if (jsl == nullptr) + { + error(L, "Failed to compile shader"); + luax_pushnil(L); + return 1; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); + proxy->bind(new Ref<Shader>(jsl, JIN_GRAPHICS_SHADER)); return 1; } - Math::Quad q; - q.x = luax_rawgetnumber(L, 2, 1); - q.y = luax_rawgetnumber(L, 2, 2); - q.w = luax_rawgetnumber(L, 2, 3); - q.h = luax_rawgetnumber(L, 2, 4); - luax_pop(L, 4); - int x = luax_optnumber(L, 3, 0); - int y = luax_optnumber(L, 4, 0); - float sx = luax_optnumber(L, 5, 1); - float sy = luax_optnumber(L, 6, 1); - float r = luax_optnumber(L, 7, 0); - float ax = luax_optnumber(L, 8, 0); - float ay = luax_optnumber(L, 9, 0); - if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Texture>& tex = proxy->getRef<Texture>(); - tex->draw(q, x, y, sx, sy, r, ax, ay); - } - else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + static int l_newShaderf(lua_State* L) { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Canvas>& p = proxy->getRef<Canvas>(); - p->draw(q, x, y, sx, sy, r, ax, ay); - } - else - { - luax_typerror(L, 1, "texture or canvas"); + const char* path = luax_checkstring(L, 1); + AssetDatabase* fs = AssetDatabase::get(); + if (!fs->exists(path)) + { + error(L, "No such shader file %s\n", path); + luax_pushnil(L); + return 1; + } + Buffer b; + fs->read(path, &b); + Shader* jsl = Shader::createShader((char*)b.data); + if (jsl == nullptr) + { + error(L, "Failed to compile shader"); + luax_pushnil(L); + return 1; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); + proxy->bind(new Ref<Shader>(jsl, JIN_GRAPHICS_SHADER)); + return 1; } - } - /* print(string, x, y, lineheight, spacing) */ - /* need set font */ - static int l_print(lua_State* L) - { - Font* font = context.curFont; - if (font == nullptr) - return 0; - unsigned length; - const char* str = luax_checklstring(L, 1, &length); - Text text(Encode::UTF8, str, length); - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - int lineheight = luax_optnumber(L, 4, font->getFontSize()); - int spacing = luax_optnumber(L, 5, 0); - font->print(text, x, y, lineheight, spacing); - return 0; - } - - static int l_setColor(lua_State* L) - { - if (luax_gettop(L) == 0) + static int l_newCanvas(lua_State* L) { - glColor4f(1, 1, 1, 1); - return 0; + 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<Canvas>(cvs, JIN_GRAPHICS_CANVAS)); + return 1; } - - context.curRenderColor.r = luax_checknumber(L, 1); - context.curRenderColor.g = luax_checknumber(L, 2); - context.curRenderColor.b = luax_checknumber(L, 3); - if (luax_gettop(L) == 4) - context.curRenderColor.a = luax_checknumber(L, 4); - else - context.curRenderColor.a = 255; - glColor4f(context.curRenderColor.r / 255.f, - context.curRenderColor.g / 255.f, - context.curRenderColor.b / 255.f, - context.curRenderColor.a / 255.f); - return 0; - } - - static int l_getColor(lua_State * L) - { - luax_pushnumber(L, context.curRenderColor.r); - luax_pushnumber(L, context.curRenderColor.g); - luax_pushnumber(L, context.curRenderColor.b); - luax_pushnumber(L, context.curRenderColor.a); - return 4; - } - static int l_bindCanvas(lua_State* L) - { - if (luax_gettop(L) == 0) + static int l_clear(lua_State* L) { - // bind to default canvas - Canvas::unbind(); + glClear(GL_COLOR_BUFFER_BIT); return 0; } - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Ref<Canvas>& ref = proxy->getRef<Canvas>(); - Canvas::bind(ref.getObject()); - return 0; - } - static int l_unbindCanvas(lua_State* L) - { - Canvas::unbind(); - return 0; - } + static int l_setClearColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glClearColor(0, 0, 0, 1); + return 0; + } - static int l_useShader(lua_State* L) - { - if (luax_gettop(L) == 0) + context.curClearColor.r = luax_checknumber(L, 1); + context.curClearColor.g = luax_checknumber(L, 2); + context.curClearColor.b = luax_checknumber(L, 3); + context.curClearColor.a = luax_checknumber(L, 4); + + gl.setClearColor(context.curClearColor.r, + context.curClearColor.g, + context.curClearColor.b, + context.curClearColor.a); + return 0; + } + + static int l_present(lua_State* L) { - Shader::unuse(); + Window::get()->swapBuffers(); return 0; } - if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref<Shader>& jsl = proxy->getRef<Shader>(); - jsl->use(); + + static void l_draw_texture(lua_State* L) + { + if (!luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) + return; + 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); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref<Texture>& tex = proxy->getRef<Texture>(); + tex->draw(x, y, sx, sy, r); + } + + static void l_draw_canvas(lua_State* L) + { + if (!luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + return; + 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); + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref<Canvas>& p = proxy->getRef<Canvas>(); + p->draw(x, y, sx, sy, r); + } + + /* jin.graphics.draw(text, font, x, y) */ + static void l_draw_text(lua_State* L) + { + if (!luax_istype(L, 1, JIN_GRAPHICS_TEXT)) + return; + Proxy* p = (Proxy*)luax_toudata(L, 1); + Text* text = p->getObject<Text>(); + int x = luax_optnumber(L, 3, 0); + int y = luax_optnumber(L, 4, 0); + int spacing = luax_optnumber(L, 6, 0); + Font* font = nullptr; + Proxy* p2 = (Proxy*)luax_toudata(L, 2); + if (luax_istype(L, 2, JIN_GRAPHICS_TEXTUREFONT)) + { + TextureFont* tf = p2->getObject<TextureFont>(); + font = tf; + } + else if (luax_istype(L, 2, JIN_GRAPHICS_TTF)) + { + TTF* ttf = p2->getObject<TTF>(); + font = ttf; + } + else + { + font = context.defaultFont; + } + int lineheight = luax_optnumber(L, 5, font->getFontSize()); + font->print(*text, x, y, lineheight, spacing); + } + + /* jin.graphics.draw(page, x, y) */ + static void l_draw_page(lua_State* L) + { + if (!luax_istype(L, 1, JIN_GRAPHICS_PAGE)) + return; + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + Proxy* p = (Proxy*)luax_toudata(L, 1); + Page* page = p->getObject<Page>(); + Font* font = page->font; + font->print(page, x, y); + } + + static int l_draw(lua_State* L) + { + if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) + l_draw_texture(L); + else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + l_draw_canvas(L); + else if (luax_istype(L, 1, JIN_GRAPHICS_TEXT)) + l_draw_text(L); + else if (luax_istype(L, 1, JIN_GRAPHICS_PAGE)) + l_draw_page(L); + else + { + luax_typerror(L, 1, "texture or canvas"); + return 1; + } + return 0; } - else + + // draw(tex, quad, x, y, sx, sy, r, ax, ay) + static int l_drawq(lua_State* L) { - luax_typerror(L, 1, "JSL shader"); + if (!luax_istable(L, 2)) + { + luax_typerror(L, 2, "table"); + return 1; + } + Math::Quad q; + q.x = luax_rawgetnumber(L, 2, 1); + q.y = luax_rawgetnumber(L, 2, 2); + q.w = luax_rawgetnumber(L, 2, 3); + q.h = luax_rawgetnumber(L, 2, 4); + luax_pop(L, 4); + int x = luax_optnumber(L, 3, 0); + int y = luax_optnumber(L, 4, 0); + float sx = luax_optnumber(L, 5, 1); + float sy = luax_optnumber(L, 6, 1); + float r = luax_optnumber(L, 7, 0); + float ax = luax_optnumber(L, 8, 0); + float ay = luax_optnumber(L, 9, 0); + + if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref<Texture>& tex = proxy->getRef<Texture>(); + tex->draw(q, x, y, sx, sy, r, ax, ay); + } + else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref<Canvas>& p = proxy->getRef<Canvas>(); + p->draw(q, x, y, sx, sy, r, ax, ay); + } + else + { + luax_typerror(L, 1, "texture or canvas"); + } } - return 0; - } - static int l_setBlend(lua_State* L) - { - - return 0; - } + /* print(string, x, y, lineheight, spacing) */ + /* need set font */ + static int l_print(lua_State* L) + { + Font* font = context.curFont; + if (font == nullptr) + return 0; + unsigned length; + const char* str = luax_checklstring(L, 1, &length); + Text text(Encode::UTF8, str, length); + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + int lineheight = luax_optnumber(L, 4, font->getFontSize()); + int spacing = luax_optnumber(L, 5, 0); + font->print(text, x, y, lineheight, spacing); + return 0; + } - static RenderMode strtomode(const char* str) - { - std::string s = std::string(str); - if (s == "fill") return RenderMode::FILL; - else if (s == "line") return RenderMode::LINE; - else return RenderMode::NONE; - } + static int l_setColor(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glColor4f(1, 1, 1, 1); + return 0; + } - static int l_point(lua_State* L) - { - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - JinEngine::Graphics::point(x, y); - - return 0; - } - - static int l_line(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); - JinEngine::Graphics::line(x1, y1, x2, y2); - - return 0; - } + context.curRenderColor.r = luax_checknumber(L, 1); + context.curRenderColor.g = luax_checknumber(L, 2); + context.curRenderColor.b = luax_checknumber(L, 3); + if (luax_gettop(L) == 4) + context.curRenderColor.a = luax_checknumber(L, 4); + else + context.curRenderColor.a = 255; + glColor4f(context.curRenderColor.r / 255.f, + context.curRenderColor.g / 255.f, + context.curRenderColor.b / 255.f, + context.curRenderColor.a / 255.f); + return 0; + } - static int l_rect(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) + static int l_getColor(lua_State * L) { - 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); + luax_pushnumber(L, context.curRenderColor.r); + luax_pushnumber(L, context.curRenderColor.g); + luax_pushnumber(L, context.curRenderColor.b); + luax_pushnumber(L, context.curRenderColor.a); + return 4; } - else + + static int l_bindCanvas(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; + if (luax_gettop(L) == 0) + { + // bind to default canvas + Canvas::unbind(); + return 0; + } + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); + Ref<Canvas>& ref = proxy->getRef<Canvas>(); + Canvas::bind(ref.getObject()); + return 0; } - return 0; - } + static int l_unbindCanvas(lua_State* L) + { + Canvas::unbind(); + return 0; + } - static int l_circle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) + static int l_useShader(lua_State* L) { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - float r = luax_checknumber(L, 4); - circle(mode, x, y, r); + if (luax_gettop(L) == 0) + { + Shader::unuse(); + return 0; + } + if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Ref<Shader>& jsl = proxy->getRef<Shader>(); + jsl->use(); + } + else + { + luax_typerror(L, 1, "JSL shader"); + } + return 0; } - else + + static int l_setBlend(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; + + return 0; } - return 0; - } + static RenderMode strtomode(const char* str) + { + std::string s = std::string(str); + if (s == "fill") return RenderMode::FILL; + else if (s == "line") return RenderMode::LINE; + else return RenderMode::NONE; + } - static int l_triangle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) + static int l_point(lua_State* L) { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); + int x = luax_checknumber(L, 1); + int y = luax_checknumber(L, 2); + JinEngine::Graphics::point(x, y); + return 0; + } + + static int l_line(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); - - int x3 = luax_checknumber(L, 5); - int y3 = luax_checknumber(L, 6); - - triangle(mode, x, y, x2, y2, x3, y3); + JinEngine::Graphics::line(x1, y1, x2, y2); + + return 0; } - else + + static int l_rect(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; + const char* modestr = luax_checkstring(L, 1); + RenderMode mode = strtomode(modestr); + if (mode != RenderMode::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; } - return 0; - } - - static int l_polygon(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - int n = luax_checknumber(L, 2); - RenderMode mode = strtomode(modestr); - if (mode != RenderMode::NONE) + static int l_circle(lua_State* L) { - if (!luax_istable(L, 3)) + const char* modestr = luax_checkstring(L, 1); + RenderMode mode = strtomode(modestr); + if (mode != RenderMode::NONE) { - luax_typerror(L, 3, "table"); - return 1; + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + float r = luax_checknumber(L, 4); + circle(mode, x, y, r); } - int tn = luax_tableidxlen(L, 3); - if (tn != n * 2) + else { - 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); + luax_typerror(L, 1, "'fill' or 'line'"); return 1; } - float* p = (float*)alloca(2 * n * sizeof(float)); - for (int i = 1; i <= 2 * n; ++i) - p[i - 1] = luax_rawgetnumber(L, 3, i); - polygon(mode, p, n); + + return 0; } - else + + static int l_triangle(lua_State* L) { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } + const char* modestr = luax_checkstring(L, 1); + RenderMode mode = strtomode(modestr); + if (mode != RenderMode::NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); - return 0; - } + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); - static int l_newTTFData(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTFDATA, sizeof(Proxy)); - TTFData* fd = nullptr; - { - const char* path = luax_checkstring(L, 1); - AssetDatabase* fs = AssetDatabase::get(); - if (!fs->exists(path)) + int x3 = luax_checknumber(L, 5); + int y3 = luax_checknumber(L, 6); + + triangle(mode, x, y, x2, y2, x3, y3); + } + else { - error(L, "No such font %s\n", path); - luax_pushnil(L); + luax_typerror(L, 1, "'fill' or 'line'"); return 1; } - Buffer b; - fs->read(path, &b); - fd = TTFData::createTTFData((unsigned char*)b.data, b.size); - } - proxy->bind(new Ref<TTFData>(fd, JIN_GRAPHICS_TTFDATA)); - return 1; - } - - /* newText(str[, encode]) */ - static int l_newText(lua_State* L) - { - Encode encode = Encode::UTF8; - if (luax_gettop(L) == 2) - { - const char* e = luax_checkstring(L, 2); - if (strcmp(e, "UTF8") == 0) encode = Encode::UTF8; - //else if (strcmp(e, "UTF16") == 0) encode = Encode::UTF16; - else if (strcmp(e, "ASCII") == 0) encode = Encode::ASCII; - else - { - luax_error(L, "wrong text encode %s", e); - return 0; - } - } - unsigned length; - const char* data = luax_checklstring(L, 1, &length); - Text* text = new Text(encode, data, length); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXT, sizeof(Proxy)); - proxy->bind(new Ref<Text>(text, JIN_GRAPHICS_TEXT)); - return 1; - } - - /* newTextureFont(bitmap, text, color | cellw, cellh) */ - static int l_newTextureFont(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - Bitmap* bitmap = p->getObject<Bitmap>(); - Text* text; - if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) - { - Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - text = pt->getObject<Text>(); - } - else if (luax_isstring(L, 2)) - { - unsigned len; - const char* str = luax_checklstring(L, 2, &len); - text = new Text(Encode::UTF8, str, len); - } - else - { - luax_typerror(L, 2, "Text or string"); - return 1; - } - float cellh = luax_checknumber(L, 4); - TextureFont* textureFont = nullptr; - if (luax_istable(L, 3)) - { - unsigned int r = luax_rawgetnumber(L, 3, 1); - unsigned int g = luax_rawgetnumber(L, 3, 2); - unsigned int b = luax_rawgetnumber(L, 3, 3); - unsigned int a = luax_rawgetnumber(L, 3, 4); - textureFont = TextureFont::createTextureFont(bitmap, *text, Color(r, g, b, a), cellh); - } - else if (luax_isnumber(L, 3)) - { - float cellw = luax_checknumber(L, 3); - textureFont = TextureFont::createTextureFont(bitmap, *text, cellw, cellh); - } - else - { - luax_error(L, "bad arguments #3 to 'newTextureFont', need to be table or number"); + return 0; } - if (luax_isstring(L, 2)) + + static int l_polygon(lua_State* L) { - // Delete temporary text. - delete text; + const char* modestr = luax_checkstring(L, 1); + int n = luax_checknumber(L, 2); + RenderMode mode = strtomode(modestr); + if (mode != RenderMode::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 = (float*)alloca(2 * n * sizeof(float)); + for (int i = 1; i <= 2 * n; ++i) + p[i - 1] = luax_rawgetnumber(L, 3, i); + polygon(mode, p, n); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTUREFONT, sizeof(Proxy)); - proxy->bind(new Ref<TextureFont>(textureFont, JIN_GRAPHICS_TEXTUREFONT)); - return 1; - } - /* setFont(font) */ - static int l_setFont(lua_State* L) - { - if (luax_istype(L, 1, JIN_GRAPHICS_TTF)) + static int l_newTTFData(lua_State* L) { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - TTF* ttf = p->getObject<TTF>(); - context.curFont = ttf; + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTFDATA, sizeof(Proxy)); + TTFData* fd = nullptr; + { + const char* path = luax_checkstring(L, 1); + AssetDatabase* fs = AssetDatabase::get(); + if (!fs->exists(path)) + { + error(L, "No such font %s\n", path); + luax_pushnil(L); + return 1; + } + Buffer b; + fs->read(path, &b); + fd = TTFData::createTTFData((unsigned char*)b.data, b.size); + } + proxy->bind(new Ref<TTFData>(fd, JIN_GRAPHICS_TTFDATA)); + return 1; } - else if (luax_istype(L, 1, JIN_GRAPHICS_TEXTUREFONT)) + + /* newText(str[, encode]) */ + static int l_newText(lua_State* L) + { + Encode encode = Encode::UTF8; + if (luax_gettop(L) == 2) + { + const char* e = luax_checkstring(L, 2); + if (strcmp(e, "UTF8") == 0) encode = Encode::UTF8; + //else if (strcmp(e, "UTF16") == 0) encode = Encode::UTF16; + else if (strcmp(e, "ASCII") == 0) encode = Encode::ASCII; + else + { + luax_error(L, "wrong text encode %s", e); + return 0; + } + } + unsigned length; + const char* data = luax_checklstring(L, 1, &length); + Text* text = new Text(encode, data, length); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXT, sizeof(Proxy)); + proxy->bind(new Ref<Text>(text, JIN_GRAPHICS_TEXT)); + return 1; + } + + /* newTextureFont(bitmap, text, color | cellw, cellh) */ + static int l_newTextureFont(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); + Bitmap* bitmap = p->getObject<Bitmap>(); + Text* text; + if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) + { + Proxy* pt = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + text = pt->getObject<Text>(); + } + else if (luax_isstring(L, 2)) + { + unsigned len; + const char* str = luax_checklstring(L, 2, &len); + text = new Text(Encode::UTF8, str, len); + } + else + { + luax_typerror(L, 2, "Text or string"); + return 1; + } + float cellh = luax_checknumber(L, 4); + TextureFont* textureFont = nullptr; + if (luax_istable(L, 3)) + { + unsigned int r = luax_rawgetnumber(L, 3, 1); + unsigned int g = luax_rawgetnumber(L, 3, 2); + unsigned int b = luax_rawgetnumber(L, 3, 3); + unsigned int a = luax_rawgetnumber(L, 3, 4); + textureFont = TextureFont::createTextureFont(bitmap, *text, Color(r, g, b, a), cellh); + } + else if (luax_isnumber(L, 3)) + { + float cellw = luax_checknumber(L, 3); + textureFont = TextureFont::createTextureFont(bitmap, *text, cellw, cellh); + } + else + { + luax_error(L, "bad arguments #3 to 'newTextureFont', need to be table or number"); + return 0; + } + if (luax_isstring(L, 2)) + { + // Delete temporary text. + delete text; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTUREFONT, sizeof(Proxy)); + proxy->bind(new Ref<TextureFont>(textureFont, JIN_GRAPHICS_TEXTUREFONT)); + return 1; + } + + /* setFont(font) */ + static int l_setFont(lua_State* L) + { + if (luax_istype(L, 1, JIN_GRAPHICS_TTF)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + TTF* ttf = p->getObject<TTF>(); + context.curFont = ttf; + } + else if (luax_istype(L, 1, JIN_GRAPHICS_TEXTUREFONT)) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + TextureFont* tf = p->getObject<TextureFont>(); + context.curFont = tf; + } + return 0; + } + + static int l_unsetFont(lua_State* L) { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - TextureFont* tf = p->getObject<TextureFont>(); - context.curFont = tf; + context.curFont = context.defaultFont; + return 0; } - return 0; - } - static int l_unsetFont(lua_State* L) - { - context.curFont = context.defaultFont; - return 0; - } - - static const luaL_Reg f[] = { - /* window */ - { "init", l_init }, - { "setTitle", l_setTitle }, - { "getSize", l_getSize }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { "destroy", l_destroy }, - /* creators */ - { "newBitmap", l_newBitmap }, - { "newTexture", l_newTexture }, - { "newShader", l_newShader }, - { "newShaderf", l_newShaderf }, - { "newCanvas", l_newCanvas }, - { "newTTFData", l_newTTFData }, - { "newText", l_newText }, - { "newTextureFont", l_newTextureFont }, - /* render */ - { "setClearColor", l_setClearColor }, - { "clear", l_clear }, - { "draw", l_draw }, - { "print", l_print }, - { "drawq", l_drawq }, - { "setColor", l_setColor }, - { "getColor", l_getColor }, - { "present", l_present }, - /* canvas */ - { "bindCanvas", l_bindCanvas }, - { "unbindCanvas", l_unbindCanvas }, - /* shader */ - { "useShader", l_useShader }, - /* shapes */ - { "point", l_point }, - { "line", l_line }, - { "rect", l_rect }, - { "circle", l_circle }, - { "triangle", l_triangle }, - { "polygon", l_polygon }, - /* font */ - { "setFont", l_setFont }, - { "unsetFont", l_unsetFont }, - { 0, 0 } - }; - - extern int luaopen_Texture(lua_State* L); - extern int luaopen_Text(lua_State* L); - extern int luaopen_TTF(lua_State* L); - extern int luaopen_TextureFont(lua_State* L); - extern int luaopen_TTFData(lua_State* L); - extern int luaopen_Page(lua_State* L); - extern int luaopen_Canvas(lua_State* L); - extern int luaopen_JSL(lua_State* L); - extern int luaopen_Bitmap(lua_State* L); + static const luaL_Reg f[] = { + /* window */ + { "init", l_init }, + { "setTitle", l_setTitle }, + { "getSize", l_getSize }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "destroy", l_destroy }, + /* creators */ + { "newBitmap", l_newBitmap }, + { "newTexture", l_newTexture }, + { "newShader", l_newShader }, + { "newShaderf", l_newShaderf }, + { "newCanvas", l_newCanvas }, + { "newTTFData", l_newTTFData }, + { "newText", l_newText }, + { "newTextureFont", l_newTextureFont }, + /* render */ + { "setClearColor", l_setClearColor }, + { "clear", l_clear }, + { "draw", l_draw }, + { "print", l_print }, + { "drawq", l_drawq }, + { "setColor", l_setColor }, + { "getColor", l_getColor }, + { "present", l_present }, + /* canvas */ + { "bindCanvas", l_bindCanvas }, + { "unbindCanvas", l_unbindCanvas }, + /* shader */ + { "useShader", l_useShader }, + /* shapes */ + { "point", l_point }, + { "line", l_line }, + { "rect", l_rect }, + { "circle", l_circle }, + { "triangle", l_triangle }, + { "polygon", l_polygon }, + /* font */ + { "setFont", l_setFont }, + { "unsetFont", l_unsetFont }, + { 0, 0 } + }; + + extern int luaopen_Texture(lua_State* L); + extern int luaopen_Text(lua_State* L); + extern int luaopen_TTF(lua_State* L); + extern int luaopen_TextureFont(lua_State* L); + extern int luaopen_TTFData(lua_State* L); + extern int luaopen_Page(lua_State* L); + extern int luaopen_Canvas(lua_State* L); + extern int luaopen_JSL(lua_State* L); + extern int luaopen_Bitmap(lua_State* L); - int luaopen_graphics(lua_State* L) - { - // register types - luaopen_Bitmap(L); - luaopen_Texture(L); - luaopen_Canvas(L); - luaopen_TTFData(L); - luaopen_TTF(L); - luaopen_Text(L); - luaopen_TextureFont(L); - luaopen_Page(L); - luaopen_JSL(L); - - // load whole lib - luax_newlib(L, f); + int luaopen_graphics(lua_State* L) + { + // register types + luaopen_Bitmap(L); + luaopen_Texture(L); + luaopen_Canvas(L); + luaopen_TTFData(L); + luaopen_TTF(L); + luaopen_Text(L); + luaopen_TextureFont(L); + luaopen_Page(L); + luaopen_JSL(L); + + // load whole lib + luax_newlib(L, f); - return 1; - } + return 1; + } -}// lua + }// lua }// jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/page.cpp b/src/lua/modules/graphics/page.cpp index e4ad829..d135ac0 100644 --- a/src/lua/modules/graphics/page.cpp +++ b/src/lua/modules/graphics/page.cpp @@ -5,69 +5,69 @@ #include <iostream> -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - typedef Ref<Font>& FontRef; + typedef Ref<Font>& FontRef; - Page* getPage(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); - return proxy->getObject<Page>(); - } + Page* getPage(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); + return proxy->getObject<Page>(); + } - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); - { - /* release font */ - Ref<Page>* page = &proxy->getRef<Page>(); - RefBase* font = (RefBase*)page->getUserdata(); - font->release(); - } - proxy->release(); - return 0; - } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); + { + /* release font */ + Ref<Page>* page = &proxy->getRef<Page>(); + RefBase* font = (RefBase*)page->getUserdata(); + font->release(); + } + proxy->release(); + return 0; + } - static int l_getSize(lua_State* L) - { - Page* page = getPage(L); - luax_pushinteger(L, page->size.w); - luax_pushinteger(L, page->size.h); - return 2; - } + static int l_getSize(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->size.w); + luax_pushinteger(L, page->size.h); + return 2; + } - static int l_getWidth(lua_State* L) - { - Page* page = getPage(L); - luax_pushinteger(L, page->size.w); - return 1; - } + static int l_getWidth(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->size.w); + return 1; + } - static int l_getHeight(lua_State* L) - { - Page* page = getPage(L); - luax_pushinteger(L, page->size.h); - return 1; - } + static int l_getHeight(lua_State* L) + { + Page* page = getPage(L); + luax_pushinteger(L, page->size.h); + return 1; + } - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "getSize", l_getSize }, - { "getWidth", l_getWidth }, - { "getHeight", l_getHeight }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getSize", l_getSize }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { 0, 0 } + }; - int luaopen_Page(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_PAGE, f); - return 0; - } + int luaopen_Page(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_PAGE, f); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/shader.cpp b/src/lua/modules/graphics/shader.cpp index 1ea5447..d2d0767 100644 --- a/src/lua/modules/graphics/shader.cpp +++ b/src/lua/modules/graphics/shader.cpp @@ -3,133 +3,133 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - typedef Ref<Shader>& ShaderRef; + typedef Ref<Shader>& ShaderRef; - static inline ShaderRef checkShader(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef<Shader>(); - } + static inline ShaderRef checkShader(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); + return proxy->getRef<Shader>(); + } - /** - * jsl:sendNumber("variable", 0.1) - */ - static int l_sendNumber (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - float number = luax_checknumber(L, 3); - ref->sendFloat(variable, number); - return 0; - } + /** + * jsl:sendNumber("variable", 0.1) + */ + static int l_sendNumber (lua_State* L) + { + ShaderRef ref = checkShader(L); + const char* variable = luax_checkstring(L, 2); + float number = luax_checknumber(L, 3); + ref->sendFloat(variable, number); + return 0; + } - static int l_sendTexture (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); - Ref<Texture>& tex = proxy->getRef<Texture>(); - ref->sendTexture(variable, tex.getObject()); - return 0; - } + static int l_sendTexture (lua_State* L) + { + ShaderRef ref = checkShader(L); + const char* variable = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); + Ref<Texture>& tex = proxy->getRef<Texture>(); + ref->sendTexture(variable, tex.getObject()); + return 0; + } - static int l_sendCanvas (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); - Ref<Canvas>& canvas = proxy->getRef<Canvas>(); - ref->sendCanvas(variable, canvas.getObject()); - return 0; - } + static int l_sendCanvas (lua_State* L) + { + ShaderRef ref = checkShader(L); + const char* variable = luax_checkstring(L, 2); + Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); + Ref<Canvas>& canvas = proxy->getRef<Canvas>(); + ref->sendCanvas(variable, canvas.getObject()); + return 0; + } - static int l_sendVec2 (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - if (!luax_istable(L, 3)) + static int l_sendVec2 (lua_State* L) { - luax_typerror(L, 3, "table"); - return 1; + ShaderRef ref = checkShader(L); + const char* variable = luax_checkstring(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + float x = luax_rawgetnumber(L, 3, 1); + float y = luax_rawgetnumber(L, 3, 2); + ref->sendVec2(variable, x, y); + return 0; } - float x = luax_rawgetnumber(L, 3, 1); - float y = luax_rawgetnumber(L, 3, 2); - ref->sendVec2(variable, x, y); - return 0; - } - static int l_sendVec3 (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - if (!luax_istable(L, 3)) + static int l_sendVec3 (lua_State* L) { - luax_typerror(L, 3, "table"); - return 1; + ShaderRef ref = checkShader(L); + const char* variable = luax_checkstring(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + float x = luax_rawgetnumber(L, 3, 1); + float y = luax_rawgetnumber(L, 3, 2); + float z = luax_rawgetnumber(L, 3, 3); + ref->sendVec3(variable, x, y, z); + return 0; } - float x = luax_rawgetnumber(L, 3, 1); - float y = luax_rawgetnumber(L, 3, 2); - float z = luax_rawgetnumber(L, 3, 3); - ref->sendVec3(variable, x, y, z); - return 0; - } - static int l_sendVec4 (lua_State* L) - { - ShaderRef ref = checkShader(L); - const char* variable = luax_checkstring(L, 2); - if (!luax_istable(L, 3)) + static int l_sendVec4 (lua_State* L) { - luax_typerror(L, 3, "table"); - return 1; + ShaderRef ref = checkShader(L); + const char* variable = luax_checkstring(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + float x = luax_rawgetnumber(L, 3, 1); + float y = luax_rawgetnumber(L, 3, 2); + float z = luax_rawgetnumber(L, 3, 3); + float w = luax_rawgetnumber(L, 3, 4); + ref->sendVec4(variable, x, y, z, w); + return 0; } - float x = luax_rawgetnumber(L, 3, 1); - float y = luax_rawgetnumber(L, 3, 2); - float z = luax_rawgetnumber(L, 3, 3); - float w = luax_rawgetnumber(L, 3, 4); - ref->sendVec4(variable, x, y, z, w); - return 0; - } - static int l_sendColor (lua_State* L) - { - return l_sendVec4(L); - } + static int l_sendColor (lua_State* L) + { + return l_sendVec4(L); + } - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - proxy->release(); - return 0; - } + 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 }, - { "sendNumber", l_sendNumber }, - { "sendTexture", l_sendTexture }, - { "sendCanvas", l_sendCanvas }, - { "sendVec2", l_sendVec2 }, - { "sendVec3", l_sendVec3 }, - { "sendVec4", l_sendVec4 }, - { "sendColor", l_sendColor }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "sendNumber", l_sendNumber }, + { "sendTexture", l_sendTexture }, + { "sendCanvas", l_sendCanvas }, + { "sendVec2", l_sendVec2 }, + { "sendVec3", l_sendVec3 }, + { "sendVec4", l_sendVec4 }, + { "sendColor", l_sendColor }, + { 0, 0 } + }; - /** - * JSL program - */ - int luaopen_JSL(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_SHADER, f); - return 0; - } + /** + * JSL program + */ + int luaopen_JSL(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_SHADER, f); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/text.cpp b/src/lua/modules/graphics/text.cpp index 9940f50..beefcae 100644 --- a/src/lua/modules/graphics/text.cpp +++ b/src/lua/modules/graphics/text.cpp @@ -3,30 +3,30 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - static int l_gc(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXT); - p->release(); - return 0; - } + static int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXT); + p->release(); + return 0; + } - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { 0, 0 } + }; - int luaopen_Text(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXT, f); - return 0; - } + int luaopen_Text(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_TEXT, f); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/texture.cpp b/src/lua/modules/graphics/texture.cpp index 980fe04..63ab5f2 100644 --- a/src/lua/modules/graphics/texture.cpp +++ b/src/lua/modules/graphics/texture.cpp @@ -3,73 +3,73 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - typedef Ref<Texture>& TextureRef; + typedef Ref<Texture>& TextureRef; - static inline TextureRef checkTexture(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); - return proxy->getRef<Texture>(); - } + static inline TextureRef checkTexture(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); + return proxy->getRef<Texture>(); + } - static int l_getWidth(lua_State* L) - { - TextureRef ref = checkTexture(L); - luax_pushnumber(L, ref->getWidth()); - return 1; - } + static int l_getWidth(lua_State* L) + { + TextureRef ref = checkTexture(L); + luax_pushnumber(L, ref->getWidth()); + return 1; + } - static int l_getHeight(lua_State *L) - { - TextureRef ref = checkTexture(L); - luax_pushnumber(L, ref->getHeight()); - return 1; - } + static int l_getHeight(lua_State *L) + { + TextureRef ref = checkTexture(L); + luax_pushnumber(L, ref->getHeight()); + return 1; + } - static int l_setAnchor(lua_State* L) - { - TextureRef ref = checkTexture(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - ref->setAnchor(x, y); - return 0; - } + static int l_setAnchor(lua_State* L) + { + TextureRef ref = checkTexture(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) - { - TextureRef ref = checkTexture(L); - luax_pushnumber(L, ref->getWidth()); - luax_pushnumber(L, ref->getHeight()); - return 2; - } + static int l_getSize(lua_State* L) + { + TextureRef ref = checkTexture(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_TEXTURE); - proxy->release(); - return 0; - } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); + 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 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "getWidth", l_getWidth }, + { "getHeight", l_getHeight }, + { "getSize", l_getSize }, + { "setAnchor", l_setAnchor }, + { 0, 0 } + }; - int luaopen_Texture(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXTURE, f); - return 0; - } + int luaopen_Texture(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_TEXTURE, f); + return 0; + } -}// lua + }// lua }// jin
\ No newline at end of file diff --git a/src/lua/modules/graphics/texture_font.cpp b/src/lua/modules/graphics/texture_font.cpp index bbb51cc..0c31ef7 100644 --- a/src/lua/modules/graphics/texture_font.cpp +++ b/src/lua/modules/graphics/texture_font.cpp @@ -3,65 +3,65 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - proxy->release(); - return 0; - } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + proxy->release(); + return 0; + } - /* typeset(Text | string, lineheight, spacing) */ - static int l_typeset(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); - TextureFont* tf = p->getObject<TextureFont>(); - int lineheight = luax_checkinteger(L, 3); - int spacing = luax_optnumber(L, 4, 0); - Page* page = nullptr; - if (luax_isstring(L, 2)) - { - unsigned length; - const char* str = luax_checklstring(L, 2, &length); - Text text(Encode::UTF8, str, length); - page = tf->typeset(text, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) - { - Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = p2->getObject<Text>(); - page = tf->typeset(*text, lineheight, spacing); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref<Page>* refPage = new Ref<Page>(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Ref<TextureFont>& refTF = p->getRef<TextureFont>(); - refTF.retain(); - refPage->setUserdata(&refTF); - } - proxy->bind(refPage); - return 1; - } + /* typeset(Text | string, lineheight, spacing) */ + static int l_typeset(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTUREFONT); + TextureFont* tf = p->getObject<TextureFont>(); + int lineheight = luax_checkinteger(L, 3); + int spacing = luax_optnumber(L, 4, 0); + Page* page = nullptr; + if (luax_isstring(L, 2)) + { + unsigned length; + const char* str = luax_checklstring(L, 2, &length); + Text text(Encode::UTF8, str, length); + page = tf->typeset(text, lineheight, spacing); + } + else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) + { + Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + Text* text = p2->getObject<Text>(); + page = tf->typeset(*text, lineheight, spacing); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); + Ref<Page>* refPage = new Ref<Page>(page, JIN_GRAPHICS_PAGE); + { + /* retain related ttf */ + Ref<TextureFont>& refTF = p->getRef<TextureFont>(); + refTF.retain(); + refPage->setUserdata(&refTF); + } + proxy->bind(refPage); + return 1; + } - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "typeset", l_typeset }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "typeset", l_typeset }, + { 0, 0 } + }; - int luaopen_TextureFont(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TEXTUREFONT, f); + int luaopen_TextureFont(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_TEXTUREFONT, f); - return 0; - } + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/ttf.cpp b/src/lua/modules/graphics/ttf.cpp index 39383e4..5eb536e 100644 --- a/src/lua/modules/graphics/ttf.cpp +++ b/src/lua/modules/graphics/ttf.cpp @@ -3,71 +3,71 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - { - /* release ttf data */ - Ref<TTF>* ttf = &proxy->getRef<TTF>(); - RefBase* data = (RefBase*)ttf->getUserdata(); - data->release(); - } - proxy->release(); - return 0; - } + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + { + /* release ttf data */ + Ref<TTF>* ttf = &proxy->getRef<TTF>(); + RefBase* data = (RefBase*)ttf->getUserdata(); + data->release(); + } + proxy->release(); + return 0; + } - /* typeset(Text | string, lineheight, spacing) */ - static int l_typeset(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); - TTF* ttf = p->getObject<TTF>(); - int lineheight = luax_optnumber(L, 3, ttf->getFontSize()); - int spacing = luax_optnumber(L, 4, 0); - Page* page = nullptr; - if (luax_isstring(L, 2)) - { - unsigned length; - const char* str = luax_checklstring(L, 2, &length); - Text text(Encode::UTF8, str, length); - page = ttf->typeset(text, lineheight, spacing); - } - else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) - { - Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); - Text* text = p2->getObject<Text>(); - page = ttf->typeset(*text, lineheight, spacing); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref<Page>* refPage = new Ref<Page>(page, JIN_GRAPHICS_PAGE); - { - /* retain related ttf */ - Ref<TTF>& refTTF = p->getRef<TTF>(); - refTTF.retain(); - refPage->setUserdata(&refTTF); - } - proxy->bind(refPage); - return 1; - } + /* typeset(Text | string, lineheight, spacing) */ + static int l_typeset(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); + TTF* ttf = p->getObject<TTF>(); + int lineheight = luax_optnumber(L, 3, ttf->getFontSize()); + int spacing = luax_optnumber(L, 4, 0); + Page* page = nullptr; + if (luax_isstring(L, 2)) + { + unsigned length; + const char* str = luax_checklstring(L, 2, &length); + Text text(Encode::UTF8, str, length); + page = ttf->typeset(text, lineheight, spacing); + } + else if (luax_istype(L, 2, JIN_GRAPHICS_TEXT)) + { + Proxy* p2 = (Proxy*)luax_checktype(L, 2, JIN_GRAPHICS_TEXT); + Text* text = p2->getObject<Text>(); + page = ttf->typeset(*text, lineheight, spacing); + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); + Ref<Page>* refPage = new Ref<Page>(page, JIN_GRAPHICS_PAGE); + { + /* retain related ttf */ + Ref<TTF>& refTTF = p->getRef<TTF>(); + refTTF.retain(); + refPage->setUserdata(&refTTF); + } + proxy->bind(refPage); + return 1; + } - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "typeset", l_typeset }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "typeset", l_typeset }, + { 0, 0 } + }; - int luaopen_TTF(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TTF, f); + int luaopen_TTF(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_TTF, f); - return 0; - } + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/graphics/ttfData.cpp b/src/lua/modules/graphics/ttfData.cpp index f61680f..d7f06fb 100644 --- a/src/lua/modules/graphics/ttfData.cpp +++ b/src/lua/modules/graphics/ttfData.cpp @@ -3,49 +3,49 @@ #include "lua/common/common.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Graphics; + using namespace JinEngine::Graphics; - static int l_newTTF(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); - int fontsize = luax_checkinteger(L, 2); - Ref<TTFData>& refFontData = p->getRef<TTFData>(); - TTFData* fontData = refFontData.getObject(); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTF, sizeof(Proxy)); - TTF* font = fontData->createTTF(fontsize); - Ref<TTF>* refTTF = new Ref<TTF>(font, JIN_GRAPHICS_TTF); - { - Ref<TTFData>& refTTFData = p->getRef<TTFData>(); - refTTFData.retain(); - refTTF->setUserdata(&refTTFData); - } - proxy->bind(refTTF); - return 1; - } + static int l_newTTF(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); + int fontsize = luax_checkinteger(L, 2); + Ref<TTFData>& refFontData = p->getRef<TTFData>(); + TTFData* fontData = refFontData.getObject(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTF, sizeof(Proxy)); + TTF* font = fontData->createTTF(fontsize); + Ref<TTF>* refTTF = new Ref<TTF>(font, JIN_GRAPHICS_TTF); + { + Ref<TTFData>& refTTFData = p->getRef<TTFData>(); + refTTFData.retain(); + refTTF->setUserdata(&refTTFData); + } + proxy->bind(refTTF); + return 1; + } - static int l_gc(lua_State* L) - { - Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); - p->release(); - return 0; - } + static int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); + p->release(); + return 0; + } - static const luaL_Reg f[] = { - { "__gc", l_gc }, - { "newTTF", l_newTTF }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "__gc", l_gc }, + { "newTTF", l_newTTF }, + { 0, 0 } + }; - int luaopen_TTFData(lua_State* L) - { - luax_newtype(L, JIN_GRAPHICS_TTFDATA, f); - return 0; - } + int luaopen_TTFData(lua_State* L) + { + luax_newtype(L, JIN_GRAPHICS_TTFDATA, f); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/joypad/joypad.cpp b/src/lua/modules/joypad/joypad.cpp index 0f0f350..75df50f 100644 --- a/src/lua/modules/joypad/joypad.cpp +++ b/src/lua/modules/joypad/joypad.cpp @@ -1,21 +1,21 @@ #include "libjin/jin.h" #include "lua/modules/luax.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - static const luaL_Reg f[] = { - { 0, 0 } - }; + static const luaL_Reg f[] = { + { 0, 0 } + }; - int luaopen_joypad(lua_State* L) - { - luax_newlib(L, f); + int luaopen_joypad(lua_State* L) + { + luax_newlib(L, f); - return 1; - } - /*SDL_JoystickGetButton*/ -} // lua -} // jin
\ No newline at end of file + return 1; + } + /*SDL_JoystickGetButton*/ + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/keyboard/keyboard.cpp b/src/lua/modules/keyboard/keyboard.cpp index e384671..22bba0d 100644 --- a/src/lua/modules/keyboard/keyboard.cpp +++ b/src/lua/modules/keyboard/keyboard.cpp @@ -1,16 +1,16 @@ #include "lua/modules/luax.h" -namespace jin +namespace JinEngine { -namespace lua -{ - //https://wiki.libsdl.org/SDL_Keycode - - int luaopen_keyboard(lua_State* L) + namespace Lua { - luax_newlib(L, 0); - return 1; - } + //https://wiki.libsdl.org/SDL_Keycode + + int luaopen_keyboard(lua_State* L) + { + luax_newlib(L, 0); + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/math/math.cpp b/src/lua/modules/math/math.cpp index dafec72..fe05b3e 100644 --- a/src/lua/modules/math/math.cpp +++ b/src/lua/modules/math/math.cpp @@ -1,30 +1,30 @@ #include "lua/modules/luax.h" #include "libjin/jin.h" -namespace jin +namespace JinEngine { -namespace lua -{ - - static int l_mod(lua_State* L) + namespace Lua { - 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 } - }; + 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; + } - int luaopen_math(lua_State* L) - { - luax_newlib(L, f); - return 1; - } + static const luaL_Reg f[] = { + { "mod", l_mod }, + { 0, 0 } + }; + + int luaopen_math(lua_State* L) + { + luax_newlib(L, f); + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/mouse/mouse.cpp b/src/lua/modules/mouse/mouse.cpp index 76bfb77..4ef5c16 100644 --- a/src/lua/modules/mouse/mouse.cpp +++ b/src/lua/modules/mouse/mouse.cpp @@ -1,42 +1,42 @@ #include "lua/modules/luax.h" #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Input; + using namespace JinEngine::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 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 int l_setVisible(lua_State* L) - { - bool visible = luax_checkbool(L, 1); - Mouse* mouse = Mouse::get(); - mouse->setVisible(visible); - return 0; - } + static int l_setVisible(lua_State* L) + { + bool visible = luax_checkbool(L, 1); + Mouse* mouse = Mouse::get(); + mouse->setVisible(visible); + return 0; + } - static const luaL_Reg f[] = { - { "position", l_pos }, - { "setVisible", l_setVisible }, - { 0, 0 } - }; + static const luaL_Reg f[] = { + { "position", l_pos }, + { "setVisible", l_setVisible }, + { 0, 0 } + }; - int luaopen_mouse(lua_State* L) - { - luax_newlib(L, f); - return 1; - } + int luaopen_mouse(lua_State* L) + { + luax_newlib(L, f); + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/net/Buffer.cpp b/src/lua/modules/net/Buffer.cpp index d4c687c..bf71fa7 100644 --- a/src/lua/modules/net/Buffer.cpp +++ b/src/lua/modules/net/Buffer.cpp @@ -4,139 +4,139 @@ #include "libjin/jin.h" #include "Buffer.h" -namespace jin -{ -namespace lua -{ -namespace net +namespace JinEngine { + namespace Lua + { + namespace Net + { - using namespace JinEngine; + using namespace JinEngine; - typedef Ref<Buffer>& BufferRef; + typedef Ref<Buffer>& BufferRef; - static inline BufferRef checkNetBuffer(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - return proxy->getRef<Buffer>(); - } + static inline BufferRef checkNetBuffer(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); + return proxy->getRef<Buffer>(); + } - // net.Buffer:append(value) -> value_length - static int l_append(lua_State* L) - { - BufferRef 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:append(value) -> value_length + static int l_append(lua_State* L) + { + BufferRef 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) - { - BufferRef ref = checkNetBuffer(L); - int offset = luax_checkinteger(L, 2); - unsigned int len; - char* data = ref->grabString(&len, offset); - Array<char> str; - str.bind(data, len); - luax_pushstring(L, &str); - luax_pushinteger(L, str.count()); - return 2; - } + // net.Buffer:grabString(offset) -> string, length + static int l_grabString(lua_State* L) + { + BufferRef ref = checkNetBuffer(L); + int offset = luax_checkinteger(L, 2); + unsigned int len; + char* data = ref->grabString(&len, offset); + Array<char> str; + str.bind(data, len); + luax_pushstring(L, &str); + luax_pushinteger(L, str.count()); + return 2; + } - // net.Buffer:grabInteger(offset) -> integer, length - static int l_grabInteger(lua_State* L) - { - BufferRef 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; - } + // net.Buffer:grabInteger(offset) -> integer, length + static int l_grabInteger(lua_State* L) + { + BufferRef 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) - { - BufferRef 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_grabFloat(lua_State* L) + { + BufferRef 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) - { - BufferRef 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_grabBoolean(lua_State* L) + { + BufferRef 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 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 } - }; + 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 + } // Net - int luaopen_Buffer(lua_State* L) - { - luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function); - return 0; - } + 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 + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/net/Buffer.h b/src/lua/modules/net/Buffer.h index 172682d..eefbc32 100644 --- a/src/lua/modules/net/Buffer.h +++ b/src/lua/modules/net/Buffer.h @@ -5,93 +5,93 @@ #include <cstdlib> #include "lua/common/common.h" -namespace jin +namespace JinEngine { -namespace lua -{ -namespace net -{ - - class Buffer + namespace Lua { - public: - Buffer(size_t s = 0) - : size(s) + namespace Net { - buffer = new char[size]; - memset(buffer, 0, size); - } - Buffer(const char* data, size_t s) - : size(s) - { - buffer = new char[size]; - memcpy(buffer, data, size); - } - - ~Buffer() - { - if (buffer != nullptr) + class Buffer { - delete[] buffer; - buffer = nullptr; - size = 0; - } - } + public: + Buffer(size_t s = 0) + : size(s) + { + buffer = new char[size]; + memset(buffer, 0, size); + } - void append(const void* data, size_t s) - { - if (data == nullptr) - return; - char* buf = buffer; - buffer = new char[size + s]; - memcpy(buffer, buf, size); - memcpy(buffer + size, data, s); - delete[] buf; - size += s; - return; - } + Buffer(const char* data, size_t s) + : size(s) + { + buffer = new char[size]; + memcpy(buffer, data, size); + } - /* grab and create a string */ - char* grabString(unsigned int* length, int offset = 0) - { - int l = offset; - for (; l < size; ++l) - { - if (buffer[l] == 0) - break; - } - *length = l - offset + 1; - char* str = (char*)malloc(*length); - memcpy(str, buffer + offset, *length); - return str; - } + ~Buffer() + { + if (buffer != nullptr) + { + delete[] buffer; + buffer = nullptr; + size = 0; + } + } - int grabInteger(int* length, int offset = 0) - { - *length = sizeof(int); - return *((int*)(buffer + offset)); - } + void append(const void* data, size_t s) + { + if (data == nullptr) + return; + char* buf = buffer; + buffer = new char[size + s]; + memcpy(buffer, buf, size); + memcpy(buffer + size, data, s); + delete[] buf; + size += s; + return; + } - float grabFloat(int* length, int offset = 0) - { - *length = sizeof(float); - return *((float*)(buffer + offset)); - } + /* grab and create a string */ + char* grabString(unsigned int* length, int offset = 0) + { + int l = offset; + for (; l < size; ++l) + { + if (buffer[l] == 0) + break; + } + *length = l - offset + 1; + char* str = (char*)malloc(*length); + memcpy(str, buffer + offset, *length); + return str; + } - bool grabBoolean(int* length, int offset = 0) - { - *length = sizeof(bool); - return *((bool*)(buffer + offset)); - } + int grabInteger(int* length, int offset = 0) + { + *length = sizeof(int); + return *((int*)(buffer + offset)); + } + + float grabFloat(int* length, int offset = 0) + { + *length = sizeof(float); + return *((float*)(buffer + offset)); + } + + bool grabBoolean(int* length, int offset = 0) + { + *length = sizeof(bool); + return *((bool*)(buffer + offset)); + } - char* buffer; - size_t size; + char* buffer; + size_t size; - }; + }; -} // net -} // lua -} // jin + } // Net + } // Lua +} // JinEngine #endif
\ No newline at end of file diff --git a/src/lua/modules/net/net.cpp b/src/lua/modules/net/net.cpp index 37a5862..cc2b8de 100644 --- a/src/lua/modules/net/net.cpp +++ b/src/lua/modules/net/net.cpp @@ -4,12 +4,12 @@ #include "lua/common/common.h" #include "Buffer.h" -namespace jin +namespace JinEngine { -namespace lua +namespace Lua { - using namespace jin::lua::net; + using namespace JinEngine::Lua::Net; using namespace JinEngine::Net; static int l_initNetwork(lua_State* L) @@ -57,7 +57,7 @@ namespace lua { int size = luax_checkinteger(L, 1); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); - net::Buffer* buffer = new net::Buffer(size); + Net::Buffer* buffer = new Net::Buffer(size); proxy->bind(new Ref<Buffer>(buffer, JIN_NETWORK_BUFFER)); return 1; } @@ -82,5 +82,5 @@ namespace lua return 1; } -} // lua -} // jin
\ No newline at end of file +} // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/net/socket.cpp b/src/lua/modules/net/socket.cpp index 4f67497..9164073 100644 --- a/src/lua/modules/net/socket.cpp +++ b/src/lua/modules/net/socket.cpp @@ -4,126 +4,126 @@ #include "libjin/jin.h" #include "Buffer.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Net; - using namespace lua::net; + using namespace JinEngine::Net; + using namespace Lua::Net; - typedef Ref<Socket>& SocketRef; + typedef Ref<Socket>& SocketRef; - const int BUFFER_SIZE = 1024; + const int BUFFER_SIZE = 1024; - static inline SocketRef checkSocket(lua_State* L, int pos = 1) - { - Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); - return proxy->getRef<Socket>(); - } + static inline SocketRef checkSocket(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); + return proxy->getRef<Socket>(); + } - static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1) - { - Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); - return proxy->getRef<Buffer>(); - } + static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1) + { + Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); + return proxy->getRef<Buffer>(); + } - // return net.Socket - static int l_accept(lua_State* L) - { - SocketRef socket = checkSocket(L); - Socket* client = socket->accept(); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(new Ref<Socket>(client, JIN_NETWORK_SOCKET)); - return 1; - } + // return net.Socket + static int l_accept(lua_State* L) + { + SocketRef socket = checkSocket(L); + Socket* client = socket->accept(); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); + proxy->bind(new Ref<Socket>(client, JIN_NETWORK_SOCKET)); + return 1; + } - // return net.Buffer - static int l_receive(lua_State* L) - { - SocketRef 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER)); - return 1; - } + // return net.Buffer + static int l_receive(lua_State* L) + { + SocketRef 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } - // Socket:receiveFrom(address, port) - static int l_receiveFrom(lua_State* L) - { - SocketRef 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER)); - return 1; - } + // Socket:receiveFrom(address, port) + static int l_receiveFrom(lua_State* L) + { + SocketRef 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<Buffer>(netBuffer, JIN_NETWORK_BUFFER)); + return 1; + } - // Socket:send(net.Buffer) -> data_length - static int l_send(lua_State* L) - { - SocketRef socket = checkSocket(L); - Ref<Buffer>& ref = checkNetBuffer(L, 2); - int len = socket->send(ref->buffer, ref->size); - luax_pushinteger(L, len); - return 1; - } + // Socket:send(net.Buffer) -> data_length + static int l_send(lua_State* L) + { + SocketRef socket = checkSocket(L); + Ref<Buffer>& 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) - { - SocketRef socket = checkSocket(L); - int address = luax_checkinteger(L, 2); - int port = luax_checkinteger(L, 3); - Ref<Buffer>& buffer = checkNetBuffer(L, 4); - socket->sendTo(buffer->buffer, buffer->size, address, port); - return 0; - } + // Socket:sendTo(address, port, net.Buffer) + static int l_sendTo(lua_State* L) + { + SocketRef socket = checkSocket(L); + int address = luax_checkinteger(L, 2); + int port = luax_checkinteger(L, 3); + Ref<Buffer>& buffer = checkNetBuffer(L, 4); + socket->sendTo(buffer->buffer, buffer->size, address, port); + return 0; + } - static int l_close(lua_State* L) - { - SocketRef socket = checkSocket(L); - socket->close(); - return 0; - } + static int l_close(lua_State* L) + { + SocketRef socket = checkSocket(L); + socket->close(); + return 0; + } - static int l_configBlocking(lua_State* L) - { - SocketRef socket = checkSocket(L); - bool blocking = luax_checkbool(L, 2); - socket->configureBlocking(blocking); - return 0; - } + static int l_configBlocking(lua_State* L) + { + SocketRef 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 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 } - }; + 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; - } + int luaopen_Socket(lua_State* L) + { + luax_newtype(L, JIN_NETWORK_SOCKET, socket_function); + return 0; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/thread/Thread.cpp b/src/lua/modules/thread/Thread.cpp index 43c5903..b0cbf8e 100644 --- a/src/lua/modules/thread/Thread.cpp +++ b/src/lua/modules/thread/Thread.cpp @@ -5,248 +5,248 @@ #include "lua/common/common.h" #include "thread.h" -namespace jin +namespace JinEngine { -namespace lua -{ - - using thread::Thread; - - typedef Ref<Thread>& ThreadRef; - - int luaopen_thread(lua_State* L); - - static inline ThreadRef checkThread(lua_State* L) + namespace Lua { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - return proxy->getRef<Thread>(); - } - static int threadRunner(void* t) - { - ThreadRef ref = *(Ref<Thread>*)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; - } + using thread::Thread; - static int l_start(lua_State* L) - { - ThreadRef ref = checkThread(L); - bool result = ref->start(&ref); - luax_pushboolean(L, result); - return 1; - } + typedef Ref<Thread>& ThreadRef; - static int l_wait(lua_State* L) - { - ThreadRef ref = checkThread(L); - ref->wait(); - return 0; - } + int luaopen_thread(lua_State* L); - static int l_send(lua_State* L) - { - ThreadRef ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - const int vp = 3; - if (luax_isnumberstrict(L, vp)) + static inline ThreadRef checkThread(lua_State* L) { - float real = luax_checknumber(L, vp); - ref->send(slot, real); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + return proxy->getRef<Thread>(); } - else if (luax_isbooleanstrict(L, vp)) + + static int threadRunner(void* t) { - bool bol = luax_checkbool(L, vp); - ref->send(slot, bol); + ThreadRef ref = *(Ref<Thread>*)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; } - else if (luax_isstringstrict(L, vp)) + + static int l_thread_gc(lua_State* L) { - const char* str = luax_checkstring(L, vp); - ref->send(slot, str); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); + proxy->release(); + return 0; } - else if (luax_isuserdata(L, vp)) + + static int l_start(lua_State* L) { - void* p = luax_touserdata(L, vp); - ref->send(slot, p); + ThreadRef ref = checkThread(L); + bool result = ref->start(&ref); + luax_pushboolean(L, result); + return 1; } - else if (luax_islightuserdata(L, vp)) + + static int l_wait(lua_State* L) { - void* p = luax_tolightuserdata(L, vp); - ref->send(slot, p); + ThreadRef ref = checkThread(L); + ref->wait(); + return 0; } - return 0; - } - static int l_receive(lua_State* L) - { - ThreadRef 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) - { - ThreadRef ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = ref->fetch(slot); - switch (v.type) + static int l_send(lua_State* L) { - 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; + ThreadRef 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; + } - 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; + static int l_receive(lua_State* L) + { + ThreadRef 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) + { + ThreadRef 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; } - return 1; - } - static int l_demand(lua_State* L) - { - ThreadRef ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - Thread::Variant v = ref->demand(slot); - switch (v.type) + static int l_demand(lua_State* L) { - case thread::Thread::Variant::INTERGER: - luax_pushinteger(L, v.integer); - break; + ThreadRef 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; + const char* objType = p->getObjectType(); + Proxy* proxy = (Proxy*)luax_newinstance(L, objType, sizeof(Proxy)); + p->reference->retain(); + proxy->bind(p->reference); + break; + + } + return 1; + } - case thread::Thread::Variant::BOOLEAN: - luax_pushboolean(L, v.boolean); - break; + static int l_remove(lua_State* L) + { + ThreadRef ref = checkThread(L); + int slot = luax_checkinteger(L, 2); + ref->remove(slot); + return 0; + } - case thread::Thread::Variant::CSTRING: - luax_pushstring(L, v.cstring); - break; + static int l_getName(lua_State* L) + { + ThreadRef ref = checkThread(L); + const char* name = ref->getName(); + luax_pushstring(L, name); + return 1; + } - case thread::Thread::Variant::REAL: - luax_pushnumber(L, v.real); - break; + static int l_isRunning(lua_State* L) + { + ThreadRef ref = checkThread(L); + bool running = ref->isRunning(); + luax_pushboolean(L, running); + return 1; + } - case thread::Thread::Variant::POINTER: - Proxy* p = (Proxy*)v.pointer; - const char* objType = p->getObjectType(); - Proxy* proxy = (Proxy*)luax_newinstance(L, objType, sizeof(Proxy)); - p->reference->retain(); - proxy->bind(p->reference); - break; + 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>(thread, JIN_THREAD_THREAD)); + return 1; } - return 1; - } - - static int l_remove(lua_State* L) - { - ThreadRef ref = checkThread(L); - int slot = luax_checkinteger(L, 2); - ref->remove(slot); - return 0; - } - static int l_getName(lua_State* L) - { - ThreadRef ref = checkThread(L); - const char* name = ref->getName(); - luax_pushstring(L, name); - return 1; - } + static int l_getThread(lua_State* L) + { + luax_getglobal(L, MODULE_NAME); + luax_getfield(L, -1, "_curThread"); + return 1; + } - static int l_isRunning(lua_State* L) - { - ThreadRef 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); + static const luaL_Reg f[] = { + { "newThread", l_newThread }, + { "getThread", l_getThread }, + { 0, 0 } + }; - 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>(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); + int luaopen_thread(lua_State* L) + { + luaopen_Thread(L); - luax_newlib(L, f); + luax_newlib(L, f); - return 1; - } + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/thread/Thread.h b/src/lua/modules/thread/Thread.h index 9c375f0..2ccadc2 100644 --- a/src/lua/modules/thread/Thread.h +++ b/src/lua/modules/thread/Thread.h @@ -1,94 +1,94 @@ #include "libjin/jin.h" #include "lua/common/common.h" -namespace jin +namespace JinEngine { -namespace lua -{ -namespace thread -{ - - class Thread + namespace Lua { - public: - typedef JinEngine::MultiThread::Thread::Variant Variant; - typedef JinEngine::MultiThread::Thread::ThreadRunner ThreadRunner; - - Thread(std::string _name, std::string _code, ThreadRunner runner) - : name(_name) - , code(_code) - { - thread = new JinEngine::MultiThread::Thread(_name, runner); - } - - ~Thread() - { - delete thread; - } - - bool start(void* p) - { - return thread->start(p); - } - - void wait() - { - thread->wait(); - } - - void send(int slot, const Variant& value) - { - thread->send(slot, value); - } - - bool receive(int slot) - { - return thread->receive(slot); - } - - Variant fetch(int slot) + namespace thread { - return thread->fetch(slot); - } - - Variant demand(int slot) - { - return thread->demand(slot); - } - - void remove(int slot) - { - thread->remove(slot); - } - - const char* getName() - { - return name.c_str(); - } - - bool isRunning() - { - return thread->isRunning(); - } - - void lock() - { - thread->lock(); - } - - void unlock() - { - thread->unlock(); - } - - const std::string name; - const std::string code; - - private: - JinEngine::MultiThread::Thread* thread; - - }; -} // thread -} // lua -} // jin
\ No newline at end of file + class Thread + { + public: + typedef JinEngine::MultiThread::Thread::Variant Variant; + typedef JinEngine::MultiThread::Thread::ThreadRunner ThreadRunner; + + Thread(std::string _name, std::string _code, ThreadRunner runner) + : name(_name) + , code(_code) + { + thread = new JinEngine::MultiThread::Thread(_name, runner); + } + + ~Thread() + { + delete thread; + } + + bool start(void* p) + { + return thread->start(p); + } + + void wait() + { + thread->wait(); + } + + void send(int slot, const Variant& value) + { + thread->send(slot, value); + } + + bool receive(int slot) + { + return thread->receive(slot); + } + + Variant fetch(int slot) + { + return thread->fetch(slot); + } + + Variant demand(int slot) + { + return thread->demand(slot); + } + + void remove(int slot) + { + thread->remove(slot); + } + + const char* getName() + { + return name.c_str(); + } + + bool isRunning() + { + return thread->isRunning(); + } + + void lock() + { + thread->lock(); + } + + void unlock() + { + thread->unlock(); + } + + const std::string name; + const std::string code; + + private: + JinEngine::MultiThread::Thread* thread; + + }; + + } // thread + } // Lua +} // JinEngine
\ No newline at end of file diff --git a/src/lua/modules/time/time.cpp b/src/lua/modules/time/time.cpp index 982ce0c..ec4d6b9 100644 --- a/src/lua/modules/time/time.cpp +++ b/src/lua/modules/time/time.cpp @@ -2,37 +2,37 @@ #include <SDL2/SDL.h> #include "libjin/jin.h" -namespace jin -{ -namespace lua +namespace JinEngine { + namespace Lua + { - using namespace JinEngine::Time; + using namespace JinEngine::Time; - static int l_sec(lua_State* L) - { - luax_pushnumber(L, getSecond()); - return 1; - } + 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 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 }, - }; + 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; - } + int luaopen_time(lua_State* L) + { + luax_newlib(L, f); + return 1; + } -} // lua -} // jin
\ No newline at end of file + } // Lua +} // JinEngine
\ No newline at end of file |