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:
parent
c26261d1cf
commit
708800d06a
6
ag.cc
6
ag.cc
@ -103,7 +103,7 @@ namespace ag
|
|||||||
size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS);
|
size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS);
|
||||||
if (pos == string::npos)
|
if (pos == string::npos)
|
||||||
{
|
{
|
||||||
string path = locateResource(modelname);
|
string path = g_engine->locateResource(modelname);
|
||||||
if (path != "")
|
if (path != "")
|
||||||
{
|
{
|
||||||
WFObj * obj = new WFObj();
|
WFObj * obj = new WFObj();
|
||||||
@ -121,13 +121,13 @@ namespace ag
|
|||||||
|
|
||||||
int videoStart(lua_State * L)
|
int videoStart(lua_State * L)
|
||||||
{
|
{
|
||||||
g_video->start();
|
g_engine->getVideo()->start();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int videoStop(lua_State * L)
|
int videoStop(lua_State * L)
|
||||||
{
|
{
|
||||||
g_video->stop();
|
g_engine->getVideo()->stop();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
119
anaglym.cc
119
anaglym.cc
@ -1,17 +1,25 @@
|
|||||||
|
|
||||||
#include "ag.h"
|
#include "ag.h"
|
||||||
#include "Video.h"
|
#include "Video.h"
|
||||||
|
#include "anaglym.h"
|
||||||
#include <lua.hpp>
|
#include <lua.hpp>
|
||||||
#include <stdlib.h> /* exit() */
|
#include <stdlib.h> /* exit() */
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static void usage();
|
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[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
@ -41,18 +49,51 @@ int main(int argc, char * argv[])
|
|||||||
usage();
|
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 )
|
if ( s == 0 )
|
||||||
{
|
{
|
||||||
// execute Lua program
|
// execute Lua program
|
||||||
s = lua_pcall(L, 0, LUA_MULTRET, 0);
|
s = lua_pcall(m_luaState, 0, LUA_MULTRET, 0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -60,48 +101,48 @@ int main(int argc, char * argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
report_errors(L, s);
|
reportErrors(s);
|
||||||
lua_close(L);
|
lua_close(m_luaState);
|
||||||
|
|
||||||
delete g_video;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void report_errors(lua_State * L, int status)
|
void Engine::registerLibraries()
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
/* Load the Lua string library */
|
/* Load the Lua string library */
|
||||||
lua_pushcfunction(L, luaopen_string);
|
lua_pushcfunction(m_luaState, luaopen_string);
|
||||||
lua_pcall(L, 0, 0, 0);
|
lua_pcall(m_luaState, 0, 0, 0);
|
||||||
|
|
||||||
/* Load the Lua math library */
|
/* Load the Lua math library */
|
||||||
lua_pushcfunction(L, luaopen_math);
|
lua_pushcfunction(m_luaState, luaopen_math);
|
||||||
lua_pcall(L, 0, 0, 0);
|
lua_pcall(m_luaState, 0, 0, 0);
|
||||||
|
|
||||||
/* Load the Lua table library */
|
/* Load the Lua table library */
|
||||||
lua_pushcfunction(L, luaopen_table);
|
lua_pushcfunction(m_luaState, luaopen_table);
|
||||||
lua_pcall(L, 0, 0, 0);
|
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 "";
|
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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
25
anaglym.h
25
anaglym.h
@ -3,11 +3,32 @@
|
|||||||
#define ANAGLYM_H
|
#define ANAGLYM_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <lua.hpp>
|
||||||
#include "Video.h"
|
#include "Video.h"
|
||||||
|
|
||||||
#define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
|
#define FILENAME_SAFE_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
|
||||||
|
|
||||||
std::string locateResource(const std::string & shortname);
|
class Engine
|
||||||
extern Video * g_video;
|
{
|
||||||
|
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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user