├── res ├── raw │ ├── blank.png │ ├── glow.png │ ├── moon.png │ ├── noise.png │ ├── star.png │ └── sun.png ├── tool │ ├── simplepacker │ ├── utils.lua │ ├── binpacking.lua │ ├── ejresource.lua │ └── ejpackage.lua └── export.sh ├── screenshots ├── ScreenShot1.png ├── ScreenShot2.png └── ScreenShot3.png ├── engine ├── lib │ ├── fault.h │ ├── ppm.h │ ├── lmatrix.h │ ├── lgeometry.h │ ├── scissor.h │ ├── lrenderbuffer.h │ ├── screenshot.h │ ├── render │ │ ├── log.h │ │ ├── block.h │ │ ├── carray.h │ │ ├── log.c │ │ ├── blendmode.h │ │ ├── carray.c │ │ └── render.h │ ├── audio │ │ ├── oal.h │ │ ├── oal_decode.h │ │ ├── oal_decode.c │ │ └── decode │ │ │ ├── ad_mp3.c │ │ │ ├── ad_hardware_mac_ios.m │ │ │ ├── ad_tools.c │ │ │ └── ad_wav.c │ ├── array.h │ ├── screen.h │ ├── fault.c │ ├── opengl.h │ ├── material.h │ ├── platform_print.h │ ├── dfont.h │ ├── lejoy2dcore.c │ ├── texture.h │ ├── renderbuffer.h │ ├── ejoy2dgame.h │ ├── scissor.c │ ├── shader.h │ ├── label.h │ ├── matrix.h │ ├── lrenderbuffer.c │ ├── screenshot.c │ ├── screen.c │ ├── sprite.h │ ├── spritepack.h │ ├── lgeometry.c │ ├── matrix.c │ ├── lmatrix.c │ └── lshader.c ├── lua │ ├── lua.hpp │ ├── lapi.h │ ├── lundump.h │ ├── lprefix.h │ ├── lualib.h │ ├── lstring.h │ ├── ldebug.h │ ├── ldo.h │ ├── lfunc.h │ ├── lzio.h │ ├── ltable.h │ ├── lzio.c │ ├── linit.c │ ├── ltm.h │ ├── lvm.h │ ├── lctype.h │ ├── lctype.c │ ├── llex.h │ ├── lmem.h │ ├── lmem.c │ ├── lcode.h │ ├── lparser.h │ ├── lopcodes.c │ ├── lfunc.c │ ├── ltm.c │ ├── lcorolib.c │ ├── lgc.h │ ├── lstring.c │ ├── ldump.c │ └── lbitlib.c ├── ejoy2d │ ├── matrix.lua │ ├── sound.lua │ ├── geometry.lua │ ├── init.lua │ ├── music.lua │ ├── simplepackage.lua │ ├── particle.lua │ └── sprite.lua └── platform │ ├── winfw.h │ └── winfw.c ├── .gitignore ├── src ├── main.lua ├── dawn_shader.lua └── dawn_config.lua └── README.md /res/raw/blank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/raw/blank.png -------------------------------------------------------------------------------- /res/raw/glow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/raw/glow.png -------------------------------------------------------------------------------- /res/raw/moon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/raw/moon.png -------------------------------------------------------------------------------- /res/raw/noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/raw/noise.png -------------------------------------------------------------------------------- /res/raw/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/raw/star.png -------------------------------------------------------------------------------- /res/raw/sun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/raw/sun.png -------------------------------------------------------------------------------- /res/tool/simplepacker: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/res/tool/simplepacker -------------------------------------------------------------------------------- /screenshots/ScreenShot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/screenshots/ScreenShot1.png -------------------------------------------------------------------------------- /screenshots/ScreenShot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/screenshots/ScreenShot2.png -------------------------------------------------------------------------------- /screenshots/ScreenShot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skykapok/dawn/HEAD/screenshots/ScreenShot3.png -------------------------------------------------------------------------------- /res/export.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd tool 3 | rm -rf ../package 4 | ./simplepacker ../raw -o ../package -n dawn -np -v 5 | -------------------------------------------------------------------------------- /engine/lib/fault.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_FAULT_H 2 | #define EJOY_2D_FAULT_H 3 | 4 | void fault(const char * format, ...); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /engine/lib/ppm.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy_2d_ppm_h 2 | #define ejoy_2d_ppm_h 3 | 4 | #include 5 | 6 | int ejoy2d_ppm(lua_State *L); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /engine/lib/lmatrix.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY2D_LUA_MATRIX_H 2 | #define EJOY2D_LUA_MATRIX_H 3 | 4 | #include 5 | 6 | int ejoy2d_matrix(lua_State *L); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /engine/lib/lgeometry.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_lua_geometry_h 2 | #define ejoy2d_lua_geometry_h 3 | 4 | #include 5 | 6 | int ejoy2d_geometry(lua_State *L); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /engine/lib/scissor.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY2D_SCISSOR_H 2 | #define EJOY2D_SCISSOR_H 3 | 4 | void scissor_push(int x, int y, int w, int h); 5 | void scissor_pop(); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /engine/lib/lrenderbuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_lua_renderbuffer_h 2 | #define ejoy2d_lua_renderbuffer_h 3 | 4 | #include 5 | 6 | int ejoy2d_renderbuffer(lua_State *L); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /engine/lua/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /engine/lib/screenshot.h: -------------------------------------------------------------------------------- 1 | #ifndef screenshot_h 2 | #define screenshot_h 3 | 4 | #include "sprite.h" 5 | 6 | int screenshot(int x, int y, int w, int h, int tex_id, struct sprite* spr, unsigned char* pixels); 7 | void release_screenshot(int tex_id); 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /engine/lib/render/log.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy3d_log_h 2 | #define ejoy3d_log_h 3 | 4 | #include 5 | 6 | struct log { 7 | FILE *f; 8 | }; 9 | 10 | void log_init(struct log *log, FILE *f); 11 | void log_printf(struct log *log, const char * format, ...); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /engine/lib/audio/oal.h: -------------------------------------------------------------------------------- 1 | #ifndef _OAL_H_ 2 | #define _OAL_H_ 3 | 4 | #include 5 | 6 | // call after interruption starts 7 | void oal_interrupted(); 8 | 9 | // call after interruption ends 10 | void oal_resumed(); 11 | 12 | int luaopen_oal(lua_State* L); 13 | 14 | #endif -------------------------------------------------------------------------------- /engine/lib/array.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_ARRAY_H 2 | #define EJOY_2D_ARRAY_H 3 | 4 | #if defined(_MSC_VER) 5 | # include 6 | # define ARRAY(type, name, size) type* name = (type*)_alloca((size) * sizeof(type)) 7 | #else 8 | # define ARRAY(type, name, size) type name[size] 9 | #endif 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | # ios 4 | *.xcworkspace 5 | xcuserdata 6 | 7 | # android 8 | build/android/gen 9 | build/android/obj 10 | build/android/bin 11 | build/android/libs 12 | build/android/assets 13 | build/android/local.properties 14 | build/android/.ant-targets-build.xml 15 | 16 | # generated resource 17 | res/package -------------------------------------------------------------------------------- /engine/lib/screen.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy_2d_screen_h 2 | #define ejoy_2d_screen_h 3 | #include 4 | 5 | struct render; 6 | 7 | void screen_initrender(struct render *R); 8 | void screen_init(float w, float h, float scale); 9 | void screen_trans(float *x, float *y); 10 | void screen_scissor(int x, int y, int w, int h); 11 | bool screen_is_visible(float x,float y); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /engine/lib/fault.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "platform_print.h" 6 | 7 | void 8 | fault(const char * format, ...) { 9 | if (format[0] == '!') { 10 | va_list ap; 11 | va_start(ap, format); 12 | pf_vprint(format+1, ap); 13 | va_end(ap); 14 | } else { 15 | va_list ap; 16 | va_start(ap, format); 17 | pf_vprint(format, ap); 18 | va_end(ap); 19 | exit(1); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /engine/lib/opengl.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_opengl_h 2 | #define ejoy2d_opengl_h 3 | 4 | #if defined( __APPLE__ ) && !defined(__MACOSX) 5 | 6 | #define OPENGLES 2 7 | #include 8 | #import 9 | 10 | #elif defined(linux) || defined(__linux) || defined(__linux__) 11 | 12 | #define OPENGLES 2 13 | #include 14 | #include 15 | 16 | #else 17 | #define OPENGLES 0 18 | #include 19 | 20 | #endif 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /engine/lib/render/block.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy3d_block_h 2 | #define ejoy3d_block_h 3 | 4 | #include 5 | 6 | struct block { 7 | char * buffer; 8 | int sz; 9 | }; 10 | 11 | static inline void 12 | block_init(struct block * B, void * buffer, int sz) { 13 | B->buffer = (char*)buffer; 14 | B->sz = sz; 15 | } 16 | 17 | static inline void * 18 | block_slice(struct block * B, int sz) { 19 | if (B->sz < sz) { 20 | return NULL; 21 | } 22 | void * ret = B->buffer; 23 | B->buffer += sz; 24 | B->sz -= sz; 25 | return ret; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /engine/lib/material.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_material_h 2 | #define ejoy2d_material_h 3 | 4 | // the implemention is in shader.c 5 | 6 | struct material; 7 | struct render; 8 | 9 | int material_size(int prog); 10 | struct material * material_init(void *self, int size, int prog); 11 | void material_apply(int prog, struct material *); 12 | int material_setuniform(struct material *, int index, int n, const float *v); 13 | int material_settexture(struct material *, int channel, int texture); 14 | // todo: change alpha blender mode, change attrib layout, etc. 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /engine/lib/platform_print.h: -------------------------------------------------------------------------------- 1 | #ifndef __PLATFORM_PRINT_H__ 2 | #define __PLATFORM_PRINT_H__ 3 | 4 | #ifdef __ANDROID__ 5 | #include 6 | #include 7 | #define LOG_TAG "ejoy2d" 8 | #define pf_log(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) 9 | #define pf_vprint(format, ap) __android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG, (format), (ap)) 10 | #else 11 | #define pf_log printf 12 | #define pf_vprint vprintf 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /engine/lib/audio/oal_decode.h: -------------------------------------------------------------------------------- 1 | #ifndef _OAL_DECODE_H_ 2 | #define _OAL_DECODE_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | struct oal_info { 11 | ALvoid* data; 12 | ALsizei size; 13 | ALenum format; 14 | ALsizei freq; 15 | char type[8]; // caf, mp3 ...etc 16 | }; 17 | void od_free_info(struct oal_info* info); 18 | 19 | void ad_error(const char* f, ...); 20 | const char* ad_last_error(); 21 | 22 | int ad_new_info(lua_State* L, struct oal_info* info); 23 | 24 | int luaopen_oal_decode(lua_State* L); 25 | #endif -------------------------------------------------------------------------------- /engine/ejoy2d/matrix.lua: -------------------------------------------------------------------------------- 1 | local c = require "ejoy2d.matrix.c" 2 | 3 | local matrix_meta = { 4 | __index = c, 5 | __tostring = c.tostring, 6 | } 7 | 8 | function matrix(srt) 9 | local mat = c.new(srt) 10 | if type(srt) == "table" then 11 | if srt.scale then 12 | c.scale(mat, srt.scale) 13 | elseif srt.sx and srt.sy then 14 | c.scale(mat,srt.sx, srt.sy) 15 | end 16 | if srt.rot then 17 | c.rot(mat, srt.rot) 18 | end 19 | if srt.x and srt.y then 20 | c.trans(mat, srt.x, srt.y) 21 | end 22 | end 23 | return debug.setmetatable(mat, matrix_meta) 24 | end 25 | 26 | return matrix -------------------------------------------------------------------------------- /engine/lib/render/carray.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy3d_array_h 2 | #define ejoy3d_array_h 3 | 4 | struct array_node; 5 | 6 | struct array { 7 | int n; 8 | int sz; 9 | char * buffer; 10 | struct array_node * freelist; 11 | }; 12 | 13 | int array_size(int n, int sz); 14 | void array_init(struct array *p, void * buffer, int n, int sz); 15 | void * array_alloc(struct array *p); 16 | void array_free(struct array *p, void *v); 17 | void array_exit(struct array *p, void (*close)(void *p, void *ud), void *ud); 18 | 19 | int array_id(struct array *p, void *); 20 | void * array_ref(struct array *p, int id); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /engine/ejoy2d/sound.lua: -------------------------------------------------------------------------------- 1 | local audio = require "audio" 2 | 3 | local M = {} 4 | local sound_group = audio:create_group(16) 5 | 6 | 7 | function M:load(file_path) 8 | return audio:load(file_path) 9 | end 10 | 11 | 12 | function M:unload(file_path) 13 | return audio:unload(file_path) 14 | end 15 | 16 | function M:play(file_path, loop, pitch, gain) 17 | local handle = sound_group:add(file_path, loop, pitch, gain) 18 | sound_group:play(handle) 19 | return handle 20 | end 21 | 22 | function M:open() 23 | return sound_group:open() 24 | end 25 | 26 | function M:close() 27 | return sound_group:close() 28 | end 29 | 30 | return M -------------------------------------------------------------------------------- /engine/lua/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 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 "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check((n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /engine/platform/winfw.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_window_fw_h 2 | #define ejoy2d_window_fw_h 3 | 4 | #define WIDTH 1024 5 | #define HEIGHT 768 6 | 7 | #define TOUCH_BEGIN 0 8 | #define TOUCH_END 1 9 | #define TOUCH_MOVE 2 10 | 11 | #define ORIENT_UP 0 12 | #define ORIENT_DOWN 1 13 | #define ORIENT_LEFT 2 14 | #define ORIENT_RIGHT 3 15 | 16 | void ejoy2d_win_init(int orix, int oriy, int width, int height, float scale, const char* folder); 17 | void ejoy2d_win_release(); 18 | void ejoy2d_win_frame(); 19 | void ejoy2d_win_update(float dt); 20 | void ejoy2d_win_touch(int x, int y,int touch); 21 | void ejoy2d_win_rotate(int width, int height, float scale, int orient); 22 | void ejoy2d_win_resume(); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /engine/ejoy2d/geometry.lua: -------------------------------------------------------------------------------- 1 | local shader = require "ejoy2d.shader" 2 | local core = require "ejoy2d.geometry.c" 3 | 4 | -- define a shader 5 | local s = shader.define { 6 | name = "GEOMETRY", 7 | vs = [[ 8 | attribute vec4 position; 9 | attribute vec4 color; 10 | 11 | varying vec4 v_color; 12 | 13 | void main() { 14 | gl_Position = position + vec4(-1.0,1.0,0,0); 15 | v_color = color; 16 | } 17 | ]], 18 | fs = [[ 19 | varying vec4 v_color; 20 | 21 | void main() { 22 | gl_FragColor = v_color; 23 | } 24 | ]], 25 | } 26 | 27 | core.setprogram(shader.id "GEOMETRY") 28 | 29 | local geo = {} 30 | 31 | geo.line = assert(core.line) 32 | geo.box = assert(core.box) 33 | geo.polygon = assert(core.polygon) 34 | 35 | return geo 36 | -------------------------------------------------------------------------------- /engine/lib/dfont.h: -------------------------------------------------------------------------------- 1 | #ifndef dynamic_font_h 2 | #define dynamic_font_h 3 | #include 4 | 5 | struct dfont; 6 | 7 | struct dfont_rect { 8 | int x; 9 | int y; 10 | int w; 11 | int h; 12 | }; 13 | 14 | struct dfont * dfont_create(int width, int height); 15 | void dfont_release(struct dfont *); 16 | const struct dfont_rect * dfont_lookup(struct dfont *, int c, int font, int edge); 17 | const struct dfont_rect * dfont_insert(struct dfont *, int c, int font, int width, int height, int edge); 18 | void dfont_remove(struct dfont *, int c, int font, int edge); 19 | void dfont_flush(struct dfont *); 20 | void dfont_dump(struct dfont *); // for debug 21 | 22 | size_t dfont_data_size(int width, int height); 23 | void dfont_init(void* d, int width, int height); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /engine/lib/render/log.c: -------------------------------------------------------------------------------- 1 | #include "log.h" 2 | #include 3 | 4 | void 5 | log_init(struct log *log, FILE *f) { 6 | log->f = f; 7 | } 8 | 9 | /* 10 | void 11 | log_printf(struct log *log, const char * format, ...) { 12 | va_list ap; 13 | va_start(ap, format); 14 | vfprintf(log->f, format, ap); 15 | va_end(ap); 16 | fflush(log->f); 17 | } 18 | */ 19 | 20 | // for ejoy2d compatibility 21 | 22 | #include 23 | #include "platform_print.h" 24 | 25 | void 26 | log_printf(struct log *log,const char * format, ...) { 27 | if (format[0] == '!') { 28 | va_list ap; 29 | va_start(ap, format); 30 | pf_vprint(format+1, ap); 31 | va_end(ap); 32 | } else { 33 | va_list ap; 34 | va_start(ap, format); 35 | pf_vprint(format, ap); 36 | va_end(ap); 37 | exit(1); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /engine/lib/lejoy2dcore.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "screen.h" 6 | #include "shader.h" 7 | #include "label.h" 8 | #include "ejoy2dgame.h" 9 | 10 | static int 11 | lviewport(lua_State *L) { 12 | int w = luaL_checkinteger(L,1); 13 | int h = luaL_checkinteger(L,2); 14 | glViewport(0, 0, w,h); 15 | screen_init(w,h,1.0f); 16 | return 0; 17 | } 18 | 19 | static int 20 | lbeginframe(lua_State *L) { 21 | reset_drawcall_count(); 22 | return 0; 23 | } 24 | 25 | static int 26 | lendframe(lua_State *L) { 27 | shader_flush(); 28 | label_flush(); 29 | return 0; 30 | } 31 | 32 | int 33 | luaopen_ejoy2d_core(lua_State *L) { 34 | if ( glewInit() != GLEW_OK ) { 35 | return luaL_error(L, "init glew failed"); 36 | } 37 | ejoy2d_init(L); 38 | luaL_Reg l[] = { 39 | { "viewport", lviewport }, 40 | { "beginframe", lbeginframe }, 41 | { "endframe", lendframe }, 42 | { NULL, NULL }, 43 | }; 44 | luaL_newlib(L,l); 45 | 46 | return 1; 47 | } 48 | -------------------------------------------------------------------------------- /engine/lua/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 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 "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | #define MYINT(s) (s[0]-'0') 22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 23 | #define LUAC_FORMAT 0 /* this is the official format */ 24 | 25 | /* load one chunk; from lundump.c */ 26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, 27 | const char* name); 28 | 29 | /* dump one chunk; from ldump.c */ 30 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 31 | void* data, int strip); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /engine/lua/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /engine/lib/texture.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_TEXTURE_H 2 | #define EJOY_2D_TEXTURE_H 3 | 4 | #include "render.h" 5 | #include 6 | 7 | void texture_initrender(struct render *R); 8 | const char * texture_load(int id, enum TEXTURE_FORMAT type, int width, int height, void *buffer, int reduce); 9 | void texture_unload(int id); 10 | RID texture_glid(int id); 11 | int texture_coord(int id, float x, float y, uint16_t *u, uint16_t *v); 12 | void texture_clearall(); 13 | void texture_exit(); 14 | 15 | const char* texture_new_rt(int id, int width, int height); 16 | const char* texture_active_rt(int id); 17 | 18 | void texture_set_inv(int id, float invw, float invh); 19 | void texture_swap(int ida, int idb); 20 | void texture_size(int id, int *width, int *height); 21 | void texture_delete_framebuffer(int id); 22 | 23 | /// update content of texture 24 | /// width and height may not equal the original by design 25 | /// useful for some condition 26 | /// async texture load,for example, 27 | /// becasue we can first push a much more small avatar 28 | const char* texture_update(int id, int width, int height, void *buffer); 29 | 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /engine/lib/renderbuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_renderbuffer_h 2 | #define ejoy2d_renderbuffer_h 3 | 4 | #include 5 | #include "render.h" 6 | 7 | #define MAX_COMMBINE 1024 8 | 9 | struct vertex_pack { 10 | float vx; 11 | float vy; 12 | uint16_t tx; 13 | uint16_t ty; 14 | }; 15 | 16 | struct vertex { 17 | struct vertex_pack vp; 18 | uint8_t rgba[4]; 19 | uint8_t add[4]; 20 | }; 21 | 22 | struct quad { 23 | struct vertex p[4]; 24 | }; 25 | 26 | struct render_buffer { 27 | int object; 28 | int texid; 29 | RID vbid; 30 | struct quad vb[MAX_COMMBINE]; 31 | }; 32 | 33 | 34 | void renderbuffer_initrender(struct render *R); 35 | void renderbuffer_init(struct render_buffer *rb); 36 | void renderbuffer_upload(struct render_buffer *rb); 37 | void renderbuffer_unload(struct render_buffer *rb); 38 | 39 | int renderbuffer_add(struct render_buffer *rb, const struct vertex_pack vb[4], uint32_t color, uint32_t additive); 40 | 41 | static inline void renderbuffer_clear(struct render_buffer *rb) { 42 | rb->object = 0; 43 | } 44 | 45 | struct sprite; 46 | 47 | // 0 : ok, 1 full, -1 error (type must be picture) 48 | int renderbuffer_drawsprite(struct render_buffer *rb, struct sprite *s); 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /engine/lib/ejoy2dgame.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_LUASTATE_H 2 | #define EJOY_2D_LUASTATE_H 3 | 4 | #include 5 | 6 | struct game { 7 | lua_State *L; 8 | float real_time; 9 | float logic_time; 10 | }; 11 | 12 | 13 | struct game * ejoy2d_game(); 14 | lua_State * ejoy2d_lua_init(); 15 | void ejoy2d_game_exit(struct game *); 16 | void ejoy2d_close_lua(struct game *); 17 | lua_State * ejoy2d_game_lua(struct game *); 18 | void ejoy2d_handle_error(lua_State *L, const char *err_type, const char *msg); 19 | void ejoy2d_game_logicframe(int); 20 | void ejoy2d_game_start(struct game *); 21 | void ejoy2d_game_update(struct game *, float dt); 22 | void ejoy2d_game_drawframe(struct game *); 23 | int ejoy2d_game_touch(struct game *, int id, float x, float y, int status); 24 | void ejoy2d_game_gesture(struct game *, int type, 25 | double x1, double y1, double x2, double y2, int s); 26 | void 27 | ejoy2d_game_message(struct game* G,int id_, const char* state, const char* data, lua_Number n); 28 | void ejoy2d_game_pause(struct game* G); 29 | void ejoy2d_game_resume(struct game* G); 30 | 31 | void 32 | ejoy2d_call_lua(lua_State *L, int n, int r); 33 | 34 | void ejoy2d_init(lua_State *L); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /engine/ejoy2d/init.lua: -------------------------------------------------------------------------------- 1 | local shader = require "ejoy2d.shader" 2 | local fw = require "ejoy2d.framework" 3 | 4 | function fw.EJOY2D_INIT() 5 | -- shader.init() 6 | end 7 | 8 | shader.init() 9 | 10 | local ejoy2d = {} 11 | 12 | local touch = { 13 | "BEGIN", 14 | "END", 15 | "MOVE", 16 | "CANCEL" 17 | } 18 | 19 | local gesture = { 20 | "PAN", 21 | "TAP", 22 | "PINCH", 23 | "PRESS", 24 | "DOUBLE_TAP", 25 | } 26 | 27 | function ejoy2d.start(callback) 28 | fw.EJOY2D_UPDATE = assert(callback.update) 29 | fw.EJOY2D_DRAWFRAME = assert(callback.drawframe) 30 | 31 | fw.EJOY2D_TOUCH = function(x,y,what,id) 32 | return callback.touch(touch[what],x,y,id) 33 | end 34 | fw.EJOY2D_GESTURE = function(what, x1, y1, x2, y2, state) 35 | return callback.gesture(gesture[what], x1, y1, x2, y2, state) 36 | end 37 | fw.EJOY2D_MESSAGE = assert(callback.message) 38 | fw.EJOY2D_HANDLE_ERROR = assert(callback.handle_error) 39 | fw.EJOY2D_RESUME = assert(callback.on_resume) 40 | fw.EJOY2D_PAUSE = assert(callback.on_pause) 41 | fw.inject() 42 | end 43 | 44 | function ejoy2d.clear(color) 45 | return shader.clear(color) 46 | end 47 | 48 | function ejoy2d.define_shader(args) 49 | return shader.define(args) 50 | end 51 | 52 | return ejoy2d 53 | -------------------------------------------------------------------------------- /src/main.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | 5 | local shader = require "dawn_shader" 6 | local scene = require "dawn_scene" 7 | 8 | -- init 9 | local function init() 10 | math.randomseed(os.time()) 11 | 12 | pack.load { 13 | pattern = fw.WorkDir.."package/?", 14 | "dawn", 15 | } 16 | 17 | shader:init() 18 | scene:init() 19 | end 20 | 21 | init() 22 | 23 | -- game callback 24 | local game = {} 25 | 26 | function game.update() 27 | scene:update() 28 | end 29 | 30 | function game.drawframe() 31 | ej.clear(0xff000000) 32 | scene:draw() 33 | end 34 | 35 | local last_x 36 | function game.touch(what, x, y) 37 | if what == "BEGIN" then 38 | scene:pause_time(true) 39 | elseif what == "END" then 40 | scene:pause_time(false) 41 | elseif what == "MOVE" then 42 | scene:shift_time(x - last_x) 43 | end 44 | 45 | last_x = x 46 | end 47 | 48 | function game.message(id, state, data, n) 49 | if data == "LEFT" or data == "RIGHT" then 50 | scene:layout_sim(true) 51 | else 52 | scene:layout_sim(false) 53 | end 54 | end 55 | 56 | function game.handle_error(...) 57 | end 58 | 59 | function game.on_resume() 60 | end 61 | 62 | function game.on_pause() 63 | end 64 | 65 | ej.start(game) 66 | -------------------------------------------------------------------------------- /engine/lua/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 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 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_UTF8LIBNAME "utf8" 33 | LUAMOD_API int (luaopen_utf8) (lua_State *L); 34 | 35 | #define LUA_BITLIBNAME "bit32" 36 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 37 | 38 | #define LUA_MATHLIBNAME "math" 39 | LUAMOD_API int (luaopen_math) (lua_State *L); 40 | 41 | #define LUA_DBLIBNAME "debug" 42 | LUAMOD_API int (luaopen_debug) (lua_State *L); 43 | 44 | #define LUA_LOADLIBNAME "package" 45 | LUAMOD_API int (luaopen_package) (lua_State *L); 46 | 47 | 48 | /* open all previous libraries */ 49 | LUALIB_API void (luaL_openlibs) (lua_State *L); 50 | 51 | 52 | 53 | #if !defined(lua_assert) 54 | #define lua_assert(x) ((void)0) 55 | #endif 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /engine/lib/render/blendmode.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_blend_mode_h 2 | #define ejoy2d_blend_mode_h 3 | 4 | // for ejoy2d compatibility 5 | 6 | #define BLEND_GL_ZERO 0 7 | #define BLEND_GL_ONE 1 8 | #define BLEND_GL_SRC_COLOR 0x0300 9 | #define BLEND_GL_ONE_MINUS_SRC_COLOR 0x0301 10 | #define BLEND_GL_SRC_ALPHA 0x0302 11 | #define BLEND_GL_ONE_MINUS_SRC_ALPHA 0x0303 12 | #define BLEND_GL_DST_ALPHA 0x0304 13 | #define BLEND_GL_ONE_MINUS_DST_ALPHA 0x0305 14 | #define BLEND_GL_DST_COLOR 0x0306 15 | #define BLEND_GL_ONE_MINUS_DST_COLOR 0x0307 16 | #define BLEND_GL_SRC_ALPHA_SATURATE 0x0308 17 | 18 | #include "render.h" 19 | 20 | static enum BLEND_FORMAT 21 | blend_mode(int gl_mode) { 22 | switch(gl_mode) { 23 | case BLEND_GL_ZERO: 24 | return BLEND_ZERO; 25 | case BLEND_GL_ONE: 26 | return BLEND_ONE; 27 | case BLEND_GL_SRC_COLOR: 28 | return BLEND_SRC_COLOR; 29 | case BLEND_GL_ONE_MINUS_SRC_COLOR: 30 | return BLEND_ONE_MINUS_SRC_COLOR; 31 | case BLEND_GL_SRC_ALPHA: 32 | return BLEND_SRC_ALPHA; 33 | case BLEND_GL_ONE_MINUS_SRC_ALPHA: 34 | return BLEND_ONE_MINUS_SRC_ALPHA; 35 | case BLEND_GL_DST_ALPHA: 36 | return BLEND_DST_ALPHA; 37 | case BLEND_GL_ONE_MINUS_DST_ALPHA: 38 | return BLEND_ONE_MINUS_DST_ALPHA; 39 | case BLEND_GL_DST_COLOR: 40 | return BLEND_DST_COLOR; 41 | case BLEND_GL_ONE_MINUS_DST_COLOR: 42 | return BLEND_ONE_MINUS_DST_COLOR; 43 | case BLEND_GL_SRC_ALPHA_SATURATE: 44 | return BLEND_SRC_ALPHA_SATURATE; 45 | } 46 | return BLEND_DISABLE; 47 | } 48 | 49 | #endif -------------------------------------------------------------------------------- /engine/lua/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.56 2014/07/18 14:46:47 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 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) 16 | #define sizestring(s) sizelstring((s)->len) 17 | 18 | #define sizeludata(l) (sizeof(union UUdata) + (l)) 19 | #define sizeudata(u) sizeludata((u)->len) 20 | 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 40 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dawn 2 | This is a tech demo showing how to develop custom graphic effects in [ejoy2d](https://github.com/ejoy/ejoy2d). This project is inspired by HGE's demo [The Big Calm](http://hge.relishgames.com/doc/index.html?tutorials_tut08.html). 3 | 4 | Here is the screenshots: 5 | 6 | ![screenshots1](screenshots/ScreenShot1.png) 7 | ![screenshots2](screenshots/ScreenShot2.png) 8 | ![screenshots3](screenshots/ScreenShot3.png) 9 | 10 | ## Build 11 | ### 1. Export Resource 12 | * cd res 13 | * ./export.sh 14 | ### 2. Build App 15 | #### For IOS 16 | * open build/ios/dawn.xcodeproj with xcode 17 | * command + r 18 | #### For Android (on macosx) 19 | * install jdk, android sdk, android ndk, ant 20 | * connect device or launch simulator 21 | * cd build/android 22 | * ./build 23 | 24 | ## Install from market 25 | * IOS: not available currently 26 | * Android: [GooglePlay](https://play.google.com/store/apps/details?id=com.poags.dawn) 27 | 28 | **NOTICE: You can do everything with the source code but please do NOT submit compiled package to appstore or googleplay directly!** 29 | 30 | ## About ejoy2d 31 | Ejoy2d is a open source cross-platform 2d game engine. The engine is written in c but provides a efficient lua binding. Check the [homepage](https://github.com/ejoy/ejoy2d) for more information. 32 | 33 | ## About resource packing 34 | Ejoy2d does not support creating sprite from image file, so we must pack all assets to a package with specific format. [Simplepacker](https://github.com/skykapok/simplepacker) is a easy way to do this work. -------------------------------------------------------------------------------- /engine/lib/scissor.c: -------------------------------------------------------------------------------- 1 | #include "scissor.h" 2 | #include "screen.h" 3 | #include "shader.h" 4 | 5 | #include 6 | 7 | #define SCISSOR_MAX 8 8 | 9 | struct box { 10 | int x; 11 | int y; 12 | int width; 13 | int height; 14 | }; 15 | 16 | struct scissor { 17 | int depth; 18 | struct box s[SCISSOR_MAX]; 19 | }; 20 | 21 | static struct scissor S; 22 | 23 | void 24 | intersection(struct box * b, int * x, int * y, int * w, int * h) { 25 | int newx = b->x > *x ? b->x : *x; 26 | int newy = b->y > *y ? b->y : *y; 27 | 28 | int bx = b->x + b->width; 29 | int by = b->y + b->height; 30 | int ax = *x + *w; 31 | int ay = *y + *h; 32 | int neww = (bx > ax ? ax : bx) - newx; 33 | int newh = (by > ay ? ay : by) - newy; 34 | 35 | *x = newx; 36 | *y = newy; 37 | *w = neww; 38 | *h = newh; 39 | } 40 | 41 | void 42 | scissor_push(int x, int y, int w, int h) { 43 | assert(S.depth < SCISSOR_MAX); 44 | shader_flush(); 45 | if (S.depth == 0) { 46 | shader_scissortest(1); 47 | } 48 | 49 | if (S.depth >= 1) { 50 | intersection(&S.s[S.depth-1], &x, &y, &w, &h); 51 | } 52 | 53 | struct box * s = &S.s[S.depth++]; 54 | s->x = x; 55 | s->y = y; 56 | s->width = w; 57 | s->height = h; 58 | screen_scissor(s->x,s->y,s->width,s->height); 59 | } 60 | 61 | void 62 | scissor_pop() { 63 | assert(S.depth > 0); 64 | shader_flush(); 65 | --S.depth; 66 | if (S.depth == 0) { 67 | shader_scissortest(0); 68 | return; 69 | } 70 | struct box * s = &S.s[S.depth-1]; 71 | screen_scissor(s->x,s->y,s->width,s->height); 72 | } 73 | -------------------------------------------------------------------------------- /engine/lib/shader.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_SHADER_H 2 | #define EJOY_2D_SHADER_H 3 | 4 | #include "renderbuffer.h" 5 | #include "render.h" 6 | 7 | #include 8 | #include 9 | 10 | #define PROGRAM_DEFAULT -1 11 | #define PROGRAM_PICTURE 0 12 | #define PROGRAM_RENDERBUFFER 1 13 | #define PROGRAM_TEXT 2 14 | #define PROGRAM_TEXT_EDGE 3 15 | 16 | struct material; 17 | 18 | void shader_init(); 19 | void shader_load(int prog, const char *fs, const char *vs, int texture, const char ** texture_uniform_name); 20 | void shader_unload(); 21 | void shader_blend(int m1,int m2); 22 | void shader_defaultblend(); 23 | void shader_texture(int id, int channel); 24 | void shader_draw(const struct vertex_pack vb[4],uint32_t color,uint32_t additive); 25 | void shader_drawpolygon(int n, const struct vertex_pack *vb, uint32_t color, uint32_t additive); 26 | void shader_program(int n, struct material *); 27 | void shader_flush(); 28 | void shader_clear(unsigned long argb); 29 | int shader_version(); 30 | void shader_scissortest(int enable); 31 | 32 | int ejoy2d_shader(lua_State *L); 33 | 34 | void shader_drawbuffer(struct render_buffer * rb, float x, float y, float s); 35 | 36 | int shader_adduniform(int prog, const char * name, enum UNIFORM_FORMAT t); 37 | void shader_setuniform(int prog, int index, enum UNIFORM_FORMAT t, float *v); 38 | int shader_uniformsize(enum UNIFORM_FORMAT t); 39 | 40 | // these api may deprecated later 41 | void shader_reset(); 42 | void shader_mask(float x, float y); 43 | void reset_drawcall_count(); 44 | int drawcall_count(); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /engine/lua/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.12 2014/11/10 14:46:05 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 getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, 29 | const TValue *p2, 30 | const char *msg); 31 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, 32 | const TValue *p2); 33 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 34 | const TValue *p2); 35 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 36 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 37 | LUAI_FUNC void luaG_traceexec (lua_State *L); 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /engine/lib/label.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY2D_LABEL_H 2 | #define EJOY2D_LABEL_H 3 | 4 | #include "spritepack.h" 5 | #include "matrix.h" 6 | 7 | #define LABEL_ALIGN_LEFT 0 8 | #define LABEL_ALIGN_RIGHT 1 9 | #define LABEL_ALIGN_CENTER 2 10 | 11 | #define RL_COLOR 1 12 | #define RL_LINEFEED 2 13 | 14 | struct label_field { 15 | struct { 16 | uint32_t type:8; 17 | uint32_t start:12; 18 | uint32_t end:12; 19 | }; 20 | union { 21 | uint32_t color; 22 | int val; 23 | }; 24 | }; 25 | 26 | struct rich_text { 27 | int count; 28 | int width; 29 | int height; 30 | const char *text; 31 | struct label_field *fields; 32 | }; 33 | 34 | struct render; 35 | void label_initrender(struct render *R); 36 | void label_load(); 37 | void label_unload(); 38 | void label_flush(); 39 | 40 | void label_draw(const struct rich_text *rich, struct pack_label * l, struct srt *srt, const struct sprite_trans *arg); 41 | void label_size(const char * str, struct pack_label * l, int* width, int* height); 42 | int label_char_size(struct pack_label* l, const char* chr, int* width, int* height, int* unicode); 43 | uint32_t label_get_color(struct pack_label * l, const struct sprite_trans *arg); 44 | 45 | struct font_context { 46 | int w; 47 | int h; 48 | int ascent; 49 | void * font; 50 | void * dc; 51 | // int edge; 52 | }; 53 | 54 | void font_size(const char *str, int unicode, struct font_context * ctx); 55 | void font_glyph(const char * str, int unicode, void * buffer, struct font_context * ctx); 56 | void font_create(int font_size, struct font_context *ctx); 57 | void font_release(struct font_context *ctx); 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /engine/lib/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_MATRIX_H 2 | #define EJOY_2D_MATRIX_H 3 | 4 | struct matrix { 5 | /* The matrix format is : 6 | * 7 | * | m[0] m[1] 0 | 8 | * | m[2] m[3] 0 | 9 | * | m[4] m[5] 1 | 10 | * 11 | * The format of the coordinates of a point is: 12 | * 13 | * | x y 1 | 14 | * 15 | * So, if you want to transform a point p with a matrix m, do: 16 | * 17 | * p * m 18 | * 19 | */ 20 | int m[6]; 21 | }; 22 | 23 | static inline void 24 | matrix_mul(struct matrix *mm, const struct matrix *mm1, const struct matrix *mm2) { 25 | int *m = mm->m; 26 | const int *m1 = mm1->m; 27 | const int *m2 = mm2->m; 28 | m[0] = (m1[0] * m2[0] + m1[1] * m2[2]) /1024; 29 | m[1] = (m1[0] * m2[1] + m1[1] * m2[3]) /1024; 30 | m[2] = (m1[2] * m2[0] + m1[3] * m2[2]) /1024; 31 | m[3] = (m1[2] * m2[1] + m1[3] * m2[3]) /1024; 32 | m[4] = (m1[4] * m2[0] + m1[5] * m2[2]) /1024 + m2[4]; 33 | m[5] = (m1[4] * m2[1] + m1[5] * m2[3]) /1024 + m2[5]; 34 | } 35 | 36 | static inline void 37 | matrix_identity(struct matrix *mm) { 38 | int *mat = mm->m; 39 | mat[0] = 1024; 40 | mat[1] = 0; 41 | mat[2] = 0; 42 | mat[3] = 1024; 43 | mat[4] = 0; 44 | mat[5] = 0; 45 | } 46 | 47 | int matrix_inverse(const struct matrix *mm, struct matrix *mo); 48 | 49 | struct srt { 50 | int offx; 51 | int offy; 52 | int scalex; 53 | int scaley; 54 | int rot; 55 | }; 56 | 57 | void matrix_srt(struct matrix *mm, const struct srt *srt); 58 | void matrix_rot(struct matrix *m, int rot); 59 | void matrix_scale(struct matrix *m, int sx, int sy); 60 | void matrix_sr(struct matrix *mat, int sx, int sy, int d); 61 | void matrix_rs(struct matrix *mat, int sx, int sy, int d); 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /engine/lua/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.21 2014/10/25 11:50:46 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) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by 'runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /engine/lua/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 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 | /* test whether thread is in 'twups' list */ 22 | #define isintwups(L) (L->twups != L) 23 | 24 | 25 | /* 26 | ** Upvalues for Lua closures 27 | */ 28 | struct UpVal { 29 | TValue *v; /* points to stack or to its own value */ 30 | lu_mem refcount; /* reference counter */ 31 | union { 32 | struct { /* (when open) */ 33 | UpVal *next; /* linked list */ 34 | int touched; /* mark to avoid cycles with dead threads */ 35 | } open; 36 | TValue value; /* the value (when closed) */ 37 | } u; 38 | }; 39 | 40 | #define upisopen(up) ((up)->v != &(up)->u.value) 41 | 42 | 43 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 44 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); 45 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); 46 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); 47 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 48 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 49 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 50 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 51 | int pc); 52 | 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /engine/lua/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 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 zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i)) 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \ 41 | (buff)->buffsize, size), \ 42 | (buff)->buffsize = size) 43 | 44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 45 | 46 | 47 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 48 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 49 | void *data); 50 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ 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; /* reader function */ 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 | -------------------------------------------------------------------------------- /engine/lua/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.20 2014/09/04 18:15:29 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 gval(n) (&(n)->i_val) 15 | #define gnext(n) ((n)->i_key.nk.next) 16 | 17 | 18 | /* 'const' to avoid wrong writings that can mess up field 'next' */ 19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) 20 | 21 | #define wgkey(n) (&(n)->i_key.nk) 22 | 23 | #define invalidateTMcache(t) ((t)->flags = 0) 24 | 25 | 26 | /* returns the key, given the value of a table entry */ 27 | #define keyfromval(v) \ 28 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 29 | 30 | 31 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); 32 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, 33 | TValue *value); 34 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 35 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 36 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 37 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 38 | LUAI_FUNC Table *luaH_new (lua_State *L); 39 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, 40 | unsigned int nhsize); 41 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); 42 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 43 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 44 | LUAI_FUNC int luaH_getn (Table *t); 45 | 46 | 47 | #if defined(LUA_DEBUG) 48 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 49 | LUAI_FUNC int luaH_isdummy (Node *n); 50 | #endif 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /res/tool/utils.lua: -------------------------------------------------------------------------------- 1 | -- utils module 2 | local M = { 3 | logf = function (...) end 4 | } 5 | 6 | function M:_logf(...) 7 | print("[SCRIPT] "..string.format(...)) 8 | end 9 | 10 | function M:enable_log() 11 | self.logf = self._logf 12 | end 13 | 14 | function M:check_ext(file_name, ext) 15 | return string.find(file_name, ext, 1, true) == #file_name - #ext + 1 16 | end 17 | 18 | function M:trim_slash(path) 19 | if path[#path] == "\\" or path[#path] == "/" then 20 | return string.sub(path, 1, -2) 21 | else 22 | return path 23 | end 24 | end 25 | 26 | function M:_matrix_identity() 27 | return {1, 0, 0, 1, 0, 0} 28 | end 29 | 30 | -- ejoy2d uses a 2x3 row-major matrix (the third column is 0,0,1) 31 | function M:_matrix_multiply(m1, m2) 32 | return { 33 | m1[1]*m2[1] + m1[2]*m2[3], 34 | m1[1]*m2[2] + m1[2]*m2[4], 35 | m1[3]*m2[1] + m1[4]*m2[3], 36 | m1[3]*m2[2] + m1[4]*m2[4], 37 | m1[5]*m2[1] + m1[6]*m2[3] + m2[5], 38 | m1[5]*m2[2] + m1[6]*m2[4] + m2[6], 39 | } 40 | end 41 | 42 | function M:create_matrix(scale, rot, trans) 43 | local s = self:_matrix_identity() 44 | local r = self:_matrix_identity() 45 | local t = self:_matrix_identity() 46 | 47 | if scale then 48 | s[1] = scale[1] 49 | s[4] = scale[2] 50 | end 51 | 52 | if rot then 53 | local rad = math.rad(rot) -- rot is in degree 54 | local cos = math.cos(rad) 55 | local sin = math.sin(rad) 56 | r[1] = cos 57 | r[2] = sin 58 | r[3] = -sin 59 | r[4] = cos 60 | end 61 | 62 | if trans then 63 | t[5] = trans[1] 64 | t[6] = trans[2] 65 | end 66 | 67 | local ret = self:_matrix_multiply(self:_matrix_multiply(s, r), t) 68 | ret[1] = math.floor(ret[1] * 1024) -- ejoy2d uses "fix-point" number 69 | ret[2] = math.floor(ret[2] * 1024) 70 | ret[3] = math.floor(ret[3] * 1024) 71 | ret[4] = math.floor(ret[4] * 1024) 72 | ret[5] = math.floor(ret[5] * 16) 73 | ret[6] = math.floor(ret[6] * 16) 74 | 75 | return ret 76 | end 77 | 78 | return M -------------------------------------------------------------------------------- /engine/lua/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lzio_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "llimits.h" 18 | #include "lmem.h" 19 | #include "lstate.h" 20 | #include "lzio.h" 21 | 22 | 23 | int luaZ_fill (ZIO *z) { 24 | size_t size; 25 | lua_State *L = z->L; 26 | const char *buff; 27 | lua_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | lua_lock(L); 30 | if (buff == NULL || size == 0) 31 | return EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (luaZ_fill(z) == EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* luaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | /* ------------------------------------------------------------------------ */ 70 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 71 | if (n > buff->buffsize) { 72 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 73 | luaZ_resizebuffer(L, buff, n); 74 | } 75 | return buff->buffer; 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /engine/lua/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | /* 12 | ** If you embed Lua in your program and need to open the standard 13 | ** libraries, call luaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | ** 17 | ** You can also *preload* libraries, so that a later 'require' can 18 | ** open the library, which is already linked to the application. 19 | ** For that, do the following code: 20 | ** 21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 22 | ** lua_pushcfunction(L, luaopen_modname); 23 | ** lua_setfield(L, -2, modname); 24 | ** lua_pop(L, 1); // remove _PRELOAD table 25 | */ 26 | 27 | #include "lprefix.h" 28 | 29 | 30 | #include 31 | 32 | #include "lua.h" 33 | 34 | #include "lualib.h" 35 | #include "lauxlib.h" 36 | 37 | 38 | /* 39 | ** these libs are loaded by lua.c and are readily available to any Lua 40 | ** program 41 | */ 42 | static const luaL_Reg loadedlibs[] = { 43 | {"_G", luaopen_base}, 44 | {LUA_LOADLIBNAME, luaopen_package}, 45 | {LUA_COLIBNAME, luaopen_coroutine}, 46 | {LUA_TABLIBNAME, luaopen_table}, 47 | {LUA_IOLIBNAME, luaopen_io}, 48 | {LUA_OSLIBNAME, luaopen_os}, 49 | {LUA_STRLIBNAME, luaopen_string}, 50 | {LUA_MATHLIBNAME, luaopen_math}, 51 | {LUA_UTF8LIBNAME, luaopen_utf8}, 52 | {LUA_DBLIBNAME, luaopen_debug}, 53 | #if defined(LUA_COMPAT_BITLIB) 54 | {LUA_BITLIBNAME, luaopen_bit32}, 55 | #endif 56 | {NULL, NULL} 57 | }; 58 | 59 | 60 | LUALIB_API void luaL_openlibs (lua_State *L) { 61 | const luaL_Reg *lib; 62 | /* "require" functions from 'loadedlibs' and set results to global table */ 63 | for (lib = loadedlibs; lib->func; lib++) { 64 | luaL_requiref(L, lib->name, lib->func, 1); 65 | lua_pop(L, 1); /* remove lib */ 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /engine/lib/audio/oal_decode.c: -------------------------------------------------------------------------------- 1 | #include "oal_decode.h" 2 | #include 3 | 4 | 5 | int adl_decode_caf(lua_State* L); 6 | int adl_decode_mp3(lua_State* L); 7 | int adl_decode_hardware_ios(lua_State* L); 8 | int adl_decode_wav(lua_State* L); 9 | int adl_decode_tools(lua_State* L); 10 | 11 | 12 | 13 | static char _last_error[512] = {0}; 14 | void 15 | ad_error(const char* f, ...) { 16 | va_list args; 17 | va_start(args, f); 18 | vsnprintf(_last_error, sizeof(_last_error)-1, f, args); 19 | va_end(args); 20 | } 21 | 22 | void od_free_info(struct oal_info* info) { 23 | free(info->data); 24 | info->data = NULL; 25 | } 26 | 27 | 28 | const char* 29 | ad_last_error() { 30 | return _last_error; 31 | } 32 | 33 | static int 34 | _info_gc(lua_State* L) { 35 | struct oal_info* p = (struct oal_info*)lua_touserdata(L, 1); 36 | od_free_info(p); 37 | return 0; 38 | } 39 | 40 | static int 41 | _info_tostring(lua_State* L) { 42 | struct oal_info* p = (struct oal_info*)lua_touserdata(L, 1); 43 | char buffer[128] = {0}; 44 | sprintf(buffer, "type:%s format: %d freq: %d size:%d", p->type, p->format, p->freq, p->size); 45 | lua_pushstring(L, buffer); 46 | return 1; 47 | } 48 | 49 | 50 | int 51 | ad_new_info(lua_State* L, struct oal_info* info) { 52 | struct oal_info* p = (struct oal_info*)lua_newuserdata(L, sizeof(*info)); 53 | *p = *info; 54 | if(luaL_newmetatable(L, "oal_info")) { 55 | lua_pushcfunction(L, _info_gc); 56 | lua_setfield(L, -2, "__gc"); 57 | lua_pushcfunction(L, _info_tostring); 58 | lua_setfield(L, -2, "__tostring"); 59 | } 60 | 61 | lua_setmetatable(L, -2); 62 | return 1; 63 | } 64 | 65 | 66 | 67 | int 68 | luaopen_oal_decode(lua_State* L) { 69 | luaL_checkversion(L); 70 | luaL_Reg l[] = { 71 | {"decode_tools", adl_decode_tools}, 72 | {"decode_mp3", adl_decode_mp3}, 73 | {"decode_wav", adl_decode_wav}, 74 | {NULL, NULL}, 75 | }; 76 | 77 | luaL_newlib(L, l); 78 | adl_decode_hardware_ios(L); 79 | lua_setfield(L, -2, "decode_hardware_ios"); 80 | return 1; 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /engine/lua/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 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" and "ORDER OP" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with fast access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_MOD, 29 | TM_POW, 30 | TM_DIV, 31 | TM_IDIV, 32 | TM_BAND, 33 | TM_BOR, 34 | TM_BXOR, 35 | TM_SHL, 36 | TM_SHR, 37 | TM_UNM, 38 | TM_BNOT, 39 | TM_LT, 40 | TM_LE, 41 | TM_CONCAT, 42 | TM_CALL, 43 | TM_N /* number of elements in the enum */ 44 | } TMS; 45 | 46 | 47 | 48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 50 | 51 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 52 | 53 | #define ttypename(x) luaT_typenames_[(x) + 1] 54 | #define objtypename(x) ttypename(ttnov(x)) 55 | 56 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 57 | 58 | 59 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 60 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 61 | TMS event); 62 | LUAI_FUNC void luaT_init (lua_State *L); 63 | 64 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 65 | const TValue *p2, TValue *p3, int hasres); 66 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 67 | StkId res, TMS event); 68 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 69 | StkId res, TMS event); 70 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, 71 | const TValue *p2, TMS event); 72 | 73 | 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /engine/lua/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 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 | #if !defined(LUA_NOCVTN2S) 17 | #define cvt2str(o) ttisnumber(o) 18 | #else 19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */ 20 | #endif 21 | 22 | 23 | #if !defined(LUA_NOCVTS2N) 24 | #define cvt2num(o) ttisstring(o) 25 | #else 26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */ 27 | #endif 28 | 29 | 30 | #define tonumber(o,n) \ 31 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) 32 | 33 | #define tointeger(o,i) \ 34 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) 35 | 36 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) 37 | 38 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 39 | 40 | 41 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 42 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 43 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 44 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); 45 | LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); 46 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 47 | StkId val); 48 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 49 | StkId val); 50 | LUAI_FUNC void luaV_finishOp (lua_State *L); 51 | LUAI_FUNC void luaV_execute (lua_State *L); 52 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 53 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); 54 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); 55 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); 56 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /engine/lib/lrenderbuffer.c: -------------------------------------------------------------------------------- 1 | #include "sprite.h" 2 | #include "renderbuffer.h" 3 | #include "shader.h" 4 | 5 | #include 6 | #include 7 | 8 | 9 | static int 10 | ldelbuffer(lua_State *L) { 11 | struct render_buffer *rb = (struct render_buffer *)lua_touserdata(L, 1); 12 | renderbuffer_unload(rb); 13 | return 0; 14 | } 15 | 16 | static int 17 | laddsprite(lua_State *L) { 18 | struct render_buffer *rb = (struct render_buffer *)luaL_checkudata(L, 1, "renderbuffer"); 19 | struct sprite * spr = (struct sprite *)lua_touserdata(L, 2); 20 | if (spr == NULL) { 21 | return luaL_error(L, "Need sprite"); 22 | } 23 | int r = renderbuffer_drawsprite(rb, spr); 24 | if (r < 0) { 25 | return luaL_error(L, "Add failed"); 26 | } 27 | lua_pushboolean(L, r == 0); 28 | return 1; 29 | } 30 | 31 | static int 32 | lupload(lua_State *L) { 33 | struct render_buffer *rb = (struct render_buffer *)luaL_checkudata(L, 1, "renderbuffer"); 34 | renderbuffer_upload(rb); 35 | 36 | return 0; 37 | } 38 | 39 | static int 40 | ldrawbuffer(lua_State *L) { 41 | // todo: srt 42 | struct render_buffer *rb = (struct render_buffer *)luaL_checkudata(L, 1, "renderbuffer"); 43 | float x = luaL_checknumber(L, 2); 44 | float y = luaL_checknumber(L, 3); 45 | float scale = luaL_optnumber(L, 4, 1.0); 46 | shader_drawbuffer(rb, x * SCREEN_SCALE,y * SCREEN_SCALE,scale); 47 | return 0; 48 | } 49 | 50 | static int 51 | lnewbuffer(lua_State *L) { 52 | struct render_buffer *rb = (struct render_buffer *)lua_newuserdata(L, sizeof(*rb)); 53 | renderbuffer_init(rb); 54 | if (luaL_newmetatable(L, "renderbuffer")) { 55 | luaL_Reg l[] = { 56 | { "add", laddsprite }, 57 | { "upload", lupload }, 58 | { "draw", ldrawbuffer }, 59 | { NULL, NULL }, 60 | }; 61 | luaL_newlib(L, l); 62 | lua_setfield(L, -2, "__index"); 63 | lua_pushcfunction(L, ldelbuffer); 64 | lua_setfield(L, -2, "__gc"); 65 | } 66 | lua_setmetatable(L, -2); 67 | return 1; 68 | } 69 | 70 | int 71 | ejoy2d_renderbuffer(lua_State *L) { 72 | luaL_Reg l[] ={ 73 | { "new", lnewbuffer }, 74 | { NULL, NULL }, 75 | }; 76 | luaL_newlib(L,l); 77 | 78 | return 1; 79 | } 80 | -------------------------------------------------------------------------------- /engine/lib/screenshot.c: -------------------------------------------------------------------------------- 1 | #include "screenshot.h" 2 | #include "sprite.h" 3 | #include "shader.h" 4 | #include "texture.h" 5 | #include "opengl.h" 6 | #include 7 | #include 8 | #include 9 | 10 | static void 11 | _get_screenshot_pixels(int x, int y, int w, int h, unsigned char *pixels) { 12 | if (w <= 0 || h <= 0 || !pixels) { 13 | return; 14 | } 15 | 16 | glFinish(); 17 | glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 18 | } 19 | 20 | static void 21 | _fill_sprite_with_texure(int tex_id, struct sprite* s, int w, int h) { 22 | s->parent = NULL; 23 | s->type = TYPE_PICTURE; 24 | s->id = 0; 25 | s->t.mat = NULL; 26 | s->t.color = 0xffffffff; 27 | s->t.additive = 0; 28 | s->t.program = PROGRAM_DEFAULT; 29 | 30 | s->start_frame = 0; 31 | s->total_frame = 0; 32 | s->frame = 0; 33 | s->flags = 0; 34 | s->name = NULL; 35 | s->material = NULL; 36 | memset(&s->data, 0, sizeof(s->data)); 37 | 38 | struct pack_picture * pp = (struct pack_picture *) malloc(sizeof(*pp)); 39 | pp->n = 1; 40 | struct pack_quad * q = &pp->rect[0]; 41 | q->texid = tex_id; 42 | int w2 = w * 4; 43 | int h2 = h * 4; 44 | 45 | q->texture_coord[0] = 0; 46 | q->texture_coord[1] = 0; 47 | q->texture_coord[2] = 0xffff; 48 | q->texture_coord[3] = 0; 49 | q->texture_coord[4] = 0xffff; 50 | q->texture_coord[5] = 0xffff; 51 | q->texture_coord[6] = 0; 52 | q->texture_coord[7] = 0xffff; 53 | 54 | q->screen_coord[0] = -w2; 55 | q->screen_coord[1] = h2; 56 | q->screen_coord[2] = w2; 57 | q->screen_coord[3] = h2; 58 | q->screen_coord[4] = w2; 59 | q->screen_coord[5] = -h2; 60 | q->screen_coord[6] = -w2; 61 | q->screen_coord[7] = -h2; 62 | s->s.pic = pp; 63 | } 64 | 65 | int 66 | screenshot(int x, int y, int w, int h, int tex_id, struct sprite* spr, unsigned char* pixels) { 67 | if (!pixels || !spr) 68 | return -1; 69 | 70 | _get_screenshot_pixels(x, y, w, h, pixels); 71 | texture_load(tex_id, TEXTURE_RGBA8, w, h, pixels, 0); 72 | _fill_sprite_with_texure(tex_id, spr, w, h); 73 | return 0; 74 | } 75 | 76 | void 77 | release_screenshot(int tex_id) { 78 | if (tex_id) { 79 | texture_unload(tex_id); 80 | } 81 | } 82 | 83 | -------------------------------------------------------------------------------- /engine/lua/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /engine/lib/render/carray.c: -------------------------------------------------------------------------------- 1 | #include "carray.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include "array.h" 7 | 8 | // align to qword 9 | #define ALIGN(n) (((n) + 7) & ~7) 10 | 11 | struct array_node { 12 | struct array_node * next; 13 | }; 14 | 15 | int 16 | array_size(int n, int sz) { 17 | sz = ALIGN(sz); 18 | return n * sz; 19 | } 20 | 21 | void 22 | array_init(struct array *p, void * buffer, int n, int nsz) { 23 | int sz = ALIGN(nsz); 24 | char * ptr = (char *)buffer; 25 | int i; 26 | for (i=0;inext = node_next; 30 | } 31 | struct array_node * node = (struct array_node *)(ptr + (n-1)*sz); 32 | node->next = NULL; 33 | p->n = n; 34 | p->sz = sz; 35 | p->buffer = (char*)buffer; 36 | p->freelist = (struct array_node*)buffer; 37 | } 38 | 39 | void * 40 | array_alloc(struct array *p) { 41 | struct array_node * node = p->freelist; 42 | if (node == NULL) { 43 | return NULL; 44 | } 45 | p->freelist = node->next; 46 | memset(node, 0, p->sz); 47 | return node; 48 | } 49 | 50 | void 51 | array_free(struct array *p, void *v) { 52 | struct array_node * node = (struct array_node *)v; 53 | if (node) { 54 | node->next = p->freelist; 55 | p->freelist = node; 56 | } 57 | } 58 | 59 | int 60 | array_id(struct array *p, void *v) { 61 | if (v == NULL) 62 | return 0; 63 | int idx = ((char *)v - p->buffer) / p->sz; 64 | assert(idx >= 0 && idx < p->n); 65 | 66 | return idx + 1; 67 | } 68 | 69 | void * 70 | array_ref(struct array *p, int id) { 71 | if (id == 0) 72 | return NULL; 73 | --id; 74 | assert(id >= 0 && id < p->n); 75 | void * ptr = p->buffer + p->sz * id; 76 | return ptr; 77 | } 78 | 79 | void 80 | array_exit(struct array *p, void (*close)(void *p, void *ud), void *ud) { 81 | ARRAY(char, flag, p->n); 82 | memset(flag, 0, p->n); 83 | struct array_node * n = p->freelist; 84 | while (n) { 85 | int idx = array_id(p, n) - 1; 86 | flag[idx] = 1; 87 | n = n->next; 88 | } 89 | int i; 90 | for (i=0;in;i++) { 91 | if (flag[i] == 0) { 92 | struct array_node * n = (struct array_node *)array_ref(p, i + 1); 93 | close(n, ud); 94 | } 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /engine/lua/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lctype.h" 14 | 15 | #if !LUA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /engine/ejoy2d/music.lua: -------------------------------------------------------------------------------- 1 | local audio = require "audio" 2 | local ad = require "oal.decode" 3 | 4 | local M = { 5 | is_close = false, 6 | _cur_playing_file_path = false, 7 | _cur_loop = false, 8 | } 9 | 10 | local function _gen_ios_hd_func() 11 | local cur_file_path = false 12 | local hd_ios = ad.decode_hardware_ios 13 | local m = {} 14 | 15 | function m.load(file_path) 16 | hd_ios.load(file_path) 17 | cur_file_path = file_path 18 | end 19 | 20 | function m.play(file_path, loop) 21 | if file_path ~= cur_file_path then 22 | m.load(file_path) 23 | end 24 | hd_ios.play(loop or false) 25 | end 26 | 27 | function m.stop() 28 | hd_ios.stop() 29 | end 30 | return m 31 | end 32 | 33 | local function _gen_oal_hd_func() 34 | local music_group = audio:create_group() 35 | local music_handle = false 36 | local m = {} 37 | local cur_file_path = false 38 | 39 | function m.load(file_path) 40 | if file_path ~= cur_file_path then 41 | audio:unload(file_path) 42 | end 43 | audio:load(file_path) 44 | end 45 | 46 | function m.play(file_path, loop) 47 | if file_path ~= cur_file_path then 48 | m.load(file_path) 49 | end 50 | music_handle = music_group:add(file_path, loop) 51 | music_group:play(music_handle) 52 | end 53 | 54 | function m.stop() 55 | if music_handle then 56 | music_group:stop(music_handle) 57 | end 58 | end 59 | 60 | return m 61 | end 62 | 63 | 64 | local bg_t = ad.decode_hardware_ios and "ios_hd" or "oal" 65 | local _bg_music_handles = { 66 | ["ios_hd"] = _gen_ios_hd_func(), 67 | ["oal"] = _gen_oal_hd_func(), 68 | } 69 | 70 | local _cur_bg_handle = _bg_music_handles[bg_t] 71 | assert(_cur_bg_handle, bg_t) 72 | 73 | 74 | 75 | function M.load(file_path) 76 | _cur_bg_handle.load(file_path) 77 | end 78 | 79 | function M.play(file_path, loop) 80 | M._cur_playing_file_path = file_path 81 | M._cur_loop = loop 82 | 83 | if M.is_close then 84 | return 85 | end 86 | _cur_bg_handle.play(file_path, loop) 87 | end 88 | 89 | 90 | function M.stop() 91 | _cur_bg_handle.stop() 92 | end 93 | 94 | 95 | function M.open() 96 | local file_path = M._cur_playing_file_path 97 | local loop = M._cur_loop 98 | M.is_close = false 99 | if file_path then 100 | _cur_bg_handle.play(file_path, loop) 101 | end 102 | end 103 | 104 | function M.close() 105 | _cur_bg_handle.stop() 106 | M.is_close = true 107 | end 108 | 109 | 110 | return M 111 | -------------------------------------------------------------------------------- /engine/lib/screen.c: -------------------------------------------------------------------------------- 1 | #include "screen.h" 2 | #include "render.h" 3 | #include "spritepack.h" 4 | 5 | struct screen { 6 | int width; 7 | int height; 8 | float scale; 9 | float invw; 10 | float invh; 11 | }; 12 | 13 | static struct screen SCREEN; 14 | static struct render *R = NULL; 15 | 16 | void 17 | screen_initrender(struct render *r) { 18 | R = r; 19 | // for ejoy2d compatibility, ejoy2d may call screen_init before screen_initrender 20 | screen_init(SCREEN.width, SCREEN.height, SCREEN.scale); 21 | } 22 | 23 | void 24 | screen_init(float w, float h, float scale) { 25 | SCREEN.width = (int)w; 26 | SCREEN.height = (int)h; 27 | SCREEN.scale = scale; 28 | SCREEN.invw = 2.0f / SCREEN_SCALE / w; 29 | SCREEN.invh = -2.0f / SCREEN_SCALE / h; 30 | if (R) { 31 | render_setviewport(R, 0, 0, w * scale, h * scale ); 32 | } 33 | } 34 | 35 | void 36 | screen_trans(float *x, float *y) { 37 | *x *= SCREEN.invw; 38 | *y *= SCREEN.invh; 39 | } 40 | 41 | void 42 | screen_scissor(int x, int y, int w, int h) { 43 | y = SCREEN.height - y - h; 44 | if (x<0) { 45 | w += x; 46 | x = 0; 47 | } else if (x>SCREEN.width) { 48 | w=0; 49 | h=0; 50 | } 51 | if (y<0) { 52 | h += y; 53 | y = 0; 54 | } else if (y>SCREEN.height) { 55 | w=0; 56 | h=0; 57 | } 58 | if (w<=0 || h<=0) { 59 | w=0; 60 | h=0; 61 | } 62 | x *= SCREEN.scale; 63 | y *= SCREEN.scale; 64 | w *= SCREEN.scale; 65 | h *= SCREEN.scale; 66 | 67 | render_setscissor(R,x,y,w,h); 68 | } 69 | 70 | bool screen_is_visible(float x,float y) 71 | { 72 | return x >= 0.0f && x <= 2.0f && y>=-2.0f && y<= 0.0f; 73 | } 74 | bool screen_is_poly_invisible(const float* points,int len,int stride) 75 | { 76 | int i =0; 77 | // test left of x 78 | bool invisible = true; 79 | for(i =0; i < len && invisible;++i) 80 | { 81 | if(points[i*stride] >= 0.0f) 82 | invisible = false; 83 | } 84 | if(invisible) 85 | return true; 86 | 87 | // test right of axis x 88 | invisible = true; 89 | for(i =0; i < len && invisible;++i) 90 | { 91 | if(points[i*stride] <= 2.0f) 92 | invisible = false; 93 | } 94 | if(invisible) 95 | return true; 96 | 97 | // test above of axis y 98 | invisible = true; 99 | for(i =0; i < len && invisible;++i) 100 | { 101 | if(points[i*stride +1] >= -2.0f) 102 | invisible = false; 103 | } 104 | if(invisible) 105 | return true; 106 | 107 | // test below of axis y 108 | invisible = true; 109 | for(i =0; i < len && invisible;++i) 110 | { 111 | if(points[i*stride +1] <= 0.0f) 112 | invisible = false; 113 | } 114 | return invisible; 115 | } 116 | 117 | 118 | -------------------------------------------------------------------------------- /engine/lua/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.78 2014/10/29 15:38:24 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 | 17 | #if !defined(LUA_ENV) 18 | #define LUA_ENV "_ENV" 19 | #endif 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_AND = FIRST_RESERVED, TK_BREAK, 29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 32 | /* other terminal symbols */ 33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 34 | TK_SHL, TK_SHR, 35 | TK_DBCOLON, TK_EOS, 36 | TK_FLT, TK_INT, TK_NAME, TK_STRING 37 | }; 38 | 39 | /* number of reserved words */ 40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | lua_Integer i; 46 | TString *ts; 47 | } SemInfo; /* semantics information */ 48 | 49 | 50 | typedef struct Token { 51 | int token; 52 | SemInfo seminfo; 53 | } Token; 54 | 55 | 56 | /* state of the lexer plus state of the parser when shared by all 57 | functions */ 58 | typedef struct LexState { 59 | int current; /* current character (charint) */ 60 | int linenumber; /* input line counter */ 61 | int lastline; /* line of last token 'consumed' */ 62 | Token t; /* current token */ 63 | Token lookahead; /* look ahead token */ 64 | struct FuncState *fs; /* current function (parser) */ 65 | struct lua_State *L; 66 | ZIO *z; /* input stream */ 67 | Mbuffer *buff; /* buffer for tokens */ 68 | Table *h; /* to avoid collection/reuse strings */ 69 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 70 | TString *source; /* current source name */ 71 | TString *envn; /* environment variable name */ 72 | char decpoint; /* locale decimal point */ 73 | } LexState; 74 | 75 | 76 | LUAI_FUNC void luaX_init (lua_State *L); 77 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 78 | TString *source, int firstchar); 79 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 80 | LUAI_FUNC void luaX_next (LexState *ls); 81 | LUAI_FUNC int luaX_lookahead (LexState *ls); 82 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 83 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 84 | 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /engine/lua/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 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 | 17 | /* 18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where 19 | ** each element has size 'e'. In case of arithmetic overflow of the 20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because 21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). 22 | ** 23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof' 24 | ** comparison avoids a runtime comparison when overflow cannot occur. 25 | ** The compiler should be able to optimize the real test by itself, but 26 | ** when it does it, it may give a warning about "comparison is always 27 | ** false due to limited range of data type"; the +1 tricks the compiler, 28 | ** avoiding this warning but also this optimization.) 29 | */ 30 | #define luaM_reallocv(L,b,on,n,e) \ 31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ 32 | ? luaM_toobig(L) : cast_void(0)) , \ 33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 34 | 35 | /* 36 | ** Arrays of chars do not need any test 37 | */ 38 | #define luaM_reallocvchar(L,b,on,n) \ 39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) 40 | 41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) 44 | 45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 47 | #define luaM_newvector(L,n,t) \ 48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 49 | 50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 51 | 52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 53 | if ((nelems)+1 > (size)) \ 54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 55 | 56 | #define luaM_reallocvector(L, v,oldn,n,t) \ 57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 58 | 59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 60 | 61 | /* not to be called directly */ 62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 63 | size_t size); 64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 65 | size_t size_elem, int limit, 66 | const char *what); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /src/dawn_shader.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | 3 | local vs = [[ 4 | attribute vec4 position; 5 | attribute vec2 texcoord; 6 | attribute vec4 color; 7 | 8 | varying vec2 v_texcoord; 9 | varying vec4 v_color; 10 | 11 | void main() { 12 | gl_Position = position + vec4(-1,1,0,0); 13 | v_texcoord = texcoord; 14 | v_color = color; 15 | } 16 | ]] 17 | 18 | local sky_fs = [[ 19 | uniform sampler2D texture0; 20 | uniform vec4 far; 21 | uniform vec4 near; 22 | 23 | varying vec2 v_texcoord; 24 | varying vec4 v_color; 25 | 26 | void main() { 27 | float f = pow(v_texcoord.y, 0.6); 28 | gl_FragColor = mix(far, near, f); 29 | } 30 | ]] 31 | 32 | local sea_fs = [[ 33 | precision highp float; 34 | 35 | uniform sampler2D Texture0; 36 | uniform float t; 37 | uniform float t1; 38 | uniform float sx; 39 | uniform vec4 far; 40 | uniform vec4 near; 41 | uniform vec4 spec; 42 | uniform vec4 refl; 43 | 44 | varying vec2 v_texcoord; 45 | varying vec4 v_color; 46 | 47 | const vec2 tex_scale = vec2(0.5, 15.0); 48 | const vec2 noise_speed = vec2(0.0, -0.08); 49 | 50 | void main(void) 51 | { 52 | vec2 tc = vec2(v_texcoord.x, pow(v_texcoord.y, 0.1)); 53 | vec2 nc = fract(tc*tex_scale + t*noise_speed); 54 | 55 | vec4 noise = texture2D(Texture0, nc); 56 | float n = mix(noise.x, noise.y, t1); 57 | 58 | float w = (sin(tc.y*80.0 + n - t) + 1.0) * 0.5; 59 | w = abs(w - n*0.1); 60 | 61 | float x = n - pow(abs(tc.x-sx), 2.0) * 60.0; 62 | x = clamp(x, 0.0, 1.0); 63 | 64 | vec4 base = near * (1.0 + n*0.1); 65 | base.xyz += spec.xyz * (1.0 - pow(w, 0.15)); 66 | base.xyz += refl.xyz * (1.0 - pow(w, x)); 67 | gl_FragColor = mix(far, base, pow(tc.y, 3.0)); 68 | } 69 | ]] 70 | 71 | local glow_fs = [[ 72 | uniform sampler2D texture0; 73 | 74 | varying vec2 v_texcoord; 75 | varying vec4 v_color; 76 | 77 | void main() { 78 | vec4 tmp = texture2D(texture0, v_texcoord); 79 | gl_FragColor.xyz = clamp(tmp.xyz/tmp.w + v_color.xyz, 0.0, 1.0); 80 | gl_FragColor.w = 1.0; 81 | gl_FragColor *= tmp.w; 82 | } 83 | ]] 84 | 85 | local M = {} 86 | 87 | function M:init() 88 | self.v_sky = ej.define_shader( 89 | { 90 | name = "sky", 91 | fs = sky_fs, 92 | vs = vs, 93 | uniform = { 94 | { name = "far", type = "float4" }, 95 | { name = "near", type = "float4" }, 96 | } 97 | }) 98 | self.v_sea = ej.define_shader( 99 | { 100 | name = "sea", 101 | fs = sea_fs, 102 | vs = vs, 103 | uniform = { 104 | { name = "t", type = "float" }, 105 | { name = "t1", type = "float" }, 106 | { name = "sx", type = "float" }, 107 | { name = "far", type = "float4" }, 108 | { name = "near", type = "float4" }, 109 | { name = "spec", type = "float4" }, 110 | { name = "refl", type = "float4" }, 111 | } 112 | }) 113 | self.v_glow = ej.define_shader( 114 | { 115 | name = "glow", 116 | fs = glow_fs, 117 | vs = vs, 118 | }) 119 | end 120 | 121 | return M 122 | -------------------------------------------------------------------------------- /engine/lib/sprite.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_SPRITE_H 2 | #define EJOY_2D_SPRITE_H 3 | 4 | #include "spritepack.h" 5 | #include "matrix.h" 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #define SPRFLAG_INVISIBLE (1) 12 | #define SPRFLAG_MESSAGE (2) 13 | #define SPRFLAG_MULTIMOUNT (4) 14 | #define SPRFLAG_FORCE_INHERIT_FRAME (8) 15 | 16 | struct material; 17 | 18 | struct anchor_data { 19 | struct particle_system *ps; 20 | struct pack_picture *pic; 21 | struct matrix mat; 22 | }; 23 | 24 | struct sprite { 25 | struct sprite * parent; 26 | uint16_t type; 27 | uint16_t id; 28 | struct sprite_trans t; 29 | union { 30 | struct pack_animation *ani; 31 | struct pack_picture *pic; 32 | struct pack_polygon *poly; 33 | struct pack_label *label; 34 | struct pack_pannel *pannel; 35 | struct matrix *mat; 36 | } s; 37 | struct matrix mat; 38 | int start_frame; 39 | int total_frame; 40 | int frame; 41 | int flags; 42 | const char *name; // name for parent 43 | struct material *material; 44 | union { 45 | struct sprite * children[1]; 46 | struct rich_text * rich_text; 47 | int scissor; 48 | struct anchor_data *anchor; 49 | } data; 50 | }; 51 | 52 | struct sprite_trans * sprite_trans_mul(struct sprite_trans *a, struct sprite_trans *b, struct sprite_trans *t, struct matrix *tmp_matrix); 53 | void sprite_drawquad(struct pack_picture *picture, const struct srt *srt, const struct sprite_trans *arg); 54 | void sprite_drawpolygon(struct pack_polygon *poly, const struct srt *srt, const struct sprite_trans *arg); 55 | 56 | // sprite_size must be call before sprite_init 57 | int sprite_size(struct sprite_pack *pack, int id); 58 | void sprite_init(struct sprite *, struct sprite_pack * pack, int id, int sz); 59 | 60 | // return action frame number, -1 means action is not exist 61 | int sprite_action(struct sprite *, const char * action); 62 | 63 | void sprite_draw(struct sprite *, struct srt *srt); 64 | void sprite_draw_as_child(struct sprite *, struct srt *srt, struct matrix *mat, uint32_t color); 65 | struct sprite * sprite_test(struct sprite *, struct srt *srt, int x, int y); 66 | 67 | // return child index, -1 means not found 68 | int sprite_child(struct sprite *, const char * childname); 69 | int sprite_child_ptr(struct sprite *, struct sprite *child); 70 | // return sprite id in pack, -1 for end 71 | int sprite_component(struct sprite *, int index); 72 | const char * sprite_childname(struct sprite *, int index); 73 | int sprite_setframe(struct sprite *, int frame, bool force_child); 74 | void sprite_mount(struct sprite *, int index, struct sprite *); 75 | 76 | void sprite_aabb(struct sprite *s, struct srt *srt, bool world_aabb, int aabb[4]); 77 | int sprite_pos(struct sprite *s, struct srt *srt, struct matrix *m, int pos[2]); // todo: maybe unused, use sprite_matrix instead 78 | // calc the sprite's world matrix 79 | void sprite_matrix(struct sprite *s, struct matrix *mat); 80 | 81 | bool sprite_child_visible(struct sprite *s, const char * childname); 82 | int sprite_material_size(struct sprite *s); 83 | 84 | int ejoy2d_sprite(lua_State *L); 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /engine/lua/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.89 2014/11/02 19:33:33 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lmem_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lgc.h" 20 | #include "lmem.h" 21 | #include "lobject.h" 22 | #include "lstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** ('osize' is the old size, 'nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block 'p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ISO C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | l_noret luaM_toobig (lua_State *L) { 69 | luaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | global_State *g = G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | lua_assert((realosize == 0) == (block == NULL)); 82 | #if defined(HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | api_check( nsize > realosize, 89 | "realloc cannot fail when shrinking a block"); 90 | luaC_fullgc(L, 1); /* try to free some memory... */ 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /engine/ejoy2d/simplepackage.lua: -------------------------------------------------------------------------------- 1 | -- It's a simple sprite package warpper, use your own asset format instead. 2 | 3 | local ejoy2d = require "ejoy2d" 4 | local ppm = require "ejoy2d.ppm" 5 | local pack = require "ejoy2d.spritepack" 6 | local sprite = require "ejoy2d.sprite" 7 | 8 | -- This limit defined in texture.c 9 | local MAX_TEXTURE = 128 10 | 11 | local textures = {} 12 | local packages = {} 13 | 14 | local spack = {} 15 | local package_pattern 16 | 17 | local function require_tex(filename) 18 | local tex = #textures 19 | assert(tex < MAX_TEXTURE) 20 | table.insert(textures, filename) 21 | ppm.texture(tex,filename) 22 | return tex 23 | end 24 | 25 | function spack.path(pattern) 26 | package_pattern = pattern 27 | end 28 | 29 | local function realname(filename) 30 | assert(package_pattern, "Need a pattern") 31 | return string.gsub(package_pattern,"([^?]*)?([^?]*)","%1"..filename.."%2") 32 | end 33 | 34 | function spack.preload(packname) 35 | if packages[packname] then 36 | return packages[packname] 37 | end 38 | local p = {} 39 | local filename = realname(packname) 40 | p.meta = assert(pack.pack(dofile(filename .. ".lua"))) 41 | 42 | p.tex = {} 43 | for i=1,p.meta.texture do 44 | p.tex[i] = require_tex(filename .. "." .. i) 45 | end 46 | pack.init(packname, p.tex, p.meta) 47 | packages[packname] = p 48 | end 49 | 50 | function spack.preload_raw(packname) 51 | if packages[packname] then 52 | return packages[packname] 53 | end 54 | local p = {} 55 | local filename = realname(packname) 56 | local data = io.open(filename..".raw", "rb"):read("*a") 57 | p.meta = assert(pack.import(data)) 58 | 59 | p.tex = {} 60 | for i=1,p.meta.texture do 61 | p.tex[i] = require_tex(filename .. "." .. i) 62 | end 63 | pack.init(packname, p.tex, p.meta) 64 | packages[packname] = p 65 | end 66 | 67 | function ejoy2d.sprite(packname, name) 68 | if packages[packname] == nil then 69 | spack.preload(packname) 70 | end 71 | return sprite.new(packname, name) 72 | end 73 | 74 | function ejoy2d.load_texture(filename) 75 | return require_tex(filename) 76 | end 77 | 78 | function spack.load(tbl) 79 | spack.path(assert(tbl.pattern)) 80 | for _,v in ipairs(tbl) do 81 | spack.preload(v) 82 | collectgarbage "collect" 83 | end 84 | end 85 | 86 | function spack.load_raw(tbl) 87 | spack.path(assert(tbl.pattern)) 88 | for _,v in ipairs(tbl) do 89 | spack.preload_raw(v) 90 | end 91 | collectgarbage "collect" 92 | end 93 | 94 | function spack.texture(packname, index) 95 | if packages[packname] == nil then 96 | spack.preload(packname) 97 | end 98 | return packages[packname].tex[index or 1] 99 | end 100 | 101 | function spack.export(outdir, tbl) 102 | spack.path(assert(tbl.pattern)) 103 | for _, packname in ipairs(tbl) do 104 | print("packname ", packname, outdir, tbl.pattern) 105 | local filename = string.gsub(outdir..tbl.pattern, 106 | "([^?]*)?([^?]*)", "%1"..packname.."%2") 107 | print("spack.export ", filename.. ".raw") 108 | 109 | local meta = assert(pack.pack(dofile(filename .. ".lua"))) 110 | local output = pack.export(meta) 111 | 112 | local file = io.open(filename .. ".raw", "w+b") 113 | file:write(output) 114 | file:close() 115 | end 116 | end 117 | 118 | return spack 119 | -------------------------------------------------------------------------------- /engine/lua/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 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 (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, 28 | OPR_DIV, 29 | OPR_IDIV, 30 | OPR_BAND, OPR_BOR, OPR_BXOR, 31 | OPR_SHL, OPR_SHR, 32 | OPR_CONCAT, 33 | OPR_EQ, OPR_LT, OPR_LE, 34 | OPR_NE, OPR_GT, OPR_GE, 35 | OPR_AND, OPR_OR, 36 | OPR_NOBINOPR 37 | } BinOpr; 38 | 39 | 40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 41 | 42 | 43 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 44 | 45 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 46 | 47 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 48 | 49 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 50 | 51 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 52 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 53 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 54 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 55 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 56 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 57 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 58 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 59 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); 60 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 63 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 64 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 65 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 67 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 68 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 69 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 70 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 71 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 72 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 73 | LUAI_FUNC int luaK_jump (FuncState *fs); 74 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 75 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 76 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 77 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 78 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 79 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 80 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 81 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 82 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 83 | expdesc *v2, int line); 84 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 85 | 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /engine/lib/spritepack.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_SPRITE_PACK_H 2 | #define EJOY_2D_SPRITE_PACK_H 3 | 4 | #include 5 | #include 6 | 7 | #define TYPE_EMPTY 0 8 | #define TYPE_PICTURE 1 9 | #define TYPE_ANIMATION 2 10 | #define TYPE_POLYGON 3 11 | #define TYPE_LABEL 4 12 | #define TYPE_PANNEL 5 13 | #define TYPE_ANCHOR 6 14 | #define TYPE_MATRIX 7 15 | 16 | #define ANCHOR_ID 0xffff 17 | #define SCREEN_SCALE 16 18 | 19 | // binary package should reserve more bytes for 64bit platform 20 | #define PTR_SIZE_DIFF (8 - sizeof(void *)) 21 | #define SIZEOF_POINTER 8 22 | 23 | struct matrix; 24 | 25 | #define SIZEOF_MATRIX (sizeof(struct matrix)) 26 | 27 | struct pack_pannel { 28 | int width; 29 | int height; 30 | int scissor; 31 | }; 32 | 33 | #define SIZEOF_PANNEL (sizeof(struct pack_pannel)) 34 | 35 | struct pack_label { 36 | uint32_t color; 37 | int width; 38 | int height; 39 | int align; 40 | int size; 41 | int edge; 42 | int space_h; 43 | int space_w; 44 | int auto_scale; 45 | }; 46 | 47 | #define SIZEOF_LABEL (sizeof(struct pack_label)) 48 | 49 | struct pack_quad { 50 | int texid; 51 | uint16_t texture_coord[8]; 52 | int32_t screen_coord[8]; 53 | }; 54 | 55 | #define SIZEOF_QUAD (sizeof(struct pack_quad)) 56 | 57 | struct pack_picture { 58 | int n; 59 | struct pack_quad rect[1]; 60 | }; 61 | 62 | #define SIZEOF_PICTURE (sizeof(struct pack_picture) - sizeof(struct pack_quad)) 63 | 64 | struct pack_poly { 65 | uint16_t *texture_coord; 66 | int32_t *screen_coord; 67 | int texid; 68 | int n; 69 | }; 70 | 71 | #define SIZEOF_POLY (sizeof(struct pack_poly) + 2 * PTR_SIZE_DIFF) 72 | 73 | struct pack_polygon { 74 | int n; 75 | int _dummy; // unused: dummy for align to 64bit 76 | struct pack_poly poly[1]; 77 | }; 78 | 79 | #define SIZEOF_POLYGON (sizeof(struct pack_polygon) - sizeof(struct pack_poly)) 80 | 81 | struct sprite_trans { 82 | struct matrix * mat; 83 | uint32_t color; 84 | uint32_t additive; 85 | int program; 86 | int _dummy; // unused: dummy for align to 64bit 87 | }; 88 | 89 | #define SIZEOF_TRANS (sizeof(struct sprite_trans) + PTR_SIZE_DIFF) 90 | 91 | struct pack_part { 92 | struct sprite_trans t; 93 | int component_id; 94 | int touchable; 95 | }; 96 | 97 | #define SIZEOF_PART (sizeof(struct pack_part) + SIZEOF_TRANS - sizeof(struct sprite_trans)) 98 | 99 | struct pack_frame { 100 | struct pack_part *part; 101 | int n; 102 | int _dummy; // unused: dummy for align to 64bit 103 | }; 104 | 105 | #define SIZEOF_FRAME (sizeof(struct pack_frame) + PTR_SIZE_DIFF) 106 | 107 | struct pack_action { 108 | const char * name; 109 | int number; 110 | int start_frame; 111 | }; 112 | 113 | #define SIZEOF_ACTION (sizeof(struct pack_action) + PTR_SIZE_DIFF) 114 | 115 | struct pack_component { 116 | const char *name; 117 | int id; 118 | int _dummy; // unused: dummy for align to 64bit 119 | }; 120 | 121 | #define SIZEOF_COMPONENT (sizeof(struct pack_component) + PTR_SIZE_DIFF) 122 | 123 | struct pack_animation { 124 | struct pack_frame *frame; 125 | struct pack_action *action; 126 | int frame_number; 127 | int action_number; 128 | int component_number; 129 | int _dummy; // unused: dummy for align to 64bit 130 | struct pack_component component[1]; 131 | }; 132 | 133 | #define SIZEOF_ANIMATION (sizeof(struct pack_animation) + 2 * PTR_SIZE_DIFF - sizeof(struct pack_component)) 134 | 135 | struct sprite_pack { 136 | uint8_t * type; 137 | void ** data; 138 | int n; 139 | int _dummy; // unused: dummy for align to 64bit 140 | int tex[2]; 141 | }; 142 | 143 | #define SIZEOF_PACK (sizeof(struct sprite_pack) + 2 * PTR_SIZE_DIFF - 2 * sizeof(int)) 144 | 145 | int ejoy2d_spritepack(lua_State *L); 146 | void dump_pack(struct sprite_pack *pack); 147 | 148 | #endif 149 | -------------------------------------------------------------------------------- /engine/platform/winfw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "opengl.h" 5 | #include "lauxlib.h" 6 | 7 | #include "ejoy2dgame.h" 8 | #include "fault.h" 9 | #include "screen.h" 10 | #include "winfw.h" 11 | 12 | struct WINDOWGAME { 13 | struct game *game; 14 | int intouch; 15 | }; 16 | 17 | static struct WINDOWGAME *G = NULL; 18 | 19 | static const char * startscript = 20 | "local path, script, sw, sh, ss = ...\n" 21 | "local fw = require('ejoy2d.framework')\n" 22 | "fw.WorkDir = path..'/'\n" 23 | "fw.ScreenWidth, fw.ScreenHeight, fw.ScreenScale = sw, sh, ss\n" 24 | "assert(script, 'I need a script name')\n" 25 | "script = path..'/'..script\n" 26 | "package.path = './?.lua;./?/init.lua;'\n" 27 | "package.path = package.path..path..'/?.lua;'\n" 28 | "package.path = package.path..path..'/?/init.lua;'\n" 29 | "package.path = package.path..path..'/src/?.lua;'\n" 30 | "local f = loadfile(script)\n" 31 | "f(script)\n"; 32 | 33 | static struct WINDOWGAME * 34 | create_game() { 35 | struct WINDOWGAME * g = malloc(sizeof(*g)); 36 | g->game = ejoy2d_game(); 37 | g->intouch = 0; 38 | return g; 39 | } 40 | 41 | static int 42 | traceback(lua_State *L) { 43 | const char *msg = lua_tostring(L, 1); 44 | if (msg) { 45 | luaL_traceback(L, L, msg, 1); 46 | } else if (!lua_isnoneornil(L, 1)) { 47 | if (!luaL_callmeta(L, 1, "__tostring")) 48 | lua_pushliteral(L, "(no error message)"); 49 | } 50 | return 1; 51 | } 52 | 53 | void 54 | ejoy2d_win_init(int orix, int oriy, int width, int height, float scale, const char* folder) { 55 | G = create_game(); 56 | lua_State *L = ejoy2d_game_lua(G->game); 57 | lua_pushcfunction(L, traceback); 58 | int tb = lua_gettop(L); 59 | int err = luaL_loadstring(L, startscript); 60 | if (err) { 61 | const char *msg = lua_tostring(L,-1); 62 | fault("%s", msg); 63 | } 64 | 65 | lua_pushstring(L, folder); 66 | lua_pushstring(L, "src/main.lua"); 67 | lua_pushnumber(L, width); 68 | lua_pushnumber(L, height); 69 | lua_pushnumber(L, scale); 70 | 71 | err = lua_pcall(L, 5, 0, tb); 72 | if (err) { 73 | const char *msg = lua_tostring(L,-1); 74 | fault("%s", msg); 75 | } 76 | 77 | lua_pop(L,1); 78 | 79 | screen_init(width,height,scale); 80 | ejoy2d_game_start(G->game); 81 | } 82 | 83 | void 84 | ejoy2d_win_release() { 85 | ejoy2d_game_exit(G->game); 86 | } 87 | 88 | void 89 | ejoy2d_win_update(float dt) { 90 | ejoy2d_game_update(G->game, dt); 91 | } 92 | 93 | void 94 | ejoy2d_win_frame() { 95 | ejoy2d_game_drawframe(G->game); 96 | } 97 | 98 | void 99 | ejoy2d_win_resume(){ 100 | ejoy2d_game_resume(G->game); 101 | } 102 | 103 | void 104 | ejoy2d_win_touch(int x, int y,int touch) { 105 | switch (touch) { 106 | case TOUCH_BEGIN: 107 | G->intouch = 1; 108 | break; 109 | case TOUCH_END: 110 | G->intouch = 0; 111 | break; 112 | case TOUCH_MOVE: 113 | if (!G->intouch) { 114 | return; 115 | } 116 | break; 117 | } 118 | // windows only support one touch id (0) 119 | int id = 0; 120 | ejoy2d_game_touch(G->game, id, x,y,touch); 121 | } 122 | 123 | void 124 | ejoy2d_win_rotate(int width, int height, float scale, int orient) { 125 | screen_init(width, height, scale); 126 | 127 | switch (orient) { 128 | case ORIENT_UP: 129 | ejoy2d_game_message(G->game, 0, NULL, "UP", 0); 130 | break; 131 | 132 | case ORIENT_DOWN: 133 | ejoy2d_game_message(G->game, 0, NULL, "DOWN", 0); 134 | break; 135 | 136 | case ORIENT_LEFT: 137 | ejoy2d_game_message(G->game, 0, NULL, "LEFT", 0); 138 | break; 139 | 140 | case ORIENT_RIGHT: 141 | ejoy2d_game_message(G->game, 0, NULL, "RIGHT", 0); 142 | break; 143 | 144 | default: 145 | break; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /engine/lib/audio/decode/ad_mp3.c: -------------------------------------------------------------------------------- 1 | #include "../oal_decode.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | #ifdef SUPPORT_AUDIO_MP3 11 | #include 12 | 13 | static struct oal_info* 14 | _get_info(mpg123_handle* handle, struct oal_info* out_info) { 15 | int channels = 0; 16 | int encoding = 0; 17 | long rate = 0; 18 | int err = mpg123_getformat(handle, &rate, &channels, &encoding); 19 | if (MPG123_OK != err) { 20 | ad_error("mpg123_getformat error status[%d]", err); 21 | return NULL; 22 | } 23 | ALsizei size = mpg123_length(handle); 24 | if (size == MPG123_ERR) { 25 | ad_error("mpg123_length error"); 26 | return NULL; 27 | } 28 | ALsizei freq = rate; 29 | ALsizei format; 30 | if (encoding == MPG123_ENC_UNSIGNED_8) { 31 | format = (channels==1)?(AL_FORMAT_MONO8):(AL_FORMAT_STEREO8); 32 | } else { 33 | format = (channels==1)?(AL_FORMAT_MONO16):(AL_FORMAT_STEREO16); 34 | } 35 | 36 | out_info->size = size; 37 | out_info->format = format; 38 | out_info->freq =freq; 39 | return out_info; 40 | } 41 | 42 | static mpg123_handle* 43 | _get_handle() { 44 | static mpg123_handle* handle = NULL; 45 | if(!handle) { 46 | mpg123_init(); 47 | int err=0; 48 | handle = mpg123_new(NULL, &err); 49 | if(MPG123_OK != mpg123_format(handle, 44100, MPG123_MONO | MPG123_STEREO, 50 | MPG123_ENC_UNSIGNED_8 | MPG123_ENC_SIGNED_16)) { 51 | mpg123_delete(handle); 52 | handle = NULL; 53 | } 54 | } 55 | return handle; 56 | } 57 | 58 | static void* 59 | _read(mpg123_handle* handle, size_t size, size_t *out_done) { 60 | unsigned char* head = malloc(size); 61 | unsigned char* buffer = head; 62 | size_t cap = size; 63 | *out_done = 0; 64 | 65 | do{ 66 | size_t _read = 0; 67 | int err = mpg123_read(handle, buffer, cap, &_read); 68 | *out_done += _read; 69 | if(err != MPG123_OK) { 70 | if(err != MPG123_DONE) { 71 | free(buffer); 72 | return NULL; 73 | } 74 | break; 75 | }else { 76 | size_t new_size = size*2; 77 | head = realloc(head, new_size); 78 | buffer = head + size; 79 | cap = size; 80 | size = new_size; 81 | } 82 | }while(true); 83 | return head; 84 | } 85 | 86 | static bool 87 | _decode_mp3(const char* filepath, struct oal_info* out) { 88 | bool ret = false; 89 | mpg123_handle* handle = _get_handle(); 90 | if(!handle){ 91 | ad_error("cannot set specified mpg123 format, file: %s", filepath); 92 | goto EXIT; 93 | } 94 | 95 | if(MPG123_OK != mpg123_open(handle, filepath)) { 96 | ad_error("open file: %s error.", filepath); 97 | goto EXIT; 98 | } 99 | 100 | if(!_get_info(handle, out)) { 101 | mpg123_close(handle); 102 | goto EXIT; 103 | } 104 | 105 | size_t size = out->size; 106 | if(out->format == AL_FORMAT_MONO16 || out->format == AL_FORMAT_STEREO16) { 107 | size *= 2; 108 | } 109 | 110 | size_t done = 0; 111 | unsigned char* buffer = _read(handle, size, &done); 112 | if(!buffer) { 113 | ad_error("mpg123_read error: %s", filepath); 114 | goto EXIT; 115 | }else { 116 | strcpy(out->type, "mp3"); 117 | out->data = buffer; 118 | out->size = done; 119 | } 120 | 121 | mpg123_close(handle); 122 | ret = true; 123 | EXIT: 124 | return ret; 125 | } 126 | 127 | #else 128 | static bool 129 | _decode_mp3(const char* filepath, struct oal_info* out) { 130 | ad_error("mp3 not support"); 131 | return false; 132 | } 133 | #endif 134 | 135 | 136 | int 137 | adl_decode_mp3(lua_State* L) { 138 | const char* file = lua_tostring(L, 1); 139 | struct oal_info out = {0}; 140 | if(_decode_mp3(file, &out)){ 141 | return ad_new_info(L, &out); 142 | } else { 143 | luaL_error(L, ad_last_error()); 144 | } 145 | return 0; 146 | } 147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /engine/lua/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.74 2014/10/25 11:50:46 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 | VKFLT, /* nval = numerical float value */ 26 | VKINT, /* nval = numerical integer value */ 27 | VNONRELOC, /* info = result register */ 28 | VLOCAL, /* info = local register */ 29 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 30 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 31 | VJMP, /* info = instruction pc */ 32 | VRELOCABLE, /* info = instruction pc */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | 38 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 39 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 40 | 41 | typedef struct expdesc { 42 | expkind k; 43 | union { 44 | struct { /* for indexed variables (VINDEXED) */ 45 | short idx; /* index (R/K) */ 46 | lu_byte t; /* table (register or upvalue) */ 47 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 48 | } ind; 49 | int info; /* for generic use */ 50 | lua_Number nval; /* for VKFLT */ 51 | lua_Integer ival; /* for VKINT */ 52 | } u; 53 | int t; /* patch list of 'exit when true' */ 54 | int f; /* patch list of 'exit when false' */ 55 | } expdesc; 56 | 57 | 58 | /* description of active local variable */ 59 | typedef struct Vardesc { 60 | short idx; /* variable index in stack */ 61 | } Vardesc; 62 | 63 | 64 | /* description of pending goto statements and label statements */ 65 | typedef struct Labeldesc { 66 | TString *name; /* label identifier */ 67 | int pc; /* position in code */ 68 | int line; /* line where it appeared */ 69 | lu_byte nactvar; /* local level where it appears in current block */ 70 | } Labeldesc; 71 | 72 | 73 | /* list of labels or gotos */ 74 | typedef struct Labellist { 75 | Labeldesc *arr; /* array */ 76 | int n; /* number of entries in use */ 77 | int size; /* array size */ 78 | } Labellist; 79 | 80 | 81 | /* dynamic structures used by the parser */ 82 | typedef struct Dyndata { 83 | struct { /* list of active local variables */ 84 | Vardesc *arr; 85 | int n; 86 | int size; 87 | } actvar; 88 | Labellist gt; /* list of pending gotos */ 89 | Labellist label; /* list of active labels */ 90 | } Dyndata; 91 | 92 | 93 | /* control of blocks */ 94 | struct BlockCnt; /* defined in lparser.c */ 95 | 96 | 97 | /* state needed to generate code for a given function */ 98 | typedef struct FuncState { 99 | Proto *f; /* current function header */ 100 | struct FuncState *prev; /* enclosing function */ 101 | struct LexState *ls; /* lexical state */ 102 | struct BlockCnt *bl; /* chain of current blocks */ 103 | int pc; /* next position to code (equivalent to 'ncode') */ 104 | int lasttarget; /* 'label' of last 'jump label' */ 105 | int jpc; /* list of pending jumps to 'pc' */ 106 | int nk; /* number of elements in 'k' */ 107 | int np; /* number of elements in 'p' */ 108 | int firstlocal; /* index of first local var (in Dyndata array) */ 109 | short nlocvars; /* number of elements in 'f->locvars' */ 110 | lu_byte nactvar; /* number of active local variables */ 111 | lu_byte nups; /* number of upvalues */ 112 | lu_byte freereg; /* first free register */ 113 | } FuncState; 114 | 115 | 116 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 117 | Dyndata *dyd, const char *name, int firstchar); 118 | 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /engine/ejoy2d/particle.lua: -------------------------------------------------------------------------------- 1 | local debug = debug 2 | local ej = require "ejoy2d" 3 | local c = require "ejoy2d.particle.c" 4 | local shader = require "ejoy2d.shader" 5 | local pack = require "ejoy2d.simplepackage" 6 | local fw = require "ejoy2d.framework" 7 | local matrix = require "ejoy2d.matrix" 8 | local math = require "math" 9 | 10 | local particle_configs = {} 11 | local particle_group_configs = {} 12 | 13 | local particle = {} 14 | 15 | local particle_meta = {__index = {mat = {}, col = {}}} 16 | 17 | function particle_meta.__index:update(dt) 18 | if not self.is_active then return end 19 | 20 | self.is_active = false 21 | loop_active = false 22 | for _, v in ipairs(self.particles) do 23 | local visible = self:is_particle_visible(v) 24 | if visible then 25 | if not v.is_visible then 26 | v.is_visible = true 27 | c.reset(v.particle) 28 | end 29 | local active = c.update(v.particle, dt, matrix(v.anchor.world_matrix), v.edge) 30 | 31 | self.is_active = active or self.is_active 32 | loop_active = loop_active or (self.is_active and v.is_loop or false) 33 | else 34 | if v.is_visible then 35 | v.is_visible = false 36 | end 37 | end 38 | end 39 | 40 | if self.group.frame_count > 1 then 41 | local stay_last = false 42 | local last_frame = self.group.frame >= self.group.frame_count - 1 43 | if self.is_active then 44 | if last_frame then 45 | if loop_active then 46 | stay_last = true 47 | self.group.frame = self.group.frame_count - 1 48 | else 49 | self.is_active = false 50 | end 51 | end 52 | else 53 | if not last_frame then 54 | self.is_active = true 55 | end 56 | end 57 | 58 | --print(self.group.frame, self.group.frame_count, stay_last, last_frame, loop_active) 59 | if not stay_last and not last_frame then 60 | self.float_frame = self.float_frame + fw.AnimationFramePerFrame 61 | self.group.frame = self.float_frame 62 | end 63 | end 64 | end 65 | 66 | function particle_meta.__index:reset() 67 | self.is_active = true 68 | self.group.frame = 0 69 | self.float_frame = 0 70 | for _, v in ipairs(self.particles) do 71 | v.is_visible = false 72 | end 73 | end 74 | 75 | function particle_meta.__index:is_particle_visible(particle) 76 | return self.group:child_visible(particle.anchor.name) 77 | end 78 | 79 | function particle.preload(config_path) 80 | particle_configs = dofile(config_path.."_particle_config.lua") 81 | end 82 | 83 | local function new_single(name, anchor) 84 | local config = rawget(particle_configs, name) 85 | assert(config ~= nil, "particle not exists:"..name) 86 | local texid = config.texId 87 | local cobj = c.new(config) 88 | anchor.visible = true 89 | 90 | if cobj then 91 | local sprite = ej.sprite("particle", texid) 92 | local x, y, w, h = sprite:aabb() 93 | local edge = 2 * math.min(w, h) 94 | anchor:anchor_particle(cobj, sprite) 95 | return {particle = cobj, 96 | sprite = sprite, 97 | edge = edge, 98 | src_blend = config.blendFuncSource, 99 | dst_blend = config.blendFuncDestination, 100 | anchor = anchor, 101 | is_loop = config.duration < 0, 102 | emit_in_world = config.positionType == 2, 103 | name = name 104 | } 105 | end 106 | end 107 | 108 | function particle.new(name, callback) 109 | 110 | local group = ej.sprite("particle", name) 111 | local config = table.pack(group:children_name()) 112 | local particles = {} 113 | local loop = false 114 | for _, v in ipairs(config) do 115 | local anchor = group:fetch(v) 116 | local spr = new_single(v, anchor) 117 | rawset(particles, #particles+1, spr) 118 | -- group:mount(v, spr.sprite) 119 | loop = loop or spr.is_loop 120 | end 121 | return debug.setmetatable({group=group, 122 | is_active = true, 123 | is_visible = false, 124 | particles = particles, 125 | end_callback = callback, 126 | is_loop = loop, 127 | float_frame = 0, 128 | }, particle_meta) 129 | end 130 | 131 | return particle 132 | -------------------------------------------------------------------------------- /engine/lib/lgeometry.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "shader.h" 7 | #include "screen.h" 8 | #include "renderbuffer.h" 9 | #include "spritepack.h" 10 | #include "array.h" 11 | 12 | static int PROGRAM = 0; 13 | 14 | static int 15 | lsetprogram(lua_State *L) { 16 | PROGRAM = luaL_checkinteger(L,1); 17 | return 0; 18 | } 19 | 20 | static uint32_t 21 | convert_color(uint32_t c) { 22 | uint32_t alpha = (c >> 24) & 0xff; 23 | if (alpha == 0xff) { 24 | return c; 25 | } 26 | if (alpha == 0) { 27 | return c | 0xff000000; 28 | } 29 | uint32_t red = (c >> 16) & 0xff; 30 | uint32_t green = (c >> 8) & 0xff; 31 | uint32_t blue = (c) & 0xff; 32 | red = red * alpha / 255; 33 | green = green * alpha / 255; 34 | blue = blue * alpha / 255; 35 | 36 | return alpha << 24 | red << 16 | green << 8 | blue; 37 | } 38 | 39 | /* 40 | float[4] endpointer x1,y1,x2,y2 41 | uint32_t color 42 | */ 43 | static int 44 | lline(lua_State *L) { 45 | float x1 = luaL_checknumber(L, 1) * SCREEN_SCALE; 46 | float y1 = luaL_checknumber(L, 2) * SCREEN_SCALE; 47 | float x2 = luaL_checknumber(L, 3) * SCREEN_SCALE; 48 | float y2 = luaL_checknumber(L, 4) * SCREEN_SCALE; 49 | uint32_t color = convert_color(luaL_checkinteger(L, 5)); 50 | struct vertex_pack vp[4]; 51 | vp[0].vx = x1; 52 | vp[0].vy = y1; 53 | vp[1].vx = x2; 54 | vp[1].vy = y2; 55 | if (abs(x1-x2) > abs(y1-y2)) { 56 | vp[2].vx = x2; 57 | vp[2].vy = y2+SCREEN_SCALE; 58 | vp[3].vx = x1; 59 | vp[3].vy = y1+SCREEN_SCALE; 60 | } else { 61 | vp[2].vx = x2+SCREEN_SCALE; 62 | vp[2].vy = y2; 63 | vp[3].vx = x1+SCREEN_SCALE; 64 | vp[3].vy = y1; 65 | } 66 | 67 | int i; 68 | for (i=0;i<4;i++) { 69 | vp[i].tx = 0; 70 | vp[i].ty = 0; 71 | screen_trans(&vp[i].vx, &vp[i].vy); 72 | } 73 | 74 | shader_program(PROGRAM, NULL); 75 | shader_draw(vp, color, 0); 76 | 77 | return 0; 78 | } 79 | 80 | /* 81 | float x,y 82 | float w,h 83 | uint32_t color 84 | */ 85 | static int 86 | lbox(lua_State *L) { 87 | float x = luaL_checknumber(L, 1) * SCREEN_SCALE; 88 | float y = luaL_checknumber(L, 2) * SCREEN_SCALE; 89 | float w = luaL_checknumber(L, 3) * SCREEN_SCALE; 90 | float h = luaL_checknumber(L, 4) * SCREEN_SCALE; 91 | uint32_t color = convert_color(luaL_checkinteger(L, 5)); 92 | struct vertex_pack vp[4]; 93 | vp[0].vx = x; 94 | vp[0].vy = y; 95 | vp[1].vx = x+w; 96 | vp[1].vy = y; 97 | vp[2].vx = x+w; 98 | vp[2].vy = y+h; 99 | vp[3].vx = x; 100 | vp[3].vy = y+h; 101 | 102 | int i; 103 | for (i=0;i<4;i++) { 104 | vp[i].tx = 0; 105 | vp[i].ty = 0; 106 | screen_trans(&vp[i].vx, &vp[i].vy); 107 | } 108 | 109 | shader_program(PROGRAM, NULL); 110 | shader_draw(vp, color, 0); 111 | 112 | return 0; 113 | } 114 | 115 | 116 | /* 117 | table float[] 118 | uint32_t color 119 | */ 120 | 121 | static int 122 | lpolygon(lua_State *L) { 123 | luaL_checktype(L, 1, LUA_TTABLE); 124 | uint32_t color = convert_color(luaL_checkinteger(L, 2)); 125 | int n = lua_rawlen(L, 1); 126 | int point = n/2; 127 | if (point * 2 != n) { 128 | return luaL_error(L, "Invalid polygon"); 129 | } 130 | ARRAY(struct vertex_pack, vb, point); 131 | int i; 132 | for (i=0;i 14 | 15 | #include "lopcodes.h" 16 | 17 | 18 | /* ORDER OP */ 19 | 20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 21 | "MOVE", 22 | "LOADK", 23 | "LOADKX", 24 | "LOADBOOL", 25 | "LOADNIL", 26 | "GETUPVAL", 27 | "GETTABUP", 28 | "GETTABLE", 29 | "SETTABUP", 30 | "SETUPVAL", 31 | "SETTABLE", 32 | "NEWTABLE", 33 | "SELF", 34 | "ADD", 35 | "SUB", 36 | "MUL", 37 | "MOD", 38 | "POW", 39 | "DIV", 40 | "IDIV", 41 | "BAND", 42 | "BOR", 43 | "BXOR", 44 | "SHL", 45 | "SHR", 46 | "UNM", 47 | "BNOT", 48 | "NOT", 49 | "LEN", 50 | "CONCAT", 51 | "JMP", 52 | "EQ", 53 | "LT", 54 | "LE", 55 | "TEST", 56 | "TESTSET", 57 | "CALL", 58 | "TAILCALL", 59 | "RETURN", 60 | "FORLOOP", 61 | "FORPREP", 62 | "TFORCALL", 63 | "TFORLOOP", 64 | "SETLIST", 65 | "CLOSURE", 66 | "VARARG", 67 | "EXTRAARG", 68 | NULL 69 | }; 70 | 71 | 72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 73 | 74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 75 | /* T A B C mode opcode */ 76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ 96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ 97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ 98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ 99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ 100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ 103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 123 | }; 124 | 125 | -------------------------------------------------------------------------------- /engine/lib/audio/decode/ad_hardware_mac_ios.m: -------------------------------------------------------------------------------- 1 | #include "../oal_decode.h" 2 | 3 | #include 4 | 5 | #ifdef SUPPORT_AUDIO_HARDWARE_MAC_IOS 6 | #import 7 | 8 | @class ADAudioSource; 9 | static ADAudioSource* _instance = nil; 10 | 11 | @interface ADAudioSource : NSObject { 12 | AVAudioPlayer* _source_player; 13 | NSString* _source_filepath; 14 | float _volume; 15 | bool _loop; 16 | bool _isplaying; 17 | } 18 | 19 | @property (readonly) bool isload; 20 | @end 21 | 22 | @implementation ADAudioSource 23 | 24 | -(id) init { 25 | if((self = [super init])) { 26 | _source_filepath = nil; 27 | _source_player = nil; 28 | _loop = false; 29 | _isplaying = false; 30 | } 31 | return self; 32 | } 33 | 34 | -(bool)isload { 35 | return _source_player != nil; 36 | } 37 | 38 | 39 | +(id) sharedInstance { 40 | if(!_instance) { 41 | _instance = [[ADAudioSource alloc] init]; 42 | // NSError* error = nil; 43 | // [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error]; 44 | // assert(error==nil); 45 | } 46 | return _instance; 47 | } 48 | 49 | -(bool) load:(NSString*) filepath { 50 | if(_source_filepath == nil || ![filepath isEqualToString:_source_filepath]) { 51 | _source_filepath = [filepath copy]; 52 | NSError *error = nil; 53 | if(_isplaying){ 54 | [self stop]; 55 | } 56 | _source_player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:_source_filepath] error:&error]; 57 | 58 | if(error == nil) { 59 | _source_player.delegate = self; 60 | [_source_player prepareToPlay]; 61 | }else { 62 | NSString* err = [NSString stringWithFormat:@"load %@ error[%@]", filepath, error]; 63 | ad_error([err UTF8String]); 64 | return false; 65 | } 66 | } 67 | return true; 68 | } 69 | 70 | 71 | -(void)loop:(bool) v { 72 | _loop = v; 73 | } 74 | 75 | -(void) play { 76 | [_source_player play]; 77 | _isplaying = true; 78 | } 79 | 80 | 81 | -(void) stop { 82 | _isplaying = false; 83 | [_source_player stop]; 84 | } 85 | 86 | -(void) pause { 87 | _isplaying = false; 88 | [_source_player pause]; 89 | } 90 | 91 | -(void)setVolume:(float) volume { 92 | _source_player.volume = volume; 93 | } 94 | 95 | 96 | - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 97 | NSLog(@"audioPlayerDidFinishPlaying %d", flag); 98 | _isplaying = false; 99 | if(_loop) { 100 | [self play]; 101 | } 102 | } 103 | 104 | - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player { 105 | // nothing todo it. 106 | } 107 | 108 | - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player { 109 | if(_isplaying) { 110 | [self play]; 111 | } 112 | } 113 | @end 114 | 115 | static int 116 | l_load(lua_State* L) { 117 | const char* filepath = lua_tostring(L, 1); 118 | bool success = [[ADAudioSource sharedInstance] load:[NSString stringWithFormat:@"%s", filepath]]; 119 | if(!success) { 120 | luaL_error(L, ad_last_error()); 121 | } 122 | return 0; 123 | } 124 | 125 | static int 126 | l_play(lua_State* L) { 127 | bool loop = lua_toboolean(L, 1); 128 | ADAudioSource* source = [ADAudioSource sharedInstance]; 129 | if([source isload]){ 130 | [source loop:loop]; 131 | [source play]; 132 | } 133 | return 0; 134 | } 135 | 136 | static int 137 | l_stop(lua_State* L) { 138 | ADAudioSource* source = [ADAudioSource sharedInstance]; 139 | if([source isload]){ 140 | [source stop]; 141 | } 142 | return 0; 143 | } 144 | 145 | static int 146 | l_pause(lua_State* L) { 147 | ADAudioSource* source = [ADAudioSource sharedInstance]; 148 | if([source isload]){ 149 | [source pause]; 150 | } 151 | return 0; 152 | } 153 | 154 | 155 | int 156 | adl_decode_hardware_ios(lua_State* L) { 157 | luaL_checkversion(L); 158 | luaL_Reg l[] = { 159 | {"load", l_load}, 160 | {"play", l_play}, 161 | {"stop", l_stop}, 162 | {"pause", l_pause}, 163 | {NULL, NULL}, 164 | }; 165 | 166 | luaL_newlib(L, l); 167 | return 1; 168 | } 169 | 170 | #else 171 | 172 | // no support hardware decode ios 173 | int 174 | adl_decode_hardware_ios(lua_State* L) { 175 | lua_pushboolean(L, false); 176 | return 1; 177 | } 178 | 179 | #endif 180 | 181 | 182 | -------------------------------------------------------------------------------- /engine/lua/lfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lfunc_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lfunc.h" 18 | #include "lgc.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | 23 | 24 | 25 | CClosure *luaF_newCclosure (lua_State *L, int n) { 26 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); 27 | CClosure *c = gco2ccl(o); 28 | c->nupvalues = cast_byte(n); 29 | return c; 30 | } 31 | 32 | 33 | LClosure *luaF_newLclosure (lua_State *L, int n) { 34 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); 35 | LClosure *c = gco2lcl(o); 36 | c->p = NULL; 37 | c->nupvalues = cast_byte(n); 38 | while (n--) c->upvals[n] = NULL; 39 | return c; 40 | } 41 | 42 | /* 43 | ** fill a closure with new closed upvalues 44 | */ 45 | void luaF_initupvals (lua_State *L, LClosure *cl) { 46 | int i; 47 | for (i = 0; i < cl->nupvalues; i++) { 48 | UpVal *uv = luaM_new(L, UpVal); 49 | uv->refcount = 1; 50 | uv->v = &uv->u.value; /* make it closed */ 51 | setnilvalue(uv->v); 52 | cl->upvals[i] = uv; 53 | } 54 | } 55 | 56 | 57 | UpVal *luaF_findupval (lua_State *L, StkId level) { 58 | UpVal **pp = &L->openupval; 59 | UpVal *p; 60 | UpVal *uv; 61 | lua_assert(isintwups(L) || L->openupval == NULL); 62 | while (*pp != NULL && (p = *pp)->v >= level) { 63 | lua_assert(upisopen(p)); 64 | if (p->v == level) /* found a corresponding upvalue? */ 65 | return p; /* return it */ 66 | pp = &p->u.open.next; 67 | } 68 | /* not found: create a new upvalue */ 69 | uv = luaM_new(L, UpVal); 70 | uv->refcount = 0; 71 | uv->u.open.next = *pp; /* link it to list of open upvalues */ 72 | uv->u.open.touched = 1; 73 | *pp = uv; 74 | uv->v = level; /* current value lives in the stack */ 75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ 76 | L->twups = G(L)->twups; /* link it to the list */ 77 | G(L)->twups = L; 78 | } 79 | return uv; 80 | } 81 | 82 | 83 | void luaF_close (lua_State *L, StkId level) { 84 | UpVal *uv; 85 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) { 86 | lua_assert(upisopen(uv)); 87 | L->openupval = uv->u.open.next; /* remove from 'open' list */ 88 | if (uv->refcount == 0) /* no references? */ 89 | luaM_free(L, uv); /* free upvalue */ 90 | else { 91 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ 92 | uv->v = &uv->u.value; /* now current value lives here */ 93 | luaC_upvalbarrier(L, uv); 94 | } 95 | } 96 | } 97 | 98 | 99 | Proto *luaF_newproto (lua_State *L) { 100 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); 101 | Proto *f = gco2p(o); 102 | f->k = NULL; 103 | f->sizek = 0; 104 | f->p = NULL; 105 | f->sizep = 0; 106 | f->code = NULL; 107 | f->cache = NULL; 108 | f->sizecode = 0; 109 | f->lineinfo = NULL; 110 | f->sizelineinfo = 0; 111 | f->upvalues = NULL; 112 | f->sizeupvalues = 0; 113 | f->numparams = 0; 114 | f->is_vararg = 0; 115 | f->maxstacksize = 0; 116 | f->locvars = NULL; 117 | f->sizelocvars = 0; 118 | f->linedefined = 0; 119 | f->lastlinedefined = 0; 120 | f->source = NULL; 121 | return f; 122 | } 123 | 124 | 125 | void luaF_freeproto (lua_State *L, Proto *f) { 126 | luaM_freearray(L, f->code, f->sizecode); 127 | luaM_freearray(L, f->p, f->sizep); 128 | luaM_freearray(L, f->k, f->sizek); 129 | luaM_freearray(L, f->lineinfo, f->sizelineinfo); 130 | luaM_freearray(L, f->locvars, f->sizelocvars); 131 | luaM_freearray(L, f->upvalues, f->sizeupvalues); 132 | luaM_free(L, f); 133 | } 134 | 135 | 136 | /* 137 | ** Look for n-th local variable at line 'line' in function 'func'. 138 | ** Returns NULL if not found. 139 | */ 140 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { 141 | int i; 142 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { 143 | if (pc < f->locvars[i].endpc) { /* is variable active? */ 144 | local_number--; 145 | if (local_number == 0) 146 | return getstr(f->locvars[i].varname); 147 | } 148 | } 149 | return NULL; /* not found */ 150 | } 151 | 152 | -------------------------------------------------------------------------------- /res/tool/binpacking.lua: -------------------------------------------------------------------------------- 1 | -- container for rects 2 | local bin_mt = {} 3 | bin_mt.__index = bin_mt 4 | 5 | local function _debug(...) 6 | -- print("[BPDEBUG] "..string.format(...)) 7 | end 8 | 9 | function bin_mt:_find_node(w, h) 10 | local node = {} -- result 11 | local best_ssf = false 12 | local best_lsf = false 13 | 14 | for _,v in ipairs(self.free) do 15 | if v.w >= w and v.h >= h then 16 | local dw = math.abs(v.w - w) 17 | local dh = math.abs(v.h - h) 18 | local ssf = math.min(dw, dh) -- short side fit 19 | local lsf = math.max(dw, dh) -- long side fit 20 | 21 | if not best_ssf or 22 | ssf < best_ssf or 23 | (ssf == best_ssf and lsf < best_lsf) then 24 | node.x = v.x -- record the best node 25 | node.y = v.y 26 | node.w = w 27 | node.h = h 28 | best_ssf = ssf 29 | best_lsf = lsf 30 | end 31 | end 32 | end 33 | 34 | return node 35 | end 36 | 37 | function bin_mt:_free_contain(node, skip) 38 | for _,v in ipairs(self.free) do 39 | if v ~= skip then -- our node is splited from this freenode, skip it 40 | if v.x <= node.x and 41 | v.x + v.w >= node.x + node.w and 42 | v.y <= node.y and 43 | v.y + v.h >= node.y + node.h then 44 | return true 45 | end 46 | end 47 | end 48 | 49 | return false 50 | end 51 | 52 | function bin_mt:_split_free(freenode, node, splits) 53 | _debug("split free node(%d,%d,%d,%d)", freenode.x, freenode.y, freenode.w, freenode.h) 54 | 55 | if node.x > freenode.x + freenode.w or 56 | node.x + node.w <= freenode.x or 57 | node.y > freenode.y + freenode.h or 58 | node.y + node.h <= freenode.y then 59 | _debug("\tno intersects") 60 | return false 61 | end 62 | 63 | if node.y > freenode.y then -- try top 64 | local n = {} 65 | n.x = freenode.x 66 | n.y = freenode.y 67 | n.w = freenode.w 68 | n.h = node.y - freenode.y 69 | if not self:_free_contain(n, freenode) then 70 | table.insert(splits, n) 71 | _debug("\ttop(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 72 | else 73 | _debug("\ttop area is covered by other freenode") 74 | end 75 | end 76 | 77 | if node.y + node.h < freenode.y + freenode.h then -- try bottom 78 | local n = {} 79 | n.x = freenode.x 80 | n.y = node.y + node.h 81 | n.w = freenode.w 82 | n.h = (freenode.y + freenode.h) - (node.y + node.h) 83 | if not self:_free_contain(n, freenode) then 84 | table.insert(splits, n) 85 | _debug("\tbottom(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 86 | else 87 | _debug("\tbottom area is covered by other freenode") 88 | end 89 | end 90 | 91 | if node.x > freenode.x then -- try left 92 | local n = {} 93 | n.x = freenode.x 94 | n.y = freenode.y 95 | n.w = node.x - freenode.x 96 | n.h = freenode.h 97 | if not self:_free_contain(n, freenode) then 98 | table.insert(splits, n) 99 | _debug("\tleft(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 100 | else 101 | _debug("\tleft area is covered by other freenode") 102 | end 103 | end 104 | 105 | if node.x + node.w < freenode.x + freenode.w then -- try right 106 | local n = {} 107 | n.x = node.x + node.w 108 | n.y = freenode.y 109 | n.w = (freenode.x + freenode.w) - (node.x + node.w) 110 | n.h = freenode.h 111 | if not self:_free_contain(n, freenode) then 112 | table.insert(splits, n) 113 | _debug("\tright(%d,%d,%d,%d) splited", n.x, n.y, n.w, n.h) 114 | else 115 | _debug("\tright area is covered by other freenode") 116 | end 117 | end 118 | 119 | return true 120 | end 121 | 122 | function bin_mt:insert(w, h) 123 | _debug("***** insert new rect(%d,%d) *****", w, h) 124 | 125 | local node = self:_find_node(w, h) 126 | if not node.h then return end -- not found 127 | _debug("found new node(%d,%d,%d,%d)", node.x, node.y, node.w, node.h) 128 | 129 | local splits = {} 130 | local i = 1 131 | while i <= #self.free do -- split free node into small pieces 132 | if self:_split_free(self.free[i], node, splits) then 133 | table.remove(self.free, i) 134 | else 135 | i = i + 1 136 | end 137 | end 138 | 139 | for i,v in ipairs(splits) do 140 | table.insert(self.free, v) 141 | end 142 | 143 | return node 144 | end 145 | 146 | -- binpacking algorithm module 147 | local M = {} 148 | 149 | function M:new_bin(w, h) 150 | local node = {x=0, y=0, w=w, h=h} 151 | local bin = {} 152 | bin.free = {node} -- free node list 153 | return setmetatable(bin, bin_mt) 154 | end 155 | 156 | return M -------------------------------------------------------------------------------- /engine/lua/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.33 2014/11/21 12:15:57 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltm_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | #include "lstring.h" 22 | #include "ltable.h" 23 | #include "ltm.h" 24 | #include "lvm.h" 25 | 26 | 27 | static const char udatatypename[] = "userdata"; 28 | 29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 30 | "no value", 31 | "nil", "boolean", udatatypename, "number", 32 | "string", "table", "function", udatatypename, "thread", 33 | "proto" /* this last case is used for tests only */ 34 | }; 35 | 36 | 37 | void luaT_init (lua_State *L) { 38 | static const char *const luaT_eventname[] = { /* ORDER TM */ 39 | "__index", "__newindex", 40 | "__gc", "__mode", "__len", "__eq", 41 | "__add", "__sub", "__mul", "__mod", "__pow", 42 | "__div", "__idiv", 43 | "__band", "__bor", "__bxor", "__shl", "__shr", 44 | "__unm", "__bnot", "__lt", "__le", 45 | "__concat", "__call" 46 | }; 47 | int i; 48 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 50 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */ 51 | } 52 | } 53 | 54 | 55 | /* 56 | ** function to be used with macro "fasttm": optimized for absence of 57 | ** tag methods 58 | */ 59 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 60 | const TValue *tm = luaH_getstr(events, ename); 61 | lua_assert(event <= TM_EQ); 62 | if (ttisnil(tm)) { /* no tag method? */ 63 | events->flags |= cast_byte(1u<metatable; 75 | break; 76 | case LUA_TUSERDATA: 77 | mt = uvalue(o)->metatable; 78 | break; 79 | default: 80 | mt = G(L)->mt[ttnov(o)]; 81 | } 82 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 83 | } 84 | 85 | 86 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, 87 | const TValue *p2, TValue *p3, int hasres) { 88 | ptrdiff_t result = savestack(L, p3); 89 | setobj2s(L, L->top++, f); /* push function (assume EXTRA_STACK) */ 90 | setobj2s(L, L->top++, p1); /* 1st argument */ 91 | setobj2s(L, L->top++, p2); /* 2nd argument */ 92 | if (!hasres) /* no result? 'p3' is third argument */ 93 | setobj2s(L, L->top++, p3); /* 3rd argument */ 94 | /* metamethod may yield only when called from Lua code */ 95 | luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci)); 96 | if (hasres) { /* if has result, move it to its place */ 97 | p3 = restorestack(L, result); 98 | setobjs2s(L, p3, --L->top); 99 | } 100 | } 101 | 102 | 103 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, 104 | StkId res, TMS event) { 105 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ 106 | if (ttisnil(tm)) 107 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ 108 | if (ttisnil(tm)) return 0; 109 | luaT_callTM(L, tm, p1, p2, res, 1); 110 | return 1; 111 | } 112 | 113 | 114 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, 115 | StkId res, TMS event) { 116 | if (!luaT_callbinTM(L, p1, p2, res, event)) { 117 | switch (event) { 118 | case TM_CONCAT: 119 | luaG_concaterror(L, p1, p2); 120 | case TM_BAND: case TM_BOR: case TM_BXOR: 121 | case TM_SHL: case TM_SHR: case TM_BNOT: { 122 | lua_Number dummy; 123 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) 124 | luaG_tointerror(L, p1, p2); 125 | else 126 | luaG_opinterror(L, p1, p2, "perform bitwise operation on"); 127 | /* else go through */ 128 | } 129 | default: 130 | luaG_opinterror(L, p1, p2, "perform arithmetic on"); 131 | } 132 | } 133 | } 134 | 135 | 136 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, 137 | TMS event) { 138 | if (!luaT_callbinTM(L, p1, p2, L->top, event)) 139 | return -1; /* no metamethod */ 140 | else 141 | return !l_isfalse(L->top); 142 | } 143 | 144 | -------------------------------------------------------------------------------- /engine/lua/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lcorolib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lauxlib.h" 18 | #include "lualib.h" 19 | 20 | 21 | static lua_State *getco (lua_State *L) { 22 | lua_State *co = lua_tothread(L, 1); 23 | luaL_argcheck(L, co, 1, "thread expected"); 24 | return co; 25 | } 26 | 27 | 28 | static int auxresume (lua_State *L, lua_State *co, int narg) { 29 | int status; 30 | if (!lua_checkstack(co, narg)) { 31 | lua_pushliteral(L, "too many arguments to resume"); 32 | return -1; /* error flag */ 33 | } 34 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 35 | lua_pushliteral(L, "cannot resume dead coroutine"); 36 | return -1; /* error flag */ 37 | } 38 | lua_xmove(L, co, narg); 39 | status = lua_resume(co, L, narg); 40 | if (status == LUA_OK || status == LUA_YIELD) { 41 | int nres = lua_gettop(co); 42 | if (!lua_checkstack(L, nres + 1)) { 43 | lua_pop(co, nres); /* remove results anyway */ 44 | lua_pushliteral(L, "too many results to resume"); 45 | return -1; /* error flag */ 46 | } 47 | lua_xmove(co, L, nres); /* move yielded values */ 48 | return nres; 49 | } 50 | else { 51 | lua_xmove(co, L, 1); /* move error message */ 52 | return -1; /* error flag */ 53 | } 54 | } 55 | 56 | 57 | static int luaB_coresume (lua_State *L) { 58 | lua_State *co = getco(L); 59 | int r; 60 | r = auxresume(L, co, lua_gettop(L) - 1); 61 | if (r < 0) { 62 | lua_pushboolean(L, 0); 63 | lua_insert(L, -2); 64 | return 2; /* return false + error message */ 65 | } 66 | else { 67 | lua_pushboolean(L, 1); 68 | lua_insert(L, -(r + 1)); 69 | return r + 1; /* return true + 'resume' returns */ 70 | } 71 | } 72 | 73 | 74 | static int luaB_auxwrap (lua_State *L) { 75 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 76 | int r = auxresume(L, co, lua_gettop(L)); 77 | if (r < 0) { 78 | if (lua_isstring(L, -1)) { /* error object is a string? */ 79 | luaL_where(L, 1); /* add extra info */ 80 | lua_insert(L, -2); 81 | lua_concat(L, 2); 82 | } 83 | return lua_error(L); /* propagate error */ 84 | } 85 | return r; 86 | } 87 | 88 | 89 | static int luaB_cocreate (lua_State *L) { 90 | lua_State *NL; 91 | luaL_checktype(L, 1, LUA_TFUNCTION); 92 | NL = lua_newthread(L); 93 | lua_pushvalue(L, 1); /* move function to top */ 94 | lua_xmove(L, NL, 1); /* move function from L to NL */ 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_cowrap (lua_State *L) { 100 | luaB_cocreate(L); 101 | lua_pushcclosure(L, luaB_auxwrap, 1); 102 | return 1; 103 | } 104 | 105 | 106 | static int luaB_yield (lua_State *L) { 107 | return lua_yield(L, lua_gettop(L)); 108 | } 109 | 110 | 111 | static int luaB_costatus (lua_State *L) { 112 | lua_State *co = getco(L); 113 | if (L == co) lua_pushliteral(L, "running"); 114 | else { 115 | switch (lua_status(co)) { 116 | case LUA_YIELD: 117 | lua_pushliteral(L, "suspended"); 118 | break; 119 | case LUA_OK: { 120 | lua_Debug ar; 121 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 122 | lua_pushliteral(L, "normal"); /* it is running */ 123 | else if (lua_gettop(co) == 0) 124 | lua_pushliteral(L, "dead"); 125 | else 126 | lua_pushliteral(L, "suspended"); /* initial state */ 127 | break; 128 | } 129 | default: /* some error occurred */ 130 | lua_pushliteral(L, "dead"); 131 | break; 132 | } 133 | } 134 | return 1; 135 | } 136 | 137 | 138 | static int luaB_yieldable (lua_State *L) { 139 | lua_pushboolean(L, lua_isyieldable(L)); 140 | return 1; 141 | } 142 | 143 | 144 | static int luaB_corunning (lua_State *L) { 145 | int ismain = lua_pushthread(L); 146 | lua_pushboolean(L, ismain); 147 | return 2; 148 | } 149 | 150 | 151 | static const luaL_Reg co_funcs[] = { 152 | {"create", luaB_cocreate}, 153 | {"resume", luaB_coresume}, 154 | {"running", luaB_corunning}, 155 | {"status", luaB_costatus}, 156 | {"wrap", luaB_cowrap}, 157 | {"yield", luaB_yield}, 158 | {"isyieldable", luaB_yieldable}, 159 | {NULL, NULL} 160 | }; 161 | 162 | 163 | 164 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 165 | luaL_newlib(L, co_funcs); 166 | return 1; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /engine/lib/render/render.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy3d_render_h 2 | #define ejoy3d_render_h 3 | 4 | #include 5 | 6 | typedef unsigned int RID; 7 | 8 | struct render; 9 | 10 | struct render_init_args { 11 | int max_buffer; 12 | int max_layout; 13 | int max_target; 14 | int max_texture; 15 | int max_shader; 16 | }; 17 | 18 | struct vertex_attrib { 19 | const char * name; 20 | int vbslot; 21 | int n; 22 | int size; 23 | int offset; 24 | }; 25 | 26 | struct shader_init_args { 27 | const char * vs; 28 | const char * fs; 29 | int texture; 30 | const char **texture_uniform; 31 | }; 32 | 33 | enum RENDER_OBJ { 34 | INVALID = 0, 35 | VERTEXLAYOUT = 1, 36 | VERTEXBUFFER = 2, 37 | INDEXBUFFER = 3, 38 | TEXTURE = 4, 39 | TARGET = 5, 40 | SHADER = 6, 41 | }; 42 | 43 | enum TEXTURE_TYPE { 44 | TEXTURE_2D = 0, 45 | TEXTURE_CUBE, 46 | }; 47 | 48 | enum TEXTURE_FORMAT { 49 | TEXTURE_INVALID = 0, 50 | TEXTURE_RGBA8, 51 | TEXTURE_RGBA4, 52 | TEXTURE_RGB, 53 | TEXTURE_RGB565, 54 | TEXTURE_A8, 55 | TEXTURE_DEPTH, // use for render target 56 | TEXTURE_PVR2, 57 | TEXTURE_PVR4, 58 | TEXTURE_ETC1, 59 | }; 60 | 61 | enum BLEND_FORMAT { 62 | BLEND_DISABLE = 0, 63 | BLEND_ZERO, 64 | BLEND_ONE, 65 | BLEND_SRC_COLOR, 66 | BLEND_ONE_MINUS_SRC_COLOR, 67 | BLEND_SRC_ALPHA, 68 | BLEND_ONE_MINUS_SRC_ALPHA, 69 | BLEND_DST_ALPHA, 70 | BLEND_ONE_MINUS_DST_ALPHA, 71 | BLEND_DST_COLOR, 72 | BLEND_ONE_MINUS_DST_COLOR, 73 | BLEND_SRC_ALPHA_SATURATE, 74 | }; 75 | 76 | enum DEPTH_FORMAT { 77 | DEPTH_DISABLE = 0, 78 | DEPTH_LESS_EQUAL, 79 | DEPTH_LESS, 80 | DEPTH_EQUAL, 81 | DEPTH_GREATER, 82 | DEPTH_GREATER_EQUAL, 83 | DEPTH_ALWAYS, 84 | }; 85 | 86 | enum CLEAR_MASK { 87 | MASKC = 0x1, 88 | MASKD = 0x2, 89 | MASKS = 0x4, 90 | }; 91 | 92 | enum UNIFORM_FORMAT { 93 | UNIFORM_INVALID = 0, 94 | UNIFORM_FLOAT1, 95 | UNIFORM_FLOAT2, 96 | UNIFORM_FLOAT3, 97 | UNIFORM_FLOAT4, 98 | UNIFORM_FLOAT33, 99 | UNIFORM_FLOAT44, 100 | }; 101 | 102 | enum DRAW_MODE { 103 | DRAW_TRIANGLE = 0, 104 | DRAW_LINE, 105 | }; 106 | 107 | enum CULL_MODE { 108 | CULL_DISABLE = 0, 109 | CULL_FRONT, 110 | CULL_BACK, 111 | }; 112 | 113 | int render_version(struct render *R); 114 | int render_size(struct render_init_args *args); 115 | struct render * render_init(struct render_init_args *args, void * buffer, int sz); 116 | void render_exit(struct render * R); 117 | 118 | void render_set(struct render *R, enum RENDER_OBJ what, RID id, int slot); 119 | void render_release(struct render *R, enum RENDER_OBJ what, RID id); 120 | 121 | RID render_register_vertexlayout(struct render *R, int n, struct vertex_attrib * attrib); 122 | 123 | // what should be VERTEXBUFFER or INDEXBUFFER 124 | RID render_buffer_create(struct render *R, enum RENDER_OBJ what, const void *data, int n, int stride); 125 | void render_buffer_update(struct render *R, RID id, const void * data, int n); 126 | 127 | RID render_texture_create(struct render *R, int width, int height, enum TEXTURE_FORMAT format, enum TEXTURE_TYPE type, int mipmap); 128 | void render_texture_update(struct render *R, RID id, int width, int height, const void *pixels, int slice, int miplevel); 129 | // subupdate only support slice 0, miplevel 0 130 | void render_texture_subupdate(struct render *R, RID id, const void *pixels, int x, int y, int w, int h); 131 | 132 | RID render_target_create(struct render *R, int width, int height, enum TEXTURE_FORMAT format); 133 | // render_release TARGET would not release the texture attachment 134 | RID render_target_texture(struct render *R, RID rt); 135 | 136 | RID render_shader_create(struct render *R, struct shader_init_args *args); 137 | void render_shader_bind(struct render *R, RID id); 138 | int render_shader_locuniform(struct render *R, const char * name); 139 | void render_shader_setuniform(struct render *R, int loc, enum UNIFORM_FORMAT format, const float *v); 140 | 141 | void render_setviewport(struct render *R, int x, int y, int width, int height ); 142 | void render_setscissor(struct render *R, int x, int y, int width, int height ); 143 | 144 | void render_setblend(struct render *R, enum BLEND_FORMAT src, enum BLEND_FORMAT dst); 145 | void render_setdepth(struct render *R, enum DEPTH_FORMAT d); 146 | void render_setcull(struct render *R, enum CULL_MODE c); 147 | void render_enabledepthmask(struct render *R, int enable); 148 | void render_enablescissor(struct render *R, int enable); 149 | 150 | void render_state_reset(struct render *R); 151 | 152 | void render_clear(struct render *R, enum CLEAR_MASK mask, unsigned long argb); 153 | void render_draw(struct render *R, enum DRAW_MODE mode, int fromidx, int ni); 154 | 155 | #endif 156 | -------------------------------------------------------------------------------- /engine/lib/audio/decode/ad_tools.c: -------------------------------------------------------------------------------- 1 | #include "../oal_decode.h" 2 | 3 | #include 4 | #include 5 | 6 | 7 | #ifdef SUPPORT_AUDIO_TOOLS 8 | #import 9 | #import 10 | 11 | static struct oal_info* 12 | _decode_tools (const char* filepath, struct oal_info* out) { 13 | struct oal_info* ret = NULL; 14 | OSStatus status = noErr; 15 | ExtAudioFileRef extRef = NULL; 16 | SInt64 file_length_in_frame = 0; 17 | AudioStreamBasicDescription file_format; 18 | AudioStreamBasicDescription output_format; 19 | UInt32 property_size = sizeof(file_format); 20 | 21 | CFURLRef url = CFURLCreateWithBytes(kCFAllocatorDefault, 22 | (const UInt8*)filepath, strlen(filepath), kCFStringEncodingUTF8, NULL); 23 | 24 | status = ExtAudioFileOpenURL(url, &extRef); 25 | if(status != noErr) { 26 | ad_error("cannot openurl %s from ExtAudioFileOpenURL status: [%ld]", filepath, status); 27 | goto EXIT; 28 | } 29 | 30 | // get the audio data format 31 | status = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &property_size, &file_format); 32 | if(status != noErr) { 33 | ad_error("ExtAudioFileGetProperty get error status: [%ld]", status); 34 | goto EXIT; 35 | } 36 | 37 | if(file_format.mChannelsPerFrame > 2) { 38 | ad_error("Unsupported Format, channel count is greater than stereo"); 39 | goto EXIT; 40 | } 41 | 42 | // Set the client format to 16 bit signed integer (native-endian) data 43 | // Maintain the channel count and sample rate of the original source format 44 | output_format.mSampleRate = file_format.mSampleRate; 45 | output_format.mChannelsPerFrame = file_format.mChannelsPerFrame; 46 | 47 | output_format.mFormatID = kAudioFormatLinearPCM; 48 | output_format.mBytesPerPacket = 2 * output_format.mChannelsPerFrame; 49 | output_format.mFramesPerPacket = 1; 50 | output_format.mBytesPerFrame = 2 * output_format.mChannelsPerFrame; 51 | output_format.mBitsPerChannel = 16; 52 | output_format.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger; 53 | 54 | // Set the desired client (output) data format 55 | status = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(output_format), &output_format); 56 | if(status != noErr) { 57 | ad_error("ExtAudioFileSetProperty error status: [%ld]", status); 58 | goto EXIT; 59 | } 60 | 61 | // get the total frame count 62 | property_size = sizeof(file_length_in_frame); 63 | status = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &property_size, &file_length_in_frame); 64 | if(status != noErr) { 65 | ad_error("ExtAudioFileGetProperty error status: [%ld]", status); 66 | goto EXIT; 67 | } 68 | 69 | // Read all the data into memory 70 | UInt32 data_size = (UInt32) file_length_in_frame * output_format.mBytesPerFrame; 71 | void* data = malloc(data_size); 72 | if(!data) { 73 | ad_error("malloc error size: %ld", data_size); 74 | goto EXIT; 75 | } 76 | 77 | memset(data, 0, data_size); 78 | AudioBufferList theDataBuffer; 79 | theDataBuffer.mNumberBuffers = 1; 80 | theDataBuffer.mBuffers[0].mDataByteSize = data_size; 81 | theDataBuffer.mBuffers[0].mNumberChannels = output_format.mChannelsPerFrame; 82 | theDataBuffer.mBuffers[0].mData = data; 83 | 84 | // Read the data into an AudioBufferList 85 | status = ExtAudioFileRead(extRef, (UInt32*)&file_length_in_frame, &theDataBuffer); 86 | if(status == noErr) { 87 | // success 88 | out->data = data; 89 | out->size = (ALsizei)data_size; 90 | out->format = (output_format.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; 91 | out->freq = (ALsizei)output_format.mSampleRate; 92 | } else { 93 | // failure 94 | free(data); 95 | ad_error("ExtAudioFileRead error status: [%ld]", status); 96 | goto EXIT; 97 | } 98 | 99 | ret = out; 100 | EXIT: 101 | CFRelease(url); 102 | if (extRef) ExtAudioFileDispose(extRef); 103 | return ret; 104 | } 105 | #else 106 | 107 | static struct oal_info* 108 | _decode_tools (const char* filepath, struct oal_info* out) { 109 | ad_error("no support audio tools"); 110 | return NULL; 111 | } 112 | #endif 113 | 114 | 115 | int 116 | adl_decode_tools(lua_State* L) { 117 | const char* file = lua_tostring(L, 1); 118 | const char* type = lua_tostring(L, 2); 119 | struct oal_info out = {0}; 120 | if(_decode_tools(file, &out)){ 121 | strncpy(out.type, type, sizeof(out.type)-1); 122 | return ad_new_info(L, &out); 123 | } else { 124 | luaL_error(L, ad_last_error()); 125 | } 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /engine/lua/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.86 2014/10/25 11:50:46 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 | #include "lstate.h" 13 | 14 | /* 15 | ** Collectable objects may have one of three colors: white, which 16 | ** means the object is not marked; gray, which means the 17 | ** object is marked, but its references may be not marked; and 18 | ** black, which means that the object and all its references are marked. 19 | ** The main invariant of the garbage collector, while marking objects, 20 | ** is that a black object can never point to a white one. Moreover, 21 | ** any gray object must be in a "gray list" (gray, grayagain, weak, 22 | ** allweak, ephemeron) so that it can be visited again before finishing 23 | ** the collection cycle. These lists have no meaning when the invariant 24 | ** is not being enforced (e.g., sweep phase). 25 | */ 26 | 27 | 28 | 29 | /* how much to allocate before next GC step */ 30 | #if !defined(GCSTEPSIZE) 31 | /* ~100 small strings */ 32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString))) 33 | #endif 34 | 35 | 36 | /* 37 | ** Possible states of the Garbage Collector 38 | */ 39 | #define GCSpropagate 0 40 | #define GCSatomic 1 41 | #define GCSswpallgc 2 42 | #define GCSswpfinobj 3 43 | #define GCSswptobefnz 4 44 | #define GCSswpend 5 45 | #define GCScallfin 6 46 | #define GCSpause 7 47 | 48 | 49 | #define issweepphase(g) \ 50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend) 51 | 52 | 53 | /* 54 | ** macro to tell when main invariant (white objects cannot point to black 55 | ** ones) must be kept. During a collection, the sweep 56 | ** phase may break the invariant, as objects turned white may point to 57 | ** still-black objects. The invariant is restored when sweep ends and 58 | ** all objects are white again. 59 | */ 60 | 61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic) 62 | 63 | 64 | /* 65 | ** some useful bit tricks 66 | */ 67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 68 | #define setbits(x,m) ((x) |= (m)) 69 | #define testbits(x,m) ((x) & (m)) 70 | #define bitmask(b) (1<<(b)) 71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 72 | #define l_setbit(x,b) setbits(x, bitmask(b)) 73 | #define resetbit(x,b) resetbits(x, bitmask(b)) 74 | #define testbit(x,b) testbits(x, bitmask(b)) 75 | 76 | 77 | /* Layout for bit use in 'marked' field: */ 78 | #define WHITE0BIT 0 /* object is white (type 0) */ 79 | #define WHITE1BIT 1 /* object is white (type 1) */ 80 | #define BLACKBIT 2 /* object is black */ 81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */ 82 | /* bit 7 is currently used by tests (luaL_checkmemory) */ 83 | 84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 85 | 86 | 87 | #define iswhite(x) testbits((x)->marked, WHITEBITS) 88 | #define isblack(x) testbit((x)->marked, BLACKBIT) 89 | #define isgray(x) /* neither white nor black */ \ 90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) 91 | 92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) 93 | 94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) 95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) 96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) 97 | 98 | #define changewhite(x) ((x)->marked ^= WHITEBITS) 99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT) 100 | 101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 102 | 103 | 104 | #define luaC_condGC(L,c) \ 105 | {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);} 106 | #define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);) 107 | 108 | 109 | #define luaC_barrier(L,p,v) { \ 110 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 111 | luaC_barrier_(L,obj2gco(p),gcvalue(v)); } 112 | 113 | #define luaC_barrierback(L,p,v) { \ 114 | if (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) \ 115 | luaC_barrierback_(L,p); } 116 | 117 | #define luaC_objbarrier(L,p,o) { \ 118 | if (isblack(p) && iswhite(o)) \ 119 | luaC_barrier_(L,obj2gco(p),obj2gco(o)); } 120 | 121 | #define luaC_upvalbarrier(L,uv) \ 122 | { if (iscollectable((uv)->v) && !upisopen(uv)) \ 123 | luaC_upvalbarrier_(L,uv); } 124 | 125 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); 126 | LUAI_FUNC void luaC_freeallobjects (lua_State *L); 127 | LUAI_FUNC void luaC_step (lua_State *L); 128 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 129 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 130 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); 131 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); 132 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); 133 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); 134 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); 135 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); 136 | 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /res/tool/ejresource.lua: -------------------------------------------------------------------------------- 1 | local libimage = require "libimage" 2 | local libos = require "libos" 3 | local binpacking = require "binpacking" 4 | local utils = require "utils" 5 | 6 | -- file templates 7 | local TEMPLATE_IMAGE = [[ 8 | { name="%s", pos={%d,%d}, size={%d,%d}, offset={%d,%d} }, 9 | ]] 10 | 11 | -- image 12 | local img_mt = {} 13 | img_mt.__index = img_mt 14 | 15 | function img_mt:save(path, desc) -- ensure no file ext in path string 16 | libimage:saveppm(path, self.w, self.h, self.pixfmt, self.buf) 17 | if desc then --generate a description file as image sheet 18 | local lua_path = path..".p.lua" 19 | local body = "return {\n\n" 20 | body = body..string.format(TEMPLATE_IMAGE, self.name, 0, 0, self.w, self.h, self.ox, self.oy) 21 | body = body.."\n}" 22 | libos:writefile(lua_path, body) 23 | end 24 | 25 | utils:logf("image <%s> saved", self.name) 26 | end 27 | 28 | -- image sheet 29 | local sheet_mt = {} 30 | sheet_mt.__index = sheet_mt 31 | 32 | function sheet_mt:pack_img(img) 33 | assert(self.bin) 34 | 35 | local rect = self.bin:insert(img.w + 2, img.h + 2) -- 2 empty pixel between images 36 | if rect then 37 | local x = rect.x + 1 38 | local y = rect.y + 1 39 | libimage:blitimg(self.buf, self.size, self.pixfmt, img.buf, img.w, img.h, x, y) 40 | local info = {name=img.name, pos={x,y}, size={img.w,img.h}, offset={img.ox,img.oy}} 41 | table.insert(self.imgs, info) 42 | return true 43 | end 44 | 45 | return false 46 | end 47 | 48 | function sheet_mt:save(path, desc) -- ensure no file ext in path string 49 | libimage:saveppm(path, self.size, self.size, self.pixfmt, self.buf) 50 | if desc then 51 | local lua_path = path..".p.lua" 52 | local body = "return {\n\n" 53 | for _,v in ipairs(self.imgs) do 54 | body = body..string.format(TEMPLATE_IMAGE, v.name, 55 | v.pos[1], v.pos[2], v.size[1], v.size[2], v.offset[1], v.offset[2]) 56 | end 57 | body = body.."\n}" 58 | libos:writefile(lua_path, body) 59 | end 60 | 61 | utils:logf("image sheet saved") 62 | end 63 | 64 | -- animations 65 | local anim_mt = {} 66 | anim_mt.__index = anim_mt 67 | 68 | function anim_mt:add_action(frames) 69 | table.insert(self.actions, frames) 70 | end 71 | 72 | function anim_mt:save(path) 73 | local body = "return {\n\n" 74 | for _,act in ipairs(self.actions) do 75 | body = body.."{\n" -- start a new action 76 | for _,frm in ipairs(act) do 77 | body = body.."\t{ " -- start a new frame 78 | for _,com in ipairs(frm) do 79 | body = body..string.format("{ name=\"%s\", ", com.name) 80 | if com.scale then 81 | body = body..string.format("scale={%d,%d}, ", com.scale[1], com.scale[2]) 82 | end 83 | if com.rot then 84 | body = body..string.format("rot=%d, ", com.rot) 85 | end 86 | if com.trans then 87 | body = body..string.format("trans={%d,%d}, ", com.trans[1], com.trans[2]) 88 | end 89 | if com.color then 90 | body = body..string.format("color=0x%08x, ", com.color) 91 | end 92 | if com.additive then 93 | body = body..string.format("additive=0x%08x, ", com.additive) 94 | end 95 | body = body.."}, " 96 | end 97 | body = body.."},\n" 98 | end 99 | body = body.."},\n" 100 | end 101 | body = body.."\n}" 102 | libos:writefile(path, body) 103 | 104 | utils:logf("animation <%s> saved", self.name) 105 | end 106 | 107 | -- ejoy2d resource module 108 | local M = {} 109 | 110 | function M:load_img(path, name) 111 | local buf, pixfmt, w, h = libimage:loadpng(path) 112 | if not buf then return end 113 | 114 | local ox = 0 115 | local oy = 0 116 | if pixfmt == "RGBA" then 117 | local buf_trim, l, r, t, b = libimage:trimimg(buf, w, h, pixfmt) 118 | if buf_trim then 119 | buf = buf_trim 120 | w = w - l - r 121 | h = h - t - b 122 | ox = (l - r) / 2 123 | oy = (t - b) / 2 124 | end 125 | end 126 | 127 | local img = {} 128 | img.buf = buf -- raw memory for pixel data 129 | img.pixfmt = pixfmt -- pixel format string 130 | img.w = w 131 | img.h = h 132 | img.ox = ox 133 | img.oy = oy 134 | img.name = name 135 | return setmetatable(img, img_mt) 136 | end 137 | 138 | function M:new_sheet(size, pixfmt) 139 | local sheet = {} 140 | sheet.buf = libimage:newimg(size, pixfmt) 141 | sheet.pixfmt = pixfmt 142 | sheet.size = size 143 | sheet.imgs = {} 144 | sheet.bin = binpacking:new_bin(size, size) 145 | return setmetatable(sheet, sheet_mt) 146 | end 147 | 148 | function M:load_sheet(path) 149 | local buf, pixfmt, w, h = libimage:loadppm(string.sub(path, 1, -7)) 150 | if w ~= h then return end -- image sheet must be a square 151 | if buf then 152 | local sheet = {} 153 | sheet.buf = buf 154 | sheet.pixfmt = pixfmt 155 | sheet.size = w 156 | sheet.imgs = dofile(path) 157 | sheet.bin = false -- loaded sheet cannot pack in new image 158 | return setmetatable(sheet, sheet_mt) 159 | end 160 | end 161 | 162 | function M:new_anim(name) 163 | local anim = {} 164 | anim.name = name 165 | anim.actions = {} 166 | return setmetatable(anim, anim_mt) 167 | end 168 | 169 | function M:load_anim(path, name) 170 | local anim = {} 171 | anim.name = name 172 | anim.actions = dofile(path) 173 | return setmetatable(anim, anim_mt) 174 | end 175 | 176 | return M -------------------------------------------------------------------------------- /src/dawn_config.lua: -------------------------------------------------------------------------------- 1 | local NIGHT = { 2 | sky_far = { 0.00, 0.00, 0.00, 1.00 }, 3 | sky_near = { 0.12, 0.12, 0.12, 1.00 }, 4 | 5 | sea_far = { 0.02, 0.02, 0.02, 1.00 }, 6 | sea_near = { 0.08, 0.08, 0.08, 1.00 }, 7 | sea_spec = { 0.20, 0.20, 0.20, 1.00 }, 8 | 9 | moon_scale = 0.7, 10 | moon_color = { 1.00, 1.00, 1.00, 1.00 }, 11 | moon_glow_scale = 0.8, 12 | moon_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 13 | moon_refl_color = { 0.40, 0.40, 0.40, 1.00 }, 14 | } 15 | 16 | local DAWN = { 17 | sky_far = { 0.00, 0.00, 0.00, 1.00 }, 18 | sky_near = { 0.12, 0.12, 0.12, 1.00 }, 19 | 20 | sea_far = { 0.02, 0.02, 0.02, 1.00 }, 21 | sea_near = { 0.08, 0.08, 0.08, 1.00 }, 22 | sea_spec = { 0.20, 0.20, 0.20, 1.00 }, 23 | 24 | sun_scale = 0.7, 25 | sun_color = { 0.00, 0.00, 0.00, 0.00 }, 26 | sun_glow_scale = 0.5, 27 | sun_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 28 | sun_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 29 | 30 | moon_scale = 0.9, 31 | moon_color = { 1.00, 1.00, 1.00, 0.80 }, 32 | moon_glow_scale = 0.7, 33 | moon_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 34 | moon_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 35 | } 36 | 37 | local SUNRISE = { 38 | sky_far = { 0.21, 0.35, 0.46, 1.00 }, 39 | sky_near = { 0.70, 0.56, 0.21, 1.00 }, 40 | 41 | sea_far = { 0.50, 0.30, 0.30, 1.00 }, 42 | sea_near = { 0.20, 0.20, 0.10, 1.00 }, 43 | sea_spec = { 0.50, 0.50, 0.30, 1.00 }, 44 | 45 | sun_scale = 1.1, 46 | sun_color = { 0.92, 0.88, 0.74, 1.00 }, 47 | sun_glow_scale = 3, 48 | sun_glow_color = { 0.50, 0.50, 0.50, 1.00 }, 49 | sun_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 50 | 51 | moon_scale = 0.9, 52 | moon_color = { 1.00, 1.00, 1.00, 0.80 }, 53 | moon_glow_scale = 0.7, 54 | moon_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 55 | moon_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 56 | } 57 | 58 | local MORNING = { 59 | sky_far = { 0.16, 0.52, 0.80, 1.00 }, 60 | sky_near = { 1.00, 0.80, 0.30, 1.00 }, 61 | 62 | sea_far = { 0.99, 0.85, 0.58, 1.00 }, 63 | sea_near = { 0.53, 0.43, 0.32, 1.00 }, 64 | sea_spec = { 1.00, 1.00, 0.60, 1.00 }, 65 | 66 | sun_scale = 1, 67 | sun_color = { 0.92, 0.88, 0.74, 1.00 }, 68 | sun_glow_scale = 4, 69 | sun_glow_color = { 0.50, 0.50, 0.50, 1.00 }, 70 | sun_refl_color = { 0.80, 0.80, 0.40, 1.00 }, 71 | } 72 | 73 | local DAY = { 74 | sky_far = { 0.17, 0.49, 0.71, 1.00 }, 75 | sky_near = { 0.60, 0.78, 0.88, 1.00 }, 76 | 77 | sea_far = { 0.70, 0.82, 0.91, 1.00 }, 78 | sea_near = { 0.06, 0.38, 0.53, 1.00 }, 79 | sea_spec = { 1.00, 1.00, 1.00, 1.00 }, 80 | 81 | sun_scale = 0.7, 82 | sun_color = { 1.00, 1.00, 1.00, 1.00 }, 83 | sun_glow_scale = 6, 84 | sun_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 85 | sun_refl_color = { 0.80, 0.80, 0.80, 1.00 }, 86 | } 87 | 88 | local SUNSET = { 89 | sky_far = { 0.16, 0.52, 0.80, 1.00 }, 90 | sky_near = { 1.00, 0.80, 0.30, 1.00 }, 91 | 92 | sea_far = { 0.99, 0.85, 0.58, 1.00 }, 93 | sea_near = { 0.53, 0.43, 0.32, 1.00 }, 94 | sea_spec = { 1.00, 1.00, 0.80, 1.00 }, 95 | 96 | sun_scale = 1, 97 | sun_color = { 0.92, 0.88, 0.74, 1.00 }, 98 | sun_glow_scale = 3.5, 99 | sun_glow_color = { 0.50, 0.50, 0.50, 1.00 }, 100 | sun_refl_color = { 1.00, 1.00, 0.80, 1.00 }, 101 | } 102 | 103 | local EVENING = { 104 | sky_far = { 0.08, 0.25, 0.40, 1.00 }, 105 | sky_near = { 0.80, 0.64, 0.24, 1.00 }, 106 | 107 | sea_far = { 0.89, 0.75, 0.48, 1.00 }, 108 | sea_near = { 0.43, 0.33, 0.22, 1.00 }, 109 | sea_spec = { 0.50, 0.50, 0.30, 1.00 }, 110 | 111 | sun_scale = 1.1, 112 | sun_color = { 0.92, 0.88, 0.74, 1.00 }, 113 | sun_glow_scale = 1.5, 114 | sun_glow_color = { 0.50, 0.50, 0.50, 1.00 }, 115 | sun_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 116 | 117 | moon_scale = 1, 118 | moon_color = { 1.00, 1.00, 1.00, 0.20 }, 119 | moon_glow_scale = 0.5, 120 | moon_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 121 | moon_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 122 | } 123 | 124 | local DUSK = { 125 | sky_far = { 0.00, 0.00, 0.00, 1.00 }, 126 | sky_near = { 0.12, 0.12, 0.12, 1.00 }, 127 | 128 | sea_far = { 0.02, 0.02, 0.02, 1.00 }, 129 | sea_near = { 0.08, 0.08, 0.08, 1.00 }, 130 | sea_spec = { 0.20, 0.20, 0.20, 1.00 }, 131 | 132 | sun_scale = 1.1, 133 | sun_color = { 0.92, 0.88, 0.74, 1.00 }, 134 | sun_glow_scale = 0.5, 135 | sun_glow_color = { 0.50, 0.50, 0.50, 1.00 }, 136 | sun_refl_color = { 0.00, 0.00, 0.00, 1.00 }, 137 | 138 | moon_scale = 0.9, 139 | moon_color = { 1.00, 1.00, 1.00, 0.80 }, 140 | moon_glow_scale = 0.7, 141 | moon_glow_color = { 1.00, 1.00, 1.00, 1.00 }, 142 | moon_refl_color = { 0.20, 0.20, 0.20, 1.00 }, 143 | } 144 | 145 | return { 146 | NIGHT, -- 00:00 147 | NIGHT, -- 01:00 148 | NIGHT, -- 02:00 149 | NIGHT, -- 03:00 150 | NIGHT, -- 04:00 151 | DAWN, -- 05:00 152 | SUNRISE, -- 06:00 153 | MORNING, -- 07:00 154 | DAY, -- 08:00 155 | DAY, -- 09:00 156 | DAY, -- 10:00 157 | DAY, -- 11:00 158 | DAY, -- 12:00 159 | DAY, -- 13:00 160 | DAY, -- 14:00 161 | DAY, -- 15:00 162 | DAY, -- 16:00 163 | SUNSET, -- 17:00 164 | EVENING, -- 18:00 165 | DUSK, -- 19:00 166 | NIGHT, -- 20:00 167 | NIGHT, -- 21:00 168 | NIGHT, -- 22:00 169 | NIGHT, -- 23:00 170 | NIGHT, -- 24:00 eq SEQ[1] 171 | } 172 | -------------------------------------------------------------------------------- /engine/lua/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lstring_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "ldebug.h" 18 | #include "ldo.h" 19 | #include "lmem.h" 20 | #include "lobject.h" 21 | #include "lstate.h" 22 | #include "lstring.h" 23 | 24 | 25 | 26 | /* 27 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to 28 | ** compute its hash 29 | */ 30 | #if !defined(LUAI_HASHLIMIT) 31 | #define LUAI_HASHLIMIT 5 32 | #endif 33 | 34 | 35 | /* 36 | ** equality for long strings 37 | */ 38 | int luaS_eqlngstr (TString *a, TString *b) { 39 | size_t len = a->len; 40 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); 41 | return (a == b) || /* same instance or... */ 42 | ((len == b->len) && /* equal length and ... */ 43 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ 44 | } 45 | 46 | 47 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { 48 | unsigned int h = seed ^ cast(unsigned int, l); 49 | size_t l1; 50 | size_t step = (l >> LUAI_HASHLIMIT) + 1; 51 | for (l1 = l; l1 >= step; l1 -= step) 52 | h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1])); 53 | return h; 54 | } 55 | 56 | 57 | /* 58 | ** resizes the string table 59 | */ 60 | void luaS_resize (lua_State *L, int newsize) { 61 | int i; 62 | stringtable *tb = &G(L)->strt; 63 | if (newsize > tb->size) { /* grow table if needed */ 64 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 65 | for (i = tb->size; i < newsize; i++) 66 | tb->hash[i] = NULL; 67 | } 68 | for (i = 0; i < tb->size; i++) { /* rehash */ 69 | TString *p = tb->hash[i]; 70 | tb->hash[i] = NULL; 71 | while (p) { /* for each node in the list */ 72 | TString *hnext = p->hnext; /* save next */ 73 | unsigned int h = lmod(p->hash, newsize); /* new position */ 74 | p->hnext = tb->hash[h]; /* chain it */ 75 | tb->hash[h] = p; 76 | p = hnext; 77 | } 78 | } 79 | if (newsize < tb->size) { /* shrink table if needed */ 80 | /* vanishing slice should be empty */ 81 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 82 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); 83 | } 84 | tb->size = newsize; 85 | } 86 | 87 | 88 | 89 | /* 90 | ** creates a new string object 91 | */ 92 | static TString *createstrobj (lua_State *L, const char *str, size_t l, 93 | int tag, unsigned int h) { 94 | TString *ts; 95 | GCObject *o; 96 | size_t totalsize; /* total size of TString object */ 97 | totalsize = sizelstring(l); 98 | o = luaC_newobj(L, tag, totalsize); 99 | ts = gco2ts(o); 100 | ts->len = l; 101 | ts->hash = h; 102 | ts->extra = 0; 103 | memcpy(getaddrstr(ts), str, l * sizeof(char)); 104 | getaddrstr(ts)[l] = '\0'; /* ending 0 */ 105 | return ts; 106 | } 107 | 108 | 109 | void luaS_remove (lua_State *L, TString *ts) { 110 | stringtable *tb = &G(L)->strt; 111 | TString **p = &tb->hash[lmod(ts->hash, tb->size)]; 112 | while (*p != ts) /* find previous element */ 113 | p = &(*p)->hnext; 114 | *p = (*p)->hnext; /* remove element from its list */ 115 | tb->nuse--; 116 | } 117 | 118 | 119 | /* 120 | ** checks whether short string exists and reuses it or creates a new one 121 | */ 122 | static TString *internshrstr (lua_State *L, const char *str, size_t l) { 123 | TString *ts; 124 | global_State *g = G(L); 125 | unsigned int h = luaS_hash(str, l, g->seed); 126 | TString **list = &g->strt.hash[lmod(h, g->strt.size)]; 127 | for (ts = *list; ts != NULL; ts = ts->hnext) { 128 | if (l == ts->len && 129 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 130 | /* found! */ 131 | if (isdead(g, ts)) /* dead (but not collected yet)? */ 132 | changewhite(ts); /* resurrect it */ 133 | return ts; 134 | } 135 | } 136 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { 137 | luaS_resize(L, g->strt.size * 2); 138 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ 139 | } 140 | ts = createstrobj(L, str, l, LUA_TSHRSTR, h); 141 | ts->hnext = *list; 142 | *list = ts; 143 | g->strt.nuse++; 144 | return ts; 145 | } 146 | 147 | 148 | /* 149 | ** new string (with explicit length) 150 | */ 151 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 152 | if (l <= LUAI_MAXSHORTLEN) /* short string? */ 153 | return internshrstr(L, str, l); 154 | else { 155 | if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) 156 | luaM_toobig(L); 157 | return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed); 158 | } 159 | } 160 | 161 | 162 | /* 163 | ** new zero-terminated string 164 | */ 165 | TString *luaS_new (lua_State *L, const char *str) { 166 | return luaS_newlstr(L, str, strlen(str)); 167 | } 168 | 169 | 170 | Udata *luaS_newudata (lua_State *L, size_t s) { 171 | Udata *u; 172 | GCObject *o; 173 | if (s > MAX_SIZE - sizeof(Udata)) 174 | luaM_toobig(L); 175 | o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); 176 | u = gco2u(o); 177 | u->len = s; 178 | u->metatable = NULL; 179 | setuservalue(L, u, luaO_nilobject); 180 | return u; 181 | } 182 | 183 | -------------------------------------------------------------------------------- /engine/lib/matrix.c: -------------------------------------------------------------------------------- 1 | #include "matrix.h" 2 | #include 3 | 4 | static inline int 5 | _inverse_scale(const int *m , int *o) { 6 | if (m[0] == 0 || m[3] == 0) 7 | return 1; 8 | o[0] = (1024 * 1024) / m[0]; 9 | o[1] = 0; 10 | o[2] = 0; 11 | o[3] = (1024 * 1024) / m[3]; 12 | o[4] = - (m[4] * o[0]) / 1024; 13 | o[5] = - (m[5] * o[3]) / 1024; 14 | return 0; 15 | } 16 | 17 | static inline int 18 | _inverse_rot(const int *m, int *o) { 19 | if (m[1] == 0 || m[2] == 0) 20 | return 1; 21 | o[0] = 0; 22 | o[1] = (1024 * 1024) / m[2]; 23 | o[2] = (1024 * 1024) / m[1]; 24 | o[3] = 0; 25 | o[4] = - (m[5] * o[2]) / 1024; 26 | o[5] = - (m[4] * o[1]) / 1024; 27 | return 0; 28 | } 29 | 30 | int 31 | matrix_inverse(const struct matrix *mm, struct matrix *mo) { 32 | const int *m = mm->m; 33 | int *o = mo->m; 34 | if (m[1] == 0 && m[2] == 0) { 35 | return _inverse_scale(m,o); 36 | } 37 | if (m[0] == 0 && m[3] == 0) { 38 | return _inverse_rot(m,o); 39 | } 40 | int t = m[0] * m[3] - m[1] * m[2] ; 41 | if (t == 0) 42 | return 1; 43 | o[0] = (int32_t)((int64_t)m[3] * (1024 * 1024) / t); 44 | o[1] = (int32_t)(- (int64_t)m[1] * (1024 * 1024) / t); 45 | o[2] = (int32_t)(- (int64_t)m[2] * (1024 * 1024) / t); 46 | o[3] = (int32_t)((int64_t)m[0] * (1024 * 1024) / t); 47 | o[4] = - (m[4] * o[0] + m[5] * o[2]) / 1024; 48 | o[5] = - (m[4] * o[1] + m[5] * o[3]) / 1024; 49 | return 0; 50 | } 51 | 52 | // SRT to matrix 53 | 54 | static inline int 55 | icost(int dd) { 56 | static int t[256] = { 57 | 1024,1023,1022,1021,1019,1016,1012,1008,1004,999,993,986,979,972,964,955, 58 | 946,936,925,914,903,890,878,865,851,837,822,807,791,775,758,741, 59 | 724,706,687,668,649,629,609,589,568,547,526,504,482,460,437,414, 60 | 391,368,344,321,297,273,248,224,199,175,150,125,100,75,50,25, 61 | 0,-25,-50,-75,-100,-125,-150,-175,-199,-224,-248,-273,-297,-321,-344,-368, 62 | -391,-414,-437,-460,-482,-504,-526,-547,-568,-589,-609,-629,-649,-668,-687,-706, 63 | -724,-741,-758,-775,-791,-807,-822,-837,-851,-865,-878,-890,-903,-914,-925,-936, 64 | -946,-955,-964,-972,-979,-986,-993,-999,-1004,-1008,-1012,-1016,-1019,-1021,-1022,-1023, 65 | -1024,-1023,-1022,-1021,-1019,-1016,-1012,-1008,-1004,-999,-993,-986,-979,-972,-964,-955, 66 | -946,-936,-925,-914,-903,-890,-878,-865,-851,-837,-822,-807,-791,-775,-758,-741, 67 | -724,-706,-687,-668,-649,-629,-609,-589,-568,-547,-526,-504,-482,-460,-437,-414, 68 | -391,-368,-344,-321,-297,-273,-248,-224,-199,-175,-150,-125,-100,-75,-50,-25, 69 | 0,25,50,75,100,125,150,175,199,224,248,273,297,321,344,368, 70 | 391,414,437,460,482,504,526,547,568,589,609,629,649,668,687,706, 71 | 724,741,758,775,791,807,822,837,851,865,878,890,903,914,925,936, 72 | 946,955,964,972,979,986,993,999,1004,1008,1012,1016,1019,1021,1022,1023, 73 | }; 74 | dd = dd & 0xff; // % 256 75 | 76 | return t[dd]; 77 | } 78 | 79 | static inline int 80 | icosd(int d) { 81 | int dd = d/4; 82 | return icost(dd); 83 | } 84 | 85 | static inline int 86 | isind(int d) { 87 | int dd = 64 - d/4; 88 | return icost(dd); 89 | } 90 | 91 | static inline void 92 | rot_mat(int *m, int d) { 93 | if (d==0) 94 | return; 95 | int cosd = icosd(d); 96 | int sind = isind(d); 97 | 98 | int m0_cosd = m[0] * cosd; 99 | int m0_sind = m[0] * sind; 100 | int m1_cosd = m[1] * cosd; 101 | int m1_sind = m[1] * sind; 102 | int m2_cosd = m[2] * cosd; 103 | int m2_sind = m[2] * sind; 104 | int m3_cosd = m[3] * cosd; 105 | int m3_sind = m[3] * sind; 106 | int m4_cosd = m[4] * cosd; 107 | int m4_sind = m[4] * sind; 108 | int m5_cosd = m[5] * cosd; 109 | int m5_sind = m[5] * sind; 110 | 111 | m[0] = (m0_cosd - m1_sind) /1024; 112 | m[1] = (m0_sind + m1_cosd) /1024; 113 | m[2] = (m2_cosd - m3_sind) /1024; 114 | m[3] = (m2_sind + m3_cosd) /1024; 115 | m[4] = (m4_cosd - m5_sind) /1024; 116 | m[5] = (m4_sind + m5_cosd) /1024; 117 | } 118 | 119 | static inline void 120 | scale_mat(int *m, int sx, int sy) { 121 | if (sx != 1024) { 122 | m[0] = m[0] * sx / 1024; 123 | m[2] = m[2] * sx / 1024; 124 | m[4] = m[4] * sx / 1024; 125 | } 126 | if (sy != 1024) { 127 | m[1] = m[1] * sy / 1024; 128 | m[3] = m[3] * sy / 1024; 129 | m[5] = m[5] * sy / 1024; 130 | } 131 | } 132 | 133 | void 134 | matrix_srt(struct matrix *mm, const struct srt *srt) { 135 | if (!srt) { 136 | return; 137 | } 138 | scale_mat(mm->m, srt->scalex, srt->scaley); 139 | rot_mat(mm->m, srt->rot); 140 | mm->m[4] += srt->offx; 141 | mm->m[5] += srt->offy; 142 | } 143 | 144 | void 145 | matrix_rot(struct matrix *m, int rot) { 146 | rot_mat(m->m, rot); 147 | } 148 | 149 | void 150 | matrix_sr(struct matrix *mat, int sx, int sy, int d) { 151 | int *m = mat->m; 152 | int cosd = icosd(d); 153 | int sind = isind(d); 154 | 155 | int m0_cosd = sx * cosd; 156 | int m0_sind = sx * sind; 157 | int m3_cosd = sy * cosd; 158 | int m3_sind = sy * sind; 159 | 160 | m[0] = m0_cosd /1024; 161 | m[1] = m0_sind /1024; 162 | m[2] = -m3_sind /1024; 163 | m[3] = m3_cosd /1024; 164 | } 165 | 166 | void 167 | matrix_rs(struct matrix *mat, int sx, int sy, int d) { 168 | int *m = mat->m; 169 | int cosd = icosd(d); 170 | int sind = isind(d); 171 | 172 | int m0_cosd = sx * cosd; 173 | int m0_sind = sx * sind; 174 | int m3_cosd = sy * cosd; 175 | int m3_sind = sy * sind; 176 | 177 | m[0] = m0_cosd /1024; 178 | m[1] = m3_sind /1024; 179 | m[2] = -m0_sind /1024; 180 | m[3] = m3_cosd /1024; 181 | } 182 | 183 | void 184 | matrix_scale(struct matrix *m, int sx, int sy) { 185 | scale_mat(m->m, sx, sy); 186 | } 187 | 188 | -------------------------------------------------------------------------------- /engine/lua/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.34 2014/11/02 19:19:04 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ldump_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | 15 | #include "lua.h" 16 | 17 | #include "lobject.h" 18 | #include "lstate.h" 19 | #include "lundump.h" 20 | 21 | 22 | typedef struct { 23 | lua_State *L; 24 | lua_Writer writer; 25 | void *data; 26 | int strip; 27 | int status; 28 | } DumpState; 29 | 30 | 31 | /* 32 | ** All high-level dumps go through DumpVector; you can change it to 33 | ** change the endianness of the result 34 | */ 35 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) 36 | 37 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) 38 | 39 | 40 | static void DumpBlock (const void *b, size_t size, DumpState *D) { 41 | if (D->status == 0) { 42 | lua_unlock(D->L); 43 | D->status = (*D->writer)(D->L, b, size, D->data); 44 | lua_lock(D->L); 45 | } 46 | } 47 | 48 | 49 | #define DumpVar(x,D) DumpVector(&x,1,D) 50 | 51 | 52 | static void DumpByte (int y, DumpState *D) { 53 | lu_byte x = (lu_byte)y; 54 | DumpVar(x, D); 55 | } 56 | 57 | 58 | static void DumpInt (int x, DumpState *D) { 59 | DumpVar(x, D); 60 | } 61 | 62 | 63 | static void DumpNumber (lua_Number x, DumpState *D) { 64 | DumpVar(x, D); 65 | } 66 | 67 | 68 | static void DumpInteger (lua_Integer x, DumpState *D) { 69 | DumpVar(x, D); 70 | } 71 | 72 | 73 | static void DumpString (const TString *s, DumpState *D) { 74 | if (s == NULL) 75 | DumpByte(0, D); 76 | else { 77 | size_t size = s->len + 1; /* include trailing '\0' */ 78 | if (size < 0xFF) 79 | DumpByte(cast_int(size), D); 80 | else { 81 | DumpByte(0xFF, D); 82 | DumpVar(size, D); 83 | } 84 | DumpVector(getstr(s), size - 1, D); /* no need to save '\0' */ 85 | } 86 | } 87 | 88 | 89 | static void DumpCode (const Proto *f, DumpState *D) { 90 | DumpInt(f->sizecode, D); 91 | DumpVector(f->code, f->sizecode, D); 92 | } 93 | 94 | 95 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D); 96 | 97 | static void DumpConstants (const Proto *f, DumpState *D) { 98 | int i; 99 | int n = f->sizek; 100 | DumpInt(n, D); 101 | for (i = 0; i < n; i++) { 102 | const TValue *o = &f->k[i]; 103 | DumpByte(ttype(o), D); 104 | switch (ttype(o)) { 105 | case LUA_TNIL: 106 | break; 107 | case LUA_TBOOLEAN: 108 | DumpByte(bvalue(o), D); 109 | break; 110 | case LUA_TNUMFLT: 111 | DumpNumber(fltvalue(o), D); 112 | break; 113 | case LUA_TNUMINT: 114 | DumpInteger(ivalue(o), D); 115 | break; 116 | case LUA_TSHRSTR: 117 | case LUA_TLNGSTR: 118 | DumpString(tsvalue(o), D); 119 | break; 120 | default: 121 | lua_assert(0); 122 | } 123 | } 124 | } 125 | 126 | 127 | static void DumpProtos (const Proto *f, DumpState *D) { 128 | int i; 129 | int n = f->sizep; 130 | DumpInt(n, D); 131 | for (i = 0; i < n; i++) 132 | DumpFunction(f->p[i], f->source, D); 133 | } 134 | 135 | 136 | static void DumpUpvalues (const Proto *f, DumpState *D) { 137 | int i, n = f->sizeupvalues; 138 | DumpInt(n, D); 139 | for (i = 0; i < n; i++) { 140 | DumpByte(f->upvalues[i].instack, D); 141 | DumpByte(f->upvalues[i].idx, D); 142 | } 143 | } 144 | 145 | 146 | static void DumpDebug (const Proto *f, DumpState *D) { 147 | int i, n; 148 | n = (D->strip) ? 0 : f->sizelineinfo; 149 | DumpInt(n, D); 150 | DumpVector(f->lineinfo, n, D); 151 | n = (D->strip) ? 0 : f->sizelocvars; 152 | DumpInt(n, D); 153 | for (i = 0; i < n; i++) { 154 | DumpString(f->locvars[i].varname, D); 155 | DumpInt(f->locvars[i].startpc, D); 156 | DumpInt(f->locvars[i].endpc, D); 157 | } 158 | n = (D->strip) ? 0 : f->sizeupvalues; 159 | DumpInt(n, D); 160 | for (i = 0; i < n; i++) 161 | DumpString(f->upvalues[i].name, D); 162 | } 163 | 164 | 165 | static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { 166 | if (D->strip || f->source == psource) 167 | DumpString(NULL, D); /* no debug info or same source as its parent */ 168 | else 169 | DumpString(f->source, D); 170 | DumpInt(f->linedefined, D); 171 | DumpInt(f->lastlinedefined, D); 172 | DumpByte(f->numparams, D); 173 | DumpByte(f->is_vararg, D); 174 | DumpByte(f->maxstacksize, D); 175 | DumpCode(f, D); 176 | DumpConstants(f, D); 177 | DumpUpvalues(f, D); 178 | DumpProtos(f, D); 179 | DumpDebug(f, D); 180 | } 181 | 182 | 183 | static void DumpHeader (DumpState *D) { 184 | DumpLiteral(LUA_SIGNATURE, D); 185 | DumpByte(LUAC_VERSION, D); 186 | DumpByte(LUAC_FORMAT, D); 187 | DumpLiteral(LUAC_DATA, D); 188 | DumpByte(sizeof(int), D); 189 | DumpByte(sizeof(size_t), D); 190 | DumpByte(sizeof(Instruction), D); 191 | DumpByte(sizeof(lua_Integer), D); 192 | DumpByte(sizeof(lua_Number), D); 193 | DumpInteger(LUAC_INT, D); 194 | DumpNumber(LUAC_NUM, D); 195 | } 196 | 197 | 198 | /* 199 | ** dump Lua function as precompiled chunk 200 | */ 201 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, 202 | int strip) { 203 | DumpState D; 204 | D.L = L; 205 | D.writer = w; 206 | D.data = data; 207 | D.strip = strip; 208 | D.status = 0; 209 | DumpHeader(&D); 210 | DumpByte(f->sizeupvalues, &D); 211 | DumpFunction(f, NULL, &D); 212 | return D.status; 213 | } 214 | 215 | -------------------------------------------------------------------------------- /engine/lib/audio/decode/ad_wav.c: -------------------------------------------------------------------------------- 1 | #include "../oal_decode.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define check(v, s) do { \ 10 | if(!(v)) { \ 11 | ad_error("decode wav: %s", (s)); \ 12 | goto EXIT; \ 13 | } \ 14 | }while(0) 15 | 16 | 17 | struct riff_header { 18 | uint8_t chunk_id[4]; 19 | uint32_t chunk_sz; 20 | uint8_t format[4]; 21 | }; 22 | 23 | struct wav_format { 24 | uint8_t subchunk_id[4]; 25 | uint32_t subchunk_sz; 26 | uint16_t audio_format; 27 | uint16_t num_channels; 28 | uint32_t sample_rate; 29 | uint32_t byte_rate; 30 | uint16_t block_align; 31 | uint16_t bits_per_sample; 32 | }; 33 | 34 | 35 | struct wav_data { 36 | uint8_t subchunk_id[4]; 37 | uint32_t subchunk_sz; 38 | }; 39 | 40 | static bool 41 | _read_buffer(FILE* fp, uint8_t* out_buffer, size_t sz) { 42 | size_t ret = fread(out_buffer, 1, sz, fp); 43 | return ret==sz; 44 | } 45 | 46 | 47 | static bool 48 | _read_short(FILE* fp, uint16_t* out_val) { 49 | uint8_t buff[2]; 50 | size_t ret = fread(buff, 1, 2, fp); 51 | if(ret==2){ 52 | *out_val = ((uint16_t)(buff[1]))<<8; 53 | *out_val |= ((uint16_t)(buff[0])); 54 | return true; 55 | } 56 | return false; 57 | } 58 | 59 | static bool 60 | _read_int(FILE* fp, uint32_t* out_val) { 61 | uint8_t buff[4]; 62 | size_t ret = fread(buff, 1, 4, fp); 63 | if(ret==4){ 64 | *out_val = ((uint32_t)(buff[3]))<<24; 65 | *out_val |= ((uint32_t)(buff[2]))<<16; 66 | *out_val |= ((uint32_t)(buff[1]))<<8; 67 | *out_val |= ((uint32_t)(buff[0])); 68 | return true; 69 | } 70 | return false; 71 | } 72 | 73 | static bool 74 | _read_riff(FILE* fp, struct riff_header* out_val) { 75 | if(_read_buffer(fp, out_val->chunk_id, 4) && 76 | _read_int(fp, &(out_val->chunk_sz)) && 77 | _read_buffer(fp, out_val->format, 4) && 78 | 0==memcmp(out_val->chunk_id, "RIFF", 4) && 79 | 0==memcmp(out_val->format, "WAVE", 4) ){ 80 | return true; 81 | } 82 | return false; 83 | } 84 | 85 | static bool 86 | _read_wave_foramt(FILE* fp, struct wav_format* out_val) { 87 | if(_read_buffer(fp, out_val->subchunk_id, 4) && 88 | _read_int(fp, &out_val->subchunk_sz) && 89 | _read_short(fp, &out_val->audio_format) && 90 | _read_short(fp, &out_val->num_channels) && 91 | _read_int(fp, &out_val->sample_rate) && 92 | _read_int(fp, &out_val->byte_rate) && 93 | _read_short(fp, &out_val->block_align) && 94 | _read_short(fp, &out_val->bits_per_sample) && 95 | 0==memcmp(out_val->subchunk_id, "fmt ", 4) ){ 96 | return true; 97 | } 98 | return false; 99 | } 100 | 101 | static bool 102 | _read_wave_data(FILE* fp, struct wav_data* out_val) { 103 | if(_read_buffer(fp, out_val->subchunk_id, 4) && 104 | _read_int(fp, &out_val->subchunk_sz) ){ 105 | return true; 106 | } 107 | return false; 108 | } 109 | 110 | 111 | static struct oal_info* 112 | _decode_wav (const char* filepath, struct oal_info* out) { 113 | struct oal_info* ret = NULL; 114 | 115 | FILE* fp = NULL; 116 | filepath = (filepath)?(filepath):(""); 117 | fp = fopen(filepath, "rb"); 118 | if(!fp) { 119 | ad_error("open file : %s error", filepath); 120 | } 121 | 122 | uint8_t* buffer = NULL; 123 | // read riff header 124 | struct riff_header riff; 125 | check(_read_riff(fp, &riff), "read riff header error"); 126 | 127 | // read wave format 128 | struct wav_format wav_fmt; 129 | _read_wave_foramt(fp, &wav_fmt); 130 | 131 | 132 | // read wave data 133 | struct wav_data data_fmt; 134 | for(;;) { 135 | check(_read_wave_data(fp, &data_fmt), "read wave data error"); 136 | if(memcmp(data_fmt.subchunk_id, "data", 4)==0){ 137 | break; 138 | }else { 139 | fseek(fp, data_fmt.subchunk_sz, SEEK_CUR); // jump not need chunk 140 | } 141 | } 142 | 143 | buffer = malloc(data_fmt.subchunk_sz); 144 | check(buffer, "malloc buffer error"); 145 | 146 | check(_read_buffer(fp, buffer, data_fmt.subchunk_sz), "read buffer error"); 147 | 148 | // 149 | strcpy(out->type, "wav"); 150 | out->freq = wav_fmt.sample_rate; 151 | out->data = buffer; 152 | out->size = data_fmt.subchunk_sz; 153 | 154 | // set format 155 | if(wav_fmt.num_channels==1){ 156 | if(wav_fmt.bits_per_sample==8) 157 | out->format = AL_FORMAT_MONO8; 158 | else if(wav_fmt.bits_per_sample==16) 159 | out->format = AL_FORMAT_MONO16; 160 | else { 161 | ad_error("not support format"); 162 | goto EXIT; 163 | } 164 | }else if (wav_fmt.num_channels==2){ 165 | if(wav_fmt.bits_per_sample==8) 166 | out->format=AL_FORMAT_STEREO8; 167 | else if(wav_fmt.bits_per_sample==16) 168 | out->format=AL_FORMAT_STEREO16; 169 | else{ 170 | ad_error("not support format"); 171 | goto EXIT; 172 | } 173 | }else { 174 | ad_error("not support format"); 175 | goto EXIT; 176 | } 177 | ret = out; 178 | 179 | EXIT: 180 | fclose(fp); 181 | if(buffer && !ret) free(buffer); 182 | return ret; 183 | } 184 | 185 | 186 | int 187 | adl_decode_wav(lua_State* L) { 188 | const char* file = lua_tostring(L, 1); 189 | struct oal_info out = {0}; 190 | if(_decode_wav(file, &out)){ 191 | return ad_new_info(L, &out); 192 | } else { 193 | luaL_error(L, ad_last_error()); 194 | } 195 | return 0; 196 | } 197 | 198 | -------------------------------------------------------------------------------- /engine/lib/lmatrix.c: -------------------------------------------------------------------------------- 1 | #include "lmatrix.h" 2 | #include "matrix.h" 3 | #include "spritepack.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | static int 10 | lnew(lua_State *L) { 11 | lua_settop(L,1); 12 | struct matrix *m = (struct matrix *)lua_newuserdata(L, sizeof(*m)); 13 | int *mat = m->m; 14 | if (lua_istable(L,1) && lua_rawlen(L,1)==6) { 15 | int i; 16 | for (i=0;i<6;i++) { 17 | lua_rawgeti(L,1,i+1); 18 | mat[i] = (int)lua_tointeger(L,-1); 19 | lua_pop(L,1); 20 | } 21 | } else if (lua_isuserdata(L,1)) { 22 | // It's a matrix 23 | memcpy(mat, lua_touserdata(L,1), 6 * sizeof(int)); 24 | } else { 25 | mat[0] = 1024; 26 | mat[1] = 0; 27 | mat[2] = 0; 28 | mat[3] = 1024; 29 | mat[4] = 0; 30 | mat[5] = 0; 31 | } 32 | return 1; 33 | } 34 | 35 | static int 36 | lmul(lua_State *L) { 37 | struct matrix *m1 = (struct matrix *)lua_touserdata(L, 1); 38 | struct matrix *m2 = (struct matrix *)lua_touserdata(L, 2); 39 | struct matrix source = *m1; 40 | matrix_mul(m1, &source, m2); 41 | lua_settop(L,1); 42 | return 1; 43 | } 44 | 45 | static int 46 | llmul(lua_State *L) { 47 | struct matrix *m1 = (struct matrix *)lua_touserdata(L, 1); 48 | struct matrix *m2 = (struct matrix *)lua_touserdata(L, 2); 49 | struct matrix source = *m1; 50 | matrix_mul(m1, m2, &source); 51 | lua_settop(L,1); 52 | return 1; 53 | } 54 | 55 | static int 56 | linverse(lua_State *L) { 57 | struct matrix *m = (struct matrix *)lua_touserdata(L, 1); 58 | struct matrix source = *m; 59 | if (matrix_inverse(&source, m)) { 60 | return luaL_error(L, "Invalid matrix"); 61 | } 62 | lua_settop(L,1); 63 | return 1; 64 | } 65 | 66 | static int 67 | ltrans(lua_State *L) { 68 | struct matrix *m = (struct matrix *)lua_touserdata(L, 1); 69 | double x = luaL_checknumber(L,2); 70 | double y = luaL_checknumber(L,3); 71 | m->m[4] += x * SCREEN_SCALE; 72 | m->m[5] += y * SCREEN_SCALE; 73 | 74 | lua_settop(L,1); 75 | return 1; 76 | } 77 | 78 | static int 79 | lscale(lua_State *L) { 80 | struct matrix *m = (struct matrix *)lua_touserdata(L, 1); 81 | double sx = luaL_checknumber(L,2); 82 | double sy = luaL_optnumber(L,3,sx); 83 | matrix_scale(m, sx * 1024, sy * 1024); 84 | 85 | lua_settop(L,1); 86 | return 1; 87 | } 88 | 89 | static int 90 | lidentity(lua_State *L) { 91 | struct matrix *m = (struct matrix *)lua_touserdata(L,1); 92 | if (m == NULL) { 93 | return luaL_error(L, "Need a matrix"); 94 | } 95 | int n = lua_gettop(L); 96 | int *mat = m->m; 97 | int scale=1024; 98 | int x=0,y=0; 99 | switch(n) { 100 | case 4: 101 | scale = luaL_checknumber(L,4) * 1024; 102 | // x,y,scale 103 | // go though 104 | case 3: 105 | // x,y 106 | x = luaL_checknumber(L,2) * SCREEN_SCALE; 107 | y = luaL_checknumber(L,3) * SCREEN_SCALE; 108 | break; 109 | case 2: 110 | // scale 111 | scale = luaL_checknumber(L,2) * 1024; 112 | break; 113 | case 1: 114 | break; 115 | default: 116 | return luaL_error(L, "Invalid parameter"); 117 | } 118 | mat[0] = scale; 119 | mat[1] = 0; 120 | mat[2] = 0; 121 | mat[3] = scale; 122 | mat[4] = x; 123 | mat[5] = y; 124 | lua_settop(L,1); 125 | return 1; 126 | } 127 | 128 | static int 129 | lrot(lua_State *L) { 130 | struct matrix *m = (struct matrix *)lua_touserdata(L, 1); 131 | double r = luaL_checknumber(L,2); 132 | matrix_rot(m, r * (1024.0 / 360.0)); 133 | 134 | lua_settop(L,1); 135 | return 1; 136 | } 137 | 138 | static int 139 | lsr(lua_State *L) { 140 | struct matrix *m = (struct matrix *)lua_touserdata(L, 1); 141 | 142 | int sx=1024,sy=1024,r=0; 143 | int n = lua_gettop(L); 144 | switch (n) { 145 | case 4: 146 | // sx,sy,rot 147 | r = luaL_checknumber(L,4) * (1024.0 / 360.0); 148 | // go through 149 | case 3: 150 | // sx, sy 151 | sx = luaL_checknumber(L,2) * 1024; 152 | sy = luaL_checknumber(L,3) * 1024; 153 | break; 154 | case 2: 155 | // rot 156 | r = luaL_checknumber(L,2) * (1024.0 / 360.0); 157 | break; 158 | } 159 | matrix_sr(m, sx, sy, r); 160 | 161 | return 0; 162 | } 163 | 164 | static int 165 | lrs(lua_State *L) { 166 | struct matrix *m = (struct matrix *)lua_touserdata(L, 1); 167 | 168 | int sx=1024,sy=1024,r=0; 169 | int n = lua_gettop(L); 170 | switch (n) { 171 | case 4: 172 | // sx,sy,rot 173 | r = luaL_checknumber(L,4) * (1024.0 / 360.0); 174 | // go through 175 | case 3: 176 | // sx, sy 177 | sx = luaL_checknumber(L,2) * 1024; 178 | sy = luaL_checknumber(L,3) * 1024; 179 | break; 180 | case 2: 181 | // rot 182 | r = luaL_checknumber(L,2) * (1024.0 / 360.0); 183 | break; 184 | } 185 | matrix_rs(m, sx, sy, r); 186 | 187 | return 0; 188 | } 189 | 190 | static int 191 | ltostring(lua_State *L) { 192 | struct matrix *mat = (struct matrix *)lua_touserdata(L, 1); 193 | int *m = mat->m; 194 | lua_pushfstring(L, "Mat(%d,%d,%d,%d,%d,%d)", 195 | m[0],m[1],m[2],m[3],m[4],m[5]); 196 | return 1; 197 | } 198 | 199 | static int 200 | lexport(lua_State *L) { 201 | int i; 202 | struct matrix *mat = (struct matrix *)lua_touserdata(L, 1); 203 | int *m = mat->m; 204 | for (i=0;i<6;i++) { 205 | lua_pushinteger(L, m[i]); 206 | } 207 | return 6; 208 | } 209 | 210 | static int 211 | limport(lua_State *L) { 212 | int i; 213 | struct matrix *mat = (struct matrix *)lua_touserdata(L, 1); 214 | int *m = mat->m; 215 | for (i=0;i<6;i++) { 216 | m[i] = (int)luaL_checkinteger(L,i+2); 217 | } 218 | return 0; 219 | } 220 | 221 | int 222 | ejoy2d_matrix(lua_State *L) { 223 | luaL_Reg l[] = { 224 | { "new", lnew }, 225 | { "scale", lscale }, 226 | { "trans", ltrans }, 227 | { "rot", lrot }, 228 | { "sr", lsr }, 229 | { "rs", lrs }, 230 | { "inverse", linverse }, 231 | { "mul", lmul }, 232 | { "lmul", llmul }, 233 | { "tostring", ltostring }, 234 | { "identity", lidentity}, 235 | { "export", lexport }, 236 | { "import", limport }, 237 | { NULL, NULL }, 238 | }; 239 | luaL_newlib(L,l); 240 | return 1; 241 | } 242 | -------------------------------------------------------------------------------- /res/tool/ejpackage.lua: -------------------------------------------------------------------------------- 1 | local libos = require "libos" 2 | local utils = require "utils" 3 | 4 | -- file templates 5 | local TEMPLATE_BODY = [[ 6 | return { 7 | 8 | %s 9 | } 10 | ]] 11 | 12 | local TEMPLATE_LABEL = [[ 13 | { 14 | type = "label", 15 | id = %d, 16 | export = "default_label", 17 | width = 200, 18 | height = 100, 19 | font = "", 20 | align = 0, 21 | size = 16, 22 | color = 0xffffffff, 23 | noedge = true, 24 | }, 25 | ]] 26 | 27 | local TEMPLATE_PICTURE = [[ 28 | { 29 | type = "picture", 30 | id = %d, 31 | export = "%s", 32 | { 33 | tex = %d, 34 | src = { %d, %d, %d, %d, %d, %d, %d, %d }, 35 | screen = { %d, %d, %d, %d, %d, %d, %d, %d }, 36 | }, 37 | }, 38 | ]] 39 | 40 | local TEMPLATE_ANIMATION = [[ 41 | { 42 | type = "animation", 43 | id = %d, 44 | export = "%s", 45 | component = { 46 | %s }, 47 | %s 48 | }, 49 | ]] 50 | 51 | -- ejoy2d package 52 | local pkg_mt = {} 53 | pkg_mt.__index = pkg_mt 54 | 55 | function pkg_mt:add_img(img) 56 | table.insert(self.sheets, img) -- treat image as sheet with a single rect 57 | local item = {} 58 | item.type = "picture" 59 | item.id = self:_next_id() 60 | item.data = {#self.sheets, {0,0}, {img.w,img.h}, {img.ox,img.oy}} 61 | self.items[img.name] = item 62 | end 63 | 64 | function pkg_mt:add_sheet(sheet) 65 | table.insert(self.sheets, sheet) 66 | for _,v in ipairs(sheet.imgs) do 67 | local item = {} 68 | item.type = "picture" 69 | item.id = self:_next_id() 70 | item.data = {#self.sheets, v.pos, v.size, v.offset} -- texid, pos, size, offset 71 | self.items[v.name] = item 72 | end 73 | end 74 | 75 | function pkg_mt:add_anim(anim) -- image name sequence 76 | local item = {} 77 | item.type = "animation" 78 | item.id = self:_next_id() 79 | item.data = anim.actions -- frames 80 | self.items[anim.name] = item 81 | end 82 | 83 | function pkg_mt:save(path) 84 | -- save image sheet 85 | for i,v in ipairs(self.sheets) do 86 | local picture_path = string.format("%s/%s.%d", path, self.name, i) 87 | v:save(picture_path, false) 88 | end 89 | 90 | -- save description file 91 | local body = string.format(TEMPLATE_LABEL, self:_next_id()) 92 | for k,v in pairs(self.items) do 93 | if v.type == "picture" then 94 | body = body..self:_serialize_picture(v.id, k, v.data) 95 | end 96 | end 97 | for k,v in pairs(self.items) do 98 | if v.type == "animation" then 99 | body = body..self:_serialize_animation(v.id, k, v.data) 100 | end 101 | end 102 | 103 | local all = string.format(TEMPLATE_BODY, body) 104 | local lua_path = string.format("%s/%s.lua", path, self.name) 105 | libos:writefile(lua_path, all) 106 | 107 | utils:logf("ejoy2d package <%s> saved", self.name) 108 | end 109 | 110 | function pkg_mt:_serialize_picture(id, name, data) 111 | local pos = data[2] 112 | local size = data[3] 113 | local offset = data[4] 114 | 115 | local tex = data[1] 116 | 117 | local sl = pos[1] 118 | local sr = pos[1] + size[1] 119 | local st = pos[2] 120 | local sb = pos[2] + size[2] 121 | 122 | local dl = (-size[1]/2 + offset[1]) * 16 -- left = (-w/2 + ox) * 16 123 | local dr = (size[1]/2 + offset[1]) * 16 124 | local dt = (-size[2]/2 + offset[2]) * 16 125 | local db = (size[2]/2 + offset[2]) * 16 126 | 127 | return string.format(TEMPLATE_PICTURE, id, name, tex, 128 | sl, st, sl, sb, sr, sb, sr, st, 129 | dl, dt, dl, db, dr, db, dr, dt) 130 | end 131 | 132 | function pkg_mt:_serialize_animation(id, name, data) 133 | local com2idx = {} 134 | local idx2com = {} 135 | 136 | local str_a = "" -- action section 137 | 138 | for _,act in ipairs(data) do 139 | local frame_list = {} 140 | for _,frm in ipairs(act) do -- multiple frames in one action 141 | local com_list = {} 142 | for _,com in ipairs(frm) do -- multiple components in one frame 143 | if not com2idx[com.name] then 144 | table.insert(idx2com, com.name) 145 | com2idx[com.name] = #idx2com - 1 -- idx base 0 146 | end 147 | 148 | local idx_only = true 149 | 150 | local str_com = string.format("{ index = %d, ", com2idx[com.name]) 151 | if com.scale or com.rot or com.trans then 152 | idx_only = false 153 | local mat = utils:create_matrix(com.scale, com.rot, com.trans) 154 | str_com = str_com..string.format("mat = { %d, %d, %d, %d, %d, %d }, ", 155 | mat[1], mat[2], mat[3], mat[4], mat[5], mat[6]) 156 | end 157 | if com.color then 158 | idx_only = false 159 | str_com = str_com..string.format("color = 0x%08x", com.color) 160 | end 161 | if com.additive then 162 | idx_only = false 163 | str_com = str_com..string.format("additive = 0x%08x", com.additive) 164 | end 165 | str_com = str_com.."}" 166 | 167 | if idx_only then 168 | table.insert(com_list, tostring(com2idx[com.name])) -- simple component without attributes 169 | else 170 | table.insert(com_list, str_com) 171 | end 172 | end 173 | local str_f = string.format("\t\t{ %s },", table.concat(com_list, ", ")) 174 | table.insert(frame_list, str_f) 175 | end 176 | str_a = string.format("\t{\n%s\n\t},", table.concat(frame_list, "\n")) 177 | end 178 | 179 | local str_c = "" -- component section 180 | for _,com in ipairs(idx2com) do 181 | local item = self.items[com] 182 | if item then 183 | str_c = string.format("%s\t\t{ id = %d, name = \"%s\" },\n", str_c, item.id, com) -- one component one line 184 | else 185 | str_c = string.format("%s\t\t{ name = \"%s\" },\n", str_c, com) -- anchor 186 | end 187 | end 188 | 189 | return string.format(TEMPLATE_ANIMATION, id, name, str_c, str_a) 190 | end 191 | 192 | function pkg_mt:_next_id() 193 | local ret = self._id 194 | self._id = self._id + 1 195 | return ret 196 | end 197 | 198 | -- ejoy2d package module 199 | local M = {} 200 | 201 | function M:new_pkg(name) 202 | local pkg = {} 203 | pkg._id = 0 204 | pkg.name = name 205 | pkg.sheets = {} 206 | pkg.items = {} -- name:item 207 | return setmetatable(pkg, pkg_mt) 208 | end 209 | 210 | return M -------------------------------------------------------------------------------- /engine/lua/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | /* number of bits to consider in a number */ 23 | #if !defined(LUA_NBITS) 24 | #define LUA_NBITS 32 25 | #endif 26 | 27 | 28 | /* 29 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 30 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 31 | ** number of bits in a lua_Unsigned.) 32 | */ 33 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 34 | 35 | 36 | /* macro to trim extra bits */ 37 | #define trim(x) ((x) & ALLONES) 38 | 39 | 40 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 41 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 42 | 43 | 44 | 45 | static lua_Unsigned andaux (lua_State *L) { 46 | int i, n = lua_gettop(L); 47 | lua_Unsigned r = ~(lua_Unsigned)0; 48 | for (i = 1; i <= n; i++) 49 | r &= luaL_checkunsigned(L, i); 50 | return trim(r); 51 | } 52 | 53 | 54 | static int b_and (lua_State *L) { 55 | lua_Unsigned r = andaux(L); 56 | lua_pushunsigned(L, r); 57 | return 1; 58 | } 59 | 60 | 61 | static int b_test (lua_State *L) { 62 | lua_Unsigned r = andaux(L); 63 | lua_pushboolean(L, r != 0); 64 | return 1; 65 | } 66 | 67 | 68 | static int b_or (lua_State *L) { 69 | int i, n = lua_gettop(L); 70 | lua_Unsigned r = 0; 71 | for (i = 1; i <= n; i++) 72 | r |= luaL_checkunsigned(L, i); 73 | lua_pushunsigned(L, trim(r)); 74 | return 1; 75 | } 76 | 77 | 78 | static int b_xor (lua_State *L) { 79 | int i, n = lua_gettop(L); 80 | lua_Unsigned r = 0; 81 | for (i = 1; i <= n; i++) 82 | r ^= luaL_checkunsigned(L, i); 83 | lua_pushunsigned(L, trim(r)); 84 | return 1; 85 | } 86 | 87 | 88 | static int b_not (lua_State *L) { 89 | lua_Unsigned r = ~luaL_checkunsigned(L, 1); 90 | lua_pushunsigned(L, trim(r)); 91 | return 1; 92 | } 93 | 94 | 95 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { 96 | if (i < 0) { /* shift right? */ 97 | i = -i; 98 | r = trim(r); 99 | if (i >= LUA_NBITS) r = 0; 100 | else r >>= i; 101 | } 102 | else { /* shift left */ 103 | if (i >= LUA_NBITS) r = 0; 104 | else r <<= i; 105 | r = trim(r); 106 | } 107 | lua_pushunsigned(L, r); 108 | return 1; 109 | } 110 | 111 | 112 | static int b_lshift (lua_State *L) { 113 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2)); 114 | } 115 | 116 | 117 | static int b_rshift (lua_State *L) { 118 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 119 | } 120 | 121 | 122 | static int b_arshift (lua_State *L) { 123 | lua_Unsigned r = luaL_checkunsigned(L, 1); 124 | lua_Integer i = luaL_checkinteger(L, 2); 125 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 126 | return b_shift(L, r, -i); 127 | else { /* arithmetic shift for 'negative' number */ 128 | if (i >= LUA_NBITS) r = ALLONES; 129 | else 130 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 131 | lua_pushunsigned(L, r); 132 | return 1; 133 | } 134 | } 135 | 136 | 137 | static int b_rot (lua_State *L, lua_Integer d) { 138 | lua_Unsigned r = luaL_checkunsigned(L, 1); 139 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 140 | r = trim(r); 141 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 142 | r = (r << i) | (r >> (LUA_NBITS - i)); 143 | lua_pushunsigned(L, trim(r)); 144 | return 1; 145 | } 146 | 147 | 148 | static int b_lrot (lua_State *L) { 149 | return b_rot(L, luaL_checkinteger(L, 2)); 150 | } 151 | 152 | 153 | static int b_rrot (lua_State *L) { 154 | return b_rot(L, -luaL_checkinteger(L, 2)); 155 | } 156 | 157 | 158 | /* 159 | ** get field and width arguments for field-manipulation functions, 160 | ** checking whether they are valid. 161 | ** ('luaL_error' called without 'return' to avoid later warnings about 162 | ** 'width' being used uninitialized.) 163 | */ 164 | static int fieldargs (lua_State *L, int farg, int *width) { 165 | lua_Integer f = luaL_checkinteger(L, farg); 166 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 167 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 168 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 169 | if (f + w > LUA_NBITS) 170 | luaL_error(L, "trying to access non-existent bits"); 171 | *width = (int)w; 172 | return (int)f; 173 | } 174 | 175 | 176 | static int b_extract (lua_State *L) { 177 | int w; 178 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); 179 | int f = fieldargs(L, 2, &w); 180 | r = (r >> f) & mask(w); 181 | lua_pushunsigned(L, r); 182 | return 1; 183 | } 184 | 185 | 186 | static int b_replace (lua_State *L) { 187 | int w; 188 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); 189 | lua_Unsigned v = luaL_checkunsigned(L, 2); 190 | int f = fieldargs(L, 3, &w); 191 | int m = mask(w); 192 | v &= m; /* erase bits outside given width */ 193 | r = (r & ~(m << f)) | (v << f); 194 | lua_pushunsigned(L, r); 195 | return 1; 196 | } 197 | 198 | 199 | static const luaL_Reg bitlib[] = { 200 | {"arshift", b_arshift}, 201 | {"band", b_and}, 202 | {"bnot", b_not}, 203 | {"bor", b_or}, 204 | {"bxor", b_xor}, 205 | {"btest", b_test}, 206 | {"extract", b_extract}, 207 | {"lrotate", b_lrot}, 208 | {"lshift", b_lshift}, 209 | {"replace", b_replace}, 210 | {"rrotate", b_rrot}, 211 | {"rshift", b_rshift}, 212 | {NULL, NULL} 213 | }; 214 | 215 | 216 | 217 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 218 | luaL_newlib(L, bitlib); 219 | return 1; 220 | } 221 | 222 | 223 | #else /* }{ */ 224 | 225 | 226 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 227 | return luaL_error(L, "library 'bit32' has been deprecated"); 228 | } 229 | 230 | #endif /* } */ 231 | -------------------------------------------------------------------------------- /engine/lib/lshader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "shader.h" 6 | #include "screen.h" 7 | #include "texture.h" 8 | #include "array.h" 9 | #include "spritepack.h" 10 | #include "render.h" 11 | #include "material.h" 12 | #include "fault.h" 13 | 14 | static int 15 | lload(lua_State *L) { 16 | int prog = (int)luaL_checkinteger(L,1); 17 | const char *fs = luaL_checkstring(L, 2); 18 | const char *vs = luaL_checkstring(L, 3); 19 | if (lua_istable(L, 4)) { 20 | int texture_number = lua_rawlen(L, 4); 21 | ARRAY(const char *, name, texture_number); 22 | luaL_checkstack(L, texture_number + 1, NULL); 23 | int i; 24 | for (i=0;i sizeof(v)/sizeof(v[0])+2) 173 | return luaL_error(L, "too many agrument"); 174 | int n = top-2; 175 | int i; 176 | for (i=0;i