first time recompiling successfully after WFObj restructuring!
git-svn-id: svn://anubis/anaglym/trunk@93 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
parent
580788097d
commit
387141b506
83
Engine.cc
83
Engine.cc
@ -1,11 +1,13 @@
|
|||||||
|
|
||||||
#include "ag.h"
|
#include "ag.h"
|
||||||
|
#include "anaglym.h"
|
||||||
#include "Engine.h"
|
#include "Engine.h"
|
||||||
#include <lua.hpp>
|
#include <lua.hpp>
|
||||||
#include <stdlib.h> /* exit() */
|
#include <stdlib.h> /* exit() */
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <math.h> /* fabs() */
|
#include <math.h> /* fabs() */
|
||||||
@ -34,6 +36,7 @@ Engine::Engine(const string & path)
|
|||||||
m_autoStartFrame = true;
|
m_autoStartFrame = true;
|
||||||
m_autoEndFrame = true;
|
m_autoEndFrame = true;
|
||||||
m_autoDrawObjects = true;
|
m_autoDrawObjects = true;
|
||||||
|
m_fileLoader = new EngineFileLoader(this);
|
||||||
|
|
||||||
size_t pos = path.find_last_of("\\/");
|
size_t pos = path.find_last_of("\\/");
|
||||||
m_engine_path = (pos != string::npos) ? string(path, 0, pos) : ".";
|
m_engine_path = (pos != string::npos) ? string(path, 0, pos) : ".";
|
||||||
@ -52,6 +55,7 @@ Engine::~Engine()
|
|||||||
{
|
{
|
||||||
delete it->second;
|
delete it->second;
|
||||||
}
|
}
|
||||||
|
delete m_fileLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::load(const char * program)
|
bool Engine::load(const char * program)
|
||||||
@ -219,6 +223,38 @@ int Engine::setCamera(lua_State * L)
|
|||||||
return 0;
|
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");
|
||||||
|
string phys_path = locateResource(name + ".obj");
|
||||||
|
|
||||||
|
WFObj * obj = new WFObj(*m_fileLoader /* TODO: texture loaders */);
|
||||||
|
|
||||||
|
if (obj->load(model_path))
|
||||||
|
{
|
||||||
|
int id = addObject(obj, static_data, scale);
|
||||||
|
if (phys_path != "")
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/* called by SDL when the update timer expires */
|
/* called by SDL when the update timer expires */
|
||||||
Uint32 Engine::updateCallback(Uint32 interval, void * param)
|
Uint32 Engine::updateCallback(Uint32 interval, void * param)
|
||||||
{
|
{
|
||||||
@ -376,3 +412,50 @@ void Engine::Object::draw()
|
|||||||
glPopMatrix();
|
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)
|
||||||
|
{
|
||||||
|
int fd = open(file_path.c_str(), O_RDONLY);
|
||||||
|
if (fd > 0)
|
||||||
|
{
|
||||||
|
Buffer buf(size);
|
||||||
|
int num_read = read(fd, buf.data, size);
|
||||||
|
close(fd);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
16
Engine.h
16
Engine.h
@ -7,7 +7,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include "OdeWorld/OdeWorld.h"
|
#include "OdeWorld/OdeWorld.h"
|
||||||
#include "wfobj/WFObj.hh"
|
#include "wfobj/WFObj.h"
|
||||||
|
|
||||||
class Engine
|
class Engine
|
||||||
{
|
{
|
||||||
@ -46,6 +46,17 @@ class Engine
|
|||||||
bool m_is_scaled;
|
bool m_is_scaled;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class EngineFileLoader : public FileLoader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EngineFileLoader(Engine * engine);
|
||||||
|
virtual int getSize(const Path & path);
|
||||||
|
virtual Buffer load(const Path & path);
|
||||||
|
protected:
|
||||||
|
std::string resolvePath(const Path & path);
|
||||||
|
Engine * m_engine;
|
||||||
|
};
|
||||||
|
|
||||||
Engine(const std::string & path);
|
Engine(const std::string & path);
|
||||||
~Engine();
|
~Engine();
|
||||||
|
|
||||||
@ -70,6 +81,8 @@ class Engine
|
|||||||
bool getAutoDrawObjects() { return m_autoDrawObjects; }
|
bool getAutoDrawObjects() { return m_autoDrawObjects; }
|
||||||
void startFrame();
|
void startFrame();
|
||||||
void endFrame();
|
void endFrame();
|
||||||
|
int loadModel(const std::string & name, bool static_data,
|
||||||
|
float scale = 1.0f);
|
||||||
|
|
||||||
/* lua services */
|
/* lua services */
|
||||||
int setCamera(lua_State * L);
|
int setCamera(lua_State * L);
|
||||||
@ -88,6 +101,7 @@ class Engine
|
|||||||
return new Object(is_static, m_world, display_list, scale);
|
return new Object(is_static, m_world, display_list, scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EngineFileLoader * m_fileLoader;
|
||||||
lua_State * m_luaState;
|
lua_State * m_luaState;
|
||||||
std::string m_program_path;
|
std::string m_program_path;
|
||||||
std::string m_engine_path;
|
std::string m_engine_path;
|
||||||
|
38
ag.cc
38
ag.cc
@ -4,7 +4,7 @@
|
|||||||
#include "Engine.h"
|
#include "Engine.h"
|
||||||
#include "Video.h"
|
#include "Video.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include "wfobj/WFObj.hh"
|
#include "wfobj/WFObj.h"
|
||||||
#include <unistd.h> /* usleep() */
|
#include <unistd.h> /* usleep() */
|
||||||
#include <lua.hpp>
|
#include <lua.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -126,48 +126,24 @@ namespace ag
|
|||||||
|
|
||||||
static int loadModelSpecify(lua_State * L, bool static_data)
|
static int loadModelSpecify(lua_State * L, bool static_data)
|
||||||
{
|
{
|
||||||
|
bool added = false;
|
||||||
float scale = 1.0f;
|
float scale = 1.0f;
|
||||||
int argc = lua_gettop(L);
|
int argc = lua_gettop(L);
|
||||||
|
|
||||||
if (argc >= 2 && lua_isnumber(L, 2))
|
if (argc >= 2 && lua_isnumber(L, 2))
|
||||||
{
|
|
||||||
scale = lua_tonumber(L, 2);
|
scale = lua_tonumber(L, 2);
|
||||||
}
|
|
||||||
if (argc >= 1 && lua_isstring(L, 1))
|
if (argc >= 1 && lua_isstring(L, 1))
|
||||||
{
|
{
|
||||||
string modelname = lua_tostring(L, 1);
|
string modelname = lua_tostring(L, 1);
|
||||||
size_t pos = modelname.find_first_not_of(FILENAME_SAFE_CHARS);
|
int id = g_engine->loadModel(modelname, static_data, scale);
|
||||||
if (pos == string::npos)
|
if (id > 0)
|
||||||
{
|
{
|
||||||
string path = g_engine->locateResource(modelname + ".obj");
|
|
||||||
string physpath = g_engine->locateResource(modelname + ".phy");
|
|
||||||
if (path != "")
|
|
||||||
{
|
|
||||||
WFObj * obj = new WFObj();
|
|
||||||
|
|
||||||
if (obj->load(path))
|
|
||||||
{
|
|
||||||
int id = g_engine->addObject(obj, static_data, scale);
|
|
||||||
createLuaObject(L, id);
|
createLuaObject(L, id);
|
||||||
if (physpath != "")
|
added = true;
|
||||||
{
|
|
||||||
Engine::Object * obj = g_engine->getObject(id);
|
|
||||||
if (obj != NULL)
|
|
||||||
{
|
|
||||||
obj->loadPhy(physpath);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
if (!added)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
delete obj;
|
|
||||||
cerr << "error loading object" << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user