added Engine class to hold the state of the engine

git-svn-id: svn://anubis/anaglym/trunk@30 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2009-09-26 16:26:04 +00:00
parent c26261d1cf
commit 708800d06a
3 changed files with 106 additions and 44 deletions

6
ag.cc
View File

@ -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;
}

View File

@ -1,17 +1,25 @@
#include "ag.h"
#include "Video.h"
#include "anaglym.h"
#include <lua.hpp>
#include <stdlib.h> /* exit() */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <string>
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()
{
}

View File

@ -3,11 +3,32 @@
#define ANAGLYM_H
#include <string>
#include <lua.hpp>
#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