From 81738dec8a1bc4a12518ab508e9ef57615c7b4e3 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 13 Dec 2009 20:34:09 +0000 Subject: [PATCH] working on bowling demo -- problem with destroying first instance of a loaded model git-svn-id: svn://anubis/anaglym/trunk@206 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 17 ++++++++++++++ Engine.h | 4 ++++ ag.cc | 26 ++++++++++++++++++++ ag.h | 3 +++ lib/demo/bowling.lua | 56 +++++++++++++++++++++++++++++--------------- lib/std.lua | 8 +++++-- tests/destroy.lua | 8 +++++++ 7 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 tests/destroy.lua diff --git a/Engine.cc b/Engine.cc index 8f45ef0..d6dbdc6 100644 --- a/Engine.cc +++ b/Engine.cc @@ -510,6 +510,23 @@ GLuint Engine::loadTexture(const char * name) return m_textureCache.load(path, *m_fileLoader); } +GLuint Engine::startList() +{ + GLuint list = glGenLists(1); + glNewList(list, GL_COMPILE); + return list; +} + +void Engine::endList() +{ + glEndList(); +} + +void Engine::callList(GLuint list) +{ + glCallList(list); +} + void Engine::debug_hook(lua_Debug * debug) { Uint32 ticks = SDL_GetTicks(); diff --git a/Engine.h b/Engine.h index 62449ea..0b949cc 100644 --- a/Engine.h +++ b/Engine.h @@ -144,6 +144,10 @@ class Engine { m_world.setGravity(gx, gy, gz); } + GLuint startList(); + void endList(); + void drawList(GLuint list); + void callList(GLuint list); void getScreenSize(int * width, int * height); void drawText(const char * text, GLfloat r, GLfloat g, GLfloat b, diff --git a/ag.cc b/ag.cc index 7600882..546f3da 100644 --- a/ag.cc +++ b/ag.cc @@ -21,6 +21,7 @@ namespace ag void register_functions(lua_State * L) { static const luaL_Reg functions[] = { + { "callList", callList }, { "clearEventHandler", clearEventHandler }, { "doPhysics", doPhysics }, { "drawArc", drawArc }, @@ -33,6 +34,7 @@ namespace ag { "drawText", drawText }, { "elapsedTime", elapsedTime }, { "endFrame", endFrame }, + { "endList", endList }, { "exit", exit }, { "fillRect", fillRect }, { "getCamera", getCamera }, @@ -54,6 +56,7 @@ namespace ag { "setGravity", setGravity }, // { "sleep", sleep }, { "startFrame", startFrame }, + { "startList", startList }, /* managed object functions */ { "createBox", createBox }, @@ -753,6 +756,29 @@ namespace ag return createCapsuleSpecify(L, true); } + int startList(lua_State * L) + { + GLuint list = g_engine->startList(); + lua_pushnumber(L, list); + return 1; + } + + int endList(lua_State * L) + { + g_engine->endList(); + return 0; + } + + int callList(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 1 && lua_isnumber(L, 1)) + { + g_engine->callList(lua_tointeger(L, 1)); + } + return 0; + } + namespace object { static Engine::Object * getObject(lua_State * L, int index) diff --git a/ag.h b/ag.h index a150cd2..f17197c 100644 --- a/ag.h +++ b/ag.h @@ -9,11 +9,13 @@ namespace ag void register_functions(lua_State * L); /* Lua interfaces */ + int callList(lua_State * L); int clearEventHandler(lua_State * L); int doPhysics(lua_State * L); int drawObjects(lua_State * L); int elapsedTime(lua_State * L); int endFrame(lua_State * L); + int endList(lua_State * L); int exit(lua_State * L); int getCamera(lua_State * L); int getScreenSize(lua_State * L); @@ -33,6 +35,7 @@ namespace ag int setGravity(lua_State * L); int sleep(lua_State * L); int startFrame(lua_State * L); + int startList(lua_State * L); /* 2D overlay functions */ int drawArc(lua_State * L); diff --git a/lib/demo/bowling.lua b/lib/demo/bowling.lua index a1ecd73..70ae450 100644 --- a/lib/demo/bowling.lua +++ b/lib/demo/bowling.lua @@ -1,41 +1,59 @@ -function setupPins() +ag.import("std") + +function resetPins() + local head_x = 0 + local head_y = 15 + local spacing = 0.7 for i = 1, 10 do if (pins[i] ~= nil) then pins[i]:destroy() end end + local n = 1 + for row = 0, 4 do + for j = 0, row do + pins[n] = reference_pin:clone() + pins[n]:setPosition(head_x - row * spacing / 2 + j * spacing, + head_y + 0.866 * spacing * row, + 1 - pin_minz) + end + end end function setupStars() - stars = {} + stars = ag.startList() local dist = 500 - for i = 1, 1000 do - local star = {} + for i = 1, 500 do local rx = math.random() * math.pi * 2 local ry = (math.random() - 0.5) * math.pi - star.x = dist * math.cos(ry) * math.cos(rx) - star.y = dist * math.cos(ry) * math.sin(rx) - star.z = dist * math.sin(ry) - star.size = math.random(2, 8) - stars[i] = star - end -end - -function drawStars() - for i = 1, #stars do - ag.drawPoint(stars[i].size, 1, 1, 1, stars[i].x, stars[i].y, stars[i].z) + local x = dist * math.cos(ry) * math.cos(rx) + local y = dist * math.cos(ry) * math.sin(rx) + local z = dist * math.sin(ry) + local size = math.random(1, 6) + ag.drawPoint(size, 1, 1, 1, x, y, z) end + ag.endList() end function init_event() - pins = {} lane = ag.loadModelStatic("bowling_lane") - setupPins() + local minx, miny, minz, maxx, maxy, maxz = lane:getAABB() + local lane_bottom = minz + pins = {} + reference_pin = ag.loadModel("bowling_pin", 1.0/4.3) + minx, miny, minz, maxx, maxy, maxz = reference_pin:getAABB() + pin_minz = minz + pin_maxz = maxz + local box = ag.createBoxStatic(1, 1, 0.1) + box:setColor(0, 0, 0) + box:setPosition(0, 0, lane_bottom - (maxz - minz) - 0.4) + reference_pin:setPosition(0, 0, lane_bottom - maxz - 0.1) + resetPins() setupStars() - ag.setCamera(0, -20, 5, 0, 20, 0) + ag.setCamera(0, -10, 5, 0, 20, 0) end function update_event() - drawStars() + ag.callList(stars) end diff --git a/lib/std.lua b/lib/std.lua index 6565578..7bfe0f2 100644 --- a/lib/std.lua +++ b/lib/std.lua @@ -74,19 +74,23 @@ std.loadModelBounds = function(model_name, max_x, max_y, max_z) local sx, sy, sz = tmp_model:getSize() tmp_model:destroy() local scale = 1.0 + local scale_set = false if (max_x > 0 and sx > 0) then scale = max_x / sx + scale_set = true end if (max_y > 0 and sy > 0) then local s = max_y / sy - if (s < scale) then + if (s < scale or not scale_set) then scale = s + scale_set = true end end if (max_z > 0 and sz > 0) then local s = max_z / sz - if (s < scale) then + if (s < scale or not scale_set) then scale = s + scale_set = true end end return ag.loadModel(model_name, scale) diff --git a/tests/destroy.lua b/tests/destroy.lua new file mode 100644 index 0000000..41b48fe --- /dev/null +++ b/tests/destroy.lua @@ -0,0 +1,8 @@ + +ag.setCamera(0, -5, 0) +pin = ag.loadModel("bowling_pin") +pin:setPosition(-1.5, 0, 0) +pin2 = ag.loadModel("bowling_pin") +pin3 = ag.loadModel("bowling_pin") +pin3:setPosition(1.5, 0, 0) +pin:destroy()