From a6306097671749ac0d24c9c7539a1d3ce54d2775 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 14 Oct 2009 00:32:02 +0000 Subject: [PATCH] compiling again after refactoring SDL calls out of Engine into anaglym.cc! git-svn-id: svn://anubis/anaglym/trunk@78 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 70 +++--------------------------------------------------- Engine.h | 8 ------- anaglym.cc | 66 +++++++++++++++++++++++++++++++++++++++++++++++++- anaglym.h | 9 +++++++ 4 files changed, 77 insertions(+), 76 deletions(-) diff --git a/Engine.cc b/Engine.cc index a57da07..e04ca9c 100644 --- a/Engine.cc +++ b/Engine.cc @@ -1,7 +1,7 @@ #include "ag.h" -#include "Video.h" #include "Engine.h" +#include "anaglym.h" #include #include /* exit() */ #include @@ -16,11 +16,8 @@ using namespace std; Engine * g_engine; -SDL_Event Engine::userEvent; - Engine::Engine(const string & path) { - m_video = new Video(); m_next_object_index = 1; m_eye[0] = 0; m_eye[1] = -1; @@ -36,21 +33,10 @@ Engine::Engine(const string & path) 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; -#if 0 - /* start in windowed mode for debugging */ - m_video->start(0, 0, false, false); -#else - m_video->start(); -#endif } Engine::~Engine() { - m_video->stop(); lua_close(m_luaState); for (std::map::iterator it = m_objects.begin(); it != m_objects.end(); @@ -58,7 +44,6 @@ Engine::~Engine() { delete it->second; } - delete m_video; } bool Engine::load(const char * program) @@ -188,7 +173,7 @@ int Engine::startFrame(lua_State * L) int Engine::endFrame(lua_State * L) { - SDL_GL_SwapBuffers(); + /* TODO: figure out how to SDL_GL_SwapBuffers(); */ return 0; } @@ -228,55 +213,6 @@ int Engine::setCamera(lua_State * L) return 0; } -/* 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: - if (event.key.keysym.sym == SDLK_ESCAPE) - { - goto RET; - } - break; - case SDL_QUIT: - goto RET; - break; - case SDL_USEREVENT: - if (event.user.code == 0) - { - update(); - } - break; - } - } -RET: - ; -} - void Engine::update() { m_drawing = true; @@ -299,7 +235,7 @@ void Engine::update() void Engine::doPhysics() { static Uint32 last_updated = 0; - Uint32 current_ticks = SDL_GetTicks(); + Uint32 current_ticks = GetTicks(); if (last_updated > 0) { Uint32 msec_steps = current_ticks - last_updated; diff --git a/Engine.h b/Engine.h index 8449f86..05f9e23 100644 --- a/Engine.h +++ b/Engine.h @@ -5,7 +5,6 @@ #include #include #include -#include "Video.h" #include "OdeWorld/OdeWorld.h" #include "wfobj/WFObj.hh" @@ -50,9 +49,7 @@ class Engine ~Engine(); std::string locateResource(const std::string & shortname); - Video * getVideo() { return m_video; } bool load(const char * program); - void run(); void reportErrors(int status); OdeWorld & getWorld() { return m_world; } int addObject(WFObj * obj, bool is_static, float scale = 1.0f); @@ -70,10 +67,6 @@ class Engine int setCamera(lua_State * L); protected: - static Uint32 updateCallback(Uint32 interval, void * param); - static SDL_Event userEvent; - - Uint32 updateCallback(Uint32 interval); void registerLibraries(); bool fileExists(const std::string & path); void update(); @@ -84,7 +77,6 @@ class Engine } lua_State * m_luaState; - Video * m_video; std::string m_program_path; std::string m_engine_path; OdeWorld m_world; diff --git a/anaglym.cc b/anaglym.cc index 4dd7e80..eef2185 100644 --- a/anaglym.cc +++ b/anaglym.cc @@ -9,6 +9,12 @@ using namespace std; static void usage(); +static void mainloop(); +static Uint32 updateCallback(Uint32 interval, void * param); + +static bool lastDrawCompleted; +static SDL_Event userEvent; +Uint32 g_ticks; static void usage() { @@ -46,10 +52,68 @@ int main(int argc, char * argv[]) usage(); } + lastDrawCompleted = true; + /* setup SDL update event */ + userEvent.type = SDL_USEREVENT; + userEvent.user.code = 0; + + Video video; +#if 0 + /* start in windowed mode for debugging */ + video.start(0, 0, false, false); +#else + video.start(); +#endif + g_engine = new Engine(argv[0]); if (g_engine->load(program)) - g_engine->run(); + { + mainloop(); + } delete g_engine; + video.stop(); + return 0; } + +/* called by SDL when the update timer expires */ +static Uint32 updateCallback(Uint32 interval, void * param) +{ + if (lastDrawCompleted) + SDL_PushEvent(&userEvent); + return interval; +} + +static void mainloop() +{ + SDL_Event event; + + /* register a screen redrawing SDL event */ + SDL_AddTimer(25, &updateCallback, NULL); + + while (SDL_WaitEvent(&event)) + { + switch (event.type) + { + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) + { + goto RET; + } + break; + case SDL_QUIT: + goto RET; + break; + case SDL_USEREVENT: + if (event.user.code == 0) + { + /* TODO: update */ + } + break; + } + } +RET: + ; +} + diff --git a/anaglym.h b/anaglym.h index d5cc2cb..e7d2325 100644 --- a/anaglym.h +++ b/anaglym.h @@ -2,6 +2,15 @@ #ifndef ANAGLYM_H #define ANAGLYM_H +#include + #define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" +extern Uint32 g_ticks; + +static inline Uint32 GetTicks() +{ + return g_ticks; +} + #endif