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 an init_event handler and move initialization code there
- 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.user.code = 1;
m_event_init_present = false;
m_event_update_present = false;
m_event_update_overlay_present = false;
m_event_key_down_present = false;
@ -177,6 +178,7 @@ bool Engine::load(const char * program)
return false;
}
checkForFunction("init", init);
checkForFunction("update_event", update);
checkForFunction("update_overlay_event", update_overlay);
checkForFunction("key_down_event", key_down);
@ -185,6 +187,15 @@ bool Engine::load(const char * program)
checkForFunction("mousebutton_up_event", mousebutton_up);
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;
}

View File

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

View File

@ -1,9 +1,17 @@
t = ag.createSolidTextBox("Hi there", 0, 0, 1)
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 init()
arena = ag.loadStaticModel("boxarena")
ball = ag.loadModel("checkerball")
ball:setPosition(-7, 7.4, 12)
ball2 = ball:clone()
ball2:setPosition(-7, 7.4, 14)
ball3 = ball:clone()
ball3:setPosition(-7, 7.4, 16)
logo = ag.loadModel("dwlogo", 0.5)
logo:setPosition(-1, 2, 11)
crate = ag.loadModel("crate")
crate:setPosition(-5, -3, 10)
end
function update_event()
ballx, bally, ballz = ball:getPosition()
@ -11,11 +19,6 @@ function update_event()
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)
@ -25,15 +28,3 @@ function key_down_event(key)
ball:addForce(-10000, 0, 0)
end
end
arena = ag.loadStaticModel("boxarena")
ball = ag.loadModel("checkerball")
ball:setPosition(-7, 7.4, 12)
ball2 = ball:clone()
ball2:setPosition(-7, 7.4, 14)
ball3 = ball:clone()
ball3:setPosition(-7, 7.4, 16)
logo = ag.loadModel("dwlogo", 0.5)
logo:setPosition(-1, 2, 11)
crate = ag.loadModel("crate")
crate:setPosition(-5, -3, 10)

View File

@ -1,30 +1,34 @@
levels = 7
crate = ag.loadModel("crate", 0.5)
offset = 0.5
crate:setPosition(0, 0, offset + levels + 0.5)
for level = 2, levels do
for x = 1, level do
local c = crate:clone()
c:setPosition(x - 0.5 - level/2,
0, offset + levels - level + 1.5)
function init()
local levels = 7
local crate = ag.loadModel("crate", 0.5)
local offset = 0.5
crate:setPosition(0, 0, offset + levels + 0.5)
for level = 2, levels do
for x = 1, level do
local c = crate:clone()
c:setPosition(x - 0.5 - level/2,
0, offset + levels - level + 1.5)
end
end
local ground = ag.loadStaticModel("crate", 10)
ground:setPosition(0, 0, -10)
ag.setCamera(levels/2, -2*levels, levels, 0, 0, levels/2, 0, 0, 1)
camx, camy, camz, cx, cy, cz = ag.getCamera()
ball = ag.loadModel("checkerball")
ball:setPosition(2*camx - cx, 2*camy - cy, 2*camz - cz)
end
ground = ag.loadStaticModel("crate", 10)
ground:setPosition(0, 0, -10)
ag.setCamera(levels/2, -2*levels, levels, 0, 0, levels/2, 0, 0, 1)
camx, camy, camz, cx, cy, cz = ag.getCamera()
ball = ag.loadModel("checkerball")
ball:setPosition(2*camx - cx, 2*camy - cy, 2*camz - cz)
function mousebutton_down_event(button)
local function button1()
local newball = ball:clone()
newball:setPosition(camx, camy, camz)
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
if (button == 1) then
button1()

View File

@ -1,25 +1,27 @@
crates = {}
function init()
crates = {}
levels = 5
crate = ag.loadModel("crate", 0.5)
crates[1] = crate
offset = 0.5
crate:setPosition(0, 0, offset + levels + 0.5)
cratenum = 1
for level = 2, levels do
for x = 1, level do
local c = crate:clone()
c:setPosition((x - 1) * 1.5 - (level * 1.5 - 0.5)/2 + 0.5,
0, offset + levels - level + 1.5)
cratenum = cratenum + 1
crates[cratenum] = c
levels = 5
local crate = ag.loadModel("crate", 0.5)
crates[1] = crate
offset = 0.5
crate:setPosition(0, 0, offset + levels + 0.5)
cratenum = 1
for level = 2, levels do
for x = 1, level do
local c = crate:clone()
c:setPosition((x - 1) * 1.5 - (level * 1.5 - 0.5)/2 + 0.5,
0, offset + levels - level + 1.5)
cratenum = cratenum + 1
crates[cratenum] = c
end
end
end
ground = ag.loadStaticModel("crate", 10)
ground:setPosition(0, 0, -10)
ag.setCamera(2, -12, 8, 0, 0, 2, 0, 0, 1)
ground = ag.loadStaticModel("crate", 10)
ground:setPosition(0, 0, -10)
ag.setCamera(2, -12, 8, 0, 0, 2, 0, 0, 1)
end
function key_down_event(key)
if (key == "d") then

View File

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