├── src ├── jinc.cpp ├── res │ ├── icon.png.h │ ├── font.ttf │ └── icon.png ├── input │ ├── keyboard.cpp │ ├── mouse.cpp │ ├── event.cpp │ ├── event.h │ ├── keyboard.h │ └── mouse.h ├── net │ └── net.h ├── render │ ├── image.cpp │ ├── rect.h │ ├── jsl.cpp │ ├── quad.h │ ├── canvas.h │ ├── image.h │ ├── window.h │ ├── graphics.h │ ├── drawable.h │ ├── jsl.h │ ├── font.h │ ├── color.h │ ├── drawable.cpp │ ├── window.cpp │ ├── canvas.cpp │ ├── graphics.cpp │ └── font.cpp ├── lua │ ├── debug │ │ └── m_debug.cpp │ ├── embed │ │ ├── graphics.lua │ │ ├── mouse.lua │ │ ├── keyboard.lua │ │ ├── path.lua │ │ ├── graphics.lua.h │ │ ├── mouse.lua.h │ │ ├── keyboard.lua.h │ │ ├── embed.h │ │ ├── path.lua.h │ │ ├── debug.lua │ │ └── boot.lua │ ├── keyboard │ │ └── m_keyboard.cpp │ ├── m_types.h │ ├── audio │ │ ├── m_Sound.cpp │ │ └── m_audio.cpp │ ├── mouse │ │ └── m_mouse.cpp │ ├── graphics │ │ ├── m_JSL.cpp │ │ ├── m_Font.cpp │ │ ├── m_Canvas.cpp │ │ └── m_Image.cpp │ ├── time │ │ └── m_time.cpp │ ├── core │ │ └── m_core.cpp │ ├── m_jin.h │ ├── m_jin.cpp │ ├── net │ │ └── m_net.cpp │ ├── filesystem │ │ └── m_filesystem.cpp │ └── event │ │ └── m_event.cpp ├── audio │ ├── audio.cpp │ └── audio.h ├── utils │ ├── error.h │ ├── math.h │ ├── utils.h │ ├── endian.h │ ├── matrix.h │ └── matrix.cpp ├── libs │ ├── tekcos │ │ ├── README │ │ └── tekcos.h │ ├── lua51 │ │ ├── lapi.h │ │ ├── linit.c │ │ ├── lstring.h │ │ ├── lundump.h │ │ ├── ldebug.h │ │ ├── ltm.h │ │ ├── lfunc.h │ │ ├── lualib.h │ │ ├── lvm.h │ │ ├── ltable.h │ │ ├── lmem.h │ │ ├── lzio.h │ │ ├── ltm.c │ │ ├── lzio.c │ │ ├── ldo.h │ │ ├── llex.h │ │ ├── lmem.c │ │ ├── lparser.h │ │ ├── lcode.h │ │ ├── llimits.h │ │ ├── lopcodes.c │ │ ├── lstring.c │ │ ├── lgc.h │ │ ├── ldump.c │ │ ├── lfunc.c │ │ ├── lundump.c │ │ ├── lstate.h │ │ ├── print.c │ │ ├── lobject.c │ │ ├── lauxlib.h │ │ ├── lstate.c │ │ ├── Makefile │ │ ├── lmathlib.c │ │ └── loslib.c │ └── smount │ │ ├── smount.h │ │ └── smount.c ├── core │ ├── game.cpp │ └── game.h ├── fs │ ├── buffer.h │ ├── filesystem.h │ └── filesystem.cpp └── main.cpp ├── utils ├── wrapy.py └── README ├── deps └── DEPS ├── test ├── test │ ├── font.ttf │ ├── img │ │ └── icon.png │ ├── config.lua │ └── main.lua └── README ├── doc ├── screenshot │ ├── a.png │ ├── b.png │ └── c.gif ├── bugs.md ├── todo.md └── features.md ├── .gitattributes ├── .gitignore ├── LICENSE ├── bin └── LICENSE ├── README_zh.md └── README.md /src/jinc.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/res/icon.png.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /utils/wrapy.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/input/keyboard.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/input/mouse.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /deps/DEPS: -------------------------------------------------------------------------------- 1 | SDL2 2 | 3 | 4 | -------------------------------------------------------------------------------- /utils/README: -------------------------------------------------------------------------------- 1 | * cwrap.c for compile lua file to Cpp code. 2 | -------------------------------------------------------------------------------- /src/net/net.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_NET_H 2 | #define __JIN_NET_H 3 | 4 | 5 | 6 | #endif -------------------------------------------------------------------------------- /src/res/font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/src/res/font.ttf -------------------------------------------------------------------------------- /src/res/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/src/res/icon.png -------------------------------------------------------------------------------- /test/test/font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/test/test/font.ttf -------------------------------------------------------------------------------- /doc/screenshot/a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/doc/screenshot/a.png -------------------------------------------------------------------------------- /doc/screenshot/b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/doc/screenshot/b.png -------------------------------------------------------------------------------- /doc/screenshot/c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/doc/screenshot/c.gif -------------------------------------------------------------------------------- /src/render/image.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/src/render/image.cpp -------------------------------------------------------------------------------- /doc/bugs.md: -------------------------------------------------------------------------------- 1 | # 0.1.0-1 2 | 3 | * In jin.graphics, shaps drawing coordinates are wrong. 4 | -------------------------------------------------------------------------------- /test/test/img/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaifix/Jin/HEAD/test/test/img/icon.png -------------------------------------------------------------------------------- /src/input/event.cpp: -------------------------------------------------------------------------------- 1 | #include "event.h" 2 | #include "SDL2\SDL.h" 3 | 4 | namespace jin 5 | { 6 | 7 | } -------------------------------------------------------------------------------- /src/lua/debug/m_debug.cpp: -------------------------------------------------------------------------------- 1 | namespace jin 2 | { 3 | namespace debug 4 | { 5 | 6 | 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.cpp linguist-language=c++ 2 | *.h linguist-language=c++ 3 | *.c linguist-language=c++ 4 | -------------------------------------------------------------------------------- /doc/todo.md: -------------------------------------------------------------------------------- 1 | * Unicode support. 2 | * Audio. 3 | * Networking. 4 | * Optimization. 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | usage 2 | 3 | jin [-d] 4 | 5 | for example 6 | 7 | jin test -d 8 | -------------------------------------------------------------------------------- /src/audio/audio.cpp: -------------------------------------------------------------------------------- 1 | #include "audio.h" 2 | 3 | namespace jin 4 | { 5 | namespace audio 6 | { 7 | 8 | 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /test/test/config.lua: -------------------------------------------------------------------------------- 1 | return 2 | { 3 | width = 600, 4 | height = 450, 5 | title = "test", 6 | fps = 60 7 | } 8 | -------------------------------------------------------------------------------- /src/lua/embed/graphics.lua: -------------------------------------------------------------------------------- 1 | ----------------- 2 | -- jin.graphics 3 | ----------------- 4 | 5 | jin.graphics = jin.graphics or {} 6 | 7 | -------------------------------------------------------------------------------- /src/utils/error.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_ERROR_H 2 | #define __JIN_ERROR_H 3 | 4 | #define assert(x, msg)\ 5 | if(!x) printf(msg) 6 | 7 | #endif -------------------------------------------------------------------------------- /src/utils/math.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_UTILS_MATH_H 2 | #define __JIN_UTILS_MATH_H 3 | 4 | #include 5 | 6 | #define PI 3.1415926f 7 | 8 | #endif -------------------------------------------------------------------------------- /src/audio/audio.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_AUDIO_H 2 | #define __JIN_AUDIO_H 3 | 4 | 5 | namespace jin 6 | { 7 | namespace audio 8 | { 9 | 10 | 11 | 12 | } 13 | } 14 | 15 | #endif -------------------------------------------------------------------------------- /src/input/event.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_EVENT_H 2 | #define __JIN_EVENT_H 3 | namespace jin 4 | { 5 | namespace input 6 | { 7 | 8 | 9 | 10 | } 11 | } 12 | 13 | #endif -------------------------------------------------------------------------------- /src/render/rect.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_RECT_H 2 | #define __JIN_RECT_H 3 | 4 | namespace jin 5 | { 6 | class Rect 7 | { 8 | public: 9 | int x, y, w, h; 10 | }; 11 | }// jin 12 | #endif -------------------------------------------------------------------------------- /src/input/keyboard.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_KEYBOARD_H 2 | #define __JIN_KEYBOARD_H 3 | namespace jin 4 | { 5 | namespace input 6 | { 7 | class Keyboard 8 | { 9 | 10 | }; 11 | } 12 | } 13 | #endif -------------------------------------------------------------------------------- /src/render/jsl.cpp: -------------------------------------------------------------------------------- 1 | #include "jsl.h" 2 | namespace jin 3 | { 4 | namespace render 5 | { 6 | const char base_v[] = " " 7 | " " 8 | " "; 9 | const char base_f[] = " " 10 | " " 11 | " "; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/vs2015/* 2 | !/build/vs2015/jin.sln 3 | !/build/vs2015/jin.vcxproj 4 | !/build/vs2015/jin.vcxproj.filters 5 | /build/vs2015/.vs/* 6 | /deps/* 7 | !/deps/DEPS 8 | /bin/* 9 | !/bin/LICENSE 10 | 11 | *.py 12 | 13 | *.o 14 | *.obj 15 | -------------------------------------------------------------------------------- /src/libs/tekcos/README: -------------------------------------------------------------------------------- 1 | Tekcos is a lightweight networking library supports basic TCP and 2 | UDP. You can trop tekcos.h and tekcos.c into your projects. Tekcos 3 | should works well on both windows, linux and macos. 4 | 5 | Copyright (c) 2016~2017 chai(neonum) -------------------------------------------------------------------------------- /src/render/quad.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_QUAD_H 2 | #define __JIN_QUAD_H 3 | 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | 9 | struct Quad 10 | { 11 | float x, y, w, h; 12 | }; 13 | 14 | } 15 | } 16 | 17 | #endif // !__JIN_RENDER_QUAD_H 18 | -------------------------------------------------------------------------------- /src/lua/embed/mouse.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | jin.mouse extension 3 | ]] 4 | 5 | jin.mouse = jin.mouse or {} 6 | 7 | local button = {} 8 | 9 | function jin.mouse.isDown(btn) 10 | return button[btn] 11 | end 12 | 13 | function jin.mouse.set(btn, status) 14 | button[btn] = status 15 | end 16 | -------------------------------------------------------------------------------- /src/lua/keyboard/m_keyboard.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "lua/embed/keyboard.lua.h" 3 | 4 | namespace jin 5 | { 6 | namespace module 7 | { 8 | 9 | int luaopen_keyboard(lua_State* L) 10 | { 11 | luax_newlib(L, 0); 12 | return 1; 13 | } 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /src/lua/m_types.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_M_TYPES_H 2 | #define __JIN_M_TYPES_H 3 | 4 | // graphics module 5 | #define TYPE_IMAGE "Image" 6 | #define TYPE_JSL "Shader" 7 | #define TYPE_CANVAS "Canvas" 8 | #define TYPE_FONT "Font" 9 | 10 | // audio module 11 | #define TYPE_SOUND "Sound" 12 | 13 | #endif -------------------------------------------------------------------------------- /doc/features.md: -------------------------------------------------------------------------------- 1 | * Subset of LOVE2d api 2 | 3 | * Lightweight 4 | 5 | * Opengl for rendering 6 | 7 | * Shader support(JSL) 8 | 9 | * Command line 10 | jin [, ...] 11 | -d for debug 12 | -p for packup game 13 | 14 | -------------------------------------------------------------------------------- /src/lua/embed/keyboard.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | jin.keyboard extension 3 | ]] 4 | 5 | jin.keyboard = jin.keyboard or {} 6 | 7 | local keys = {} 8 | 9 | function jin.keyboard.isDown(k) 10 | return keys[k] 11 | end 12 | 13 | function jin.keyboard.set(k, status) 14 | keys[k] = status 15 | end 16 | 17 | -------------------------------------------------------------------------------- /src/lua/embed/path.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | jin.path extension 3 | ]] 4 | 5 | jin.path = jin.path or {} 6 | 7 | -- game root directory 8 | jin._root = nil 9 | 10 | -- return full path of a given path 11 | function jin.path.full(path) 12 | local root = jin._dir .. '/' .. jin._argv[2] 13 | return root .. '/' .. path 14 | end 15 | 16 | -------------------------------------------------------------------------------- /src/libs/lua51/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/utils/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_UTILS_H 2 | #define __JIN_UTILS_H 3 | 4 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 5 | #define max(a,b) (((a) > (b)) ? (a) : (b)) 6 | #define clamp(a, mi,ma) min(max(a,mi),ma) 7 | 8 | #define within(a,min,max) (a >= min && a <= max) 9 | #define without(a,min,max) (a < min || a > max) 10 | 11 | #endif -------------------------------------------------------------------------------- /src/lua/audio/m_Sound.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | 3 | namespace jin 4 | { 5 | namespace module 6 | { 7 | 8 | static int l_play(lua_State* L) 9 | { 10 | 11 | return 0; 12 | } 13 | 14 | static const luaL_Reg f[] = { 15 | {"play", l_play}, 16 | {0, 0} 17 | }; 18 | 19 | int luaopen_Sound(lua_State* L) 20 | { 21 | 22 | return 1; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/lua/embed/graphics.lua.h: -------------------------------------------------------------------------------- 1 | /* graphics.lua */ 2 | static const char graphics_lua[] = 3 | {45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,13,10,45,45,32,106,105,110, 4 | 46,103,114,97,112,104,105,99,115,32,13,10,45,45,45,45,45,45,45,45,45,45,45,45, 5 | 45,45,45,45,45,13,10,13,10,106,105,110,46,103,114,97,112,104,105,99,115,32,61, 6 | 32,106,105,110,46,103,114,97,112,104,105,99,115,32,111,114,32,123,125,32,13, 7 | 10,13,10}; 8 | 9 | -------------------------------------------------------------------------------- /src/core/game.cpp: -------------------------------------------------------------------------------- 1 | #include "game.h" 2 | 3 | namespace jin 4 | { 5 | namespace core 6 | { 7 | Game* Game::g_game = 0; 8 | 9 | Game::Game() :run(true) {}; 10 | 11 | Game* Game::get() 12 | { 13 | return g_game ? g_game : (g_game = new Game()); 14 | } 15 | 16 | void Game::quit() 17 | { 18 | run = false; 19 | } 20 | 21 | bool Game::running() 22 | { 23 | return run; 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /src/core/game.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_CORE_GAME_H 2 | #define __JIN_CORE_GAME_H 3 | 4 | namespace jin 5 | { 6 | namespace core 7 | { 8 | class Game 9 | { 10 | public: 11 | 12 | void quit(); 13 | 14 | bool running(); 15 | 16 | static Game* get(); 17 | 18 | private: 19 | 20 | Game(); 21 | 22 | static Game* g_game; 23 | 24 | bool run; 25 | }; 26 | } 27 | } 28 | 29 | #endif -------------------------------------------------------------------------------- /src/lua/mouse/m_mouse.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "SDL2/SDL.h" 3 | namespace jin 4 | { 5 | namespace module 6 | { 7 | static int l_pos(lua_State* L) 8 | { 9 | int x, y; 10 | SDL_GetMouseState(&x, &y); 11 | luax_pushnumber(L, x); 12 | luax_pushnumber(L, y); 13 | return 2; 14 | } 15 | 16 | static const luaL_Reg f[] = { 17 | {"position", l_pos}, 18 | {0, 0} 19 | }; 20 | 21 | int luaopen_mouse(lua_State* L) 22 | { 23 | luax_newlib(L, f); 24 | return 1; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/lua/graphics/m_JSL.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "../m_types.h" 3 | namespace jin 4 | { 5 | namespace module 6 | { 7 | /** 8 | * Use send function send variables to JSL program. 9 | */ 10 | static int l_send(lua_State* L) 11 | { 12 | 13 | return 1; 14 | } 15 | 16 | static const luaL_Reg f[] = { 17 | {"send", l_send}, 18 | {0, 0} 19 | }; 20 | 21 | /** 22 | * JSL program 23 | */ 24 | int luaopen_JSL(lua_State* L) 25 | { 26 | luax_newtype(L, TYPE_JSL, f); 27 | return 0; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/input/mouse.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_MOUSE_H 2 | #define __JIN_MOUSE_H 3 | namespace jin 4 | { 5 | namespace input 6 | { 7 | class Mouse 8 | { 9 | public: 10 | 11 | }; 12 | 13 | inline const char* buttonStr(int id) { 14 | switch (id) { 15 | case 1: return "left"; 16 | case 2: return "middle"; 17 | case 3: return "right"; 18 | case 4: return "wheelup"; 19 | case 5: return "wheeldown"; 20 | default: return "?"; 21 | } 22 | } 23 | 24 | inline const char* wheelStr(int dir) 25 | { 26 | 27 | } 28 | } 29 | } 30 | #endif -------------------------------------------------------------------------------- /src/lua/audio/m_audio.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "audio/audio.h" 3 | 4 | namespace jin 5 | { 6 | namespace module 7 | { 8 | static int l_init(lua_State* L) 9 | { 10 | 11 | return 0; 12 | } 13 | 14 | static int l_newSound(lua_State* L) 15 | { 16 | 17 | return 0; 18 | } 19 | 20 | static const luaL_Reg f[] = { 21 | {"init", l_init}, 22 | {"Sound", l_newSound}, 23 | {0, 0} 24 | }; 25 | 26 | int luaopen_audio(lua_State* L) 27 | { 28 | 29 | return 1; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/render/canvas.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_CANVAS_H 2 | #define __JIN_CANVAS_H 3 | #include "drawable.h" 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | class Canvas: public Drawable 9 | { 10 | public: 11 | 12 | Canvas(); 13 | ~Canvas(); 14 | 15 | bool init(int w, int h); 16 | 17 | void bind(); 18 | 19 | static void unbind(); 20 | 21 | static bool hasbind(GLint fbo); 22 | 23 | private: 24 | 25 | GLuint fbo; 26 | 27 | // current binded fbo 28 | static GLint cur; 29 | }; 30 | } 31 | }// jin 32 | 33 | #endif -------------------------------------------------------------------------------- /src/render/image.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_IMAGE_H 2 | #define __JIN_IMAGE_H 3 | #include "libs/GLee/GLee.h" 4 | #include "color.h" 5 | #include "drawable.h" 6 | namespace jin 7 | { 8 | namespace render 9 | { 10 | class Image: public Drawable 11 | { 12 | public: 13 | Image(); 14 | ~Image(); 15 | 16 | // just like Image() 17 | void init(); 18 | 19 | // load from file 20 | bool loadf(const char* f); 21 | 22 | // load from memory 23 | bool loadb(const char* b, int size); 24 | 25 | color getPixel(int x, int y); 26 | 27 | private: 28 | 29 | color* pixels; 30 | }; 31 | } 32 | } 33 | 34 | #endif -------------------------------------------------------------------------------- /src/lua/time/m_time.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include 3 | namespace jin 4 | { 5 | namespace module 6 | { 7 | 8 | static int l_sec(lua_State* L) 9 | { 10 | luax_pushnumber(L, SDL_GetTicks()/1000.f); 11 | return 1; 12 | } 13 | 14 | static int l_sleep(lua_State* L) 15 | { 16 | double sec = luax_checknumber(L, 1); 17 | SDL_Delay(sec * 1000); 18 | return 0; 19 | } 20 | 21 | static const luaL_Reg f[] = { 22 | {"second", l_sec}, 23 | {"sleep", l_sleep}, 24 | {0, 0}, 25 | }; 26 | 27 | int luaopen_time(lua_State* L) 28 | { 29 | luax_newlib(L, f); 30 | return 1; 31 | } 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /src/utils/endian.h: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | #ifndef JIN_LIL_ENDIAN && JIN_BIG_ENDIAN 5 | 6 | #define JIN_LIL_ENDIAN 2 7 | #define JIN_BIG_ENDIAN 4 8 | 9 | #endif 10 | 11 | #ifndef JIN_BYTEORDER 12 | #ifdef __linux__ 13 | #include 14 | #define JIN_BYTEORDER __BYTE_ORDER 15 | #else /* __linux__ */ 16 | #if defined(__hppa__) || \ 17 | defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \ 18 | (defined(__MIPS__) && defined(__MISPEB__)) || \ 19 | defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \ 20 | defined(__sparc__) 21 | #define JIN_BYTEORDER JIN_BIG_ENDIAN 22 | #else 23 | #define JIN_BYTEORDER JIN_LIL_ENDIAN 24 | #endif 25 | #endif /* __linux__ */ 26 | #endif /* !SDL_BYTEORDER */ -------------------------------------------------------------------------------- /src/lua/core/m_core.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "core/game.h" 3 | using namespace jin::core; 4 | namespace jin 5 | { 6 | namespace module 7 | { 8 | static int l_running(lua_State* L) 9 | { 10 | bool r = Game::get()->running(); 11 | luax_pushboolean(L, r); 12 | return 1; 13 | } 14 | 15 | static int l_quit(lua_State* L) 16 | { 17 | Game::get()->quit(); 18 | return 0; 19 | } 20 | 21 | static const luaL_Reg f[] = { 22 | {"running", l_running}, 23 | {"quit", l_quit}, 24 | {0, 0} 25 | }; 26 | 27 | int luaopen_core(lua_State* L) 28 | { 29 | luax_newlib(L, f); 30 | 31 | return 1; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/fs/buffer.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace jin 4 | { 5 | namespace fs 6 | { 7 | 8 | /** 9 | * A file data buffer. 10 | */ 11 | class Buffer 12 | { 13 | public: 14 | 15 | inline Buffer(): data(0), size(0) 16 | { 17 | } 18 | 19 | inline ~Buffer() 20 | { 21 | size = 0; 22 | delete[] data; 23 | } 24 | 25 | inline Buffer(void* d, int size) 26 | { 27 | data = new char(size); 28 | memcpy(data, d, size); 29 | } 30 | 31 | public: 32 | 33 | // data position in memory 34 | void* data; 35 | 36 | // data buffer size 37 | unsigned int size; 38 | 39 | }; 40 | 41 | } 42 | } -------------------------------------------------------------------------------- /src/render/window.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_RENDER_WINDOW 2 | #define __JIN_RENDER_WINDOW 3 | #include "SDL2/SDL.h" 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | class Window 9 | { 10 | public: 11 | 12 | void init(int w, int h, const char* t); 13 | 14 | SDL_Window* getWnd(); 15 | 16 | SDL_GLContext getCtx(); 17 | 18 | static Window* get(); 19 | 20 | int getW(); 21 | int getH(); 22 | 23 | void swapBuffers(); 24 | 25 | private: 26 | 27 | Window(); 28 | ~Window(); 29 | 30 | static Window* g_wnd; 31 | 32 | SDL_Window* wnd; 33 | 34 | SDL_GLContext ctx; 35 | 36 | int w, h; 37 | }; 38 | } 39 | } 40 | #endif -------------------------------------------------------------------------------- /src/lua/embed/mouse.lua.h: -------------------------------------------------------------------------------- 1 | static const char mouse_lua[] = 2 | {45,45,91,91,32,13,10,9,109,111,117,115,101,32,101,120,116,101,110,115,105,111, 3 | 110,13,10,93,93,32,13,10,13,10,106,105,110,46,109,111,117,115,101,32,61,32, 4 | 106,105,110,46,109,111,117,115,101,32,111,114,32,123,125,32,13,10,13,10,108, 5 | 111,99,97,108,32,98,117,116,116,111,110,32,61,32,123,125,32,13,10,13,10,102, 6 | 117,110,99,116,105,111,110,32,106,105,110,46,109,111,117,115,101,46,105,115, 7 | 68,111,119,110,40,98,116,110,41,32,13,10,9,114,101,116,117,114,110,32,98,117, 8 | 116,116,111,110,91,98,116,110,93,13,10,101,110,100,32,13,10,13,10,102,117,110, 9 | 99,116,105,111,110,32,106,105,110,46,109,111,117,115,101,46,115,101,116,40,98, 10 | 116,110,44,32,115,116,97,116,117,115,41,32,13,10,9,98,117,116,116,111,110,91, 11 | 98,116,110,93,32,61,32,115,116,97,116,117,115,13,10,101,110,100,32,13,10}; 12 | -------------------------------------------------------------------------------- /src/render/graphics.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_GRAPHICS_H 2 | #define __JIN_GRAPHICS_H 3 | 4 | #include "color.h" 5 | #include "canvas.h" 6 | #include "image.h" 7 | 8 | namespace jin 9 | { 10 | namespace render 11 | { 12 | typedef enum { 13 | NONE = 0, 14 | FILL , 15 | LINE 16 | }RENDER_MODE; 17 | 18 | /** 19 | * TODO: 20 | * drawPixels(int n, points) 21 | */ 22 | extern void line(int x1, int y1, int x2, int y2); 23 | 24 | extern void rect(RENDER_MODE mode, int x, int y, int w, int h); 25 | 26 | extern void triangle(RENDER_MODE mode, int x1, int y1, int x2, int y2, int x3, int y3); 27 | 28 | extern void circle(RENDER_MODE mode, int x, int y, int r); 29 | 30 | extern void point(int x, int y); 31 | 32 | extern void points(int n, GLshort* p, GLubyte* c); 33 | 34 | extern void polygon(RENDER_MODE mode, float* p, int count); 35 | } 36 | } 37 | #endif -------------------------------------------------------------------------------- /src/lua/embed/keyboard.lua.h: -------------------------------------------------------------------------------- 1 | 2 | static const char keyboard_lua[] = 3 | { 45,45,91,91,32,13,10,9,107,101,121,98,111,97,114,100,32,101,120,116,101,110, 4 | 115,105,111,110,32,13,10,93,93,32,13,10,13,10,106,105,110,46,107,101,121,98, 5 | 111,97,114,100,32,61,32,106,105,110,46,107,101,121,98,111,97,114,100,32,111, 6 | 114,32,123,125,32,13,10,13,10,108,111,99,97,108,32,107,101,121,115,32,61,32, 7 | 123,125,32,13,10,13,10,102,117,110,99,116,105,111,110,32,106,105,110,46,107, 8 | 101,121,98,111,97,114,100,46,105,115,68,111,119,110,40,107,41,32,13,10,32,32, 9 | 32,32,114,101,116,117,114,110,32,107,101,121,115,91,107,93,13,10,101,110,100, 10 | 32,32,13,10,13,10,102,117,110,99,116,105,111,110,32,106,105,110,46,107,101, 11 | 121,98,111,97,114,100,46,115,101,116,40,107,44,32,115,116,97,116,117,115,41, 12 | 32,13,10,9,107,101,121,115,91,107,93,32,61,32,115,116,97,116,117,115,32,13,10, 13 | 101,110,100,32,13,10 }; 14 | -------------------------------------------------------------------------------- /src/libs/lua51/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Initialization of libraries for lua.c 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | #include "lua.h" 12 | 13 | #include "lualib.h" 14 | #include "lauxlib.h" 15 | 16 | 17 | static const luaL_Reg lualibs[] = { 18 | {"", luaopen_base}, 19 | {LUA_LOADLIBNAME, luaopen_package}, 20 | {LUA_TABLIBNAME, luaopen_table}, 21 | {LUA_IOLIBNAME, luaopen_io}, 22 | {LUA_OSLIBNAME, luaopen_os}, 23 | {LUA_STRLIBNAME, luaopen_string}, 24 | {LUA_MATHLIBNAME, luaopen_math}, 25 | {LUA_DBLIBNAME, luaopen_debug}, 26 | {NULL, NULL} 27 | }; 28 | 29 | 30 | LUALIB_API void luaL_openlibs (lua_State *L) { 31 | const luaL_Reg *lib = lualibs; 32 | for (; lib->func; lib++) { 33 | lua_pushcfunction(L, lib->func); 34 | lua_pushstring(L, lib->name); 35 | lua_call(L, 1, 0); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/libs/lua51/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lgc.h" 12 | #include "lobject.h" 13 | #include "lstate.h" 14 | 15 | 16 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 17 | 18 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 19 | 20 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 25 | 26 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 27 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 28 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/render/drawable.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_DRAWABLE 2 | #define __JIN_DRAWABLE 3 | #include "libs/GLee/GLee.h" 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | class Drawable 9 | { 10 | public: 11 | 12 | Drawable(); 13 | virtual ~Drawable(); 14 | 15 | /* pseudo constructor*/ 16 | void init(int w = 0, int h = 0); 17 | 18 | /* set anchor of texture, (0, 0) by default */ 19 | void setAnchor(int x, int y); 20 | 21 | void draw(int x, int y, float sx, float sy, float r); 22 | 23 | int getWidth(); 24 | int getHeight(); 25 | 26 | protected: 27 | #define DRAWABLE_V_SIZE 8 28 | void setVertices(float* v, float* t); 29 | 30 | GLuint texture; 31 | 32 | int width, height; 33 | 34 | /* anchor point */ 35 | int ancx, ancy; 36 | 37 | // render coords 38 | float* textCoord; 39 | float* vertCoord; 40 | 41 | }; 42 | } 43 | }// jin 44 | 45 | #endif -------------------------------------------------------------------------------- /src/render/jsl.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_JSL_H 2 | #define __JIN_JSL_H 3 | #include 4 | #include "libs/GLee/GLee.h" 5 | namespace jin 6 | { 7 | namespace render 8 | { 9 | /** 10 | * A JSL program for shadering textures which is 11 | * actually a glsl program. 12 | */ 13 | class JSLProgram 14 | { 15 | public: 16 | 17 | // psuedo constructor 18 | void init(); 19 | 20 | void use(); 21 | 22 | static void unuse(); 23 | 24 | void sendFloat(const char* name, int size, const GLfloat* vec, int count); 25 | void sendMatrix(const char* name, int size, const GLfloat* m, int count); 26 | //void sendImage(const char* name, const Image& image); 27 | //void sendCanvas(const char* name, const Canvas& canvas); 28 | 29 | static void hasuse(GLint id); 30 | 31 | private: 32 | 33 | // only id for identify glsl program 34 | GLuint pid; 35 | 36 | // currently used jsl program 37 | static GLint cur; 38 | }; 39 | } 40 | } 41 | #endif -------------------------------------------------------------------------------- /src/libs/lua51/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (char* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | #ifdef luac_c 23 | /* print one chunk; from print.c */ 24 | LUAI_FUNC void luaU_print (const Proto* f, int full); 25 | #endif 26 | 27 | /* for header of binary files -- this is Lua 5.1 */ 28 | #define LUAC_VERSION 0x51 29 | 30 | /* for header of binary files -- this is the official format */ 31 | #define LUAC_FORMAT 0 32 | 33 | /* size of header of binary files */ 34 | #define LUAC_HEADERSIZE 12 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/lua/graphics/m_Font.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "../m_types.h" 3 | #include "render/font.h" 4 | 5 | using namespace jin::render; 6 | 7 | namespace jin 8 | { 9 | namespace module 10 | { 11 | 12 | static int l_gc(lua_State* L) 13 | { 14 | return 0; 15 | } 16 | 17 | static int l_box(lua_State* L) 18 | { 19 | Font* font = (Font*)luax_checktype(L, 1, TYPE_FONT); 20 | const char* text = luax_checkstring(L, 2); 21 | int fheight = luax_checknumber(L, 3); 22 | int spacing = luax_checknumber(L, 4); 23 | int lheight = luax_checknumber(L, 5); 24 | int w, h; 25 | font->box(text, fheight, lheight, spacing, &w, &h); 26 | luax_pushnumber(L, w); 27 | luax_pushnumber(L, h); 28 | return 2; 29 | } 30 | 31 | static const luaL_Reg f[] = { 32 | {"__gc", l_gc}, 33 | {"box", l_box}, 34 | {0, 0} 35 | }; 36 | 37 | int luaopen_Font(lua_State* L) 38 | { 39 | luax_newtype(L, TYPE_FONT, f); 40 | 41 | return 0; 42 | } 43 | 44 | } 45 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2016~2017 chai(neonum) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copyof 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to use, 6 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 7 | Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 14 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 15 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 16 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 17 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 18 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /bin/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2016~2017 chai(neonum) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copyof 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to use, 6 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 7 | Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 14 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 15 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 16 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 17 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 18 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /src/fs/filesystem.h: -------------------------------------------------------------------------------- 1 | #include "buffer.h" 2 | #include "libs/smount/smount.h" 3 | 4 | namespace jin 5 | { 6 | namespace fs 7 | { 8 | 9 | class Filesystem 10 | { 11 | public: 12 | 13 | Filesystem(); 14 | 15 | static Filesystem* get(); 16 | 17 | /** 18 | * is a path a directroy or a single file 19 | */ 20 | bool isDir(const char* path); 21 | 22 | /** 23 | * is a path a directroy or a single file 24 | */ 25 | bool isFile(const char* path); 26 | 27 | /** 28 | * is path a valid path 29 | */ 30 | bool exists(const char* path); 31 | 32 | /** 33 | * read a file and return data buffer 34 | */ 35 | int read(const char* path, Buffer* buffer); 36 | 37 | /** 38 | * set root directory, can only mount once. 39 | */ 40 | void mount(const char* root); 41 | 42 | /** 43 | * convret relative path to absolute path 44 | */ 45 | const char* getFull(const char* path); 46 | 47 | private: 48 | 49 | static Filesystem* fs; 50 | 51 | sm_Shared* S; 52 | 53 | }; 54 | } 55 | } -------------------------------------------------------------------------------- /src/libs/lua51/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg (lua_State *L); 30 | LUAI_FUNC int luaG_checkcode (const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop (Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | // SDL main entry 3 | #include 4 | // directory 5 | #include 6 | #endif 7 | 8 | // luax 9 | #include "libs/luax/luax.h" 10 | 11 | // load jin module 12 | #include "lua/m_jin.h" 13 | using namespace jin::module; 14 | 15 | #include "fs/filesystem.h" 16 | 17 | int main(int argc, char* argv[]) 18 | { 19 | 20 | // global lua state, all lua values are here 21 | lua_State* L = luax_newstate(); 22 | luax_openlibs(L); 23 | 24 | /** 25 | * open jin module, jin module is on the top 26 | * of stack 27 | */ 28 | luaopen_jin(L); 29 | 30 | // add args to global field 31 | luax_newtable(L); 32 | for (int i = 0; i < argc; i++) 33 | luax_setraw_string(L, -2, i + 1, argv[i]); 34 | luax_setfield(L, -2, "_argv"); 35 | 36 | /** 37 | * jin._dir is the folder of jin binary executable 38 | */ 39 | int bsize = 1024; 40 | char* buffer = new char[bsize]; 41 | #ifdef _WIN32 42 | _getcwd(buffer, bsize); 43 | #elif defined __unix__ 44 | #elif defined __APPLE__ 45 | #endif 46 | #undef BUFFER_SIZE 47 | luax_setfield_string(L, "_dir", buffer); 48 | delete[] buffer; 49 | 50 | // boot 51 | boot(L); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /src/libs/lua51/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/libs/lua51/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/libs/lua51/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* Key to file-handle type */ 15 | #define LUA_FILEHANDLE "FILE*" 16 | 17 | 18 | #define LUA_COLIBNAME "coroutine" 19 | LUALIB_API int (luaopen_base) (lua_State *L); 20 | 21 | #define LUA_TABLIBNAME "table" 22 | LUALIB_API int (luaopen_table) (lua_State *L); 23 | 24 | #define LUA_IOLIBNAME "io" 25 | LUALIB_API int (luaopen_io) (lua_State *L); 26 | 27 | #define LUA_OSLIBNAME "os" 28 | LUALIB_API int (luaopen_os) (lua_State *L); 29 | 30 | #define LUA_STRLIBNAME "string" 31 | LUALIB_API int (luaopen_string) (lua_State *L); 32 | 33 | #define LUA_MATHLIBNAME "math" 34 | LUALIB_API int (luaopen_math) (lua_State *L); 35 | 36 | #define LUA_DBLIBNAME "debug" 37 | LUALIB_API int (luaopen_debug) (lua_State *L); 38 | 39 | #define LUA_LOADLIBNAME "package" 40 | LUALIB_API int (luaopen_package) (lua_State *L); 41 | 42 | 43 | /* open all previous libraries */ 44 | LUALIB_API void (luaL_openlibs) (lua_State *L); 45 | 46 | 47 | 48 | #ifndef lua_assert 49 | #define lua_assert(x) ((void)0) 50 | #endif 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/libs/lua51/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 26 | LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 27 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 28 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 29 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 30 | StkId val); 31 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 32 | StkId val); 33 | LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 34 | LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/lua/embed/embed.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_LUA_EMBED_H 2 | #define __JIN_LUA_EMBED_H 3 | #include 4 | 5 | namespace jin 6 | { 7 | namespace embed 8 | { 9 | 10 | /** 11 | * embed lua script to context. 12 | */ 13 | #define embed(L, script, name) \ 14 | if(luaL_loadbuffer(L, script, strlen(script), name) == 0)\ 15 | lua_call(L, 0, 0); 16 | 17 | /** 18 | * embed structure. 19 | */ 20 | struct jin_Embed 21 | { 22 | const char* fname, *source; 23 | }; 24 | 25 | static void boot(lua_State* L) 26 | { 27 | // embed scripts 28 | #include "graphics.lua.h" // graphics 29 | #include "keyboard.lua.h" // keyboard 30 | #include "mouse.lua.h" // mouse 31 | #include "debug.lua.h" // debug 32 | #include "boot.lua.h" // boot 33 | 34 | // embed scripts 35 | const jin_Embed scripts[] = { 36 | { "graphics.lua", graphics_lua }, 37 | { "keyboard.lua", keyboard_lua }, 38 | { "mouse.lua", mouse_lua }, 39 | { "debug.lua", debug_lua}, 40 | { "boot.lua", boot_lua }, 41 | { 0, 0 } 42 | }; 43 | 44 | // load all emebd lua scripts 45 | for (int i = 0; scripts[i].fname; i++) 46 | embed(L, scripts[i].source, scripts[i].fname); 47 | } 48 | } 49 | } 50 | 51 | #endif -------------------------------------------------------------------------------- /src/libs/lua51/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.nk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 22 | LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 28 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 29 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 30 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 31 | LUAI_FUNC int luaH_getn (Table *t); 32 | 33 | 34 | #if defined(LUA_DEBUG) 35 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 36 | LUAI_FUNC int luaH_isdummy (Node *n); 37 | #endif 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/render/font.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_FONT_H 2 | #define __JIN_FONT_H 3 | 4 | #include "drawable.h" 5 | #include "libs/stb/stb_truetype.h" 6 | #include "quad.h" 7 | 8 | namespace jin 9 | { 10 | namespace render 11 | { 12 | /** 13 | * Usage of stb_truetype.h here might be a little 14 | * bit dummy. Implementation of Font is referring 15 | * to stb_truetype.h L243~284. I basicly copy it:) 16 | */ 17 | class Font: public Drawable 18 | { 19 | public: 20 | 21 | Font(); 22 | 23 | /** 24 | * load ttf font data from .ttf 25 | */ 26 | void loadf(const char* file); 27 | 28 | /** 29 | * load ttf font data from memory 30 | */ 31 | void loadb(const unsigned char* data); 32 | 33 | /** 34 | * render text to screen 35 | */ 36 | void render( 37 | const char* str, // rendered text 38 | float x, float y, // render position 39 | int fheight, // font size 40 | int spacing, // font spacing 41 | int lheight // line height 42 | ); 43 | 44 | void box(const char* str, int fheight, int spacing, int lheight, int* w, int * h); 45 | 46 | private: 47 | 48 | /** 49 | * ASCII 32(space)..126(~) is 95 glyphs 50 | */ 51 | stbtt_bakedchar cdata[96]; 52 | 53 | }; 54 | 55 | } 56 | } 57 | 58 | #endif -------------------------------------------------------------------------------- /src/lua/embed/path.lua.h: -------------------------------------------------------------------------------- 1 | /* path.lua */ 2 | static const char path_lua[] = 3 | {45,45,91,91,32,13,10,32,32,32,32,106,105,110,46,112,97,116,104,32,101,120,116, 4 | 101,110,115,105,111,110,13,10,93,93,32,13,10,13,10,106,105,110,46,112,97,116, 5 | 104,32,61,32,106,105,110,46,112,97,116,104,32,111,114,32,123,125,32,13,10,13, 6 | 10,45,45,32,103,97,109,101,32,114,111,111,116,32,100,105,114,101,99,116,111, 7 | 114,121,32,13,10,106,105,110,46,95,114,111,111,116,32,61,32,110,105,108,32,13, 8 | 10,13,10,108,111,99,97,108,32,102,117,110,99,116,105,111,110,32,105,115,102, 9 | 117,108,108,40,112,97,116,104,41,32,13,10,32,32,32,32,13,10,101,110,100,32,13, 10 | 10,13,10,45,45,32,109,101,114,103,101,32,115,117,98,32,112,97,116,104,32,105, 11 | 110,116,111,32,111,110,101,32,13,10,108,111,99,97,108,32,102,117,110,99,116, 12 | 105,111,110,32,109,101,114,103,101,40,46,46,46,41,32,13,10,32,32,32,32,13,10, 13 | 101,110,100,32,13,10,13,10,45,45,32,114,101,116,117,114,110,32,102,117,108, 14 | 108,32,112,97,116,104,32,111,102,32,97,32,103,105,118,101,110,32,112,97,116, 15 | 104,32,13,10,102,117,110,99,116,105,111,110,32,106,105,110,46,112,97,116,104, 16 | 46,102,117,108,108,40,112,97,116,104,41,13,10,32,32,32,32,108,111,99,97,108, 17 | 32,114,111,111,116,32,61,32,106,105,110,46,95,100,105,114,32,46,46,32,39,47, 18 | 39,32,46,46,32,106,105,110,46,95,97,114,103,118,91,50,93,13,10,32,32,32,32, 19 | 114,101,116,117,114,110,32,114,111,111,116,32,46,46,32,39,47,39,32,46,46,32, 20 | 112,97,116,104,32,13,10,101,110,100,13,10,13,10}; 21 | 22 | -------------------------------------------------------------------------------- /src/fs/filesystem.cpp: -------------------------------------------------------------------------------- 1 | #include "filesystem.h" 2 | #include 3 | #include 4 | #include /* defines FILENAME_MAX */ 5 | 6 | namespace jin 7 | { 8 | namespace fs 9 | { 10 | 11 | Filesystem* Filesystem::fs = 0; 12 | 13 | Filesystem::Filesystem() 14 | { 15 | S = sm_newshared(); 16 | } 17 | 18 | Filesystem* Filesystem::get() 19 | { 20 | return fs ? fs : (fs = new Filesystem()); 21 | } 22 | 23 | /** 24 | * r is relative path 25 | */ 26 | void Filesystem::mount(const char * path) 27 | { 28 | int err = sm_mount(S, path); 29 | if (err) 30 | { 31 | printf("%s mounted path %s", sm_errstr(err), path); 32 | exit(1); 33 | } 34 | } 35 | 36 | /** 37 | * 38 | */ 39 | int Filesystem::read(const char* path, Buffer* buffer) 40 | { 41 | buffer->data = sm_read(S, path, &buffer->size); 42 | if (buffer->data == 0) 43 | return 0; 44 | return 1; 45 | } 46 | 47 | const char* Filesystem::getFull(const char* path) 48 | { 49 | return sm_fullpath(S, path); 50 | } 51 | 52 | bool Filesystem::isDir(const char* path) 53 | { 54 | return sm_isdir(S, path); 55 | } 56 | 57 | bool Filesystem::isFile(const char* path) 58 | { 59 | return sm_isreg(S, path); 60 | } 61 | 62 | bool Filesystem::exists(const char* path) 63 | { 64 | return sm_exists(S, path) == 0; 65 | } 66 | 67 | } 68 | } -------------------------------------------------------------------------------- /src/render/color.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Some color operating here. 3 | */ 4 | #ifndef __JIN_COLOR_H 5 | #define __JIN_COLOR_H 6 | #include "utils/endian.h" 7 | 8 | namespace jin 9 | { 10 | namespace render 11 | { 12 | 13 | class color { 14 | 15 | public: 16 | 17 | inline color(): r(0), g(0), b(0), a(0) 18 | { 19 | } 20 | 21 | inline color(int c) 22 | { 23 | #if JIN_BYTEORDER == JIN_BIG_ENDIAN 24 | /* big endian machine, ABGR */ 25 | r = c & 0xff000000 >> 24; 26 | g = c & 0xff0000 >> 16; 27 | b = c & 0xff00 >> 8; 28 | a = c & 0xff; 29 | #else 30 | /* little endian machine, RGBA */ 31 | r = c & 0xff; 32 | g = (c & 0xff00) >> 8; 33 | b = (c & 0xff0000) >> 16; 34 | a = (c & 0xff000000) >> 24; 35 | #endif 36 | } 37 | 38 | unsigned char r; 39 | unsigned char g; 40 | unsigned char b; 41 | unsigned char a; 42 | 43 | }; 44 | 45 | /** 46 | * Translate 8bits(only alpha channel) pixels to 32bits color pixels. 47 | */ 48 | inline color* a_c(unsigned char* src, int size) 49 | { 50 | color* pixels = new color[size]; 51 | for (int i = 0; i < size; i++) 52 | { 53 | pixels[i].r = 0xff; 54 | pixels[i].g = 0xff; 55 | pixels[i].b = 0xff; 56 | pixels[i].a = src[i]; 57 | } 58 | } 59 | 60 | } 61 | } 62 | 63 | #endif -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | # Jin 2 | 3 | 一个用于制作lua 2d游戏的游戏框架。 4 | 5 | ## 截图 6 | ![doc/screenshot/a.png](doc/screenshot/a.png) 7 | ![doc/screenshot/b.png](doc/screenshot/b.png) 8 | ![doc/screenshot/c.gif](doc/screenshot/c.gif) 9 | 10 | ## 指南 11 | 你可以在[发布页面](https://github.com/neonum/jin/releases)下载可执行文件。 为了运行 12 | 游戏,你需要把第一个参数设为你的游戏目录。比如,你的游戏放在一个文件夹`mygame`之下,必须 13 | 运行一下命令: 14 | 15 | ```batch 16 | jin mygame 17 | ``` 18 | 19 | 如果你想开启debug模式,需要将第二个参数设置为`-d`。 你需要在游戏目录下创建一个 `main.lua` 文件 20 | 来作为游戏入口。如果你想配置游戏的窗口大小,帧率,标题,你还需要创建一个 `config.lua` 文件。 21 | `config.lua` 类似以下形式: 22 | 23 | ```lua 24 | return{ 25 | width = 512, 26 | height = 512, 27 | fps = 60, 28 | title = "my title" 29 | } 30 | ``` 31 | 32 | 你需要在 `main.lua` 中定义四个函数,他们分别是: 33 | 34 | ```lua 35 | jin.core.load() -- run before game loop 36 | jin.core.onEvent(e) -- called every event loop 37 | jin.core.onUpdate(dt) -- called every frame 38 | jin.core.onDraw() -- called every frame 39 | ``` 40 | 41 | 以下是一个小例子,在屏幕上绘制圆形并打印hello,world: 42 | 43 | ```lua 44 | function jin.core.onEvent(e) 45 | if e.type == "quit" then 46 | jin.core.quit() 47 | end 48 | end 49 | 50 | function jin.core.onDraw() 51 | jin.graphics.circle("fill", 10, 10, 20) 52 | jin.graphics.write("hello, world", 100, 100, 16, 1, 20) 53 | end 54 | ``` 55 | 56 | API参见 [doc/api.md](doc/api.md) . 57 | 58 | ## 模块 59 | * 图形 60 | * 文件系统 61 | * 键盘输入 62 | * 鼠标输入 63 | * 计时器 64 | * 音频[WIP] 65 | * 网络支持[WIP] 66 | 67 | ## 许可证 68 | 详见 [LICENSE](LICENSE)。 69 | 70 | 71 | -------------------------------------------------------------------------------- /src/lua/m_jin.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2016~2017 chai(neonum) 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copyof 5 | * this software and associated documentation files (the "Software"), to deal in 6 | * the Software without restriction, including without limitation the rights to use, 7 | * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 8 | * Software, and to permit persons to whom the Software is furnished to do so, 9 | * subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in all 12 | * copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 15 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 16 | * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 17 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 18 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 19 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef __JIN_M_JIN_H 23 | #define __JIN_M_JIN_H 24 | #include "libs/luax/luax.h" 25 | 26 | #define MODULE_NAME "jin" 27 | #define VERSION "0.1.0" 28 | #define AUTHOR "chai(neonum)" 29 | 30 | namespace jin 31 | { 32 | namespace module 33 | { 34 | 35 | /** 36 | * open jin lib and boot 37 | */ 38 | int luaopen_jin(lua_State* L); 39 | 40 | void boot(lua_State* L); 41 | 42 | } 43 | } 44 | 45 | #endif -------------------------------------------------------------------------------- /src/lua/graphics/m_Canvas.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "../m_types.h" 3 | #include "render/canvas.h" 4 | using namespace jin::render; 5 | namespace jin 6 | { 7 | namespace module 8 | { 9 | static int l_getWidth(lua_State* L) 10 | { 11 | Canvas* c = (Canvas*)luax_checktype(L, 1, TYPE_CANVAS); 12 | luax_pushnumber(L, c->getWidth()); 13 | return 1; 14 | } 15 | 16 | static int l_getHeight(lua_State* L) 17 | { 18 | Canvas* c = (Canvas*)luax_checktype(L, 1, TYPE_CANVAS); 19 | luax_pushnumber(L, c->getHeight()); 20 | return 1; 21 | } 22 | 23 | static int l_getSize(lua_State* L) 24 | { 25 | Canvas* c = (Canvas*)luax_checktype(L, 1, TYPE_CANVAS); 26 | luax_pushnumber(L, c->getWidth()); 27 | luax_pushnumber(L, c->getHeight()); 28 | return 2; 29 | } 30 | 31 | static int l_setAnchor(lua_State* L) 32 | { 33 | Canvas* c = (Canvas*)luax_checktype(L, 1, TYPE_CANVAS); 34 | int x = luax_checknumber(L, 1); 35 | int y = luax_checknumber(L, 2); 36 | c->setAnchor(x, y); 37 | return 0; 38 | } 39 | 40 | static int l_gc(lua_State* L) 41 | { 42 | 43 | return 0; 44 | } 45 | 46 | static const luaL_Reg f[] = { 47 | {"__gc", l_gc}, 48 | {"getWidth", l_getWidth}, 49 | {"getHeight", l_getHeight}, 50 | {"getSize", l_getSize}, 51 | {"setAnchor", l_setAnchor}, 52 | {0, 0 } 53 | }; 54 | 55 | int luaopen_Canvas(lua_State* L) 56 | { 57 | luax_newtype(L, TYPE_CANVAS, f); 58 | return 0; 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /src/libs/lua51/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig (lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /src/libs/lua51/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define char2int(c) cast(int, cast(unsigned char, (c))) 21 | 22 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/libs/lua51/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | 23 | const char *const luaT_typenames[] = { 24 | "nil", "boolean", "userdata", "number", 25 | "string", "table", "function", "userdata", "thread", 26 | "proto", "upval" 27 | }; 28 | 29 | 30 | void luaT_init (lua_State *L) { 31 | static const char *const luaT_eventname[] = { /* ORDER TM */ 32 | "__index", "__newindex", 33 | "__gc", "__mode", "__eq", 34 | "__add", "__sub", "__mul", "__div", "__mod", 35 | "__pow", "__unm", "__len", "__lt", "__le", 36 | "__concat", "__call" 37 | }; 38 | int i; 39 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 41 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 42 | } 43 | } 44 | 45 | 46 | /* 47 | ** function to be used with macro "fasttm": optimized for absence of 48 | ** tag methods 49 | */ 50 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 51 | const TValue *tm = luaH_getstr(events, ename); 52 | lua_assert(event <= TM_EQ); 53 | if (ttisnil(tm)) { /* no tag method? */ 54 | events->flags |= cast_byte(1u<metatable; 66 | break; 67 | case LUA_TUSERDATA: 68 | mt = uvalue(o)->metatable; 69 | break; 70 | default: 71 | mt = G(L)->mt[ttype(o)]; 72 | } 73 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 74 | } 75 | 76 | -------------------------------------------------------------------------------- /src/libs/lua51/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) return EOZ; 29 | z->n = size - 1; 30 | z->p = buff; 31 | return char2int(*(z->p++)); 32 | } 33 | 34 | 35 | int luaZ_lookahead (ZIO *z) { 36 | if (z->n == 0) { 37 | if (luaZ_fill(z) == EOZ) 38 | return EOZ; 39 | else { 40 | z->n++; /* luaZ_fill removed first byte; put back it */ 41 | z->p--; 42 | } 43 | } 44 | return char2int(*z->p); 45 | } 46 | 47 | 48 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 49 | z->L = L; 50 | z->reader = reader; 51 | z->data = data; 52 | z->n = 0; 53 | z->p = NULL; 54 | } 55 | 56 | 57 | /* --------------------------------------------------------------- read --- */ 58 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 59 | while (n) { 60 | size_t m; 61 | if (luaZ_lookahead(z) == EOZ) 62 | return n; /* return number of missing bytes */ 63 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 64 | memcpy(b, z->p, m); 65 | z->n -= m; 66 | z->p += m; 67 | b = (char *)b + m; 68 | n -= m; 69 | } 70 | return 0; 71 | } 72 | 73 | /* ------------------------------------------------------------------------ */ 74 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 75 | if (n > buff->buffsize) { 76 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 77 | luaZ_resizebuffer(L, buff, n); 78 | } 79 | return buff->buffer; 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /src/libs/smount/smount.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017 chai(neonum) 3 | */ 4 | #ifndef SMOUNT_H 5 | #define SMOUNT_H 6 | 7 | // path type 8 | enum 9 | { 10 | PATH_DIR = 1, // directory 11 | PATH_REG = 2 // regular file 12 | }; 13 | 14 | // sm status 15 | enum 16 | { 17 | SM_SUCCESS = 0, 18 | SM_INVALIDMOUNT = 1, // invalid mount directory. 19 | SM_NOSUCHDIR = 2, // directory or file doesn't exsist. 20 | SM_UNABLEOPEN = 3, // 21 | SM_CANTWRITE = 4, 22 | }; 23 | 24 | typedef struct sm_Path 25 | { 26 | int type; 27 | char* path; 28 | struct sm_Path* next; 29 | }sm_Path; 30 | 31 | /** 32 | * A shared context structrue. 33 | */ 34 | typedef struct sm_Shared 35 | { 36 | // the root directory 37 | sm_Path* mount; 38 | 39 | }sm_Shared; 40 | 41 | sm_Shared* sm_newshared(); 42 | 43 | void sm_closeshared(sm_Shared* S); 44 | 45 | /** 46 | * Get error string with given error code. 47 | */ 48 | const char *sm_errstr(int err); 49 | 50 | /** 51 | * Mount a sub file system. 52 | */ 53 | int sm_mount(sm_Shared* S, const char *path); 54 | 55 | /** 56 | * Free mount 57 | */ 58 | void sm_unmount(sm_Shared* S); 59 | 60 | int sm_exists(sm_Shared* S, const char *path); 61 | 62 | /** 63 | * Get size of a file. 64 | */ 65 | int sm_size(sm_Shared* S, const char *path); 66 | 67 | /** 68 | * Can only read files under root directory. 69 | */ 70 | void *sm_read(sm_Shared* S, const char *path, unsigned int *size); 71 | 72 | int sm_isdir(sm_Shared* S, const char *path); 73 | 74 | int sm_isreg(sm_Shared* S, const char *path); 75 | 76 | /** 77 | * List all folders and files inside current mount directory. 78 | */ 79 | sm_Path *sm_list(sm_Shared*S, const char *path); 80 | 81 | void sm_freelist(sm_Path* S); 82 | 83 | int sm_write(sm_Shared* S, const char *path, const void *data, int size); 84 | 85 | void sm_delete(sm_Shared* S, const char *path); 86 | 87 | int sm_mkdir(sm_Shared* S, const char *path); 88 | 89 | char* sm_fullpath(sm_Shared* S, const char* path); 90 | 91 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jin 2 | [中文版](README_zh.md) 3 | A game framework for making 2D games in lua. 4 | 5 | ## Screenshot 6 | ![doc/screenshot/a.png](doc/screenshot/a.png) 7 | ![doc/screenshot/b.png](doc/screenshot/b.png) 8 | ![doc/screenshot/c.gif](doc/screenshot/c.gif) 9 | 10 | ## Guide 11 | You can download Jin from the [releases page](https://github.com/neonum/jin/releases). To 12 | run the game, you need set the first argument as your game directory. For example, if your 13 | game is in a directory called `mygame`, you would run the following command: 14 | 15 | ```batch 16 | jin mygame 17 | ``` 18 | 19 | If you want to open debug mode, set the second argument to `-d`. You need make a 20 | `main.lua` file inside game directory, which is entry of the game. If you want to config 21 | window size, fps and title, create a `config.lua` file inside game directory. The configure 22 | file would be like this: 23 | 24 | ```lua 25 | return{ 26 | width = 512, 27 | height = 512, 28 | fps = 60, 29 | title = "my title" 30 | } 31 | ``` 32 | 33 | To create your game, you need create 4 functions(optional) in `main.lua`. They are: 34 | 35 | ```lua 36 | jin.core.load() -- run before game loop 37 | jin.core.onEvent(e) -- called every event loop 38 | jin.core.onUpdate(dt) -- called every frame 39 | jin.core.onDraw() -- called every frame 40 | ``` 41 | 42 | Here is a small example which draws a filled circle and prints "hello, world" on screen: 43 | 44 | ```lua 45 | function jin.core.onEvent(e) 46 | if e.type == "quit" then 47 | jin.core.quit() 48 | end 49 | end 50 | 51 | function jin.core.onDraw() 52 | jin.graphics.circle("fill", 10, 10, 20) 53 | jin.graphics.write("hello, world", 100, 100, 16, 1, 20) 54 | end 55 | ``` 56 | 57 | See [doc/api.md](doc/api.md) for reference. 58 | 59 | ## Modules 60 | * Graphics 61 | * Filesystem 62 | * Keyboard 63 | * Mouse 64 | * Timer 65 | * Audio[WIP] 66 | * Network[WIP] 67 | 68 | ## License 69 | See [LICENSE](LICENSE) for details. 70 | 71 | 72 | -------------------------------------------------------------------------------- /test/test/main.lua: -------------------------------------------------------------------------------- 1 | local jc = jin.core 2 | local je = jin.event 3 | local jg = jin.graphics 4 | local jk = jin.keyboard 5 | local jm = jin.mouse 6 | local ja = jin.audio 7 | local jf = jin.filesystem 8 | local jn = jin.net 9 | local jt = jin.time 10 | local jd = jin.debug 11 | 12 | local img = nil 13 | local fnt = nil 14 | function jin.core.load() 15 | jd.size(3) 16 | img = jg.Image("./img/icon.png") 17 | fnt = jg.Font("font.ttf") 18 | local w, h = fnt:box("nima", 16, 1, 20) 19 | jd.print("./main.lua:22: debug \n moduie is currently in use.") 20 | jd.print("./init.lua:13: init graphics subsystem successed.") 21 | jd.print("./main.lua:13: load subsystems.") 22 | jd.print("./main.lua:13: init \n games.") 23 | end 24 | 25 | local str = "" 26 | function jin.core.onEvent(e) 27 | if e.type == "quit" then 28 | jc.quit() 29 | elseif e.type == "wheel" then 30 | print(e.y) 31 | elseif e.type == "keydown" then 32 | if e.key == "Escape" then 33 | jc.quit() 34 | end 35 | if e.key == "Return" then 36 | str = str .. '\n' 37 | elseif e.key == "Space" then 38 | str = str .. ' ' 39 | else 40 | str = str .. e.key 41 | end 42 | end 43 | end 44 | 45 | local i = 0 46 | function jin.core.onUpdate(dt) 47 | i = i + 0.01 48 | if i > 1 then 49 | jin.debug.print(i) 50 | i = 0 51 | end 52 | end 53 | 54 | local c = jg.Canvas(500, 500) 55 | local r = 0 56 | 57 | function jin.core.onDraw() 58 | 59 | r =r + 0.01 60 | jg.color() 61 | jg.circle("fill", 100, 400, 100* math.sin(r)) 62 | img:setAnchor(20, 20) 63 | local s = 1 + 5 * math.abs(math.sin(r)) 64 | jg.draw(img, 100, 400, s, s, math.cos(r)) 65 | jg.draw(img, 300, 400, s, s, math.cos(r)) 66 | -- jg.color(255, 0, 0, 255) 67 | -- jg.clear() 68 | jg.study() 69 | jg.bind(c) 70 | jg.clear(0, 0, 0, 0) 71 | --jg.color(255,0,0,255) 72 | jg.write("This is a text test.", 100, 100, 16, 1, 20) 73 | jg.bind() 74 | jg.draw(c, 0, 0, 1, 1) 75 | 76 | end 77 | -------------------------------------------------------------------------------- /src/lua/graphics/m_Image.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "render/image.h" 3 | #include "../m_types.h" 4 | 5 | using namespace jin::render; 6 | 7 | namespace jin 8 | { 9 | namespace module 10 | { 11 | 12 | static int l_getWidth(lua_State* L) 13 | { 14 | Image* i = (Image*)luax_checktype(L, 1, TYPE_IMAGE); 15 | luax_pushnumber(L, i->getWidth()); 16 | return 1; 17 | } 18 | 19 | static int l_getHeight(lua_State *L) 20 | { 21 | Image* i = (Image*)luax_checktype(L, 1, TYPE_IMAGE); 22 | luax_pushnumber(L, i->getHeight()); 23 | return 1; 24 | } 25 | 26 | static int l_getPixel(lua_State* L) 27 | { 28 | Image* i = (Image*)luax_checktype(L, 1, TYPE_IMAGE); 29 | int x = luax_checknumber(L, 2); 30 | int y = luax_checknumber(L, 3); 31 | color c = i->getPixel(x, y); 32 | luax_pushnumber(L, c.r); 33 | luax_pushnumber(L, c.g); 34 | luax_pushnumber(L, c.b); 35 | luax_pushnumber(L, c.a); 36 | return 4; 37 | } 38 | 39 | static int l_setAnchor(lua_State* L) 40 | { 41 | Image* i = (Image*)luax_checktype(L, 1, TYPE_IMAGE); 42 | int x = luax_checknumber(L, 2); 43 | int y = luax_checknumber(L, 3); 44 | i->setAnchor(x, y); 45 | return 0; 46 | } 47 | 48 | static int l_getSize(lua_State* L) 49 | { 50 | Image* i = (Image*)luax_checktype(L, 1, TYPE_IMAGE); 51 | luax_pushnumber(L, i->getWidth()); 52 | luax_pushnumber(L, i->getHeight()); 53 | return 2; 54 | } 55 | 56 | static int l_gc(lua_State* L) 57 | { 58 | 59 | return 0; 60 | } 61 | 62 | static const luaL_Reg f[] = { 63 | {"__gc", l_gc}, 64 | {"getWidth", l_getWidth}, 65 | {"getHeight", l_getHeight}, 66 | {"getSize", l_getSize}, 67 | {"getPixel", l_getPixel}, 68 | {"setAnchor", l_setAnchor}, 69 | {0, 0 } 70 | }; 71 | 72 | int luaopen_Image(lua_State* L) 73 | { 74 | luax_newtype(L, TYPE_IMAGE, f); 75 | return 0; 76 | } 77 | 78 | }// graphics 79 | }// jin -------------------------------------------------------------------------------- /src/libs/lua51/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | 36 | 37 | /* type of protected functions, to be ran by `runprotected' */ 38 | typedef void (*Pfunc) (lua_State *L, void *ud); 39 | 40 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 41 | LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 45 | ptrdiff_t oldtop, ptrdiff_t ef); 46 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 47 | LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 48 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 49 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 50 | 51 | LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 52 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 53 | 54 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /src/render/drawable.cpp: -------------------------------------------------------------------------------- 1 | #include "drawable.h" 2 | #include "utils/matrix.h" 3 | #include 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | Drawable::Drawable():texture(0), width(0), height(0), ancx(0), ancy(0), textCoord(0), vertCoord(0) 9 | { 10 | } 11 | 12 | Drawable::~Drawable() 13 | { 14 | glDeleteTextures(1, &texture); 15 | delete[] vertCoord; 16 | delete[] textCoord; 17 | } 18 | 19 | void Drawable::init(int w, int h) 20 | { 21 | texture = 0; 22 | width = w; 23 | height = h; 24 | ancx = 0; 25 | ancy = 0; 26 | textCoord = 0; 27 | vertCoord = 0; 28 | } 29 | 30 | void Drawable::setVertices(float* v, float* t) 31 | { 32 | // render area 33 | if (vertCoord) 34 | delete[] vertCoord; 35 | vertCoord = v; 36 | 37 | // textrue 38 | if (textCoord) 39 | delete[] textCoord; 40 | textCoord = t; 41 | } 42 | 43 | void Drawable::setAnchor(int x, int y) 44 | { 45 | ancx = x; 46 | ancy = y; 47 | } 48 | 49 | int Drawable::getWidth() 50 | { 51 | return width; 52 | } 53 | 54 | int Drawable::getHeight() 55 | { 56 | return height; 57 | } 58 | 59 | void Drawable::draw(int x, int y, float sx, float sy, float r) 60 | { 61 | // Must set textCoord and vertCoord before renderring 62 | if (! textCoord||! vertCoord) return; 63 | 64 | static jin::util::Matrix t; 65 | t.setTransformation(x, y, r, sx, sy, ancx, ancy); 66 | 67 | glEnable(GL_TEXTURE_2D); 68 | 69 | glBindTexture(GL_TEXTURE_2D, texture); 70 | 71 | // push modle matrix 72 | glPushMatrix(); 73 | glMultMatrixf((const GLfloat*)t.getElements()); 74 | 75 | glEnableClientState(GL_VERTEX_ARRAY); 76 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); 77 | glTexCoordPointer(2, GL_FLOAT, 0, textCoord); 78 | glVertexPointer(2, GL_FLOAT, 0, vertCoord); 79 | glDrawArrays(GL_QUADS, 0, 4); 80 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); 81 | glDisableClientState(GL_VERTEX_ARRAY); 82 | 83 | // pop the model matrix 84 | glPopMatrix(); 85 | 86 | // bind texture to default screen 87 | glBindTexture(GL_TEXTURE_2D, 0); 88 | 89 | glDisable(GL_TEXTURE_2D); 90 | } 91 | } 92 | } -------------------------------------------------------------------------------- /src/libs/lua51/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 24 | enum RESERVED { 25 | /* terminal symbols denoted by reserved words */ 26 | TK_AND = FIRST_RESERVED, TK_BREAK, 27 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 28 | TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 29 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 30 | /* other terminal symbols */ 31 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 32 | TK_NAME, TK_STRING, TK_EOS 33 | }; 34 | 35 | /* number of reserved words */ 36 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 37 | 38 | 39 | /* array with token `names' */ 40 | LUAI_DATA const char *const luaX_tokens []; 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | TString *ts; 46 | } SemInfo; /* semantics information */ 47 | 48 | 49 | typedef struct Token { 50 | int token; 51 | SemInfo seminfo; 52 | } Token; 53 | 54 | 55 | typedef struct LexState { 56 | int current; /* current character (charint) */ 57 | int linenumber; /* input line counter */ 58 | int lastline; /* line of last token `consumed' */ 59 | Token t; /* current token */ 60 | Token lookahead; /* look ahead token */ 61 | struct FuncState *fs; /* `FuncState' is private to the parser */ 62 | struct lua_State *L; 63 | ZIO *z; /* input stream */ 64 | Mbuffer *buff; /* buffer for tokens */ 65 | TString *source; /* current source name */ 66 | char decpoint; /* locale decimal point */ 67 | } LexState; 68 | 69 | 70 | LUAI_FUNC void luaX_init (lua_State *L); 71 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 72 | TString *source); 73 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 74 | LUAI_FUNC void luaX_next (LexState *ls); 75 | LUAI_FUNC void luaX_lookahead (LexState *ls); 76 | LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 77 | LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 78 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 79 | 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/libs/lua51/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | /* 24 | ** About the realloc function: 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26 | ** (`osize' is the old size, `nsize' is the new size) 27 | ** 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). 29 | ** 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL). 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *errormsg) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, errormsg); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | void *luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | return NULL; /* to avoid warnings */ 69 | } 70 | 71 | 72 | 73 | /* 74 | ** generic allocation routine. 75 | */ 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 77 | global_State *g = G(L); 78 | lua_assert((osize == 0) == (block == NULL)); 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); 80 | if (block == NULL && nsize > 0) 81 | luaD_throw(L, LUA_ERRMEM); 82 | lua_assert((nsize == 0) == (block == NULL)); 83 | g->totalbytes = (g->totalbytes - osize) + nsize; 84 | return block; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /src/render/window.cpp: -------------------------------------------------------------------------------- 1 | #include "window.h" 2 | #include "libs/GLee/GLee.h" 3 | #include "canvas.h" 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | 9 | Window* Window::g_wnd = 0; 10 | 11 | Window* Window::get() 12 | { 13 | return (g_wnd ? g_wnd : (g_wnd = new Window())); 14 | } 15 | 16 | Window::Window(): wnd(0), ctx(0) 17 | { 18 | } 19 | 20 | Window::~Window() 21 | { 22 | } 23 | 24 | void Window::init(int pw, int ph, const char* t) 25 | { 26 | w = pw; 27 | h = ph; 28 | 29 | if (wnd) 30 | { 31 | SDL_DestroyWindow(wnd); 32 | SDL_FlushEvent(SDL_WINDOWEVENT); 33 | } 34 | 35 | if (ctx) 36 | { 37 | SDL_GL_DeleteContext(ctx); 38 | } 39 | 40 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 41 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); 42 | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); 43 | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); 44 | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); 45 | SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); 46 | 47 | Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL; 48 | 49 | int wx = SDL_WINDOWPOS_UNDEFINED, 50 | wy = SDL_WINDOWPOS_UNDEFINED; 51 | 52 | /* Create window */ 53 | wnd = SDL_CreateWindow(t, wx, wy, w, h, flags); 54 | 55 | // Create an opengl context 56 | ctx = SDL_GL_CreateContext(wnd); 57 | SDL_GL_MakeCurrent(wnd, ctx); 58 | 59 | // Default clear color 60 | glClearColor(0.f, 0.f, 0.f, 1.f); 61 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); 62 | 63 | // Default render color 64 | glColor4f(1, 1, 1, 1); 65 | 66 | /** 67 | * Set the viewport to top-left corner. 68 | * Bind to the default render buffer. 69 | */ 70 | Canvas::unbind(); 71 | 72 | // Swap window buffer 73 | swapBuffers(); 74 | } 75 | 76 | SDL_Window* Window::getWnd() 77 | { 78 | return wnd; 79 | } 80 | 81 | SDL_GLContext Window::getCtx() 82 | { 83 | return ctx; 84 | } 85 | 86 | int Window::getW() 87 | { 88 | return w; 89 | } 90 | 91 | int Window::getH() 92 | { 93 | return h; 94 | } 95 | 96 | void Window::swapBuffers() 97 | { 98 | if (wnd) 99 | SDL_GL_SwapWindow(wnd); 100 | } 101 | 102 | } 103 | } -------------------------------------------------------------------------------- /src/lua/m_jin.cpp: -------------------------------------------------------------------------------- 1 | #include "m_jin.h" 2 | #include "libs/luax/luax.h" 3 | 4 | // embed all lua scripts 5 | #include "embed/embed.h" 6 | 7 | namespace jin 8 | { 9 | namespace module 10 | { 11 | /** 12 | * sub modules 13 | */ 14 | extern int luaopen_core(lua_State* L); 15 | extern int luaopen_graphics(lua_State* L); 16 | extern int luaopen_audio(lua_State* L); 17 | extern int luaopen_net(lua_State* L); 18 | extern int luaopen_event(lua_State* L); 19 | extern int luaopen_time(lua_State* L); 20 | extern int luaopen_mouse(lua_State* L); 21 | extern int luaopen_keyboard(lua_State* L); 22 | extern int luaopen_filesystem(lua_State* L); 23 | 24 | static int l_getversion(lua_State* L) 25 | { 26 | luax_pushstring(L, VERSION); 27 | return 1; 28 | } 29 | 30 | static int l_getAuthor(lua_State* L) 31 | { 32 | luax_pushstring(L, AUTHOR); 33 | return 1; 34 | } 35 | 36 | static int l_getOS(lua_State* L) 37 | { 38 | #ifdef _WIN32 39 | luax_pushstring(L, "windows"); 40 | #elif defined __unix__ 41 | luax_pushstring(L, "unix"); 42 | #elif defined __APPLE__ 43 | luax_pushstring(L, "macos"); 44 | #endif 45 | return 1; 46 | } 47 | 48 | static const luaL_Reg f[] = { 49 | {"version", l_getversion}, 50 | {"author", l_getAuthor}, 51 | {"os", l_getOS}, 52 | {0, 0} 53 | }; 54 | 55 | // submodules 56 | static const luaL_Reg mods[] = { 57 | {"core", luaopen_core}, 58 | {"event", luaopen_event}, 59 | {"graphics", luaopen_graphics}, 60 | {"time", luaopen_time}, 61 | {"mouse", luaopen_mouse}, 62 | {"keyboard", luaopen_keyboard}, 63 | {"filesystem", luaopen_filesystem}, 64 | {"net", luaopen_net}, 65 | /* 66 | {"audio", luaopen_audio} 67 | */ 68 | {0, 0} 69 | }; 70 | 71 | int luaopen_jin(lua_State* L) 72 | { 73 | // jin module is on top of the stack 74 | luax_newlib(L, f); 75 | 76 | // set to global field 77 | luax_justglobal(L, -1, MODULE_NAME); 78 | 79 | // register submodules 80 | for (int i = 0; mods[i].name; i++) 81 | { 82 | // open submodules 83 | mods[i].func(L); 84 | luax_setfield(L, -2, mods[i].name); 85 | } 86 | 87 | return 1; 88 | } 89 | 90 | /** 91 | * boot jin 92 | */ 93 | void boot(lua_State* L) 94 | { 95 | jin::embed::boot(L); 96 | } 97 | 98 | } 99 | } -------------------------------------------------------------------------------- /src/libs/lua51/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VLOCAL, /* info = local register */ 27 | VUPVAL, /* info = index of upvalue in `upvalues' */ 28 | VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 29 | VINDEXED, /* info = table register; aux = index register (or `k') */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VNONRELOC, /* info = result register */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | typedef struct expdesc { 38 | expkind k; 39 | union { 40 | struct { int info, aux; } s; 41 | lua_Number nval; 42 | } u; 43 | int t; /* patch list of `exit when true' */ 44 | int f; /* patch list of `exit when false' */ 45 | } expdesc; 46 | 47 | 48 | typedef struct upvaldesc { 49 | lu_byte k; 50 | lu_byte info; 51 | } upvaldesc; 52 | 53 | 54 | struct BlockCnt; /* defined in lparser.c */ 55 | 56 | 57 | /* state needed to generate code for a given function */ 58 | typedef struct FuncState { 59 | Proto *f; /* current function header */ 60 | Table *h; /* table to find (and reuse) elements in `k' */ 61 | struct FuncState *prev; /* enclosing function */ 62 | struct LexState *ls; /* lexical state */ 63 | struct lua_State *L; /* copy of the Lua state */ 64 | struct BlockCnt *bl; /* chain of current blocks */ 65 | int pc; /* next position to code (equivalent to `ncode') */ 66 | int lasttarget; /* `pc' of last `jump target' */ 67 | int jpc; /* list of pending jumps to `pc' */ 68 | int freereg; /* first free register */ 69 | int nk; /* number of elements in `k' */ 70 | int np; /* number of elements in `p' */ 71 | short nlocvars; /* number of elements in `locvars' */ 72 | lu_byte nactvar; /* number of active local variables */ 73 | upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 74 | unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 75 | } FuncState; 76 | 77 | 78 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 79 | const char *name); 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /src/lua/net/m_net.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Notice: the net module is not finished yet. 3 | */ 4 | #include "libs/luax/luax.h" 5 | #include "libs/tekcos/tekcos.h" 6 | 7 | namespace jin 8 | { 9 | namespace module 10 | { 11 | 12 | struct 13 | { 14 | tk_TCPsocket* sk; 15 | }context; 16 | 17 | /** 18 | * A table is needed. For example: 19 | * local conf = { 20 | * mode = "server", 21 | * ip = "", 22 | * port = 8000 23 | * } 24 | */ 25 | static int l_open(lua_State* L) 26 | { 27 | // init context.sk 28 | context.sk = 0; 29 | if (!luax_istable(L, 1)) 30 | { 31 | luax_typerror(L, 1, "table is needed"); 32 | return 0; 33 | } 34 | luax_getfield(L, 1, "mode"); 35 | if (luax_isnil(L, -1)) 36 | {// no mode field 37 | luax_error(L, "mode field is needed, but get nil"); 38 | return 0; 39 | } 40 | const char* mode = luax_checkstring(L, -1); 41 | if (strcmp(mode, "server") == 0 || strcmp(mode, "client") == 0) 42 | { 43 | 44 | if (strcmp(mode, "server") == 0) 45 | {// a server, ignore ip field 46 | 47 | } 48 | else 49 | { 50 | 51 | } 52 | } 53 | else 54 | { 55 | luax_error(L, "\"server\" or \"client\" is needed, but get %s", mode); 56 | return 0; 57 | } 58 | return 1; 59 | } 60 | 61 | static int l_accept(lua_State* L) 62 | { 63 | return 1; 64 | } 65 | 66 | static int l_send(lua_State* L) 67 | { 68 | return 1; 69 | } 70 | 71 | static int l_recv(lua_State* L) 72 | { 73 | return 1; 74 | } 75 | 76 | static int l_close(lua_State* L) 77 | { 78 | return 1; 79 | } 80 | 81 | static int l_nonblocking(lua_State* L) 82 | { 83 | return 1; 84 | } 85 | 86 | // block mode by default 87 | static int l_blocking(lua_State* L) 88 | { 89 | return 1; 90 | } 91 | 92 | static const luaL_Reg f[] = { 93 | {"open", l_open}, 94 | {"accept", l_accept}, 95 | {"send", l_send}, 96 | {"recv", l_recv}, 97 | {"close", l_close}, 98 | {"blocking", l_blocking }, 99 | {"nonblocking", l_nonblocking}, 100 | {0, 0} 101 | }; 102 | 103 | // only tcp 104 | int luaopen_net(lua_State* L) 105 | { 106 | luax_newlib(L, f); 107 | 108 | return 1; 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /src/libs/tekcos/tekcos.h: -------------------------------------------------------------------------------- 1 | #ifndef _TEKCOS_H 2 | #define _TEKCOS_H 3 | 4 | #if defined(_WIN32) 5 | #include 6 | #include 7 | #else 8 | #include 9 | #include 10 | #include 11 | #include 12 | #endif 13 | 14 | #define TK_VERSION "0.1.0" 15 | 16 | typedef unsigned int uint32; 17 | typedef unsigned short uint16; 18 | 19 | #ifdef _WIN32 20 | // neccessary for windows 21 | int tk_init(); 22 | #endif 23 | // Under AF_INET domain, we use 32bit host + 16bit port to 24 | // locate a proccess. 25 | typedef struct 26 | { 27 | uint32 host; // 32bit ip 28 | uint16 port; // 16 bit port 29 | } tk_IPaddress; 30 | 31 | uint32 tk_strtohl(const char* str); // string to host long(32 bits) 32 | const char* tk_hltostr(uint32 ip); // host long to string 33 | #define tk_htons htons // host to network short(16 bits) 34 | #define tk_ntohl ntohl // network to host long(32bits) 35 | 36 | /* 37 | * TCP socket 38 | * type = SOCK_STREAM 39 | * protocol = IPPROTO_TCP 40 | */ 41 | 42 | #ifdef __linux__ 43 | typedef unsigned int SOCKET; 44 | #endif 45 | // TCP socket structrue. 46 | typedef struct 47 | { 48 | SOCKET id; // socket id 49 | int type; // socket type 50 | tk_IPaddress remote; // remote ip 51 | tk_IPaddress local; // local ip 52 | } tk_TCPsocket; 53 | 54 | // create a tcp socket. if ip.host is INADDR_NONE or 55 | // INADDR_ANY, creeate a listenning server socket, 56 | // otherwise, connect to a remote server with given 57 | // ip address. 58 | tk_TCPsocket* tk_tcp_open(tk_IPaddress ip); 59 | 60 | int tk_tcp_close(tk_TCPsocket* sk); 61 | 62 | int tk_tcp_send(tk_TCPsocket* client, const void* buffer, int bsize, int* len); 63 | 64 | int tk_tcp_recv(tk_TCPsocket* client, char* buffer, int bsize, int* len); 65 | 66 | tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server); 67 | 68 | int tk_tcp_nonblocking(tk_TCPsocket* sk); 69 | 70 | int tk_tcp_blocking(tk_TCPsocket* sk); 71 | 72 | /* 73 | * UDP socket 74 | * type = SOCK_DGRAM 75 | * protocol = IPPTOTO_UDP 76 | */ 77 | 78 | // UDP socket structure. 79 | typedef struct 80 | { 81 | SOCKET id; // socket id 82 | } tk_UDPsocket; 83 | 84 | typedef struct 85 | { 86 | tk_IPaddress ip; // recvfrom or sendto ip addreass 87 | int len; // length of data 88 | char* data; // data 89 | }tk_UDPpack; 90 | 91 | tk_UDPsocket* tk_udp_open(uint16 portnumber); 92 | 93 | int tk_udp_close(tk_UDPsocket* sk); 94 | 95 | int tk_udp_sendto(tk_UDPsocket* sk, tk_UDPpack* pack); 96 | 97 | int tk_udp_recvfrom(tk_UDPsocket* sk, tk_UDPpack* pack); 98 | 99 | int tk_freepack(tk_UDPpack* pack); 100 | 101 | // Get error message if some errors occured. 102 | const char* tk_errmsg(); 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /src/libs/lua51/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_NE, OPR_EQ, 30 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 46 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 47 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 48 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 49 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 50 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 51 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 52 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 53 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 54 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 55 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 56 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 59 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 60 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 61 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 62 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 63 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 64 | LUAI_FUNC int luaK_jump (FuncState *fs); 65 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 66 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 67 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 68 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 69 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 70 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 71 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 72 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 73 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/libs/lua51/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /src/lua/embed/debug.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | for debug purpose 3 | +-------------------+ 4 | |debug msg old | 5 | |... | 6 | |... | 7 | |... | 8 | |debug msg new | 9 | +-------------------+ 10 | ]] 11 | 12 | jin.debug = jin.debug or {} 13 | 14 | -- render panel 15 | local panel = nil 16 | 17 | local debug = false 18 | 19 | -- debug msg buffer 20 | local buffer = {} 21 | 22 | -- configure 23 | local bsize = 10 24 | local fsize = 15 25 | local lheight = 18 26 | local alpha = 220 27 | local margin = 10 28 | 29 | -- refresh buffer or not 30 | local refresh = true 31 | 32 | function jin.debug.init() 33 | debug = true 34 | panel = jin.graphics.Canvas(jin.graphics.size()) 35 | end 36 | 37 | -- set buffer size 38 | function jin.debug.size(c) 39 | bsize = c 40 | end 41 | 42 | function jin.debug.print(msg) 43 | if not debug then return end 44 | 45 | msg = tostring(msg) 46 | local tp = type(msg) 47 | if tp ~= "string" and tp ~= "number" then 48 | msg = string.format("print failed, expect string or number but get a %s", tp) 49 | end 50 | 51 | -- remove the first one (old msg) 52 | if #buffer >= bsize then 53 | table.remove(buffer, 1) 54 | end 55 | 56 | buffer[#buffer + 1] = msg 57 | refresh = true 58 | end 59 | 60 | -- clear debug buffer 61 | function jin.debug.clear() 62 | buffer = {} 63 | end 64 | 65 | local function getStrHeight(str, lheight) 66 | local h = lheight 67 | if #str == 0 then 68 | h = 0 69 | end 70 | for i = 1, #str do 71 | local c = string.sub(str, i, i) 72 | if c == '\n' then 73 | h = h + lheight 74 | end 75 | end 76 | return h 77 | end 78 | 79 | local function getBgQuad() 80 | local width, height = 0, 0 81 | for i = 1, #buffer do 82 | local w, h = jin.graphics.box( buffer[i], fsize, 1, lheight) 83 | height = height + h 84 | if width < w then 85 | width = w 86 | end 87 | end 88 | return width, height 89 | end 90 | 91 | -- render to screen 92 | function jin.debug.render() 93 | if not debug then return end 94 | 95 | if refresh then 96 | 97 | jin.graphics.bind(panel) 98 | 99 | jin.graphics.clear(0, 0, 0, 0) 100 | 101 | jin.graphics.study() 102 | 103 | local ww, wh = jin.graphics.size() 104 | local bgw, bgh = getBgQuad() 105 | jin.graphics.color(0, 0, 0, alpha) 106 | jin.graphics.rect("fill", 0, wh - bgh - margin, bgw + margin, bgh + margin) 107 | 108 | jin.graphics.color() 109 | local y = wh 110 | for i = #buffer, 1, -1 do 111 | local msg = buffer[i] 112 | local h = getStrHeight(msg, lheight) 113 | y = y - h 114 | jin.graphics.write(msg, margin / 2, y - margin/ 2, fsize, 1, lheight) 115 | end 116 | 117 | jin.graphics.bind() 118 | 119 | refresh = false 120 | end 121 | 122 | jin.graphics.color() 123 | jin.graphics.draw(panel, 0, 0) 124 | end 125 | 126 | function jin.debug.status() 127 | return debug 128 | end 129 | -------------------------------------------------------------------------------- /src/render/canvas.cpp: -------------------------------------------------------------------------------- 1 | #include "canvas.h" 2 | #include "window.h" 3 | 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | Canvas::Canvas() :Drawable() 9 | { 10 | } 11 | 12 | Canvas::~Canvas() 13 | { 14 | } 15 | 16 | // no canvas has binded 17 | GLint Canvas::cur = -1; 18 | 19 | bool Canvas::init(int w, int h) 20 | { 21 | Drawable::init(w, h); 22 | Drawable::setVertices( 23 | new float [DRAWABLE_V_SIZE] { 24 | 0, 0, 25 | 0, (float)h, 26 | (float)w, (float)h, 27 | (float)w, 0, 28 | }, 29 | new float [DRAWABLE_V_SIZE] { 30 | 0, 1, 31 | 0, 0, 32 | 1, 0, 33 | 1, 1 34 | } 35 | ); 36 | 37 | GLint current_fbo; 38 | glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, ¤t_fbo); 39 | 40 | // generate a new render buffer object 41 | glGenFramebuffers(1, &fbo); 42 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); 43 | 44 | // generate texture save target 45 | glGenTextures(1, &texture); 46 | glBindTexture(GL_TEXTURE_2D, texture); 47 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 48 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 49 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 50 | glBindTexture(GL_TEXTURE_2D, 0); 51 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); 52 | 53 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 54 | 55 | // unbind framebuffer 56 | glBindFramebuffer(GL_FRAMEBUFFER, current_fbo); 57 | 58 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) 59 | return false; 60 | return true; 61 | } 62 | 63 | bool Canvas::hasbind(GLint fbo) 64 | { 65 | return cur == fbo; 66 | } 67 | 68 | /** 69 | * bind to canvas 70 | */ 71 | void Canvas::bind() 72 | { 73 | if (hasbind(fbo)) return; 74 | 75 | cur = fbo; 76 | 77 | glBindFramebuffer(GL_FRAMEBUFFER, fbo); 78 | 79 | glMatrixMode(GL_PROJECTION); 80 | glPushMatrix(); 81 | glLoadIdentity(); 82 | 83 | glViewport(0, 0, width, height); 84 | glOrtho(0, width, height, 0, -1, 1); 85 | 86 | // Switch back to modelview matrix 87 | glMatrixMode(GL_MODELVIEW); 88 | glPushMatrix(); 89 | glLoadIdentity(); 90 | } 91 | 92 | /** 93 | * bind to default screen render buffer. 94 | */ 95 | /*static*/ void Canvas::unbind() 96 | { 97 | if (hasbind(0)) return; 98 | 99 | cur = 0; 100 | 101 | glBindFramebuffer(GL_FRAMEBUFFER, 0); 102 | Window* wnd = Window::get(); 103 | int ww = wnd->getW(), 104 | wh = wnd->getH(); 105 | 106 | glViewport(0, 0, ww, wh); 107 | 108 | // load back to normal 109 | glMatrixMode(GL_PROJECTION); 110 | glPushMatrix(); 111 | glLoadIdentity(); 112 | 113 | // set viewport matrix 114 | glOrtho(0, ww, wh, 0, -1, 1); 115 | 116 | // switch to model matrix 117 | glMatrixMode(GL_MODELVIEW); 118 | glPushMatrix(); 119 | glLoadIdentity(); 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /src/render/graphics.cpp: -------------------------------------------------------------------------------- 1 | #include "graphics.h" 2 | #include "utils/math.h" 3 | #include 4 | namespace jin 5 | { 6 | namespace render 7 | { 8 | 9 | void point(int x, int y) 10 | { 11 | float vers[] = { x + 0.5f , y + 0.5f }; 12 | glEnableClientState(GL_VERTEX_ARRAY); 13 | glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)vers); 14 | glDrawArrays(GL_POINTS, 0, 1); 15 | glDisableClientState(GL_VERTEX_ARRAY); 16 | } 17 | 18 | void points(int n, GLshort* p) 19 | { 20 | glEnableClientState(GL_VERTEX_ARRAY); 21 | 22 | glVertexPointer(2, GL_SHORT, 0, (GLvoid*)p); 23 | glDrawArrays(GL_POINTS, 0, n); 24 | 25 | glDisableClientState(GL_VERTEX_ARRAY); 26 | } 27 | 28 | void line(int x1, int y1, int x2, int y2) 29 | { 30 | glDisable(GL_TEXTURE_2D); 31 | float verts[] = { 32 | x1, y1, 33 | x2, y2 34 | }; 35 | 36 | glEnableClientState(GL_VERTEX_ARRAY); 37 | glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)verts); 38 | glDrawArrays(GL_LINES, 0, 2); 39 | glDisableClientState(GL_VERTEX_ARRAY); 40 | } 41 | 42 | void circle(RENDER_MODE mode, int x, int y, int r) 43 | { 44 | r = r < 0 ? 0 : r; 45 | 46 | int points = 40; 47 | float two_pi = static_cast(PI * 2); 48 | if (points <= 0) points = 1; 49 | float angle_shift = (two_pi / points); 50 | float phi = .0f; 51 | 52 | float *coords = new float[2 * (points + 1)]; 53 | for (int i = 0; i < points; ++i, phi += angle_shift) 54 | { 55 | coords[2 * i] = x + r * cos(phi); 56 | coords[2 * i + 1] = y + r * sin(phi); 57 | } 58 | 59 | coords[2 * points] = coords[0]; 60 | coords[2 * points + 1] = coords[1]; 61 | 62 | polygon(mode, coords, points); 63 | 64 | delete[] coords; 65 | } 66 | 67 | void rect(RENDER_MODE mode, int x, int y, int w, int h) 68 | { 69 | float coords[] = { x, y, x + w, y, x + w, y + h, x, y + h }; 70 | polygon(mode, coords, 4); 71 | } 72 | 73 | void triangle(RENDER_MODE mode, int x1, int y1, int x2, int y2, int x3, int y3) 74 | { 75 | float coords[] = { x1, y1, x2, y2, x3, y3 }; 76 | polygon(mode, coords, 3); 77 | } 78 | 79 | void polygon_line(float* p, int count) 80 | { 81 | float* verts = new float[count * 4]; 82 | for (int i = 0; i < count; i++) 83 | { 84 | // each line has two point n,n+1 85 | verts[i * 4] = p[i * 2]; 86 | verts[i * 4 + 1] = p[i * 2 + 1]; 87 | verts[i * 4 + 2] = p[(i + 1) % count * 2]; 88 | verts[i * 4 + 3] = p[(i + 1) % count * 2 + 1]; 89 | } 90 | 91 | glEnableClientState(GL_VERTEX_ARRAY); 92 | glVertexPointer(2, GL_FLOAT, 0, (GLvoid*)verts); 93 | glDrawArrays(GL_LINES, 0, count * 2); 94 | glDisableClientState(GL_VERTEX_ARRAY); 95 | 96 | delete[] verts; 97 | } 98 | 99 | void polygon(RENDER_MODE mode, float* p, int count) 100 | { 101 | if (mode == LINE) 102 | { 103 | polygon_line(p, count); 104 | } 105 | else if (mode == FILL) 106 | { 107 | glEnableClientState(GL_VERTEX_ARRAY); 108 | glVertexPointer(2, GL_FLOAT, 0, (const GLvoid*)p); 109 | glDrawArrays(GL_POLYGON, 0, count); 110 | glDisableClientState(GL_VERTEX_ARRAY); 111 | } 112 | } 113 | 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/libs/lua51/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADBOOL", 20 | "LOADNIL", 21 | "GETUPVAL", 22 | "GETGLOBAL", 23 | "GETTABLE", 24 | "SETGLOBAL", 25 | "SETUPVAL", 26 | "SETTABLE", 27 | "NEWTABLE", 28 | "SELF", 29 | "ADD", 30 | "SUB", 31 | "MUL", 32 | "DIV", 33 | "MOD", 34 | "POW", 35 | "UNM", 36 | "NOT", 37 | "LEN", 38 | "CONCAT", 39 | "JMP", 40 | "EQ", 41 | "LT", 42 | "LE", 43 | "TEST", 44 | "TESTSET", 45 | "CALL", 46 | "TAILCALL", 47 | "RETURN", 48 | "FORLOOP", 49 | "FORPREP", 50 | "TFORLOOP", 51 | "SETLIST", 52 | "CLOSE", 53 | "CLOSURE", 54 | "VARARG", 55 | NULL 56 | }; 57 | 58 | 59 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 60 | 61 | const lu_byte luaP_opmodes[NUM_OPCODES] = { 62 | /* T A B C mode opcode */ 63 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 64 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 65 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 66 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 67 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 68 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 69 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 70 | ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 71 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 72 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 73 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 74 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 75 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 76 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 77 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 81 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 82 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 83 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 84 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 85 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 86 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 87 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 88 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 89 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 90 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 91 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 92 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 93 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 94 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 95 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 96 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 97 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 98 | ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 99 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 100 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 101 | }; 102 | 103 | -------------------------------------------------------------------------------- /src/lua/embed/boot.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | program entry 3 | ]] 4 | 5 | local function _onEvent(e) 6 | -- update keyboard status 7 | if e.type == "keydown" then 8 | jin.keyboard.set(e.key, true) 9 | elseif e.type == "keyup" then 10 | jin.keyboard.set(e.key, false) 11 | end 12 | 13 | -- call user onEvent function 14 | if jin.core.onEvent then 15 | jin.core.onEvent(e) 16 | end 17 | end 18 | 19 | ------------------------------------------------- 20 | -- init file system 21 | ------------------------------------------------- 22 | jin._argv[2] = jin._argv[2] or '.' 23 | jin.filesystem.init() 24 | jin.filesystem.mount(jin._argv[2]) 25 | 26 | -- config 27 | local conf = {} 28 | if jin.filesystem.exist("config.lua") then 29 | conf = require "config" 30 | end 31 | conf.width = conf.width or 600 32 | conf.height = conf.height or 500 33 | conf.fps = conf.fps or 60 34 | conf.title = conf.title or ("jin v" .. jin.version()) 35 | 36 | -- init video subsystem 37 | jin.graphics.init(conf.width,conf.height,conf.title) 38 | 39 | -- open debug mode, must after jin.graphics.init 40 | if jin._argv[3] == '-d' then 41 | jin.debug.init() 42 | end 43 | 44 | function jin.core.run() 45 | local now = jin.time.second() 46 | local last = now 47 | local fsec = 1/conf.fps 48 | -- for loading resources 49 | if jin.core.load then 50 | jin.core.load() 51 | end 52 | while(jin.core.running()) do 53 | -- frame controle 54 | last = now 55 | now = jin.time.second() 56 | if (now - last) < fsec then 57 | jin.time.sleep(fsec - now + last) 58 | end 59 | 60 | -- handle events 61 | for _, e in pairs(jin.event.poll()) do 62 | if _onEvent then 63 | _onEvent(e) 64 | end 65 | end 66 | 67 | -- update 68 | local dt = now - last 69 | if dt < fsec then 70 | dt = fsec 71 | end 72 | if jin.core.onUpdate then 73 | jin.core.onUpdate(dt) 74 | end 75 | 76 | -- bind to default render buffer 77 | jin.graphics.bind() 78 | jin.graphics.clear() 79 | jin.graphics.color() 80 | jin.graphics.study() 81 | 82 | -- custom drawing 83 | if jin.core.onDraw then 84 | jin.core.onDraw() 85 | end 86 | 87 | -- render debug window 88 | if jin.debug.status() then 89 | jin.debug.render() 90 | end 91 | 92 | -- swap window buffer 93 | jin.graphics.present() 94 | 95 | end 96 | end 97 | 98 | local function onError(msg) 99 | local tab = ' ' 100 | print("Error:\n" .. msg) 101 | function jin.core.onEvent(e) 102 | if e.type == 'quit' then 103 | jin.core.quit() 104 | end 105 | end 106 | local ww, wh = jin.graphics.size() 107 | function jin.core.onDraw() 108 | jin.graphics.write("Error: ", 10, 10, 30, 3, 30) 109 | jin.graphics.write(msg, 10, 50) 110 | end 111 | end 112 | 113 | if jin.filesystem.exist("main.lua") then 114 | -- require main game script 115 | xpcall(function() require"main" end, onError) 116 | jin.core.run() 117 | else 118 | -- no game 119 | function jin.core.onEvent(e) 120 | if e.type == 'quit' then 121 | jin.core.quit() 122 | end 123 | end 124 | function jin.core.onDraw() 125 | jin.graphics.clear(111, 134, 125, 255) 126 | local ww, wh = jin.graphics.size() 127 | local fw, fh = jin.graphics.box("no game", 20, 1, 20) 128 | jin.graphics.write("no game", ww /2 - fw / 2, wh * 2/3, 16, 1, 18) 129 | end 130 | jin.core.run() 131 | end 132 | -------------------------------------------------------------------------------- /src/libs/lua51/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | 22 | void luaS_resize (lua_State *L, int newsize) { 23 | GCObject **newhash; 24 | stringtable *tb; 25 | int i; 26 | if (G(L)->gcstate == GCSsweepstring) 27 | return; /* cannot resize during GC traverse */ 28 | newhash = luaM_newvector(L, newsize, GCObject *); 29 | tb = &G(L)->strt; 30 | for (i=0; isize; i++) { 33 | GCObject *p = tb->hash[i]; 34 | while (p) { /* for each node in the list */ 35 | GCObject *next = p->gch.next; /* save next */ 36 | unsigned int h = gco2ts(p)->hash; 37 | int h1 = lmod(h, newsize); /* new position */ 38 | lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 39 | p->gch.next = newhash[h1]; /* chain it */ 40 | newhash[h1] = p; 41 | p = next; 42 | } 43 | } 44 | luaM_freearray(L, tb->hash, tb->size, TString *); 45 | tb->size = newsize; 46 | tb->hash = newhash; 47 | } 48 | 49 | 50 | static TString *newlstr (lua_State *L, const char *str, size_t l, 51 | unsigned int h) { 52 | TString *ts; 53 | stringtable *tb; 54 | if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 55 | luaM_toobig(L); 56 | ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 57 | ts->tsv.len = l; 58 | ts->tsv.hash = h; 59 | ts->tsv.marked = luaC_white(G(L)); 60 | ts->tsv.tt = LUA_TSTRING; 61 | ts->tsv.reserved = 0; 62 | memcpy(ts+1, str, l*sizeof(char)); 63 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 64 | tb = &G(L)->strt; 65 | h = lmod(h, tb->size); 66 | ts->tsv.next = tb->hash[h]; /* chain new entry */ 67 | tb->hash[h] = obj2gco(ts); 68 | tb->nuse++; 69 | if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 70 | luaS_resize(L, tb->size*2); /* too crowded */ 71 | return ts; 72 | } 73 | 74 | 75 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 76 | GCObject *o; 77 | unsigned int h = cast(unsigned int, l); /* seed */ 78 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 79 | size_t l1; 80 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 81 | h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 82 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 83 | o != NULL; 84 | o = o->gch.next) { 85 | TString *ts = rawgco2ts(o); 86 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { 87 | /* string may be dead */ 88 | if (isdead(G(L), o)) changewhite(o); 89 | return ts; 90 | } 91 | } 92 | return newlstr(L, str, l, h); /* not found */ 93 | } 94 | 95 | 96 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 97 | Udata *u; 98 | if (s > MAX_SIZET - sizeof(Udata)) 99 | luaM_toobig(L); 100 | u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 101 | u->uv.marked = luaC_white(G(L)); /* is not finalized */ 102 | u->uv.tt = LUA_TUSERDATA; 103 | u->uv.len = s; 104 | u->uv.metatable = NULL; 105 | u->uv.env = e; 106 | /* chain it on udata list (after main thread) */ 107 | u->uv.next = G(L)->mainthread->next; 108 | G(L)->mainthread->next = obj2gco(u); 109 | return u; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /src/lua/filesystem/m_filesystem.cpp: -------------------------------------------------------------------------------- 1 | #include "libs/luax/luax.h" 2 | #include "fs/filesystem.h" 3 | #include 4 | 5 | using namespace jin::fs; 6 | 7 | namespace jin 8 | { 9 | namespace module 10 | { 11 | 12 | static struct 13 | { 14 | Filesystem* fs; 15 | } context; 16 | 17 | static int l_init(lua_State* L) 18 | { 19 | context.fs = Filesystem::get(); 20 | return 0; 21 | } 22 | 23 | /** 24 | * set current game root, like 25 | * C:/jin/games/tank/ 26 | */ 27 | static int l_mount(lua_State* L) 28 | { 29 | const char* path = luax_checkstring(L, 1); 30 | context.fs->mount(path); 31 | return 0; 32 | } 33 | 34 | /** 35 | * 36 | */ 37 | static int l_isDir(lua_State *L) 38 | { 39 | const char* path = luax_checkstring(L, 1); 40 | int r = context.fs->isDir(path); 41 | luax_pushboolean(L, r); 42 | return 1; 43 | } 44 | 45 | /** 46 | * 47 | */ 48 | static int l_exist(lua_State * L) 49 | { 50 | const char* path = luax_checkstring(L, 1); 51 | int r = context.fs->exists(path); 52 | luax_pushboolean(L, r); 53 | return 1; 54 | } 55 | 56 | static int l_isdir(lua_State* L) 57 | { 58 | const char* path = luax_checkstring(L, 1); 59 | int r = context.fs->isDir(path); 60 | luax_pushboolean(L, r); 61 | return 1; 62 | } 63 | 64 | // load but dont run it 65 | static int loadf(lua_State* L) 66 | { 67 | const char* filename = lua_tostring(L, -1); 68 | Buffer bf; 69 | context.fs->read(filename, &bf); 70 | luax_loadbuffer(L, (const char*)bf.data, bf.size, filename); 71 | return 1; 72 | } 73 | 74 | static int loader(lua_State* L) 75 | { 76 | const char * filename = lua_tostring(L, -1); 77 | 78 | std::string tmp(filename); 79 | tmp += ".lua"; 80 | 81 | int size = tmp.size(); 82 | 83 | for (int i = 0; iexists(tmp.c_str())) 93 | { 94 | lua_pop(L, 1); 95 | lua_pushstring(L, tmp.c_str()); 96 | // Ok, load it. 97 | return loadf(L); 98 | } 99 | 100 | tmp = filename; 101 | size = tmp.size(); 102 | for (int i = 0; iisDir(tmp.c_str())) 111 | { 112 | tmp += "/init.lua"; 113 | if (context.fs->exists(tmp.c_str())) 114 | { 115 | lua_pop(L, 1); 116 | lua_pushstring(L, tmp.c_str()); 117 | // Ok, load it. 118 | return loadf(L); 119 | } 120 | } 121 | 122 | lua_pushfstring(L, "\n\tno file \"%s\" in jin game directories.\n", (tmp + ".lua").c_str()); 123 | return 1; 124 | } 125 | 126 | static const luaL_Reg f[] = { 127 | {"init", l_init}, 128 | {"mount", l_mount}, 129 | {"isdir", l_isDir}, 130 | {"exist", l_exist}, 131 | {0, 0} 132 | }; 133 | 134 | int luaopen_filesystem(lua_State* L) 135 | { 136 | luax_newlib(L, f); 137 | luax_register_searcher(L, loader, 1); 138 | return 0; 139 | } 140 | 141 | } 142 | } -------------------------------------------------------------------------------- /src/lua/event/m_event.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Event module 3 | */ 4 | #include "input/event.h" 5 | #include "libs/luax/luax.h" 6 | #include "SDL2/SDL.h" 7 | #include "input/event.h" 8 | #include "input/mouse.h" 9 | #include "input/keyboard.h" 10 | 11 | using namespace jin::input; 12 | 13 | namespace jin 14 | { 15 | namespace module 16 | { 17 | /** 18 | * Load event poll, return a iterator(a table). 19 | */ 20 | static int l_event_poll(lua_State *L) 21 | { 22 | // table to store events 23 | luax_newtable(L); 24 | SDL_Event e; 25 | int i = 1; 26 | poll: 27 | while (SDL_PollEvent(&e)) 28 | { 29 | // each event is a table 30 | luax_newtable(L); 31 | switch (e.type) 32 | { 33 | case SDL_QUIT: 34 | luax_setfield_string(L, "type", "quit"); 35 | break; 36 | 37 | case SDL_KEYDOWN: 38 | luax_setfield_string(L, "type", "keydown"); 39 | luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); 40 | break; 41 | 42 | case SDL_KEYUP: 43 | luax_setfield_string(L, "type", "keyup"); 44 | luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); 45 | break; 46 | 47 | case SDL_MOUSEMOTION: 48 | luax_setfield_string(L, "type", "mousemotion"); 49 | luax_setfield_number(L, "x", e.motion.x); 50 | luax_setfield_number(L, "y", e.motion.y); 51 | break; 52 | 53 | case SDL_MOUSEBUTTONDOWN: 54 | luax_setfield_string(L, "type", "mousebuttondown"); 55 | luax_setfield_string(L, "button", buttonStr(e.button.button)); 56 | luax_setfield_number(L, "x", e.button.x); 57 | luax_setfield_number(L, "y", e.button.y); 58 | break; 59 | 60 | case SDL_MOUSEBUTTONUP: 61 | luax_setfield_string(L, "type", "mousebuttonup"); 62 | luax_setfield_string(L, "button", buttonStr(e.button.button)); 63 | luax_setfield_number(L, "x", e.button.x); 64 | luax_setfield_number(L, "y", e.button.y); 65 | break; 66 | 67 | case SDL_MOUSEWHEEL: 68 | luax_setfield_string(L, "type", "wheel"); 69 | if(e.wheel.x == -1) 70 | luax_setfield_string(L, "x", "left"); 71 | else if(e.wheel.x == 1) 72 | luax_setfield_string(L, "x", "right"); 73 | else 74 | luax_setfield_string(L, "x", "none"); 75 | 76 | if (e.wheel.y == -1) 77 | luax_setfield_string(L, "y", "near"); 78 | else if (e.wheel.y == 1) 79 | luax_setfield_string(L, "y", "far"); 80 | else 81 | luax_setfield_string(L, "y", "none"); 82 | break; 83 | 84 | default: 85 | /* ignore other events */ 86 | luax_pop(L, 1); // pop table out 87 | goto poll; 88 | break; 89 | } 90 | luax_rawseti(L, -2, i++); 91 | } 92 | return 1; 93 | } 94 | 95 | static const luaL_Reg f[] = { 96 | {"poll", l_event_poll}, 97 | {0 ,0 } 98 | }; 99 | 100 | /** 101 | * load event module 102 | */ 103 | int luaopen_event(lua_State* L) 104 | { 105 | luax_newlib(L, f); 106 | return 1; 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /src/libs/lua51/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | ** Possible states of the Garbage Collector 16 | */ 17 | #define GCSpause 0 18 | #define GCSpropagate 1 19 | #define GCSsweepstring 2 20 | #define GCSsweep 3 21 | #define GCSfinalize 4 22 | 23 | 24 | /* 25 | ** some userful bit tricks 26 | */ 27 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 28 | #define setbits(x,m) ((x) |= (m)) 29 | #define testbits(x,m) ((x) & (m)) 30 | #define bitmask(b) (1<<(b)) 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 32 | #define l_setbit(x,b) setbits(x, bitmask(b)) 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) 34 | #define testbit(x,b) testbits(x, bitmask(b)) 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 36 | #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 37 | #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 38 | 39 | 40 | 41 | /* 42 | ** Layout for bit use in `marked' field: 43 | ** bit 0 - object is white (type 0) 44 | ** bit 1 - object is white (type 1) 45 | ** bit 2 - object is black 46 | ** bit 3 - for userdata: has been finalized 47 | ** bit 3 - for tables: has weak keys 48 | ** bit 4 - for tables: has weak values 49 | ** bit 5 - object is fixed (should not be collected) 50 | ** bit 6 - object is "super" fixed (only the main thread) 51 | */ 52 | 53 | 54 | #define WHITE0BIT 0 55 | #define WHITE1BIT 1 56 | #define BLACKBIT 2 57 | #define FINALIZEDBIT 3 58 | #define KEYWEAKBIT 3 59 | #define VALUEWEAKBIT 4 60 | #define FIXEDBIT 5 61 | #define SFIXEDBIT 6 62 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 63 | 64 | 65 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 66 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 67 | #define isgray(x) (!isblack(x) && !iswhite(x)) 68 | 69 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 70 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71 | 72 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 73 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 74 | 75 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 76 | 77 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 78 | 79 | 80 | #define luaC_checkGC(L) { \ 81 | condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 82 | if (G(L)->totalbytes >= G(L)->GCthreshold) \ 83 | luaC_step(L); } 84 | 85 | 86 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 87 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 88 | 89 | #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 90 | luaC_barrierback(L,t); } 91 | 92 | #define luaC_objbarrier(L,p,o) \ 93 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 94 | luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 95 | 96 | #define luaC_objbarriert(L,t,o) \ 97 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 98 | 99 | LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 100 | LUAI_FUNC void luaC_callGCTM (lua_State *L); 101 | LUAI_FUNC void luaC_freeall (lua_State *L); 102 | LUAI_FUNC void luaC_step (lua_State *L); 103 | LUAI_FUNC void luaC_fullgc (lua_State *L); 104 | LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 105 | LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 106 | LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 107 | LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 108 | 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /src/libs/lua51/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL || getstr(s)==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size,D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttype(o),D); 88 | switch (ttype(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: 102 | lua_assert(0); /* cannot happen */ 103 | break; 104 | } 105 | } 106 | n=f->sizep; 107 | DumpInt(n,D); 108 | for (i=0; ip[i],f->source,D); 109 | } 110 | 111 | static void DumpDebug(const Proto* f, DumpState* D) 112 | { 113 | int i,n; 114 | n= (D->strip) ? 0 : f->sizelineinfo; 115 | DumpVector(f->lineinfo,n,sizeof(int),D); 116 | n= (D->strip) ? 0 : f->sizelocvars; 117 | DumpInt(n,D); 118 | for (i=0; ilocvars[i].varname,D); 121 | DumpInt(f->locvars[i].startpc,D); 122 | DumpInt(f->locvars[i].endpc,D); 123 | } 124 | n= (D->strip) ? 0 : f->sizeupvalues; 125 | DumpInt(n,D); 126 | for (i=0; iupvalues[i],D); 127 | } 128 | 129 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 130 | { 131 | DumpString((f->source==p || D->strip) ? NULL : f->source,D); 132 | DumpInt(f->linedefined,D); 133 | DumpInt(f->lastlinedefined,D); 134 | DumpChar(f->nups,D); 135 | DumpChar(f->numparams,D); 136 | DumpChar(f->is_vararg,D); 137 | DumpChar(f->maxstacksize,D); 138 | DumpCode(f,D); 139 | DumpConstants(f,D); 140 | DumpDebug(f,D); 141 | } 142 | 143 | static void DumpHeader(DumpState* D) 144 | { 145 | char h[LUAC_HEADERSIZE]; 146 | luaU_header(h); 147 | DumpBlock(h,LUAC_HEADERSIZE,D); 148 | } 149 | 150 | /* 151 | ** dump Lua function as precompiled chunk 152 | */ 153 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 154 | { 155 | DumpState D; 156 | D.L=L; 157 | D.writer=w; 158 | D.data=data; 159 | D.strip=strip; 160 | D.status=0; 161 | DumpHeader(&D); 162 | DumpFunction(f,NULL,&D); 163 | return D.status; 164 | } 165 | -------------------------------------------------------------------------------- /src/utils/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef __JIN_MATRIX_H 2 | #define __JIN_MATRIX_H 3 | #include 4 | namespace jin 5 | { 6 | namespace util 7 | { 8 | 9 | struct vertex 10 | { 11 | unsigned char r, g, b, a; 12 | float x, y; 13 | float s, t; 14 | }; 15 | /** 16 | * This class is the basis for all transformations in LOVE. Althought not 17 | * really needed for 2D, it contains 4x4 elements to be compatible with 18 | * OpenGL without conversions. 19 | **/ 20 | class Matrix 21 | { 22 | private: 23 | 24 | /** 25 | * | e0 e4 e8 e12 | 26 | * | e1 e5 e9 e13 | 27 | * | e2 e6 e10 e14 | 28 | * | e3 e7 e11 e15 | 29 | **/ 30 | float e[16]; 31 | 32 | public: 33 | 34 | /** 35 | * Creates a new identity matrix. 36 | **/ 37 | Matrix(); 38 | 39 | /** 40 | * Destructor. 41 | **/ 42 | ~Matrix(); 43 | 44 | /** 45 | * Multiplies this Matrix with another Matrix, changing neither. 46 | * @param m The Matrix to multiply with this Matrix. 47 | * @return The combined matrix. 48 | **/ 49 | Matrix operator * (const Matrix & m) const; 50 | 51 | /** 52 | * Multiplies a Matrix into this Matrix. 53 | * @param m The Matrix to combine into this Matrix. 54 | **/ 55 | void operator *= (const Matrix & m); 56 | 57 | /** 58 | * Gets a pointer to the 16 array elements. 59 | * @return The array elements. 60 | **/ 61 | const float * getElements() const; 62 | 63 | /** 64 | * Resets this Matrix to the identity matrix. 65 | **/ 66 | void setIdentity(); 67 | 68 | /** 69 | * Resets this Matrix to a translation. 70 | * @param x Translation along x-axis. 71 | * @param y Translation along y-axis. 72 | **/ 73 | void setTranslation(float x, float y); 74 | 75 | /** 76 | * Resets this Matrix to a rotation. 77 | * @param r The angle in radians. 78 | **/ 79 | void setRotation(float r); 80 | 81 | /** 82 | * Resets this Matrix to a scale transformation. 83 | * @param sx Scale factor along the x-axis. 84 | * @param sy Scale factor along the y-axis. 85 | **/ 86 | void setScale(float sx, float sy); 87 | 88 | /** 89 | * Resets this Matrix to a shear transformation. 90 | * @param kx Shear along x-axis. 91 | * @param ky Shear along y-axis. 92 | **/ 93 | void setShear(float kx, float ky); 94 | 95 | /** 96 | * Creates a transformation with a certain position, orientation, scale 97 | * and offset. Perfect for Drawables -- what a coincidence! 98 | * 99 | * @param x The translation along the x-axis. 100 | * @param y The translation along the y-axis. 101 | * @param angle The rotation (rad) around the center with offset (ox,oy). 102 | * @param sx Scale along x-axis. 103 | * @param sy Scale along y-axis. 104 | * @param ox The offset for rotation along the x-axis. 105 | * @param oy The offset for rotation along the y-axis. 106 | * @param kx Shear along x-axis 107 | * @param ky Shear along y-axis 108 | **/ 109 | void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy); 110 | 111 | /** 112 | * Multiplies this Matrix with a translation. 113 | * @param x Translation along x-axis. 114 | * @param y Translation along y-axis. 115 | **/ 116 | void translate(float x, float y); 117 | 118 | /** 119 | * Multiplies this Matrix with a rotation. 120 | * @param r Angle in radians. 121 | **/ 122 | void rotate(float r); 123 | 124 | /** 125 | * Multiplies this Matrix with a scale transformation. 126 | * @param sx Scale factor along the x-axis. 127 | * @param sy Scale factor along the y-axis. 128 | **/ 129 | void scale(float sx, float sy); 130 | 131 | /** 132 | * Multiplies this Matrix with a shear transformation. 133 | * @param kx Shear along the x-axis. 134 | * @param ky Shear along the y-axis. 135 | **/ 136 | void shear(float kx, float ky); 137 | 138 | /** 139 | * Transforms an array of vertices by this Matrix. The sources and 140 | * destination arrays may be the same. 141 | * 142 | * @param dst Storage for the transformed vertices. 143 | * @param src The source vertices. 144 | * @param size The number of vertices. 145 | **/ 146 | void transform(vertex * dst, const vertex * src, int size) const; 147 | 148 | }; 149 | 150 | } 151 | } 152 | 153 | #endif -------------------------------------------------------------------------------- /src/libs/smount/smount.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "smount.h" 7 | #include "dirent.h" 8 | 9 | #define isseparator(c) (c == '/' || c == '\\') 10 | 11 | static sm_Path* sm_newpath(int type, const char* path, sm_Path* next) 12 | { 13 | sm_Path* temp = (sm_Path*)malloc(sizeof(sm_Path)); 14 | int len = strlen(path); 15 | temp->path = (char*)malloc(len + 1); 16 | memcpy(temp->path, path, len); 17 | temp->path[len] = '\0'; 18 | // trim trailing separator 19 | while (isseparator(temp->path[--len])) 20 | temp->path[len] = '\0'; 21 | temp->next = next; 22 | temp->type = type; 23 | return temp; 24 | } 25 | 26 | static const char* mkstr(const char* str) 27 | { 28 | int len = strlen(str); 29 | char* temp = (char*)malloc(strlen(str)); 30 | memcpy(temp, str, len); 31 | return temp; 32 | } 33 | 34 | /** 35 | * Create a new shared contex. 36 | */ 37 | sm_Shared* sm_newshared() 38 | { 39 | sm_Shared* shared = (sm_Shared*)malloc(sizeof(sm_Shared)); 40 | shared->mount = 0; 41 | return shared; 42 | } 43 | 44 | /** 45 | * Concatenate strings together. 46 | */ 47 | char *concat(const char *str, ...) { 48 | va_list args; 49 | const char *s; 50 | // Get len 51 | int len = strlen(str); 52 | va_start(args, str); 53 | while ((s = va_arg(args, char*))) { 54 | len += strlen(s); 55 | } 56 | va_end(args); 57 | // Build string 58 | char *res = (char*)malloc(len + 1); 59 | if (!res) return NULL; 60 | strcpy(res, str); 61 | va_start(args, str); 62 | while ((s = va_arg(args, char*))) { 63 | strcat(res, s); 64 | } 65 | va_end(args); 66 | return res; 67 | } 68 | 69 | static int isdir(const char *path) { 70 | struct stat s; 71 | int res = stat(path, &s); 72 | return S_ISDIR(s.st_mode); 73 | } 74 | 75 | int sm_mount(sm_Shared* S, const char *path) 76 | { 77 | if (!isdir(path)) 78 | { 79 | return SM_INVALIDMOUNT; 80 | } 81 | 82 | S->mount = sm_newpath(PATH_DIR, path, 0); 83 | 84 | return SM_SUCCESS; 85 | } 86 | 87 | void sm_unmount(sm_Shared* S) 88 | { 89 | sm_Path* mount = S->mount; 90 | while (mount) 91 | { 92 | free(mount->path); 93 | mount = mount->next; 94 | } 95 | } 96 | 97 | int sm_exists(sm_Shared* S, const char *path) 98 | { 99 | char* r = concat(S->mount->path, "/", path, 0); 100 | if (!r) return SM_NOSUCHDIR; 101 | struct stat s; 102 | int res = stat(r, &s); 103 | free(r); 104 | if (res == 0) 105 | return SM_SUCCESS; 106 | return SM_NOSUCHDIR; 107 | } 108 | 109 | static struct stat sm_getstat(sm_Shared* S, const char*path) 110 | { 111 | char* r = concat(S->mount->path, "/", path, 0); 112 | struct stat s = {}; 113 | if (!r) return s; 114 | int res = stat(r, &s); 115 | free(r); 116 | return s; 117 | } 118 | 119 | int sm_isdir(sm_Shared* S, const char *path) 120 | { 121 | struct stat s = sm_getstat(S, path); 122 | return S_ISDIR(s.st_mode); 123 | } 124 | 125 | int sm_isreg(sm_Shared* S, const char *path) 126 | { 127 | struct stat s = sm_getstat(S, path); 128 | return S_ISREG(s.st_mode); 129 | } 130 | 131 | const char* sm_errstr(int e) 132 | { 133 | switch (e) 134 | { 135 | case SM_INVALIDMOUNT: return "invalid mount directory"; 136 | default: return "unknown error"; 137 | } 138 | } 139 | 140 | void *sm_read(sm_Shared* S, const char *path, unsigned int *size) 141 | { 142 | if (!sm_isreg(S, path)) return 0; 143 | int fr = 0; 144 | if (size == 0) 145 | { 146 | fr = 1; 147 | size = (unsigned int*)malloc(sizeof(unsigned int)); 148 | } 149 | char *r = concat(S->mount->path, "/", path, NULL); 150 | if (!r) return NULL; 151 | FILE *fp = fopen(r, "rb"); 152 | free(r); 153 | if (!fp) return 0; 154 | /* Get file size */ 155 | fseek(fp, 0, SEEK_END); 156 | *size = ftell(fp); 157 | /* Load file */ 158 | fseek(fp, 0, SEEK_SET); 159 | char *res = (char*)malloc(*size + 1); 160 | if (!res) return NULL; 161 | res[*size] = '\0'; 162 | if (fread(res, 1, *size, fp) != *size) { 163 | free(res); 164 | fclose(fp); 165 | return NULL; 166 | } 167 | fclose(fp); 168 | if (fr) free(size); 169 | return res; 170 | } 171 | 172 | char* sm_fullpath(sm_Shared* S, const char* path) 173 | { 174 | return concat(S->mount->path, "/", path, 0); 175 | } 176 | 177 | int sm_size(sm_Shared* S, const char *path) 178 | { 179 | struct stat s = sm_getstat(S, path); 180 | return s.st_size; 181 | } 182 | 183 | void sm_delete(sm_Shared* S, const char *path) 184 | { 185 | char* name = sm_fullpath(S, path); 186 | remove(name); 187 | free(name); 188 | } 189 | 190 | int sm_write(sm_Shared* S, const char *path, const void *data, int size) 191 | { 192 | char* name = sm_fullpath(S, path); 193 | if (!name) return SM_NOSUCHDIR; 194 | FILE *fp = fopen(name, "wb"); 195 | free(name); 196 | if (!fp) return SM_UNABLEOPEN; 197 | int res = fwrite(data, size, 1, fp); 198 | fclose(fp); 199 | return (res == 1) ? SM_SUCCESS : SM_CANTWRITE; 200 | } -------------------------------------------------------------------------------- /src/libs/lua51/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lfunc_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lfunc.h" 16 | #include "lgc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 24 | Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 25 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 26 | c->c.isC = 1; 27 | c->c.env = e; 28 | c->c.nupvalues = cast_byte(nelems); 29 | return c; 30 | } 31 | 32 | 33 | Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 34 | Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 35 | luaC_link(L, obj2gco(c), LUA_TFUNCTION); 36 | c->l.isC = 0; 37 | c->l.env = e; 38 | c->l.nupvalues = cast_byte(nelems); 39 | while (nelems--) c->l.upvals[nelems] = NULL; 40 | return c; 41 | } 42 | 43 | 44 | UpVal *luaF_newupval (lua_State *L) { 45 | UpVal *uv = luaM_new(L, UpVal); 46 | luaC_link(L, obj2gco(uv), LUA_TUPVAL); 47 | uv->v = &uv->u.value; 48 | setnilvalue(uv->v); 49 | return uv; 50 | } 51 | 52 | 53 | UpVal *luaF_findupval (lua_State *L, StkId level) { 54 | global_State *g = G(L); 55 | GCObject **pp = &L->openupval; 56 | UpVal *p; 57 | UpVal *uv; 58 | while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { 59 | lua_assert(p->v != &p->u.value); 60 | if (p->v == level) { /* found a corresponding upvalue? */ 61 | if (isdead(g, obj2gco(p))) /* is it dead? */ 62 | changewhite(obj2gco(p)); /* ressurect it */ 63 | return p; 64 | } 65 | pp = &p->next; 66 | } 67 | uv = luaM_new(L, UpVal); /* not found: create a new one */ 68 | uv->tt = LUA_TUPVAL; 69 | uv->marked = luaC_white(g); 70 | uv->v = level; /* current value lives in the stack */ 71 | uv->next = *pp; /* chain it in the proper position */ 72 | *pp = obj2gco(uv); 73 | uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 74 | uv->u.l.next = g->uvhead.u.l.next; 75 | uv->u.l.next->u.l.prev = uv; 76 | g->uvhead.u.l.next = uv; 77 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 78 | return uv; 79 | } 80 | 81 | 82 | static void unlinkupval (UpVal *uv) { 83 | lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); 84 | uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ 85 | uv->u.l.prev->u.l.next = uv->u.l.next; 86 | } 87 | 88 | 89 | void luaF_freeupval (lua_State *L, UpVal *uv) { 90 | if (uv->v != &uv->u.value) /* is it open? */ 91 | unlinkupval(uv); /* remove from open list */ 92 | luaM_free(L, uv); /* free upvalue */ 93 | } 94 | 95 | 96 | void luaF_close (lua_State *L, StkId level) { 97 | UpVal *uv; 98 | global_State *g = G(L); 99 | while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { 100 | GCObject *o = obj2gco(uv); 101 | lua_assert(!isblack(o) && uv->v != &uv->u.value); 102 | L->openupval = uv->next; /* remove from `open' list */ 103 | if (isdead(g, o)) 104 | luaF_freeupval(L, uv); /* free upvalue */ 105 | else { 106 | unlinkupval(uv); 107 | setobj(L, &uv->u.value, uv->v); 108 | uv->v = &uv->u.value; /* now current value lives here */ 109 | luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ 110 | } 111 | } 112 | } 113 | 114 | 115 | Proto *luaF_newproto (lua_State *L) { 116 | Proto *f = luaM_new(L, Proto); 117 | luaC_link(L, obj2gco(f), LUA_TPROTO); 118 | f->k = NULL; 119 | f->sizek = 0; 120 | f->p = NULL; 121 | f->sizep = 0; 122 | f->code = NULL; 123 | f->sizecode = 0; 124 | f->sizelineinfo = 0; 125 | f->sizeupvalues = 0; 126 | f->nups = 0; 127 | f->upvalues = NULL; 128 | f->numparams = 0; 129 | f->is_vararg = 0; 130 | f->maxstacksize = 0; 131 | f->lineinfo = NULL; 132 | f->sizelocvars = 0; 133 | f->locvars = NULL; 134 | f->linedefined = 0; 135 | f->lastlinedefined = 0; 136 | f->source = NULL; 137 | return f; 138 | } 139 | 140 | 141 | void luaF_freeproto (lua_State *L, Proto *f) { 142 | luaM_freearray(L, f->code, f->sizecode, Instruction); 143 | luaM_freearray(L, f->p, f->sizep, Proto *); 144 | luaM_freearray(L, f->k, f->sizek, TValue); 145 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); 146 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); 147 | luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); 148 | luaM_free(L, f); 149 | } 150 | 151 | 152 | void luaF_freeclosure (lua_State *L, Closure *c) { 153 | int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : 154 | sizeLclosure(c->l.nupvalues); 155 | luaM_freemem(L, c, size); 156 | } 157 | 158 | 159 | /* 160 | ** Look for n-th local variable at line `line' in function `func'. 161 | ** Returns NULL if not found. 162 | */ 163 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 164 | int i; 165 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 166 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 167 | local_number--; 168 | if (local_number == 0) 169 | return getstr(f->locvars[i].varname); 170 | } 171 | } 172 | return NULL; /* not found */ 173 | } 174 | 175 | -------------------------------------------------------------------------------- /src/render/font.cpp: -------------------------------------------------------------------------------- 1 | #include "font.h" 2 | 3 | #include 4 | 5 | #define STB_TRUETYPE_IMPLEMENTATION 6 | #include "libs/stb/stb_truetype.h" 7 | #include "color.h" 8 | 9 | namespace jin 10 | { 11 | namespace render 12 | { 13 | 14 | #define BITMAP_WIDTH 512 15 | #define BITMAP_HEIGHT 512 16 | #define PIXEL_HEIGHT 32 17 | 18 | Font::Font():Drawable() 19 | { 20 | } 21 | 22 | // ttf file read buffer 23 | static unsigned char ttf_buffer[1 << 20]; 24 | 25 | // bitmap for saving font data 26 | static unsigned char temp_bitmap[BITMAP_WIDTH * BITMAP_HEIGHT]; 27 | 28 | void Font::loadf(const char* path) 29 | { 30 | fread(ttf_buffer, 1, 1 << 20, fopen(path, "rb")); 31 | 32 | loadb(ttf_buffer); 33 | } 34 | 35 | /** 36 | * load from memory 37 | */ 38 | void Font::loadb(const unsigned char* data) 39 | { 40 | stbtt_BakeFontBitmap(data, 0, PIXEL_HEIGHT, temp_bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, 32, 96, cdata); 41 | 42 | glGenTextures(1, &texture); 43 | glBindTexture(GL_TEXTURE_2D, texture); 44 | 45 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, BITMAP_WIDTH, 46 | BITMAP_HEIGHT, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap); 47 | 48 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 49 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 50 | 51 | glBindTexture(GL_TEXTURE_2D, 0); 52 | } 53 | 54 | /** 55 | * get texture quad 56 | */ 57 | static Quad getCharQuad(const stbtt_bakedchar* chardata, int pw, int ph, int char_index) 58 | { 59 | float ipw = 1.0f / pw, iph = 1.0f / ph; 60 | const stbtt_bakedchar *b = chardata + char_index; 61 | Quad q; 62 | q.x = b->x0 * ipw; 63 | q.y = b->y0 * iph; 64 | q.w = b->x1 * ipw - b->x0 * ipw; 65 | q.h = b->y1 * iph - b->y0 * iph; 66 | return q; 67 | } 68 | 69 | /** 70 | * render function draw mutiple times 71 | * in loop 72 | */ 73 | void Font::render( 74 | const char* text, // rendered text 75 | float x, float y, // render position 76 | int fheight, // font height 77 | int spacing, // font spacing 78 | int lheight) // line height 79 | { 80 | float _x = x, 81 | _y = y; 82 | 83 | int len = strlen(text); 84 | 85 | glEnable(GL_TEXTURE_2D); 86 | glBindTexture(GL_TEXTURE_2D, texture); 87 | 88 | glEnableClientState(GL_VERTEX_ARRAY); 89 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); 90 | 91 | // for saving clip quad 92 | stbtt_aligned_quad q; 93 | 94 | // render every given character 95 | int xc = x; 96 | int yc = y; 97 | 98 | float factor = fheight / (float)PIXEL_HEIGHT; 99 | 100 | for (int i = 0; i < len; i++) 101 | { 102 | char c = text[i]; 103 | if (c == '\n') 104 | { 105 | xc = x; 106 | yc += lheight; 107 | continue; 108 | } 109 | 110 | // only support ASCII 111 | Quad q = getCharQuad(cdata, 512, 512, c - 32); 112 | float s0 = q.x, 113 | s1 = q.x + q.w, 114 | t0 = q.y, 115 | t1 = q.y + q.h; 116 | 117 | // texture quad 118 | float tc[] = { 119 | s0, t1, 120 | s1, t1, 121 | s1, t0, 122 | s0, t0 123 | }; 124 | 125 | // character bound box 126 | stbtt_bakedchar box = cdata[c - 32]; 127 | 128 | float width = factor * (box.x1 - box.x0); 129 | float height = factor * (box.y1 - box.y0); 130 | float xoffset = factor * box.xoff; 131 | // I don't know why add PIXEL_HEIGHT to box.yoff, but 132 | // it works. 133 | float yoffset = factor * (box.yoff + PIXEL_HEIGHT); 134 | float xadvance = factor * box.xadvance; 135 | 136 | // render quad 137 | float vc[] = { 138 | xc + xoffset, yc + height + yoffset, 139 | xc + width + xoffset, yc + height + yoffset, 140 | xc + width + xoffset, yc + yoffset, 141 | xc + xoffset, yc + yoffset 142 | }; 143 | 144 | // forward to next character 145 | xc += xadvance + spacing; 146 | 147 | glTexCoordPointer(2, GL_FLOAT, 0, tc); 148 | glVertexPointer(2, GL_FLOAT, 0, vc); 149 | glDrawArrays(GL_QUADS, 0, 4); 150 | } 151 | 152 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); 153 | glDisableClientState(GL_VERTEX_ARRAY); 154 | 155 | glBindTexture(GL_TEXTURE_2D, 0); 156 | glDisable(GL_TEXTURE_2D); 157 | } 158 | 159 | void Font::box(const char* str, int fheight, int spacing, int lheight, int* w, int * h) 160 | { 161 | int len = strlen(str); 162 | 163 | float xc = 0; 164 | int yc = len ? lheight: 0; 165 | int maxX = 0; 166 | 167 | float factor = fheight / (float)PIXEL_HEIGHT; 168 | 169 | for (int i = 0; i < len; i++) 170 | { 171 | char c = str[i]; 172 | if (c == '\n') 173 | { 174 | yc += lheight; 175 | xc = 0; 176 | continue; 177 | } 178 | stbtt_bakedchar box = cdata[c - 32]; 179 | 180 | xc += factor * box.xadvance + spacing; 181 | if (xc > maxX) maxX = xc; 182 | } 183 | 184 | *w = maxX; 185 | *h = yc; 186 | } 187 | 188 | } 189 | } -------------------------------------------------------------------------------- /src/utils/matrix.cpp: -------------------------------------------------------------------------------- 1 | #include "Matrix.h" 2 | 3 | #include // memcpy 4 | #include 5 | 6 | namespace jin 7 | { 8 | namespace util 9 | { 10 | 11 | // | e0 e4 e8 e12 | 12 | // | e1 e5 e9 e13 | 13 | // | e2 e6 e10 e14 | 14 | // | e3 e7 e11 e15 | 15 | 16 | Matrix::Matrix() 17 | { 18 | setIdentity(); 19 | } 20 | 21 | Matrix::~Matrix() 22 | { 23 | } 24 | 25 | // | e0 e4 e8 e12 | 26 | // | e1 e5 e9 e13 | 27 | // | e2 e6 e10 e14 | 28 | // | e3 e7 e11 e15 | 29 | // | e0 e4 e8 e12 | 30 | // | e1 e5 e9 e13 | 31 | // | e2 e6 e10 e14 | 32 | // | e3 e7 e11 e15 | 33 | 34 | Matrix Matrix::operator * (const Matrix & m) const 35 | { 36 | Matrix t; 37 | 38 | t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]); 39 | t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]); 40 | t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]); 41 | t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]); 42 | 43 | t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]); 44 | t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]); 45 | t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]); 46 | t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]); 47 | 48 | t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]); 49 | t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]); 50 | t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]); 51 | t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]); 52 | 53 | t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]); 54 | t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]); 55 | t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]); 56 | t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]); 57 | 58 | return t; 59 | } 60 | 61 | void Matrix::operator *= (const Matrix & m) 62 | { 63 | Matrix t = (*this) * m; 64 | memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16); 65 | } 66 | 67 | const float * Matrix::getElements() const 68 | { 69 | return e; 70 | } 71 | 72 | void Matrix::setIdentity() 73 | { 74 | memset(e, 0, sizeof(float) * 16); 75 | e[0] = e[5] = e[10] = e[15] = 1; 76 | } 77 | 78 | void Matrix::setTranslation(float x, float y) 79 | { 80 | setIdentity(); 81 | e[12] = x; 82 | e[13] = y; 83 | } 84 | 85 | void Matrix::setRotation(float rad) 86 | { 87 | setIdentity(); 88 | float c = cos(rad), s = sin(rad); 89 | e[0] = c; e[4] = -s; 90 | e[1] = s; e[5] = c; 91 | } 92 | 93 | void Matrix::setScale(float sx, float sy) 94 | { 95 | setIdentity(); 96 | e[0] = sx; 97 | e[5] = sy; 98 | } 99 | 100 | void Matrix::setShear(float kx, float ky) 101 | { 102 | setIdentity(); 103 | e[1] = ky; 104 | e[4] = kx; 105 | } 106 | 107 | void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy) 108 | { 109 | memset(e, 0, sizeof(float) * 16); // zero out matrix 110 | float c = cos(angle), s = sin(angle); 111 | // matrix multiplication carried out on paper: 112 | // |1 x| |c -s | |sx | |1 -ox| 113 | // | 1 y| |s c | | sy | | 1 -oy| 114 | // | 1 | | 1 | | 1 | | 1 | 115 | // | 1| | 1| | 1| | 1 | 116 | // move rotate scale origin 117 | e[10] = e[15] = 1.0f; 118 | e[0] = c * sx ; // = a 119 | e[1] = s * sx ; // = b 120 | e[4] = - s * sy; // = c 121 | e[5] = c * sy; // = d 122 | e[12] = x - ox * e[0] - oy * e[4]; 123 | e[13] = y - ox * e[1] - oy * e[5]; 124 | } 125 | 126 | void Matrix::translate(float x, float y) 127 | { 128 | Matrix t; 129 | t.setTranslation(x, y); 130 | this->operator *=(t); 131 | } 132 | 133 | void Matrix::rotate(float rad) 134 | { 135 | Matrix t; 136 | t.setRotation(rad); 137 | this->operator *=(t); 138 | } 139 | 140 | void Matrix::scale(float sx, float sy) 141 | { 142 | Matrix t; 143 | t.setScale(sx, sy); 144 | this->operator *=(t); 145 | } 146 | 147 | void Matrix::shear(float kx, float ky) 148 | { 149 | Matrix t; 150 | t.setShear(kx, ky); 151 | this->operator *=(t); 152 | } 153 | 154 | // | x | 155 | // | y | 156 | // | 0 | 157 | // | 1 | 158 | // | e0 e4 e8 e12 | 159 | // | e1 e5 e9 e13 | 160 | // | e2 e6 e10 e14 | 161 | // | e3 e7 e11 e15 | 162 | 163 | void Matrix::transform(vertex * dst, const vertex * src, int size) const 164 | { 165 | for (int i = 0; i 8 | 9 | #define lundump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "ldebug.h" 15 | #include "ldo.h" 16 | #include "lfunc.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstring.h" 20 | #include "lundump.h" 21 | #include "lzio.h" 22 | 23 | typedef struct { 24 | lua_State* L; 25 | ZIO* Z; 26 | Mbuffer* b; 27 | const char* name; 28 | } LoadState; 29 | 30 | #ifdef LUAC_TRUST_BINARIES 31 | #define IF(c,s) 32 | #define error(S,s) 33 | #else 34 | #define IF(c,s) if (c) error(S,s) 35 | 36 | static void error(LoadState* S, const char* why) 37 | { 38 | luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 39 | luaD_throw(S->L,LUA_ERRSYNTAX); 40 | } 41 | #endif 42 | 43 | #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 44 | #define LoadByte(S) (lu_byte)LoadChar(S) 45 | #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 46 | #define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 47 | 48 | static void LoadBlock(LoadState* S, void* b, size_t size) 49 | { 50 | size_t r=luaZ_read(S->Z,b,size); 51 | IF (r!=0, "unexpected end"); 52 | } 53 | 54 | static int LoadChar(LoadState* S) 55 | { 56 | char x; 57 | LoadVar(S,x); 58 | return x; 59 | } 60 | 61 | static int LoadInt(LoadState* S) 62 | { 63 | int x; 64 | LoadVar(S,x); 65 | IF (x<0, "bad integer"); 66 | return x; 67 | } 68 | 69 | static lua_Number LoadNumber(LoadState* S) 70 | { 71 | lua_Number x; 72 | LoadVar(S,x); 73 | return x; 74 | } 75 | 76 | static TString* LoadString(LoadState* S) 77 | { 78 | size_t size; 79 | LoadVar(S,size); 80 | if (size==0) 81 | return NULL; 82 | else 83 | { 84 | char* s=luaZ_openspace(S->L,S->b,size); 85 | LoadBlock(S,s,size); 86 | return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 87 | } 88 | } 89 | 90 | static void LoadCode(LoadState* S, Proto* f) 91 | { 92 | int n=LoadInt(S); 93 | f->code=luaM_newvector(S->L,n,Instruction); 94 | f->sizecode=n; 95 | LoadVector(S,f->code,n,sizeof(Instruction)); 96 | } 97 | 98 | static Proto* LoadFunction(LoadState* S, TString* p); 99 | 100 | static void LoadConstants(LoadState* S, Proto* f) 101 | { 102 | int i,n; 103 | n=LoadInt(S); 104 | f->k=luaM_newvector(S->L,n,TValue); 105 | f->sizek=n; 106 | for (i=0; ik[i]); 107 | for (i=0; ik[i]; 110 | int t=LoadChar(S); 111 | switch (t) 112 | { 113 | case LUA_TNIL: 114 | setnilvalue(o); 115 | break; 116 | case LUA_TBOOLEAN: 117 | setbvalue(o,LoadChar(S)!=0); 118 | break; 119 | case LUA_TNUMBER: 120 | setnvalue(o,LoadNumber(S)); 121 | break; 122 | case LUA_TSTRING: 123 | setsvalue2n(S->L,o,LoadString(S)); 124 | break; 125 | default: 126 | error(S,"bad constant"); 127 | break; 128 | } 129 | } 130 | n=LoadInt(S); 131 | f->p=luaM_newvector(S->L,n,Proto*); 132 | f->sizep=n; 133 | for (i=0; ip[i]=NULL; 134 | for (i=0; ip[i]=LoadFunction(S,f->source); 135 | } 136 | 137 | static void LoadDebug(LoadState* S, Proto* f) 138 | { 139 | int i,n; 140 | n=LoadInt(S); 141 | f->lineinfo=luaM_newvector(S->L,n,int); 142 | f->sizelineinfo=n; 143 | LoadVector(S,f->lineinfo,n,sizeof(int)); 144 | n=LoadInt(S); 145 | f->locvars=luaM_newvector(S->L,n,LocVar); 146 | f->sizelocvars=n; 147 | for (i=0; ilocvars[i].varname=NULL; 148 | for (i=0; ilocvars[i].varname=LoadString(S); 151 | f->locvars[i].startpc=LoadInt(S); 152 | f->locvars[i].endpc=LoadInt(S); 153 | } 154 | n=LoadInt(S); 155 | f->upvalues=luaM_newvector(S->L,n,TString*); 156 | f->sizeupvalues=n; 157 | for (i=0; iupvalues[i]=NULL; 158 | for (i=0; iupvalues[i]=LoadString(S); 159 | } 160 | 161 | static Proto* LoadFunction(LoadState* S, TString* p) 162 | { 163 | Proto* f; 164 | if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); 165 | f=luaF_newproto(S->L); 166 | setptvalue2s(S->L,S->L->top,f); incr_top(S->L); 167 | f->source=LoadString(S); if (f->source==NULL) f->source=p; 168 | f->linedefined=LoadInt(S); 169 | f->lastlinedefined=LoadInt(S); 170 | f->nups=LoadByte(S); 171 | f->numparams=LoadByte(S); 172 | f->is_vararg=LoadByte(S); 173 | f->maxstacksize=LoadByte(S); 174 | LoadCode(S,f); 175 | LoadConstants(S,f); 176 | LoadDebug(S,f); 177 | IF (!luaG_checkcode(f), "bad code"); 178 | S->L->top--; 179 | S->L->nCcalls--; 180 | return f; 181 | } 182 | 183 | static void LoadHeader(LoadState* S) 184 | { 185 | char h[LUAC_HEADERSIZE]; 186 | char s[LUAC_HEADERSIZE]; 187 | luaU_header(h); 188 | LoadBlock(S,s,LUAC_HEADERSIZE); 189 | IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 190 | } 191 | 192 | /* 193 | ** load precompiled chunk 194 | */ 195 | Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 196 | { 197 | LoadState S; 198 | if (*name=='@' || *name=='=') 199 | S.name=name+1; 200 | else if (*name==LUA_SIGNATURE[0]) 201 | S.name="binary string"; 202 | else 203 | S.name=name; 204 | S.L=L; 205 | S.Z=Z; 206 | S.b=buff; 207 | LoadHeader(&S); 208 | return LoadFunction(&S,luaS_newliteral(L,"=?")); 209 | } 210 | 211 | /* 212 | * make header 213 | */ 214 | void luaU_header (char* h) 215 | { 216 | int x=1; 217 | memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 218 | h+=sizeof(LUA_SIGNATURE)-1; 219 | *h++=(char)LUAC_VERSION; 220 | *h++=(char)LUAC_FORMAT; 221 | *h++=(char)*(char*)&x; /* endianness */ 222 | *h++=(char)sizeof(int); 223 | *h++=(char)sizeof(size_t); 224 | *h++=(char)sizeof(Instruction); 225 | *h++=(char)sizeof(lua_Number); 226 | *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 227 | } 228 | -------------------------------------------------------------------------------- /src/libs/lua51/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | 18 | struct lua_longjmp; /* defined in ldo.c */ 19 | 20 | 21 | /* table of globals */ 22 | #define gt(L) (&L->l_gt) 23 | 24 | /* registry */ 25 | #define registry(L) (&G(L)->l_registry) 26 | 27 | 28 | /* extra stack space to handle TM calls and some other extras */ 29 | #define EXTRA_STACK 5 30 | 31 | 32 | #define BASIC_CI_SIZE 8 33 | 34 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 35 | 36 | 37 | 38 | typedef struct stringtable { 39 | GCObject **hash; 40 | lu_int32 nuse; /* number of elements */ 41 | int size; 42 | } stringtable; 43 | 44 | 45 | /* 46 | ** informations about a call 47 | */ 48 | typedef struct CallInfo { 49 | StkId base; /* base for this function */ 50 | StkId func; /* function index in the stack */ 51 | StkId top; /* top for this function */ 52 | const Instruction *savedpc; 53 | int nresults; /* expected number of results from this function */ 54 | int tailcalls; /* number of tail calls lost under this entry */ 55 | } CallInfo; 56 | 57 | 58 | 59 | #define curr_func(L) (clvalue(L->ci->func)) 60 | #define ci_func(ci) (clvalue((ci)->func)) 61 | #define f_isLua(ci) (!ci_func(ci)->c.isC) 62 | #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 63 | 64 | 65 | /* 66 | ** `global state', shared by all threads of this state 67 | */ 68 | typedef struct global_State { 69 | stringtable strt; /* hash table for strings */ 70 | lua_Alloc frealloc; /* function to reallocate memory */ 71 | void *ud; /* auxiliary data to `frealloc' */ 72 | lu_byte currentwhite; 73 | lu_byte gcstate; /* state of garbage collector */ 74 | int sweepstrgc; /* position of sweep in `strt' */ 75 | GCObject *rootgc; /* list of all collectable objects */ 76 | GCObject **sweepgc; /* position of sweep in `rootgc' */ 77 | GCObject *gray; /* list of gray objects */ 78 | GCObject *grayagain; /* list of objects to be traversed atomically */ 79 | GCObject *weak; /* list of weak tables (to be cleared) */ 80 | GCObject *tmudata; /* last element of list of userdata to be GC */ 81 | Mbuffer buff; /* temporary buffer for string concatentation */ 82 | lu_mem GCthreshold; 83 | lu_mem totalbytes; /* number of bytes currently allocated */ 84 | lu_mem estimate; /* an estimate of number of bytes actually in use */ 85 | lu_mem gcdept; /* how much GC is `behind schedule' */ 86 | int gcpause; /* size of pause between successive GCs */ 87 | int gcstepmul; /* GC `granularity' */ 88 | lua_CFunction panic; /* to be called in unprotected errors */ 89 | TValue l_registry; 90 | struct lua_State *mainthread; 91 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 92 | struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 93 | TString *tmname[TM_N]; /* array with tag-method names */ 94 | } global_State; 95 | 96 | 97 | /* 98 | ** `per thread' state 99 | */ 100 | struct lua_State { 101 | CommonHeader; 102 | lu_byte status; 103 | StkId top; /* first free slot in the stack */ 104 | StkId base; /* base of current function */ 105 | global_State *l_G; 106 | CallInfo *ci; /* call info for current function */ 107 | const Instruction *savedpc; /* `savedpc' of current function */ 108 | StkId stack_last; /* last free slot in the stack */ 109 | StkId stack; /* stack base */ 110 | CallInfo *end_ci; /* points after end of ci array*/ 111 | CallInfo *base_ci; /* array of CallInfo's */ 112 | int stacksize; 113 | int size_ci; /* size of array `base_ci' */ 114 | unsigned short nCcalls; /* number of nested C calls */ 115 | unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 116 | lu_byte hookmask; 117 | lu_byte allowhook; 118 | int basehookcount; 119 | int hookcount; 120 | lua_Hook hook; 121 | TValue l_gt; /* table of globals */ 122 | TValue env; /* temporary place for environments */ 123 | GCObject *openupval; /* list of open upvalues in this stack */ 124 | GCObject *gclist; 125 | struct lua_longjmp *errorJmp; /* current error recover point */ 126 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 127 | }; 128 | 129 | 130 | #define G(L) (L->l_G) 131 | 132 | 133 | /* 134 | ** Union of all collectable objects 135 | */ 136 | union GCObject { 137 | GCheader gch; 138 | union TString ts; 139 | union Udata u; 140 | union Closure cl; 141 | struct Table h; 142 | struct Proto p; 143 | struct UpVal uv; 144 | struct lua_State th; /* thread */ 145 | }; 146 | 147 | 148 | /* macros to convert a GCObject into a specific value */ 149 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 150 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 151 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 152 | #define gco2u(o) (&rawgco2u(o)->uv) 153 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 154 | #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 155 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 156 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 157 | #define ngcotouv(o) \ 158 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 159 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 160 | 161 | /* macro to convert any Lua object into a GCObject */ 162 | #define obj2gco(v) (cast(GCObject *, (v))) 163 | 164 | 165 | LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 166 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 167 | 168 | #endif 169 | 170 | -------------------------------------------------------------------------------- /src/libs/lua51/print.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ 3 | ** print bytecodes 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #define luac_c 11 | #define LUA_CORE 12 | 13 | #include "ldebug.h" 14 | #include "lobject.h" 15 | #include "lopcodes.h" 16 | #include "lundump.h" 17 | 18 | #define PrintFunction luaU_print 19 | 20 | #define Sizeof(x) ((int)sizeof(x)) 21 | #define VOID(p) ((const void*)(p)) 22 | 23 | static void PrintString(const TString* ts) 24 | { 25 | const char* s=getstr(ts); 26 | size_t i,n=ts->tsv.len; 27 | putchar('"'); 28 | for (i=0; ik[i]; 54 | switch (ttype(o)) 55 | { 56 | case LUA_TNIL: 57 | printf("nil"); 58 | break; 59 | case LUA_TBOOLEAN: 60 | printf(bvalue(o) ? "true" : "false"); 61 | break; 62 | case LUA_TNUMBER: 63 | printf(LUA_NUMBER_FMT,nvalue(o)); 64 | break; 65 | case LUA_TSTRING: 66 | PrintString(rawtsvalue(o)); 67 | break; 68 | default: /* cannot happen */ 69 | printf("? type=%d",ttype(o)); 70 | break; 71 | } 72 | } 73 | 74 | static void PrintCode(const Proto* f) 75 | { 76 | const Instruction* code=f->code; 77 | int pc,n=f->sizecode; 78 | for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); 90 | printf("%-9s\t",luaP_opnames[o]); 91 | switch (getOpMode(o)) 92 | { 93 | case iABC: 94 | printf("%d",a); 95 | if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); 96 | if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); 97 | break; 98 | case iABx: 99 | if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); 100 | break; 101 | case iAsBx: 102 | if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); 103 | break; 104 | } 105 | switch (o) 106 | { 107 | case OP_LOADK: 108 | printf("\t; "); PrintConstant(f,bx); 109 | break; 110 | case OP_GETUPVAL: 111 | case OP_SETUPVAL: 112 | printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); 113 | break; 114 | case OP_GETGLOBAL: 115 | case OP_SETGLOBAL: 116 | printf("\t; %s",svalue(&f->k[bx])); 117 | break; 118 | case OP_GETTABLE: 119 | case OP_SELF: 120 | if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } 121 | break; 122 | case OP_SETTABLE: 123 | case OP_ADD: 124 | case OP_SUB: 125 | case OP_MUL: 126 | case OP_DIV: 127 | case OP_POW: 128 | case OP_EQ: 129 | case OP_LT: 130 | case OP_LE: 131 | if (ISK(b) || ISK(c)) 132 | { 133 | printf("\t; "); 134 | if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); 135 | printf(" "); 136 | if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); 137 | } 138 | break; 139 | case OP_JMP: 140 | case OP_FORLOOP: 141 | case OP_FORPREP: 142 | printf("\t; to %d",sbx+pc+2); 143 | break; 144 | case OP_CLOSURE: 145 | printf("\t; %p",VOID(f->p[bx])); 146 | break; 147 | case OP_SETLIST: 148 | if (c==0) printf("\t; %d",(int)code[++pc]); 149 | else printf("\t; %d",c); 150 | break; 151 | default: 152 | break; 153 | } 154 | printf("\n"); 155 | } 156 | } 157 | 158 | #define SS(x) (x==1)?"":"s" 159 | #define S(x) x,SS(x) 160 | 161 | static void PrintHeader(const Proto* f) 162 | { 163 | const char* s=getstr(f->source); 164 | if (*s=='@' || *s=='=') 165 | s++; 166 | else if (*s==LUA_SIGNATURE[0]) 167 | s="(bstring)"; 168 | else 169 | s="(string)"; 170 | printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", 171 | (f->linedefined==0)?"main":"function",s, 172 | f->linedefined,f->lastlinedefined, 173 | S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); 174 | printf("%d%s param%s, %d slot%s, %d upvalue%s, ", 175 | f->numparams,f->is_vararg?"+":"",SS(f->numparams), 176 | S(f->maxstacksize),S(f->nups)); 177 | printf("%d local%s, %d constant%s, %d function%s\n", 178 | S(f->sizelocvars),S(f->sizek),S(f->sizep)); 179 | } 180 | 181 | static void PrintConstants(const Proto* f) 182 | { 183 | int i,n=f->sizek; 184 | printf("constants (%d) for %p:\n",n,VOID(f)); 185 | for (i=0; isizelocvars; 196 | printf("locals (%d) for %p:\n",n,VOID(f)); 197 | for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); 201 | } 202 | } 203 | 204 | static void PrintUpvalues(const Proto* f) 205 | { 206 | int i,n=f->sizeupvalues; 207 | printf("upvalues (%d) for %p:\n",n,VOID(f)); 208 | if (f->upvalues==NULL) return; 209 | for (i=0; iupvalues[i])); 212 | } 213 | } 214 | 215 | void PrintFunction(const Proto* f, int full) 216 | { 217 | int i,n=f->sizep; 218 | PrintHeader(f); 219 | PrintCode(f); 220 | if (full) 221 | { 222 | PrintConstants(f); 223 | PrintLocals(f); 224 | PrintUpvalues(f); 225 | } 226 | for (i=0; ip[i],full); 227 | } 228 | -------------------------------------------------------------------------------- /src/libs/lua51/lobject.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Some generic functions over Lua objects 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define lobject_c 14 | #define LUA_CORE 15 | 16 | #include "lua.h" 17 | 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "lvm.h" 24 | 25 | 26 | 27 | const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; 28 | 29 | 30 | /* 31 | ** converts an integer to a "floating point byte", represented as 32 | ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 33 | ** eeeee != 0 and (xxx) otherwise. 34 | */ 35 | int luaO_int2fb (unsigned int x) { 36 | int e = 0; /* expoent */ 37 | while (x >= 16) { 38 | x = (x+1) >> 1; 39 | e++; 40 | } 41 | if (x < 8) return x; 42 | else return ((e+1) << 3) | (cast_int(x) - 8); 43 | } 44 | 45 | 46 | /* converts back */ 47 | int luaO_fb2int (int x) { 48 | int e = (x >> 3) & 31; 49 | if (e == 0) return x; 50 | else return ((x & 7)+8) << (e - 1); 51 | } 52 | 53 | 54 | int luaO_log2 (unsigned int x) { 55 | static const lu_byte log_2[256] = { 56 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 57 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 58 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 59 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 60 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 61 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 62 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 63 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 64 | }; 65 | int l = -1; 66 | while (x >= 256) { l += 8; x >>= 8; } 67 | return l + log_2[x]; 68 | 69 | } 70 | 71 | 72 | int luaO_rawequalObj (const TValue *t1, const TValue *t2) { 73 | if (ttype(t1) != ttype(t2)) return 0; 74 | else switch (ttype(t1)) { 75 | case LUA_TNIL: 76 | return 1; 77 | case LUA_TNUMBER: 78 | return luai_numeq(nvalue(t1), nvalue(t2)); 79 | case LUA_TBOOLEAN: 80 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ 81 | case LUA_TLIGHTUSERDATA: 82 | return pvalue(t1) == pvalue(t2); 83 | default: 84 | lua_assert(iscollectable(t1)); 85 | return gcvalue(t1) == gcvalue(t2); 86 | } 87 | } 88 | 89 | 90 | int luaO_str2d (const char *s, lua_Number *result) { 91 | char *endptr; 92 | *result = lua_str2number(s, &endptr); 93 | if (endptr == s) return 0; /* conversion failed */ 94 | if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ 95 | *result = cast_num(strtoul(s, &endptr, 16)); 96 | if (*endptr == '\0') return 1; /* most common case */ 97 | while (isspace(cast(unsigned char, *endptr))) endptr++; 98 | if (*endptr != '\0') return 0; /* invalid trailing characters? */ 99 | return 1; 100 | } 101 | 102 | 103 | 104 | static void pushstr (lua_State *L, const char *str) { 105 | setsvalue2s(L, L->top, luaS_new(L, str)); 106 | incr_top(L); 107 | } 108 | 109 | 110 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ 111 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 112 | int n = 1; 113 | pushstr(L, ""); 114 | for (;;) { 115 | const char *e = strchr(fmt, '%'); 116 | if (e == NULL) break; 117 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); 118 | incr_top(L); 119 | switch (*(e+1)) { 120 | case 's': { 121 | const char *s = va_arg(argp, char *); 122 | if (s == NULL) s = "(null)"; 123 | pushstr(L, s); 124 | break; 125 | } 126 | case 'c': { 127 | char buff[2]; 128 | buff[0] = cast(char, va_arg(argp, int)); 129 | buff[1] = '\0'; 130 | pushstr(L, buff); 131 | break; 132 | } 133 | case 'd': { 134 | setnvalue(L->top, cast_num(va_arg(argp, int))); 135 | incr_top(L); 136 | break; 137 | } 138 | case 'f': { 139 | setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 140 | incr_top(L); 141 | break; 142 | } 143 | case 'p': { 144 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ 145 | sprintf(buff, "%p", va_arg(argp, void *)); 146 | pushstr(L, buff); 147 | break; 148 | } 149 | case '%': { 150 | pushstr(L, "%"); 151 | break; 152 | } 153 | default: { 154 | char buff[3]; 155 | buff[0] = '%'; 156 | buff[1] = *(e+1); 157 | buff[2] = '\0'; 158 | pushstr(L, buff); 159 | break; 160 | } 161 | } 162 | n += 2; 163 | fmt = e+2; 164 | } 165 | pushstr(L, fmt); 166 | luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); 167 | L->top -= n; 168 | return svalue(L->top - 1); 169 | } 170 | 171 | 172 | const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 173 | const char *msg; 174 | va_list argp; 175 | va_start(argp, fmt); 176 | msg = luaO_pushvfstring(L, fmt, argp); 177 | va_end(argp); 178 | return msg; 179 | } 180 | 181 | 182 | void luaO_chunkid (char *out, const char *source, size_t bufflen) { 183 | if (*source == '=') { 184 | strncpy(out, source+1, bufflen); /* remove first char */ 185 | out[bufflen-1] = '\0'; /* ensures null termination */ 186 | } 187 | else { /* out = "source", or "...source" */ 188 | if (*source == '@') { 189 | size_t l; 190 | source++; /* skip the `@' */ 191 | bufflen -= sizeof(" '...' "); 192 | l = strlen(source); 193 | strcpy(out, ""); 194 | if (l > bufflen) { 195 | source += (l-bufflen); /* get last part of file name */ 196 | strcat(out, "..."); 197 | } 198 | strcat(out, source); 199 | } 200 | else { /* out = [string "string"] */ 201 | size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 202 | bufflen -= sizeof(" [string \"...\"] "); 203 | if (len > bufflen) len = bufflen; 204 | strcpy(out, "[string \""); 205 | if (source[len] != '\0') { /* must truncate? */ 206 | strncat(out, source, len); 207 | strcat(out, "..."); 208 | } 209 | else 210 | strcat(out, source); 211 | strcat(out, "\"]"); 212 | } 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /src/libs/lua51/lauxlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions for building Lua libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lauxlib_h 9 | #define lauxlib_h 10 | 11 | 12 | #include 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | 18 | #if defined(LUA_COMPAT_GETN) 19 | LUALIB_API int (luaL_getn) (lua_State *L, int t); 20 | LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); 21 | #else 22 | #define luaL_getn(L,i) ((int)lua_objlen(L, i)) 23 | #define luaL_setn(L,i,j) ((void)0) /* no op! */ 24 | #endif 25 | 26 | #if defined(LUA_COMPAT_OPENLIB) 27 | #define luaI_openlib luaL_openlib 28 | #endif 29 | 30 | 31 | /* extra error code for `luaL_load' */ 32 | #define LUA_ERRFILE (LUA_ERRERR+1) 33 | 34 | 35 | typedef struct luaL_Reg { 36 | const char *name; 37 | lua_CFunction func; 38 | } luaL_Reg; 39 | 40 | 41 | 42 | LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, 43 | const luaL_Reg *l, int nup); 44 | LUALIB_API void (luaL_register) (lua_State *L, const char *libname, 45 | const luaL_Reg *l); 46 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); 47 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); 48 | LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); 49 | LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); 50 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, 51 | size_t *l); 52 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, 53 | const char *def, size_t *l); 54 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); 55 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); 56 | 57 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); 58 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, 59 | lua_Integer def); 60 | 61 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); 62 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); 63 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); 64 | 65 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); 66 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); 67 | 68 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); 69 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 70 | 71 | LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, 72 | const char *const lst[]); 73 | 74 | LUALIB_API int (luaL_ref) (lua_State *L, int t); 75 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); 76 | 77 | LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); 78 | LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, 79 | const char *name); 80 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); 81 | 82 | LUALIB_API lua_State *(luaL_newstate) (void); 83 | 84 | 85 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, 86 | const char *r); 87 | 88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, 89 | const char *fname, int szhint); 90 | 91 | 92 | 93 | 94 | /* 95 | ** =============================================================== 96 | ** some useful macros 97 | ** =============================================================== 98 | */ 99 | 100 | #define luaL_argcheck(L, cond,numarg,extramsg) \ 101 | ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) 102 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) 103 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) 104 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) 105 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) 106 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) 107 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) 108 | 109 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) 110 | 111 | #define luaL_dofile(L, fn) \ 112 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) 113 | 114 | #define luaL_dostring(L, s) \ 115 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) 116 | 117 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) 118 | 119 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) 120 | 121 | /* 122 | ** {====================================================== 123 | ** Generic Buffer manipulation 124 | ** ======================================================= 125 | */ 126 | 127 | 128 | 129 | typedef struct luaL_Buffer { 130 | char *p; /* current position in buffer */ 131 | int lvl; /* number of strings in the stack (level) */ 132 | lua_State *L; 133 | char buffer[LUAL_BUFFERSIZE]; 134 | } luaL_Buffer; 135 | 136 | #define luaL_addchar(B,c) \ 137 | ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ 138 | (*(B)->p++ = (char)(c))) 139 | 140 | /* compatibility only */ 141 | #define luaL_putchar(B,c) luaL_addchar(B,c) 142 | 143 | #define luaL_addsize(B,n) ((B)->p += (n)) 144 | 145 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); 146 | LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); 147 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 148 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); 149 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); 150 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); 151 | 152 | 153 | /* }====================================================== */ 154 | 155 | 156 | /* compatibility with ref system */ 157 | 158 | /* pre-defined references */ 159 | #define LUA_NOREF (-2) 160 | #define LUA_REFNIL (-1) 161 | 162 | #define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ 163 | (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) 164 | 165 | #define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) 166 | 167 | #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) 168 | 169 | #define luaL_reg luaL_Reg 170 | 171 | #endif 172 | 173 | 174 | -------------------------------------------------------------------------------- /src/libs/lua51/lstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstate_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "llex.h" 20 | #include "lmem.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | #include "ltable.h" 24 | #include "ltm.h" 25 | 26 | 27 | #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) 28 | #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) 29 | #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) 30 | 31 | 32 | /* 33 | ** Main thread combines a thread state and the global state 34 | */ 35 | typedef struct LG { 36 | lua_State l; 37 | global_State g; 38 | } LG; 39 | 40 | 41 | 42 | static void stack_init (lua_State *L1, lua_State *L) { 43 | /* initialize CallInfo array */ 44 | L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); 45 | L1->ci = L1->base_ci; 46 | L1->size_ci = BASIC_CI_SIZE; 47 | L1->end_ci = L1->base_ci + L1->size_ci - 1; 48 | /* initialize stack array */ 49 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); 50 | L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 51 | L1->top = L1->stack; 52 | L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; 53 | /* initialize first ci */ 54 | L1->ci->func = L1->top; 55 | setnilvalue(L1->top++); /* `function' entry for this `ci' */ 56 | L1->base = L1->ci->base = L1->top; 57 | L1->ci->top = L1->top + LUA_MINSTACK; 58 | } 59 | 60 | 61 | static void freestack (lua_State *L, lua_State *L1) { 62 | luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); 63 | luaM_freearray(L, L1->stack, L1->stacksize, TValue); 64 | } 65 | 66 | 67 | /* 68 | ** open parts that may cause memory-allocation errors 69 | */ 70 | static void f_luaopen (lua_State *L, void *ud) { 71 | global_State *g = G(L); 72 | UNUSED(ud); 73 | stack_init(L, L); /* init stack */ 74 | sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ 75 | sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ 76 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ 77 | luaT_init(L); 78 | luaX_init(L); 79 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); 80 | g->GCthreshold = 4*g->totalbytes; 81 | } 82 | 83 | 84 | static void preinit_state (lua_State *L, global_State *g) { 85 | G(L) = g; 86 | L->stack = NULL; 87 | L->stacksize = 0; 88 | L->errorJmp = NULL; 89 | L->hook = NULL; 90 | L->hookmask = 0; 91 | L->basehookcount = 0; 92 | L->allowhook = 1; 93 | resethookcount(L); 94 | L->openupval = NULL; 95 | L->size_ci = 0; 96 | L->nCcalls = L->baseCcalls = 0; 97 | L->status = 0; 98 | L->base_ci = L->ci = NULL; 99 | L->savedpc = NULL; 100 | L->errfunc = 0; 101 | setnilvalue(gt(L)); 102 | } 103 | 104 | 105 | static void close_state (lua_State *L) { 106 | global_State *g = G(L); 107 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 108 | luaC_freeall(L); /* collect all objects */ 109 | lua_assert(g->rootgc == obj2gco(L)); 110 | lua_assert(g->strt.nuse == 0); 111 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); 112 | luaZ_freebuffer(L, &g->buff); 113 | freestack(L, L); 114 | lua_assert(g->totalbytes == sizeof(LG)); 115 | (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); 116 | } 117 | 118 | 119 | lua_State *luaE_newthread (lua_State *L) { 120 | lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); 121 | luaC_link(L, obj2gco(L1), LUA_TTHREAD); 122 | preinit_state(L1, G(L)); 123 | stack_init(L1, L); /* init stack */ 124 | setobj2n(L, gt(L1), gt(L)); /* share table of globals */ 125 | L1->hookmask = L->hookmask; 126 | L1->basehookcount = L->basehookcount; 127 | L1->hook = L->hook; 128 | resethookcount(L1); 129 | lua_assert(iswhite(obj2gco(L1))); 130 | return L1; 131 | } 132 | 133 | 134 | void luaE_freethread (lua_State *L, lua_State *L1) { 135 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 136 | lua_assert(L1->openupval == NULL); 137 | luai_userstatefree(L1); 138 | freestack(L, L1); 139 | luaM_freemem(L, fromstate(L1), state_size(lua_State)); 140 | } 141 | 142 | 143 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 144 | int i; 145 | lua_State *L; 146 | global_State *g; 147 | void *l = (*f)(ud, NULL, 0, state_size(LG)); 148 | if (l == NULL) return NULL; 149 | L = tostate(l); 150 | g = &((LG *)L)->g; 151 | L->next = NULL; 152 | L->tt = LUA_TTHREAD; 153 | g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); 154 | L->marked = luaC_white(g); 155 | set2bits(L->marked, FIXEDBIT, SFIXEDBIT); 156 | preinit_state(L, g); 157 | g->frealloc = f; 158 | g->ud = ud; 159 | g->mainthread = L; 160 | g->uvhead.u.l.prev = &g->uvhead; 161 | g->uvhead.u.l.next = &g->uvhead; 162 | g->GCthreshold = 0; /* mark it as unfinished state */ 163 | g->strt.size = 0; 164 | g->strt.nuse = 0; 165 | g->strt.hash = NULL; 166 | setnilvalue(registry(L)); 167 | luaZ_initbuffer(L, &g->buff); 168 | g->panic = NULL; 169 | g->gcstate = GCSpause; 170 | g->rootgc = obj2gco(L); 171 | g->sweepstrgc = 0; 172 | g->sweepgc = &g->rootgc; 173 | g->gray = NULL; 174 | g->grayagain = NULL; 175 | g->weak = NULL; 176 | g->tmudata = NULL; 177 | g->totalbytes = sizeof(LG); 178 | g->gcpause = LUAI_GCPAUSE; 179 | g->gcstepmul = LUAI_GCMUL; 180 | g->gcdept = 0; 181 | for (i=0; imt[i] = NULL; 182 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { 183 | /* memory allocation error: free partial state */ 184 | close_state(L); 185 | L = NULL; 186 | } 187 | else 188 | luai_userstateopen(L); 189 | return L; 190 | } 191 | 192 | 193 | static void callallgcTM (lua_State *L, void *ud) { 194 | UNUSED(ud); 195 | luaC_callGCTM(L); /* call GC metamethods for all udata */ 196 | } 197 | 198 | 199 | LUA_API void lua_close (lua_State *L) { 200 | L = G(L)->mainthread; /* only the main thread can be closed */ 201 | lua_lock(L); 202 | luaF_close(L, L->stack); /* close all upvalues for this thread */ 203 | luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ 204 | L->errfunc = 0; /* no error function during GC metamethods */ 205 | do { /* repeat until no more errors */ 206 | L->ci = L->base_ci; 207 | L->base = L->top = L->ci->base; 208 | L->nCcalls = L->baseCcalls = 0; 209 | } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); 210 | lua_assert(G(L)->tmudata == NULL); 211 | luai_userstateclose(L); 212 | close_state(L); 213 | } 214 | 215 | -------------------------------------------------------------------------------- /src/libs/lua51/Makefile: -------------------------------------------------------------------------------- 1 | # makefile for building Lua 2 | # see ../INSTALL for installation instructions 3 | # see ../Makefile and luaconf.h for further customization 4 | 5 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 6 | 7 | # Your platform. See PLATS for possible values. 8 | PLAT= none 9 | 10 | CC= gcc 11 | CFLAGS= -O2 -Wall $(MYCFLAGS) 12 | AR= ar rcu 13 | RANLIB= ranlib 14 | RM= rm -f 15 | LIBS= -lm $(MYLIBS) 16 | 17 | MYCFLAGS= 18 | MYLDFLAGS= 19 | MYLIBS= 20 | 21 | # == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE ========= 22 | 23 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 24 | 25 | LUA_A= liblua.a 26 | CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \ 27 | lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \ 28 | lundump.o lvm.o lzio.o 29 | LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \ 30 | lstrlib.o loadlib.o linit.o 31 | 32 | LUA_T= lua 33 | LUA_O= lua.o 34 | 35 | LUAC_T= luac 36 | LUAC_O= luac.o print.o 37 | 38 | ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O) 39 | ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) 40 | ALL_A= $(LUA_A) 41 | 42 | default: $(PLAT) 43 | 44 | all: $(ALL_T) 45 | 46 | o: $(ALL_O) 47 | 48 | a: $(ALL_A) 49 | 50 | $(LUA_A): $(CORE_O) $(LIB_O) 51 | $(AR) $@ $(CORE_O) $(LIB_O) # DLL needs all object files 52 | $(RANLIB) $@ 53 | 54 | $(LUA_T): $(LUA_O) $(LUA_A) 55 | $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) 56 | 57 | $(LUAC_T): $(LUAC_O) $(LUA_A) 58 | $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS) 59 | 60 | clean: 61 | $(RM) $(ALL_T) $(ALL_O) 62 | 63 | depend: 64 | @$(CC) $(CFLAGS) -MM l*.c print.c 65 | 66 | echo: 67 | @echo "PLAT = $(PLAT)" 68 | @echo "CC = $(CC)" 69 | @echo "CFLAGS = $(CFLAGS)" 70 | @echo "AR = $(AR)" 71 | @echo "RANLIB = $(RANLIB)" 72 | @echo "RM = $(RM)" 73 | @echo "MYCFLAGS = $(MYCFLAGS)" 74 | @echo "MYLDFLAGS = $(MYLDFLAGS)" 75 | @echo "MYLIBS = $(MYLIBS)" 76 | 77 | # convenience targets for popular platforms 78 | 79 | none: 80 | @echo "Please choose a platform:" 81 | @echo " $(PLATS)" 82 | 83 | aix: 84 | $(MAKE) all CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" MYLDFLAGS="-brtl -bexpall" 85 | 86 | ansi: 87 | $(MAKE) all MYCFLAGS=-DLUA_ANSI 88 | 89 | bsd: 90 | $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-Wl,-E" 91 | 92 | freebsd: 93 | $(MAKE) all MYCFLAGS="-DLUA_USE_LINUX" MYLIBS="-Wl,-E -lreadline" 94 | 95 | generic: 96 | $(MAKE) all MYCFLAGS= 97 | 98 | linux: 99 | $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses" 100 | 101 | macosx: 102 | $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-lreadline" 103 | # use this on Mac OS X 10.3- 104 | # $(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX 105 | 106 | mingw: 107 | $(MAKE) "LUA_A=lua51.dll" "LUA_T=lua.exe" \ 108 | "AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \ 109 | "MYCFLAGS=-DLUA_BUILD_AS_DLL" "MYLIBS=" "MYLDFLAGS=-s" lua.exe 110 | $(MAKE) "LUAC_T=luac.exe" luac.exe 111 | 112 | posix: 113 | $(MAKE) all MYCFLAGS=-DLUA_USE_POSIX 114 | 115 | solaris: 116 | $(MAKE) all MYCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" MYLIBS="-ldl" 117 | 118 | # list targets that do not create files (but not all makes understand .PHONY) 119 | .PHONY: all $(PLATS) default o a clean depend echo none 120 | 121 | # DO NOT DELETE 122 | 123 | lapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \ 124 | lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \ 125 | lundump.h lvm.h 126 | lauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h 127 | lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h 128 | lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 129 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \ 130 | ltable.h 131 | ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h 132 | ldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \ 133 | llex.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \ 134 | lfunc.h lstring.h lgc.h ltable.h lvm.h 135 | ldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 136 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h lstring.h \ 137 | ltable.h lundump.h lvm.h 138 | ldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \ 139 | lzio.h lmem.h lundump.h 140 | lfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \ 141 | lstate.h ltm.h lzio.h 142 | lgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 143 | lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h 144 | linit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h 145 | liolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h 146 | llex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \ 147 | lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h 148 | lmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h 149 | lmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 150 | ltm.h lzio.h lmem.h ldo.h 151 | loadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h 152 | lobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \ 153 | ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h 154 | lopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h 155 | loslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h 156 | lparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \ 157 | lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \ 158 | lfunc.h lstring.h lgc.h ltable.h 159 | lstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 160 | ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h 161 | lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ 162 | ltm.h lzio.h lstring.h lgc.h 163 | lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h 164 | ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \ 165 | ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h 166 | ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h 167 | ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \ 168 | lmem.h lstring.h lgc.h ltable.h 169 | lua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h 170 | luac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \ 171 | lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \ 172 | lundump.h 173 | lundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \ 174 | llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h 175 | lvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \ 176 | lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h 177 | lzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \ 178 | lzio.h 179 | print.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \ 180 | ltm.h lzio.h lmem.h lopcodes.h lundump.h 181 | 182 | # (end of Makefile) 183 | -------------------------------------------------------------------------------- /src/libs/lua51/lmathlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Standard mathematical library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | 11 | #define lmathlib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | #undef PI 21 | #define PI (3.14159265358979323846) 22 | #define RADIANS_PER_DEGREE (PI/180.0) 23 | 24 | 25 | 26 | static int math_abs (lua_State *L) { 27 | lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); 28 | return 1; 29 | } 30 | 31 | static int math_sin (lua_State *L) { 32 | lua_pushnumber(L, sin(luaL_checknumber(L, 1))); 33 | return 1; 34 | } 35 | 36 | static int math_sinh (lua_State *L) { 37 | lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); 38 | return 1; 39 | } 40 | 41 | static int math_cos (lua_State *L) { 42 | lua_pushnumber(L, cos(luaL_checknumber(L, 1))); 43 | return 1; 44 | } 45 | 46 | static int math_cosh (lua_State *L) { 47 | lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); 48 | return 1; 49 | } 50 | 51 | static int math_tan (lua_State *L) { 52 | lua_pushnumber(L, tan(luaL_checknumber(L, 1))); 53 | return 1; 54 | } 55 | 56 | static int math_tanh (lua_State *L) { 57 | lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); 58 | return 1; 59 | } 60 | 61 | static int math_asin (lua_State *L) { 62 | lua_pushnumber(L, asin(luaL_checknumber(L, 1))); 63 | return 1; 64 | } 65 | 66 | static int math_acos (lua_State *L) { 67 | lua_pushnumber(L, acos(luaL_checknumber(L, 1))); 68 | return 1; 69 | } 70 | 71 | static int math_atan (lua_State *L) { 72 | lua_pushnumber(L, atan(luaL_checknumber(L, 1))); 73 | return 1; 74 | } 75 | 76 | static int math_atan2 (lua_State *L) { 77 | lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 78 | return 1; 79 | } 80 | 81 | static int math_ceil (lua_State *L) { 82 | lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); 83 | return 1; 84 | } 85 | 86 | static int math_floor (lua_State *L) { 87 | lua_pushnumber(L, floor(luaL_checknumber(L, 1))); 88 | return 1; 89 | } 90 | 91 | static int math_fmod (lua_State *L) { 92 | lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 93 | return 1; 94 | } 95 | 96 | static int math_modf (lua_State *L) { 97 | double ip; 98 | double fp = modf(luaL_checknumber(L, 1), &ip); 99 | lua_pushnumber(L, ip); 100 | lua_pushnumber(L, fp); 101 | return 2; 102 | } 103 | 104 | static int math_sqrt (lua_State *L) { 105 | lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); 106 | return 1; 107 | } 108 | 109 | static int math_pow (lua_State *L) { 110 | lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); 111 | return 1; 112 | } 113 | 114 | static int math_log (lua_State *L) { 115 | lua_pushnumber(L, log(luaL_checknumber(L, 1))); 116 | return 1; 117 | } 118 | 119 | static int math_log10 (lua_State *L) { 120 | lua_pushnumber(L, log10(luaL_checknumber(L, 1))); 121 | return 1; 122 | } 123 | 124 | static int math_exp (lua_State *L) { 125 | lua_pushnumber(L, exp(luaL_checknumber(L, 1))); 126 | return 1; 127 | } 128 | 129 | static int math_deg (lua_State *L) { 130 | lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); 131 | return 1; 132 | } 133 | 134 | static int math_rad (lua_State *L) { 135 | lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); 136 | return 1; 137 | } 138 | 139 | static int math_frexp (lua_State *L) { 140 | int e; 141 | lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); 142 | lua_pushinteger(L, e); 143 | return 2; 144 | } 145 | 146 | static int math_ldexp (lua_State *L) { 147 | lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); 148 | return 1; 149 | } 150 | 151 | 152 | 153 | static int math_min (lua_State *L) { 154 | int n = lua_gettop(L); /* number of arguments */ 155 | lua_Number dmin = luaL_checknumber(L, 1); 156 | int i; 157 | for (i=2; i<=n; i++) { 158 | lua_Number d = luaL_checknumber(L, i); 159 | if (d < dmin) 160 | dmin = d; 161 | } 162 | lua_pushnumber(L, dmin); 163 | return 1; 164 | } 165 | 166 | 167 | static int math_max (lua_State *L) { 168 | int n = lua_gettop(L); /* number of arguments */ 169 | lua_Number dmax = luaL_checknumber(L, 1); 170 | int i; 171 | for (i=2; i<=n; i++) { 172 | lua_Number d = luaL_checknumber(L, i); 173 | if (d > dmax) 174 | dmax = d; 175 | } 176 | lua_pushnumber(L, dmax); 177 | return 1; 178 | } 179 | 180 | 181 | static int math_random (lua_State *L) { 182 | /* the `%' avoids the (rare) case of r==1, and is needed also because on 183 | some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ 184 | lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; 185 | switch (lua_gettop(L)) { /* check number of arguments */ 186 | case 0: { /* no arguments */ 187 | lua_pushnumber(L, r); /* Number between 0 and 1 */ 188 | break; 189 | } 190 | case 1: { /* only upper limit */ 191 | int u = luaL_checkint(L, 1); 192 | luaL_argcheck(L, 1<=u, 1, "interval is empty"); 193 | lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ 194 | break; 195 | } 196 | case 2: { /* lower and upper limits */ 197 | int l = luaL_checkint(L, 1); 198 | int u = luaL_checkint(L, 2); 199 | luaL_argcheck(L, l<=u, 2, "interval is empty"); 200 | lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ 201 | break; 202 | } 203 | default: return luaL_error(L, "wrong number of arguments"); 204 | } 205 | return 1; 206 | } 207 | 208 | 209 | static int math_randomseed (lua_State *L) { 210 | srand(luaL_checkint(L, 1)); 211 | return 0; 212 | } 213 | 214 | 215 | static const luaL_Reg mathlib[] = { 216 | {"abs", math_abs}, 217 | {"acos", math_acos}, 218 | {"asin", math_asin}, 219 | {"atan2", math_atan2}, 220 | {"atan", math_atan}, 221 | {"ceil", math_ceil}, 222 | {"cosh", math_cosh}, 223 | {"cos", math_cos}, 224 | {"deg", math_deg}, 225 | {"exp", math_exp}, 226 | {"floor", math_floor}, 227 | {"fmod", math_fmod}, 228 | {"frexp", math_frexp}, 229 | {"ldexp", math_ldexp}, 230 | {"log10", math_log10}, 231 | {"log", math_log}, 232 | {"max", math_max}, 233 | {"min", math_min}, 234 | {"modf", math_modf}, 235 | {"pow", math_pow}, 236 | {"rad", math_rad}, 237 | {"random", math_random}, 238 | {"randomseed", math_randomseed}, 239 | {"sinh", math_sinh}, 240 | {"sin", math_sin}, 241 | {"sqrt", math_sqrt}, 242 | {"tanh", math_tanh}, 243 | {"tan", math_tan}, 244 | {NULL, NULL} 245 | }; 246 | 247 | 248 | /* 249 | ** Open math library 250 | */ 251 | LUALIB_API int luaopen_math (lua_State *L) { 252 | luaL_register(L, LUA_MATHLIBNAME, mathlib); 253 | lua_pushnumber(L, PI); 254 | lua_setfield(L, -2, "pi"); 255 | lua_pushnumber(L, HUGE_VAL); 256 | lua_setfield(L, -2, "huge"); 257 | #if defined(LUA_COMPAT_MOD) 258 | lua_getfield(L, -1, "fmod"); 259 | lua_setfield(L, -2, "mod"); 260 | #endif 261 | return 1; 262 | } 263 | 264 | -------------------------------------------------------------------------------- /src/libs/lua51/loslib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 3 | ** Standard Operating System library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define loslib_c 15 | #define LUA_LIB 16 | 17 | #include "lua.h" 18 | 19 | #include "lauxlib.h" 20 | #include "lualib.h" 21 | 22 | 23 | static int os_pushresult (lua_State *L, int i, const char *filename) { 24 | int en = errno; /* calls to Lua API may change this value */ 25 | if (i) { 26 | lua_pushboolean(L, 1); 27 | return 1; 28 | } 29 | else { 30 | lua_pushnil(L); 31 | lua_pushfstring(L, "%s: %s", filename, strerror(en)); 32 | lua_pushinteger(L, en); 33 | return 3; 34 | } 35 | } 36 | 37 | 38 | static int os_execute (lua_State *L) { 39 | lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 40 | return 1; 41 | } 42 | 43 | 44 | static int os_remove (lua_State *L) { 45 | const char *filename = luaL_checkstring(L, 1); 46 | return os_pushresult(L, remove(filename) == 0, filename); 47 | } 48 | 49 | 50 | static int os_rename (lua_State *L) { 51 | const char *fromname = luaL_checkstring(L, 1); 52 | const char *toname = luaL_checkstring(L, 2); 53 | return os_pushresult(L, rename(fromname, toname) == 0, fromname); 54 | } 55 | 56 | 57 | static int os_tmpname (lua_State *L) { 58 | char buff[LUA_TMPNAMBUFSIZE]; 59 | int err; 60 | lua_tmpnam(buff, err); 61 | if (err) 62 | return luaL_error(L, "unable to generate a unique filename"); 63 | lua_pushstring(L, buff); 64 | return 1; 65 | } 66 | 67 | 68 | static int os_getenv (lua_State *L) { 69 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 70 | return 1; 71 | } 72 | 73 | 74 | static int os_clock (lua_State *L) { 75 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 76 | return 1; 77 | } 78 | 79 | 80 | /* 81 | ** {====================================================== 82 | ** Time/Date operations 83 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 84 | ** wday=%w+1, yday=%j, isdst=? } 85 | ** ======================================================= 86 | */ 87 | 88 | static void setfield (lua_State *L, const char *key, int value) { 89 | lua_pushinteger(L, value); 90 | lua_setfield(L, -2, key); 91 | } 92 | 93 | static void setboolfield (lua_State *L, const char *key, int value) { 94 | if (value < 0) /* undefined? */ 95 | return; /* does not set field */ 96 | lua_pushboolean(L, value); 97 | lua_setfield(L, -2, key); 98 | } 99 | 100 | static int getboolfield (lua_State *L, const char *key) { 101 | int res; 102 | lua_getfield(L, -1, key); 103 | res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 104 | lua_pop(L, 1); 105 | return res; 106 | } 107 | 108 | 109 | static int getfield (lua_State *L, const char *key, int d) { 110 | int res; 111 | lua_getfield(L, -1, key); 112 | if (lua_isnumber(L, -1)) 113 | res = (int)lua_tointeger(L, -1); 114 | else { 115 | if (d < 0) 116 | return luaL_error(L, "field " LUA_QS " missing in date table", key); 117 | res = d; 118 | } 119 | lua_pop(L, 1); 120 | return res; 121 | } 122 | 123 | 124 | static int os_date (lua_State *L) { 125 | const char *s = luaL_optstring(L, 1, "%c"); 126 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 127 | struct tm *stm; 128 | if (*s == '!') { /* UTC? */ 129 | stm = gmtime(&t); 130 | s++; /* skip `!' */ 131 | } 132 | else 133 | stm = localtime(&t); 134 | if (stm == NULL) /* invalid date? */ 135 | lua_pushnil(L); 136 | else if (strcmp(s, "*t") == 0) { 137 | lua_createtable(L, 0, 9); /* 9 = number of fields */ 138 | setfield(L, "sec", stm->tm_sec); 139 | setfield(L, "min", stm->tm_min); 140 | setfield(L, "hour", stm->tm_hour); 141 | setfield(L, "day", stm->tm_mday); 142 | setfield(L, "month", stm->tm_mon+1); 143 | setfield(L, "year", stm->tm_year+1900); 144 | setfield(L, "wday", stm->tm_wday+1); 145 | setfield(L, "yday", stm->tm_yday+1); 146 | setboolfield(L, "isdst", stm->tm_isdst); 147 | } 148 | else { 149 | char cc[3]; 150 | luaL_Buffer b; 151 | cc[0] = '%'; cc[2] = '\0'; 152 | luaL_buffinit(L, &b); 153 | for (; *s; s++) { 154 | if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 155 | luaL_addchar(&b, *s); 156 | else { 157 | size_t reslen; 158 | char buff[200]; /* should be big enough for any conversion result */ 159 | cc[1] = *(++s); 160 | reslen = strftime(buff, sizeof(buff), cc, stm); 161 | luaL_addlstring(&b, buff, reslen); 162 | } 163 | } 164 | luaL_pushresult(&b); 165 | } 166 | return 1; 167 | } 168 | 169 | 170 | static int os_time (lua_State *L) { 171 | time_t t; 172 | if (lua_isnoneornil(L, 1)) /* called without args? */ 173 | t = time(NULL); /* get current time */ 174 | else { 175 | struct tm ts; 176 | luaL_checktype(L, 1, LUA_TTABLE); 177 | lua_settop(L, 1); /* make sure table is at the top */ 178 | ts.tm_sec = getfield(L, "sec", 0); 179 | ts.tm_min = getfield(L, "min", 0); 180 | ts.tm_hour = getfield(L, "hour", 12); 181 | ts.tm_mday = getfield(L, "day", -1); 182 | ts.tm_mon = getfield(L, "month", -1) - 1; 183 | ts.tm_year = getfield(L, "year", -1) - 1900; 184 | ts.tm_isdst = getboolfield(L, "isdst"); 185 | t = mktime(&ts); 186 | } 187 | if (t == (time_t)(-1)) 188 | lua_pushnil(L); 189 | else 190 | lua_pushnumber(L, (lua_Number)t); 191 | return 1; 192 | } 193 | 194 | 195 | static int os_difftime (lua_State *L) { 196 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 197 | (time_t)(luaL_optnumber(L, 2, 0)))); 198 | return 1; 199 | } 200 | 201 | /* }====================================================== */ 202 | 203 | 204 | static int os_setlocale (lua_State *L) { 205 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 206 | LC_NUMERIC, LC_TIME}; 207 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 208 | "numeric", "time", NULL}; 209 | const char *l = luaL_optstring(L, 1, NULL); 210 | int op = luaL_checkoption(L, 2, "all", catnames); 211 | lua_pushstring(L, setlocale(cat[op], l)); 212 | return 1; 213 | } 214 | 215 | 216 | static int os_exit (lua_State *L) { 217 | exit(luaL_optint(L, 1, EXIT_SUCCESS)); 218 | } 219 | 220 | static const luaL_Reg syslib[] = { 221 | {"clock", os_clock}, 222 | {"date", os_date}, 223 | {"difftime", os_difftime}, 224 | {"execute", os_execute}, 225 | {"exit", os_exit}, 226 | {"getenv", os_getenv}, 227 | {"remove", os_remove}, 228 | {"rename", os_rename}, 229 | {"setlocale", os_setlocale}, 230 | {"time", os_time}, 231 | {"tmpname", os_tmpname}, 232 | {NULL, NULL} 233 | }; 234 | 235 | /* }====================================================== */ 236 | 237 | 238 | 239 | LUALIB_API int luaopen_os (lua_State *L) { 240 | luaL_register(L, LUA_OSLIBNAME, syslib); 241 | return 1; 242 | } 243 | 244 | --------------------------------------------------------------------------------