#include "ag.h" #include "anaglym.h" #include "Engine.h" #include "Video.h" #include "sdl_keymap.h" #include #include /* exit() */ #include #include #include #include #include #include #include #include /* fabs() */ #include #include using namespace std; #define AG_EVENT_PREFIX "__ag_event_" #define EVENT_PRESENT_FLAG(event) m_event_ ## event ## _present #define EVENT_HANDLER_AG_NAME(event) AG_EVENT_PREFIX #event #define checkForFunction(lua_name, event) \ checkForFunctionFull(lua_name, #event, EVENT_PRESENT_FLAG(event)) #define doRegisterHandler(index, event) \ doRegisterHandlerFull(index, EVENT_HANDLER_AG_NAME(event), \ EVENT_PRESENT_FLAG(event)) #define doClearHandler(event) \ do { EVENT_PRESENT_FLAG(event) = false; } while(0) #define WHITESPACE " \t\r\n\f" /* Global data */ Engine * g_engine; SDL_Event Engine::userEvent; /* static helper functions */ static string trim(const string & orig) { string result = orig; size_t pos = result.find_first_not_of(WHITESPACE); if (pos == string::npos) { result = ""; } else { if (pos > 0) result = result.substr(pos, result.length() - pos); pos = result.find_last_not_of(WHITESPACE); if (pos < result.length() - 1) result = result.substr(0, pos + 1); } return result; } /******** Engine functions ********/ Engine::Engine(const string & path, Video & video) : m_video(video) { m_next_object_index = 1; m_eye[0] = 0; m_eye[1] = -1; m_eye[2] = 0; m_center[0] = 0; m_center[1] = 0; m_center[2] = 0; m_up[0] = 0; m_up[1] = 0; m_up[2] = 1; m_drawing = false; m_autoPhysics = true; m_autoStartFrame = true; m_autoEndFrame = true; m_autoDrawObjects = true; m_fileLoader = new EngineFileLoader(this); size_t pos = path.find_last_of("\\/"); m_engine_path = (pos != string::npos) ? string(path, 0, pos) : "."; /* setup redraw SDL event structure */ userEvent.type = SDL_USEREVENT; userEvent.user.code = 0; m_event_update_present = false; m_event_key_down_present = false; m_event_key_up_present = false; m_event_mousebutton_down_present = false; m_event_mousebutton_up_present = false; m_event_mouse_motion_present = false; } Engine::~Engine() { lua_close(m_luaState); for (std::map::iterator it = m_objects.begin(); it != m_objects.end(); it++) { delete it->second; } delete m_fileLoader; } bool Engine::load(const char * program) { m_program_path = program; size_t pos = m_program_path.find_last_of("/\\"); m_program_path = (pos != string::npos) ? m_program_path.substr(0, pos) : "."; m_luaState = lua_open(); registerLibraries(); int s = luaL_loadfile(m_luaState, program); if (s == 0) { // execute Lua program s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0); } else { reportErrors(s); cerr << "Error loading '" << program << "'" << endl; cerr << "Exiting." << endl; return false; } if (s != 0) { reportErrors(s); return false; } checkForFunction("update_event", update); checkForFunction("key_down_event", key_down); checkForFunction("key_up_event", key_up); checkForFunction("mousebutton_down_event", mousebutton_down); checkForFunction("mousebutton_up_event", mousebutton_up); checkForFunction("mouse_motion_event", mouse_motion); return true; } void Engine::reportErrors(int status) { if (status != 0) { cerr << "Engine: Error: " << lua_tostring(m_luaState, -1) << endl; lua_pop(m_luaState, 1); // remove error message } } void Engine::registerLibraries() { /* Load the Lua string library */ lua_pushcfunction(m_luaState, luaopen_string); lua_pcall(m_luaState, 0, 0, 0); /* Load the Lua math library */ lua_pushcfunction(m_luaState, luaopen_math); lua_pcall(m_luaState, 0, 0, 0); /* Load the Lua table library */ lua_pushcfunction(m_luaState, luaopen_table); lua_pcall(m_luaState, 0, 0, 0); ag::register_functions(m_luaState); } string Engine::locateResource(const string & shortname) { string try_path; /* look for the resource relative to the loaded script's directory */ try_path = m_program_path + "/" + shortname; if (fileExists(try_path)) return try_path; /* next look for the resource in the engine's library directory */ try_path = m_engine_path + "/lib/" + shortname; if (fileExists(try_path)) return try_path; return ""; } bool Engine::fileExists(const string & path) { struct stat st; if (stat(path.c_str(), &st) == 0) { return S_ISREG(st.st_mode) && (st.st_size > 0); } return false; } int Engine::addObject(WFObj * obj, bool is_static, float scale) { int id = m_next_object_index++; Object * o = createObject(is_static, obj->render(), scale); m_objects[id] = o; return id; } void Engine::removeObject(int id) { Object * obj = getObject(id); if (obj != NULL) { m_objects.erase(id); delete obj; } } int Engine::cloneObject(const Engine::Object * obj) { int id = m_next_object_index++; Object * o = new Object(*obj); m_objects[id] = o; return id; } Engine::Object * Engine::getObject(int id) { return m_objects.find(id) != m_objects.end() ? m_objects[id] : NULL; } void Engine::startFrame() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(m_eye[0], m_eye[1], m_eye[2], m_center[0], m_center[1], m_center[2], m_up[0], m_up[1], m_up[2]); } void Engine::endFrame() { SDL_GL_SwapBuffers(); } int Engine::setCamera(lua_State * L) { int argc = lua_gettop(L); vector args; for (int i = 1; i <= argc; i++) { int type = lua_type(L, i); if (type == LUA_TNUMBER || type == LUA_TSTRING) args.push_back(lua_tonumber(L, i)); else args.push_back(0); } if (argc >= 3) { m_eye[0] = args[0]; m_eye[1] = args[1]; m_eye[2] = args[2]; } if (argc >= 6) { m_center[0] = args[3]; m_center[1] = args[4]; m_center[2] = args[5]; } if (argc >= 9) { m_up[0] = args[6]; m_up[1] = args[7]; m_up[2] = args[8]; } return 0; } int Engine::getCamera(lua_State * L) { lua_pushnumber(L, m_eye[0]); lua_pushnumber(L, m_eye[1]); lua_pushnumber(L, m_eye[2]); lua_pushnumber(L, m_center[0]); lua_pushnumber(L, m_center[1]); lua_pushnumber(L, m_center[2]); lua_pushnumber(L, m_up[0]); lua_pushnumber(L, m_up[1]); lua_pushnumber(L, m_up[2]); return 9; } int Engine::registerEventHandler(lua_State * L) { int argc = lua_gettop(L); if (argc == 2 && lua_isfunction(L, 1) && lua_isstring(L, 2)) { string event = lua_tostring(L, 2); if (event == "update") doRegisterHandler(1, update); else if (event == "key_down") doRegisterHandler(1, key_down); else if (event == "key_up") doRegisterHandler(1, key_up); else if (event == "mousebutton_down") doRegisterHandler(1, mousebutton_down); else if (event == "mousebutton_up") doRegisterHandler(1, mousebutton_up); else if (event == "mouse_motion") doRegisterHandler(1, mouse_motion); } return 0; } int Engine::clearEventHandler(lua_State * L) { int argc = lua_gettop(L); if (argc == 1 && lua_isstring(L, 1)) { string event = lua_tostring(L, 1); if (event == "update") doClearHandler(update); else if (event == "key_down") doClearHandler(key_down); else if (event == "key_up") doClearHandler(key_up); else if (event == "mousebutton_down") doClearHandler(mousebutton_down); else if (event == "mousebutton_up") doClearHandler(mousebutton_up); else if (event == "mouse_motion") doClearHandler(mouse_motion); } return 0; } int Engine::loadModel(const string & name, bool static_data, float scale) { size_t pos = name.find_first_not_of(FILENAME_SAFE_CHARS); if (pos == string::npos) { FileLoader::Path model_path("", name + ".obj"); FileLoader::Path phys_path("", name + ".phy"); WFObj * obj = new WFObj(*m_fileLoader, m_textureCache); if (obj->load(model_path)) { int id = addObject(obj, static_data, scale); Engine::Object * engine_obj = getObject(id); if (engine_obj != NULL) { engine_obj->loadPhy(phys_path); } return id; } else { delete obj; cerr << "error loading object" << endl; } } return 0; } bool Engine::isKeyDown(const std::string & key) { return m_keysDown.find(key) != m_keysDown.end(); } /* called by SDL when the update timer expires */ Uint32 Engine::updateCallback(Uint32 interval, void * param) { Engine * engine = (Engine *) param; return engine->updateCallback(interval); } /* member update function to be called by our registered * SDL callback non-member function */ Uint32 Engine::updateCallback(Uint32 interval) { if (!m_drawing) { SDL_PushEvent(&userEvent); } return interval; } void Engine::run() { /* register a screen redrawing SDL event */ SDL_AddTimer(25, &updateCallback, this); SDL_Event event; while (SDL_WaitEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: goto RET; case SDLK_F1: m_video.toggleFullScreen(); break; case SDLK_F2: SDL_ShowCursor( SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE ? SDL_DISABLE : SDL_ENABLE); SDL_WM_GrabInput( SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON ? SDL_GRAB_OFF : SDL_GRAB_ON); break; default: key_down_event(event.key.keysym.sym); } break; case SDL_KEYUP: key_up_event(event.key.keysym.sym); break; case SDL_MOUSEBUTTONDOWN: mousebutton_down_event(event.button.button, event.button.x, event.button.y); break; case SDL_MOUSEBUTTONUP: mousebutton_up_event(event.button.button, event.button.x, event.button.y); break; case SDL_MOUSEMOTION: mouse_motion_event(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel); break; case SDL_QUIT: goto RET; break; case SDL_USEREVENT: if (event.user.code == 0) { update_event(); } break; } } RET: ; } void Engine::update_event() { m_drawing = true; if (m_autoPhysics) doPhysics(); if (m_autoStartFrame) startFrame(); if (m_event_update_present) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, EVENT_HANDLER_AG_NAME(update)); /* call the update function - pops the function ref from the stack */ int s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0); reportErrors(s); } if (m_autoDrawObjects) drawObjects(); if (m_autoEndFrame) endFrame(); m_drawing = false; } void Engine::key_down_event(int keysym) { m_keysDown[sdl_keymap[keysym]] = true; if (m_event_key_down_present) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, EVENT_HANDLER_AG_NAME(key_down)); lua_pushstring(m_luaState, sdl_keymap[keysym]); /* call the key down event function * This pops the function ref and arguments from the stack */ int s = lua_pcall(m_luaState, 1, LUA_MULTRET, 0); reportErrors(s); } } void Engine::key_up_event(int keysym) { m_keysDown.erase(sdl_keymap[keysym]); if (m_event_key_up_present) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, EVENT_HANDLER_AG_NAME(key_up)); lua_pushstring(m_luaState, sdl_keymap[keysym]); /* call the key up event function * This pops the function ref and arguments from the stack */ int s = lua_pcall(m_luaState, 1, LUA_MULTRET, 0); reportErrors(s); } } void Engine::mousebutton_down_event(int button, int x, int y) { if (m_event_mousebutton_down_present) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, EVENT_HANDLER_AG_NAME(mousebutton_down)); lua_pushinteger(m_luaState, button); lua_pushnumber(m_luaState, x); lua_pushnumber(m_luaState, y); /* call the mouse button down event function * This pops the function ref and arguments from the stack */ int s = lua_pcall(m_luaState, 3, LUA_MULTRET, 0); reportErrors(s); } } void Engine::mousebutton_up_event(int button, int x, int y) { if (m_event_mousebutton_up_present) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, EVENT_HANDLER_AG_NAME(mousebutton_up)); lua_pushinteger(m_luaState, button); lua_pushnumber(m_luaState, x); lua_pushnumber(m_luaState, y); /* call the mouse button up event function * This pops the function ref and arguments from the stack */ int s = lua_pcall(m_luaState, 3, LUA_MULTRET, 0); reportErrors(s); } } void Engine::mouse_motion_event(int x, int y, int xrel, int yrel) { if (m_event_mouse_motion_present) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, EVENT_HANDLER_AG_NAME(mouse_motion)); lua_pushnumber(m_luaState, x); lua_pushnumber(m_luaState, y); lua_pushnumber(m_luaState, xrel); lua_pushnumber(m_luaState, yrel); /* call the mouse motion event function * This pops the function ref and arguments from the stack */ int s = lua_pcall(m_luaState, 4, LUA_MULTRET, 0); reportErrors(s); } } void Engine::checkForFunctionFull(const std::string & lua_fn_name, const std::string & event_name, bool & presentFlag) { lua_getfield(m_luaState, LUA_GLOBALSINDEX, lua_fn_name.c_str()); if (lua_isfunction(m_luaState, -1)) { lua_setfield(m_luaState, LUA_GLOBALSINDEX, (AG_EVENT_PREFIX + event_name).c_str()); presentFlag = true; } else { lua_pop(m_luaState, 1); presentFlag = false; } } void Engine::doRegisterHandlerFull(int index, const std::string & event_name, bool & presentFlag) { lua_pushvalue(m_luaState, index); lua_setfield(m_luaState, LUA_GLOBALSINDEX, event_name.c_str()); presentFlag = true; } void Engine::doPhysics() { static Uint32 last_updated = 0; Uint32 current_ticks = SDL_GetTicks(); if (last_updated > 0) { Uint32 msec_steps = current_ticks - last_updated; for (Uint32 i = 0; i < msec_steps; i++) m_world.step(); } last_updated = current_ticks; } void Engine::drawObjects() { std::map::iterator it; for (it = m_objects.begin(); it != m_objects.end(); it++) { it->second->draw(); } } /******** Engine::Object functions ********/ Engine::Object::Object(bool is_static, OdeWorld & world, GLuint dl, float scale) { m_ode_object = world.createObject(is_static, scale); m_display_list = dl; m_display_list_refcnt = new int; *m_display_list_refcnt = 1; m_is_visible = true; m_scale = scale; m_is_scaled = ! (fabs(scale - 1.0) < 0.0001); m_is_managed = false; } Engine::Object::Object(bool is_static, OdeWorld & world, OdeWorld::GeomType geom_type, refptr< std::vector > args) { m_is_visible = true; m_scale = 1.0f; m_is_scaled = false; m_display_list = -1; m_display_list_refcnt = NULL; m_ode_object = world.createObject( geom_type == OdeWorld::PLANE ? false : is_static, m_scale); m_is_managed = true; m_geom_type = geom_type; m_args = args; for (int i = 0; i < 4; i++) m_color[i] = 1.0f; createManagedObject(); } Engine::Object::Object(const Engine::Object & orig) { m_is_visible = orig.m_is_visible; m_ode_object = new OdeWorld::Object(*orig.m_ode_object); m_display_list = orig.m_display_list; m_display_list_refcnt = orig.m_display_list_refcnt; if (m_display_list_refcnt != NULL) (*m_display_list_refcnt)++; m_scale = orig.m_scale; m_is_scaled = orig.m_is_scaled; m_is_managed = orig.m_is_managed; m_geom_type = orig.m_geom_type; m_args = orig.m_args; for (int i = 0; i < 4; i++) m_color[i] = orig.m_color[i]; } Engine::Object::~Object() { delete m_ode_object; (*m_display_list_refcnt)--; if (*m_display_list_refcnt < 1) { /* we hold the last reference to the OpenGL display list */ delete m_display_list_refcnt; glDeleteLists(m_display_list, 1); } } void Engine::Object::createManagedObject() { switch (m_geom_type) { case OdeWorld::CUBE: m_ode_object->addCube(m_args); break; case OdeWorld::SPHERE: m_ode_object->addSphere(m_args); break; case OdeWorld::PLANE: m_ode_object->addPlane(m_args); break; case OdeWorld::CYLINDER: m_ode_object->addCylinder(m_args); break; case OdeWorld::CCYLINDER: m_ode_object->addCCylinder(m_args); break; } m_ode_object->finalize(); render(); } /* render a managed object */ void Engine::Object::render() { bool valid = false; switch (m_geom_type) { case OdeWorld::CUBE: if (m_args->size() >= 3) valid = true; break; case OdeWorld::SPHERE: if (m_args->size() == 4) valid = true; break; case OdeWorld::PLANE: if (m_args->size() == 6) valid = true; break; case OdeWorld::CYLINDER: if (m_args->size() == 8) valid = true; break; case OdeWorld::CCYLINDER: if (m_args->size() == 8) valid = true; break; } if (!valid) return; m_display_list = glGenLists(1); glNewList(m_display_list, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, m_color); GLUquadric * quad = gluNewQuadric(); /* TODO: finish */ switch (m_geom_type) { case OdeWorld::CUBE: break; case OdeWorld::SPHERE: gluSphere(quad, (*m_args)[0], 16, 8); break; case OdeWorld::PLANE: break; case OdeWorld::CYLINDER: glPushMatrix(); glTranslatef(0, 0, -(*m_args)[1] / 2.0); gluCylinder(quad, (*m_args)[0], (*m_args)[0], (*m_args)[1], 12, 1); glPopMatrix(); break; case OdeWorld::CCYLINDER: { glPushAttrib(GL_TRANSFORM_BIT); /* save current clip planes */ double halfheight = (*m_args)[1] / 2.0; glPushMatrix(); glTranslatef(0, 0, -halfheight); gluCylinder(quad, (*m_args)[0], (*m_args)[0], (*m_args)[1], 12, 1); double clip_plane[] = {0, 0, -1, halfheight}; glClipPlane(GL_CLIP_PLANE0, clip_plane); glEnable(GL_CLIP_PLANE0); gluSphere(quad, (*m_args)[0], 12, 8); glTranslatef(0, 0, 2*halfheight); clip_plane[2] = 1.0; glClipPlane(GL_CLIP_PLANE0, clip_plane); gluSphere(quad, (*m_args)[0], 12, 8); glPopMatrix(); glPopAttrib(); } break; } gluDeleteQuadric(quad); glEndList(); } void Engine::Object::loadPhy(const FileLoader::Path & path) { FileLoader::Buffer buff = g_engine->m_fileLoader->load(path); if (buff.size <= 0) return; string str(buff.data, buff.size); stringstream istr(str, ios_base::in); while (!istr.eof()) { string line; getline(istr, line); line = trim(line); if (line == "" || line[0] == '#') continue; size_t pos = line.find_first_of(WHITESPACE); if (pos == string::npos) continue; string type = line.substr(0, pos); pos = line.find("\"", pos); if (pos == string::npos) continue; size_t pos2 = line.find("\"", pos + 1); if (pos2 == string::npos) continue; string name = line.substr(pos + 1, pos2 - pos - 1); pos = pos2 + 1; refptr< vector > args = new vector(); for (;;) { pos = line.find_first_not_of(WHITESPACE, pos); if (pos == string::npos) break; pos2 = line.find_first_of(WHITESPACE, pos); string n = line.substr(pos, pos2 - pos); float f = atof(n.c_str()); args->push_back(f); if (pos2 == string::npos) break; pos = pos2 + 1; } if (type == "cube") { m_ode_object->addCube(args); } else if (type == "sphere") { m_ode_object->addSphere(args); } else if (type == "cylinder") { m_ode_object->addCylinder(args); } else if (type == "plane") { m_ode_object->addPlane(args); } } m_ode_object->finalize(); } void Engine::Object::draw() { if (m_is_visible) { const dReal * pos = m_ode_object->getPosition(); const dReal * rot = m_ode_object->getRotation(); bool transform = (pos != NULL && rot != NULL); if (transform) OdeWorld::pushTransform(pos, rot); if (m_is_scaled) { glPushAttrib(GL_TRANSFORM_BIT); glEnable(GL_NORMALIZE); glScalef(m_scale, m_scale, m_scale); } glCallList(m_display_list); if (m_is_scaled) glPopAttrib(); if (transform) glPopMatrix(); } } /******** Engine::EngineFileLoader functions ********/ Engine::EngineFileLoader::EngineFileLoader(Engine * engine) { m_engine = engine; } int Engine::EngineFileLoader::getSize(const Path & path) { struct stat st; string file_path = resolvePath(path); if (file_path == "") return -1; if (stat(file_path.c_str(), &st)) return -2; return st.st_size; } FileLoader::Buffer Engine::EngineFileLoader::load(const Path & path) { string file_path = resolvePath(path); int size = getSize(path); if (size > 0) { FILE * fp = fopen(file_path.c_str(), "rb"); if (fp != NULL) { Buffer buf(size); int num_read = fread(buf.data, size, 1, fp); fclose(fp); if (num_read > 0) return buf; } } return Buffer(0); } string Engine::EngineFileLoader::resolvePath(const Path & path) { string file_path = m_engine->locateResource(path.shortPath); if (file_path == "") file_path = path.fullPath; return file_path; }