bluecore/engine/main.cpp

258 lines
5.7 KiB
C++

#include "GL/glew.h"
#include "RenderWindow.h"
#include "RenderDevice.h"
#include "FontManager.h"
#include "MeshManager.h"
#include "TextureManager.h"
#include "ShaderManager.h"
#include "ModelManager.h"
#include "TextureImage.h"
#include "ScriptSystem.h"
#include "ScriptSystem_Font.h"
#include "ScriptSystem_Image.h"
#include "ScriptSystem_Math.h"
#include "ScriptSystem_RigidBody.h"
#include "RigidBodySimulation.h"
#include "Camera.h"
#include "SceneNode.h"
#include "Utilities/CfgParser.h"
#include "Utilities/Log.h"
#include "Utilities/Kernel.h"
#include "physfs.h"
using namespace BlueCore;
void initializePhysfs(char* program)
{
// setup physfs
PHYSFS_init(program);
std::string appdir = PHYSFS_getUserDir();
appdir += ".bluecore";
if ( !PHYSFS_setWriteDir(appdir.c_str()) )
{
if ( (PHYSFS_setWriteDir(PHYSFS_getUserDir()))
&& (PHYSFS_mkdir(".bluecore")))
PHYSFS_setWriteDir(appdir.c_str() );
}
PHYSFS_addToSearchPath(appdir.c_str(), 0);
PHYSFS_addToSearchPath("data", 1);
char **rc = PHYSFS_enumerateFiles("");
for (char **i = rc; *i != 0; i++)
{
std::string filename( *i);
if (filename.substr(filename.size() - 4, 4) == ".zip")
{
PHYSFS_addToSearchPath(( "data/" + filename ).c_str(), 1);
clog << ">>> Using addon: "<< filename << endlog;
}
}
PHYSFS_freeList(rc);
}
void shutdownPhysfs()
{
PHYSFS_deinit();
}
class Application : public sigslot::has_slots<>
{
bool _Running;
double _DeltaTime;
double _LastTime;
void KeySlot(int key, int action)
{
if (key == GLFW_KEY_ESC && action == GLFW_RELEASE)
quit();
}
ref_ptr<RenderWindow> _Window;
ref_ptr<RenderDevice> _Device;
ref_ptr<FontManager> _FontManager;
ref_ptr<MeshManager> _MeshManager;
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;
public:
bool initialize()
{
CfgParser cfg;
cfg.parseFile("options.cfg");
int width = cfg.get("width", 640);
int height = cfg.get("height", 480);
bool fullscreen = cfg.get("fullscreen", false);
_Window = new RenderWindow();
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;
_DeltaTime = 0;
_LastTime = glfwGetTime();
while (_Window->isOpen() && _Running)
{
double time = glfwGetTime();
_DeltaTime = time - _LastTime;
_LastTime = time;
_Device->clear();
/*
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);
device->pushAbsoluteTransformation(Vector3(), camera->getRotation());
class RenderState
{
bool _Blending;
GLint _BlendFuncSrc;
GLint _BlendFuncDest;
bool _DepthTest;
bool _DepthMask;
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->setTexture (stage, name, texture)
// device->clearTextures (stage+1);
//glEnable (GL_TEXTURE_2D);
//glEnable (GL_LIGHTING);
//glBindTexture (GL_TEXTURE_2D, texture->getId() );
// device->
//device->
//model->render();
_Simulation->saveStates();
_Simulation->updateSteps(_DeltaTime);
while (_Simulation->step())
;
_ScriptSystem->callFunction("OnFrame", _DeltaTime);
_Device->swap();
}
clog << "--- main loop finished..."<< endlog;
}
};
int main(int argc, char **argv)
{
initializePhysfs(argv[0]);
Application app;
if (app.initialize())
{
app.run();
}
app.shutdown();
shutdownPhysfs();
}