From 6de2b93e3d68ae2a55332ee9f0b6d6ef3b15ebe9 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 14 Nov 2009 14:32:08 +0000 Subject: [PATCH] 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 --- .todo | 1 - Engine.cc | 11 +++++++++++ Engine.h | 1 + tests/ballstairs.lua | 35 +++++++++++++--------------------- tests/cannon.lua | 40 +++++++++++++++++++++------------------ tests/cratestack.lua | 38 +++++++++++++++++++------------------ tests/managed_objects.lua | 29 +++++++++++++++------------- 7 files changed, 83 insertions(+), 72 deletions(-) diff --git a/.todo b/.todo index 9d4f812..8e81950 100644 --- a/.todo +++ b/.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 diff --git a/Engine.cc b/Engine.cc index 8c62c75..866ee50 100644 --- a/Engine.cc +++ b/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; } diff --git a/Engine.h b/Engine.h index 42512af..3ff3677 100644 --- a/Engine.h +++ b/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; diff --git a/tests/ballstairs.lua b/tests/ballstairs.lua index 98014b9..6486759 100755 --- a/tests/ballstairs.lua +++ b/tests/ballstairs.lua @@ -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) diff --git a/tests/cannon.lua b/tests/cannon.lua index a3dbf44..9f8a02b 100644 --- a/tests/cannon.lua +++ b/tests/cannon.lua @@ -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() diff --git a/tests/cratestack.lua b/tests/cratestack.lua index 0aa7ea3..cb9c323 100644 --- a/tests/cratestack.lua +++ b/tests/cratestack.lua @@ -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 diff --git a/tests/managed_objects.lua b/tests/managed_objects.lua index 0f5b846..b7ce070 100644 --- a/tests/managed_objects.lua +++ b/tests/managed_objects.lua @@ -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