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 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
|
||||||
|
11
Engine.cc
11
Engine.cc
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
Engine.h
1
Engine.h
@ -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;
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
|
|
||||||
t = ag.createSolidTextBox("Hi there", 0, 0, 1)
|
function init()
|
||||||
t2 = ag.createShadedTextBox("Hi there", 0, 1, 1, 0, 0, 0)
|
arena = ag.loadStaticModel("boxarena")
|
||||||
t3 = ag.createBlendedTextBox("Hi there", 1, 1, 1)
|
ball = ag.loadModel("checkerball")
|
||||||
ag.setAutoEndFrame(false)
|
ball:setPosition(-7, 7.4, 12)
|
||||||
ag.setAutoDrawObjects(false)
|
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()
|
function update_event()
|
||||||
ballx, bally, ballz = ball:getPosition()
|
ballx, bally, ballz = ball:getPosition()
|
||||||
@ -11,11 +19,6 @@ function update_event()
|
|||||||
if (ag.isKeyDown("m")) then
|
if (ag.isKeyDown("m")) then
|
||||||
ball:setPosition(-7, 7.4, 12)
|
ball:setPosition(-7, 7.4, 12)
|
||||||
end
|
end
|
||||||
ag.drawObjects()
|
|
||||||
ag.endFrame()
|
|
||||||
t:draw(10, 10)
|
|
||||||
t2:draw(10, 40)
|
|
||||||
t3:draw(10, 90)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function key_down_event(key)
|
function key_down_event(key)
|
||||||
@ -25,15 +28,3 @@ function key_down_event(key)
|
|||||||
ball:addForce(-10000, 0, 0)
|
ball:addForce(-10000, 0, 0)
|
||||||
end
|
end
|
||||||
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)
|
|
||||||
|
@ -1,30 +1,34 @@
|
|||||||
|
|
||||||
levels = 7
|
function init()
|
||||||
crate = ag.loadModel("crate", 0.5)
|
local levels = 7
|
||||||
offset = 0.5
|
local crate = ag.loadModel("crate", 0.5)
|
||||||
crate:setPosition(0, 0, offset + levels + 0.5)
|
local offset = 0.5
|
||||||
for level = 2, levels do
|
crate:setPosition(0, 0, offset + levels + 0.5)
|
||||||
for x = 1, level do
|
for level = 2, levels do
|
||||||
local c = crate:clone()
|
for x = 1, level do
|
||||||
c:setPosition(x - 0.5 - level/2,
|
local c = crate:clone()
|
||||||
0, offset + levels - level + 1.5)
|
c:setPosition(x - 0.5 - level/2,
|
||||||
|
0, offset + levels - level + 1.5)
|
||||||
|
end
|
||||||
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
|
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)
|
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()
|
||||||
|
@ -1,25 +1,27 @@
|
|||||||
|
|
||||||
crates = {}
|
function init()
|
||||||
|
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)
|
||||||
cratenum = 1
|
cratenum = 1
|
||||||
for level = 2, levels do
|
for level = 2, levels do
|
||||||
for x = 1, level do
|
for x = 1, level do
|
||||||
local c = crate:clone()
|
local c = crate:clone()
|
||||||
c:setPosition((x - 1) * 1.5 - (level * 1.5 - 0.5)/2 + 0.5,
|
c:setPosition((x - 1) * 1.5 - (level * 1.5 - 0.5)/2 + 0.5,
|
||||||
0, offset + levels - level + 1.5)
|
0, offset + levels - level + 1.5)
|
||||||
cratenum = cratenum + 1
|
cratenum = cratenum + 1
|
||||||
crates[cratenum] = c
|
crates[cratenum] = c
|
||||||
|
end
|
||||||
end
|
end
|
||||||
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
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
|
|
||||||
crate_texture = ag.loadTexture("checker.jpg")
|
function init()
|
||||||
ground = ag.createStaticPlane(0, 0, 1, 0)
|
local crate_texture = ag.loadTexture("checker.jpg")
|
||||||
ground:setColor(0.2, 1.0, 0.2)
|
local ground = ag.createStaticPlane(0, 0, 1, 0)
|
||||||
ground:setTexture(crate_texture)
|
ground:setColor(0.2, 1.0, 0.2)
|
||||||
ag.setCamera(10, -10, 10, 0, 0, 0)
|
ground:setTexture(crate_texture)
|
||||||
|
ag.setCamera(10, -10, 10, 0, 0, 0)
|
||||||
|
|
||||||
if (ag.import("rot_camera") ~= true) then
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user