some cleanup, fixed memory leak in scripting

This commit is contained in:
cirdan 2008-01-18 23:01:44 +00:00
parent 219d5743b8
commit 77504c68f3
6 changed files with 171 additions and 87 deletions

View File

@ -14,3 +14,6 @@ all clean:
@$(MAKE) -s -C tinyxml $@ @$(MAKE) -s -C tinyxml $@
@$(MAKE) -s -C engine $@ @$(MAKE) -s -C engine $@
@echo done @echo done
obj:
@$(MAKE) -s -C engine $@

View File

@ -54,6 +54,9 @@ $(NAME): $(OBJ)
@g++ -o $(NAME) $(OBJ) $(LDFLAGS) @g++ -o $(NAME) $(OBJ) $(LDFLAGS)
@echo done @echo done
obj: $(OBJ)
@echo done
clean: clean:
-@$(RMSUB) *.o -@$(RMSUB) *.o
-@$(RMSUB) *.d -@$(RMSUB) *.d

View File

@ -152,6 +152,15 @@ void RenderDevice::pushTransformation(const Vector3& position,
glMultMatrixd( ( GLdouble * ) &m.m ); glMultMatrixd( ( GLdouble * ) &m.m );
} }
void RenderDevice::pushAbsoluteTransformation(const Vector3& position,
const Quaternion& orientation)
{
glMatrixMode( GL_MODELVIEW);
glPushMatrix();
Matrix4x4 m(orientation, position);
glLoadMatrixd( ( GLdouble * ) &m.m );
}
void RenderDevice::popTransformation() void RenderDevice::popTransformation()
{ {
glMatrixMode( GL_MODELVIEW); glMatrixMode( GL_MODELVIEW);

View File

@ -45,6 +45,7 @@ public:
void setMaterial(Material *); void setMaterial(Material *);
void setTexture(unsigned int unit, Texture* texture, bool last = false); void setTexture(unsigned int unit, Texture* texture, bool last = false);
void pushAbsoluteTransformation (const Vector3&, const Quaternion&);
void pushTransformation (const Vector3&, const Quaternion&); void pushTransformation (const Vector3&, const Quaternion&);
void popTransformation (); void popTransformation ();
void setupProjectionMatrix(); void setupProjectionMatrix();

View File

@ -180,8 +180,8 @@ void ScriptSystem::callFunction(const std::string &name)
if (SQ_SUCCEEDED(sq_get(_VM, -2)) ) if (SQ_SUCCEEDED(sq_get(_VM, -2)) )
{ {
sq_pushroottable(_VM); sq_pushroottable(_VM);
if (sq_call(_VM, 1, SQTrue, SQTrue) ) sq_call(_VM, 1, SQFalse, SQTrue);
sq_pop(_VM, 1); sq_pop(_VM, 1);
} }
sq_pop(_VM, 1); sq_pop(_VM, 1);
} }
@ -195,8 +195,8 @@ void ScriptSystem::callFunction(const std::string &name, double value)
{ {
sq_pushroottable(_VM); sq_pushroottable(_VM);
sq_pushfloat(_VM, value); sq_pushfloat(_VM, value);
if (sq_call(_VM, 2, SQTrue, SQTrue) ) sq_call(_VM, 2, SQFalse, SQTrue);
sq_pop(_VM, 1); sq_pop(_VM, 1);
} }
sq_pop(_VM, 1); sq_pop(_VM, 1);
} }

View File

@ -65,101 +65,157 @@ void shutdownPhysfs()
PHYSFS_deinit(); PHYSFS_deinit();
} }
int main(int argc, char **argv) class Application : public sigslot::has_slots<>
{ {
initializePhysfs(argv[0]); bool _Running;
double _DeltaTime;
double _LastTime;
CfgParser cfg; void KeySlot(int key, int action)
cfg.parseFile("options.cfg");
int width = cfg.get("width", 640);
int height = cfg.get("height", 480);
bool fullscreen = cfg.get("fullscreen", false);
ref_ptr<RenderWindow> window = new RenderWindow();
window->create(width, height, 0, 0, 0, fullscreen);
ref_ptr<RenderDevice> device = new RenderDevice(window);
ref_ptr<FontManager> fontmanager = new FontManager(device);
ref_ptr<MeshManager> meshmanager = new MeshManager(device);
ref_ptr<TextureManager> texturemanager = new TextureManager();
ref_ptr<ScriptSystem> scriptsystem = new ScriptSystem();
ref_ptr<ShaderManager> shadermanager = new ShaderManager(window);
ref_ptr<RigidBodySimulation> simulation = new RigidBodySimulation(scriptsystem);
ref_ptr<ModelManager> modelmanager = new ModelManager (texturemanager, shadermanager, meshmanager);
ref_ptr<Camera> camera = new Camera();
ref_ptr<RenderQueue> queue = new RenderQueue();
setupScriptSystem_Font(scriptsystem, fontmanager);
setupScriptSystem_Image(scriptsystem, texturemanager, device);
setupScriptSystem_Math(scriptsystem);
setupScriptSystem_RigidBody(scriptsystem, simulation);
GLfloat mat_specular[] =
{ 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] =
{ 2.0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
ref_ptr<Mesh> starMesh = meshmanager->loadMesh("stars.3ds");
ref_ptr<Texture> starTexture = texturemanager->loadTexture("stars.png", 0);
ref_ptr<Mesh> nebularMesh = meshmanager->loadMesh("nebular.3ds");
ref_ptr<Texture> nebularTexture = texturemanager->loadTexture("nebular.png");
if (window.valid() && device.valid())
{ {
scriptsystem->loadScript("main"); if (key == GLFW_KEY_ESC && action == GLFW_RELEASE)
quit();
}
camera->setFoV(90.0); ref_ptr<RenderWindow> _Window;
camera->setAspectRatio((double)width/(double)height); ref_ptr<RenderDevice> _Device;
camera->setNearPlane(1.0); ref_ptr<FontManager> _FontManager;
camera->setFarPlane(15000.0); ref_ptr<MeshManager> _MeshManager;
camera->setPosition(Vector3(0.0, 0.0, 20.0)); ref_ptr<TextureManager> _TextureManager;
ref_ptr<ScriptSystem> _ScriptSystem;
ref_ptr<ShaderManager> _ShaderManager;
ref_ptr<RigidBodySimulation> _Simulation;
ref_ptr<ModelManager> _ModelManager;
ref_ptr<Camera> _Camera;
ref_ptr<RenderQueue> _RenderQueue;
device->setAmbientLight(1.0, 1.0, 1.0); public:
//ref_ptr<SceneNode> rootnode(new SceneNode("root node")); bool initialize()
{
CfgParser cfg;
cfg.parseFile("options.cfg");
double _DeltaTime = 0; int width = cfg.get("width", 640);
double _LastTime = glfwGetTime(); int height = cfg.get("height", 480);
bool fullscreen = cfg.get("fullscreen", false);
ref_ptr<Model> model = modelmanager->loadModel("combat"); _Window = new RenderWindow();
scriptsystem->callFunction("Initialize"); if (_Window->create(width, height, 0, 0, 0, fullscreen) == false)
return false;
_Device = new RenderDevice(_Window);
_FontManager = new FontManager(_Device);
_MeshManager = new MeshManager(_Device);
_TextureManager = new TextureManager();
_ScriptSystem = new ScriptSystem();
_ShaderManager = new ShaderManager(_Window);
_Simulation = new RigidBodySimulation(_ScriptSystem);
_ModelManager = new ModelManager (_TextureManager, _ShaderManager, _MeshManager);
_Camera = new Camera();
_RenderQueue = new RenderQueue();
setupScriptSystem_Font(_ScriptSystem, _FontManager);
setupScriptSystem_Image(_ScriptSystem, _TextureManager, _Device);
setupScriptSystem_Math(_ScriptSystem);
setupScriptSystem_RigidBody(_ScriptSystem, _Simulation);
_ScriptSystem->loadScript("main");
_ScriptSystem->callFunction("Initialize");
_Window->KeySignal.connect(this, &Application::KeySlot);
_Running = true;
return true;
}
/*
if (window.valid() && device.valid())
{
camera->setFoV(45.0);
camera->setAspectRatio((double)width/(double)height);
camera->setNearPlane(1.0);
camera->setFarPlane(15000.0);
camera->setPosition(Vector3(0.0, 0.0, 20.0));
device->setAmbientLight(1.0, 1.0, 1.0);
//ref_ptr<SceneNode> rootnode(new SceneNode("root node"));
ref_ptr<Model> model = modelmanager->loadModel("combat");
}
*/
void shutdown()
{
_ScriptSystem->callFunction("Shutdown");
}
void quit()
{
_Running = false;
}
void run()
{
clog << "--- starting main loop..."<< endlog; clog << "--- starting main loop..."<< endlog;
while (window->isOpen()) _DeltaTime = 0;
_LastTime = glfwGetTime();
while (_Window->isOpen() && _Running)
{ {
double time = glfwGetTime(); double time = glfwGetTime();
_DeltaTime = time - _LastTime; _DeltaTime = time - _LastTime;
_LastTime = time; _LastTime = time;
device->clear(); _Device->clear();
device->begin3D(camera); /*
camera->setRotation(Quaternion(Vector3(0.0, 1.0, 0.0), fmod(time
/5.0, 6.2)));
device->begin3D(camera);
queue->clear();
queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion());
queue->render(device);
queue->clear(); device->pushAbsoluteTransformation(Vector3(), camera->getRotation());
queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion(Vector3(0.0,1.0,0.0), fmod(time, 3)));
queue->render(device);
class RenderState
glMatrixMode ( GL_MODELVIEW ); {
glPushMatrix(); bool _Blending;
glLoadIdentity(); GLint _BlendFuncSrc;
glEnable ( GL_BLEND ); GLint _BlendFuncDest;
glBlendFunc ( GL_ONE, GL_ONE );
glEnable ( GL_DEPTH_TEST );
glDepthMask ( GL_FALSE );
glDisable ( GL_LIGHTING );
glColor4d ( 1.0f, 1.0f, 1.0f, 1.0f );
device->setTexture(0, starTexture, true); bool _DepthTest;
starMesh->render(); bool _DepthMask;
glDisable ( GL_BLEND );
bool _Lighting;
};
GLfloat mat_specular[] =
{ 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] =
{ 2.0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glDisable(GL_LIGHTING);
glColor4d( 1.0f, 1.0f, 1.0f, 1.0f);
device->setTexture(0, starTexture, true);
starMesh->render();
glDisable(GL_BLEND);
device->popTransformation();
*/
// device->useShader (program); // device->useShader (program);
// device->setTexture (stage, name, texture) // device->setTexture (stage, name, texture)
// device->clearTextures (stage+1); // device->clearTextures (stage+1);
@ -171,19 +227,31 @@ int main(int argc, char **argv)
//device-> //device->
//model->render(); //model->render();
simulation->saveStates(); _Simulation->saveStates();
simulation->updateSteps(_DeltaTime); _Simulation->updateSteps(_DeltaTime);
while (simulation->step()) while (_Simulation->step())
; ;
scriptsystem->callFunction("OnFrame", _DeltaTime);
device->swap(); _ScriptSystem->callFunction("OnFrame", _DeltaTime);
_Device->swap();
} }
scriptsystem->callFunction("Shutdown");
clog << "--- main loop finished..."<< endlog; clog << "--- main loop finished..."<< endlog;
} }
};
int main(int argc, char **argv)
{
initializePhysfs(argv[0]);
Application app;
if (app.initialize())
{
app.run();
}
app.shutdown();
shutdownPhysfs(); shutdownPhysfs();
} }