From e92caa97329016d012b46b9d37e1b2c3b613a8f2 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 10 Nov 2018 20:13:55 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Graphics/particles/je_particle_emitter.cpp | 4 +- .../Graphics/particles/je_particle_emitter.h | 2 +- src/libjin/Math/je_random.cpp | 2 - src/libjin/ai/je_state_machine.h | 13 +- src/lua/common/je_lua_common.h | 2 +- src/lua/common/je_lua_proxy.h | 30 ++--- src/lua/common/je_lua_reference.hpp | 88 ------------- src/lua/common/je_lua_shared.hpp | 99 +++++++++++++++ src/lua/embed/boot.lua.h | 138 -------------------- src/lua/embed/embed.h | 8 +- src/lua/embed/graphics.lua.h | 53 -------- src/lua/embed/keyboard.lua.h | 15 --- src/lua/embed/mouse.lua.h | 14 --- src/lua/embed/net.lua.h | 4 - src/lua/embed/path.lua.h | 14 --- src/lua/embed/scripts/ai.lua.h | 26 ++++ src/lua/embed/scripts/boot.lua.h | 139 +++++++++++++++++++++ src/lua/embed/scripts/graphics.lua.h | 61 +++++++++ src/lua/embed/scripts/keyboard.lua.h | 17 +++ src/lua/embed/scripts/mouse.lua.h | 16 +++ src/lua/embed/scripts/net.lua.h | 7 ++ src/lua/embed/scripts/path.lua.h | 16 +++ src/lua/jin.cpp | 2 +- src/lua/libraries/luax/luax.h | 6 +- src/lua/modules/ai/je_lua_state_machine.cpp | 23 ++++ src/lua/modules/ai/je_lua_statemachine.cpp | 11 -- src/lua/modules/audio/je_lua_audio.cpp | 2 +- src/lua/modules/audio/je_lua_source.cpp | 4 +- src/lua/modules/graphics/je_lua_bitmap.cpp | 6 +- src/lua/modules/graphics/je_lua_canvas.cpp | 4 +- src/lua/modules/graphics/je_lua_graphics.cpp | 60 ++++----- src/lua/modules/graphics/je_lua_page.cpp | 6 +- src/lua/modules/graphics/je_lua_shader.cpp | 8 +- src/lua/modules/graphics/je_lua_texture.cpp | 4 +- src/lua/modules/graphics/je_lua_texture_font.cpp | 4 +- src/lua/modules/graphics/je_lua_ttf.cpp | 8 +- src/lua/modules/graphics/je_lua_ttf_data.cpp | 6 +- src/lua/modules/net/je_lua_buffer.cpp | 4 +- src/lua/modules/net/je_lua_net.cpp | 4 +- src/lua/modules/net/je_lua_socket.cpp | 18 +-- src/lua/modules/thread/je_lua_thread.cpp | 16 +-- src/lua/modules/time/je_lua_timer.cpp | 30 +++++ src/lua/modules/time/je_lua_timer.h | 19 +++ src/lua/modules/types.h | 3 + 44 files changed, 557 insertions(+), 459 deletions(-) delete mode 100644 src/lua/common/je_lua_reference.hpp create mode 100644 src/lua/common/je_lua_shared.hpp delete mode 100644 src/lua/embed/boot.lua.h delete mode 100644 src/lua/embed/graphics.lua.h delete mode 100644 src/lua/embed/keyboard.lua.h delete mode 100644 src/lua/embed/mouse.lua.h delete mode 100644 src/lua/embed/net.lua.h delete mode 100644 src/lua/embed/path.lua.h create mode 100644 src/lua/embed/scripts/ai.lua.h create mode 100644 src/lua/embed/scripts/boot.lua.h create mode 100644 src/lua/embed/scripts/graphics.lua.h create mode 100644 src/lua/embed/scripts/keyboard.lua.h create mode 100644 src/lua/embed/scripts/mouse.lua.h create mode 100644 src/lua/embed/scripts/net.lua.h create mode 100644 src/lua/embed/scripts/path.lua.h create mode 100644 src/lua/modules/ai/je_lua_state_machine.cpp delete mode 100644 src/lua/modules/ai/je_lua_statemachine.cpp create mode 100644 src/lua/modules/time/je_lua_timer.cpp create mode 100644 src/lua/modules/time/je_lua_timer.h (limited to 'src') diff --git a/src/libjin/Graphics/particles/je_particle_emitter.cpp b/src/libjin/Graphics/particles/je_particle_emitter.cpp index 63d3393..edc4bee 100644 --- a/src/libjin/Graphics/particles/je_particle_emitter.cpp +++ b/src/libjin/Graphics/particles/je_particle_emitter.cpp @@ -31,9 +31,9 @@ namespace JinEngine mTime -= 1; } - void ParticleEmitter::emit(const ParticleEmitterDef& emitterDef, const ParticleDef& def, Particle& particle) + void ParticleEmitter::emit() { - particle.reset(); + } diff --git a/src/libjin/Graphics/particles/je_particle_emitter.h b/src/libjin/Graphics/particles/je_particle_emitter.h index 34d0ee4..9200532 100644 --- a/src/libjin/Graphics/particles/je_particle_emitter.h +++ b/src/libjin/Graphics/particles/je_particle_emitter.h @@ -96,7 +96,7 @@ namespace JinEngine /// Emit a particle according to emitter definition and particle definition, particle system should /// assign particle value to the particle in particle pool, but not use this return particle. /// - void emit(const ParticleEmitterDef& emitterDef, const ParticleDef& def, Particle& particle); + void emit(); /// /// diff --git a/src/libjin/Math/je_random.cpp b/src/libjin/Math/je_random.cpp index a8f3310..983fa36 100644 --- a/src/libjin/Math/je_random.cpp +++ b/src/libjin/Math/je_random.cpp @@ -5,8 +5,6 @@ namespace JinEngine namespace Math { - - RandomGenerator::RandomGenerator() { } diff --git a/src/libjin/ai/je_state_machine.h b/src/libjin/ai/je_state_machine.h index d496c61..6cd3f17 100644 --- a/src/libjin/ai/je_state_machine.h +++ b/src/libjin/ai/je_state_machine.h @@ -411,15 +411,4 @@ namespace JinEngine #endif // jin_ai -#endif - -//local sp = jin.graphics.newSprite() -//local sm = jin.ai.newStateMachine(jin.StateMachineMode.STEPWISE, sp) -//sm:addState("run") -//sm:addEnterCallback("run", function(spr) -// spr:setRun() -//end) -// -//function jin.core.onUpdate(dt) -// sm:update() -//end +#endif \ No newline at end of file diff --git a/src/lua/common/je_lua_common.h b/src/lua/common/je_lua_common.h index 1d772a5..82c550a 100644 --- a/src/lua/common/je_lua_common.h +++ b/src/lua/common/je_lua_common.h @@ -3,7 +3,7 @@ #include "je_lua_port.h" #include "je_lua_proxy.h" -#include "je_lua_reference.hpp" +#include "je_lua_shared.hpp" #include "je_lua_error.h" #endif \ No newline at end of file diff --git a/src/lua/common/je_lua_proxy.h b/src/lua/common/je_lua_proxy.h index b428dc9..56e5fee 100644 --- a/src/lua/common/je_lua_proxy.h +++ b/src/lua/common/je_lua_proxy.h @@ -1,7 +1,7 @@ #ifndef __JIN_COMMON_PROXY_H #define __JIN_COMMON_PROXY_H -#include "je_lua_reference.hpp" +#include "je_lua_shared.hpp" namespace JinEngine { @@ -11,53 +11,53 @@ namespace JinEngine class Proxy { public: - void bind(RefBase* ref) + void bind(SharedBase* ref) { if (ref == nullptr) return; - reference = ref; + shared = ref; } void release() { - if (reference != nullptr) + if (shared != nullptr) { - reference->release(); - reference = nullptr; + shared->release(); + shared = nullptr; } } void retain() { - if (reference != nullptr) - reference->retain(); + if (shared != nullptr) + shared->retain(); } void setUserdata(void* data) { - if (reference != nullptr) - reference->setUserdata(data); + if (shared != nullptr) + shared->setUserdata(data); } template - Ref& getRef() + Shared& getShared() { - return *(Ref*) reference; + return *(static_cast*>(shared)); } template T* getObject() { - Ref& ref = getRef(); + Shared& ref = getShared(); return ref.getObject(); } const char* getObjectType() { - return reference->type; + return shared->type; } - RefBase* reference; + SharedBase* shared; }; diff --git a/src/lua/common/je_lua_reference.hpp b/src/lua/common/je_lua_reference.hpp deleted file mode 100644 index ba918bb..0000000 --- a/src/lua/common/je_lua_reference.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef __JIN_COMMON_REFERENCE_H -#define __JIN_COMMON_REFERENCE_H - -namespace JinEngine -{ - namespace Lua - { - - /*abstract*/class RefBase - { - public: - void retain() - { - ++count; - } - - void release() - { - if (--count <= 0) - delete this; - } - - // object type string - const char* const type; - - void setUserdata(void* data) - { - userdata = data; - } - - void* getUserdata() - { - return userdata; - } - - protected: - RefBase(void* obj, const char* t) - : count(1) - , object(obj) - , type(t) - { - } - - RefBase(const RefBase&); - - virtual ~RefBase() - { - } - - void* object; - int count; - void* userdata; - }; - - template - class Ref : public RefBase - { - public: - Ref(T* obj, const char* type) - : RefBase(obj, type) - { - } - - ~Ref() - { - T* obj = static_cast(object); - delete obj; - } - - T* operator->() - { - return (T*)object; - } - - T* getObject() - { - return (T*)object; - } - - private: - Ref(const Ref& ref); - - }; - - } -} - -#endif \ No newline at end of file diff --git a/src/lua/common/je_lua_shared.hpp b/src/lua/common/je_lua_shared.hpp new file mode 100644 index 0000000..7ad7c1d --- /dev/null +++ b/src/lua/common/je_lua_shared.hpp @@ -0,0 +1,99 @@ +#ifndef __JIN_COMMON_REFERENCE_H +#define __JIN_COMMON_REFERENCE_H + +namespace JinEngine +{ + namespace Lua + { + + /*abstract*/class SharedBase + { + public: + void retain() + { + ++(*count); + } + + void release() + { + if (--(*count) <= 0) + delete this; + } + + // object type string + const char* const type; + + void setUserdata(void* data) + { + userdata = data; + } + + void* getUserdata() + { + return userdata; + } + + protected: + SharedBase(void* obj, const char* t) + : object(obj) + , type(t) + { + count = new int(1); + } + + SharedBase(const SharedBase&); + + virtual ~SharedBase() + { + + } + + void* object; + int* count; + void* userdata; + }; + + template + class Shared : public SharedBase + { + public: + Shared(T* obj, const char* type) + : SharedBase(obj, type) + { + } + + ~Shared() + { + T* obj = static_cast(object); + delete obj; + } + + T* operator->() + { + return static_cast(object); + } + + T* getObject() + { + return static_cast(object); + } + + T* operator &() + { + return static_cast(object); + } + + T& operator *() + { + return *(static_cast(object)); + } + + private: + Shared(const Shared& ref); + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h deleted file mode 100644 index 0884188..0000000 --- a/src/lua/embed/boot.lua.h +++ /dev/null @@ -1,138 +0,0 @@ -/* boot.lua */ -static const char* boot_lua = R"( -local cwd = jin.args['cwd'] or '.' -jin.filesystem.init() -jin.filesystem.mount(cwd) - -------------------------------------------------------------------------- --- Config game -------------------------------------------------------------------------- - -jin.config = {} -if jin.filesystem.exist("config.lua") then - xpcall(function()jin.config = require "config" end, function()end) -end -jin.config.width = jin.config.width or 580 -jin.config.height = jin.config.height or 450 -jin.config.vsync = jin.config.vsync or true -jin.config.title = jin.config.title or ("jin v" .. jin.version) -jin.config.resizable = jin.config.resizable or false -jin.config.fullscreen = jin.config.fullscreen or false -jin.config.fps = jin.config.fps or 60 -jin.config.icon = jin.config.icon or "" - -------------------------------------------------------------------------- --- Default game loop -------------------------------------------------------------------------- - -local function call(func, ...) - if func then - return func(...) - end -end - -function jin.core.run() - jin.graphics.reset() - call(jin.core.onLoad) - local dt = 0 - local previous = jin.time.second() - local current = previous - while jin.core.running() do - for _, e in pairs(jin.event.poll()) do - if e.type == "KeyDown" then - jin.keyboard.set(e.key, true) - elseif e.type == "KeyUp" then - jin.keyboard.set(e.key, false) - end - call(jin.core.onEvent, e) - end - previous = current - current = jin.time.second() - dt = current - previous - call(jin.core.onUpdate, dt) - jin.graphics.clear() - call(jin.core.onDraw) - jin.graphics.present() - jin.time.sleep(0.001) - end -end - -------------------------------------------------------------------------- --- Boot game -------------------------------------------------------------------------- - --- Display error message. -local function onError(msg) - jin.graphics.showWindow() - local err = "Error:\n" .. msg .. "\n" .. debug.traceback() - jin.graphics.reset() - jin.graphics.setClearColor(100, 100, 100, 255) - jin.graphics.clear() - jin.graphics.print(err, 5, 5) - jin.graphics.present() - while jin.core.running() do - for _, e in pairs(jin.event.poll()) do - if e.type == "Quit" then - jin.core.stop() - end - end - jin.time.sleep(0.001) - end -end - --- No game screen. -local function noGame() - jin.graphics.showWindow() - jin.graphics.reset() - jin.graphics.setClearColor(100, 100, 100, 255) - jin.graphics.clear() - jin.graphics.print("No Game", 5, 5) - jin.graphics.present() - while jin.core.running() do - for _, e in pairs(jin.event.poll()) do - if e.type == "Quit" then - jin.core.stop() - end - end - jin.time.sleep(0.001) - end -end - -local function boot() - if jin.filesystem.exist("main.lua") then - call(function() - require"main" - jin.core.run() - end) - else - noGame() - end -end - -------------------------------------------------------------------------- --- Initialize sub systems -------------------------------------------------------------------------- - -jin.audio.init() -jin.graphics.init(jin.config) - -------------------------------------------------------------------------- --- Boot game -------------------------------------------------------------------------- - -xpcall(boot, onError) - -------------------------------------------------------------------------- --- Destroy sub-systems -------------------------------------------------------------------------- - -jin.graphics.destroy() -jin.audio.destroy() - -------------------------------------------------------------------------- --- Quit game -------------------------------------------------------------------------- - -jin.core.quit() - -)"; \ No newline at end of file diff --git a/src/lua/embed/embed.h b/src/lua/embed/embed.h index 1efbc95..054672c 100644 --- a/src/lua/embed/embed.h +++ b/src/lua/embed/embed.h @@ -18,10 +18,10 @@ namespace JinEngine }; // Embed scripts. - #include "graphics.lua.h" - #include "keyboard.lua.h" - #include "mouse.lua.h" - #include "boot.lua.h" + #include "scripts/graphics.lua.h" + #include "scripts/keyboard.lua.h" + #include "scripts/mouse.lua.h" + #include "scripts/boot.lua.h" // In order. const jin_Embed scripts[] = { diff --git a/src/lua/embed/graphics.lua.h b/src/lua/embed/graphics.lua.h deleted file mode 100644 index b2a19b5..0000000 --- a/src/lua/embed/graphics.lua.h +++ /dev/null @@ -1,53 +0,0 @@ -/* graphics.lua */ -static const char* graphics_lua = R"( -jin.graphics = jin.graphics or {} - -local default_shader = nil -local default_shader_source = [[ -#VERTEX_SHADER - -Vertex vert(Vertex v) -{ - return v; -} - -#END_VERTEX_SHADER - -#FRAGMENT_SHADER - -Color frag(Color col, Texture tex, Vertex v) -{ - return col * texel(tex, v.uv); -} - -#END_FRAGMENT_SHADER -]] - -local _init = jin.graphics.init -local initialized = false - -jin.graphics.init = function(setting) - if initialized then - return initialized - end - initialized = _init(setting) - if initialized then - default_shader = jin.graphics.newShader(default_shader_source) - jin.graphics.useShader(default_shader) - end - return initialized -end - -jin.graphics.unuseShader = function() - jin.graphics.useShader(default_shader) -end - --- Reset all attributes to default value. -jin.graphics.reset = function() - jin.graphics.setColor(255, 255, 255, 255) - jin.graphics.setClearColor(0, 0, 0, 255) - jin.graphics.clear() - jin.graphics.unsetFont() -end - -)"; diff --git a/src/lua/embed/keyboard.lua.h b/src/lua/embed/keyboard.lua.h deleted file mode 100644 index ee8428f..0000000 --- a/src/lua/embed/keyboard.lua.h +++ /dev/null @@ -1,15 +0,0 @@ - -static const char* keyboard_lua = R"( -jin.keyboard = jin.keyboard or {} - -local keys = {} - -function jin.keyboard.isPressed(k) - return keys[k] -end - -function jin.keyboard.set(k, status) - keys[k] = status -end - -)"; diff --git a/src/lua/embed/mouse.lua.h b/src/lua/embed/mouse.lua.h deleted file mode 100644 index 3c222f3..0000000 --- a/src/lua/embed/mouse.lua.h +++ /dev/null @@ -1,14 +0,0 @@ -static const char* mouse_lua = R"( -jin.mouse = jin.mouse or {} - -local button = {} - -function jin.mouse.isDown(btn) - return button[btn] -end - -function jin.mouse.set(btn, status) - button[btn] = status -end - -)"; \ No newline at end of file diff --git a/src/lua/embed/net.lua.h b/src/lua/embed/net.lua.h deleted file mode 100644 index 4d89dc7..0000000 --- a/src/lua/embed/net.lua.h +++ /dev/null @@ -1,4 +0,0 @@ -/* net.lua */ -static const char* net_lua = R"( -jin.net = jin.net or {} -)"; \ No newline at end of file diff --git a/src/lua/embed/path.lua.h b/src/lua/embed/path.lua.h deleted file mode 100644 index 648adf8..0000000 --- a/src/lua/embed/path.lua.h +++ /dev/null @@ -1,14 +0,0 @@ -/* path.lua */ -static const char* path_lua = R"( -jin.path = jin.path or {} - --- game root directory -jin._root = nil - --- return full path of a given path -function jin.path.full(path) - local root = jin._dir .. '/' .. jin._argv[2] - return root .. '/' .. path -end - -)"; \ No newline at end of file diff --git a/src/lua/embed/scripts/ai.lua.h b/src/lua/embed/scripts/ai.lua.h new file mode 100644 index 0000000..a69da84 --- /dev/null +++ b/src/lua/embed/scripts/ai.lua.h @@ -0,0 +1,26 @@ +/* graphics.lua */ +static const char* ai_lua = R"( +jin.ai = jin.ai or {} + +local ja = jin.ai + +ja.StateMachineType = { + STEPWISE = 1, + ITERATIVE = 2, +} + + + +)"; + + +//local sp = jin.graphics.newSprite() +//local sm = jin.ai.newStateMachine(jin.StateMachineMode.STEPWISE, sp) +//sm:addState("run") +//sm:addEnterCallback("run", function(spr) +// spr:setRun() +//end) +// +//function jin.core.onUpdate(dt) +// sm:update() +//end diff --git a/src/lua/embed/scripts/boot.lua.h b/src/lua/embed/scripts/boot.lua.h new file mode 100644 index 0000000..cffeca6 --- /dev/null +++ b/src/lua/embed/scripts/boot.lua.h @@ -0,0 +1,139 @@ +/* boot.lua */ +static const char* boot_lua = R"( +local cwd = jin.args['cwd'] or '.' +jin.filesystem.init() +jin.filesystem.mount(cwd) + +------------------------------------------------------------------------- +-- Config game +------------------------------------------------------------------------- + +jin.config = {} +if jin.filesystem.exist("config.lua") then + xpcall(function()jin.config = require "config" end, function()end) +end +jin.config.width = jin.config.width or 580 +jin.config.height = jin.config.height or 450 +jin.config.vsync = jin.config.vsync or true +jin.config.title = jin.config.title or ("jin v" .. jin.version) +jin.config.resizable = jin.config.resizable or false +jin.config.fullscreen = jin.config.fullscreen or false +jin.config.fps = jin.config.fps or 60 +jin.config.icon = jin.config.icon or "" + +------------------------------------------------------------------------- +-- Default game loop +------------------------------------------------------------------------- + +local function call(func, ...) + if func then + return func(...) + end +end + +function jin.core.run() + jin.graphics.reset() + call(jin.core.onLoad) + local dt = 0 + local previous = jin.time.second() + local current = previous + while jin.core.running() do + for _, e in pairs(jin.event.poll()) do + if e.type == "KeyDown" then + jin.keyboard.set(e.key, true) + elseif e.type == "KeyUp" then + jin.keyboard.set(e.key, false) + end + call(jin.core.onEvent, e) + end + previous = current + current = jin.time.second() + dt = current - previous + call(jin.core.onUpdate, dt) + jin.graphics.clear() + call(jin.core.onDraw) + jin.graphics.present() + jin.time.sleep(0.001) + end +end + +------------------------------------------------------------------------- +-- Boot game +------------------------------------------------------------------------- + +-- Display error message. +local function onError(msg) + jin.audio.destroy() + jin.graphics.showWindow() + local err = "Error:\n" .. msg .. "\n" .. debug.traceback() + jin.graphics.reset() + jin.graphics.setClearColor(100, 100, 100, 255) + jin.graphics.clear() + jin.graphics.print(err, 5, 5) + jin.graphics.present() + while jin.core.running() do + for _, e in pairs(jin.event.poll()) do + if e.type == "Quit" then + jin.core.stop() + end + end + jin.time.sleep(0.001) + end +end + +-- No game screen. +local function noGame() + jin.graphics.showWindow() + jin.graphics.reset() + jin.graphics.setClearColor(100, 100, 100, 255) + jin.graphics.clear() + jin.graphics.print("No Game", 5, 5) + jin.graphics.present() + while jin.core.running() do + for _, e in pairs(jin.event.poll()) do + if e.type == "Quit" then + jin.core.stop() + end + end + jin.time.sleep(0.001) + end +end + +local function boot() + if jin.filesystem.exist("main.lua") then + call(function() + require"main" + jin.core.run() + end) + else + noGame() + end +end + +------------------------------------------------------------------------- +-- Initialize sub systems +------------------------------------------------------------------------- + +jin.audio.init() +jin.graphics.init(jin.config) + +------------------------------------------------------------------------- +-- Boot game +------------------------------------------------------------------------- + +xpcall(boot, onError) + +------------------------------------------------------------------------- +-- Destroy sub-systems +------------------------------------------------------------------------- + +jin.graphics.destroy() +jin.audio.destroy() + +------------------------------------------------------------------------- +-- Quit game +------------------------------------------------------------------------- + +jin.core.quit() + +)"; \ No newline at end of file diff --git a/src/lua/embed/scripts/graphics.lua.h b/src/lua/embed/scripts/graphics.lua.h new file mode 100644 index 0000000..751d029 --- /dev/null +++ b/src/lua/embed/scripts/graphics.lua.h @@ -0,0 +1,61 @@ +/* graphics.lua */ +static const char* graphics_lua = R"( +jin.graphics = jin.graphics or {} + +local jg = jin.graphics + +jg.RenderMode = { + FILL = 1, + LINE = 2, +} + +local default_shader = nil +local default_shader_source = [[ +#VERTEX_SHADER + +Vertex vert(Vertex v) +{ + return v; +} + +#END_VERTEX_SHADER + +#FRAGMENT_SHADER + +Color frag(Color col, Texture tex, Vertex v) +{ + return col * texel(tex, v.uv); +} + +#END_FRAGMENT_SHADER +]] + +local _init = jg.init +local initialized = false + +jg.init = function(setting) + if initialized then + return initialized + end + initialized = _init(setting) + if initialized then + default_shader = jg.newShader(default_shader_source) + jg.useShader(default_shader) + end + return initialized +end + +jg.unuseShader = function() + jg.useShader(default_shader) +end + +-- Reset all attributes to default value. +jg.reset = function() + jg.setColor(255, 255, 255, 255) + jg.setClearColor(0, 0, 0, 255) + jg.clear() + jg.unsetFont() + jg.unuseShader() +end + +)"; \ No newline at end of file diff --git a/src/lua/embed/scripts/keyboard.lua.h b/src/lua/embed/scripts/keyboard.lua.h new file mode 100644 index 0000000..e989928 --- /dev/null +++ b/src/lua/embed/scripts/keyboard.lua.h @@ -0,0 +1,17 @@ + +static const char* keyboard_lua = R"( +jin.keyboard = jin.keyboard or {} + +local jk = jin.keyboard + +local keys = {} + +function jin.keyboard.isPressed(k) + return keys[k] +end + +function jin.keyboard.set(k, status) + keys[k] = status +end + +)"; diff --git a/src/lua/embed/scripts/mouse.lua.h b/src/lua/embed/scripts/mouse.lua.h new file mode 100644 index 0000000..ca070a3 --- /dev/null +++ b/src/lua/embed/scripts/mouse.lua.h @@ -0,0 +1,16 @@ +static const char* mouse_lua = R"( +jin.mouse = jin.mouse or {} + +local jm = jin.mouse + +local button = {} + +function jin.mouse.isDown(btn) + return button[btn] +end + +function jin.mouse.set(btn, status) + button[btn] = status +end + +)"; \ No newline at end of file diff --git a/src/lua/embed/scripts/net.lua.h b/src/lua/embed/scripts/net.lua.h new file mode 100644 index 0000000..a986ce6 --- /dev/null +++ b/src/lua/embed/scripts/net.lua.h @@ -0,0 +1,7 @@ +/* net.lua */ +static const char* net_lua = R"( +jin.net = jin.net or {} + +local jn = jin.net + +)"; \ No newline at end of file diff --git a/src/lua/embed/scripts/path.lua.h b/src/lua/embed/scripts/path.lua.h new file mode 100644 index 0000000..f7e1ec3 --- /dev/null +++ b/src/lua/embed/scripts/path.lua.h @@ -0,0 +1,16 @@ +/* path.lua */ +static const char* path_lua = R"( +jin.path = jin.path or {} + +local jp = jin.path + +-- game root directory +jin._root = nil + +-- return full path of a given path +function jin.path.full(path) + local root = jin._dir .. '/' .. jin._argv[2] + return root .. '/' .. path +end + +)"; \ No newline at end of file diff --git a/src/lua/jin.cpp b/src/lua/jin.cpp index 26b5f04..186b9b2 100644 --- a/src/lua/jin.cpp +++ b/src/lua/jin.cpp @@ -65,7 +65,7 @@ namespace JinEngine }; /* sub modules */ - LUA_IMPLEMENT const luax_Ref mods[] = { + LUA_IMPLEMENT const luax_Reg mods[] = { { "core", luaopen_core }, { "event", luaopen_event }, { "graphics", luaopen_graphics }, diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h index be2e227..8a01a05 100644 --- a/src/lua/libraries/luax/luax.h +++ b/src/lua/libraries/luax/luax.h @@ -417,7 +417,11 @@ inline void luax_setfieldnumbers(lua_State* L, const luax_Num* strs) } } -typedef luaL_Reg luax_Ref; +typedef luaL_Reg luax_Reg; + +#define luax_ref luaL_ref + +#define luax_unref luaL_unref #endif // #if LUA_VERSION_NUM == 501 diff --git a/src/lua/modules/ai/je_lua_state_machine.cpp b/src/lua/modules/ai/je_lua_state_machine.cpp new file mode 100644 index 0000000..9b95f43 --- /dev/null +++ b/src/lua/modules/ai/je_lua_state_machine.cpp @@ -0,0 +1,23 @@ +#include "lua/common/je_lua_common.h" +#include "libjin/jin.h" + +using namespace JinEngine::AI; + +namespace JinEngine +{ + namespace Lua + { + + LUA_IMPLEMENT int l_addEnterCallback(lua_State* L) + { + StateMachine* sm; + sm->addEnterListener("", [](void* p) -> void{ + + + }); + + return 0; + } + + } +} \ No newline at end of file diff --git a/src/lua/modules/ai/je_lua_statemachine.cpp b/src/lua/modules/ai/je_lua_statemachine.cpp deleted file mode 100644 index f79d79a..0000000 --- a/src/lua/modules/ai/je_lua_statemachine.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "libjin/jin.h" - -namespace JinEngine -{ - namespace Lua - { - - - - } -} \ No newline at end of file diff --git a/src/lua/modules/audio/je_lua_audio.cpp b/src/lua/modules/audio/je_lua_audio.cpp index 698d88a..88c0561 100644 --- a/src/lua/modules/audio/je_lua_audio.cpp +++ b/src/lua/modules/audio/je_lua_audio.cpp @@ -98,7 +98,7 @@ namespace JinEngine return 1; } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); - proxy->bind(new Ref(src, JIN_AUDIO_SOURCE)); + proxy->bind(new Shared(src, JIN_AUDIO_SOURCE)); return 1; } diff --git a/src/lua/modules/audio/je_lua_source.cpp b/src/lua/modules/audio/je_lua_source.cpp index d75d942..04f0528 100644 --- a/src/lua/modules/audio/je_lua_source.cpp +++ b/src/lua/modules/audio/je_lua_source.cpp @@ -10,12 +10,12 @@ namespace JinEngine namespace Lua { - typedef Ref& SourceRef; + typedef Shared& SourceRef; LUA_IMPLEMENT inline SourceRef checkSource(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_AUDIO_SOURCE); - return proxy->getRef(); + return proxy->getShared(); } LUA_IMPLEMENT int l_play(lua_State* L) diff --git a/src/lua/modules/graphics/je_lua_bitmap.cpp b/src/lua/modules/graphics/je_lua_bitmap.cpp index a8d5bc0..8d4897b 100644 --- a/src/lua/modules/graphics/je_lua_bitmap.cpp +++ b/src/lua/modules/graphics/je_lua_bitmap.cpp @@ -10,12 +10,12 @@ namespace JinEngine namespace Lua { - typedef Ref& BitmapRef; + typedef Shared& BitmapRef; LUA_IMPLEMENT inline BitmapRef checkBitmap(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - return proxy->getRef(); + return proxy->getShared(); } LUA_IMPLEMENT int l_gc(lua_State* L) @@ -88,7 +88,7 @@ namespace JinEngine Bitmap* bitmap = ref.getObject(); Bitmap* b = Bitmap::clone(bitmap); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); - proxy->bind(new Ref(b, JIN_GRAPHICS_BITMAP)); + proxy->bind(new Shared(b, JIN_GRAPHICS_BITMAP)); return 1; } diff --git a/src/lua/modules/graphics/je_lua_canvas.cpp b/src/lua/modules/graphics/je_lua_canvas.cpp index f6834b1..87516b3 100644 --- a/src/lua/modules/graphics/je_lua_canvas.cpp +++ b/src/lua/modules/graphics/je_lua_canvas.cpp @@ -10,12 +10,12 @@ namespace JinEngine namespace Lua { - typedef Ref& CanvasRef; + typedef Shared& CanvasRef; LUA_IMPLEMENT inline CanvasRef checkCanvas(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - return proxy->getRef(); + return proxy->getShared(); } LUA_IMPLEMENT int l_getWidth(lua_State* L) diff --git a/src/lua/modules/graphics/je_lua_graphics.cpp b/src/lua/modules/graphics/je_lua_graphics.cpp index 3befdde..639a542 100644 --- a/src/lua/modules/graphics/je_lua_graphics.cpp +++ b/src/lua/modules/graphics/je_lua_graphics.cpp @@ -165,7 +165,7 @@ namespace JinEngine } } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); - proxy->bind(new Ref(bitmap, JIN_GRAPHICS_BITMAP)); + proxy->bind(new Shared(bitmap, JIN_GRAPHICS_BITMAP)); return 1; } @@ -176,7 +176,7 @@ namespace JinEngine if (luax_istype(L, 1, JIN_GRAPHICS_BITMAP)) { Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP); - Ref& refBitmap = p->getRef(); + Shared& refBitmap = p->getShared(); Bitmap* bitmap = refBitmap.getObject(); texture = Texture::createTexture(bitmap); } @@ -186,7 +186,7 @@ namespace JinEngine texture = Texture::createTexture(path); } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); - proxy->bind(new Ref(texture, JIN_GRAPHICS_TEXTURE)); + proxy->bind(new Shared(texture, JIN_GRAPHICS_TEXTURE)); return 1; } @@ -201,7 +201,7 @@ namespace JinEngine return 1; } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - proxy->bind(new Ref(jsl, JIN_GRAPHICS_SHADER)); + proxy->bind(new Shared(jsl, JIN_GRAPHICS_SHADER)); return 1; } @@ -225,7 +225,7 @@ namespace JinEngine return 1; } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); - proxy->bind(new Ref(jsl, JIN_GRAPHICS_SHADER)); + proxy->bind(new Shared(jsl, JIN_GRAPHICS_SHADER)); return 1; } @@ -235,7 +235,7 @@ namespace JinEngine int h = luax_checknumber(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_CANVAS, sizeof(Proxy)); Canvas* cvs = Canvas::createCanvas(w, h); - proxy->bind(new Ref(cvs, JIN_GRAPHICS_CANVAS)); + proxy->bind(new Shared(cvs, JIN_GRAPHICS_CANVAS)); return 1; } @@ -283,7 +283,7 @@ namespace JinEngine float ox = luax_optnumber(L, 7, 0); float oy = luax_optnumber(L, 8, 0); Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& tex = proxy->getRef(); + Shared& tex = proxy->getShared(); tex->render(x, y, sx, sy, r, ox, oy); } @@ -299,7 +299,7 @@ namespace JinEngine float ox = luax_optnumber(L, 7, 0); float oy = luax_optnumber(L, 8, 0); Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& p = proxy->getRef(); + Shared& p = proxy->getShared(); p->render(x, y, sx, sy, r, ox, oy); } @@ -389,13 +389,13 @@ namespace JinEngine if (luax_istype(L, 1, JIN_GRAPHICS_TEXTURE)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& tex = proxy->getRef(); + Shared& tex = proxy->getShared(); tex->render(q, x, y, sx, sy, r, ox, oy); } else if (luax_istype(L, 1, JIN_GRAPHICS_CANVAS)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& p = proxy->getRef(); + Shared& p = proxy->getShared(); p->render(q, x, y, sx, sy, r, ox, oy); } else @@ -462,7 +462,7 @@ namespace JinEngine return 0; } Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_CANVAS); - Ref& ref = proxy->getRef(); + Shared& ref = proxy->getShared(); Canvas::bind(ref.getObject()); return 0; } @@ -483,7 +483,7 @@ namespace JinEngine if (luax_istype(L, 1, JIN_GRAPHICS_SHADER)) { Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Ref& jsl = proxy->getRef(); + Shared& jsl = proxy->getShared(); jsl->use(); } else @@ -496,18 +496,10 @@ namespace JinEngine LUA_IMPLEMENT int l_setBlend(lua_State* L) { - return 0; + return 0; } - LUA_IMPLEMENT 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; - } +#define IntToRenderMode(I) static_cast(I) LUA_IMPLEMENT int l_point(lua_State* L) { @@ -531,8 +523,7 @@ namespace JinEngine LUA_IMPLEMENT int l_rect(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); + RenderMode mode = IntToRenderMode(luax_checkinteger(L, 1)); if (mode != RenderMode::NONE) { int x = luax_checknumber(L, 2); @@ -552,8 +543,7 @@ namespace JinEngine LUA_IMPLEMENT int l_circle(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); + RenderMode mode = IntToRenderMode(luax_checkinteger(L, 1)); if (mode != RenderMode::NONE) { int x = luax_checknumber(L, 2); @@ -572,8 +562,7 @@ namespace JinEngine LUA_IMPLEMENT int l_triangle(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - RenderMode mode = strtomode(modestr); + RenderMode mode = IntToRenderMode(luax_checkinteger(L, 1)); if (mode != RenderMode::NONE) { int x = luax_checknumber(L, 2); @@ -598,9 +587,8 @@ namespace JinEngine LUA_IMPLEMENT int l_polygon(lua_State* L) { - const char* modestr = luax_checkstring(L, 1); - int n = luax_checknumber(L, 2); - RenderMode mode = strtomode(modestr); + RenderMode mode = IntToRenderMode(luax_checkinteger(L, 1)); + int n = luax_checknumber(L, 2); if (mode != RenderMode::NONE) { if (!luax_istable(L, 3)) @@ -648,7 +636,7 @@ namespace JinEngine fs->read(path, b); fd = TTFData::createTTFData(&b, b.size()); } - proxy->bind(new Ref(fd, JIN_GRAPHICS_TTFDATA)); + proxy->bind(new Shared(fd, JIN_GRAPHICS_TTFDATA)); return 1; } @@ -672,7 +660,7 @@ namespace JinEngine 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, JIN_GRAPHICS_TEXT)); + proxy->bind(new Shared(text, JIN_GRAPHICS_TEXT)); return 1; } @@ -724,7 +712,7 @@ namespace JinEngine delete text; } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTUREFONT, sizeof(Proxy)); - proxy->bind(new Ref(textureFont, JIN_GRAPHICS_TEXTUREFONT)); + proxy->bind(new Shared(textureFont, JIN_GRAPHICS_TEXTUREFONT)); return 1; } @@ -810,7 +798,7 @@ namespace JinEngine LUA_EXPORT int luaopen_graphics(lua_State* L) { - // register types + // Register types. luaopen_Bitmap(L); luaopen_Texture(L); luaopen_Canvas(L); @@ -821,7 +809,7 @@ namespace JinEngine luaopen_Page(L); luaopen_JSL(L); - // load whole lib + // Load whole lib. luax_newlib(L, f); return 1; diff --git a/src/lua/modules/graphics/je_lua_page.cpp b/src/lua/modules/graphics/je_lua_page.cpp index 36754a0..f3c2517 100644 --- a/src/lua/modules/graphics/je_lua_page.cpp +++ b/src/lua/modules/graphics/je_lua_page.cpp @@ -14,7 +14,7 @@ namespace JinEngine namespace Lua { - typedef Ref& FontRef; + typedef Shared& FontRef; Page* getPage(lua_State* L) { @@ -27,8 +27,8 @@ namespace JinEngine Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_PAGE); { /* release font */ - Ref* page = &proxy->getRef(); - RefBase* font = (RefBase*)page->getUserdata(); + Shared* page = &proxy->getShared(); + SharedBase* font = (SharedBase*)page->getUserdata(); font->release(); } proxy->release(); diff --git a/src/lua/modules/graphics/je_lua_shader.cpp b/src/lua/modules/graphics/je_lua_shader.cpp index 9131815..e2e1413 100644 --- a/src/lua/modules/graphics/je_lua_shader.cpp +++ b/src/lua/modules/graphics/je_lua_shader.cpp @@ -11,12 +11,12 @@ namespace JinEngine namespace Lua { - typedef Ref& ShaderRef; + typedef Shared& ShaderRef; static inline ShaderRef checkShader(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_SHADER); - return proxy->getRef(); + return proxy->getShared(); } /** @@ -36,7 +36,7 @@ namespace JinEngine ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_TEXTURE); - Ref& tex = proxy->getRef(); + Shared& tex = proxy->getShared(); ref->sendTexture(variable, tex.getObject()); return 0; } @@ -46,7 +46,7 @@ namespace JinEngine ShaderRef ref = checkShader(L); const char* variable = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_checktype(L, 3, JIN_GRAPHICS_CANVAS); - Ref& canvas = proxy->getRef(); + Shared& canvas = proxy->getShared(); ref->sendCanvas(variable, canvas.getObject()); return 0; } diff --git a/src/lua/modules/graphics/je_lua_texture.cpp b/src/lua/modules/graphics/je_lua_texture.cpp index 0ab79b5..a643dae 100644 --- a/src/lua/modules/graphics/je_lua_texture.cpp +++ b/src/lua/modules/graphics/je_lua_texture.cpp @@ -10,12 +10,12 @@ namespace JinEngine namespace Lua { - typedef Ref& TextureRef; + typedef Shared& TextureRef; LUA_IMPLEMENT inline TextureRef checkTexture(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TEXTURE); - return proxy->getRef(); + return proxy->getShared(); } LUA_IMPLEMENT int l_getWidth(lua_State* L) diff --git a/src/lua/modules/graphics/je_lua_texture_font.cpp b/src/lua/modules/graphics/je_lua_texture_font.cpp index ba0a504..58011a7 100644 --- a/src/lua/modules/graphics/je_lua_texture_font.cpp +++ b/src/lua/modules/graphics/je_lua_texture_font.cpp @@ -40,10 +40,10 @@ namespace JinEngine page = tf->typeset(*text, lineheight, spacing); } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref* refPage = new Ref(page, JIN_GRAPHICS_PAGE); + Shared* refPage = new Shared(page, JIN_GRAPHICS_PAGE); { /* retain related ttf */ - Ref& refTF = p->getRef(); + Shared& refTF = p->getShared(); refTF.retain(); refPage->setUserdata(&refTF); } diff --git a/src/lua/modules/graphics/je_lua_ttf.cpp b/src/lua/modules/graphics/je_lua_ttf.cpp index 49f13c0..4c56ae1 100644 --- a/src/lua/modules/graphics/je_lua_ttf.cpp +++ b/src/lua/modules/graphics/je_lua_ttf.cpp @@ -16,8 +16,8 @@ namespace JinEngine Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTF); { /* release ttf data */ - Ref* ttf = &proxy->getRef(); - RefBase* data = (RefBase*)ttf->getUserdata(); + Shared* ttf = &proxy->getShared(); + SharedBase* data = (SharedBase*)ttf->getUserdata(); data->release(); } proxy->release(); @@ -46,10 +46,10 @@ namespace JinEngine page = ttf->typeset(*text, lineheight, spacing); } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_PAGE, sizeof(Proxy)); - Ref* refPage = new Ref(page, JIN_GRAPHICS_PAGE); + Shared* refPage = new Shared(page, JIN_GRAPHICS_PAGE); { /* retain related ttf */ - Ref& refTTF = p->getRef(); + Shared& refTTF = p->getShared(); refTTF.retain(); refPage->setUserdata(&refTTF); } diff --git a/src/lua/modules/graphics/je_lua_ttf_data.cpp b/src/lua/modules/graphics/je_lua_ttf_data.cpp index 4212778..d076ab8 100644 --- a/src/lua/modules/graphics/je_lua_ttf_data.cpp +++ b/src/lua/modules/graphics/je_lua_ttf_data.cpp @@ -15,13 +15,13 @@ namespace JinEngine { Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_TTFDATA); int fontsize = luax_checkinteger(L, 2); - Ref& refFontData = p->getRef(); + Shared& refFontData = p->getShared(); TTFData* fontData = refFontData.getObject(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TTF, sizeof(Proxy)); TTF* font = fontData->createTTF(fontsize); - Ref* refTTF = new Ref(font, JIN_GRAPHICS_TTF); + Shared* refTTF = new Shared(font, JIN_GRAPHICS_TTF); { - Ref& refTTFData = p->getRef(); + Shared& refTTFData = p->getShared(); refTTFData.retain(); refTTF->setUserdata(&refTTFData); } diff --git a/src/lua/modules/net/je_lua_buffer.cpp b/src/lua/modules/net/je_lua_buffer.cpp index 9e769e8..4c6880a 100644 --- a/src/lua/modules/net/je_lua_buffer.cpp +++ b/src/lua/modules/net/je_lua_buffer.cpp @@ -11,12 +11,12 @@ namespace JinEngine namespace Net { - typedef Ref& BufferRef; + typedef Shared& BufferRef; static inline BufferRef checkNetBuffer(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER); - return proxy->getRef(); + return proxy->getShared(); } // net.Buffer:append(value) -> value_length diff --git a/src/lua/modules/net/je_lua_net.cpp b/src/lua/modules/net/je_lua_net.cpp index de5d506..cd454d5 100644 --- a/src/lua/modules/net/je_lua_net.cpp +++ b/src/lua/modules/net/je_lua_net.cpp @@ -49,7 +49,7 @@ namespace Lua } Socket* socket = new Socket(info); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(new Ref(socket, JIN_NETWORK_SOCKET)); + proxy->bind(new Shared(socket, JIN_NETWORK_SOCKET)); return 1; } @@ -58,7 +58,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); - proxy->bind(new Ref(buffer, JIN_NETWORK_BUFFER)); + proxy->bind(new Shared(buffer, JIN_NETWORK_BUFFER)); return 1; } diff --git a/src/lua/modules/net/je_lua_socket.cpp b/src/lua/modules/net/je_lua_socket.cpp index d68cb51..0b59199 100644 --- a/src/lua/modules/net/je_lua_socket.cpp +++ b/src/lua/modules/net/je_lua_socket.cpp @@ -12,20 +12,20 @@ namespace JinEngine namespace Lua { - typedef Ref& SocketRef; + typedef Shared& SocketRef; const int BUFFER_SIZE = 1024; LUA_IMPLEMENT inline SocketRef checkSocket(lua_State* L, int pos = 1) { Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET); - return proxy->getRef(); + return proxy->getShared(); } - LUA_IMPLEMENT inline Ref& checkNetBuffer(lua_State* L, int pos = 1) + LUA_IMPLEMENT inline Shared& checkNetBuffer(lua_State* L, int pos = 1) { Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER); - return proxy->getRef(); + return proxy->getShared(); } // return net.Socket @@ -34,7 +34,7 @@ namespace JinEngine SocketRef socket = checkSocket(L); Socket* client = socket->accept(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); - proxy->bind(new Ref(client, JIN_NETWORK_SOCKET)); + proxy->bind(new Shared(client, JIN_NETWORK_SOCKET)); return 1; } @@ -46,7 +46,7 @@ namespace JinEngine int size = socket->receive(buffer, BUFFER_SIZE); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); Net::Buffer* netBuffer = new Net::Buffer(buffer, size); - proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); + proxy->bind(new Shared(netBuffer, JIN_NETWORK_BUFFER)); return 1; } @@ -60,7 +60,7 @@ namespace JinEngine int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port); Net::Buffer* netBuffer = new Net::Buffer(buffer, size); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); - proxy->bind(new Ref(netBuffer, JIN_NETWORK_BUFFER)); + proxy->bind(new Shared(netBuffer, JIN_NETWORK_BUFFER)); return 1; } @@ -68,7 +68,7 @@ namespace JinEngine LUA_IMPLEMENT int l_send(lua_State* L) { SocketRef socket = checkSocket(L); - Ref& ref = checkNetBuffer(L, 2); + Shared& ref = checkNetBuffer(L, 2); int len = socket->send(ref->buffer, ref->size); luax_pushinteger(L, len); return 1; @@ -80,7 +80,7 @@ namespace JinEngine SocketRef socket = checkSocket(L); int address = luax_checkinteger(L, 2); int port = luax_checkinteger(L, 3); - Ref& buffer = checkNetBuffer(L, 4); + Shared& buffer = checkNetBuffer(L, 4); socket->sendTo(buffer->buffer, buffer->size, address, port); return 0; } diff --git a/src/lua/modules/thread/je_lua_thread.cpp b/src/lua/modules/thread/je_lua_thread.cpp index 5babc13..ab03561 100644 --- a/src/lua/modules/thread/je_lua_thread.cpp +++ b/src/lua/modules/thread/je_lua_thread.cpp @@ -10,19 +10,19 @@ namespace JinEngine namespace Lua { - typedef Ref& ThreadRef; + typedef Shared& ThreadRef; int luaopen_thread(lua_State* L); static inline ThreadRef checkThread(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_THREAD_THREAD); - return proxy->getRef(); + return proxy->getShared(); } LUA_IMPLEMENT int threadRunner(void* t) { - ThreadRef ref = *(Ref*)t; + ThreadRef ref = *(Shared*)t; lua_State* L = lua_open(); luax_openlibs(L); luaopen_jin(L); @@ -126,8 +126,8 @@ namespace JinEngine case 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); + p->shared->retain(); + proxy->bind(p->shared); break; } @@ -161,8 +161,8 @@ namespace JinEngine 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); + p->retain(); + proxy->bind(p->shared); break; } @@ -220,7 +220,7 @@ namespace JinEngine const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); Thread* thread = new Thread(name, code, threadRunner); - proxy->bind(new Ref(thread, JIN_THREAD_THREAD)); + proxy->bind(new Shared(thread, JIN_THREAD_THREAD)); return 1; } diff --git a/src/lua/modules/time/je_lua_timer.cpp b/src/lua/modules/time/je_lua_timer.cpp new file mode 100644 index 0000000..c28aafe --- /dev/null +++ b/src/lua/modules/time/je_lua_timer.cpp @@ -0,0 +1,30 @@ +#include "../types.h" +#include "lua/common/je_lua_common.h" +#include "je_lua_timer.h" + +namespace JinEngine +{ + namespace Lua + { + + typedef Shared& TimerRef; + + LUA_IMPLEMENT inline TimerRef checkTimer(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + return proxy->getShared(); + } + + LUA_IMPLEMENT int l_every(lua_State* L) + { + //TimerRef ref = checkTimer(L); + //Timer* timer = ref.getObject(); + //int n = luax_checkinteger(L, 1); + //int ref = luax_ref(L, 2); + //timer->every(n, [](void* data)->void { + // + //}, ); + } + + } +} \ No newline at end of file diff --git a/src/lua/modules/time/je_lua_timer.h b/src/lua/modules/time/je_lua_timer.h new file mode 100644 index 0000000..9beadf5 --- /dev/null +++ b/src/lua/modules/time/je_lua_timer.h @@ -0,0 +1,19 @@ +#ifndef __JE_LUA_TIMER_H__ +#define __JE_LUA_TIMER_H__ + +#include "libjin/jin.h" + +namespace JinEngine +{ + namespace Lua + { + + class Timer : public JinEngine::Time::Timer + { + + }; + + } +} + +#endif \ No newline at end of file diff --git a/src/lua/modules/types.h b/src/lua/modules/types.h index 123604e..c6600dc 100644 --- a/src/lua/modules/types.h +++ b/src/lua/modules/types.h @@ -22,4 +22,7 @@ #define JIN_NETWORK_SOCKET "Socket" #define JIN_NETWORK_BUFFER "Buffer" +// time module +#define JIN_TIME_TIMER "Timer" + #endif \ No newline at end of file -- cgit v1.1-26-g67d0