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:
parent
c3a9443c16
commit
6de2b93e3d
1
.todo
1
.todo
@ -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
|
||||
|
11
Engine.cc
11
Engine.cc
@ -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;
|
||||
}
|
||||
|
||||
|
1
Engine.h
1
Engine.h
@ -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;
|
||||
|
@ -1,31 +1,5 @@
|
||||
|
||||
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 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
|
||||
|
||||
function init()
|
||||
arena = ag.loadStaticModel("boxarena")
|
||||
ball = ag.loadModel("checkerball")
|
||||
ball:setPosition(-7, 7.4, 12)
|
||||
@ -37,3 +11,20 @@ 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()
|
||||
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
|
||||
|
@ -1,7 +1,8 @@
|
||||
|
||||
levels = 7
|
||||
crate = ag.loadModel("crate", 0.5)
|
||||
offset = 0.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
|
||||
@ -11,20 +12,23 @@ for level = 2, levels do
|
||||
end
|
||||
end
|
||||
|
||||
ground = ag.loadStaticModel("crate", 10)
|
||||
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
|
||||
|
||||
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()
|
||||
|
@ -1,8 +1,9 @@
|
||||
|
||||
function init()
|
||||
crates = {}
|
||||
|
||||
levels = 5
|
||||
crate = ag.loadModel("crate", 0.5)
|
||||
local crate = ag.loadModel("crate", 0.5)
|
||||
crates[1] = crate
|
||||
offset = 0.5
|
||||
crate:setPosition(0, 0, offset + levels + 0.5)
|
||||
@ -20,6 +21,7 @@ end
|
||||
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
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
crate_texture = ag.loadTexture("checker.jpg")
|
||||
ground = ag.createStaticPlane(0, 0, 1, 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)
|
||||
@ -9,8 +10,9 @@ 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user