2010-04-05 23:38:59 +02:00
|
|
|
#include "Engine.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "Variables.h"
|
|
|
|
#include "Commands.h"
|
|
|
|
|
|
|
|
#include "PhysicsBase.h"
|
|
|
|
#include "CameraBase.h"
|
2010-10-18 00:40:50 +02:00
|
|
|
#include "SoundBase.h"
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
#include "ViewBase.h"
|
|
|
|
#include "ModelBase.h"
|
|
|
|
#include "ControllerBase.h"
|
|
|
|
|
|
|
|
#include "EntityFactoryBase.h"
|
2010-07-28 18:02:12 +02:00
|
|
|
#include "EventManager.h"
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <assert.h>
|
2011-01-02 21:04:11 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
int vasprintf (char **result, const char *format, va_list *string) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Engine {
|
|
|
|
|
|
|
|
/* Globaly visible classes */
|
|
|
|
Engine* EngineInstance = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inherited Module functions
|
|
|
|
*/
|
|
|
|
int Engine::OnInit (int argc, char* argv[]) {
|
|
|
|
EngineInstance = this;
|
|
|
|
mStatus = EngineStatusUndefined;
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
/* Phase 0: Logging, Events, Variables, Commands, SDL */
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
/* Initialization of the base modules */
|
|
|
|
if (mLogging == NULL)
|
|
|
|
mLogging = new Logging ();
|
|
|
|
|
|
|
|
if (! mLogging ) {
|
|
|
|
SetError ("Could not allocate memory for Logging!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
mLogging->Init (argc, argv);
|
|
|
|
SetStatus (EngineStatusInitializing);
|
|
|
|
|
|
|
|
if (mEventManager == NULL)
|
|
|
|
mEventManager = new EventManager();
|
|
|
|
|
|
|
|
mEventManager->Init (argc, argv);
|
|
|
|
|
|
|
|
if (mVariables == NULL)
|
|
|
|
mVariables = new Variables ();
|
|
|
|
if (! mVariables ) {
|
|
|
|
SetError ("Could not allocate memory for Variables!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
mVariables->Init (argc, argv);
|
|
|
|
|
|
|
|
if (mCommands == NULL)
|
|
|
|
mCommands = new Commands ();
|
|
|
|
if (! mCommands ) {
|
|
|
|
SetError ("Could not allocate memory for Commands!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
mCommands->Init (argc, argv);
|
|
|
|
|
|
|
|
// Initialize the SDL
|
|
|
|
LogDebug ("Initializing SDL");
|
2010-10-18 00:40:50 +02:00
|
|
|
if ( SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0) {
|
2010-04-05 23:38:59 +02:00
|
|
|
LogError ("Error initializing SDL: %s", SDL_GetError ());
|
|
|
|
exit (-1);
|
|
|
|
}
|
2010-04-08 20:04:39 +02:00
|
|
|
SDL_WM_SetCaption("Engine Initializing","Engine Initializing");
|
2010-04-05 23:38:59 +02:00
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
/* Now register the commands */
|
|
|
|
mLogging->RegisterCommands ();
|
|
|
|
mVariables->RegisterCommands ();
|
|
|
|
mCommands->RegisterCommands ();
|
|
|
|
|
|
|
|
/* Phase 1: Sounds, Model, EntityFactory, Physics, View, Camera, Controller*/
|
|
|
|
|
2010-10-18 00:40:50 +02:00
|
|
|
/* Sound */
|
|
|
|
mSoundManager = new SoundBase();
|
|
|
|
mSoundManager->Init (argc, argv);
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
/* Model */
|
|
|
|
if (mModel == NULL)
|
|
|
|
mModel = new ModelBase ();
|
|
|
|
|
|
|
|
if (! mModel ) {
|
|
|
|
SetError ("Could not allocate memory for Model!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialization of the modules for the model */
|
|
|
|
if (mEntityFactory == NULL)
|
|
|
|
mEntityFactory = new EntityFactoryBase;
|
|
|
|
|
|
|
|
mModel->mEntityFactory = mEntityFactory;
|
|
|
|
|
|
|
|
/* Physics */
|
|
|
|
if (mPhysics == NULL) {
|
|
|
|
mPhysics = new PhysicsBase ();
|
|
|
|
|
|
|
|
if (! mPhysics ) {
|
|
|
|
SetError ("Could not allocate memory for Physics!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mModel->mPhysics = mPhysics;
|
|
|
|
|
|
|
|
mEntityFactory->Init (argc, argv);
|
|
|
|
mModel->mPhysics->Init (argc, argv);
|
|
|
|
mModel->Init (argc, argv);
|
|
|
|
|
|
|
|
/* View */
|
|
|
|
if (mView == NULL)
|
|
|
|
mView = new ViewBase ();
|
|
|
|
|
|
|
|
if (! mView ) {
|
|
|
|
SetError ("Could not allocate memory for View!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mView->mCamera = new CameraBase ();
|
|
|
|
mView->mCamera->Init (argc, argv);
|
|
|
|
|
|
|
|
mView->mModel = mModel;
|
|
|
|
mView->Init (argc, argv);
|
|
|
|
|
|
|
|
/* Controller */
|
|
|
|
if (mController == NULL)
|
|
|
|
mController = new ControllerBase ();
|
|
|
|
|
|
|
|
if (! mController ) {
|
|
|
|
SetError ("Could not allocate memory for Controller!");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mController->mModel = mModel;
|
|
|
|
mController->mView = mView;
|
|
|
|
|
|
|
|
mController->Init (argc, argv);
|
|
|
|
|
2011-03-16 17:03:41 +01:00
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
mModel->mPhysics->RegisterCommands ();
|
|
|
|
mModel->RegisterCommands ();
|
|
|
|
|
|
|
|
mView->mCamera->RegisterCommands ();
|
|
|
|
mView->RegisterCommands ();
|
|
|
|
|
|
|
|
mController->RegisterCommands ();
|
|
|
|
|
|
|
|
RegisterCommands ();
|
|
|
|
|
|
|
|
/* Now we are done */
|
|
|
|
SetStatus (EngineStatusInitialized);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::OnDestroy () {
|
|
|
|
SetStatus (EngineStatusDestroying);
|
|
|
|
|
2010-10-18 00:40:50 +02:00
|
|
|
if (mSoundManager) {
|
|
|
|
mSoundManager->Destroy();
|
|
|
|
delete mSoundManager;
|
|
|
|
mSoundManager = NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
// Quit the SDL
|
|
|
|
SDL_Quit ();
|
|
|
|
|
|
|
|
if (mController) {
|
|
|
|
mController->Destroy ();
|
|
|
|
delete mController;
|
|
|
|
mController = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mView) {
|
|
|
|
if (mView->mCamera ) {
|
|
|
|
mView->mCamera->Destroy ();
|
|
|
|
delete mView->mCamera;
|
|
|
|
}
|
|
|
|
|
|
|
|
mView->Destroy ();
|
|
|
|
delete mView;
|
|
|
|
mView = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mModel) {
|
|
|
|
mModel->Destroy ();
|
|
|
|
|
|
|
|
if (mModel->mPhysics ) {
|
|
|
|
mModel->mPhysics->Destroy ();
|
|
|
|
delete mModel->mPhysics;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete mModel;
|
|
|
|
mModel = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCommands) {
|
|
|
|
mCommands->Destroy ();
|
|
|
|
delete mCommands;
|
|
|
|
mCommands = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVariables) {
|
|
|
|
mVariables->Destroy ();
|
|
|
|
delete mVariables;
|
|
|
|
mVariables = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatus (EngineStatusStopped);
|
|
|
|
|
|
|
|
if (mEventManager) {
|
|
|
|
mEventManager->Destroy();
|
|
|
|
delete mEventManager;
|
|
|
|
mEventManager = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLogging) {
|
|
|
|
mLogging->Destroy ();
|
|
|
|
delete mLogging;
|
|
|
|
mLogging = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStatus = EngineStatusUndefined;
|
|
|
|
EngineInstance = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module specific functions
|
|
|
|
*/
|
|
|
|
void Engine::SetStatus (EngineStatus new_status) {
|
2010-04-14 20:13:19 +02:00
|
|
|
LogDebug ("EngineStatus Change: '%s' -> '%s'", GetStringEngineStatus(mStatus), GetStringEngineStatus(new_status));
|
|
|
|
// LogDebug ("EngineStatus Change: '%d' -> '%d'", mStatus, new_status);
|
2010-04-05 23:38:59 +02:00
|
|
|
mStatus = new_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineStatus Engine::GetStatus () {
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::OnMainLoop () {
|
|
|
|
SetStatus (EngineStatusRunning);
|
|
|
|
|
|
|
|
while (mStatus == EngineStatusRunning) {
|
|
|
|
mController->Process ();
|
|
|
|
mCommands->QueueExecute ();
|
|
|
|
mEventManager->Process();
|
|
|
|
mModel->UpdateTimer ();
|
|
|
|
mModel->Process ();
|
2010-08-29 23:59:24 +02:00
|
|
|
mController->IMGUIPrepare();
|
2010-04-05 23:38:59 +02:00
|
|
|
mView->Draw ();
|
2010-08-29 23:59:24 +02:00
|
|
|
mController->IMGUIFinish();
|
2010-04-05 23:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::SetError (const char* str, ...) {
|
|
|
|
static char msg[LOG_MAX_MESSAGE_LENGTH];
|
|
|
|
static int last_length = LOG_MAX_MESSAGE_LENGTH;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < last_length; i++)
|
|
|
|
msg[i] = 0;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, str);
|
|
|
|
vsprintf(msg, str, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
mErrorString = msg;
|
|
|
|
|
|
|
|
last_length = strlen (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Engine::GetError () {
|
|
|
|
return mErrorString.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global functions
|
|
|
|
*/
|
|
|
|
void EngineSetError (const char* str, ...) {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char msg[LOG_MAX_MESSAGE_LENGTH];
|
|
|
|
static int last_length = LOG_MAX_MESSAGE_LENGTH;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < last_length; i++)
|
|
|
|
msg[i] = 0;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, str);
|
|
|
|
vsprintf(msg, str, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
EngineInstance->SetError (msg);
|
|
|
|
|
|
|
|
last_length = strlen (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* EngineGetError () {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetError ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EngineSetStatus (const EngineStatus status) {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineInstance->SetStatus (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineStatus EngineGetStatus () {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetStatus ();
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelBase* EngineGetModel () {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewBase* EngineGetView () {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetView();
|
|
|
|
}
|
|
|
|
|
|
|
|
ControllerBase* EngineGetController () {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetController();
|
|
|
|
}
|
|
|
|
|
2010-12-03 00:15:26 +01:00
|
|
|
std::string GetResourceFullPath (const std::string &resource) {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetResourceFullPath(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetUserDirFullPath (const std::string &path) {
|
|
|
|
if (EngineInstance == NULL) {
|
|
|
|
std::cerr << "Error: Engine Instance not yet initialized!" << std::endl;
|
|
|
|
assert (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EngineInstance->GetUserDirFullPath(path);
|
|
|
|
}
|
|
|
|
|
2011-01-02 21:04:11 +01:00
|
|
|
bool FileExists (const std::string &path) {
|
|
|
|
boost::filesystem::path file_path (path);
|
|
|
|
|
|
|
|
if (!boost::filesystem::exists(file_path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (boost::filesystem::is_directory(file_path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
}
|
|
|
|
|