From fa234f9663b992cf50bcf865a1cde6845b42193c Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 21 May 2018 16:02:00 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9audio=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/3rdparty/luax/luax.h | 15 +- src/libjin/audio/audio.cpp | 37 +- src/libjin/audio/audio.h | 52 ++- src/libjin/audio/sdl/audio.cpp | 83 +++++ src/libjin/audio/sdl/audio.h | 59 +++ src/libjin/audio/sdl/source.cpp | 2 + src/libjin/audio/sdl/source.h | 19 + src/libjin/audio/source.h | 31 +- src/libjin/common/subsystem.h | 8 +- src/libjin/core/thread.cpp | 0 src/libjin/core/thread.h | 1 + src/libjin/core/timer.cpp | 0 src/libjin/core/timer.h | 1 + src/libjin/jin.h | 5 + src/libjin/math/constant.h | 4 + src/libjin/math/math.h | 7 +- src/libjin/math/rect.h | 15 - src/libjin/physics/physics.h | 12 + src/libjin/render/image.cpp | 1 + src/libjin/utils/endian.h | 3 - src/libjin/utils/unittest.cpp | 33 +- src/libjin/utils/utils.h | 6 - src/lua/audio/luaopen_Source.cpp | 26 ++ src/lua/audio/luaopen_audio.cpp | 39 ++ src/lua/core/luaopen_core.cpp | 43 +++ src/lua/debug/luaopen_debug.cpp | 8 + src/lua/embed/boot.lua | 132 +++++++ src/lua/embed/boot.lua.h | 156 ++++++++ src/lua/embed/debug.lua | 128 +++++++ src/lua/embed/debug.lua.h | 132 +++++++ src/lua/embed/embed.h | 51 +++ src/lua/embed/graphics.lua | 6 + src/lua/embed/graphics.lua.h | 8 + src/lua/embed/keyboard.lua | 16 + src/lua/embed/keyboard.lua.h | 20 + src/lua/embed/mouse.lua | 15 + src/lua/embed/mouse.lua.h | 18 + src/lua/embed/path.lua | 15 + src/lua/embed/path.lua.h | 18 + src/lua/event/luaopen_event.cpp | 124 +++++++ src/lua/filesystem/luaopen_filesystem.cpp | 142 ++++++++ src/lua/graphics/luaopen_Canvas.cpp | 75 ++++ src/lua/graphics/luaopen_Font.cpp | 45 +++ src/lua/graphics/luaopen_Image.cpp | 89 +++++ src/lua/graphics/luaopen_JSL.cpp | 146 ++++++++ src/lua/graphics/luaopen_graphics.cpp | 524 ++++++++++++++++++++++++++ src/lua/joypad/joypad.h | 14 + src/lua/joypad/luaopen_joypad.cpp | 21 ++ src/lua/keyboard/luaopen_keyboard.cpp | 16 + src/lua/luaopen_jin.cpp | 97 +++++ src/lua/luaopen_jin.h | 45 +++ src/lua/luaopen_types.h | 24 ++ src/lua/luax.h | 10 + src/lua/mouse/luaopen_mouse.cpp | 27 ++ src/lua/net/luaopen_net.cpp | 110 ++++++ src/lua/thread/luaopen_Thread.cpp | 0 src/lua/time/luaopen_time.cpp | 34 ++ src/main.cpp | 4 +- src/script/audio/luaopen_Source.cpp | 26 -- src/script/audio/luaopen_audio.cpp | 39 -- src/script/core/luaopen_core.cpp | 43 --- src/script/debug/luaopen_debug.cpp | 8 - src/script/embed/boot.lua | 132 ------- src/script/embed/boot.lua.h | 156 -------- src/script/embed/debug.lua | 128 ------- src/script/embed/debug.lua.h | 132 ------- src/script/embed/embed.h | 51 --- src/script/embed/graphics.lua | 6 - src/script/embed/graphics.lua.h | 8 - src/script/embed/keyboard.lua | 16 - src/script/embed/keyboard.lua.h | 20 - src/script/embed/mouse.lua | 15 - src/script/embed/mouse.lua.h | 18 - src/script/embed/path.lua | 15 - src/script/embed/path.lua.h | 18 - src/script/event/luaopen_event.cpp | 124 ------- src/script/filesystem/luaopen_filesystem.cpp | 142 -------- src/script/graphics/luaopen_Canvas.cpp | 75 ---- src/script/graphics/luaopen_Font.cpp | 45 --- src/script/graphics/luaopen_Image.cpp | 89 ----- src/script/graphics/luaopen_JSL.cpp | 146 -------- src/script/graphics/luaopen_graphics.cpp | 526 --------------------------- src/script/joypad/joypad.h | 14 - src/script/joypad/luaopen_joypad.cpp | 21 -- src/script/keyboard/luaopen_keyboard.cpp | 16 - src/script/luaopen_jin.cpp | 97 ----- src/script/luaopen_jin.h | 45 --- src/script/luaopen_types.h | 24 -- src/script/mouse/luaopen_mouse.cpp | 27 -- src/script/net/luaopen_net.cpp | 110 ------ src/script/thread/luaopen_Thread.cpp | 0 src/script/time/luaopen_time.cpp | 34 -- 92 files changed, 2651 insertions(+), 2487 deletions(-) create mode 100644 src/libjin/audio/sdl/audio.cpp create mode 100644 src/libjin/audio/sdl/audio.h create mode 100644 src/libjin/audio/sdl/source.cpp create mode 100644 src/libjin/audio/sdl/source.h create mode 100644 src/libjin/core/thread.cpp create mode 100644 src/libjin/core/thread.h create mode 100644 src/libjin/core/timer.cpp create mode 100644 src/libjin/core/timer.h delete mode 100644 src/libjin/math/rect.h create mode 100644 src/libjin/physics/physics.h create mode 100644 src/lua/audio/luaopen_Source.cpp create mode 100644 src/lua/audio/luaopen_audio.cpp create mode 100644 src/lua/core/luaopen_core.cpp create mode 100644 src/lua/debug/luaopen_debug.cpp create mode 100644 src/lua/embed/boot.lua create mode 100644 src/lua/embed/boot.lua.h create mode 100644 src/lua/embed/debug.lua create mode 100644 src/lua/embed/debug.lua.h create mode 100644 src/lua/embed/embed.h create mode 100644 src/lua/embed/graphics.lua create mode 100644 src/lua/embed/graphics.lua.h create mode 100644 src/lua/embed/keyboard.lua create mode 100644 src/lua/embed/keyboard.lua.h create mode 100644 src/lua/embed/mouse.lua create mode 100644 src/lua/embed/mouse.lua.h create mode 100644 src/lua/embed/path.lua create mode 100644 src/lua/embed/path.lua.h create mode 100644 src/lua/event/luaopen_event.cpp create mode 100644 src/lua/filesystem/luaopen_filesystem.cpp create mode 100644 src/lua/graphics/luaopen_Canvas.cpp create mode 100644 src/lua/graphics/luaopen_Font.cpp create mode 100644 src/lua/graphics/luaopen_Image.cpp create mode 100644 src/lua/graphics/luaopen_JSL.cpp create mode 100644 src/lua/graphics/luaopen_graphics.cpp create mode 100644 src/lua/joypad/joypad.h create mode 100644 src/lua/joypad/luaopen_joypad.cpp create mode 100644 src/lua/keyboard/luaopen_keyboard.cpp create mode 100644 src/lua/luaopen_jin.cpp create mode 100644 src/lua/luaopen_jin.h create mode 100644 src/lua/luaopen_types.h create mode 100644 src/lua/luax.h create mode 100644 src/lua/mouse/luaopen_mouse.cpp create mode 100644 src/lua/net/luaopen_net.cpp create mode 100644 src/lua/thread/luaopen_Thread.cpp create mode 100644 src/lua/time/luaopen_time.cpp delete mode 100644 src/script/audio/luaopen_Source.cpp delete mode 100644 src/script/audio/luaopen_audio.cpp delete mode 100644 src/script/core/luaopen_core.cpp delete mode 100644 src/script/debug/luaopen_debug.cpp delete mode 100644 src/script/embed/boot.lua delete mode 100644 src/script/embed/boot.lua.h delete mode 100644 src/script/embed/debug.lua delete mode 100644 src/script/embed/debug.lua.h delete mode 100644 src/script/embed/embed.h delete mode 100644 src/script/embed/graphics.lua delete mode 100644 src/script/embed/graphics.lua.h delete mode 100644 src/script/embed/keyboard.lua delete mode 100644 src/script/embed/keyboard.lua.h delete mode 100644 src/script/embed/mouse.lua delete mode 100644 src/script/embed/mouse.lua.h delete mode 100644 src/script/embed/path.lua delete mode 100644 src/script/embed/path.lua.h delete mode 100644 src/script/event/luaopen_event.cpp delete mode 100644 src/script/filesystem/luaopen_filesystem.cpp delete mode 100644 src/script/graphics/luaopen_Canvas.cpp delete mode 100644 src/script/graphics/luaopen_Font.cpp delete mode 100644 src/script/graphics/luaopen_Image.cpp delete mode 100644 src/script/graphics/luaopen_JSL.cpp delete mode 100644 src/script/graphics/luaopen_graphics.cpp delete mode 100644 src/script/joypad/joypad.h delete mode 100644 src/script/joypad/luaopen_joypad.cpp delete mode 100644 src/script/keyboard/luaopen_keyboard.cpp delete mode 100644 src/script/luaopen_jin.cpp delete mode 100644 src/script/luaopen_jin.h delete mode 100644 src/script/luaopen_types.h delete mode 100644 src/script/mouse/luaopen_mouse.cpp delete mode 100644 src/script/net/luaopen_net.cpp delete mode 100644 src/script/thread/luaopen_Thread.cpp delete mode 100644 src/script/time/luaopen_time.cpp (limited to 'src') diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h index b5ef0f4..d3047b9 100644 --- a/src/3rdparty/luax/luax.h +++ b/src/3rdparty/luax/luax.h @@ -18,14 +18,14 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - +/** +* include following 3 files before this +* lua.h +* lauxlib.h +* lualib.h +*/ #ifndef __LUAX_H #define __LUAX_H - -#include "3rdparty/lua51/lua.h" -#include "3rdparty/lua51/lauxlib.h" -#include "3rdparty/lua51/lualib.h" - // only for lua 5.1 #if LUA_VERSION_NUM == 501 @@ -331,4 +331,5 @@ inline int luax_register_searcher(lua_State * L, lua_CFunction f, int pos) } #endif -#endif + +#endif \ No newline at end of file diff --git a/src/libjin/audio/audio.cpp b/src/libjin/audio/audio.cpp index 7142625..edba496 100644 --- a/src/libjin/audio/audio.cpp +++ b/src/libjin/audio/audio.cpp @@ -6,7 +6,7 @@ namespace jin { namespace audio { - + shared Audio* Audio::audio = NULL; bool Audio::init(const SettingBase* setting) @@ -20,40 +20,5 @@ namespace audio CallOnce(_quit()); } - onlyonce bool Audio::_init(const SettingBase* s) - { - if (SDL_Init(SDL_INIT_AUDIO) < 0) - return false; - - const AudioSetting* setting = (AudioSetting*)s; - SDL_AudioSpec wanted; - zero(wanted); - wanted.freq = setting->freq; - wanted.format = setting->format; - wanted.channels = setting->channels; - wanted.samples = setting->samples; - wanted.userdata = setting->userdata; - wanted.callback = setting->callback; - - if (SDL_OpenAudio(&wanted, NULL) < 0) - { - return false; - } - // start audio - SDL_PauseAudio(0); - return true; - } - - onlyonce void Audio::_quit() - { - SDL_CloseAudio(); - delete audio; - } - - shared void defaultCallback(void *udata, Uint8 *stream, int len) - { - - } - } } \ No newline at end of file diff --git a/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index 80e463f..6d57cd2 100644 --- a/src/libjin/audio/audio.h +++ b/src/libjin/audio/audio.h @@ -10,44 +10,42 @@ namespace jin { namespace audio { - - class Audio : public common::Subsystem + class Source; + + class IAudio { + public: - - struct Setting : SettingBase - { - int freq; - int format; - char channels; - int samples; - SDL_AudioCallback callback; - void* userdata; - }; - - bool init(const SettingBase* setting) override; - void quit() override; - static inline Audio* get() - { - return (audio == NULL ? (audio = new Audio()) : audio); - } + virtual void play() = 0; + virtual void stop() = 0; + virtual bool pause() = 0; + virtual bool pause(Source* source) = 0; + virtual bool resume() = 0; + virtual bool resume(Source* source) = 0; + virtual void rewind() = 0; + virtual void setVolume(float volume) = 0; + virtual float getVolume() = 0; - static void defaultCallback(void *udata, Uint8 *stream, int len); + }; - private: + class Audio : public common::Subsystem, IAudio + { - Audio() {}; - ~Audio() {}; + public: + + Audio() {}; + virtual ~Audio() {}; + + bool init(const SettingBase* setting) override; + void quit() override; + + protected: static Audio* audio; - onlyonce bool _init(const SettingBase* setting) override; - onlyonce void _quit() override; }; - typedef Audio::Setting AudioSetting; - } } diff --git a/src/libjin/audio/sdl/audio.cpp b/src/libjin/audio/sdl/audio.cpp new file mode 100644 index 0000000..0559c18 --- /dev/null +++ b/src/libjin/audio/sdl/audio.cpp @@ -0,0 +1,83 @@ +#include "audio.h" + +namespace jin +{ +namespace audio +{ + + onlyonce bool SDLAudio::_init(const SettingBase* s) + { + if (SDL_Init(SDL_INIT_AUDIO) < 0) + return false; + + const SDLAudioSetting* setting = (SDLAudioSetting*)s; + SDL_AudioSpec wanted; + zero(wanted); + wanted.freq = setting->rate; + wanted.format = setting->resolution; + wanted.channels = setting->channels; + wanted.samples = setting->samples; + wanted.userdata = setting->userdata; + wanted.callback = setting->callback; + + if (SDL_OpenAudio(&wanted, NULL) < 0) + { + return false; + } + // start audio + SDL_PauseAudio(0); + return true; + } + + onlyonce void SDLAudio::_quit() + { + SDL_CloseAudio(); + delete audio; + } + + void SDLAudio::defaultCallback(void *udata, Uint8 *stream, int len) + { + + } + + void SDLAudio::play() {} + + void SDLAudio::stop() {} + + bool SDLAudio::pause() + { + return false; + } + + bool SDLAudio::pause(Source* source) + { + return false; + } + + bool SDLAudio::resume() + { + return false; + } + + bool SDLAudio::resume(Source* source) + { + return false; + } + + void SDLAudio::rewind() + { + + } + + void SDLAudio::setVolume(float volume) + { + + } + + float SDLAudio::getVolume() + { + return 0.f; + } + +} +} diff --git a/src/libjin/audio/sdl/audio.h b/src/libjin/audio/sdl/audio.h new file mode 100644 index 0000000..920f56a --- /dev/null +++ b/src/libjin/audio/sdl/audio.h @@ -0,0 +1,59 @@ +#ifndef __JIN_AUDIO_SDL_H +#define __JIN_AUDIO_SDL_H + +#include "../audio.h" + +namespace jin +{ +namespace audio +{ + + class SDLAudio : public Audio + { + + public: + + struct Setting : SettingBase + { + int rate; // 采样率 + int resolution; // 分辨率 + char channels; // 声道数 + int samples; // 取样数 + SDL_AudioCallback callback; // 音频回调 + void* userdata; // 自定义参数 + }; + + static void defaultCallback(void *udata, Uint8 *stream, int len); + + static inline Audio* get() + { + return audio != NULL ? audio : (audio = new SDLAudio()); + } + + /* IAudio interface */ + void play() override; + void stop() override; + bool pause() override; + bool pause(Source* source) override; + bool resume() override; + bool resume(Source* source) override; + void rewind() override; + void setVolume(float volume) override; + float getVolume() override; + + private: + + SDLAudio() {} + ~SDLAudio() {} + + onlyonce bool _init(const SettingBase* setting) override; + onlyonce void _quit() override; + + }; + + typedef SDLAudio::Setting SDLAudioSetting; + +} +} + +#endif \ No newline at end of file diff --git a/src/libjin/audio/sdl/source.cpp b/src/libjin/audio/sdl/source.cpp new file mode 100644 index 0000000..cca4d3e --- /dev/null +++ b/src/libjin/audio/sdl/source.cpp @@ -0,0 +1,2 @@ +#include "source.h" + diff --git a/src/libjin/audio/sdl/source.h b/src/libjin/audio/sdl/source.h new file mode 100644 index 0000000..052a49e --- /dev/null +++ b/src/libjin/audio/sdl/source.h @@ -0,0 +1,19 @@ +#ifndef __JIN_SOURCE_SDL_H +#define __JIN_SOURCE_SDL_H + +#include "../source.h" + +namespace jin +{ +namespace audio +{ + + class SDLSource : public Source + { + + }; + +} +} + +#endif \ No newline at end of file diff --git a/src/libjin/audio/source.h b/src/libjin/audio/source.h index 96da208..79fa8e0 100644 --- a/src/libjin/audio/source.h +++ b/src/libjin/audio/source.h @@ -10,21 +10,32 @@ namespace audio class Source { + public: - void play(); - void stop(); - void pause(); - void resume(); - void rewind(); - void isStopped() const; - void isPaused() const; - void isFinished() const; - void setPitch(float pitch); - void setVolume(float volume); + + virtual void play() = 0; + virtual void stop() = 0; + virtual void pause() = 0; + virtual void resume() = 0; + virtual void rewind() = 0; + virtual void isStopped() const = 0; + virtual void isPaused() const = 0; + virtual void isFinished() const = 0; + virtual void setPitch(float pitch) = 0; + virtual void setVolume(float volume) = 0; + virtual bool setLoop(bool loop) = 0; + virtual void setRate(float rate) = 0; + + protected: + + Source(); + virtual ~Source(); private: + unsigned char * curPos; unsigned int length; + }; } diff --git a/src/libjin/common/subsystem.h b/src/libjin/common/subsystem.h index ad28061..ca4b33d 100644 --- a/src/libjin/common/subsystem.h +++ b/src/libjin/common/subsystem.h @@ -10,6 +10,7 @@ namespace common class Subsystem { + public: struct Setting {}; @@ -20,11 +21,14 @@ namespace common virtual void quit() = 0; - private: + protected: - virtual onlyonce bool _init(const Setting* setting) = 0; + Subsystem() {}; + virtual ~Subsystem() {}; + virtual onlyonce bool _init(const Setting* setting) = 0; virtual onlyonce void _quit() = 0; + }; } diff --git a/src/libjin/core/thread.cpp b/src/libjin/core/thread.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/core/thread.h b/src/libjin/core/thread.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/src/libjin/core/thread.h @@ -0,0 +1 @@ +#pragma once diff --git a/src/libjin/core/timer.cpp b/src/libjin/core/timer.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/libjin/core/timer.h b/src/libjin/core/timer.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/src/libjin/core/timer.h @@ -0,0 +1 @@ +#pragma once diff --git a/src/libjin/jin.h b/src/libjin/jin.h index 95895f7..32c04c7 100644 --- a/src/libjin/jin.h +++ b/src/libjin/jin.h @@ -9,4 +9,9 @@ #include "net/net.h" #include "render/render.h" +#define JIN_VERSION "Jin 0.1" +#define JIN_RELEASE "Jin 0.1.0" +#define JIN_VERSION_NUM 100 // 00.01.00 +#define JIN_AUTHOR "Chai" + #endif \ No newline at end of file diff --git a/src/libjin/math/constant.h b/src/libjin/math/constant.h index 74acaea..f2f740f 100644 --- a/src/libjin/math/constant.h +++ b/src/libjin/math/constant.h @@ -3,4 +3,8 @@ #define PI 3.1415926f +// int16 范围 +#define INT16_RANGE_LEFT -32768 +#define INT16_RANGE_RIGHT 32767 + #endif \ No newline at end of file diff --git a/src/libjin/math/math.h b/src/libjin/math/math.h index fdf1725..f6a299e 100644 --- a/src/libjin/math/math.h +++ b/src/libjin/math/math.h @@ -6,6 +6,11 @@ #include "constant.h" #include "matrix.h" #include "quad.h" -#include "rect.h" + +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#define clamp(a, mi,ma) min(max(a,mi),ma) +#define within(a,min,max) (a >= min && a <= max) +#define without(a,min,max) (a < min || a > max) #endif \ No newline at end of file diff --git a/src/libjin/math/rect.h b/src/libjin/math/rect.h deleted file mode 100644 index 79c1df2..0000000 --- a/src/libjin/math/rect.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __JIN_RECT_H -#define __JIN_RECT_H - -namespace jin -{ -namespace math -{ - class Rect - { - public: - int x, y, w, h; - }; -} -} -#endif \ No newline at end of file diff --git a/src/libjin/physics/physics.h b/src/libjin/physics/physics.h new file mode 100644 index 0000000..9927301 --- /dev/null +++ b/src/libjin/physics/physics.h @@ -0,0 +1,12 @@ +#ifndef __JIN_PHYSICS_H +#define __JIN_PHYSICS_H + +namespace jin +{ +namespace physics +{ + +} +} + +#endif \ No newline at end of file diff --git a/src/libjin/render/image.cpp b/src/libjin/render/image.cpp index 583bd8d..7955aa2 100644 --- a/src/libjin/render/image.cpp +++ b/src/libjin/render/image.cpp @@ -1,6 +1,7 @@ #include "image.h" #include "3rdparty/stb/stb_image.h" #include "../utils/utils.h" +#include "../math/math.h" namespace jin { diff --git a/src/libjin/utils/endian.h b/src/libjin/utils/endian.h index df28ddb..d4c441a 100644 --- a/src/libjin/utils/endian.h +++ b/src/libjin/utils/endian.h @@ -1,6 +1,3 @@ -/** -* -*/ #ifndef JIN_LIL_ENDIAN && JIN_BIG_ENDIAN #define JIN_LIL_ENDIAN 2 diff --git a/src/libjin/utils/unittest.cpp b/src/libjin/utils/unittest.cpp index 764a2bd..5b73312 100644 --- a/src/libjin/utils/unittest.cpp +++ b/src/libjin/utils/unittest.cpp @@ -2,32 +2,41 @@ #if UNITTEST #include -#include "../audio/audio.h" +#include "../audio/sdl/audio.h" using namespace jin::audio; -void fill_audio(void *udata, Uint8 *stream, int len) +int sample_nr = 0; + +void fill_audio(void *user_data, Uint8 *raw_buffer, int bytes) { - printf("%d\n", len); - memset(stream, 0x11, len); + Sint16 *buffer = (Sint16*)raw_buffer; + int length = bytes / 2; // 2 bytes per sample for AUDIO_S16SYS + int &sample_nr(*(int*)user_data); + + for (int i = 0; i < length; i++, sample_nr++) + { + double time = (double)sample_nr / (double)44100; + buffer[i] = (Sint16)(28000 * sin(2.0f * M_PI * 441.0f * time)); // render 441 HZ sine wave + } } int main(int argc, char* argv[]) { - Audio* audio = Audio::get(); - - AudioSetting setting; - setting.freq = 22050; - setting.format = AUDIO_S16; - setting.channels = 2; + Audio* audio = SDLAudio::get(); + SDLAudioSetting setting; + setting.rate = 44100; + setting.resolution = AUDIO_S16SYS; + setting.channels = 1; setting.callback = fill_audio; - setting.samples = 1024; - setting.userdata = NULL; + setting.samples = 44100; + setting.userdata = &sample_nr; audio->init(&setting); while (true) { SDL_Delay(100); } + audio->quit(); return 0; } diff --git a/src/libjin/utils/utils.h b/src/libjin/utils/utils.h index 1a4ef35..d597c83 100644 --- a/src/libjin/utils/utils.h +++ b/src/libjin/utils/utils.h @@ -1,12 +1,6 @@ #ifndef __JIN_UTILS_H #define __JIN_UTILS_H -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#define clamp(a, mi,ma) min(max(a,mi),ma) - -#define within(a,min,max) (a >= min && a <= max) -#define without(a,min,max) (a < min || a > max) #include "macros.h" #include "endian.h" diff --git a/src/lua/audio/luaopen_Source.cpp b/src/lua/audio/luaopen_Source.cpp new file mode 100644 index 0000000..8ecaf7e --- /dev/null +++ b/src/lua/audio/luaopen_Source.cpp @@ -0,0 +1,26 @@ +#include "lua/luax.h" + +namespace jin +{ +namespace lua +{ + + static int l_play(lua_State* L) + { + + return 0; + } + + static const luaL_Reg f[] = { + {"play", l_play}, + {0, 0} + }; + + int luaopen_Source(lua_State* L) + { + + return 1; + } + +} +} diff --git a/src/lua/audio/luaopen_audio.cpp b/src/lua/audio/luaopen_audio.cpp new file mode 100644 index 0000000..992c3ec --- /dev/null +++ b/src/lua/audio/luaopen_audio.cpp @@ -0,0 +1,39 @@ +#include + +#include "lua/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + static int l_init(lua_State* L) + { + if (SDL_Init(SDL_INIT_AUDIO) < 0) + { + luax_error(L, "could not init audio"); + luax_pushboolean(L, false); + return 1; + } + } + + static int l_newSound(lua_State* L) + { + + return 0; + } + + static const luaL_Reg f[] = { + {"init", l_init}, + {"Sound", l_newSound}, + {0, 0} + }; + + int luaopen_audio(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/core/luaopen_core.cpp b/src/lua/core/luaopen_core.cpp new file mode 100644 index 0000000..c8df1b6 --- /dev/null +++ b/src/lua/core/luaopen_core.cpp @@ -0,0 +1,43 @@ +#include "lua/luax.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + using namespace jin::core; + + static int l_running(lua_State* L) + { + bool running = Game::get()->running(); + luax_pushboolean(L, running); + return 1; + } + + static int l_quit(lua_State* L) + { + Game::get()->quit(); + return 0; + } + + static int l_exit(lua_State* L) + { + Game::get()->exit(); + return 0; + } + + static const luaL_Reg f[] = { + {"running", l_running}, + {"quit", l_quit}, // for end game loop + {"exit", l_exit}, // for exit whole game + {0, 0} + }; + + int luaopen_core(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/debug/luaopen_debug.cpp b/src/lua/debug/luaopen_debug.cpp new file mode 100644 index 0000000..b33c058 --- /dev/null +++ b/src/lua/debug/luaopen_debug.cpp @@ -0,0 +1,8 @@ +namespace jin +{ +namespace debug +{ + + +} +} \ No newline at end of file diff --git a/src/lua/embed/boot.lua b/src/lua/embed/boot.lua new file mode 100644 index 0000000..2f6fa93 --- /dev/null +++ b/src/lua/embed/boot.lua @@ -0,0 +1,132 @@ +--[[ + program entry +]] + +local function _onEvent(e) + -- update keyboard status + if e.type == "keydown" then + jin.keyboard.set(e.key, true) + elseif e.type == "keyup" then + jin.keyboard.set(e.key, false) + end + + -- call user onEvent function + if jin.core.onEvent then + jin.core.onEvent(e) + end +end + +------------------------------------------------- +-- init file system +------------------------------------------------- +jin._argv[2] = jin._argv[2] or '.' +jin.filesystem.init() +jin.filesystem.mount(jin._argv[2]) + +-- config +local conf = {} +if jin.filesystem.exist("config.lua") then + conf = require "config" +end +conf.width = conf.width or 600 +conf.height = conf.height or 500 +conf.fps = conf.fps or 60 +conf.title = conf.title or ("jin v" .. jin.version()) + +-- init video subsystem +jin.graphics.init(conf.width,conf.height,conf.title) + +-- open debug mode, must after jin.graphics.init +if jin._argv[3] == '-d' then + jin.debug.init() +end + +function jin.core.run() + local now = jin.time.second() + local last = now + local fsec = 1/conf.fps + -- for loading resources + if jin.core.load then + jin.core.load() + end + local dt = 0 + while(jin.core.running()) do + -- frame controle + last = now + now = jin.time.second() + if (now - last) < fsec then + jin.time.sleep(fsec - now + last) + end + + -- handle events + for _, e in pairs(jin.event.poll()) do + if _onEvent then + _onEvent(e) + end + end + + -- update + dt = now - last + if dt < fsec then + dt = fsec + end + if jin.core.onUpdate then + jin.core.onUpdate(dt) + end + + -- bind to default render buffer + jin.graphics.bind() + jin.graphics.clear() + jin.graphics.color() + jin.graphics.study() + + -- custom drawing + if jin.core.onDraw then + jin.core.onDraw() + end + + -- render debug window + if jin.debug.status() then + jin.debug.render() + end + + -- swap window buffer + jin.graphics.present() + + end +end + +local function onError(msg) + local tab = ' ' + print("Error:\n" .. msg) + function jin.core.onEvent(e) + if e.type == 'quit' then + jin.core.quit() + end + end + local ww, wh = jin.graphics.size() + function jin.core.onDraw() + jin.graphics.write("Error: ", 10, 10, 30, 3, 30) + jin.graphics.write(msg, 10, 50) + end +end + +if jin.filesystem.exist("main.lua") then + -- require main game script + xpcall(function() require"main" end, onError) + jin.core.run() +else + -- no game + function jin.core.onEvent(e) + if e.type == 'quit' then + jin.core.quit() + end + end + function jin.core.onDraw() + jin.graphics.clear(111, 134, 125, 255) + local ww, wh = jin.graphics.size() + local fw, fh = jin.graphics.box("no game", 20, 1, 20) + jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) + end + jin.core.run() +end diff --git a/src/lua/embed/boot.lua.h b/src/lua/embed/boot.lua.h new file mode 100644 index 0000000..21c1899 --- /dev/null +++ b/src/lua/embed/boot.lua.h @@ -0,0 +1,156 @@ +/* boot.lua */ +static const char* boot_lua = R"( + +-- init filesystem +jin._argv[2] = jin._argv[2] or '.' +jin.filesystem.init() +jin.filesystem.mount(jin._argv[2]) + +-- config +local conf = {} +if jin.filesystem.exist("config.lua") then + conf = require "config" +end +conf.width = conf.width or 600 +conf.height = conf.height or 500 +conf.fps = conf.fps or 60 +conf.vsync = conf.vsync or false +conf.title = conf.title or ("jin v" .. jin.version()) + +-- initialize subsystems +jin.graphics.init(conf) +--jin.audio.init(conf) + +-- open debug mode, must after jin.graphics.init +if jin._argv[3] == '-d' then + jin.debug.init() +end + +function jin.core.run() + local load = jin.core.load + local running = jin.core.running + local second = jin.time.second + local sleep = jin.time.sleep + local poll = jin.event.poll + local unbind = jin.graphics.unbind + local clear = jin.graphics.clear + local color = jin.graphics.color + local study = jin.graphics.study + local onDraw = jin.core.onDraw + local onUpdate = jin.core.onUpdate + local onEvent = jin.core.onEvent + local present = jin.graphics.present + local setkey = jin.keyboard.set + + local dstatus = jin.debug.status + local drender = jin.debug.render + + local fps = conf.fps + + local _onEvent = function (e) + if e.type == "keydown" then + setkey(e.key, true) + elseif e.type == "keyup" then + setkey(e.key, false) + end + if onEvent then + onEvent(e) + end + end + + if load then + load() + end + + local now = second() + local last = now + local fsec = 1/fps + local dt = 0 + + while(running()) do + -- frame controle + last = now + now = second() + dt = now - last + if dt < fsec then + sleep(fsec - dt) + dt = fsec + end + + -- handle events + for _, e in pairs(poll()) do + if _onEvent then + _onEvent(e) + end + end + + -- update + if onUpdate then + onUpdate(dt) + end + + -- bind to default render buffer + unbind() + clear() + color() + study() + + -- custom drawing + if onDraw then + onDraw() + end + + -- render debug window + if dstatus() then + drender() + end + + -- swap window buffer + present() + end +end + +local function onError(msg) + local tab = ' ' + print("Error:\n" .. msg) + function jin.core.onEvent(e) + if e.type == 'quit' then + jin.core.quit() + end + end + local ww, wh = jin.graphics.size() + function jin.core.onDraw() + jin.graphics.write("Error: ", 10, 10, 30, 3, 30) + jin.graphics.write(msg, 10, 50) + end +end + +local function main() + if jin.filesystem.exist("main.lua") then + -- require main game script + xpcall(function() require"main" end, onError) + jin.core.run() + else + -- no game + function jin.core.onEvent(e) + if e.type == 'quit' then + jin.core.quit() + end + end + function jin.core.onDraw() + jin.graphics.clear(111, 134, 125, 255) + local ww, wh = jin.graphics.size() + local fw, fh = jin.graphics.box("no game", 20, 1, 20) + jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) + end + jin.core.run() + end + -- quit subsystems + jin.graphics.destroy() + -- exit whole game + jin.core.exit() +end + +main() + +)"; diff --git a/src/lua/embed/debug.lua b/src/lua/embed/debug.lua new file mode 100644 index 0000000..76f59ed --- /dev/null +++ b/src/lua/embed/debug.lua @@ -0,0 +1,128 @@ +--[[ + for debug purpose + +-------------------+ + |debug msg old | + |... | + |... | + |... | + |debug msg new | + +-------------------+ +]] + +jin.debug = jin.debug or {} + +-- render panel +local panel = nil + +local debug = false + +-- debug msg buffer +local buffer = {} + +-- configure +local bsize = 10 +local fsize = 15 +local lheight = 18 +local alpha = 220 +local margin = 10 + +-- refresh buffer or not +local refresh = true + +function jin.debug.init() + debug = true + panel = jin.graphics.Canvas(jin.graphics.size()) +end + +-- set buffer size +function jin.debug.size(c) + bsize = c +end + +function jin.debug.print(msg) + if not debug then return end + + msg = tostring(msg) + local tp = type(msg) + if tp ~= "string" and tp ~= "number" then + msg = string.format("print failed, expect string or number but get a %s", tp) + end + + -- remove the first one (old msg) + if #buffer >= bsize then + table.remove(buffer, 1) + end + + buffer[#buffer + 1] = msg + refresh = true +end + +-- clear debug buffer +function jin.debug.clear() + buffer = {} +end + +local function getStrHeight(str, lheight) + local h = lheight + if #str == 0 then + h = 0 + end + for i = 1, #str do + local c = string.sub(str, i, i) + if c == '\n' then + h = h + lheight + end + end + return h +end + +local function getBgQuad() + local width, height = 0, 0 + for i = 1, #buffer do + local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) + height = height + h + if width < w then + width = w + end + end + return width, height +end + +-- render to screen +function jin.debug.render() + if not debug then return end + + if refresh then + + jin.graphics.bind(panel) + + jin.graphics.clear(0, 0, 0, 0) + + jin.graphics.study() + + local ww, wh = jin.graphics.size() + local bgw, bgh = getBgQuad() + jin.graphics.color(0, 0, 0, alpha) + jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) + + jin.graphics.color() + local y = wh + for i = #buffer, 1, -1 do + local msg = buffer[i] + local h = getStrHeight(msg, lheight) + y = y - h + jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) + end + + jin.graphics.bind() + + refresh = false + end + + jin.graphics.color() + jin.graphics.draw(panel, 0, 0) +end + +function jin.debug.status() + return debug +end diff --git a/src/lua/embed/debug.lua.h b/src/lua/embed/debug.lua.h new file mode 100644 index 0000000..79c95ba --- /dev/null +++ b/src/lua/embed/debug.lua.h @@ -0,0 +1,132 @@ +/* debug.lua */ +static const char* debug_lua = R"( +--[[ + for debug purpose + +-------------------+ + |debug msg old | + |... | + |... | + |... | + |debug msg new | + +-------------------+ +]] + +jin.debug = jin.debug or {} + +-- render panel +local panel = nil + +local debug = false + +-- debug msg buffer +local buffer = {} + +-- configure +local bsize = 10 +local fsize = 15 +local lheight = 18 +local alpha = 220 +local margin = 10 + +-- refresh buffer or not +local refresh = true + +function jin.debug.init() + debug = true + panel = jin.graphics.Canvas(jin.graphics.size()) +end + +-- set buffer size +function jin.debug.size(c) + bsize = c +end + +function jin.debug.print(msg) + if not debug then return end + + msg = tostring(msg) + local tp = type(msg) + if tp ~= "string" and tp ~= "number" then + msg = string.format("print failed, expect string or number but get a %s", tp) + end + + -- remove the first one (old msg) + if #buffer >= bsize then + table.remove(buffer, 1) + end + + buffer[#buffer + 1] = msg + refresh = true +end + +-- clear debug buffer +function jin.debug.clear() + buffer = {} +end + +local function getStrHeight(str, lheight) + local h = lheight + if #str == 0 then + h = 0 + end + for i = 1, #str do + local c = string.sub(str, i, i) + if c == '\n' then + h = h + lheight + end + end + return h +end + +local function getBgQuad() + local width, height = 0, 0 + for i = 1, #buffer do + local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) + height = height + h + if width < w then + width = w + end + end + return width, height +end + +-- render to screen +function jin.debug.render() + if not debug then return end + + if refresh then + + jin.graphics.bind(panel) + + jin.graphics.clear(0, 0, 0, 0) + + jin.graphics.study() + + local ww, wh = jin.graphics.size() + local bgw, bgh = getBgQuad() + jin.graphics.color(0, 0, 0, alpha) + jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) + + jin.graphics.color() + local y = wh + for i = #buffer, 1, -1 do + local msg = buffer[i] + local h = getStrHeight(msg, lheight) + y = y - h + jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) + end + + jin.graphics.bind() + + refresh = false + end + + jin.graphics.color() + jin.graphics.draw(panel, 0, 0) +end + +function jin.debug.status() + return debug +end + +)"; \ No newline at end of file diff --git a/src/lua/embed/embed.h b/src/lua/embed/embed.h new file mode 100644 index 0000000..2ef8b75 --- /dev/null +++ b/src/lua/embed/embed.h @@ -0,0 +1,51 @@ +#ifndef __JIN_LUA_EMBED_H +#define __JIN_LUA_EMBED_H +#include + +namespace jin +{ +namespace embed +{ + + /** + * embed lua script to context. + */ +#define embed(L, script, name) \ + if(luaL_loadbuffer(L, script, strlen(script), name) == 0)\ + lua_call(L, 0, 0); + + /** + * embed structure. + */ + struct jin_Embed + { + const char* fname, *source; + }; + + static void boot(lua_State* L) + { + // embed scripts + #include "graphics.lua.h" // graphics + #include "keyboard.lua.h" // keyboard + #include "mouse.lua.h" // mouse + #include "debug.lua.h" // debug + #include "boot.lua.h" // boot + + // embed scripts + const jin_Embed scripts[] = { + {"graphics.lua", graphics_lua}, + {"keyboard.lua", keyboard_lua}, + {"mouse.lua", mouse_lua}, + {"debug.lua", debug_lua}, + {"boot.lua", boot_lua}, + {0, 0} + }; + + // load all emebd lua scripts + for (int i = 0; scripts[i].fname; ++i) + embed(L, scripts[i].source, scripts[i].fname); + } +} +} + +#endif \ No newline at end of file diff --git a/src/lua/embed/graphics.lua b/src/lua/embed/graphics.lua new file mode 100644 index 0000000..03a891d --- /dev/null +++ b/src/lua/embed/graphics.lua @@ -0,0 +1,6 @@ +----------------- +-- jin.graphics +----------------- + +jin.graphics = jin.graphics or {} + diff --git a/src/lua/embed/graphics.lua.h b/src/lua/embed/graphics.lua.h new file mode 100644 index 0000000..85cf979 --- /dev/null +++ b/src/lua/embed/graphics.lua.h @@ -0,0 +1,8 @@ +/* graphics.lua */ +static const char* graphics_lua = R"( +----------------- +-- jin.graphics +----------------- + +jin.graphics = jin.graphics or {} +)"; diff --git a/src/lua/embed/keyboard.lua b/src/lua/embed/keyboard.lua new file mode 100644 index 0000000..08214f8 --- /dev/null +++ b/src/lua/embed/keyboard.lua @@ -0,0 +1,16 @@ +--[[ + jin.keyboard extension +]] + +jin.keyboard = jin.keyboard or {} + +local keys = {} + +function jin.keyboard.isDown(k) + return keys[k] +end + +function jin.keyboard.set(k, status) + keys[k] = status +end + diff --git a/src/lua/embed/keyboard.lua.h b/src/lua/embed/keyboard.lua.h new file mode 100644 index 0000000..66e3c2a --- /dev/null +++ b/src/lua/embed/keyboard.lua.h @@ -0,0 +1,20 @@ + +static const char* keyboard_lua = R"( +--[[ + jin.keyboard extension +]] + +jin.keyboard = jin.keyboard or {} + +local keys = {} + +function jin.keyboard.isDown(k) + return keys[k] +end + +function jin.keyboard.set(k, status) + keys[k] = status +end + + +)"; diff --git a/src/lua/embed/mouse.lua b/src/lua/embed/mouse.lua new file mode 100644 index 0000000..9dcd472 --- /dev/null +++ b/src/lua/embed/mouse.lua @@ -0,0 +1,15 @@ +--[[ + jin.mouse extension +]] + +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 diff --git a/src/lua/embed/mouse.lua.h b/src/lua/embed/mouse.lua.h new file mode 100644 index 0000000..f57d08c --- /dev/null +++ b/src/lua/embed/mouse.lua.h @@ -0,0 +1,18 @@ +static const char* mouse_lua = R"( +--[[ + jin.mouse extension +]] + +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/path.lua b/src/lua/embed/path.lua new file mode 100644 index 0000000..5b99dd2 --- /dev/null +++ b/src/lua/embed/path.lua @@ -0,0 +1,15 @@ +--[[ + jin.path extension +]] + +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 + diff --git a/src/lua/embed/path.lua.h b/src/lua/embed/path.lua.h new file mode 100644 index 0000000..b398c99 --- /dev/null +++ b/src/lua/embed/path.lua.h @@ -0,0 +1,18 @@ +/* path.lua */ +static const char* path_lua = R"( +--[[ + jin.path extension +]] + +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/event/luaopen_event.cpp b/src/lua/event/luaopen_event.cpp new file mode 100644 index 0000000..8a84c97 --- /dev/null +++ b/src/lua/event/luaopen_event.cpp @@ -0,0 +1,124 @@ +/** +* Event module +*/ +#include + +#include "lua/luax.h" +#include "libjin/jin.h" + +using namespace jin::input; + +namespace jin +{ +namespace lua +{ + + static inline const char* buttonstr(int id) { + switch (id) { + case 1: return "left"; + case 2: return "middle"; + case 3: return "right"; + case 4: return "wheelup"; + case 5: return "wheeldown"; + default: return "?"; + } + } + + static inline const char* wheelstr(int dir) + { + + } + + /** + * Load event poll, return a iterator(a table). + */ + static int l_event_poll(lua_State *L) + { + // table to store events + luax_newtable(L); + SDL_Event e; + int i = 1; + poll: + while (SDL_PollEvent(&e)) + { + // each event is a table + luax_newtable(L); + switch (e.type) + { + case SDL_QUIT: + luax_setfield_string(L, "type", "quit"); + break; + + case SDL_KEYDOWN: + luax_setfield_string(L, "type", "keydown"); + luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); + break; + + case SDL_KEYUP: + luax_setfield_string(L, "type", "keyup"); + luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); + break; + + case SDL_MOUSEMOTION: + luax_setfield_string(L, "type", "mousemotion"); + luax_setfield_number(L, "x", e.motion.x); + luax_setfield_number(L, "y", e.motion.y); + break; + + case SDL_MOUSEBUTTONDOWN: + luax_setfield_string(L, "type", "mousebuttondown"); + luax_setfield_string(L, "button", buttonstr(e.button.button)); + luax_setfield_number(L, "x", e.button.x); + luax_setfield_number(L, "y", e.button.y); + break; + + case SDL_MOUSEBUTTONUP: + luax_setfield_string(L, "type", "mousebuttonup"); + luax_setfield_string(L, "button", buttonstr(e.button.button)); + luax_setfield_number(L, "x", e.button.x); + luax_setfield_number(L, "y", e.button.y); + break; + + case SDL_MOUSEWHEEL: + luax_setfield_string(L, "type", "wheel"); + if(e.wheel.x == -1) + luax_setfield_string(L, "x", "left"); + else if(e.wheel.x == 1) + luax_setfield_string(L, "x", "right"); + else + luax_setfield_string(L, "x", "none"); + + if (e.wheel.y == -1) + luax_setfield_string(L, "y", "near"); + else if (e.wheel.y == 1) + luax_setfield_string(L, "y", "far"); + else + luax_setfield_string(L, "y", "none"); + break; + + default: + /* ignore other events */ + luax_pop(L, 1); // pop table out + goto poll; + break; + } + luax_rawseti(L, -2, i++); + } + return 1; + } + + static const luaL_Reg f[] = { + {"poll", l_event_poll}, + {0 ,0 } + }; + + /** + * load event module + */ + int luaopen_event(lua_State* L) + { + luax_newlib(L, f); + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/filesystem/luaopen_filesystem.cpp b/src/lua/filesystem/luaopen_filesystem.cpp new file mode 100644 index 0000000..db0216b --- /dev/null +++ b/src/lua/filesystem/luaopen_filesystem.cpp @@ -0,0 +1,142 @@ +#include "lua/luax.h" +#include "libjin/jin.h" +#include + +using namespace jin::fs; + +namespace jin +{ +namespace lua +{ + + static struct + { + Filesystem* fs; + } context; + + static int l_init(lua_State* L) + { + context.fs = Filesystem::get(); + return 0; + } + + /** + * set current game root, like + * C:/jin/games/tank/ + */ + static int l_mount(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + context.fs->mount(path); + return 0; + } + + /** + * + */ + static int l_isDir(lua_State *L) + { + const char* path = luax_checkstring(L, 1); + int r = context.fs->isDir(path); + luax_pushboolean(L, r); + return 1; + } + + /** + * + */ + static int l_exist(lua_State * L) + { + const char* path = luax_checkstring(L, 1); + int r = context.fs->exists(path); + luax_pushboolean(L, r); + return 1; + } + + static int l_isdir(lua_State* L) + { + const char* path = luax_checkstring(L, 1); + int r = context.fs->isDir(path); + luax_pushboolean(L, r); + return 1; + } + + // load but dont run it + static int loadf(lua_State* L) + { + const char* filename = lua_tostring(L, -1); + Buffer bf; + context.fs->read(filename, &bf); + luax_loadbuffer(L, (const char*)bf.data, bf.size, filename); + return 1; + } + + static int loader(lua_State* L) + { + const char * filename = lua_tostring(L, -1); + + std::string tmp(filename); + tmp += ".lua"; + + int size = tmp.size(); + + for (int i = 0; iexists(tmp.c_str())) + { + lua_pop(L, 1); + lua_pushstring(L, tmp.c_str()); + // Ok, load it. + return loadf(L); + } + + tmp = filename; + size = tmp.size(); + for (int i = 0; iisDir(tmp.c_str())) + { + tmp += "/init.lua"; + if (context.fs->exists(tmp.c_str())) + { + lua_pop(L, 1); + lua_pushstring(L, tmp.c_str()); + // Ok, load it. + return loadf(L); + } + } + + lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); + return 1; + } + + static const luaL_Reg f[] = { + {"init", l_init}, + {"mount", l_mount}, + {"isdir", l_isDir}, + {"exist", l_exist}, + {0, 0} + }; + + int luaopen_filesystem(lua_State* L) + { + luax_newlib(L, f); + luax_register_searcher(L, loader, 1); + return 0; + } + +} +} \ No newline at end of file diff --git a/src/lua/graphics/luaopen_Canvas.cpp b/src/lua/graphics/luaopen_Canvas.cpp new file mode 100644 index 0000000..5dede84 --- /dev/null +++ b/src/lua/graphics/luaopen_Canvas.cpp @@ -0,0 +1,75 @@ +#include "lua/luax.h" +#include "lua/luaopen_types.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::render; + + static inline Canvas* checkCanvas(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + if (proxy != nullptr) + return (Canvas*)proxy->object; + return nullptr; + } + + static int l_getWidth(lua_State* L) + { + Canvas* c = checkCanvas(L); + luax_pushnumber(L, c->getWidth()); + return 1; + } + + static int l_getHeight(lua_State* L) + { + Canvas* c = checkCanvas(L); + luax_pushnumber(L, c->getHeight()); + return 1; + } + + static int l_getSize(lua_State* L) + { + Canvas* c = checkCanvas(L); + luax_pushnumber(L, c->getWidth()); + luax_pushnumber(L, c->getHeight()); + return 2; + } + + static int l_setAnchor(lua_State* L) + { + Canvas* c = checkCanvas(L); + int x = luax_checknumber(L, 1); + int y = luax_checknumber(L, 2); + c->setAnchor(x, y); + return 0; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + Canvas* canvas = (Canvas*)proxy->object; + delete canvas; + return 0; + } + + static const luaL_Reg f[] = { + {"__gc", l_gc}, + {"getWidth", l_getWidth}, + {"getHeight", l_getHeight}, + {"getSize", l_getSize}, + {"setAnchor", l_setAnchor}, + {0, 0 } + }; + + int luaopen_Canvas(lua_State* L) + { + luax_newtype(L, TYPE_CANVAS, f); + return 0; + } + +}// lua +}// jin \ No newline at end of file diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp new file mode 100644 index 0000000..387b220 --- /dev/null +++ b/src/lua/graphics/luaopen_Font.cpp @@ -0,0 +1,45 @@ +#include "lua/luax.h" +#include "lua/luaopen_types.h" +#include "libjin/jin.h" + +using namespace jin::render; + +namespace jin +{ +namespace lua +{ + + static int l_gc(lua_State* L) + { + return 0; + } + + static int l_box(lua_State* L) + { + Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); + const char* text = luax_checkstring(L, 2); + int fheight = luax_checknumber(L, 3); + int spacing = luax_checknumber(L, 4); + int lheight = luax_checknumber(L, 5); + int w, h; + font->box(text, fheight, lheight, spacing, &w, &h); + luax_pushnumber(L, w); + luax_pushnumber(L, h); + return 2; + } + + static const luaL_Reg f[] = { + {"__gc", l_gc}, + {"box", l_box}, + {0, 0} + }; + + int luaopen_Font(lua_State* L) + { + luax_newtype(L, TYPE_FONT, f); + + return 0; + } + +} +} \ No newline at end of file diff --git a/src/lua/graphics/luaopen_Image.cpp b/src/lua/graphics/luaopen_Image.cpp new file mode 100644 index 0000000..c5648a5 --- /dev/null +++ b/src/lua/graphics/luaopen_Image.cpp @@ -0,0 +1,89 @@ +#include "lua/luax.h" +#include "libjin/jin.h" +#include "lua/luaopen_types.h" + +using namespace jin::render; + +namespace jin +{ +namespace lua +{ + + static inline Image* checkImage(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); + if (proxy != 0 && proxy != nullptr) + return (Image*)proxy->object; + return nullptr; + } + + static int l_getWidth(lua_State* L) + { + Image* i = checkImage(L); + luax_pushnumber(L, i->getWidth()); + return 1; + } + + static int l_getHeight(lua_State *L) + { + Image* i = checkImage(L); + luax_pushnumber(L, i->getHeight()); + return 1; + } + + static int l_getPixel(lua_State* L) + { + Image* i = checkImage(L); + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + color c = i->getPixel(x, y); + luax_pushnumber(L, c.rgba.r); + luax_pushnumber(L, c.rgba.g); + luax_pushnumber(L, c.rgba.b); + luax_pushnumber(L, c.rgba.a); + return 4; + } + + static int l_setAnchor(lua_State* L) + { + Image* i = checkImage(L); + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + i->setAnchor(x, y); + return 0; + } + + static int l_getSize(lua_State* L) + { + Image* i = checkImage(L); + luax_pushnumber(L, i->getWidth()); + luax_pushnumber(L, i->getHeight()); + return 2; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); + Image* img = (Image*)proxy->object; + delete img; + return 0; + } + + static const luaL_Reg f[] = { + {"__gc", l_gc}, + {"getWidth", l_getWidth}, + {"getHeight", l_getHeight}, + {"getSize", l_getSize}, + {"getPixel", l_getPixel}, + {"setAnchor", l_setAnchor}, + {0, 0 } + }; + + int luaopen_Image(lua_State* L) + { + luax_newtype(L, TYPE_IMAGE, f); + return 0; + } + +}// graphics +}// jin \ No newline at end of file diff --git a/src/lua/graphics/luaopen_JSL.cpp b/src/lua/graphics/luaopen_JSL.cpp new file mode 100644 index 0000000..93ca5d3 --- /dev/null +++ b/src/lua/graphics/luaopen_JSL.cpp @@ -0,0 +1,146 @@ +#include "lua/luax.h" +#include "libjin/jin.h" +#include "lua/luaopen_types.h" + +namespace jin +{ +namespace lua +{ + + using namespace render; + + static inline JSLProgram* checkJSLProgram(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); + if(proxy != nullptr) + return (JSLProgram*)proxy->object; + return nullptr; + } + + static enum VARIABLE_TYPE + { + INVALID = 0, + + NUMBER, + IMAGE, + CANVAS, + VEC2, + VEC3, + VEC4, + COLOR, + }; + + static VARIABLE_TYPE strtotype(const char* str) + { + std::string s = std::string(str); + if (s == "number") return NUMBER; + else if (s == "Image") return IMAGE; + else if (s == "Canvas") return CANVAS; + else if (s == "vec2") return VEC2; + else if (s == "vec3") return VEC3; + else if (s == "vec4") return VEC4; + else if (s == "Color") return COLOR; + else return INVALID; + } + + /** + * Use send function send variables to JSL program. + */ + static int l_send(lua_State* L) + { + JSLProgram* jsl = checkJSLProgram(L); + // number Image Texel + const char* typestr = luax_checkstring(L, 2); + // variable name + const char* variable = luax_checkstring(L, 3); + if (typestr != nullptr) + { + int type = strtotype(typestr); + switch (type) + { + case NUMBER: + { + float number = luax_checknumber(L, 4); + jsl->sendFloat(variable, number); + break; + } + case IMAGE: + { + Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); + Image* img = (Image*)proxy->object; + jsl->sendImage(variable, img); + break; + } + case CANVAS: + { + Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); + Canvas* canvas = (Canvas*)proxy->object; + jsl->sendCanvas(variable, canvas); + break; + } + case VEC2: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + jsl->sendVec2(variable, x, y); + break; + } + case VEC3: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + float z = luax_checknumber(L, 6); + jsl->sendVec3(variable, x, y, z); + break; + } + case VEC4: + { + float x = luax_checknumber(L, 4); + float y = luax_checknumber(L, 5); + float z = luax_checknumber(L, 6); + float w = luax_checknumber(L, 7); + jsl->sendVec4(variable, x, y, z, w); + break; + } + case COLOR: + { + color col; + col.rgba.r = luax_checkinteger(L, 4); + col.rgba.g = luax_checkinteger(L, 5); + col.rgba.b = luax_checkinteger(L, 6); + col.rgba.a = luax_checkinteger(L, 7); + jsl->sendColor(variable, &col); + break; + } + default: + return 0; + } + } + return 1; + } + + static int l_gc(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); + JSLProgram* jsl = (JSLProgram*)proxy->object; + delete jsl; + return 0; + } + + static const luaL_Reg f[] = { + {"__gc", l_gc }, + {"send", l_send}, + {0, 0} + }; + + /** + * JSL program + */ + int luaopen_JSL(lua_State* L) + { + luax_newtype(L, TYPE_JSL, f); + return 0; + } + +} +} \ No newline at end of file diff --git a/src/lua/graphics/luaopen_graphics.cpp b/src/lua/graphics/luaopen_graphics.cpp new file mode 100644 index 0000000..6fa5a4d --- /dev/null +++ b/src/lua/graphics/luaopen_graphics.cpp @@ -0,0 +1,524 @@ +#include "libjin/jin.h" +#include "lua/luax.h" +#include "lua/luaopen_types.h" +#include "lua/embed/graphics.lua.h" + +namespace jin +{ +namespace lua +{ + using namespace render; + using namespace fs; + + /** + * jin.graphics context, storge some module + * shared variables. + */ + static struct + { + color curRenderColor; + Font* curFont = 0; + Font* defaultFont = 0; + } context; + + /** + * Init video system. + * jin.graphics.init(width, height, title) + */ + static int l_init(lua_State* L) + { + Window* wnd = Window::get(); + WindowSetting setting; + setting.width = luax_getfield_integer(L, 1, "width"); + setting.height = luax_getfield_integer(L, 1, "height"); + setting.title = luax_getfield_string(L, 1, "title"); + setting.vsync = luax_getfield_bool(L, 1, "vsync"); + luax_pushboolean(L, wnd->init(&setting)); + return 1; + } + + static int l_destroy(lua_State* L) + { + Window* wnd = Window::get(); + wnd->quit(); + return 0; + } + + /** + * Get windows size. + */ + static int l_getSize(lua_State* L) + { + Window* wnd = Window::get(); + luax_pushnumber(L, wnd->getW()); + luax_pushnumber(L, wnd->getH()); + return 2; + } + + /** + * Create a image userdata and set metatable for it. + */ + static int l_newImage(lua_State* L) + { + Filesystem* fs = Filesystem::get(); + const char* f = luax_checkstring(L, 1); + if (!fs->exists(f)) + { + printf("Error: no such image %s\n", f); + exit(1); + } + Buffer b; + fs->read(f, &b); + + Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy)); + Image* img = new Image((const char*)b.data, b.size); + proxy->bind(img); + return 1; + } + + /** + * Create a new JSL program. + * graphics.Shader(program) + */ + static int l_newShader(lua_State* L) + { + Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_JSL, sizeof(JSLProgram)); + const char* program = luax_checkstring(L, 1); + JSLProgram* jsl = new JSLProgram(program); + proxy->bind(jsl); + return 1; + } + + /** + * Create a new Canvas, don't use it in loop, very slow. + * jin.graphics.newCanvas(w, h) + */ + 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, TYPE_CANVAS, sizeof(Proxy)); + Canvas* cvs = new Canvas(w, h); + proxy->bind(cvs); + return 1; + } + + static int l_clear(lua_State* L) + { + if (luax_gettop(L) == 0) + { + glClearColor(0, 0, 0, 1); + } + else + { + int r = luax_checknumber(L, 1); + int g = luax_checknumber(L, 2); + int b = luax_checknumber(L, 3); + int a = luax_checknumber(L, 4); + glClearColor(r / 255.f, g / 255.f, b / 255.f, a / 255.f); + } + glClear(GL_COLOR_BUFFER_BIT); + return 0; + } + + /** + * Swap render buffers, present current buffer to front. + */ + static int l_present(lua_State* L) + { + Window::get()->swapBuffers(); + return 0; + } + + // jin.graphics.draw(x, y, scalex, scaley, r) + static int l_draw(lua_State* L) + { + int x = luax_optnumber(L, 2, 0); + int y = luax_optnumber(L, 3, 0); + float sx = luax_optnumber(L, 4, 1); + float sy = luax_optnumber(L, 5, 1); + float r = luax_optnumber(L, 6, 0); + if (luax_istype(L, 1, TYPE_IMAGE)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Image* img = (Image*)proxy->object; + img->draw(x, y, sx, sy, r); + } + else if (luax_istype(L, 1, TYPE_CANVAS)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + Canvas* p = (Canvas*)proxy->object; + p->draw(x, y, sx, sy, r); + } + else + { + /* wrong type */ + luax_typerror(L, 1, "image or canvas"); + } + return 0; + } + + static int l_setColor(lua_State* L) + { + // jin.graphics.color() to set back to default + // render color + if (luax_gettop(L) == 0) + { + glColor4f(1, 1, 1, 1); + return 0; + } + + context.curRenderColor.rgba.r = luax_checknumber(L, 1); + context.curRenderColor.rgba.g = luax_checknumber(L, 2); + context.curRenderColor.rgba.b = luax_checknumber(L, 3); + context.curRenderColor.rgba.a = luax_checknumber(L, 4); + + glColor4f(context.curRenderColor.rgba.r / 255.f, + context.curRenderColor.rgba.g / 255.f, + context.curRenderColor.rgba.b / 255.f, + context.curRenderColor.rgba.a / 255.f); + return 0; + } + + static int l_getColor(lua_State * L) + { + luax_pushnumber(L, context.curRenderColor.rgba.r); + luax_pushnumber(L, context.curRenderColor.rgba.g); + luax_pushnumber(L, context.curRenderColor.rgba.b); + luax_pushnumber(L, context.curRenderColor.rgba.a); + return 4; + } + + static int l_bindCanvas(lua_State* L) + { + if (luax_gettop(L) == 0) + { + // bind to default canvas + Canvas::unbind(); + return 0; + } + Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); + Canvas* c = (Canvas*)proxy->object; + c->bind(); + return 0; + } + + static int l_unbindCanvas(lua_State* L) + { + Canvas::unbind(); + return 0; + } + + static int l_useShader(lua_State* L) + { + if (luax_gettop(L) == 0) + { + JSLProgram::unuse(); + return 0; + } + if (luax_istype(L, 1, TYPE_JSL)) + { + Proxy* proxy = (Proxy*)luax_toudata(L, 1); + JSLProgram* jsl = (JSLProgram*)proxy->object; + jsl->use(); + } + else + { + luax_typerror(L, 1, "JSL shader"); + } + return 0; + } + + static int l_unuseShader(lua_State* L) + { + JSLProgram::unuse(); + return 0; + } + + static int l_setBlend(lua_State* L) + { + + return 0; + } + + static RENDER_MODE strtomode(const char* str) + { + std::string s = std::string(str); + if (s == "fill") return FILL; + else if (s == "line") return LINE; + else return NONE; + } + + /** + * draw pixel to screen + * jin.graphics.pixel(x, y) + */ + static int l_drawpoint(lua_State* L) + { + int x = luax_checknumber(L, 1); + int y = luax_checknumber(L, 2); + render::point(x, y); + + return 0; + } + + static int l_drawLine(lua_State* L) + { + int x1 = luax_checknumber(L, 1); + int y1 = luax_checknumber(L, 2); + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); + render::line(x1, y1, x2, y2); + + return 0; + } + + static int l_drawRect(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + RENDER_MODE mode = strtomode(modestr); + if (mode != 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); + render::rect(mode, x, y, w, h); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_drawCircle(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + RENDER_MODE mode = strtomode(modestr); + if (mode != NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + float r = luax_checknumber(L, 4); + render::circle(mode, x, y, r); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_drawTriangle(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + RENDER_MODE mode = strtomode(modestr); + if (mode != NONE) + { + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + + int x2 = luax_checknumber(L, 3); + int y2 = luax_checknumber(L, 4); + + int x3 = luax_checknumber(L, 5); + int y3 = luax_checknumber(L, 6); + + render::triangle(mode, x, y, x2, y2, x3, y3); + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + /** + * draw polygon. + * jin.graphics.polygon(mode, n, {{}, {}, {}...}) + */ + static int l_drawPolygon(lua_State* L) + { + const char* modestr = luax_checkstring(L, 1); + int n = luax_checknumber(L, 2); + RENDER_MODE mode = strtomode(modestr); + if (mode != NONE) + { + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + int tn = luax_tableidxlen(L, 3); + if (tn != n * 2) + { + static char* emsg = \ + "number of polygon vertices doesn't match " \ + "provided n, expect %d numbers but get %d"; + luax_error(L, emsg, n * 2, tn); + return 1; + } + float* p = new float[2 * n]; + for (int i = 1; i <= 2 * n; ++i) + p[i - 1] = luax_rawgetnumber(L, 3, i); + render::polygon(mode, p, n); + delete[] p; + } + else + { + luax_typerror(L, 1, "'fill' or 'line'"); + return 1; + } + + return 0; + } + + static int l_newFont(lua_State* L) + { + Font* font = (Font*)luax_newinstance(L, TYPE_FONT, sizeof(Font)); + const char* path = luax_checkstring(L, 1); + Filesystem* fs = Filesystem::get(); + Buffer b = {}; + if (!fs->exists(path)) + { + printf("Error: no such font %s\n", path); + exit(1); + } + fs->read(path, &b); + font->loadb((const unsigned char*)b.data); + + return 1; + } + + /** + * study font, 0 args for study default font. + */ + static int l_study(lua_State* L) + { + int n = luax_gettop(L); + if (n == 0) + { + if (context.defaultFont == 0) + { + #include "data/font.ttf.h" + // load default font + context.defaultFont = new Font(); + context.defaultFont->loadb(font_ttf); + } + context.curFont = context.defaultFont; + return 0; + } + Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); + context.curFont = font; + return 0; + } + + /** + * draw text with current font(after study). befor write, must + * study a font. + */ + static int l_write(lua_State* L) + { + if (context.curFont == 0) + return 0; + + const char* text = luax_checkstring(L, 1); + int x = luax_checknumber(L, 2); + int y = luax_checknumber(L, 3); + + int fh = luax_optnumber(L, 4, 15); + int spacing = luax_optnumber(L, 5, 1); + int lh = luax_optnumber(L, 6, 18); + + context.curFont->render(text, x, y, fh, spacing, lh); + + return 0; + } + + /** + * get text bound box + */ + static int l_box(lua_State* L) + { + const char* text = luax_checkstring(L, 1); + int fontheight = luax_checknumber(L, 2); + int spacing = luax_checknumber(L, 3); + int lineheight = luax_checknumber(L, 4); + int w, h; + context.curFont->box(text, fontheight, spacing, lineheight, &w, &h); + luax_pushnumber(L, w); + luax_pushnumber(L, h); + return 2; + } + + static const luaL_Reg f[] = { + {"init", l_init}, + {"size", l_getSize}, + {"Image", l_newImage}, + {"Shader", l_newShader}, + {"Canvas", l_newCanvas}, + {"Font", l_newFont}, + /** + * before using box and write + * must call study to set + * current font + */ + {"box", l_box}, + {"write", l_write}, + {"clear", l_clear}, + {"draw", l_draw}, + {"color", l_setColor}, + {"palette", l_getColor}, + {"present", l_present}, + //{"blend", l_setBlend}, + // study font + {"study", l_study}, + // bind canvas + {"bind", l_bindCanvas}, + {"unbind", l_unbindCanvas}, + // use shader + {"use", l_useShader}, + {"unuse", l_unuseShader}, + // draw shapes + {"point", l_drawpoint}, + {"line", l_drawLine}, + {"rect", l_drawRect}, + {"circle", l_drawCircle}, + {"triangle", l_drawTriangle}, + {"polygon", l_drawPolygon}, + // + {"destroy", l_destroy}, + {0, 0} + }; + + extern int luaopen_Image(lua_State* L); + + extern int luaopen_Font(lua_State* L); + + extern int luaopen_Canvas(lua_State* L); + + extern int luaopen_JSL(lua_State* L); + + int luaopen_graphics(lua_State* L) + { + // register types + luaopen_Image(L); + luaopen_Canvas(L); + luaopen_Font(L); + luaopen_JSL(L); + + // load whole lib + luax_newlib(L, f); + + return 1; + } + +}// lua +}// jin diff --git a/src/lua/joypad/joypad.h b/src/lua/joypad/joypad.h new file mode 100644 index 0000000..e8d309b --- /dev/null +++ b/src/lua/joypad/joypad.h @@ -0,0 +1,14 @@ +#ifndef __JIN_JOYPAD_H +#define __JIN_JOYPAD_H + +namespace jin +{ +namespace input +{ + + + +} +} + +#endif \ No newline at end of file diff --git a/src/lua/joypad/luaopen_joypad.cpp b/src/lua/joypad/luaopen_joypad.cpp new file mode 100644 index 0000000..6c98c3b --- /dev/null +++ b/src/lua/joypad/luaopen_joypad.cpp @@ -0,0 +1,21 @@ +#include "libjin/jin.h" +#include "lua/luax.h" + +namespace jin +{ +namespace lua +{ + + static const luaL_Reg f[] = { + { 0, 0 } + }; + + int luaopen_joypad(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } + +} +} \ No newline at end of file diff --git a/src/lua/keyboard/luaopen_keyboard.cpp b/src/lua/keyboard/luaopen_keyboard.cpp new file mode 100644 index 0000000..ea1386c --- /dev/null +++ b/src/lua/keyboard/luaopen_keyboard.cpp @@ -0,0 +1,16 @@ +#include "lua/luax.h" +#include "lua/embed/keyboard.lua.h" + +namespace jin +{ +namespace lua +{ + + int luaopen_keyboard(lua_State* L) + { + luax_newlib(L, 0); + return 1; + } + +} +} \ No newline at end of file diff --git a/src/lua/luaopen_jin.cpp b/src/lua/luaopen_jin.cpp new file mode 100644 index 0000000..93e0422 --- /dev/null +++ b/src/lua/luaopen_jin.cpp @@ -0,0 +1,97 @@ +#include "luaopen_jin.h" +#include "lua/luax.h" + +// embed all lua scripts +#include "embed/embed.h" + +namespace jin +{ +namespace lua +{ + + extern int luaopen_core(lua_State* L); + extern int luaopen_graphics(lua_State* L); + extern int luaopen_audio(lua_State* L); + extern int luaopen_net(lua_State* L); + extern int luaopen_event(lua_State* L); + extern int luaopen_time(lua_State* L); + extern int luaopen_mouse(lua_State* L); + extern int luaopen_keyboard(lua_State* L); + extern int luaopen_filesystem(lua_State* L); + extern int luaopen_joypad(lua_State* L); + + static int l_getversion(lua_State* L) + { + luax_pushstring(L, VERSION); + return 1; + } + + static int l_getAuthor(lua_State* L) + { + luax_pushstring(L, AUTHOR); + return 1; + } + + static int l_getOS(lua_State* L) + { + #ifdef _WIN32 + luax_pushstring(L, "windows"); + #elif defined __unix__ + luax_pushstring(L, "unix"); + #elif defined __APPLE__ + luax_pushstring(L, "macos"); + #endif + return 1; + } + + static const luaL_Reg f[] = { + {"version", l_getversion}, + {"author", l_getAuthor}, + {"os", l_getOS}, + {0, 0} + }; + + // submodules + static const luaL_Reg mods[] = { + {"core", luaopen_core}, + {"event", luaopen_event}, + {"graphics", luaopen_graphics}, + {"time", luaopen_time}, + {"mouse", luaopen_mouse}, + {"keyboard", luaopen_keyboard}, + {"filesystem", luaopen_filesystem}, + {"net", luaopen_net}, + {"audio", luaopen_audio}, + {"joypad", luaopen_joypad}, + {0, 0} + }; + + int luaopen_jin(lua_State* L) + { + // jin module is on top of the stack + luax_newlib(L, f); + + // set to global field + luax_justglobal(L, -1, MODULE_NAME); + + // register submodules + for (int i = 0; mods[i].name; ++i) + { + // open submodules + mods[i].func(L); + luax_setfield(L, -2, mods[i].name); + } + + return 1; + } + + /** + * boot jin + */ + void boot(lua_State* L) + { + jin::embed::boot(L); + } + +} +} \ No newline at end of file diff --git a/src/lua/luaopen_jin.h b/src/lua/luaopen_jin.h new file mode 100644 index 0000000..3d446a5 --- /dev/null +++ b/src/lua/luaopen_jin.h @@ -0,0 +1,45 @@ +/** +* Copyright (C) 2016~2018 chai +* +* Permission is hereby granted, free of charge, to any person obtaining a copyof +* this software and associated documentation files (the "Software"), to deal in +* the Software without restriction, including without limitation the rights to use, +* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the +* Software, and to permit persons to whom the Software is furnished to do so, +* subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef __JIN_M_JIN_H +#define __JIN_M_JIN_H +#include "lua/luax.h" + +#define MODULE_NAME "jin" +#define VERSION "0.1.0" +#define AUTHOR "chai" + +namespace jin +{ +namespace lua +{ + + /** + * open jin lib and boot + */ + int luaopen_jin(lua_State* L); + + void boot(lua_State* L); + +} +} + +#endif \ No newline at end of file diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h new file mode 100644 index 0000000..bb6f5b1 --- /dev/null +++ b/src/lua/luaopen_types.h @@ -0,0 +1,24 @@ +#ifndef __JIN_M_TYPES_H +#define __JIN_M_TYPES_H + +// graphics module +#define TYPE_IMAGE "Image" +#define TYPE_JSL "Shader" +#define TYPE_CANVAS "Canvas" +#define TYPE_FONT "Font" + +// audio module +#define TYPE_SOURCE "Source" + +class Proxy +{ +public: + inline void bind(void* obj) + { + object = obj; + } + + void* object; +}; + +#endif diff --git a/src/lua/luax.h b/src/lua/luax.h new file mode 100644 index 0000000..3b587d3 --- /dev/null +++ b/src/lua/luax.h @@ -0,0 +1,10 @@ +#ifndef __JIN_LUA_LUAX_H +#define __JIN_LUA_LUAX_H + +#include "3rdparty/lua51/lua.h" +#include "3rdparty/lua51/lauxlib.h" +#include "3rdparty/lua51/lualib.h" + +#include "3rdparty/luax/luax.h" + +#endif \ No newline at end of file diff --git a/src/lua/mouse/luaopen_mouse.cpp b/src/lua/mouse/luaopen_mouse.cpp new file mode 100644 index 0000000..a013f3d --- /dev/null +++ b/src/lua/mouse/luaopen_mouse.cpp @@ -0,0 +1,27 @@ +#include "lua/luax.h" +#include "SDL2/SDL.h" +namespace jin +{ +namespace lua +{ + static int l_pos(lua_State* L) + { + int x, y; + SDL_GetMouseState(&x, &y); + luax_pushnumber(L, x); + luax_pushnumber(L, y); + return 2; + } + + static const luaL_Reg f[] = { + {"position", l_pos}, + {0, 0} + }; + + int luaopen_mouse(lua_State* L) + { + luax_newlib(L, f); + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp new file mode 100644 index 0000000..821f273 --- /dev/null +++ b/src/lua/net/luaopen_net.cpp @@ -0,0 +1,110 @@ +/** +* Notice: the net module is not finished yet. +*/ +#include "lua/luax.h" +#include "3rdparty/tekcos/tekcos.h" + +namespace jin +{ +namespace lua +{ + struct + { + tk_TCPsocket* sk; + }context; + + /** + * A table is needed. For example: + * local conf = { + * mode = "server", + * ip = "", + * port = 8000 + * } + */ + static int l_open(lua_State* L) + { + // init context.sk + context.sk = 0; + if (!luax_istable(L, 1)) + { + luax_typerror(L, 1, "table is needed"); + return 0; + } + luax_getfield(L, 1, "mode"); + if (luax_isnil(L, -1)) + {// no mode field + luax_error(L, "mode field is needed, but get nil"); + return 0; + } + const char* mode = luax_checkstring(L, -1); + if (strcmp(mode, "server") == 0 || strcmp(mode, "client") == 0) + { + + if (strcmp(mode, "server") == 0) + {// a server, ignore ip field + + } + else + { + + } + } + else + { + luax_error(L, "\"server\" or \"client\" is needed, but get %s", mode); + return 0; + } + return 1; + } + + static int l_accept(lua_State* L) + { + return 1; + } + + static int l_send(lua_State* L) + { + return 1; + } + + static int l_recv(lua_State* L) + { + return 1; + } + + static int l_close(lua_State* L) + { + return 1; + } + + static int l_nonblocking(lua_State* L) + { + return 1; + } + + // block mode by default + static int l_blocking(lua_State* L) + { + return 1; + } + + static const luaL_Reg f[] = { + {"open", l_open}, + {"accept", l_accept}, + {"send", l_send}, + {"recv", l_recv}, + {"close", l_close}, + {"blocking", l_blocking }, + {"nonblocking", l_nonblocking}, + {0, 0} + }; + + // only tcp + int luaopen_net(lua_State* L) + { + luax_newlib(L, f); + + return 1; + } +} +} \ No newline at end of file diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/lua/time/luaopen_time.cpp b/src/lua/time/luaopen_time.cpp new file mode 100644 index 0000000..f7310dc --- /dev/null +++ b/src/lua/time/luaopen_time.cpp @@ -0,0 +1,34 @@ +#include "lua/luax.h" +#include +namespace jin +{ +namespace lua +{ + + static int l_sec(lua_State* L) + { + luax_pushnumber(L, SDL_GetTicks()/1000.f); + return 1; + } + + static int l_sleep(lua_State* L) + { + double sec = luax_checknumber(L, 1); + SDL_Delay(sec * 1000); + return 0; + } + + static const luaL_Reg f[] = { + {"second", l_sec}, + {"sleep", l_sleep}, + {0, 0}, + }; + + int luaopen_time(lua_State* L) + { + luax_newlib(L, f); + return 1; + } + +} +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4617beb..505f48c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,8 +5,8 @@ #include #endif -#include "3rdparty/luax/luax.h" -#include "script/luaopen_jin.h" +#include "lua/luax.h" +#include "lua/luaopen_jin.h" #include "libjin/jin.h" diff --git a/src/script/audio/luaopen_Source.cpp b/src/script/audio/luaopen_Source.cpp deleted file mode 100644 index 2ab5d16..0000000 --- a/src/script/audio/luaopen_Source.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "3rdparty/luax/luax.h" - -namespace jin -{ -namespace lua -{ - - static int l_play(lua_State* L) - { - - return 0; - } - - static const luaL_Reg f[] = { - {"play", l_play}, - {0, 0} - }; - - int luaopen_Source(lua_State* L) - { - - return 1; - } - -} -} diff --git a/src/script/audio/luaopen_audio.cpp b/src/script/audio/luaopen_audio.cpp deleted file mode 100644 index 78d7a69..0000000 --- a/src/script/audio/luaopen_audio.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include - -#include "3rdparty/luax/luax.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - static int l_init(lua_State* L) - { - if (SDL_Init(SDL_INIT_AUDIO) < 0) - { - luax_error(L, "could not init audio"); - luax_pushboolean(L, false); - return 1; - } - } - - static int l_newSound(lua_State* L) - { - - return 0; - } - - static const luaL_Reg f[] = { - {"init", l_init}, - {"Sound", l_newSound}, - {0, 0} - }; - - int luaopen_audio(lua_State* L) - { - luax_newlib(L, f); - - return 1; - } -} -} \ No newline at end of file diff --git a/src/script/core/luaopen_core.cpp b/src/script/core/luaopen_core.cpp deleted file mode 100644 index a222013..0000000 --- a/src/script/core/luaopen_core.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - using namespace jin::core; - - static int l_running(lua_State* L) - { - bool running = Game::get()->running(); - luax_pushboolean(L, running); - return 1; - } - - static int l_quit(lua_State* L) - { - Game::get()->quit(); - return 0; - } - - static int l_exit(lua_State* L) - { - Game::get()->exit(); - return 0; - } - - static const luaL_Reg f[] = { - {"running", l_running}, - {"quit", l_quit}, // for end game loop - {"exit", l_exit}, // for exit whole game - {0, 0} - }; - - int luaopen_core(lua_State* L) - { - luax_newlib(L, f); - - return 1; - } -} -} \ No newline at end of file diff --git a/src/script/debug/luaopen_debug.cpp b/src/script/debug/luaopen_debug.cpp deleted file mode 100644 index b33c058..0000000 --- a/src/script/debug/luaopen_debug.cpp +++ /dev/null @@ -1,8 +0,0 @@ -namespace jin -{ -namespace debug -{ - - -} -} \ No newline at end of file diff --git a/src/script/embed/boot.lua b/src/script/embed/boot.lua deleted file mode 100644 index 2f6fa93..0000000 --- a/src/script/embed/boot.lua +++ /dev/null @@ -1,132 +0,0 @@ ---[[ - program entry -]] - -local function _onEvent(e) - -- update keyboard status - if e.type == "keydown" then - jin.keyboard.set(e.key, true) - elseif e.type == "keyup" then - jin.keyboard.set(e.key, false) - end - - -- call user onEvent function - if jin.core.onEvent then - jin.core.onEvent(e) - end -end - -------------------------------------------------- --- init file system -------------------------------------------------- -jin._argv[2] = jin._argv[2] or '.' -jin.filesystem.init() -jin.filesystem.mount(jin._argv[2]) - --- config -local conf = {} -if jin.filesystem.exist("config.lua") then - conf = require "config" -end -conf.width = conf.width or 600 -conf.height = conf.height or 500 -conf.fps = conf.fps or 60 -conf.title = conf.title or ("jin v" .. jin.version()) - --- init video subsystem -jin.graphics.init(conf.width,conf.height,conf.title) - --- open debug mode, must after jin.graphics.init -if jin._argv[3] == '-d' then - jin.debug.init() -end - -function jin.core.run() - local now = jin.time.second() - local last = now - local fsec = 1/conf.fps - -- for loading resources - if jin.core.load then - jin.core.load() - end - local dt = 0 - while(jin.core.running()) do - -- frame controle - last = now - now = jin.time.second() - if (now - last) < fsec then - jin.time.sleep(fsec - now + last) - end - - -- handle events - for _, e in pairs(jin.event.poll()) do - if _onEvent then - _onEvent(e) - end - end - - -- update - dt = now - last - if dt < fsec then - dt = fsec - end - if jin.core.onUpdate then - jin.core.onUpdate(dt) - end - - -- bind to default render buffer - jin.graphics.bind() - jin.graphics.clear() - jin.graphics.color() - jin.graphics.study() - - -- custom drawing - if jin.core.onDraw then - jin.core.onDraw() - end - - -- render debug window - if jin.debug.status() then - jin.debug.render() - end - - -- swap window buffer - jin.graphics.present() - - end -end - -local function onError(msg) - local tab = ' ' - print("Error:\n" .. msg) - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - local ww, wh = jin.graphics.size() - function jin.core.onDraw() - jin.graphics.write("Error: ", 10, 10, 30, 3, 30) - jin.graphics.write(msg, 10, 50) - end -end - -if jin.filesystem.exist("main.lua") then - -- require main game script - xpcall(function() require"main" end, onError) - jin.core.run() -else - -- no game - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - function jin.core.onDraw() - jin.graphics.clear(111, 134, 125, 255) - local ww, wh = jin.graphics.size() - local fw, fh = jin.graphics.box("no game", 20, 1, 20) - jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) - end - jin.core.run() -end diff --git a/src/script/embed/boot.lua.h b/src/script/embed/boot.lua.h deleted file mode 100644 index 21c1899..0000000 --- a/src/script/embed/boot.lua.h +++ /dev/null @@ -1,156 +0,0 @@ -/* boot.lua */ -static const char* boot_lua = R"( - --- init filesystem -jin._argv[2] = jin._argv[2] or '.' -jin.filesystem.init() -jin.filesystem.mount(jin._argv[2]) - --- config -local conf = {} -if jin.filesystem.exist("config.lua") then - conf = require "config" -end -conf.width = conf.width or 600 -conf.height = conf.height or 500 -conf.fps = conf.fps or 60 -conf.vsync = conf.vsync or false -conf.title = conf.title or ("jin v" .. jin.version()) - --- initialize subsystems -jin.graphics.init(conf) ---jin.audio.init(conf) - --- open debug mode, must after jin.graphics.init -if jin._argv[3] == '-d' then - jin.debug.init() -end - -function jin.core.run() - local load = jin.core.load - local running = jin.core.running - local second = jin.time.second - local sleep = jin.time.sleep - local poll = jin.event.poll - local unbind = jin.graphics.unbind - local clear = jin.graphics.clear - local color = jin.graphics.color - local study = jin.graphics.study - local onDraw = jin.core.onDraw - local onUpdate = jin.core.onUpdate - local onEvent = jin.core.onEvent - local present = jin.graphics.present - local setkey = jin.keyboard.set - - local dstatus = jin.debug.status - local drender = jin.debug.render - - local fps = conf.fps - - local _onEvent = function (e) - if e.type == "keydown" then - setkey(e.key, true) - elseif e.type == "keyup" then - setkey(e.key, false) - end - if onEvent then - onEvent(e) - end - end - - if load then - load() - end - - local now = second() - local last = now - local fsec = 1/fps - local dt = 0 - - while(running()) do - -- frame controle - last = now - now = second() - dt = now - last - if dt < fsec then - sleep(fsec - dt) - dt = fsec - end - - -- handle events - for _, e in pairs(poll()) do - if _onEvent then - _onEvent(e) - end - end - - -- update - if onUpdate then - onUpdate(dt) - end - - -- bind to default render buffer - unbind() - clear() - color() - study() - - -- custom drawing - if onDraw then - onDraw() - end - - -- render debug window - if dstatus() then - drender() - end - - -- swap window buffer - present() - end -end - -local function onError(msg) - local tab = ' ' - print("Error:\n" .. msg) - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - local ww, wh = jin.graphics.size() - function jin.core.onDraw() - jin.graphics.write("Error: ", 10, 10, 30, 3, 30) - jin.graphics.write(msg, 10, 50) - end -end - -local function main() - if jin.filesystem.exist("main.lua") then - -- require main game script - xpcall(function() require"main" end, onError) - jin.core.run() - else - -- no game - function jin.core.onEvent(e) - if e.type == 'quit' then - jin.core.quit() - end - end - function jin.core.onDraw() - jin.graphics.clear(111, 134, 125, 255) - local ww, wh = jin.graphics.size() - local fw, fh = jin.graphics.box("no game", 20, 1, 20) - jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) - end - jin.core.run() - end - -- quit subsystems - jin.graphics.destroy() - -- exit whole game - jin.core.exit() -end - -main() - -)"; diff --git a/src/script/embed/debug.lua b/src/script/embed/debug.lua deleted file mode 100644 index 76f59ed..0000000 --- a/src/script/embed/debug.lua +++ /dev/null @@ -1,128 +0,0 @@ ---[[ - for debug purpose - +-------------------+ - |debug msg old | - |... | - |... | - |... | - |debug msg new | - +-------------------+ -]] - -jin.debug = jin.debug or {} - --- render panel -local panel = nil - -local debug = false - --- debug msg buffer -local buffer = {} - --- configure -local bsize = 10 -local fsize = 15 -local lheight = 18 -local alpha = 220 -local margin = 10 - --- refresh buffer or not -local refresh = true - -function jin.debug.init() - debug = true - panel = jin.graphics.Canvas(jin.graphics.size()) -end - --- set buffer size -function jin.debug.size(c) - bsize = c -end - -function jin.debug.print(msg) - if not debug then return end - - msg = tostring(msg) - local tp = type(msg) - if tp ~= "string" and tp ~= "number" then - msg = string.format("print failed, expect string or number but get a %s", tp) - end - - -- remove the first one (old msg) - if #buffer >= bsize then - table.remove(buffer, 1) - end - - buffer[#buffer + 1] = msg - refresh = true -end - --- clear debug buffer -function jin.debug.clear() - buffer = {} -end - -local function getStrHeight(str, lheight) - local h = lheight - if #str == 0 then - h = 0 - end - for i = 1, #str do - local c = string.sub(str, i, i) - if c == '\n' then - h = h + lheight - end - end - return h -end - -local function getBgQuad() - local width, height = 0, 0 - for i = 1, #buffer do - local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) - height = height + h - if width < w then - width = w - end - end - return width, height -end - --- render to screen -function jin.debug.render() - if not debug then return end - - if refresh then - - jin.graphics.bind(panel) - - jin.graphics.clear(0, 0, 0, 0) - - jin.graphics.study() - - local ww, wh = jin.graphics.size() - local bgw, bgh = getBgQuad() - jin.graphics.color(0, 0, 0, alpha) - jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) - - jin.graphics.color() - local y = wh - for i = #buffer, 1, -1 do - local msg = buffer[i] - local h = getStrHeight(msg, lheight) - y = y - h - jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) - end - - jin.graphics.bind() - - refresh = false - end - - jin.graphics.color() - jin.graphics.draw(panel, 0, 0) -end - -function jin.debug.status() - return debug -end diff --git a/src/script/embed/debug.lua.h b/src/script/embed/debug.lua.h deleted file mode 100644 index 79c95ba..0000000 --- a/src/script/embed/debug.lua.h +++ /dev/null @@ -1,132 +0,0 @@ -/* debug.lua */ -static const char* debug_lua = R"( ---[[ - for debug purpose - +-------------------+ - |debug msg old | - |... | - |... | - |... | - |debug msg new | - +-------------------+ -]] - -jin.debug = jin.debug or {} - --- render panel -local panel = nil - -local debug = false - --- debug msg buffer -local buffer = {} - --- configure -local bsize = 10 -local fsize = 15 -local lheight = 18 -local alpha = 220 -local margin = 10 - --- refresh buffer or not -local refresh = true - -function jin.debug.init() - debug = true - panel = jin.graphics.Canvas(jin.graphics.size()) -end - --- set buffer size -function jin.debug.size(c) - bsize = c -end - -function jin.debug.print(msg) - if not debug then return end - - msg = tostring(msg) - local tp = type(msg) - if tp ~= "string" and tp ~= "number" then - msg = string.format("print failed, expect string or number but get a %s", tp) - end - - -- remove the first one (old msg) - if #buffer >= bsize then - table.remove(buffer, 1) - end - - buffer[#buffer + 1] = msg - refresh = true -end - --- clear debug buffer -function jin.debug.clear() - buffer = {} -end - -local function getStrHeight(str, lheight) - local h = lheight - if #str == 0 then - h = 0 - end - for i = 1, #str do - local c = string.sub(str, i, i) - if c == '\n' then - h = h + lheight - end - end - return h -end - -local function getBgQuad() - local width, height = 0, 0 - for i = 1, #buffer do - local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) - height = height + h - if width < w then - width = w - end - end - return width, height -end - --- render to screen -function jin.debug.render() - if not debug then return end - - if refresh then - - jin.graphics.bind(panel) - - jin.graphics.clear(0, 0, 0, 0) - - jin.graphics.study() - - local ww, wh = jin.graphics.size() - local bgw, bgh = getBgQuad() - jin.graphics.color(0, 0, 0, alpha) - jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) - - jin.graphics.color() - local y = wh - for i = #buffer, 1, -1 do - local msg = buffer[i] - local h = getStrHeight(msg, lheight) - y = y - h - jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) - end - - jin.graphics.bind() - - refresh = false - end - - jin.graphics.color() - jin.graphics.draw(panel, 0, 0) -end - -function jin.debug.status() - return debug -end - -)"; \ No newline at end of file diff --git a/src/script/embed/embed.h b/src/script/embed/embed.h deleted file mode 100644 index 2ef8b75..0000000 --- a/src/script/embed/embed.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __JIN_LUA_EMBED_H -#define __JIN_LUA_EMBED_H -#include - -namespace jin -{ -namespace embed -{ - - /** - * embed lua script to context. - */ -#define embed(L, script, name) \ - if(luaL_loadbuffer(L, script, strlen(script), name) == 0)\ - lua_call(L, 0, 0); - - /** - * embed structure. - */ - struct jin_Embed - { - const char* fname, *source; - }; - - static void boot(lua_State* L) - { - // embed scripts - #include "graphics.lua.h" // graphics - #include "keyboard.lua.h" // keyboard - #include "mouse.lua.h" // mouse - #include "debug.lua.h" // debug - #include "boot.lua.h" // boot - - // embed scripts - const jin_Embed scripts[] = { - {"graphics.lua", graphics_lua}, - {"keyboard.lua", keyboard_lua}, - {"mouse.lua", mouse_lua}, - {"debug.lua", debug_lua}, - {"boot.lua", boot_lua}, - {0, 0} - }; - - // load all emebd lua scripts - for (int i = 0; scripts[i].fname; ++i) - embed(L, scripts[i].source, scripts[i].fname); - } -} -} - -#endif \ No newline at end of file diff --git a/src/script/embed/graphics.lua b/src/script/embed/graphics.lua deleted file mode 100644 index 03a891d..0000000 --- a/src/script/embed/graphics.lua +++ /dev/null @@ -1,6 +0,0 @@ ------------------ --- jin.graphics ------------------ - -jin.graphics = jin.graphics or {} - diff --git a/src/script/embed/graphics.lua.h b/src/script/embed/graphics.lua.h deleted file mode 100644 index 85cf979..0000000 --- a/src/script/embed/graphics.lua.h +++ /dev/null @@ -1,8 +0,0 @@ -/* graphics.lua */ -static const char* graphics_lua = R"( ------------------ --- jin.graphics ------------------ - -jin.graphics = jin.graphics or {} -)"; diff --git a/src/script/embed/keyboard.lua b/src/script/embed/keyboard.lua deleted file mode 100644 index 08214f8..0000000 --- a/src/script/embed/keyboard.lua +++ /dev/null @@ -1,16 +0,0 @@ ---[[ - jin.keyboard extension -]] - -jin.keyboard = jin.keyboard or {} - -local keys = {} - -function jin.keyboard.isDown(k) - return keys[k] -end - -function jin.keyboard.set(k, status) - keys[k] = status -end - diff --git a/src/script/embed/keyboard.lua.h b/src/script/embed/keyboard.lua.h deleted file mode 100644 index 66e3c2a..0000000 --- a/src/script/embed/keyboard.lua.h +++ /dev/null @@ -1,20 +0,0 @@ - -static const char* keyboard_lua = R"( ---[[ - jin.keyboard extension -]] - -jin.keyboard = jin.keyboard or {} - -local keys = {} - -function jin.keyboard.isDown(k) - return keys[k] -end - -function jin.keyboard.set(k, status) - keys[k] = status -end - - -)"; diff --git a/src/script/embed/mouse.lua b/src/script/embed/mouse.lua deleted file mode 100644 index 9dcd472..0000000 --- a/src/script/embed/mouse.lua +++ /dev/null @@ -1,15 +0,0 @@ ---[[ - jin.mouse extension -]] - -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 diff --git a/src/script/embed/mouse.lua.h b/src/script/embed/mouse.lua.h deleted file mode 100644 index f57d08c..0000000 --- a/src/script/embed/mouse.lua.h +++ /dev/null @@ -1,18 +0,0 @@ -static const char* mouse_lua = R"( ---[[ - jin.mouse extension -]] - -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/script/embed/path.lua b/src/script/embed/path.lua deleted file mode 100644 index 5b99dd2..0000000 --- a/src/script/embed/path.lua +++ /dev/null @@ -1,15 +0,0 @@ ---[[ - jin.path extension -]] - -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 - diff --git a/src/script/embed/path.lua.h b/src/script/embed/path.lua.h deleted file mode 100644 index b398c99..0000000 --- a/src/script/embed/path.lua.h +++ /dev/null @@ -1,18 +0,0 @@ -/* path.lua */ -static const char* path_lua = R"( ---[[ - jin.path extension -]] - -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/script/event/luaopen_event.cpp b/src/script/event/luaopen_event.cpp deleted file mode 100644 index 16ecbad..0000000 --- a/src/script/event/luaopen_event.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/** -* Event module -*/ -#include - -#include "3rdparty/luax/luax.h" -#include "libjin/jin.h" - -using namespace jin::input; - -namespace jin -{ -namespace lua -{ - - static inline const char* buttonstr(int id) { - switch (id) { - case 1: return "left"; - case 2: return "middle"; - case 3: return "right"; - case 4: return "wheelup"; - case 5: return "wheeldown"; - default: return "?"; - } - } - - static inline const char* wheelstr(int dir) - { - - } - - /** - * Load event poll, return a iterator(a table). - */ - static int l_event_poll(lua_State *L) - { - // table to store events - luax_newtable(L); - SDL_Event e; - int i = 1; - poll: - while (SDL_PollEvent(&e)) - { - // each event is a table - luax_newtable(L); - switch (e.type) - { - case SDL_QUIT: - luax_setfield_string(L, "type", "quit"); - break; - - case SDL_KEYDOWN: - luax_setfield_string(L, "type", "keydown"); - luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); - break; - - case SDL_KEYUP: - luax_setfield_string(L, "type", "keyup"); - luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); - break; - - case SDL_MOUSEMOTION: - luax_setfield_string(L, "type", "mousemotion"); - luax_setfield_number(L, "x", e.motion.x); - luax_setfield_number(L, "y", e.motion.y); - break; - - case SDL_MOUSEBUTTONDOWN: - luax_setfield_string(L, "type", "mousebuttondown"); - luax_setfield_string(L, "button", buttonstr(e.button.button)); - luax_setfield_number(L, "x", e.button.x); - luax_setfield_number(L, "y", e.button.y); - break; - - case SDL_MOUSEBUTTONUP: - luax_setfield_string(L, "type", "mousebuttonup"); - luax_setfield_string(L, "button", buttonstr(e.button.button)); - luax_setfield_number(L, "x", e.button.x); - luax_setfield_number(L, "y", e.button.y); - break; - - case SDL_MOUSEWHEEL: - luax_setfield_string(L, "type", "wheel"); - if(e.wheel.x == -1) - luax_setfield_string(L, "x", "left"); - else if(e.wheel.x == 1) - luax_setfield_string(L, "x", "right"); - else - luax_setfield_string(L, "x", "none"); - - if (e.wheel.y == -1) - luax_setfield_string(L, "y", "near"); - else if (e.wheel.y == 1) - luax_setfield_string(L, "y", "far"); - else - luax_setfield_string(L, "y", "none"); - break; - - default: - /* ignore other events */ - luax_pop(L, 1); // pop table out - goto poll; - break; - } - luax_rawseti(L, -2, i++); - } - return 1; - } - - static const luaL_Reg f[] = { - {"poll", l_event_poll}, - {0 ,0 } - }; - - /** - * load event module - */ - int luaopen_event(lua_State* L) - { - luax_newlib(L, f); - return 1; - } -} -} \ No newline at end of file diff --git a/src/script/filesystem/luaopen_filesystem.cpp b/src/script/filesystem/luaopen_filesystem.cpp deleted file mode 100644 index 2060d40..0000000 --- a/src/script/filesystem/luaopen_filesystem.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "libjin/jin.h" -#include - -using namespace jin::fs; - -namespace jin -{ -namespace lua -{ - - static struct - { - Filesystem* fs; - } context; - - static int l_init(lua_State* L) - { - context.fs = Filesystem::get(); - return 0; - } - - /** - * set current game root, like - * C:/jin/games/tank/ - */ - static int l_mount(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - context.fs->mount(path); - return 0; - } - - /** - * - */ - static int l_isDir(lua_State *L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->isDir(path); - luax_pushboolean(L, r); - return 1; - } - - /** - * - */ - static int l_exist(lua_State * L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->exists(path); - luax_pushboolean(L, r); - return 1; - } - - static int l_isdir(lua_State* L) - { - const char* path = luax_checkstring(L, 1); - int r = context.fs->isDir(path); - luax_pushboolean(L, r); - return 1; - } - - // load but dont run it - static int loadf(lua_State* L) - { - const char* filename = lua_tostring(L, -1); - Buffer bf; - context.fs->read(filename, &bf); - luax_loadbuffer(L, (const char*)bf.data, bf.size, filename); - return 1; - } - - static int loader(lua_State* L) - { - const char * filename = lua_tostring(L, -1); - - std::string tmp(filename); - tmp += ".lua"; - - int size = tmp.size(); - - for (int i = 0; iexists(tmp.c_str())) - { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - // Ok, load it. - return loadf(L); - } - - tmp = filename; - size = tmp.size(); - for (int i = 0; iisDir(tmp.c_str())) - { - tmp += "/init.lua"; - if (context.fs->exists(tmp.c_str())) - { - lua_pop(L, 1); - lua_pushstring(L, tmp.c_str()); - // Ok, load it. - return loadf(L); - } - } - - lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); - return 1; - } - - static const luaL_Reg f[] = { - {"init", l_init}, - {"mount", l_mount}, - {"isdir", l_isDir}, - {"exist", l_exist}, - {0, 0} - }; - - int luaopen_filesystem(lua_State* L) - { - luax_newlib(L, f); - luax_register_searcher(L, loader, 1); - return 0; - } - -} -} \ No newline at end of file diff --git a/src/script/graphics/luaopen_Canvas.cpp b/src/script/graphics/luaopen_Canvas.cpp deleted file mode 100644 index 1e22772..0000000 --- a/src/script/graphics/luaopen_Canvas.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "../luaopen_types.h" -#include "libjin/jin.h" - -namespace jin -{ -namespace lua -{ - - using namespace jin::render; - - static inline Canvas* checkCanvas(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); - if (proxy != nullptr) - return (Canvas*)proxy->object; - return nullptr; - } - - static int l_getWidth(lua_State* L) - { - Canvas* c = checkCanvas(L); - luax_pushnumber(L, c->getWidth()); - return 1; - } - - static int l_getHeight(lua_State* L) - { - Canvas* c = checkCanvas(L); - luax_pushnumber(L, c->getHeight()); - return 1; - } - - static int l_getSize(lua_State* L) - { - Canvas* c = checkCanvas(L); - luax_pushnumber(L, c->getWidth()); - luax_pushnumber(L, c->getHeight()); - return 2; - } - - static int l_setAnchor(lua_State* L) - { - Canvas* c = checkCanvas(L); - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - c->setAnchor(x, y); - return 0; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); - Canvas* canvas = (Canvas*)proxy->object; - delete canvas; - return 0; - } - - static const luaL_Reg f[] = { - {"__gc", l_gc}, - {"getWidth", l_getWidth}, - {"getHeight", l_getHeight}, - {"getSize", l_getSize}, - {"setAnchor", l_setAnchor}, - {0, 0 } - }; - - int luaopen_Canvas(lua_State* L) - { - luax_newtype(L, TYPE_CANVAS, f); - return 0; - } - -}// lua -}// jin \ No newline at end of file diff --git a/src/script/graphics/luaopen_Font.cpp b/src/script/graphics/luaopen_Font.cpp deleted file mode 100644 index 82dc078..0000000 --- a/src/script/graphics/luaopen_Font.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "../luaopen_types.h" -#include "libjin/jin.h" - -using namespace jin::render; - -namespace jin -{ -namespace lua -{ - - static int l_gc(lua_State* L) - { - return 0; - } - - static int l_box(lua_State* L) - { - Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); - const char* text = luax_checkstring(L, 2); - int fheight = luax_checknumber(L, 3); - int spacing = luax_checknumber(L, 4); - int lheight = luax_checknumber(L, 5); - int w, h; - font->box(text, fheight, lheight, spacing, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - - static const luaL_Reg f[] = { - {"__gc", l_gc}, - {"box", l_box}, - {0, 0} - }; - - int luaopen_Font(lua_State* L) - { - luax_newtype(L, TYPE_FONT, f); - - return 0; - } - -} -} \ No newline at end of file diff --git a/src/script/graphics/luaopen_Image.cpp b/src/script/graphics/luaopen_Image.cpp deleted file mode 100644 index 246ea09..0000000 --- a/src/script/graphics/luaopen_Image.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "libjin/jin.h" -#include "../luaopen_types.h" - -using namespace jin::render; - -namespace jin -{ -namespace lua -{ - - static inline Image* checkImage(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); - if (proxy != 0 && proxy != nullptr) - return (Image*)proxy->object; - return nullptr; - } - - static int l_getWidth(lua_State* L) - { - Image* i = checkImage(L); - luax_pushnumber(L, i->getWidth()); - return 1; - } - - static int l_getHeight(lua_State *L) - { - Image* i = checkImage(L); - luax_pushnumber(L, i->getHeight()); - return 1; - } - - static int l_getPixel(lua_State* L) - { - Image* i = checkImage(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - color c = i->getPixel(x, y); - luax_pushnumber(L, c.rgba.r); - luax_pushnumber(L, c.rgba.g); - luax_pushnumber(L, c.rgba.b); - luax_pushnumber(L, c.rgba.a); - return 4; - } - - static int l_setAnchor(lua_State* L) - { - Image* i = checkImage(L); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - i->setAnchor(x, y); - return 0; - } - - static int l_getSize(lua_State* L) - { - Image* i = checkImage(L); - luax_pushnumber(L, i->getWidth()); - luax_pushnumber(L, i->getHeight()); - return 2; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_IMAGE); - Image* img = (Image*)proxy->object; - delete img; - return 0; - } - - static const luaL_Reg f[] = { - {"__gc", l_gc}, - {"getWidth", l_getWidth}, - {"getHeight", l_getHeight}, - {"getSize", l_getSize}, - {"getPixel", l_getPixel}, - {"setAnchor", l_setAnchor}, - {0, 0 } - }; - - int luaopen_Image(lua_State* L) - { - luax_newtype(L, TYPE_IMAGE, f); - return 0; - } - -}// graphics -}// jin \ No newline at end of file diff --git a/src/script/graphics/luaopen_JSL.cpp b/src/script/graphics/luaopen_JSL.cpp deleted file mode 100644 index d5db363..0000000 --- a/src/script/graphics/luaopen_JSL.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "libjin/jin.h" -#include "../luaopen_types.h" - -namespace jin -{ -namespace lua -{ - - using namespace render; - - static inline JSLProgram* checkJSLProgram(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); - if(proxy != nullptr) - return (JSLProgram*)proxy->object; - return nullptr; - } - - static enum VARIABLE_TYPE - { - INVALID = 0, - - NUMBER, - IMAGE, - CANVAS, - VEC2, - VEC3, - VEC4, - COLOR, - }; - - static VARIABLE_TYPE strtotype(const char* str) - { - std::string s = std::string(str); - if (s == "number") return NUMBER; - else if (s == "Image") return IMAGE; - else if (s == "Canvas") return CANVAS; - else if (s == "vec2") return VEC2; - else if (s == "vec3") return VEC3; - else if (s == "vec4") return VEC4; - else if (s == "Color") return COLOR; - else return INVALID; - } - - /** - * Use send function send variables to JSL program. - */ - static int l_send(lua_State* L) - { - JSLProgram* jsl = checkJSLProgram(L); - // number Image Texel - const char* typestr = luax_checkstring(L, 2); - // variable name - const char* variable = luax_checkstring(L, 3); - if (typestr != nullptr) - { - int type = strtotype(typestr); - switch (type) - { - case NUMBER: - { - float number = luax_checknumber(L, 4); - jsl->sendFloat(variable, number); - break; - } - case IMAGE: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); - Image* img = (Image*)proxy->object; - jsl->sendImage(variable, img); - break; - } - case CANVAS: - { - Proxy* proxy = (Proxy*)luax_checktype(L, 4, TYPE_IMAGE); - Canvas* canvas = (Canvas*)proxy->object; - jsl->sendCanvas(variable, canvas); - break; - } - case VEC2: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - jsl->sendVec2(variable, x, y); - break; - } - case VEC3: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - float z = luax_checknumber(L, 6); - jsl->sendVec3(variable, x, y, z); - break; - } - case VEC4: - { - float x = luax_checknumber(L, 4); - float y = luax_checknumber(L, 5); - float z = luax_checknumber(L, 6); - float w = luax_checknumber(L, 7); - jsl->sendVec4(variable, x, y, z, w); - break; - } - case COLOR: - { - color col; - col.rgba.r = luax_checkinteger(L, 4); - col.rgba.g = luax_checkinteger(L, 5); - col.rgba.b = luax_checkinteger(L, 6); - col.rgba.a = luax_checkinteger(L, 7); - jsl->sendColor(variable, &col); - break; - } - default: - return 0; - } - } - return 1; - } - - static int l_gc(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_JSL); - JSLProgram* jsl = (JSLProgram*)proxy->object; - delete jsl; - return 0; - } - - static const luaL_Reg f[] = { - {"__gc", l_gc }, - {"send", l_send}, - {0, 0} - }; - - /** - * JSL program - */ - int luaopen_JSL(lua_State* L) - { - luax_newtype(L, TYPE_JSL, f); - return 0; - } - -} -} \ No newline at end of file diff --git a/src/script/graphics/luaopen_graphics.cpp b/src/script/graphics/luaopen_graphics.cpp deleted file mode 100644 index 40bdaa1..0000000 --- a/src/script/graphics/luaopen_graphics.cpp +++ /dev/null @@ -1,526 +0,0 @@ -#include "3rdparty/luax/luax.h" - -#include "libjin/jin.h" - -#include "../luaopen_types.h" -#include "../embed/graphics.lua.h" - -namespace jin -{ -namespace lua -{ - using namespace render; - using namespace fs; - - /** - * jin.graphics context, storge some module - * shared variables. - */ - static struct - { - color curRenderColor; - Font* curFont = 0; - Font* defaultFont = 0; - } context; - - /** - * Init video system. - * jin.graphics.init(width, height, title) - */ - static int l_init(lua_State* L) - { - Window* wnd = Window::get(); - WindowSetting setting; - setting.width = luax_getfield_integer(L, 1, "width"); - setting.height = luax_getfield_integer(L, 1, "height"); - setting.title = luax_getfield_string(L, 1, "title"); - setting.vsync = luax_getfield_bool(L, 1, "vsync"); - luax_pushboolean(L, wnd->init(&setting)); - return 1; - } - - static int l_destroy(lua_State* L) - { - Window* wnd = Window::get(); - wnd->quit(); - return 0; - } - - /** - * Get windows size. - */ - static int l_getSize(lua_State* L) - { - Window* wnd = Window::get(); - luax_pushnumber(L, wnd->getW()); - luax_pushnumber(L, wnd->getH()); - return 2; - } - - /** - * Create a image userdata and set metatable for it. - */ - static int l_newImage(lua_State* L) - { - Filesystem* fs = Filesystem::get(); - const char* f = luax_checkstring(L, 1); - if (!fs->exists(f)) - { - printf("Error: no such image %s\n", f); - exit(1); - } - Buffer b; - fs->read(f, &b); - - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_IMAGE, sizeof(Proxy)); - Image* img = new Image((const char*)b.data, b.size); - proxy->bind(img); - return 1; - } - - /** - * Create a new JSL program. - * graphics.Shader(program) - */ - static int l_newShader(lua_State* L) - { - Proxy* proxy = (Proxy*)luax_newinstance(L, TYPE_JSL, sizeof(JSLProgram)); - const char* program = luax_checkstring(L, 1); - JSLProgram* jsl = new JSLProgram(program); - proxy->bind(jsl); - return 1; - } - - /** - * Create a new Canvas, don't use it in loop, very slow. - * jin.graphics.newCanvas(w, h) - */ - 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, TYPE_CANVAS, sizeof(Proxy)); - Canvas* cvs = new Canvas(w, h); - proxy->bind(cvs); - return 1; - } - - static int l_clear(lua_State* L) - { - if (luax_gettop(L) == 0) - { - glClearColor(0, 0, 0, 1); - } - else - { - int r = luax_checknumber(L, 1); - int g = luax_checknumber(L, 2); - int b = luax_checknumber(L, 3); - int a = luax_checknumber(L, 4); - glClearColor(r / 255.f, g / 255.f, b / 255.f, a / 255.f); - } - glClear(GL_COLOR_BUFFER_BIT); - return 0; - } - - /** - * Swap render buffers, present current buffer to front. - */ - static int l_present(lua_State* L) - { - Window::get()->swapBuffers(); - return 0; - } - - // jin.graphics.draw(x, y, scalex, scaley, r) - static int l_draw(lua_State* L) - { - int x = luax_optnumber(L, 2, 0); - int y = luax_optnumber(L, 3, 0); - float sx = luax_optnumber(L, 4, 1); - float sy = luax_optnumber(L, 5, 1); - float r = luax_optnumber(L, 6, 0); - if (luax_istype(L, 1, TYPE_IMAGE)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Image* img = (Image*)proxy->object; - img->draw(x, y, sx, sy, r); - } - else if (luax_istype(L, 1, TYPE_CANVAS)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - Canvas* p = (Canvas*)proxy->object; - p->draw(x, y, sx, sy, r); - } - else - { - /* wrong type */ - luax_typerror(L, 1, "image or canvas"); - } - return 0; - } - - static int l_setColor(lua_State* L) - { - // jin.graphics.color() to set back to default - // render color - if (luax_gettop(L) == 0) - { - glColor4f(1, 1, 1, 1); - return 0; - } - - context.curRenderColor.rgba.r = luax_checknumber(L, 1); - context.curRenderColor.rgba.g = luax_checknumber(L, 2); - context.curRenderColor.rgba.b = luax_checknumber(L, 3); - context.curRenderColor.rgba.a = luax_checknumber(L, 4); - - glColor4f(context.curRenderColor.rgba.r / 255.f, - context.curRenderColor.rgba.g / 255.f, - context.curRenderColor.rgba.b / 255.f, - context.curRenderColor.rgba.a / 255.f); - return 0; - } - - static int l_getColor(lua_State * L) - { - luax_pushnumber(L, context.curRenderColor.rgba.r); - luax_pushnumber(L, context.curRenderColor.rgba.g); - luax_pushnumber(L, context.curRenderColor.rgba.b); - luax_pushnumber(L, context.curRenderColor.rgba.a); - return 4; - } - - static int l_bindCanvas(lua_State* L) - { - if (luax_gettop(L) == 0) - { - // bind to default canvas - Canvas::unbind(); - return 0; - } - Proxy* proxy = (Proxy*)luax_checktype(L, 1, TYPE_CANVAS); - Canvas* c = (Canvas*)proxy->object; - c->bind(); - return 0; - } - - static int l_unbindCanvas(lua_State* L) - { - Canvas::unbind(); - return 0; - } - - static int l_useShader(lua_State* L) - { - if (luax_gettop(L) == 0) - { - JSLProgram::unuse(); - return 0; - } - if (luax_istype(L, 1, TYPE_JSL)) - { - Proxy* proxy = (Proxy*)luax_toudata(L, 1); - JSLProgram* jsl = (JSLProgram*)proxy->object; - jsl->use(); - } - else - { - luax_typerror(L, 1, "JSL shader"); - } - return 0; - } - - static int l_unuseShader(lua_State* L) - { - JSLProgram::unuse(); - return 0; - } - - static int l_setBlend(lua_State* L) - { - - return 0; - } - - static RENDER_MODE strtomode(const char* str) - { - std::string s = std::string(str); - if (s == "fill") return FILL; - else if (s == "line") return LINE; - else return NONE; - } - - /** - * draw pixel to screen - * jin.graphics.pixel(x, y) - */ - static int l_drawpoint(lua_State* L) - { - int x = luax_checknumber(L, 1); - int y = luax_checknumber(L, 2); - render::point(x, y); - - return 0; - } - - static int l_drawLine(lua_State* L) - { - int x1 = luax_checknumber(L, 1); - int y1 = luax_checknumber(L, 2); - int x2 = luax_checknumber(L, 3); - int y2 = luax_checknumber(L, 4); - render::line(x1, y1, x2, y2); - - return 0; - } - - static int l_drawRect(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != 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); - render::rect(mode, x, y, w, h); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_drawCircle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - float r = luax_checknumber(L, 4); - render::circle(mode, x, y, r); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_drawTriangle(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) - { - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int x2 = luax_checknumber(L, 3); - int y2 = luax_checknumber(L, 4); - - int x3 = luax_checknumber(L, 5); - int y3 = luax_checknumber(L, 6); - - render::triangle(mode, x, y, x2, y2, x3, y3); - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - /** - * draw polygon. - * jin.graphics.polygon(mode, n, {{}, {}, {}...}) - */ - static int l_drawPolygon(lua_State* L) - { - const char* modestr = luax_checkstring(L, 1); - int n = luax_checknumber(L, 2); - RENDER_MODE mode = strtomode(modestr); - if (mode != NONE) - { - if (!luax_istable(L, 3)) - { - luax_typerror(L, 3, "table"); - return 1; - } - int tn = luax_tableidxlen(L, 3); - if (tn != n * 2) - { - static char* emsg = \ - "number of polygon vertices doesn't match " \ - "provided n, expect %d numbers but get %d"; - luax_error(L, emsg, n * 2, tn); - return 1; - } - float* p = new float[2 * n]; - for (int i = 1; i <= 2 * n; ++i) - p[i - 1] = luax_rawgetnumber(L, 3, i); - render::polygon(mode, p, n); - delete[] p; - } - else - { - luax_typerror(L, 1, "'fill' or 'line'"); - return 1; - } - - return 0; - } - - static int l_newFont(lua_State* L) - { - Font* font = (Font*)luax_newinstance(L, TYPE_FONT, sizeof(Font)); - const char* path = luax_checkstring(L, 1); - Filesystem* fs = Filesystem::get(); - Buffer b = {}; - if (!fs->exists(path)) - { - printf("Error: no such font %s\n", path); - exit(1); - } - fs->read(path, &b); - font->loadb((const unsigned char*)b.data); - - return 1; - } - - /** - * study font, 0 args for study default font. - */ - static int l_study(lua_State* L) - { - int n = luax_gettop(L); - if (n == 0) - { - if (context.defaultFont == 0) - { - #include "data/font.ttf.h" - // load default font - context.defaultFont = new Font(); - context.defaultFont->loadb(font_ttf); - } - context.curFont = context.defaultFont; - return 0; - } - Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); - context.curFont = font; - return 0; - } - - /** - * draw text with current font(after study). befor write, must - * study a font. - */ - static int l_write(lua_State* L) - { - if (context.curFont == 0) - return 0; - - const char* text = luax_checkstring(L, 1); - int x = luax_checknumber(L, 2); - int y = luax_checknumber(L, 3); - - int fh = luax_optnumber(L, 4, 15); - int spacing = luax_optnumber(L, 5, 1); - int lh = luax_optnumber(L, 6, 18); - - context.curFont->render(text, x, y, fh, spacing, lh); - - return 0; - } - - /** - * get text bound box - */ - static int l_box(lua_State* L) - { - const char* text = luax_checkstring(L, 1); - int fontheight = luax_checknumber(L, 2); - int spacing = luax_checknumber(L, 3); - int lineheight = luax_checknumber(L, 4); - int w, h; - context.curFont->box(text, fontheight, spacing, lineheight, &w, &h); - luax_pushnumber(L, w); - luax_pushnumber(L, h); - return 2; - } - - static const luaL_Reg f[] = { - {"init", l_init}, - {"size", l_getSize}, - {"Image", l_newImage}, - {"Shader", l_newShader}, - {"Canvas", l_newCanvas}, - {"Font", l_newFont}, - /** - * before using box and write - * must call study to set - * current font - */ - {"box", l_box}, - {"write", l_write}, - {"clear", l_clear}, - {"draw", l_draw}, - {"color", l_setColor}, - {"palette", l_getColor}, - {"present", l_present}, - //{"blend", l_setBlend}, - // study font - {"study", l_study}, - // bind canvas - {"bind", l_bindCanvas}, - {"unbind", l_unbindCanvas}, - // use shader - {"use", l_useShader}, - {"unuse", l_unuseShader}, - // draw shapes - {"point", l_drawpoint}, - {"line", l_drawLine}, - {"rect", l_drawRect}, - {"circle", l_drawCircle}, - {"triangle", l_drawTriangle}, - {"polygon", l_drawPolygon}, - // - {"destroy", l_destroy}, - {0, 0} - }; - - extern int luaopen_Image(lua_State* L); - - extern int luaopen_Font(lua_State* L); - - extern int luaopen_Canvas(lua_State* L); - - extern int luaopen_JSL(lua_State* L); - - int luaopen_graphics(lua_State* L) - { - // register types - luaopen_Image(L); - luaopen_Canvas(L); - luaopen_Font(L); - luaopen_JSL(L); - - // load whole lib - luax_newlib(L, f); - - return 1; - } - -}// lua -}// jin diff --git a/src/script/joypad/joypad.h b/src/script/joypad/joypad.h deleted file mode 100644 index e8d309b..0000000 --- a/src/script/joypad/joypad.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef __JIN_JOYPAD_H -#define __JIN_JOYPAD_H - -namespace jin -{ -namespace input -{ - - - -} -} - -#endif \ No newline at end of file diff --git a/src/script/joypad/luaopen_joypad.cpp b/src/script/joypad/luaopen_joypad.cpp deleted file mode 100644 index 0166a57..0000000 --- a/src/script/joypad/luaopen_joypad.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "libjin/jin.h" -#include "3rdparty/luax/luax.h" - -namespace jin -{ -namespace lua -{ - - static const luaL_Reg f[] = { - { 0, 0 } - }; - - int luaopen_joypad(lua_State* L) - { - luax_newlib(L, f); - - return 1; - } - -} -} \ No newline at end of file diff --git a/src/script/keyboard/luaopen_keyboard.cpp b/src/script/keyboard/luaopen_keyboard.cpp deleted file mode 100644 index 1ec1859..0000000 --- a/src/script/keyboard/luaopen_keyboard.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "script/embed/keyboard.lua.h" - -namespace jin -{ -namespace lua -{ - - int luaopen_keyboard(lua_State* L) - { - luax_newlib(L, 0); - return 1; - } - -} -} \ No newline at end of file diff --git a/src/script/luaopen_jin.cpp b/src/script/luaopen_jin.cpp deleted file mode 100644 index dcf8830..0000000 --- a/src/script/luaopen_jin.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include "luaopen_jin.h" -#include "3rdparty/luax/luax.h" - -// embed all lua scripts -#include "embed/embed.h" - -namespace jin -{ -namespace lua -{ - - extern int luaopen_core(lua_State* L); - extern int luaopen_graphics(lua_State* L); - extern int luaopen_audio(lua_State* L); - extern int luaopen_net(lua_State* L); - extern int luaopen_event(lua_State* L); - extern int luaopen_time(lua_State* L); - extern int luaopen_mouse(lua_State* L); - extern int luaopen_keyboard(lua_State* L); - extern int luaopen_filesystem(lua_State* L); - extern int luaopen_joypad(lua_State* L); - - static int l_getversion(lua_State* L) - { - luax_pushstring(L, VERSION); - return 1; - } - - static int l_getAuthor(lua_State* L) - { - luax_pushstring(L, AUTHOR); - return 1; - } - - static int l_getOS(lua_State* L) - { - #ifdef _WIN32 - luax_pushstring(L, "windows"); - #elif defined __unix__ - luax_pushstring(L, "unix"); - #elif defined __APPLE__ - luax_pushstring(L, "macos"); - #endif - return 1; - } - - static const luaL_Reg f[] = { - {"version", l_getversion}, - {"author", l_getAuthor}, - {"os", l_getOS}, - {0, 0} - }; - - // submodules - static const luaL_Reg mods[] = { - {"core", luaopen_core}, - {"event", luaopen_event}, - {"graphics", luaopen_graphics}, - {"time", luaopen_time}, - {"mouse", luaopen_mouse}, - {"keyboard", luaopen_keyboard}, - {"filesystem", luaopen_filesystem}, - {"net", luaopen_net}, - {"audio", luaopen_audio}, - {"joypad", luaopen_joypad}, - {0, 0} - }; - - int luaopen_jin(lua_State* L) - { - // jin module is on top of the stack - luax_newlib(L, f); - - // set to global field - luax_justglobal(L, -1, MODULE_NAME); - - // register submodules - for (int i = 0; mods[i].name; ++i) - { - // open submodules - mods[i].func(L); - luax_setfield(L, -2, mods[i].name); - } - - return 1; - } - - /** - * boot jin - */ - void boot(lua_State* L) - { - jin::embed::boot(L); - } - -} -} \ No newline at end of file diff --git a/src/script/luaopen_jin.h b/src/script/luaopen_jin.h deleted file mode 100644 index 161b482..0000000 --- a/src/script/luaopen_jin.h +++ /dev/null @@ -1,45 +0,0 @@ -/** -* Copyright (C) 2016~2018 chai -* -* Permission is hereby granted, free of charge, to any person obtaining a copyof -* this software and associated documentation files (the "Software"), to deal in -* the Software without restriction, including without limitation the rights to use, -* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the -* Software, and to permit persons to whom the Software is furnished to do so, -* subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - -#ifndef __JIN_M_JIN_H -#define __JIN_M_JIN_H -#include "3rdparty/luax/luax.h" - -#define MODULE_NAME "jin" -#define VERSION "0.1.0" -#define AUTHOR "chai" - -namespace jin -{ -namespace lua -{ - - /** - * open jin lib and boot - */ - int luaopen_jin(lua_State* L); - - void boot(lua_State* L); - -} -} - -#endif \ No newline at end of file diff --git a/src/script/luaopen_types.h b/src/script/luaopen_types.h deleted file mode 100644 index bb6f5b1..0000000 --- a/src/script/luaopen_types.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __JIN_M_TYPES_H -#define __JIN_M_TYPES_H - -// graphics module -#define TYPE_IMAGE "Image" -#define TYPE_JSL "Shader" -#define TYPE_CANVAS "Canvas" -#define TYPE_FONT "Font" - -// audio module -#define TYPE_SOURCE "Source" - -class Proxy -{ -public: - inline void bind(void* obj) - { - object = obj; - } - - void* object; -}; - -#endif diff --git a/src/script/mouse/luaopen_mouse.cpp b/src/script/mouse/luaopen_mouse.cpp deleted file mode 100644 index 29819e1..0000000 --- a/src/script/mouse/luaopen_mouse.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include "SDL2/SDL.h" -namespace jin -{ -namespace lua -{ - static int l_pos(lua_State* L) - { - int x, y; - SDL_GetMouseState(&x, &y); - luax_pushnumber(L, x); - luax_pushnumber(L, y); - return 2; - } - - static const luaL_Reg f[] = { - {"position", l_pos}, - {0, 0} - }; - - int luaopen_mouse(lua_State* L) - { - luax_newlib(L, f); - return 1; - } -} -} \ No newline at end of file diff --git a/src/script/net/luaopen_net.cpp b/src/script/net/luaopen_net.cpp deleted file mode 100644 index 363a131..0000000 --- a/src/script/net/luaopen_net.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/** -* Notice: the net module is not finished yet. -*/ -#include "3rdparty/luax/luax.h" -#include "3rdparty/tekcos/tekcos.h" - -namespace jin -{ -namespace lua -{ - struct - { - tk_TCPsocket* sk; - }context; - - /** - * A table is needed. For example: - * local conf = { - * mode = "server", - * ip = "", - * port = 8000 - * } - */ - static int l_open(lua_State* L) - { - // init context.sk - context.sk = 0; - if (!luax_istable(L, 1)) - { - luax_typerror(L, 1, "table is needed"); - return 0; - } - luax_getfield(L, 1, "mode"); - if (luax_isnil(L, -1)) - {// no mode field - luax_error(L, "mode field is needed, but get nil"); - return 0; - } - const char* mode = luax_checkstring(L, -1); - if (strcmp(mode, "server") == 0 || strcmp(mode, "client") == 0) - { - - if (strcmp(mode, "server") == 0) - {// a server, ignore ip field - - } - else - { - - } - } - else - { - luax_error(L, "\"server\" or \"client\" is needed, but get %s", mode); - return 0; - } - return 1; - } - - static int l_accept(lua_State* L) - { - return 1; - } - - static int l_send(lua_State* L) - { - return 1; - } - - static int l_recv(lua_State* L) - { - return 1; - } - - static int l_close(lua_State* L) - { - return 1; - } - - static int l_nonblocking(lua_State* L) - { - return 1; - } - - // block mode by default - static int l_blocking(lua_State* L) - { - return 1; - } - - static const luaL_Reg f[] = { - {"open", l_open}, - {"accept", l_accept}, - {"send", l_send}, - {"recv", l_recv}, - {"close", l_close}, - {"blocking", l_blocking }, - {"nonblocking", l_nonblocking}, - {0, 0} - }; - - // only tcp - int luaopen_net(lua_State* L) - { - luax_newlib(L, f); - - return 1; - } -} -} \ No newline at end of file diff --git a/src/script/thread/luaopen_Thread.cpp b/src/script/thread/luaopen_Thread.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/script/time/luaopen_time.cpp b/src/script/time/luaopen_time.cpp deleted file mode 100644 index a26be17..0000000 --- a/src/script/time/luaopen_time.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "3rdparty/luax/luax.h" -#include -namespace jin -{ -namespace lua -{ - - static int l_sec(lua_State* L) - { - luax_pushnumber(L, SDL_GetTicks()/1000.f); - return 1; - } - - static int l_sleep(lua_State* L) - { - double sec = luax_checknumber(L, 1); - SDL_Delay(sec * 1000); - return 0; - } - - static const luaL_Reg f[] = { - {"second", l_sec}, - {"sleep", l_sleep}, - {0, 0}, - }; - - int luaopen_time(lua_State* L) - { - luax_newlib(L, f); - return 1; - } - -} -} \ No newline at end of file -- cgit v1.1-26-g67d0