view states are now managed by a stack + bugfixing

- when one defines DECL_ENUM_LAST it is possible to check whether a string for
	a give enum exists
- making sure ships are alive when they are spawned
- added warning concerning QueueEvent and TriggerEvent (one shall avoid the
	latter)
This commit is contained in:
2010-11-27 20:56:38 +01:00
parent f8e5b7e873
commit 3340471c61
15 changed files with 114 additions and 55 deletions
+6 -3
View File
@@ -21,7 +21,8 @@ BEGIN_ENUM(EngineStatus)
DECL_ENUM_ELEMENT(EngineStatusRunning),
DECL_ENUM_ELEMENT(EngineStatusStopping),
DECL_ENUM_ELEMENT(EngineStatusStopped),
DECL_ENUM_ELEMENT(EngineStatusDestroying)
DECL_ENUM_ELEMENT(EngineStatusDestroying),
DECL_ENUM_LAST(EngineStatus)
}
END_ENUM(EngineStatus)
@@ -30,7 +31,8 @@ BEGIN_ENUM(LogLevel)
DECL_ENUM_ELEMENT(LogLevelDebug),
DECL_ENUM_ELEMENT(LogLevelWarning),
DECL_ENUM_ELEMENT(LogLevelMessage),
DECL_ENUM_ELEMENT(LogLevelError)
DECL_ENUM_ELEMENT(LogLevelError),
DECL_ENUM_LAST(LogLevel)
}
END_ENUM(LogLevel)
@@ -38,7 +40,8 @@ BEGIN_ENUM(FontJustification)
{
DECL_ENUM_ELEMENT(FontJustificationRight),
DECL_ENUM_ELEMENT(FontJustificationCenter),
DECL_ENUM_ELEMENT(FontJustificationLeft)
DECL_ENUM_ELEMENT(FontJustificationLeft),
DECL_ENUM_LAST(FontJustification)
}
END_ENUM(FontJustification)
+5 -2
View File
@@ -15,21 +15,24 @@
*
*/
#undef DECL_ENUM_LAST
#undef DECL_ENUM_ELEMENT
#undef BEGIN_ENUM
#undef END_ENUM
#ifndef GENERATE_ENUM_STRINGS
#define DECL_ENUM_LAST( ENUM_NAME ) ENUM_NAME ## Last
#define DECL_ENUM_ELEMENT( element ) element
#define BEGIN_ENUM( ENUM_NAME ) typedef enum tag##ENUM_NAME
#define END_ENUM( ENUM_NAME ) ENUM_NAME; \
const char* GetString##ENUM_NAME(enum tag##ENUM_NAME index); \
const char* GetString##ENUM_NAME(unsigned int index);
#else
#define DECL_ENUM_LAST( ENUM_NAME ) #ENUM_NAME
#define DECL_ENUM_ELEMENT( element ) #element
#define BEGIN_ENUM( ENUM_NAME ) const char* gs_##ENUM_NAME [] =
#define END_ENUM( ENUM_NAME ) ; \
const char* GetString##ENUM_NAME(enum \
tag##ENUM_NAME index){ return gs_##ENUM_NAME [index]; } \
const char* GetString##ENUM_NAME(unsigned int index){ return gs_##ENUM_NAME [index]; }
tag##ENUM_NAME index){ if (index < 0 || index > ENUM_NAME ## Last) return "Unknown Enum"; return gs_##ENUM_NAME [index]; } \
const char* GetString##ENUM_NAME(unsigned int index){ if (index < 0 || index > ENUM_NAME ## Last) return "Unknown Enum"; return gs_##ENUM_NAME [index]; }
#endif
+6 -1
View File
@@ -9,7 +9,12 @@ namespace Engine {
bool RegisterListener (Module *listener_module, const int event_type);
/** \brief Calls all event listeners to handle the events */
bool QueueEvent (const EventBasePtr &event);
/** \brief Calls the listener handlers immediately */
/** \brief Calls the listener handlers immediately
*
* \warning It is safer to use QueueEvent() instead as the event
* \warning handlers are called at a safely defined time and
* \warning there is a lower risk deleting entities that still
* \warning might be in use somewhere! */
bool TriggerEvent (const EventBasePtr &event);
}
+3 -1
View File
@@ -80,8 +80,10 @@ void Logging::Log (LogLevel level, const char *str, ...) {
mLogFileOut << "Error occured: Aborting!" << std::endl;
mLogFileOut.flush();
mLogFileOut.close();
exit (-1);
}
// we abort if there was an error
assert (level != LogLevelError);
}
void Logging::SetLogPrintLevel (LogLevel print_level) {
+25 -8
View File
@@ -58,21 +58,28 @@ void ModelBase::Process () {
entity_iter++;
} while (entity_iter != mEntities.end());
/*
// Update the timers
for (TimerIter timer_iter = mTimers.begin(); timer_iter != mTimers.end(); timer_iter++) {
timer_iter->second.Update(mDeltaSec);
timer_iter++;
}
*/
// simulate the world
mPhysics->Simulate (mDeltaSec, this);
// remove killed entities
unsigned int i;
for (i = 0; i < mKilledEntities.size(); i++)
UnregisterEntity (mKilledEntities[i]);
assert (mKilledEntities.size() <= mEntities.size());
mKilledEntities.clear();
// remove killed entities
if (mKilledEntities.size() > 0) {
unsigned int i;
LogDebug ("There are %d entities to kill", mKilledEntities.size());
for (i = 0; i < mKilledEntities.size(); i++)
UnregisterEntity (mKilledEntities[i]);
mKilledEntities.clear();
}
}
EntityBase* ModelBase::CreateEntity (int type) {
@@ -94,10 +101,10 @@ EntityBase* ModelBase::CreateEntity (int type) {
void ModelBase::RegisterEntity (EntityBase* entity) {
unsigned int id = entity->mId;
LogDebug ("Registering Entity with id '%d'", id);
LogDebug ("Registering Entity with id '%u' and type '%u'", id, entity->mType);
if (mEntities.find(id) != mEntities.end ()) {
LogError ("Replacing Entity with id '%d'", id);
LogError ("Replacing Entity with id '%u' and type '%u'", id, entity->mType);
}
if (entity->mPhysicState)
@@ -109,14 +116,18 @@ void ModelBase::RegisterEntity (EntityBase* entity) {
void ModelBase::KillEntity (const unsigned int id) {
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.find (id);
LogDebug ("Killing entity with id '%u'", id);
if (iter == mEntities.end ()) {
LogError ("Could not kill Entity with id '%d': Entity not found!", id);
LogError ("Could not kill Entity with id '%u': Entity not found!", id);
return;
} else {
EntityBase *entity = iter->second;
LogDebug ("Entity pointer = 0x%u", entity);
// call the event handler
OnKillEntity (entity);
LogDebug ("Entity pointer (after kill) = 0x%u", entity);
if (entity->mPhysicState) {
entity->mPhysicState->mAlive = false;
@@ -130,6 +141,7 @@ void ModelBase::UnregisterEntity (const unsigned int id) {
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.find (id);
if (iter == mEntities.end ()) {
LogDebug ("iter id=%u entitypointer=0x%x type=%d", iter->first, iter->second, iter->second->mType);
LogError ("Could not unregister Entity with id '%d': Entity not found!", id);
return;
} else {
@@ -180,6 +192,9 @@ void ModelBase::ClearEntities () {
}
mEntityIdCounter = 0;
// we alsohave to clear the vector of killed entities!
mKilledEntities.clear();
}
unsigned int ModelBase::GetPlayerEntityId () {
@@ -291,6 +306,7 @@ EntityPhysicState * GetEntityPhysicState (unsigned int id) {
return NULL;
}
/*
void StartTimer(const std::string &id, float sec) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntity(): Model not initialized!");
@@ -306,6 +322,7 @@ bool CheckTimer(const std::string &id) {
return ModelInstance->CheckTimer(id);
}
*/
}
+5 -1
View File
@@ -3,7 +3,7 @@
#include "Engine.h"
#include "EntityBase.h"
#include "Timer.h"
// #include "Timer.h"
namespace Engine {
@@ -80,6 +80,7 @@ class ModelBase : public Module {
};
unsigned int GetGameState () { return mGameState; };
/*
void StartTimer(const std::string &id, float msec) {
TimerIter cur_timer = mTimers.find(id);
if (cur_timer != mTimers.end()) {
@@ -102,6 +103,7 @@ class ModelBase : public Module {
return cur_timer->second.Query();
}
*/
protected:
/** \brief Initializes the system */
@@ -128,8 +130,10 @@ class ModelBase : public Module {
/** \brief contains all Engine::Entities that ceased to exist */
std::vector<unsigned int> mKilledEntities;
/*
std::map<std::string, Timer> mTimers;
typedef std::map<std::string, Timer>::iterator TimerIter;
*/
unsigned int mEntityIdCounter;
unsigned int mPlayerEntityId;
+5 -5
View File
@@ -9,11 +9,11 @@ unsigned int GetPlayerEntityId ();
/** \brief Returns the duration of the frame in seconds */
float GetFrameDuration ();
/** \brief Starts a timer with the given id that expires after sec seconds */
void StartTimer(const std::string &id, float sec);
/** \brief Checks whether a timer expired */
bool CheckTimer(const std::string &id);
// /** \brief Starts a timer with the given id that expires after sec seconds */
// void StartTimer(const std::string &id, float sec);
//
// /** \brief Checks whether a timer expired */
// bool CheckTimer(const std::string &id);
}