├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── doc └── apicn.md ├── ejoy2d ├── geometry.lua ├── init.lua ├── matrix.lua ├── particle.lua ├── richtext.lua ├── shader.lua ├── simplepackage.lua ├── sprite.lua └── spritepack.lua ├── examples ├── asset │ ├── bird_config.lua │ ├── birds.1.pgm │ ├── birds.1.ppm │ ├── birds.lua │ ├── particle.1.pgm │ ├── particle.1.ppm │ ├── particle.lua │ ├── particle_particle_config.lua │ ├── sample.1.pgm │ ├── sample.1.ppm │ └── sample.lua ├── ex01.lua ├── ex02.lua ├── ex03.lua ├── ex04.lua ├── ex05.lua ├── ex06.lua ├── ex07.lua ├── ex08.lua ├── ex09.lua └── flappybird.lua ├── ios └── example │ ├── example.xcodeproj │ └── project.pbxproj │ └── example │ ├── EJAppDelegate.h │ ├── EJAppDelegate.m │ ├── EJViewController.h │ ├── EJViewController.m │ ├── Images.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json │ ├── en.lproj │ └── InfoPlist.strings │ ├── example-Info.plist │ ├── example-Prefix.pch │ ├── font.m │ ├── main.m │ ├── winfw.c │ └── winfw.h ├── lib ├── array.h ├── dfont.c ├── dfont.h ├── ejoy2dgame.c ├── ejoy2dgame.h ├── fault.c ├── fault.h ├── label.c ├── label.h ├── lejoy2dcore.c ├── lgeometry.c ├── lgeometry.h ├── list.h ├── lmatrix.c ├── lmatrix.h ├── lparticle.c ├── lrenderbuffer.c ├── lrenderbuffer.h ├── lshader.c ├── lsprite.c ├── material.h ├── matrix.c ├── matrix.h ├── opengl.h ├── particle.c ├── particle.h ├── platform_print.h ├── ppm.c ├── ppm.h ├── render │ ├── blendmode.h │ ├── block.h │ ├── carray.c │ ├── carray.h │ ├── log.c │ ├── log.h │ ├── render.c │ └── render.h ├── renderbuffer.c ├── renderbuffer.h ├── scissor.c ├── scissor.h ├── screen.c ├── screen.h ├── screenshot.c ├── screenshot.h ├── shader.c ├── shader.h ├── sprite.c ├── sprite.h ├── spritepack.c ├── spritepack.h ├── texture.c └── texture.h ├── lua ├── lapi.c ├── lapi.h ├── lauxlib.c ├── lauxlib.h ├── lbaselib.c ├── lbitlib.c ├── lcode.c ├── lcode.h ├── lcorolib.c ├── lctype.c ├── lctype.h ├── ldblib.c ├── ldebug.c ├── ldebug.h ├── ldo.c ├── ldo.h ├── ldump.c ├── lfunc.c ├── lfunc.h ├── lgc.c ├── lgc.h ├── linit.c ├── liolib.c ├── llex.c ├── llex.h ├── llimits.h ├── lmathlib.c ├── lmem.c ├── lmem.h ├── loadlib.c ├── lobject.c ├── lobject.h ├── lopcodes.c ├── lopcodes.h ├── loslib.c ├── lparser.c ├── lparser.h ├── lprefix.h ├── lstate.c ├── lstate.h ├── lstring.c ├── lstring.h ├── lstrlib.c ├── ltable.c ├── ltable.h ├── ltablib.c ├── ltm.c ├── ltm.h ├── lua.h ├── lua.hpp ├── luaconf.h ├── lualib.h ├── lundump.c ├── lundump.h ├── lutf8lib.c ├── lvm.c ├── lvm.h ├── lzio.c └── lzio.h ├── mac └── example │ ├── example.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── example.xccheckout │ └── example │ ├── window.c │ ├── winfont.c │ ├── winfw.c │ └── winfw.h ├── mingw ├── window.c ├── winfont.c ├── winfw.c └── winfw.h ├── msvc ├── .gitignore ├── build │ ├── ejoy2d.props │ ├── ejoy2d.sln │ ├── ejoy2d │ │ ├── ejoy2d.vcxproj │ │ ├── ejoy2d.vcxproj.filters │ │ └── ejoy2d.vcxproj.user │ ├── glew │ │ ├── glew.vcxproj │ │ └── glew.vcxproj.filters │ └── lua │ │ ├── lua.vcxproj │ │ └── lua.vcxproj.filters ├── ejoy2d.root ├── include │ ├── lauxlib.h │ ├── lua.h │ ├── lua_path.h │ ├── lualib.h │ └── stdbool.h ├── make.bat └── src │ ├── ejoy2d │ └── winmain.c │ └── glew │ ├── GL │ ├── glew.h │ ├── glxew.h │ └── wglew.h │ ├── glew.c │ ├── glewinfo.c │ └── visualinfo.c └── posix ├── glx.c ├── window.c ├── winfont.c ├── winfw.c └── winfw.h /.gitignore: -------------------------------------------------------------------------------- 1 | ios/example/example.xcodeproj/project.xcworkspace/ 2 | ios/example/example.xcodeproj/xcuserdata/ 3 | ej2d 4 | ej2d.dSYM/* 5 | *.exe 6 | *.dll 7 | *.ilk 8 | mac/example/example.xcodeproj/xcuserdata 9 | mac/example/example.xcodeproj/project.xcworkspace/xcuserdata 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Ejoy.com Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY : mingw ej2d linux undefined 2 | 3 | CFLAGS = -g -Wall -Ilib -Ilib/render -Ilua -D EJOY2D_OS=$(OS) -D FONT_EDGE_HASH 4 | LDFLAGS := 5 | 6 | RENDER := \ 7 | lib/render/render.c \ 8 | lib/render/carray.c \ 9 | lib/render/log.c 10 | 11 | EJOY2D := \ 12 | lib/shader.c \ 13 | lib/lshader.c \ 14 | lib/ejoy2dgame.c \ 15 | lib/fault.c \ 16 | lib/screen.c \ 17 | lib/texture.c \ 18 | lib/ppm.c \ 19 | lib/spritepack.c \ 20 | lib/sprite.c \ 21 | lib/lsprite.c \ 22 | lib/matrix.c \ 23 | lib/lmatrix.c \ 24 | lib/dfont.c \ 25 | lib/label.c \ 26 | lib/particle.c \ 27 | lib/lparticle.c \ 28 | lib/scissor.c \ 29 | lib/renderbuffer.c \ 30 | lib/lrenderbuffer.c \ 31 | lib/lgeometry.c 32 | 33 | SRC := $(EJOY2D) $(RENDER) 34 | 35 | LUASRC := \ 36 | lua/lapi.c \ 37 | lua/lauxlib.c \ 38 | lua/lbaselib.c \ 39 | lua/lbitlib.c \ 40 | lua/lcode.c \ 41 | lua/lcorolib.c \ 42 | lua/lctype.c \ 43 | lua/ldblib.c \ 44 | lua/ldebug.c \ 45 | lua/ldo.c \ 46 | lua/ldump.c \ 47 | lua/lfunc.c \ 48 | lua/lgc.c \ 49 | lua/linit.c \ 50 | lua/liolib.c \ 51 | lua/llex.c \ 52 | lua/lmathlib.c \ 53 | lua/lmem.c \ 54 | lua/loadlib.c \ 55 | lua/lobject.c \ 56 | lua/lopcodes.c \ 57 | lua/loslib.c \ 58 | lua/lparser.c \ 59 | lua/lstate.c \ 60 | lua/lstring.c \ 61 | lua/lstrlib.c \ 62 | lua/ltable.c \ 63 | lua/ltablib.c \ 64 | lua/ltm.c \ 65 | lua/lundump.c \ 66 | lua/lutf8lib.c \ 67 | lua/lvm.c \ 68 | lua/lzio.c 69 | 70 | CC=gcc 71 | UNAME=$(shell uname) 72 | SYS=$(if $(filter Linux%,$(UNAME)),linux,\ 73 | $(if $(filter MINGW%,$(UNAME)),mingw,\ 74 | $(if $(filter Darwin%,$(UNAME)),macosx,\ 75 | undefined\ 76 | ))) 77 | 78 | all: $(SYS) 79 | 80 | undefined: 81 | @echo "I can't guess your platform, please do 'make PLATFORM' where PLATFORM is one of these:" 82 | @echo " linux mingw macosx" 83 | 84 | 85 | mingw : OS := WINDOWS 86 | mingw : TARGET := ej2d.exe 87 | mingw : CFLAGS += -I/usr/include 88 | mingw : LDFLAGS += -L/usr/bin -lgdi32 -lglew32 -lopengl32 89 | mingw : SRC += mingw/window.c mingw/winfw.c mingw/winfont.c 90 | 91 | mingw : $(SRC) ej2d 92 | 93 | winlib : OS := WINDOWS 94 | winlib : TARGET := ejoy2d.dll 95 | winlib : CFLAGS += -I/usr/include -I/usr/local/include --shared 96 | winlib : LDFLAGS += -L/usr/bin -lgdi32 -lglew32 -lopengl32 -L/usr/local/bin -llua53 97 | winlib : SRC += mingw/winfont.c lib/lejoy2dcore.c 98 | 99 | winlib : $(SRC) ej2dlib 100 | 101 | ej2dlib : 102 | $(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LDFLAGS) 103 | 104 | linux : OS := LINUX 105 | linux : TARGET := ej2d 106 | linux : CFLAGS += -I/usr/include $(shell freetype-config --cflags) 107 | linux : LDFLAGS += -lGLEW -lGL -lX11 -lfreetype -lm 108 | linux : SRC += posix/window.c posix/winfw.c posix/winfont.c 109 | 110 | linux : $(SRC) ej2d 111 | 112 | macosx : CC := clang 113 | macosx : OS := MACOSX 114 | macosx : TARGET := ej2d 115 | macosx : CFLAGS += -I/usr/include $(shell freetype-config --cflags) -D __MACOSX 116 | macosx : LDFLAGS += -lglfw3 -framework OpenGL -lfreetype -lm -ldl 117 | macosx : SRC += mac/example/example/window.c posix/winfw.c mac/example/example/winfont.c 118 | 119 | macosx : $(SRC) ej2d 120 | 121 | ej2d : 122 | $(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LUASRC) $(LDFLAGS) 123 | 124 | clean : 125 | -rm -f ej2d.exe 126 | -rm -f ej2d 127 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | EJOY 2D 2 | ======= 3 | 4 | Make 5 | ==== 6 | 7 | For Windows and msvc 8 | 9 | * msvc\make.bat 10 | * ej2d examples/ex01.lua to test 11 | 12 | For Windows and mingw32 13 | 14 | * Install glew 1.9 15 | * make or make mingw 16 | * ej2d examples/ex01.lua to test 17 | 18 | For Linux , 19 | 20 | * Install glew 1.9 21 | * Install freetype 2 22 | * make or make linux 23 | * ./ej2d examples/ex01.lua to test 24 | 25 | For Mac OS , 26 | 27 | * Install glfw3 28 | * Install freetype 2 29 | * make or make macosx 30 | * ./ej2d examples/ex01.lua to test 31 | 32 | API 33 | ==== 34 | 35 | https://github.com/cloudwu/ejoy2d/blob/master/doc/apicn.md (work in process , in Chinese) 36 | 37 | Question? 38 | ======= 39 | 40 | Please read http://blog.codingnow.com/2013/12/ejoy2d.html first (In Chinese) 41 | 42 | Chinese API document 43 | 44 | Put your questions in [Issues](https://github.com/cloudwu/ejoy2d/issues) . 45 | -------------------------------------------------------------------------------- /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 | uniform mat4 inv_pmv; 14 | 15 | void main() { 16 | gl_Position = inv_pmv * position + vec4(-1.0,1.0,0,0); 17 | v_color = color; 18 | } 19 | ]], 20 | fs = [[ 21 | varying vec4 v_color; 22 | 23 | void main() { 24 | gl_FragColor = v_color; 25 | } 26 | ]], 27 | uniform={ 28 | { 29 | name="inv_pmv", 30 | type="matrix44" 31 | }, 32 | }, 33 | } 34 | 35 | local id = shader.id("GEOMETRY") 36 | core:setprogram(id) 37 | if core.__obj then 38 | core.material = debug.setmetatable(core, shader.material_meta(id)) 39 | core.material:inv_pmv(1.0,0,0,0, 0,1.0,0,0, 0,0,1.0,0, 0,0,0,1.0) 40 | else 41 | core.material = nil 42 | end 43 | 44 | local function set_mat(...) 45 | if core.material then 46 | core.material:inv_pmv(...) 47 | shader.gui_text_material:inv_pmv(...) 48 | shader.gui_edge_material:inv_pmv(...) 49 | end 50 | end 51 | 52 | local geo = {} 53 | 54 | geo.line = assert(core.line) 55 | geo.box = assert(core.box) 56 | geo.polygon = assert(core.polygon) 57 | geo.frame = assert(core.frame) 58 | geo.scissor = assert(core.scissor) 59 | geo.matrix = set_mat 60 | 61 | return geo 62 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ejoy2d/sprite.lua: -------------------------------------------------------------------------------- 1 | local debug = debug 2 | local c = require "ejoy2d.sprite.c" 3 | local pack = require "ejoy2d.spritepack" 4 | local shader = require "ejoy2d.shader" 5 | local richtext = require "ejoy2d.richtext" 6 | 7 | local setmetatable = setmetatable 8 | local method = c.method 9 | local method_fetch = method.fetch 10 | local method_test = method.test 11 | local method_fetch_by_index = method.fetch_by_index 12 | local dfont_method = c.dfont_method 13 | local drawtext = c.drawtext 14 | local fetch 15 | local test 16 | 17 | local get = c.get 18 | local set = c.set 19 | 20 | local get_material = get.material 21 | function get:material() 22 | local m = get_material(self) 23 | if m == nil then 24 | local prog 25 | m, prog = c.new_material(self) 26 | if m == nil then 27 | return 28 | end 29 | local meta = shader.material_meta(prog) 30 | setmetatable(m, meta) 31 | end 32 | 33 | return m 34 | end 35 | 36 | local set_program = set.program 37 | function set:program(prog) 38 | if prog == nil then 39 | set_program(self) 40 | else 41 | set_program(self, shader.id(prog)) 42 | end 43 | end 44 | 45 | local set_text = set.text 46 | function set:text(txt) 47 | if not txt or txt == "" then 48 | set_text(self, nil) 49 | else 50 | local t = type(txt) 51 | assert(t=="string" or t=="number") 52 | set_text(self, richtext.format(self, tostring(txt))) 53 | end 54 | end 55 | 56 | local sprite_meta = {} 57 | 58 | function sprite_meta.__index(spr, key) 59 | if method[key] then 60 | return method[key] 61 | end 62 | local getter = get[key] 63 | if getter then 64 | return getter(spr) 65 | end 66 | local child = fetch(spr, key) 67 | 68 | if child then 69 | return child 70 | else 71 | print("Unsupport get " .. key) 72 | return nil 73 | end 74 | end 75 | 76 | function sprite_meta.__newindex(spr, key, v) 77 | local setter = set[key] 78 | if setter then 79 | setter(spr, v) 80 | return 81 | end 82 | assert(debug.getmetatable(v) == sprite_meta, "Need a sprite") 83 | method.mount(spr, key, v) 84 | end 85 | 86 | local get_parent = get.parent 87 | function get:parent() 88 | local p = get_parent(self) 89 | if p and not getmetatable(p) then 90 | return debug.setmetatable(p, sprite_meta) 91 | end 92 | return p 93 | end 94 | 95 | -- local function 96 | function fetch(spr, child) 97 | local cobj = method_fetch(spr, child) 98 | if cobj then 99 | return debug.setmetatable(cobj, sprite_meta) 100 | end 101 | end 102 | 103 | -- local function 104 | function test(...) 105 | local cobj = method_test(...) 106 | if cobj then 107 | return debug.setmetatable(cobj, sprite_meta) 108 | end 109 | end 110 | 111 | local function fetch_by_index(spr, index) 112 | local cobj = method_fetch_by_index(spr, index) 113 | if cobj then 114 | return debug.setmetatable(cobj, sprite_meta) 115 | end 116 | end 117 | 118 | method.fetch = fetch 119 | method.fetch_by_index = fetch_by_index 120 | method.test = test 121 | 122 | local sprite = {} 123 | 124 | function sprite.new(packname, name) 125 | local pack, id = pack.query(packname, name) 126 | local cobj = c.new(pack,id) 127 | if cobj then 128 | return debug.setmetatable(cobj, sprite_meta) 129 | end 130 | end 131 | 132 | function sprite.label(tbl) 133 | local size = tbl.size or tbl.height - 2 134 | local l = (c.label(tbl.width, tbl.height, size, tbl.color, tbl.align)) 135 | if l then 136 | l = debug.setmetatable(l, sprite_meta) 137 | if tbl.text then 138 | l.text = tbl.text 139 | end 140 | return l 141 | end 142 | end 143 | 144 | function sprite.proxy() 145 | local cobj = c.proxy() 146 | return debug.setmetatable(cobj, sprite_meta) 147 | end 148 | 149 | local dfont_meta = {} 150 | 151 | function dfont_meta.__index(spr, key) 152 | if dfont_method[key] then 153 | return dfont_method[key] 154 | else 155 | error("Unsupport dfont get " .. key) 156 | end 157 | end 158 | 159 | function sprite.dfont(width, height, fmt, tid) 160 | local cobj = c.dfont(width, height, fmt, tid) 161 | return debug.setmetatable(cobj, dfont_meta) 162 | end 163 | 164 | function sprite.drawtext(txt,x,y,w,size,color,edge,align) 165 | local mat = edge and shader.gui_edge_material or shader.gui_text_material 166 | mat=mat.__obj 167 | drawtext(mat, txt, x, y, w, size, color, edge, align) 168 | end 169 | 170 | return sprite 171 | -------------------------------------------------------------------------------- /examples/asset/bird_config.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | M.bird_scale = 2.5 4 | 5 | M.pipe_scale = 2.5 6 | M.blank_height = 80 7 | M.header_height = 20 8 | M.tail_height = 100 9 | return M 10 | 11 | -------------------------------------------------------------------------------- /examples/asset/birds.1.pgm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/examples/asset/birds.1.pgm -------------------------------------------------------------------------------- /examples/asset/birds.1.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/examples/asset/birds.1.ppm -------------------------------------------------------------------------------- /examples/asset/particle.1.pgm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/examples/asset/particle.1.pgm -------------------------------------------------------------------------------- /examples/asset/particle.1.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/examples/asset/particle.1.ppm -------------------------------------------------------------------------------- /examples/asset/particle_particle_config.lua: -------------------------------------------------------------------------------- 1 | return { 2 | fire ={ 3 | texId= 4, 4 | positionType= 2, 5 | angle= 90.0, 6 | angleVariance= 10.0, 7 | blendFuncDestination= 771.0, 8 | blendFuncSource= 1.0, 9 | configName= fire, 10 | duration= -1.0, 11 | emitterType= 0.0, 12 | finishColorAlpha= 1.0, 13 | finishColorBlue= 0.0, 14 | finishColorGreen= 0.03045729175209999, 15 | finishColorRed= 0.38788047432899475, 16 | finishColorVarianceAlpha= 0.0, 17 | finishColorVarianceBlue= 0.0, 18 | finishColorVarianceGreen= 0.0, 19 | finishColorVarianceRed= 0.0, 20 | finishParticleSize= 0.0, 21 | finishParticleSizeVariance= 0.0, 22 | gravityx= 0.0, 23 | gravityy= 0.0, 24 | maxParticles= 250.0, 25 | maxRadius= 0, 26 | maxRadiusVariance= 0.0, 27 | minRadius= 300, 28 | minRadiusVariance= 0.0, 29 | particleLifespan= 2.0, 30 | particleLifespanVariance= 0.0, 31 | radialAccelVariance= 0.0, 32 | radialAcceleration= 0.0, 33 | rotatePerSecond= 360, 34 | rotatePerSecondVariance= 0.0, 35 | rotationEnd= 0.0, 36 | rotationEndVariance= 0.0, 37 | rotationStart= 0.0, 38 | rotationStartVariance= 0.0, 39 | sourcePositionVariancex= 7.0, 40 | sourcePositionVariancey= 7.0, 41 | sourcePositionx= 0, 42 | sourcePositiony= 0, 43 | speed= 72.37, 44 | speedVariance= 30.0, 45 | startColorAlpha= 1.0, 46 | startColorBlue= 0.0, 47 | startColorGreen= 0.07000067085027695, 48 | startColorRed= 0.5977813601493835, 49 | startColorVarianceAlpha= 0.0, 50 | startColorVarianceBlue= 0.0, 51 | startColorVarianceGreen= 0.0, 52 | startColorVarianceRed= 0.0, 53 | startParticleSize= 64.0, 54 | startParticleSizeVariance= 5.0, 55 | tangentialAccelVariance= 0.0, 56 | tangentialAcceleration= 0.0, 57 | yCoordFlipped= 1 58 | }, 59 | ice ={ 60 | texId= 5, 61 | angle= 90.0, 62 | angleVariance= 221.366455078125, 63 | blendFuncDestination= 771, 64 | blendFuncSource= 1, 65 | configName= ice, 66 | duration= -1.0, 67 | emitterType= 0, 68 | finishColorAlpha= 0.0, 69 | finishColorBlue= 0.0, 70 | finishColorGreen= 0.0, 71 | finishColorRed= 0.0, 72 | finishColorVarianceAlpha= 0.0, 73 | finishColorVarianceBlue= 0.0, 74 | finishColorVarianceGreen= 0.0, 75 | finishColorVarianceRed= 0.0, 76 | finishParticleSize= 0.0, 77 | finishParticleSizeVariance= 0.0, 78 | gravityx= 37.26708984375, 79 | gravityy= -1254.658447265625, 80 | maxParticles= 77, 81 | maxRadius= 0, 82 | maxRadiusVariance= 0.0, 83 | minRadius= 300, 84 | minRadiusVariance= 0.0, 85 | particleLifespan= 1, 86 | particleLifespanVariance= 0.0, 87 | radialAccelVariance= 0.0, 88 | radialAcceleration= 0.0, 89 | rotatePerSecond= 360, 90 | rotatePerSecondVariance= 0.0, 91 | rotationEnd= 0.0, 92 | rotationEndVariance= 0.0, 93 | rotationStart= 0.0, 94 | rotationStartVariance= 0.0, 95 | sourcePositionVariancex= 7.0, 96 | sourcePositionVariancey= 7.0, 97 | sourcePositionx= 0, 98 | sourcePositiony= 0, 99 | speed= 225, 100 | speedVariance= 30.0, 101 | startColorAlpha= 0.5, 102 | startColorBlue= 0.8, 103 | startColorGreen= 0.498, 104 | startColorRed= 0.372, 105 | startColorVarianceAlpha= 0.0, 106 | startColorVarianceBlue= 0.0, 107 | startColorVarianceGreen= 0.0, 108 | startColorVarianceRed= 0.0, 109 | startParticleSize= 64.0, 110 | startParticleSizeVariance= 5.0, 111 | tangentialAccelVariance= 0.0, 112 | tangentialAcceleration= 0.0, 113 | yCoordFlipped= 1 114 | }, 115 | } -------------------------------------------------------------------------------- /examples/asset/sample.1.pgm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/examples/asset/sample.1.pgm -------------------------------------------------------------------------------- /examples/asset/sample.1.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/examples/asset/sample.1.ppm -------------------------------------------------------------------------------- /examples/ex01.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | 5 | pack.load { 6 | pattern = fw.WorkDir..[[examples/asset/?]], 7 | "sample", 8 | } 9 | 10 | local obj = ej.sprite("sample","cannon") 11 | local turret = obj.turret 12 | 13 | -- set position (-100,0) scale (0.5) 14 | obj:ps(-100,0,0.5) 15 | 16 | local obj2 = ej.sprite("sample","mine") 17 | obj2.resource.frame = 70 18 | -- set position(100,0) scale(1.2) separately 19 | obj2:ps(100,0) 20 | obj2:ps(1.2) 21 | 22 | local game = {} 23 | local screencoord = { x = 512, y = 384, scale = 1.2 } 24 | local x1,y1,x2,y2 = obj2:aabb(screencoord) 25 | obj2.label.text = string.format("AABB\n%d x %d", x2-x1, y2-y1) 26 | 27 | function game.update() 28 | turret.frame = turret.frame + 3 29 | obj2.frame = obj2.frame + 1 30 | end 31 | 32 | function game.drawframe() 33 | ej.clear(0xff808080) -- clear (0.5,0.5,0.5,1) gray 34 | obj:draw(screencoord) 35 | obj2:draw(screencoord) 36 | end 37 | 38 | function game.touch(what, x, y) 39 | end 40 | 41 | function game.message(...) 42 | end 43 | 44 | function game.handle_error(...) 45 | end 46 | 47 | function game.on_resume() 48 | end 49 | 50 | function game.on_pause() 51 | end 52 | 53 | ej.start(game) 54 | 55 | 56 | -------------------------------------------------------------------------------- /examples/ex02.lua: -------------------------------------------------------------------------------- 1 | -- This example show how low level api works 2 | 3 | local ej = require "ejoy2d" 4 | local fw = require "ejoy2d.framework" 5 | local shader = require "ejoy2d.shader" 6 | local ppm = require "ejoy2d.ppm" 7 | 8 | local TEXID = 0 9 | 10 | -- load ppm/pgm file into texture slot TEXID 11 | ppm.texture(TEXID,fw.WorkDir.."examples/asset/sample.1") 12 | 13 | local game = {} 14 | 15 | function game.update() 16 | end 17 | 18 | function game.drawframe() 19 | ej.clear(0xff808080) -- clear (0.5,0.5,0.5,1) gray 20 | 21 | -- use shader.draw to draw a polygon to screen (for debug use) 22 | shader.draw(TEXID, { 23 | 88, 0, 88, 45, 147, 45, 147, 0, -- texture coord 24 | -958, -580, -958, 860, 918, 860, 918, -580, -- screen coord, 16x pixel, (0,0) is the center of screen 25 | }) 26 | end 27 | 28 | function game.touch(what, x, y) 29 | end 30 | 31 | function game.message(...) 32 | end 33 | 34 | function game.handle_error(...) 35 | end 36 | 37 | function game.on_resume() 38 | end 39 | 40 | function game.on_pause() 41 | end 42 | 43 | ej.start(game) 44 | 45 | 46 | -------------------------------------------------------------------------------- /examples/ex03.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | local particle = require "ejoy2d.particle" 5 | 6 | fw.AnimationFramePerFrame = 1 7 | 8 | pack.load { pattern = fw.WorkDir..[[examples/asset/?]], "sample", } 9 | particle.preload(fw.WorkDir.."examples/asset/particle") 10 | 11 | local ps = particle.new("fire", function() 12 | print("particle has gone") 13 | end) 14 | ps.group:ps(160, 240) 15 | 16 | local obj = ej.sprite("sample", "mine") 17 | obj.label.text = "Hello World" 18 | 19 | local game = {} 20 | 21 | function game.update() 22 | ps:update(0.0333) 23 | ps.group.frame = ps.group.frame + 1 24 | end 25 | 26 | local pos = {x=160, y= 300} 27 | local pos2 = {x=160, y = 240} 28 | 29 | function game.drawframe() 30 | ej.clear() -- default clear color is black (0,0,0,1) 31 | ps.group:draw() 32 | obj:draw(pos) 33 | end 34 | 35 | function game.touch(what, x, y) 36 | end 37 | 38 | function game.message(...) 39 | end 40 | 41 | function game.handle_error(...) 42 | end 43 | 44 | function game.on_resume() 45 | end 46 | 47 | function game.on_pause() 48 | end 49 | 50 | ej.start(game) 51 | 52 | 53 | -------------------------------------------------------------------------------- /examples/ex04.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | local sprite = require "ejoy2d.sprite" 5 | 6 | pack.load { 7 | pattern = fw.WorkDir..[[examples/asset/?]], 8 | "sample", 9 | } 10 | 11 | local scissor = false 12 | local obj = ej.sprite("sample","mine") 13 | obj.resource.frame = 70 14 | obj.label.text = "The #[red]quick#[green] brown #[blue]fox jumps#[stop] over the lazy dog" 15 | obj:ps(400,300) 16 | local screencoord = { scale = 1.2 } 17 | 18 | local game = {} 19 | 20 | function game.update() 21 | obj.frame = obj.frame + 1 22 | end 23 | 24 | function game.drawframe() 25 | ej.clear() 26 | obj:draw(screencoord) 27 | end 28 | 29 | function game.touch(what, x, y) 30 | if what == "END" then 31 | local touched = obj:test(x,y,screencoord) 32 | if touched then 33 | if touched.name == "label" then 34 | touched.text = "label touched" 35 | end 36 | if touched.name == "panel" then 37 | scissor = not scissor 38 | touched.scissor = scissor 39 | obj.label.text = scissor and "Set scissor" or "Clear scissor" 40 | end 41 | else 42 | obj.label.text = "Not Hit" 43 | end 44 | end 45 | end 46 | 47 | function game.message(...) 48 | end 49 | 50 | function game.handle_error(...) 51 | end 52 | 53 | function game.on_resume() 54 | end 55 | 56 | function game.on_pause() 57 | end 58 | 59 | ej.start(game) 60 | 61 | 62 | -------------------------------------------------------------------------------- /examples/ex05.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | local sprite = require "ejoy2d.sprite" 5 | 6 | pack.load { 7 | pattern = fw.WorkDir..[[examples/asset/?]], 8 | "sample", 9 | } 10 | 11 | local obj = ej.sprite("sample","cannon") 12 | local turret = obj.turret 13 | 14 | local game = {} 15 | local screencoord = { x = 512, y = 384, scale = 1.2 } 16 | 17 | local label = sprite.label { width = 100, height = 100, size = 30, text = "Hello" , color = 0xffffffff } 18 | 19 | -- you can also mount the label to turret.anchor 20 | -- turret.anchor = label 21 | local anchor = turret.anchor 22 | anchor.visible = true 23 | 24 | function game.update() 25 | turret.frame = turret.frame + 1 26 | end 27 | 28 | function game.drawframe() 29 | ej.clear() 30 | obj:draw(screencoord) 31 | -- If anchor is visible, obj:draw will update anchor's world_matrix 32 | label.matrix = anchor.world_matrix 33 | label:draw() 34 | end 35 | 36 | function game.touch(what, x, y) 37 | end 38 | 39 | function game.message(...) 40 | end 41 | 42 | function game.handle_error(...) 43 | end 44 | 45 | function game.on_resume() 46 | end 47 | 48 | function game.on_pause() 49 | end 50 | 51 | ej.start(game) 52 | 53 | 54 | -------------------------------------------------------------------------------- /examples/ex06.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | local sprite = require "ejoy2d.sprite" 5 | 6 | pack.load { 7 | pattern = fw.WorkDir..[[examples/asset/?]], 8 | "sample", 9 | } 10 | 11 | local obj = ej.sprite("sample","cannon") 12 | local obj2 = ej.sprite("sample","cannon") 13 | obj2:ps(100,0) 14 | 15 | local proxy = sprite.proxy() 16 | local turret = obj.turret 17 | proxy.proxy = turret -- proxy:mount("proxy", turret) 18 | 19 | -- multi mount proxy 20 | obj.turret = proxy 21 | obj2.turret = proxy 22 | 23 | assert(proxy.parent == nil) 24 | assert(proxy.name == nil) 25 | 26 | local game = {} 27 | local screencoord = { x = 512, y = 384, scale = 1.2 } 28 | 29 | function game.update() 30 | turret.frame = turret.frame + 1 31 | end 32 | 33 | function game.drawframe() 34 | ej.clear() 35 | obj:draw(screencoord) 36 | obj2:draw(screencoord) 37 | end 38 | 39 | function game.touch(what, x, y) 40 | end 41 | 42 | function game.message(...) 43 | end 44 | 45 | function game.handle_error(...) 46 | end 47 | 48 | function game.on_resume() 49 | end 50 | 51 | function game.on_pause() 52 | end 53 | 54 | ej.start(game) 55 | 56 | 57 | -------------------------------------------------------------------------------- /examples/ex07.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local fw = require "ejoy2d.framework" 3 | local pack = require "ejoy2d.simplepackage" 4 | local sprite = require "ejoy2d.sprite" 5 | local renderbuffer = require "ejoy2d.renderbuffer" 6 | 7 | pack.load { 8 | pattern = fw.WorkDir..[[examples/asset/?]], 9 | "birds", 10 | } 11 | 12 | local bird = ej.sprite("birds","bird") 13 | local rb = renderbuffer.new() 14 | 15 | for i=1, 100 do 16 | rb:add(bird) 17 | bird:ps(5 *i, 5 *i) 18 | end 19 | rb:upload() 20 | 21 | 22 | local game = {} 23 | local screencoord = { x = 512, y = 384, scale = 1.2 } 24 | 25 | local p = 0 26 | 27 | function game.update() 28 | p=p+1 29 | end 30 | 31 | bird:ps(200,0) 32 | 33 | function game.drawframe() 34 | ej.clear() 35 | rb:draw(p,p) 36 | bird:draw(screencoord) 37 | end 38 | 39 | function game.touch(what, x, y) 40 | end 41 | 42 | function game.message(...) 43 | end 44 | 45 | function game.handle_error(...) 46 | end 47 | 48 | function game.on_resume() 49 | end 50 | 51 | function game.on_pause() 52 | end 53 | 54 | ej.start(game) 55 | 56 | 57 | -------------------------------------------------------------------------------- /examples/ex08.lua: -------------------------------------------------------------------------------- 1 | -- This example show how user defined shader works 2 | 3 | local ej = require "ejoy2d" 4 | local fw = require "ejoy2d.framework" 5 | local pack = require "ejoy2d.simplepackage" 6 | 7 | pack.load { 8 | pattern = fw.WorkDir..[[examples/asset/?]], 9 | "sample", 10 | } 11 | 12 | 13 | -- define a shader 14 | local s = ej.define_shader { 15 | name = "EXAMPLE", 16 | fs = [[ 17 | varying vec2 v_texcoord; 18 | uniform vec4 color; 19 | uniform sampler2D texture0; 20 | 21 | void main() { 22 | vec4 tmp = texture2D(texture0, v_texcoord); 23 | gl_FragColor = color + tmp; 24 | } 25 | ]], 26 | uniform = { 27 | { 28 | name = "color", 29 | type = "float4", 30 | } 31 | }, 32 | texture = { 33 | "texture0", 34 | } 35 | } 36 | 37 | s.color(1,0,0,1) -- set shader color 38 | 39 | 40 | local obj = ej.sprite("sample","cannon") 41 | obj:ps(100,100) 42 | obj.program = "EXAMPLE" 43 | obj.turret.program = "EXAMPLE" 44 | obj.turret.material:color(0,0,1,1) 45 | 46 | 47 | local obj2 = ej.sprite("sample","cannon") 48 | obj2:ps(200,100) 49 | 50 | obj2.turret.program = "EXAMPLE" 51 | obj2.turret.material:color(0,1,0,1) -- uniform can be set in material 52 | 53 | 54 | local obj3 = ej.sprite("sample","cannon") 55 | obj3:ps(300,100) 56 | obj3.program = "EXAMPLE" 57 | 58 | local game = {} 59 | 60 | function game.update() 61 | end 62 | 63 | function game.drawframe() 64 | ej.clear(0xff808080) -- clear (0.5,0.5,0.5,1) gray 65 | obj:draw() 66 | obj2:draw() 67 | obj3:draw() 68 | end 69 | 70 | function game.touch(what, x, y) 71 | end 72 | 73 | function game.message(...) 74 | end 75 | 76 | function game.handle_error(...) 77 | end 78 | 79 | function game.on_resume() 80 | end 81 | 82 | function game.on_pause() 83 | end 84 | 85 | ej.start(game) 86 | 87 | -------------------------------------------------------------------------------- /examples/ex09.lua: -------------------------------------------------------------------------------- 1 | local ej = require "ejoy2d" 2 | local geo = require "ejoy2d.geometry" 3 | local spr = require "ejoy2d.sprite" 4 | 5 | local game = {} 6 | 7 | local x = 0 8 | local y = 768 9 | 10 | function game.update() 11 | x = x + 1 12 | y = y - 1 13 | end 14 | 15 | local hexagon = {} 16 | 17 | for i = 0, 5 do 18 | local r = math.pi * 2 * i / 6 19 | table.insert(hexagon, math.sin(r) * 100 + 300) 20 | table.insert(hexagon, math.cos(r) * 100 + 300) 21 | end 22 | 23 | 24 | local style_button = { 25 | frame = 0xffffffff, 26 | bgcolor = 0x80404040, 27 | margin = 6, 28 | border = 1, 29 | } 30 | 31 | local function button(x,y,w,h,text,s) 32 | local default = style_button 33 | s = s or default 34 | local margin = s.margin or default.margin 35 | local color = s.frame or default.frame 36 | local bgcolor = s.bgcolor or default.bgcolor 37 | local charsize = h - margin * 2 38 | local border = s.border or default.border 39 | geo.frame(x,y,w,h,color, border) 40 | geo.box(x+border,y+border,w-border*2, h-border*2, bgcolor) 41 | geo.scissor(x+border,y+border,w-border*2, h-border*2) 42 | spr.drawtext(text,x,y+margin,w,charsize,color) 43 | geo.scissor() 44 | end 45 | 46 | local function button_hover(x,y,w,h,text,s) 47 | local default = style_button 48 | s = s or default 49 | local margin = s.margin or default.margin 50 | local color = s.frame or default.frame 51 | local bgcolor = s.bgcolor or default.bgcolor 52 | local charsize = h - margin * 2 53 | local border = s.border or default.border 54 | geo.box(x,y,w, h, color) 55 | geo.frame(x+border,y+border,w-border*2,h-border*2,bgcolor, border) 56 | spr.drawtext(text,x,y+margin,w,charsize,bgcolor) 57 | end 58 | 59 | function game.drawframe() 60 | ej.clear(0xff808080) -- clear (0.5,0.5,0.5,1) gray 61 | geo.matrix(1.3,0,0,0, 0,1.0,0,0, 0,0,1.0,0, 0.1,0,0,1.0) 62 | geo.line(0,0,x,y,0xffffffff) 63 | geo.box(100,100,200,300, 0x80ff0000) 64 | geo.polygon(hexagon, 0x40ffff00) 65 | button(400,400,80, 32, "我是一个按钮") 66 | button_hover(400,440,80, 32, "按钮") 67 | end 68 | 69 | function game.touch(what, x, y) 70 | end 71 | 72 | function game.message(...) 73 | end 74 | 75 | function game.handle_error(...) 76 | end 77 | 78 | function game.on_resume() 79 | end 80 | 81 | function game.on_pause() 82 | end 83 | 84 | ej.start(game) 85 | 86 | -------------------------------------------------------------------------------- /ios/example/example/EJAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // EJAppDelegate.h 3 | // sample 4 | // 5 | // Created by Lei Yu on 13-12-31. 6 | // Copyright (c) 2013年 ejoy2d. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "EJViewController.h" 11 | 12 | @interface EJAppDelegate : UIResponder 13 | 14 | @property (strong, nonatomic) UIWindow *window; 15 | 16 | @property (strong, nonatomic) EJViewController *viewController; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /ios/example/example/EJAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // EJAppDelegate.m 3 | // sample 4 | // 5 | // Created by Lei Yu on 13-12-31. 6 | // Copyright (c) 2013年 ejoy2d. All rights reserved. 7 | // 8 | 9 | #import "EJAppDelegate.h" 10 | #import "EJViewController.h" 11 | #import "winfw.h" 12 | 13 | @implementation EJAppDelegate 14 | 15 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 16 | { 17 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 18 | self.viewController = [[EJViewController alloc] init]; 19 | self.window.rootViewController = self.viewController; 20 | [self.window makeKeyAndVisible]; 21 | return YES; 22 | } 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application 25 | { 26 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 27 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 28 | } 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application 31 | { 32 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 33 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 34 | } 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application 37 | { 38 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 39 | } 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application 42 | { 43 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 44 | ejoy2d_win_resume(); 45 | } 46 | 47 | - (void)applicationWillTerminate:(UIApplication *)application 48 | { 49 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /ios/example/example/EJViewController.h: -------------------------------------------------------------------------------- 1 | #import 2 | #import 3 | 4 | @class EJViewController; 5 | 6 | @interface EJViewController : GLKViewController 7 | 8 | +(EJViewController*)getLastInstance; 9 | 10 | @end 11 | -------------------------------------------------------------------------------- /ios/example/example/EJViewController.m: -------------------------------------------------------------------------------- 1 | #import "EJViewController.h" 2 | //#import "lejoy2d.h" 3 | //#import "ejoy2d.h" 4 | #import "winfw.h" 5 | 6 | static EJViewController* _controller = nil; 7 | 8 | @interface EJViewController () { 9 | } 10 | @property (strong, nonatomic) EAGLContext *context; 11 | 12 | @end 13 | 14 | @implementation EJViewController 15 | - (id)init { 16 | _controller = [super init]; 17 | return _controller; 18 | } 19 | 20 | -(void) loadView { 21 | CGRect bounds = [UIScreen mainScreen].bounds; 22 | self.view = [[GLKView alloc] initWithFrame:bounds]; 23 | } 24 | 25 | +(EJViewController*)getLastInstance{ 26 | return _controller; 27 | } 28 | 29 | - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 30 | { 31 | return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight; 32 | } 33 | 34 | - (void)dealloc 35 | { 36 | //lejoy_exit(); 37 | _controller = nil; 38 | if ([EAGLContext currentContext] == self.context) { 39 | [EAGLContext setCurrentContext:nil]; 40 | } 41 | } 42 | 43 | - (void)viewDidLoad 44 | { 45 | [super viewDidLoad]; 46 | 47 | NSLog(@"viewDidLoad"); 48 | 49 | self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 50 | 51 | if (!self.context) { 52 | NSLog(@"Failed to create ES context"); 53 | } 54 | 55 | GLKView *view = (GLKView *)self.view; 56 | view.context = self.context; 57 | 58 | [EAGLContext setCurrentContext:self.context]; 59 | 60 | CGFloat screenScale = [[UIScreen mainScreen] scale]; 61 | CGRect bounds = [[UIScreen mainScreen] bounds]; 62 | 63 | printf("screenScale: %f\n", screenScale); 64 | printf("bounds: x:%f y:%f w:%f h:%f\n", 65 | bounds.origin.x, bounds.origin.y, 66 | bounds.size.width, bounds.size.height); 67 | 68 | NSString *appFolderPath = [[NSBundle mainBundle] resourcePath]; 69 | const char* folder = [appFolderPath UTF8String]; 70 | 71 | ejoy2d_win_init(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height, screenScale, folder); 72 | } 73 | 74 | -(BOOL)prefersStatusBarHidden 75 | { 76 | return YES; 77 | } 78 | 79 | -(void)viewDidUnload 80 | { 81 | [super viewDidUnload]; 82 | 83 | NSLog(@"viewDidUnload"); 84 | 85 | // lejoy_unload(); 86 | 87 | if ([self isViewLoaded] && ([[self view] window] == nil)) { 88 | self.view = nil; 89 | 90 | if ([EAGLContext currentContext] == self.context) { 91 | [EAGLContext setCurrentContext:nil]; 92 | } 93 | self.context = nil; 94 | } 95 | } 96 | 97 | - (void)update 98 | { 99 | ejoy2d_win_update(); 100 | /* lejoy_update(self.timeSinceLastUpdate); 101 | if(lejoy_check_reload()) { 102 | lejoy_reload(); 103 | }*/ 104 | } 105 | 106 | - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect 107 | { 108 | ejoy2d_win_frame(); 109 | // lejoy_drawframe(); 110 | } 111 | 112 | // handle Touches 113 | 114 | - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 115 | // UITouch *touch = [touches anyObject]; 116 | for(UITouch *touch in touches) { 117 | CGPoint p = [touch locationInView:touch.view]; 118 | ejoy2d_win_touch(p.x, p.y, TOUCH_BEGIN); 119 | } 120 | } 121 | 122 | - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 123 | // UITouch *touch = [touches anyObject]; 124 | for(UITouch *touch in touches) { 125 | CGPoint p = [touch locationInView:touch.view]; 126 | ejoy2d_win_touch(p.x, p.y, TOUCH_MOVE); 127 | } 128 | } 129 | 130 | - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 131 | // UITouch *touch = [touches anyObject]; 132 | for(UITouch *touch in touches) { 133 | CGPoint p = [touch locationInView:touch.view]; 134 | ejoy2d_win_touch(p.x, p.y, TOUCH_END); 135 | } 136 | } 137 | /* 138 | - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 139 | // UITouch *touch = [touches anyObject]; 140 | for(UITouch *touch in touches) { 141 | CGPoint p = [touch locationInView:touch.view]; 142 | // lejoy_touch("CANCEL", p.x, p.y); 143 | } 144 | } 145 | */ 146 | @end 147 | 148 | -------------------------------------------------------------------------------- /ios/example/example/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "ipad", 20 | "size" : "29x29", 21 | "scale" : "1x" 22 | }, 23 | { 24 | "idiom" : "ipad", 25 | "size" : "29x29", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "ipad", 30 | "size" : "40x40", 31 | "scale" : "1x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "40x40", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "76x76", 41 | "scale" : "1x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "76x76", 46 | "scale" : "2x" 47 | } 48 | ], 49 | "info" : { 50 | "version" : 1, 51 | "author" : "xcode" 52 | } 53 | } -------------------------------------------------------------------------------- /ios/example/example/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "orientation" : "portrait", 20 | "idiom" : "ipad", 21 | "extent" : "full-screen", 22 | "minimum-system-version" : "7.0", 23 | "scale" : "1x" 24 | }, 25 | { 26 | "orientation" : "landscape", 27 | "idiom" : "ipad", 28 | "extent" : "full-screen", 29 | "minimum-system-version" : "7.0", 30 | "scale" : "1x" 31 | }, 32 | { 33 | "orientation" : "portrait", 34 | "idiom" : "ipad", 35 | "extent" : "full-screen", 36 | "minimum-system-version" : "7.0", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "orientation" : "landscape", 41 | "idiom" : "ipad", 42 | "extent" : "full-screen", 43 | "minimum-system-version" : "7.0", 44 | "scale" : "2x" 45 | } 46 | ], 47 | "info" : { 48 | "version" : 1, 49 | "author" : "xcode" 50 | } 51 | } -------------------------------------------------------------------------------- /ios/example/example/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /ios/example/example/example-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.ejoy.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /ios/example/example/example-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #import 8 | 9 | #ifndef __IPHONE_3_0 10 | #warning "This project uses features only available in iOS SDK 3.0 and later." 11 | #endif 12 | 13 | #ifdef __OBJC__ 14 | #import 15 | #import 16 | #endif 17 | -------------------------------------------------------------------------------- /ios/example/example/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // sample 4 | // 5 | // Created by Lei Yu on 13-12-31. 6 | // Copyright (c) 2013年 ejoy2d. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "EJAppDelegate.h" 12 | 13 | int main(int argc, char * argv[]) 14 | { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([EJAppDelegate class])); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ios/example/example/winfw.c: -------------------------------------------------------------------------------- 1 | 2 | #include "opengl.h" 3 | #include "ejoy2dgame.h" 4 | #include "fault.h" 5 | #include "screen.h" 6 | #include "winfw.h" 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | struct WINDOWGAME { 14 | struct game *game; 15 | int intouch; 16 | }; 17 | 18 | static struct WINDOWGAME *G = NULL; 19 | 20 | static const char * startscript = 21 | "local path, script = ...\n" 22 | "require(\"ejoy2d.framework\").WorkDir = path..'/'\n" 23 | "assert(script, 'I need a script name')\n" 24 | "script = path..[[/]]..script\n" 25 | "package.path = path .. [[/?.lua;]] .. path .. [[/?/init.lua;./?.lua;./?/init.lua]]\n" 26 | "local f = loadfile(script)\n" 27 | "f(script)\n"; 28 | 29 | static struct WINDOWGAME * 30 | create_game() { 31 | struct WINDOWGAME * g = malloc(sizeof(*g)); 32 | g->game = ejoy2d_game(); 33 | g->intouch = 0; 34 | return g; 35 | } 36 | 37 | static int 38 | traceback(lua_State *L) { 39 | const char *msg = lua_tostring(L, 1); 40 | if (msg == NULL) { 41 | if (luaL_callmeta(L, 1, "__tostring") && 42 | lua_type(L, -1) == LUA_TSTRING) 43 | return 1; 44 | else 45 | msg = lua_pushfstring(L, "(error object is a %s value)", 46 | luaL_typename(L, 1)); 47 | } 48 | luaL_traceback(L, L, msg, 1); 49 | return 1; 50 | } 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, "examples/ex09.lua"); 67 | 68 | err = lua_pcall(L, 2, 0, tb); 69 | if (err) { 70 | const char *msg = lua_tostring(L,-1); 71 | fault("%s", msg); 72 | } 73 | 74 | lua_pop(L,1); 75 | 76 | screen_init(width,height,scale); 77 | ejoy2d_game_start(G->game); 78 | } 79 | 80 | void 81 | ejoy2d_win_update() { 82 | ejoy2d_game_update(G->game, 0.01f); 83 | } 84 | 85 | void 86 | ejoy2d_win_frame() { 87 | ejoy2d_game_drawframe(G->game); 88 | } 89 | 90 | void 91 | ejoy2d_win_resume(){ 92 | ejoy2d_game_resume(G->game); 93 | } 94 | 95 | void 96 | ejoy2d_win_touch(int x, int y,int touch) { 97 | switch (touch) { 98 | case TOUCH_BEGIN: 99 | G->intouch = 1; 100 | break; 101 | case TOUCH_END: 102 | G->intouch = 0; 103 | break; 104 | case TOUCH_MOVE: 105 | if (!G->intouch) { 106 | return; 107 | } 108 | break; 109 | } 110 | // windows only support one touch id (0) 111 | int id = 0; 112 | ejoy2d_game_touch(G->game, id, x,y,touch); 113 | } 114 | 115 | -------------------------------------------------------------------------------- /ios/example/example/winfw.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_windows_fw_h 2 | #define ejoy2d_windows_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 | void ejoy2d_win_init(int orix, int oriy, int width, int height, float scale, const char* folder); 12 | void ejoy2d_win_frame(); 13 | void ejoy2d_win_update(); 14 | void ejoy2d_win_touch(int x, int y,int touch); 15 | void ejoy2d_win_resume(); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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_rawdraw(const char * str, float x, float y, struct pack_label * l); 41 | int label_rawline(const char * str, struct pack_label *l); 42 | void label_draw(const struct rich_text *rich, struct pack_label * l, struct srt *srt, const struct sprite_trans *arg); 43 | void label_size(const char * str, struct pack_label * l, int* width, int* height); 44 | int label_char_size(struct pack_label* l, const char* chr, int* width, int* height, int* unicode); 45 | uint32_t label_get_color(struct pack_label * l, const struct sprite_trans *arg); 46 | 47 | struct font_context { 48 | int w; 49 | int h; 50 | int ascent; 51 | void * font; 52 | void * dc; 53 | // int edge; 54 | }; 55 | 56 | void font_size(const char *str, int unicode, struct font_context * ctx); 57 | void font_glyph(const char * str, int unicode, void * buffer, struct font_context * ctx); 58 | void font_create(int font_size, struct font_context *ctx); 59 | void font_release(struct font_context *ctx); 60 | 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /lib/lejoy2dcore.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include "opengl.h" 6 | #include "screen.h" 7 | #include "shader.h" 8 | #include "label.h" 9 | #include "ejoy2dgame.h" 10 | 11 | static int 12 | lviewport(lua_State *L) { 13 | int w = luaL_checkinteger(L,1); 14 | int h = luaL_checkinteger(L,2); 15 | glViewport(0, 0, w,h); 16 | screen_init(w,h,1.0f); 17 | return 0; 18 | } 19 | 20 | static int 21 | lbeginframe(lua_State *L) { 22 | reset_drawcall_count(); 23 | return 0; 24 | } 25 | 26 | static int 27 | lendframe(lua_State *L) { 28 | shader_flush(); 29 | label_flush(); 30 | return 0; 31 | } 32 | 33 | int 34 | luaopen_ejoy2d_core(lua_State *L) { 35 | if ( glewInit() != GLEW_OK ) { 36 | return luaL_error(L, "init glew failed"); 37 | } 38 | ejoy2d_init(L); 39 | luaL_Reg l[] = { 40 | { "viewport", lviewport }, 41 | { "beginframe", lbeginframe }, 42 | { "endframe", lendframe }, 43 | { NULL, NULL }, 44 | }; 45 | luaL_newlib(L,l); 46 | 47 | return 1; 48 | } 49 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef EJOY_2D_MATRIX_H 2 | #define EJOY_2D_MATRIX_H 3 | 4 | #define EJMAT_R_FACTOR (4096.0) 5 | 6 | struct matrix { 7 | /* The matrix format is : 8 | * 9 | * | m[0] m[1] 0 | 10 | * | m[2] m[3] 0 | 11 | * | m[4] m[5] 1 | 12 | * 13 | * The format of the coordinates of a point is: 14 | * 15 | * | x y 1 | 16 | * 17 | * So, if you want to transform a point p with a matrix m, do: 18 | * 19 | * p * m 20 | * 21 | */ 22 | int m[6]; 23 | }; 24 | 25 | static inline void 26 | matrix_mul(struct matrix *mm, const struct matrix *mm1, const struct matrix *mm2) { 27 | int *m = mm->m; 28 | const int *m1 = mm1->m; 29 | const int *m2 = mm2->m; 30 | m[0] = (m1[0] * m2[0] + m1[1] * m2[2]) /1024; 31 | m[1] = (m1[0] * m2[1] + m1[1] * m2[3]) /1024; 32 | m[2] = (m1[2] * m2[0] + m1[3] * m2[2]) /1024; 33 | m[3] = (m1[2] * m2[1] + m1[3] * m2[3]) /1024; 34 | m[4] = (m1[4] * m2[0] + m1[5] * m2[2]) /1024 + m2[4]; 35 | m[5] = (m1[4] * m2[1] + m1[5] * m2[3]) /1024 + m2[5]; 36 | } 37 | 38 | static inline void 39 | matrix_identity(struct matrix *mm) { 40 | int *mat = mm->m; 41 | mat[0] = 1024; 42 | mat[1] = 0; 43 | mat[2] = 0; 44 | mat[3] = 1024; 45 | mat[4] = 0; 46 | mat[5] = 0; 47 | } 48 | 49 | int matrix_inverse(const struct matrix *mm, struct matrix *mo); 50 | 51 | struct srt { 52 | int offx; 53 | int offy; 54 | int scalex; 55 | int scaley; 56 | int rot; 57 | }; 58 | 59 | void matrix_srt(struct matrix *mm, const struct srt *srt); 60 | void matrix_rot(struct matrix *m, int rot); 61 | void matrix_scale(struct matrix *m, int sx, int sy); 62 | void matrix_sr(struct matrix *mat, int sx, int sy, int d); 63 | void matrix_rs(struct matrix *mat, int sx, int sy, int d); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /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(__MACOSX) 11 | #define OPENGLES 3 12 | #include 13 | #include 14 | 15 | #elif defined(linux) || defined(__linux) || defined(__linux__) 16 | 17 | #define OPENGLES 2 18 | #include 19 | #include 20 | 21 | #else 22 | #define OPENGLES 0 23 | #include 24 | 25 | #endif 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | static 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | #define PROGRAM_GUI_TEXT 4 16 | #define PROGRAM_GUI_EDGE 5 17 | 18 | struct material; 19 | 20 | void shader_init(); 21 | void shader_load(int prog, const char *fs, const char *vs, int texture, const char ** texture_uniform_name); 22 | void shader_unload(); 23 | void shader_blend(int m1,int m2); 24 | void shader_defaultblend(); 25 | void shader_texture(int id, int channel); 26 | void shader_draw(const struct vertex_pack vb[4],uint32_t color,uint32_t additive); 27 | void shader_drawpolygon(int n, const struct vertex_pack *vb, uint32_t color, uint32_t additive); 28 | void shader_program(int n, struct material *); 29 | void shader_flush(); 30 | void shader_clear(unsigned long argb); 31 | int shader_version(); 32 | void shader_scissortest(int enable); 33 | 34 | int ejoy2d_shader(lua_State *L); 35 | 36 | void shader_drawbuffer(struct render_buffer * rb, float x, float y, float s); 37 | 38 | int shader_adduniform(int prog, const char * name, enum UNIFORM_FORMAT t); 39 | void shader_setuniform(int prog, int index, enum UNIFORM_FORMAT t, float *v); 40 | int shader_uniformsize(enum UNIFORM_FORMAT t); 41 | 42 | // these api may deprecated later 43 | void shader_reset(); 44 | void shader_mask(float x, float y); 45 | void reset_drawcall_count(); 46 | int drawcall_count(); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | struct sprite_pack * pack; 27 | uint16_t type; 28 | uint16_t id; 29 | struct sprite_trans t; 30 | union { 31 | struct pack_animation *ani; 32 | struct pack_picture *pic; 33 | struct pack_polygon_data *poly; 34 | struct pack_label *label; 35 | struct pack_pannel *pannel; 36 | struct matrix *mat; 37 | } s; 38 | struct matrix mat; 39 | int start_frame; 40 | int total_frame; 41 | int frame; 42 | int flags; 43 | const char *name; // name for parent 44 | struct material *material; 45 | union { 46 | struct sprite * children[1]; 47 | struct rich_text * rich_text; 48 | int scissor; 49 | struct anchor_data *anchor; 50 | } data; 51 | }; 52 | 53 | struct sprite_trans * sprite_trans_mul(struct sprite_trans *a, struct sprite_trans *b, struct sprite_trans *t, struct matrix *tmp_matrix); 54 | struct sprite_trans * sprite_trans_mul2(struct sprite_pack *pack, struct sprite_trans_data *a, struct sprite_trans *b, struct sprite_trans *t, struct matrix *tmp_matrix); 55 | void sprite_drawquad(struct pack_picture *picture, const struct srt *srt, const struct sprite_trans *arg); 56 | void sprite_drawpolygon(struct sprite_pack *pack, struct pack_polygon_data *poly, const struct srt *srt, const struct sprite_trans *arg); 57 | 58 | // sprite_size must be call before sprite_init 59 | int sprite_size(struct sprite_pack *pack, int id); 60 | void sprite_init(struct sprite *, struct sprite_pack * pack, int id, int sz); 61 | 62 | // return action frame number, -1 means action is not exist 63 | int sprite_action(struct sprite *, const char * action); 64 | 65 | void sprite_draw(struct sprite *, struct srt *srt); 66 | void sprite_draw_as_child(struct sprite *, struct srt *srt, struct matrix *mat, uint32_t color); 67 | struct sprite * sprite_test(struct sprite *, struct srt *srt, int x, int y); 68 | 69 | // return child index, -1 means not found 70 | int sprite_child(struct sprite *, const char * childname); 71 | int sprite_child_ptr(struct sprite *, struct sprite *child); 72 | // return sprite id in pack, -1 for end 73 | int sprite_component(struct sprite *, int index); 74 | const char * sprite_childname(struct sprite *, int index); 75 | int sprite_setframe(struct sprite *, int frame, bool force_child); 76 | void sprite_mount(struct sprite *, int index, struct sprite *); 77 | 78 | void sprite_aabb(struct sprite *s, struct srt *srt, bool world_aabb, int aabb[4]); 79 | int sprite_pos(struct sprite *s, struct srt *srt, struct matrix *m, int pos[2]); // todo: maybe unused, use sprite_matrix instead 80 | // calc the sprite's world matrix 81 | void sprite_matrix(struct sprite *s, struct matrix *mat); 82 | 83 | bool sprite_child_visible(struct sprite *s, const char * childname); 84 | int material_size(int program); 85 | 86 | int ejoy2d_sprite(lua_State *L); 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /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 | struct matrix; 20 | typedef uint32_t offset_t; 21 | typedef uint16_t uv_t; 22 | 23 | #define SIZEOF_MATRIX (sizeof(struct matrix)) 24 | 25 | struct pack_pannel { 26 | int width; 27 | int height; 28 | int scissor; 29 | }; 30 | 31 | #define SIZEOF_PANNEL (sizeof(struct pack_pannel)) 32 | 33 | struct pack_label { 34 | uint32_t color; 35 | int width; 36 | int height; 37 | int align; 38 | int size; 39 | int edge; 40 | int space_h; 41 | int space_w; 42 | int auto_scale; 43 | }; 44 | 45 | #define SIZEOF_LABEL (sizeof(struct pack_label)) 46 | 47 | struct pack_quad { 48 | int texid; 49 | uv_t texture_coord[8]; 50 | int32_t screen_coord[8]; 51 | }; 52 | 53 | #define SIZEOF_QUAD (sizeof(struct pack_quad)) 54 | 55 | struct pack_picture { 56 | int n; 57 | struct pack_quad rect[1]; 58 | }; 59 | 60 | #define SIZEOF_PICTURE (sizeof(struct pack_picture) - sizeof(struct pack_quad)) 61 | 62 | struct pack_poly_data { 63 | offset_t texture_coord; // uv_t * 64 | offset_t screen_coord; // int32_t * 65 | int texid; 66 | int n; 67 | }; 68 | 69 | #define SIZEOF_POLY (sizeof(struct pack_poly_data)) 70 | 71 | struct pack_polygon_data { 72 | int n; 73 | struct pack_poly_data poly[1]; 74 | }; 75 | 76 | #define SIZEOF_POLYGON (sizeof(struct pack_polygon_data) - sizeof(struct pack_poly_data)) 77 | 78 | struct sprite_trans { 79 | struct matrix * mat; 80 | uint32_t color; 81 | uint32_t additive; 82 | int program; 83 | }; 84 | 85 | struct sprite_trans_data { 86 | offset_t mat; 87 | uint32_t color; 88 | uint32_t additive; 89 | }; 90 | 91 | #define SIZEOF_TRANS (sizeof(struct sprite_trans_data)) 92 | 93 | struct pack_part { 94 | struct sprite_trans_data t; 95 | int16_t component_id; 96 | int16_t touchable; 97 | }; 98 | 99 | #define SIZEOF_PART (sizeof(struct pack_part)) 100 | 101 | struct pack_frame { 102 | offset_t part; // struct pack_part * 103 | int n; 104 | }; 105 | 106 | #define SIZEOF_FRAME (sizeof(struct pack_frame)) 107 | 108 | struct pack_action { 109 | offset_t name; // const char * 110 | int16_t number; 111 | int16_t start_frame; 112 | }; 113 | 114 | #define SIZEOF_ACTION (sizeof(struct pack_action)) 115 | 116 | struct pack_component { 117 | offset_t name; // const char * 118 | int id; 119 | }; 120 | 121 | #define SIZEOF_COMPONENT (sizeof(struct pack_component)) 122 | 123 | struct pack_animation { 124 | offset_t frame; // struct pack_frame * 125 | offset_t action; // struct pack_action * 126 | int frame_number; 127 | int action_number; 128 | int component_number; 129 | struct pack_component component[1]; 130 | }; 131 | 132 | #define SIZEOF_ANIMATION (sizeof(struct pack_animation) - sizeof(struct pack_component)) 133 | 134 | struct sprite_pack { 135 | offset_t type; // uint8_t * 136 | offset_t data; // void ** 137 | int n; 138 | int tex[2]; 139 | }; 140 | 141 | #define SIZEOF_PACK (sizeof(struct sprite_pack) - 2 * sizeof(int)) 142 | 143 | int ejoy2d_spritepack(lua_State *L); 144 | void dump_pack(struct sprite_pack *pack); 145 | 146 | #define OFFSET_TO_POINTER(t, pack, off) ((off == 0) ? NULL : (t*)((uintptr_t)(pack) + (off))) 147 | #define OFFSET_TO_STRING(pack, off) ((const char *)(pack) + (off)) 148 | #define POINTER_TO_OFFSET(pack, ptr) ((ptr == NULL) ? 0 : (offset_t)((uintptr_t)(ptr) - (uintptr_t)pack)) 149 | 150 | #endif 151 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lua/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /mac/example/example.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /mac/example/example.xcodeproj/project.xcworkspace/xcshareddata/example.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | CE4B60CE-5670-433F-B670-07F804E027F5 9 | IDESourceControlProjectName 10 | example 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 7BECAF10-5173-4676-9513-C1C7F21D6682 14 | http://github.com/cloudwu/ejoy2d.git 15 | 16 | IDESourceControlProjectPath 17 | mac/example/example.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 7BECAF10-5173-4676-9513-C1C7F21D6682 21 | ../../../.. 22 | 23 | IDESourceControlProjectURL 24 | http://github.com/cloudwu/ejoy2d.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 7BECAF10-5173-4676-9513-C1C7F21D6682 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 7BECAF10-5173-4676-9513-C1C7F21D6682 36 | IDESourceControlWCCName 37 | ejoy2d 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /mac/example/example/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "fault.h" 9 | 10 | #include "winfw.h" 11 | 12 | #define UPDATE_INTERVAL 1 /* 10ms */ 13 | 14 | #define TITLE "Ejoy" 15 | #define WIDTH 1024 16 | #define HEIGHT 768 17 | 18 | 19 | struct GLFW_context { 20 | GLFWwindow* window; 21 | bool is_press; 22 | }; 23 | 24 | static struct GLFW_context *CONTEXT = NULL; 25 | 26 | void font_init(); 27 | 28 | 29 | static uint32_t 30 | _gettime(void) { 31 | uint32_t t; 32 | struct timeval tv; 33 | gettimeofday(&tv, NULL); 34 | t = (uint32_t)(tv.tv_sec & 0xffffff) * 100; 35 | t += tv.tv_usec / 10000; 36 | return t; 37 | } 38 | 39 | static void 40 | _glfw_error_cb(int error, const char* desc) { 41 | fault("glfw: %s\n", desc); 42 | } 43 | 44 | static void 45 | _glfw_key_cb(GLFWwindow* window, int key, int scancode, int action, int mods) { 46 | if(key == GLFW_KEY_Q && action == GLFW_PRESS) { 47 | glfwSetWindowShouldClose(window, GL_TRUE); 48 | } 49 | } 50 | 51 | static void 52 | _glfw_pos_cb(GLFWwindow* window, double xpos, double ypos) { 53 | if(CONTEXT->is_press) { 54 | ejoy2d_win_touch(xpos, ypos, TOUCH_MOVE); 55 | } 56 | } 57 | 58 | 59 | static void 60 | _glfw_btn_cb(GLFWwindow* window, int button, int action, int mods) { 61 | CONTEXT->is_press = (action==GLFW_PRESS)?(true):(false); 62 | double xpos, ypos; 63 | glfwGetCursorPos(window, &xpos, &ypos); 64 | 65 | switch(action) { 66 | case GLFW_PRESS: 67 | ejoy2d_win_touch(xpos, ypos, TOUCH_BEGIN); 68 | CONTEXT->is_press = true; 69 | break; 70 | case GLFW_RELEASE: 71 | ejoy2d_win_touch(xpos, ypos, TOUCH_END); 72 | CONTEXT->is_press = false; 73 | break; 74 | default: 75 | CONTEXT->is_press = false; 76 | break; 77 | } 78 | } 79 | 80 | static void 81 | _glfw_init(struct GLFW_context* context) { 82 | if(!glfwInit()) { 83 | fault("glfw init error.\n"); 84 | } 85 | 86 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 87 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 88 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 89 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 90 | 91 | context->window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL); 92 | if (!context->window) { 93 | glfwTerminate(); 94 | fault("glfw create window error.\n"); 95 | } 96 | 97 | // set call back 98 | glfwSetErrorCallback(_glfw_error_cb); 99 | glfwSetKeyCallback(context->window, _glfw_key_cb); 100 | glfwSetCursorPosCallback(context->window, _glfw_pos_cb); 101 | glfwSetMouseButtonCallback(context->window, _glfw_btn_cb); 102 | 103 | 104 | glfwMakeContextCurrent(context->window); 105 | glfwSwapInterval(1); 106 | } 107 | 108 | 109 | static void 110 | _version_info() { 111 | const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string 112 | const GLubyte* version = glGetString(GL_VERSION); // version as a string 113 | const GLubyte* glsl_version = glGetString(GL_SHADING_LANGUAGE_VERSION); // glsl version 114 | 115 | printf("Renderer: %s\n", renderer); 116 | printf("OpenGL version supported %s\n", version); 117 | printf("GLSL: %s\n", glsl_version); 118 | } 119 | 120 | 121 | int 122 | main(int argc, char *argv[]) { 123 | struct GLFW_context ctx_value = { 124 | .window = NULL, 125 | .is_press = false, 126 | }; 127 | 128 | CONTEXT = &ctx_value; 129 | _glfw_init(CONTEXT); 130 | 131 | _version_info(); 132 | 133 | font_init(); 134 | ejoy2d_win_init(argc, argv); 135 | 136 | uint32_t timestamp = 0; 137 | GLFWwindow* window = CONTEXT->window; 138 | 139 | /* Loop until the user closes the window */ 140 | while(!glfwWindowShouldClose(window)) { 141 | // update frame 142 | uint32_t current = _gettime(); 143 | if(current - timestamp >= UPDATE_INTERVAL) { 144 | timestamp = current; 145 | ejoy2d_win_update(); 146 | ejoy2d_win_frame(); 147 | } 148 | 149 | /* Swap front and back buffers */ 150 | glfwSwapBuffers(window); 151 | 152 | /* Poll for and process events */ 153 | glfwPollEvents(); 154 | } 155 | 156 | glfwDestroyWindow(window); 157 | glfwTerminate(); 158 | 159 | return 0; 160 | } -------------------------------------------------------------------------------- /mac/example/example/winfont.c: -------------------------------------------------------------------------------- 1 | #include "label.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include FT_FREETYPE_H 8 | #include FT_GLYPH_H 9 | 10 | FT_Library library; 11 | 12 | #ifdef __APPLE__ 13 | static const char* TTFONT = "/System/Library/Fonts/STHeiti Light.ttc"; 14 | #else 15 | static const char* TTFONT = "/usr/share/fonts/wenquanyi/wqy-zenhei/wqy-zenhei.ttc"; 16 | #endif 17 | 18 | #define _fault(errcode, msg) __fault(errcode, msg, __FILE__, __LINE__) 19 | 20 | static void __fault(int errcode, const char * msg, const char *file, int line) { 21 | if (errcode) 22 | printf("err(%d): %s, error occured in file %s, line %d\n\n\t have a look at fterrdef.h\n",errcode, msg, file, line); 23 | else 24 | printf("err: %s, occured in file %s, line %d\n", msg, file, line); 25 | exit(1); 26 | } 27 | struct bitmap { 28 | void * buf; 29 | int w; 30 | int h; 31 | int pitch; 32 | }; 33 | 34 | /* x,y is offset of target */ 35 | static void 36 | cpy_bitmap(struct bitmap * target, struct bitmap * src, int x, int y) { 37 | unsigned char * sbuf = src->buf; 38 | unsigned char * tbuf = target->buf; 39 | 40 | int x0,y0,x1,y1; 41 | x0 = x > 0 ? x : 0; 42 | y0 = y > 0 ? y : 0; 43 | x1 = (x + src->w > target->w) ? target->w : (x+src->w); 44 | y1 = (y + src->h > target->h) ? target->h : (y+src->h); 45 | 46 | if (x1 <= x0 || y1 <= y0) 47 | return; 48 | if (x0 >= target->w || y0 >= target->h) 49 | return; 50 | 51 | int w = x1 - x0; 52 | int h = y1 - y0; 53 | 54 | tbuf += y0 * target->pitch + x0; 55 | sbuf += (y0 - y)*src->pitch + x0 - x; 56 | int i,j; 57 | for (i=0;ipitch; 61 | tbuf += target->pitch; 62 | } 63 | } 64 | 65 | void 66 | font_create(int font_size, struct font_context *ctx) { 67 | FT_Face face; 68 | int err = FT_New_Face(library, TTFONT, 0, &face); 69 | if (err) { 70 | if (err == 1) 71 | _fault(err, "set your own vector font resource path"); 72 | else 73 | _fault(err, "new face failed"); 74 | } 75 | 76 | err = FT_Set_Pixel_Sizes(face,0,font_size); 77 | 78 | if (err) 79 | _fault(err, "set char size failed"); 80 | 81 | ctx->font = face; 82 | ctx->ascent = face->size->metrics.ascender >>6; 83 | ctx->h = face->size->metrics.height >> 6; 84 | } 85 | 86 | void 87 | font_release(struct font_context *ctx) { 88 | FT_Done_Face(ctx->font); 89 | } 90 | 91 | void 92 | font_size(const char *str, int unicode, struct font_context *ctx) { 93 | FT_Face face = ctx->font; 94 | FT_UInt glyph_index = FT_Get_Char_Index(face, unicode); 95 | if (!glyph_index) { 96 | ctx->w = 0; 97 | return; 98 | } 99 | FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_BITMAP); 100 | 101 | int err = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL); 102 | if (err) 103 | _fault(err, "render failed"); 104 | 105 | FT_GlyphSlot slot = face->glyph; 106 | 107 | ctx->w = slot->advance.x >> 6; 108 | } 109 | 110 | 111 | void 112 | font_glyph(const char * str, int unicode, void * buffer, struct font_context *ctx) { 113 | FT_Face face = ctx->font; 114 | FT_GlyphSlot slot = face->glyph; 115 | FT_Bitmap *bitmap = &(slot->bitmap); 116 | 117 | int offx = slot->bitmap_left; 118 | int offy = ctx->ascent - slot->bitmap_top; 119 | 120 | struct bitmap src; 121 | struct bitmap target; 122 | src.buf = bitmap->buffer; 123 | src.pitch = bitmap->pitch; 124 | src.w = bitmap->width; 125 | src.h = bitmap->rows; 126 | target.buf = buffer; 127 | target.w = target.pitch = ctx->w; 128 | target.h = ctx->h; 129 | 130 | cpy_bitmap(&target, &src, offx, offy); 131 | } 132 | 133 | void 134 | font_init() { 135 | if (FT_Init_FreeType(&library)) { 136 | printf("font init failed"); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /mac/example/example/winfw.c: -------------------------------------------------------------------------------- 1 | #include "opengl.h" 2 | #include "ejoy2dgame.h" 3 | #include "fault.h" 4 | #include "screen.h" 5 | #include "winfw.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | struct WINDOWGAME { 13 | struct game *game; 14 | int intouch; 15 | }; 16 | 17 | static const int BUFSIZE = 2048; 18 | 19 | static struct WINDOWGAME *G = NULL; 20 | 21 | static const char * startscript = 22 | "local path,script = ...\n" 23 | "require(\"ejoy2d.framework\").WorkDir = ''\n" 24 | "assert(script, 'I need a script name')\n" 25 | "path = string.match(path,[[(.*)/[^/]*$]])\n" 26 | "package.path = path .. [[/?.lua;]] .. path .. [[/?/init.lua]]\n" 27 | "local f = assert(loadfile(script))\n" 28 | "f(script)\n" 29 | ; 30 | 31 | static struct WINDOWGAME * 32 | create_game() { 33 | struct WINDOWGAME * g = (struct WINDOWGAME *)malloc(sizeof(*g)); 34 | g->game = ejoy2d_game(); 35 | g->intouch = 0; 36 | return g; 37 | } 38 | 39 | static int 40 | traceback(lua_State *L) { 41 | const char *msg = lua_tostring(L, 1); 42 | if (msg == NULL) { 43 | if (luaL_callmeta(L, 1, "__tostring") && 44 | lua_type(L, -1) == LUA_TSTRING) 45 | return 1; 46 | else 47 | msg = lua_pushfstring(L, "(error object is a %s value)", 48 | luaL_typename(L, 1)); 49 | } 50 | luaL_traceback(L, L, msg, 1); 51 | return 1; 52 | } 53 | 54 | #ifdef __APPLE__ 55 | static const char* 56 | _read_exepath(char * buf, int bufsz) { 57 | return getenv("_"); 58 | } 59 | #define read_exepath(buf,bufsz) _read_exepath(buf,bufsz) 60 | 61 | #else 62 | static const char* 63 | read_exepath(char * buf, int bufsz) { 64 | int count; 65 | count = readlink("/proc/self/exe", buf, bufsz); 66 | 67 | if (count < 0) 68 | return NULL; 69 | return buf; 70 | } 71 | #endif 72 | 73 | 74 | void 75 | ejoy2d_win_init(int argc, char *argv[]) { 76 | G = create_game(); 77 | lua_State *L = ejoy2d_game_lua(G->game); 78 | lua_pushcfunction(L, traceback); 79 | int tb = lua_gettop(L); 80 | int err = luaL_loadstring(L, startscript); 81 | if (err) { 82 | const char *msg = lua_tostring(L,-1); 83 | fault("%s", msg); 84 | } 85 | 86 | lua_pushstring(L, "./"); 87 | lua_pushstring(L, "examples/ex03.lua"); 88 | 89 | err = lua_pcall(L, 2, 0, tb); 90 | if (err) { 91 | const char *msg = lua_tostring(L,-1); 92 | fault("%s", msg); 93 | } 94 | 95 | lua_pop(L,1); 96 | 97 | screen_init(WIDTH,HEIGHT,1.0f); 98 | ejoy2d_game_start(G->game); 99 | } 100 | 101 | void 102 | ejoy2d_win_update() { 103 | ejoy2d_game_update(G->game, 0.01f); 104 | } 105 | 106 | void 107 | ejoy2d_win_frame() { 108 | ejoy2d_game_drawframe(G->game); 109 | } 110 | 111 | void 112 | ejoy2d_win_touch(int x, int y,int touch) { 113 | switch (touch) { 114 | case TOUCH_BEGIN: 115 | G->intouch = 1; 116 | break; 117 | case TOUCH_END: 118 | G->intouch = 0; 119 | break; 120 | case TOUCH_MOVE: 121 | if (!G->intouch) { 122 | return; 123 | } 124 | break; 125 | } 126 | // windows only support one touch id (0) 127 | int id = 0; 128 | ejoy2d_game_touch(G->game, id, x,y,touch); 129 | } 130 | 131 | -------------------------------------------------------------------------------- /mac/example/example/winfw.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_windows_fw_h 2 | #define ejoy2d_windows_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 | void ejoy2d_win_init(); 12 | void ejoy2d_win_frame(); 13 | void ejoy2d_win_update(); 14 | void ejoy2d_win_touch(int x, int y,int touch); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /mingw/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "winfw.h" 8 | 9 | #define CLASSNAME L"EJOY" 10 | #define WINDOWNAME L"EJOY" 11 | #define WINDOWSTYLE (WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX) 12 | 13 | static void 14 | set_pixel_format_to_hdc(HDC hDC) 15 | { 16 | int color_deep; 17 | PIXELFORMATDESCRIPTOR pfd; 18 | 19 | color_deep = GetDeviceCaps(hDC, BITSPIXEL); 20 | 21 | memset(&pfd, 0, sizeof(pfd)); 22 | pfd.nSize = sizeof(pfd); 23 | pfd.nVersion = 1; 24 | pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 25 | pfd.iPixelType = PFD_TYPE_RGBA; 26 | pfd.cColorBits = color_deep; 27 | pfd.cDepthBits = 0; 28 | pfd.cStencilBits = 0; 29 | pfd.iLayerType = PFD_MAIN_PLANE; 30 | 31 | int pixelFormat = ChoosePixelFormat(hDC, &pfd); 32 | 33 | SetPixelFormat(hDC, pixelFormat, &pfd); 34 | } 35 | 36 | static void 37 | init_window(HWND hWnd) { 38 | HDC hDC = GetDC(hWnd); 39 | 40 | set_pixel_format_to_hdc(hDC); 41 | HGLRC glrc = wglCreateContext(hDC); 42 | 43 | if (glrc == 0) { 44 | exit(1); 45 | } 46 | 47 | wglMakeCurrent(hDC, glrc); 48 | 49 | if ( glewInit() != GLEW_OK ) { 50 | exit(1); 51 | } 52 | 53 | glViewport(0, 0, WIDTH, HEIGHT); 54 | 55 | ReleaseDC(hWnd, hDC); 56 | } 57 | 58 | static void 59 | update_frame(HDC hDC) { 60 | ejoy2d_win_frame(); 61 | SwapBuffers(hDC); 62 | 63 | } 64 | 65 | static void 66 | get_xy(LPARAM lParam, int *x, int *y) { 67 | *x = (short)(lParam & 0xffff); 68 | *y = (short)((lParam>>16) & 0xffff); 69 | } 70 | 71 | static LRESULT CALLBACK 72 | WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 73 | { 74 | switch (message) { 75 | case WM_CREATE: 76 | init_window(hWnd); 77 | SetTimer(hWnd,0,10,NULL); 78 | break; 79 | case WM_PAINT: { 80 | if (GetUpdateRect(hWnd, NULL, FALSE)) { 81 | HDC hDC = GetDC(hWnd); 82 | update_frame(hDC); 83 | ValidateRect(hWnd, NULL); 84 | ReleaseDC(hWnd, hDC); 85 | } 86 | return 0; 87 | } 88 | case WM_TIMER : { 89 | ejoy2d_win_update(); 90 | InvalidateRect(hWnd, NULL , FALSE); 91 | break; 92 | } 93 | case WM_DESTROY: 94 | PostQuitMessage(0); 95 | return 0; 96 | case WM_LBUTTONUP: { 97 | int x,y; 98 | get_xy(lParam, &x, &y); 99 | ejoy2d_win_touch(x,y,TOUCH_END); 100 | break; 101 | } 102 | case WM_LBUTTONDOWN: { 103 | int x,y; 104 | get_xy(lParam, &x, &y); 105 | ejoy2d_win_touch(x,y,TOUCH_BEGIN); 106 | break; 107 | } 108 | case WM_MOUSEMOVE: { 109 | int x,y; 110 | get_xy(lParam, &x, &y); 111 | ejoy2d_win_touch(x,y,TOUCH_MOVE); 112 | break; 113 | } 114 | } 115 | return DefWindowProcW(hWnd, message, wParam, lParam); 116 | } 117 | 118 | static void 119 | register_class() 120 | { 121 | WNDCLASSW wndclass; 122 | 123 | wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 124 | wndclass.lpfnWndProc = WndProc; 125 | wndclass.cbClsExtra = 0; 126 | wndclass.cbWndExtra = 0; 127 | wndclass.hInstance = GetModuleHandleW(0); 128 | wndclass.hIcon = 0; 129 | wndclass.hCursor = 0; 130 | wndclass.hbrBackground = 0; 131 | wndclass.lpszMenuName = 0; 132 | wndclass.lpszClassName = CLASSNAME; 133 | 134 | RegisterClassW(&wndclass); 135 | } 136 | 137 | static HWND 138 | create_window(int w, int h) { 139 | RECT rect; 140 | 141 | rect.left=0; 142 | rect.right=w; 143 | rect.top=0; 144 | rect.bottom=h; 145 | 146 | AdjustWindowRect(&rect,WINDOWSTYLE,0); 147 | 148 | HWND wnd=CreateWindowExW(0,CLASSNAME,WINDOWNAME, 149 | WINDOWSTYLE, CW_USEDEFAULT,0, 150 | rect.right-rect.left,rect.bottom-rect.top, 151 | 0,0, 152 | GetModuleHandleW(0), 153 | 0); 154 | 155 | return wnd; 156 | } 157 | 158 | int 159 | main(int argc, char *argv[]) { 160 | register_class(); 161 | HWND wnd = create_window(WIDTH,HEIGHT); 162 | ejoy2d_win_init(argc, argv); 163 | 164 | ShowWindow(wnd, SW_SHOWDEFAULT); 165 | UpdateWindow(wnd); 166 | 167 | MSG msg; 168 | while (GetMessage(&msg, NULL, 0, 0)) { 169 | TranslateMessage(&msg); 170 | DispatchMessage(&msg); 171 | } 172 | 173 | return 0; 174 | } 175 | -------------------------------------------------------------------------------- /mingw/winfont.c: -------------------------------------------------------------------------------- 1 | #include "label.h" 2 | #include "array.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void 10 | font_create(int font_size, struct font_context *ctx) { 11 | TEXTMETRIC tm; 12 | HFONT f = CreateFontW( 13 | font_size,0, 14 | 0, 0, 15 | FW_SEMIBOLD, 16 | FALSE, FALSE, FALSE, 17 | DEFAULT_CHARSET, 18 | OUT_DEFAULT_PRECIS, 19 | CLIP_DEFAULT_PRECIS, 20 | ANTIALIASED_QUALITY, 21 | DEFAULT_PITCH, 22 | NULL 23 | ); 24 | 25 | HDC dc = CreateCompatibleDC(NULL); 26 | SelectObject(dc, f); 27 | ctx->font = (void *)f; 28 | ctx->dc = (void *)dc; 29 | GetTextMetrics(dc,&tm); 30 | ctx->h=tm.tmHeight + 1; 31 | ctx->ascent=tm.tmAscent; 32 | } 33 | 34 | void 35 | font_release(struct font_context *ctx) { 36 | DeleteObject((HFONT)ctx->font); 37 | DeleteDC((HDC)ctx->dc); 38 | } 39 | 40 | static MAT2 mat2={{0,1},{0,0},{0,0},{0,1}}; 41 | 42 | void 43 | font_size(const char *str, int unicode, struct font_context *ctx) { 44 | GLYPHMETRICS gm; 45 | 46 | GetGlyphOutlineW( 47 | (HDC)ctx->dc, 48 | unicode, 49 | GGO_GRAY8_BITMAP, 50 | &gm, 51 | 0, 52 | NULL, 53 | &mat2 54 | ); 55 | 56 | ctx->w = gm.gmCellIncX + 1; 57 | } 58 | 59 | void 60 | font_glyph(const char * str, int unicode, void * buffer, struct font_context *ctx) { 61 | GLYPHMETRICS gm; 62 | memset(&gm,0,sizeof(gm)); 63 | 64 | ARRAY(uint8_t, tmp, ctx->w * ctx->h); 65 | memset(tmp,0, ctx->w * ctx->h); 66 | 67 | GetGlyphOutlineW( 68 | (HDC)ctx->dc, 69 | unicode, 70 | GGO_GRAY8_BITMAP, 71 | &gm, 72 | ctx->w * ctx->h, 73 | tmp, 74 | &mat2 75 | ); 76 | 77 | int w = (gm.gmBlackBoxX + 3) & ~3; 78 | int h = gm.gmBlackBoxY; 79 | 80 | uint8_t * buf = (uint8_t *)buffer; 81 | int offx = gm.gmptGlyphOrigin.x; 82 | int offy = ctx->ascent - gm.gmptGlyphOrigin.y; 83 | assert(offx >= 0); 84 | assert(offy >= 0); 85 | assert(offx + gm.gmBlackBoxX <= ctx->w); 86 | assert(offy + h <= ctx->h); 87 | 88 | int i,j; 89 | 90 | for (i=0;iw + j + offx] = src * 255 / 64; 94 | } 95 | } 96 | } 97 | 98 | 99 | -------------------------------------------------------------------------------- /mingw/winfw.c: -------------------------------------------------------------------------------- 1 | #include "opengl.h" 2 | #include "ejoy2dgame.h" 3 | #include "fault.h" 4 | #include "screen.h" 5 | #include "winfw.h" 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | struct WINDOWGAME { 14 | struct game *game; 15 | int intouch; 16 | }; 17 | 18 | static struct WINDOWGAME *G = NULL; 19 | 20 | static const char * startscript = 21 | "local path, script = ...\n" 22 | "require(\"ejoy2d.framework\").WorkDir = ''\n" 23 | "assert(script, 'I need a script name')\n" 24 | "path = string.match(path,[[(.*)\\[^\\]*$]])\n" 25 | "package.path = path .. [[\\?.lua;]] .. path .. [[\\?\\init.lua;.\\?.lua;.\\?\\init.lua]]\n" 26 | "local f = assert(loadfile(script))\n" 27 | "f(script)\n" 28 | ; 29 | 30 | static struct WINDOWGAME * 31 | create_game() { 32 | struct WINDOWGAME * g = (struct WINDOWGAME *)malloc(sizeof(*g)); 33 | g->game = ejoy2d_game(); 34 | g->intouch = 0; 35 | return g; 36 | } 37 | 38 | static int 39 | traceback(lua_State *L) { 40 | const char *msg = lua_tostring(L, 1); 41 | if (msg == NULL) { 42 | if (luaL_callmeta(L, 1, "__tostring") && 43 | lua_type(L, -1) == LUA_TSTRING) 44 | return 1; 45 | else 46 | msg = lua_pushfstring(L, "(error object is a %s value)", 47 | luaL_typename(L, 1)); 48 | } 49 | luaL_traceback(L, L, msg, 1); 50 | return 1; 51 | } 52 | 53 | void 54 | ejoy2d_win_init(int argc, char *argv[]) { 55 | G = create_game(); 56 | screen_init(WIDTH,HEIGHT,1.0f); 57 | lua_State *L = ejoy2d_game_lua(G->game); 58 | lua_pushcfunction(L, traceback); 59 | int tb = lua_gettop(L); 60 | int err = luaL_loadstring(L, startscript); 61 | if (err) { 62 | const char *msg = lua_tostring(L,-1); 63 | fault("%s", msg); 64 | } 65 | 66 | char modname[1024]; 67 | 68 | int sz = GetModuleFileNameA(0, modname, 1024); 69 | 70 | lua_pushlstring(L, modname, sz); 71 | 72 | int i; 73 | for (i=1;igame); 86 | } 87 | 88 | void 89 | ejoy2d_win_update() { 90 | ejoy2d_game_update(G->game, 0.01f); 91 | } 92 | 93 | void 94 | ejoy2d_win_frame() { 95 | ejoy2d_game_drawframe(G->game); 96 | } 97 | 98 | void 99 | ejoy2d_win_touch(int x, int y,int touch) { 100 | switch (touch) { 101 | case TOUCH_BEGIN: 102 | G->intouch = 1; 103 | break; 104 | case TOUCH_END: 105 | G->intouch = 0; 106 | break; 107 | case TOUCH_MOVE: 108 | if (!G->intouch) { 109 | return; 110 | } 111 | break; 112 | } 113 | // windows only support one touch id (0) 114 | int id = 0; 115 | ejoy2d_game_touch(G->game, id, x,y,touch); 116 | } 117 | 118 | -------------------------------------------------------------------------------- /mingw/winfw.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_windows_fw_h 2 | #define ejoy2d_windows_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 | void ejoy2d_win_init(int argc, char *argv[]); 12 | void ejoy2d_win_frame(); 13 | void ejoy2d_win_update(); 14 | void ejoy2d_win_touch(int x, int y,int touch); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /msvc/.gitignore: -------------------------------------------------------------------------------- 1 | lib/ 2 | obj/ 3 | *.bsc 4 | *.exp 5 | *.ilk 6 | *.lastbuildstate 7 | *.pdb 8 | *.suo 9 | *.sdf 10 | *.opensdf 11 | *.ipch 12 | *.sln.cache 13 | -------------------------------------------------------------------------------- /msvc/build/ejoy2d.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), ejoy2d.root)) 6 | $(EJOY2D)\..\ 7 | $(EJOY2D)\obj\$(Configuration)\$(ProjectName)\ 8 | $(EJOY2D)\lib\$(Configuration)\ 9 | $(EJOY2D)\..\lua\ 10 | $(EJOY2D)\src\glew\ 11 | 12 | 13 | $(EJOY2D_OBJ) 14 | $(EJOY2D_LIB) 15 | 16 | 17 | 18 | Level3 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | $(EJOY2D) 33 | 34 | 35 | $(EJOY2D_ROOT) 36 | 37 | 38 | $(EJOY2D_OBJ) 39 | 40 | 41 | $(EJOY2D_LIB) 42 | 43 | 44 | $(EJOY2D_LUA) 45 | 46 | 47 | $(EJOY2D_GLEW) 48 | 49 | 50 | -------------------------------------------------------------------------------- /msvc/build/ejoy2d.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30110.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ejoy2d", "ejoy2d\ejoy2d.vcxproj", "{D3A9E9A6-062D-4C26-BF35-8F18ED840D8F}" 7 | ProjectSection(ProjectDependencies) = postProject 8 | {01BCD8E7-7F07-46E6-92CA-4B3E56E35031} = {01BCD8E7-7F07-46E6-92CA-4B3E56E35031} 9 | {029E13FB-276A-4B6F-8346-00F3072734E7} = {029E13FB-276A-4B6F-8346-00F3072734E7} 10 | EndProjectSection 11 | EndProject 12 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lua", "lua\lua.vcxproj", "{029E13FB-276A-4B6F-8346-00F3072734E7}" 13 | EndProject 14 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "glew", "glew\glew.vcxproj", "{01BCD8E7-7F07-46E6-92CA-4B3E56E35031}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Win32 = Debug|Win32 19 | Release|Win32 = Release|Win32 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {D3A9E9A6-062D-4C26-BF35-8F18ED840D8F}.Debug|Win32.ActiveCfg = Debug|Win32 23 | {D3A9E9A6-062D-4C26-BF35-8F18ED840D8F}.Debug|Win32.Build.0 = Debug|Win32 24 | {D3A9E9A6-062D-4C26-BF35-8F18ED840D8F}.Release|Win32.ActiveCfg = Release|Win32 25 | {D3A9E9A6-062D-4C26-BF35-8F18ED840D8F}.Release|Win32.Build.0 = Release|Win32 26 | {029E13FB-276A-4B6F-8346-00F3072734E7}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {029E13FB-276A-4B6F-8346-00F3072734E7}.Debug|Win32.Build.0 = Debug|Win32 28 | {029E13FB-276A-4B6F-8346-00F3072734E7}.Release|Win32.ActiveCfg = Release|Win32 29 | {029E13FB-276A-4B6F-8346-00F3072734E7}.Release|Win32.Build.0 = Release|Win32 30 | {01BCD8E7-7F07-46E6-92CA-4B3E56E35031}.Debug|Win32.ActiveCfg = Debug|Win32 31 | {01BCD8E7-7F07-46E6-92CA-4B3E56E35031}.Debug|Win32.Build.0 = Debug|Win32 32 | {01BCD8E7-7F07-46E6-92CA-4B3E56E35031}.Release|Win32.ActiveCfg = Release|Win32 33 | {01BCD8E7-7F07-46E6-92CA-4B3E56E35031}.Release|Win32.Build.0 = Release|Win32 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | EndGlobal 39 | -------------------------------------------------------------------------------- /msvc/build/ejoy2d/ejoy2d.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | WindowsLocalDebugger 5 | examples/ex09.lua 6 | $(EJOY2D_ROOT)$(TargetName)$(TargetExt) 7 | $(EJOY2D_ROOT) 8 | 9 | 10 | $(EJOY2D_ROOT)$(TargetName)$(TargetExt) 11 | WindowsLocalDebugger 12 | examples/ex01.lua 13 | $(EJOY2D_ROOT) 14 | 15 | -------------------------------------------------------------------------------- /msvc/build/glew/glew.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {01BCD8E7-7F07-46E6-92CA-4B3E56E35031} 15 | Win32Proj 16 | glew 17 | 18 | 19 | 20 | DynamicLibrary 21 | true 22 | v110 23 | MultiByte 24 | 25 | 26 | DynamicLibrary 27 | false 28 | v110 29 | true 30 | MultiByte 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | Disabled 51 | GLEW_BUILD;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 52 | $(EJOY2D)\src\glew\;%(AdditionalIncludeDirectories) 53 | 54 | 55 | Windows 56 | true 57 | $(EJOY2D_ROOT)$(TargetName)$(TargetExt) 58 | opengl32.lib;%(AdditionalDependencies) 59 | 60 | 61 | 62 | 63 | 64 | 65 | MaxSpeed 66 | true 67 | true 68 | GLEW_BUILD;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 69 | $(EJOY2D)\src\glew\;%(AdditionalIncludeDirectories) 70 | 71 | 72 | Windows 73 | true 74 | true 75 | true 76 | $(EJOY2D_ROOT)$(TargetName)$(TargetExt) 77 | opengl32.lib;%(AdditionalDependencies) 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /msvc/build/glew/glew.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {55d0d2cd-11ff-4266-8730-f5bff95888c0} 14 | 15 | 16 | 17 | 18 | inc\GL 19 | 20 | 21 | inc\GL 22 | 23 | 24 | 25 | 26 | src 27 | 28 | 29 | -------------------------------------------------------------------------------- /msvc/ejoy2d.root: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/msvc/ejoy2d.root -------------------------------------------------------------------------------- /msvc/include/lauxlib.h: -------------------------------------------------------------------------------- 1 | #include "lua_path.h" 2 | 3 | extern "C" { 4 | #include EJOY2D_LUA_PATH(lauxlib.h) 5 | } 6 | -------------------------------------------------------------------------------- /msvc/include/lua.h: -------------------------------------------------------------------------------- 1 | #include "lua_path.h" 2 | 3 | extern "C" { 4 | #include EJOY2D_LUA_PATH(lua.h) 5 | } 6 | -------------------------------------------------------------------------------- /msvc/include/lua_path.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if !defined(EJOY2D_LUA_PATH) 4 | # define EJOY2D_PP_CAT(a, b) EJOY2D_PP_CAT_I(a, b) 5 | # define EJOY2D_PP_CAT_I(a, b) a ## b 6 | # define EJOY2D_PP_STRINGIZE(text) EJOY2D_PP_STRINGIZE_A((text)) 7 | # define EJOY2D_PP_STRINGIZE_A(arg) EJOY2D_PP_STRINGIZE_I arg 8 | # define EJOY2D_PP_STRINGIZE_I(text) #text 9 | # define EJOY2D_LUA_PATH(file) EJOY2D_PP_STRINGIZE(EJOY2D_PP_CAT(EJOY2D_PP_CAT(EJOY2D_LUA, \), file)) 10 | #endif 11 | -------------------------------------------------------------------------------- /msvc/include/lualib.h: -------------------------------------------------------------------------------- 1 | #include "lua_path.h" 2 | 3 | extern "C" { 4 | #include EJOY2D_LUA_PATH(lualib.h) 5 | } 6 | -------------------------------------------------------------------------------- /msvc/include/stdbool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ejoy/ejoy2d/bf4af9140af2bf0692b96dbc78f7b1e89dfb6924/msvc/include/stdbool.h -------------------------------------------------------------------------------- /msvc/make.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | if defined VS120COMNTOOLS ( 4 | call "%VS120COMNTOOLS%\vsvars32.bat" 5 | ) else if defined VS110COMNTOOLS ( 6 | call "%VS110COMNTOOLS%\vsvars32.bat" 7 | ) else if defined VS100COMNTOOLS ( 8 | call "%VS100COMNTOOLS%\vsvars32.bat" 9 | ) else ( 10 | echo ERROR: Cannot found Visual Studio Toolset. 11 | echo Are you sure Visual Studio 2010 or later is installed? 12 | exit /B 1 13 | ) 14 | 15 | if {%1}=={} ( 16 | set configuration="Release" 17 | ) else ( 18 | set configuration=%1 19 | ) 20 | 21 | msbuild "%~dp0/build/ejoy2d/ejoy2d.vcxproj" /m /v:m /t:rebuild /clp:ShowEventId /p:Configuration="%configuration%",Platform="Win32" 22 | -------------------------------------------------------------------------------- /msvc/src/ejoy2d/winmain.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]); 4 | 5 | #if defined(_DEBUG) 6 | 7 | #include 8 | 9 | bool has_exception = true; 10 | 11 | static int 12 | exit_event() { 13 | if (has_exception) { 14 | printf("\n\nThe process will exit.\n"); 15 | system("pause"); 16 | } 17 | return 0; 18 | } 19 | 20 | int __stdcall 21 | WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) { 22 | FILE* new_file; 23 | AllocConsole(); 24 | freopen_s(&new_file, "CONIN$", "r", stdin); 25 | freopen_s(&new_file, "CONOUT$", "w", stdout); 26 | freopen_s(&new_file, "CONOUT$", "w", stderr); 27 | onexit(exit_event); 28 | 29 | int retval = main(__argc, __argv); 30 | has_exception = false; 31 | return retval; 32 | } 33 | 34 | #else 35 | 36 | int __stdcall 37 | WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) { 38 | return main(__argc, __argv); 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /posix/glx.c: -------------------------------------------------------------------------------- 1 | #include "opengl.h" 2 | #include "visual.h" 3 | #include "log.h" 4 | #include "module.h" 5 | 6 | #include "posix/xwindow.h" 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | static GLXContext g_context = 0; 14 | static struct X_context *g_X=0; 15 | 16 | void 17 | opengl_release() 18 | { 19 | if (g_context){ 20 | glXMakeCurrent( g_X->display, None, NULL); 21 | g_context=NULL; 22 | } 23 | } 24 | 25 | void 26 | opengl_swap_buffers() 27 | { 28 | if (g_context){ 29 | glXSwapBuffers( g_X->display, g_X->wnd); 30 | } 31 | } 32 | 33 | static int 34 | glx_init(struct X_context *X) 35 | { 36 | XVisualInfo *vi; 37 | 38 | int attrib[]={ 39 | GLX_RGBA, 40 | GLX_DOUBLEBUFFER, 41 | GLX_DEPTH_SIZE, 1, 42 | GLX_STENCIL_SIZE, 1, 43 | None 44 | }; 45 | 46 | if (g_context) 47 | return 0; 48 | 49 | vi = glXChooseVisual( X->display, X->screen_num, attrib); 50 | 51 | if (vi==0) { 52 | return 1; 53 | } 54 | 55 | g_context = glXCreateContext( X->display, vi , NULL , True); 56 | 57 | if (g_context == 0) { 58 | return 1; 59 | } 60 | 61 | if (!glXMakeCurrent( X->display, X->wnd, g_context )) { 62 | g_context=NULL; 63 | return 1; 64 | } 65 | /* 66 | if (!glXIsDirect( X->display, g_context)) { 67 | printf("undirect\n"); 68 | } 69 | */ 70 | 71 | g_X=X; 72 | 73 | return 0; 74 | } 75 | 76 | 77 | static int 78 | gl_change_context(struct X_context *X) 79 | { 80 | if (g_context == 0) { 81 | return 1; 82 | } 83 | 84 | if (!glXMakeCurrent( X->display, X->wnd, g_context )) { 85 | g_context=NULL; 86 | return 1; 87 | } 88 | 89 | g_X=X; 90 | 91 | return 0; 92 | } 93 | 94 | 95 | int 96 | opengl_create_window(int w,int h,void *ud) 97 | { 98 | visual_resize(w,h,ud); 99 | struct X_context *X=visual_device(0,0); 100 | return gl_change_context(X); 101 | } 102 | 103 | const char * 104 | opengl_error_string(const char *src) 105 | { 106 | return src; 107 | } 108 | 109 | int 110 | opengl_init() 111 | { 112 | USING(visual); 113 | USING(log); 114 | 115 | if (glx_init((struct X_context *)visual_device(0,0))) { 116 | return 1; 117 | } 118 | if ( glewInit() != GLEW_OK ) { 119 | return 1; 120 | } 121 | 122 | log_write(LOG_INFO,"%s %s\n",glGetString(GL_RENDERER),glGetString(GL_VENDOR)); 123 | 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /posix/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /* include some silly stuff */ 8 | #include 9 | #include 10 | #include 11 | #include "winfw.h" 12 | 13 | #define UPDATE_INTERVAL 1 /* 10ms */ 14 | 15 | void font_init(); 16 | 17 | struct X_context { 18 | Display *display; 19 | int screen_num; 20 | Window wnd; 21 | }; 22 | 23 | static GC gc; 24 | static GLXContext g_context = 0; 25 | struct X_context g_X; 26 | /* Used to intercept window closing requests. */ 27 | static Atom wm_delete_window; 28 | 29 | static uint32_t 30 | _gettime(void) { 31 | uint32_t t; 32 | #if !defined(__APPLE__) 33 | struct timespec ti; 34 | clock_gettime(CLOCK_MONOTONIC, &ti); 35 | t = (uint32_t)(ti.tv_sec & 0xffffff) * 100; 36 | t += ti.tv_nsec / 10000000; 37 | #else 38 | struct timeval tv; 39 | gettimeofday(&tv, NULL); 40 | t = (uint32_t)(tv.tv_sec & 0xffffff) * 100; 41 | t += tv.tv_usec / 10000; 42 | #endif 43 | 44 | return t; 45 | } 46 | 47 | static void 48 | update_frame() { 49 | ejoy2d_win_frame(); 50 | glXSwapBuffers(g_X.display, g_X.wnd); 51 | } 52 | 53 | static int 54 | glx_init(struct X_context *X) 55 | { 56 | XVisualInfo *vi; 57 | 58 | int attrib[]={ 59 | GLX_RGBA, 60 | GLX_DOUBLEBUFFER, 61 | GLX_DEPTH_SIZE, 1, 62 | GLX_STENCIL_SIZE, 1, 63 | None 64 | }; 65 | 66 | if (g_context) 67 | return 0; 68 | 69 | vi = glXChooseVisual( X->display, X->screen_num, attrib); 70 | 71 | if (vi==0) { 72 | return 1; 73 | } 74 | 75 | g_context = glXCreateContext( X->display, vi , NULL , True); 76 | 77 | if (g_context == 0) { 78 | return 1; 79 | } 80 | 81 | if (!glXMakeCurrent(X->display, X->wnd, g_context )) { 82 | g_context=NULL; 83 | return 1; 84 | } 85 | 86 | return 0; 87 | } 88 | 89 | static void 90 | init_x() { 91 | unsigned long black,white; 92 | Display *dis; 93 | int screen; 94 | static Window win; 95 | 96 | dis=XOpenDisplay(NULL); 97 | screen=DefaultScreen(dis); 98 | black=BlackPixel(dis,screen); 99 | white=WhitePixel(dis, screen); 100 | 101 | 102 | win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0, 103 | WIDTH, HEIGHT, 5,white, black); 104 | 105 | XMapWindow(dis, win); 106 | wm_delete_window = XInternAtom(dis, "WM_DELETE_WINDOW", False); 107 | XSetWMProtocols(dis, win, &wm_delete_window, 1); 108 | 109 | XSetStandardProperties(dis,win,"ejoy2d",NULL,None,NULL,0,NULL); 110 | XSelectInput(dis, win, 111 | ExposureMask|KeyPressMask|KeyReleaseMask 112 | |ButtonPressMask|ButtonReleaseMask|ButtonMotionMask); 113 | gc=XCreateGC(dis, win, 0,0); 114 | XSetBackground(dis,gc,white); 115 | XSetForeground(dis,gc,black); 116 | XClearWindow(dis, win); 117 | XMapRaised(dis, win); 118 | 119 | g_X.display = dis; 120 | g_X.screen_num = screen; 121 | g_X.wnd = win; 122 | 123 | if (glx_init(&g_X)){ 124 | printf("glx init failed\n"); 125 | exit(1); 126 | } 127 | if ( glewInit() != GLEW_OK ) { 128 | printf("glew init failed"); 129 | exit(1); 130 | } 131 | }; 132 | 133 | static void 134 | close_x() { 135 | Display *dis = g_X.display; 136 | XFreeGC(dis, gc); 137 | XDestroyWindow(dis, g_X.wnd); 138 | XCloseDisplay(dis); 139 | exit(1); 140 | } 141 | 142 | int 143 | main(int argc, char *argv[]) { 144 | XEvent event; 145 | uint32_t timestamp = 0; 146 | KeySym keysym; 147 | char keychar[255]; 148 | init_x(); 149 | font_init(); 150 | 151 | ejoy2d_win_init(argc, argv); 152 | 153 | for (;;) { 154 | while(XPending(g_X.display) > 0) { 155 | XNextEvent(g_X.display, &event); 156 | if (XFilterEvent(&event,None)) 157 | continue; 158 | switch (event.type) { 159 | case Expose: 160 | if (event.xexpose.count==0) 161 | update_frame(); 162 | break; 163 | case KeyPress: 164 | XLookupString(&event.xkey, keychar, 255, &keysym, 0); 165 | if (keychar[0] == 'q' || keychar[0] == 'Q') { 166 | close_x(); 167 | } 168 | break; 169 | case ButtonPress: 170 | ejoy2d_win_touch(event.xbutton.x, event.xbutton.y, TOUCH_BEGIN); 171 | break; 172 | case ButtonRelease: 173 | ejoy2d_win_touch(event.xbutton.x,event.xbutton.y,TOUCH_END); 174 | break; 175 | case MotionNotify: 176 | ejoy2d_win_touch(event.xbutton.x,event.xbutton.y,TOUCH_MOVE); 177 | break; 178 | case ClientMessage: 179 | if ((Atom)event.xclient.data.l[0] == wm_delete_window) { 180 | close_x(); 181 | } 182 | break; 183 | } 184 | } 185 | 186 | uint32_t current = _gettime(); 187 | if (current - timestamp >= UPDATE_INTERVAL) { 188 | timestamp = current; 189 | ejoy2d_win_update(); 190 | update_frame(); 191 | } else { 192 | usleep(1000); 193 | } 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /posix/winfont.c: -------------------------------------------------------------------------------- 1 | #include "label.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include FT_FREETYPE_H 9 | #include FT_GLYPH_H 10 | 11 | FT_Library library; 12 | 13 | #ifdef __APPLE__ 14 | static const char* TTFONT = "/System/Library/Fonts/STHeiti Light.ttc"; 15 | #else 16 | static const char* TTFONT = "/usr/share/fonts/wenquanyi/wqy-zenhei/wqy-zenhei.ttc"; 17 | #endif 18 | 19 | #define _fault(errcode, msg) __fault(errcode, msg, __FILE__, __LINE__) 20 | 21 | static void __fault(int errcode, const char * msg, const char *file, int line) { 22 | if (errcode) 23 | printf("err(%d): %s, error occured in file %s, line %d\n\n\t have a look at fterrdef.h\n",errcode, msg, file, line); 24 | else 25 | printf("err: %s, occured in file %s, line %d\n", msg, file, line); 26 | exit(1); 27 | } 28 | struct bitmap { 29 | void * buf; 30 | int w; 31 | int h; 32 | int pitch; 33 | }; 34 | 35 | /* x,y is offset of target */ 36 | static void 37 | cpy_bitmap(struct bitmap * target, struct bitmap * src, int x, int y) { 38 | unsigned char * sbuf = src->buf; 39 | unsigned char * tbuf = target->buf; 40 | 41 | int x0,y0,x1,y1; 42 | x0 = x > 0 ? x : 0; 43 | y0 = y > 0 ? y : 0; 44 | x1 = (x + src->w > target->w) ? target->w : (x+src->w); 45 | y1 = (y + src->h > target->h) ? target->h : (y+src->h); 46 | 47 | if (x1 <= x0 || y1 <= y0) 48 | return; 49 | if (x0 >= target->w || y0 >= target->h) 50 | return; 51 | 52 | int w = x1 - x0; 53 | int h = y1 - y0; 54 | 55 | tbuf += y0 * target->pitch + x0; 56 | sbuf += (y0 - y)*src->pitch + x0 - x; 57 | int i,j; 58 | for (i=0;ipitch; 62 | tbuf += target->pitch; 63 | } 64 | } 65 | 66 | void 67 | font_create(int font_size, struct font_context *ctx) { 68 | FT_Face face; 69 | int err = FT_New_Face(library, TTFONT, 0, &face); 70 | if (err) { 71 | if (err == 1) 72 | _fault(err, "set your own vector font resource path"); 73 | else 74 | _fault(err, "new face failed"); 75 | } 76 | 77 | err = FT_Set_Pixel_Sizes(face,0,font_size); 78 | 79 | if (err) 80 | _fault(err, "set char size failed"); 81 | 82 | ctx->font = face; 83 | ctx->ascent = face->size->metrics.ascender >>6; 84 | ctx->h = face->size->metrics.height >> 6; 85 | } 86 | 87 | void 88 | font_release(struct font_context *ctx) { 89 | FT_Done_Face(ctx->font); 90 | } 91 | 92 | void 93 | font_size(const char *str, int unicode, struct font_context *ctx) { 94 | FT_Face face = ctx->font; 95 | FT_UInt glyph_index = FT_Get_Char_Index(face, unicode); 96 | if (!glyph_index) { 97 | ctx->w = 0; 98 | return; 99 | } 100 | FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_BITMAP); 101 | 102 | int err = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL); 103 | if (err) 104 | _fault(err, "render failed"); 105 | 106 | FT_GlyphSlot slot = face->glyph; 107 | 108 | ctx->w = slot->advance.x >> 6; 109 | } 110 | 111 | 112 | void 113 | font_glyph(const char * str, int unicode, void * buffer, struct font_context *ctx) { 114 | FT_Face face = ctx->font; 115 | FT_GlyphSlot slot = face->glyph; 116 | FT_Bitmap *bitmap = &(slot->bitmap); 117 | 118 | int offx = slot->bitmap_left; 119 | int offy = ctx->ascent - slot->bitmap_top; 120 | 121 | struct bitmap src; 122 | struct bitmap target; 123 | src.buf = bitmap->buffer; 124 | src.pitch = bitmap->pitch; 125 | src.w = bitmap->width; 126 | src.h = bitmap->rows; 127 | target.buf = buffer; 128 | target.w = target.pitch = ctx->w; 129 | target.h = ctx->h; 130 | 131 | cpy_bitmap(&target, &src, offx, offy); 132 | } 133 | 134 | void 135 | font_init() { 136 | if (FT_Init_FreeType(&library)) { 137 | printf("font init failed"); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /posix/winfw.c: -------------------------------------------------------------------------------- 1 | #include "opengl.h" 2 | #include "ejoy2dgame.h" 3 | #include "fault.h" 4 | #include "screen.h" 5 | #include "winfw.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | struct WINDOWGAME { 13 | struct game *game; 14 | int intouch; 15 | }; 16 | 17 | static const int BUFSIZE = 2048; 18 | 19 | static struct WINDOWGAME *G = NULL; 20 | 21 | static const char * startscript = 22 | "local path,script = ...\n" 23 | "require(\"ejoy2d.framework\").WorkDir = ''\n" 24 | "assert(script, 'I need a script name')\n" 25 | "path = string.match(path,[[(.*)/[^/]*$]])\n" 26 | "package.path = path .. [[/?.lua;]] .. path .. [[/?/init.lua]]\n" 27 | "local f = assert(loadfile(script))\n" 28 | "f(script)\n" 29 | ; 30 | 31 | static struct WINDOWGAME * 32 | create_game() { 33 | struct WINDOWGAME * g = (struct WINDOWGAME *)malloc(sizeof(*g)); 34 | g->game = ejoy2d_game(); 35 | g->intouch = 0; 36 | return g; 37 | } 38 | 39 | static int 40 | traceback(lua_State *L) { 41 | const char *msg = lua_tostring(L, 1); 42 | if (msg == NULL) { 43 | if (luaL_callmeta(L, 1, "__tostring") && 44 | lua_type(L, -1) == LUA_TSTRING) 45 | return 1; 46 | else 47 | msg = lua_pushfstring(L, "(error object is a %s value)", 48 | luaL_typename(L, 1)); 49 | } 50 | luaL_traceback(L, L, msg, 1); 51 | return 1; 52 | } 53 | 54 | #ifdef __APPLE__ 55 | static const char* 56 | _read_exepath(char * buf, int bufsz) { 57 | return getenv("_"); 58 | } 59 | #define read_exepath(buf,bufsz) _read_exepath(buf,bufsz) 60 | 61 | #else 62 | static const char* 63 | read_exepath(char * buf, int bufsz) { 64 | int count; 65 | count = readlink("/proc/self/exe", buf, bufsz); 66 | 67 | if (count < 0) 68 | return NULL; 69 | return buf; 70 | } 71 | #endif 72 | 73 | 74 | void 75 | ejoy2d_win_init(int argc, char *argv[]) { 76 | G = create_game(); 77 | lua_State *L = ejoy2d_game_lua(G->game); 78 | lua_pushcfunction(L, traceback); 79 | int tb = lua_gettop(L); 80 | 81 | char buf[BUFSIZE]; 82 | const char *pathbuf = read_exepath(buf, BUFSIZE); 83 | if (pathbuf == NULL) 84 | fault("can't read exepath"); 85 | 86 | int err = luaL_loadstring(L, startscript); 87 | if (err) { 88 | const char *msg = lua_tostring(L,-1); 89 | fault("%s", msg); 90 | } 91 | 92 | lua_pushstring(L, pathbuf); 93 | int i; 94 | for (i=1;igame); 108 | } 109 | 110 | void 111 | ejoy2d_win_update() { 112 | ejoy2d_game_update(G->game, 0.01f); 113 | } 114 | 115 | void 116 | ejoy2d_win_frame() { 117 | ejoy2d_game_drawframe(G->game); 118 | } 119 | 120 | void 121 | ejoy2d_win_touch(int x, int y,int touch) { 122 | switch (touch) { 123 | case TOUCH_BEGIN: 124 | G->intouch = 1; 125 | break; 126 | case TOUCH_END: 127 | G->intouch = 0; 128 | break; 129 | case TOUCH_MOVE: 130 | if (!G->intouch) { 131 | return; 132 | } 133 | break; 134 | } 135 | // windows only support one touch id (0) 136 | int id = 0; 137 | ejoy2d_game_touch(G->game, id, x,y,touch); 138 | } 139 | 140 | -------------------------------------------------------------------------------- /posix/winfw.h: -------------------------------------------------------------------------------- 1 | #ifndef ejoy2d_windows_fw_h 2 | #define ejoy2d_windows_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 | void ejoy2d_win_init(); 12 | void ejoy2d_win_frame(); 13 | void ejoy2d_win_update(); 14 | void ejoy2d_win_touch(int x, int y,int touch); 15 | 16 | #endif 17 | --------------------------------------------------------------------------------