diff --git a/ag.cc b/ag.cc index 4713e19..49e3f4c 100644 --- a/ag.cc +++ b/ag.cc @@ -103,7 +103,7 @@ namespace ag size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS); if (pos == string::npos) { - string path = locateResource(modelname); + string path = g_engine->locateResource(modelname); if (path != "") { WFObj * obj = new WFObj(); @@ -121,13 +121,13 @@ namespace ag int videoStart(lua_State * L) { - g_video->start(); + g_engine->getVideo()->start(); return 0; } int videoStop(lua_State * L) { - g_video->stop(); + g_engine->getVideo()->stop(); return 0; } diff --git a/anaglym.cc b/anaglym.cc index c24ea37..80f9a01 100644 --- a/anaglym.cc +++ b/anaglym.cc @@ -1,17 +1,25 @@ #include "ag.h" #include "Video.h" +#include "anaglym.h" #include #include /* exit() */ +#include +#include +#include #include #include using namespace std; static void usage(); -static void report_errors(lua_State * L, int status); -static void register_libraries(lua_State * L); -Video * g_video = NULL; +Engine * g_engine; + +static void usage() +{ + cerr << "Usage: anaglym [options] program.lua[c]" << endl; + exit(42); +} int main(int argc, char * argv[]) { @@ -41,18 +49,51 @@ int main(int argc, char * argv[]) usage(); } - g_video = new Video(); + g_engine = new Engine(); + g_engine->load(program); + g_engine->run(); + delete g_engine; - lua_State * L = lua_open(); + return 0; +} - register_libraries(L); +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 + } +} - int s = luaL_loadfile(L, program); +Engine::Engine() +{ + m_video = new Video(); +} + +Engine::~Engine() +{ + delete m_video; +} + +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(L, 0, LUA_MULTRET, 0); + s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0); } else { @@ -60,48 +101,48 @@ int main(int argc, char * argv[]) return 1; } - report_errors(L, s); - lua_close(L); - - delete g_video; - - return 0; + reportErrors(s); + lua_close(m_luaState); } -static void report_errors(lua_State * L, int status) -{ - if ( status!=0 ) - { - cerr << "-- " << lua_tostring(L, -1) << endl; - lua_pop(L, 1); // remove error message - } -} - -static void usage() -{ - cerr << "Usage: anaglym [options] program.lua[c]" << endl; - exit(42); -} - -static void register_libraries(lua_State * L) +void Engine::registerLibraries() { /* Load the Lua string library */ - lua_pushcfunction(L, luaopen_string); - lua_pcall(L, 0, 0, 0); + lua_pushcfunction(m_luaState, luaopen_string); + lua_pcall(m_luaState, 0, 0, 0); /* Load the Lua math library */ - lua_pushcfunction(L, luaopen_math); - lua_pcall(L, 0, 0, 0); + lua_pushcfunction(m_luaState, luaopen_math); + lua_pcall(m_luaState, 0, 0, 0); /* Load the Lua table library */ - lua_pushcfunction(L, luaopen_table); - lua_pcall(L, 0, 0, 0); + lua_pushcfunction(m_luaState, luaopen_table); + lua_pcall(m_luaState, 0, 0, 0); - ag::register_functions(L); + ag::register_functions(m_luaState); } -string locateResource(const string & shortname) +string Engine::locateResource(const string & shortname) { - /* TODO: fill in */ + string try_path; + + try_path = m_program_path + "/" + 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; +} + +void Engine::run() +{ +} diff --git a/anaglym.h b/anaglym.h index 51eef69..d711aea 100644 --- a/anaglym.h +++ b/anaglym.h @@ -3,11 +3,32 @@ #define ANAGLYM_H #include +#include #include "Video.h" #define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" -std::string locateResource(const std::string & shortname); -extern Video * g_video; +class Engine +{ + public: + 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); + + protected: + void registerLibraries(); + bool fileExists(const std::string & path); + + lua_State * m_luaState; + Video * m_video; + std::string m_program_path; +}; + +extern Engine * g_engine; #endif