added an init event so that initialization can be done inside of it and no code outside of a function is necessary

git-svn-id: svn://anubis/anaglym/trunk@167 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-11-14 14:32:08 +00:00
parent c3a9443c16
commit 6de2b93e3d
7 changed files with 83 additions and 72 deletions

1
.todo
View File

@ -1,3 +1,2 @@
- add functions to draw 2d graphics (lines, rectangles) - add functions to draw 2d graphics (lines, rectangles)
- add an init_event handler and move initialization code there
- add a reload engine key that will "import" the current script - add a reload engine key that will "import" the current script

View File

@ -106,6 +106,7 @@ Engine::Engine(const string & path, Video & video)
m_exitEvent.type = SDL_USEREVENT; m_exitEvent.type = SDL_USEREVENT;
m_exitEvent.user.code = 1; m_exitEvent.user.code = 1;
m_event_init_present = false;
m_event_update_present = false; m_event_update_present = false;
m_event_update_overlay_present = false; m_event_update_overlay_present = false;
m_event_key_down_present = false; m_event_key_down_present = false;
@ -177,6 +178,7 @@ bool Engine::load(const char * program)
return false; return false;
} }
checkForFunction("init", init);
checkForFunction("update_event", update); checkForFunction("update_event", update);
checkForFunction("update_overlay_event", update_overlay); checkForFunction("update_overlay_event", update_overlay);
checkForFunction("key_down_event", key_down); checkForFunction("key_down_event", key_down);
@ -185,6 +187,15 @@ bool Engine::load(const char * program)
checkForFunction("mousebutton_up_event", mousebutton_up); checkForFunction("mousebutton_up_event", mousebutton_up);
checkForFunction("mouse_motion_event", mouse_motion); checkForFunction("mouse_motion_event", mouse_motion);
if (m_event_init_present)
{
lua_getfield(m_luaState, LUA_GLOBALSINDEX,
EVENT_HANDLER_AG_NAME(init));
/* call the init function - pops the function ref from the stack */
int s = lua_pcall(m_luaState, 0, 0, 0);
reportErrors(s);
}
return true; return true;
} }

View File

@ -189,6 +189,7 @@ class Engine
Uint32 m_event_time; Uint32 m_event_time;
FTFont * m_font; FTFont * m_font;
bool m_event_init_present;
bool m_event_update_present; bool m_event_update_present;
bool m_event_update_overlay_present; bool m_event_update_overlay_present;
bool m_event_key_down_present; bool m_event_key_down_present;

View File

@ -1,31 +1,5 @@
t = ag.createSolidTextBox("Hi there", 0, 0, 1) function init()
t2 = ag.createShadedTextBox("Hi there", 0, 1, 1, 0, 0, 0)
t3 = ag.createBlendedTextBox("Hi there", 1, 1, 1)
ag.setAutoEndFrame(false)
ag.setAutoDrawObjects(false)
function update_event()
ballx, bally, ballz = ball:getPosition()
ag.setCamera(7, -6, 15, ballx, bally, ballz, 0, 0, 1)
if (ag.isKeyDown("m")) then
ball:setPosition(-7, 7.4, 12)
end
ag.drawObjects()
ag.endFrame()
t:draw(10, 10)
t2:draw(10, 40)
t3:draw(10, 90)
end
function key_down_event(key)
if (key == "d") then
ball2:destroy()
elseif (key == "p") then
ball:addForce(-10000, 0, 0)
end
end
arena = ag.loadStaticModel("boxarena") arena = ag.loadStaticModel("boxarena")
ball = ag.loadModel("checkerball") ball = ag.loadModel("checkerball")
ball:setPosition(-7, 7.4, 12) ball:setPosition(-7, 7.4, 12)
@ -37,3 +11,20 @@ logo = ag.loadModel("dwlogo", 0.5)
logo:setPosition(-1, 2, 11) logo:setPosition(-1, 2, 11)
crate = ag.loadModel("crate") crate = ag.loadModel("crate")
crate:setPosition(-5, -3, 10) crate:setPosition(-5, -3, 10)
end
function update_event()
ballx, bally, ballz = ball:getPosition()
ag.setCamera(7, -6, 15, ballx, bally, ballz, 0, 0, 1)
if (ag.isKeyDown("m")) then
ball:setPosition(-7, 7.4, 12)
end
end
function key_down_event(key)
if (key == "d") then
ball2:destroy()
elseif (key == "p") then
ball:addForce(-10000, 0, 0)
end
end

View File

@ -1,7 +1,8 @@
levels = 7 function init()
crate = ag.loadModel("crate", 0.5) local levels = 7
offset = 0.5 local crate = ag.loadModel("crate", 0.5)
local offset = 0.5
crate:setPosition(0, 0, offset + levels + 0.5) crate:setPosition(0, 0, offset + levels + 0.5)
for level = 2, levels do for level = 2, levels do
for x = 1, level do for x = 1, level do
@ -11,20 +12,23 @@ for level = 2, levels do
end end
end end
ground = ag.loadStaticModel("crate", 10) local ground = ag.loadStaticModel("crate", 10)
ground:setPosition(0, 0, -10) ground:setPosition(0, 0, -10)
ag.setCamera(levels/2, -2*levels, levels, 0, 0, levels/2, 0, 0, 1) ag.setCamera(levels/2, -2*levels, levels, 0, 0, levels/2, 0, 0, 1)
camx, camy, camz, cx, cy, cz = ag.getCamera() camx, camy, camz, cx, cy, cz = ag.getCamera()
ball = ag.loadModel("checkerball") ball = ag.loadModel("checkerball")
ball:setPosition(2*camx - cx, 2*camy - cy, 2*camz - cz) ball:setPosition(2*camx - cx, 2*camy - cy, 2*camz - cz)
end
function mousebutton_down_event(button) function mousebutton_down_event(button)
local function button1() local function button1()
local newball = ball:clone() local newball = ball:clone()
newball:setPosition(camx, camy, camz) newball:setPosition(camx, camy, camz)
force = 10000 force = 10000
newball:addForce(force * (cx-camx), force * (cy-camy), force * (cz-camz)) newball:addForce(force * (cx-camx),
force * (cy-camy),
force * (cz-camz))
end end
if (button == 1) then if (button == 1) then
button1() button1()

View File

@ -1,8 +1,9 @@
function init()
crates = {} crates = {}
levels = 5 levels = 5
crate = ag.loadModel("crate", 0.5) local crate = ag.loadModel("crate", 0.5)
crates[1] = crate crates[1] = crate
offset = 0.5 offset = 0.5
crate:setPosition(0, 0, offset + levels + 0.5) crate:setPosition(0, 0, offset + levels + 0.5)
@ -20,6 +21,7 @@ end
ground = ag.loadStaticModel("crate", 10) ground = ag.loadStaticModel("crate", 10)
ground:setPosition(0, 0, -10) ground:setPosition(0, 0, -10)
ag.setCamera(2, -12, 8, 0, 0, 2, 0, 0, 1) ag.setCamera(2, -12, 8, 0, 0, 2, 0, 0, 1)
end
function key_down_event(key) function key_down_event(key)
if (key == "d") then if (key == "d") then

View File

@ -1,6 +1,7 @@
crate_texture = ag.loadTexture("checker.jpg") function init()
ground = ag.createStaticPlane(0, 0, 1, 0) local crate_texture = ag.loadTexture("checker.jpg")
local ground = ag.createStaticPlane(0, 0, 1, 0)
ground:setColor(0.2, 1.0, 0.2) ground:setColor(0.2, 1.0, 0.2)
ground:setTexture(crate_texture) ground:setTexture(crate_texture)
ag.setCamera(10, -10, 10, 0, 0, 0) ag.setCamera(10, -10, 10, 0, 0, 0)
@ -9,8 +10,9 @@ if (ag.import("rot_camera") ~= true) then
ag.println("error importing rot_camera") ag.println("error importing rot_camera")
ag.exit() ag.exit()
end end
end
function init(obj) function init_obj(obj)
obj:setColor(math.random(), math.random(), math.random()) obj:setColor(math.random(), math.random(), math.random())
obj:setPosition((math.random() - 0.5) * 10, (math.random() - 0.5) * 10, 10) obj:setPosition((math.random() - 0.5) * 10, (math.random() - 0.5) * 10, 10)
end end
@ -22,16 +24,17 @@ end
function key_down_event(key) function key_down_event(key)
if (key == "b") then if (key == "b") then
local box = ag.createBox(1, 1, 1) local box = ag.createBox(1, 1, 1)
init(box) init_obj(box)
elseif (key == "c") then elseif (key == "c") then
-- cylinders are buggy in ODE
local cyl = ag.createCylinder(0.5, 1) local cyl = ag.createCylinder(0.5, 1)
init(cyl) init_obj(cyl)
elseif (key == "a") then elseif (key == "a") then
local ccyl = ag.createCapsule(0.5, 1) local ccyl = ag.createCapsule(0.5, 1)
init(ccyl) init_obj(ccyl)
elseif (key == "s") then elseif (key == "s") then
local sphere = ag.createSphere(0.8) local sphere = ag.createSphere(0.8)
init(sphere) init_obj(sphere)
elseif (key == "q") then elseif (key == "q") then
ag.exit() ag.exit()
end end