Refactoring in src/common

- refactored CInstanceManager and some headers
- removed old headers
dev-ui
Piotr Dziwinski 2012-09-09 17:51:10 +02:00
parent b3d5491af4
commit efe4f0badd
28 changed files with 401 additions and 508 deletions

View File

@ -64,12 +64,11 @@ app/main.cpp
app/system.cpp app/system.cpp
common/event.cpp common/event.cpp
common/image.cpp common/image.cpp
common/logger.cpp
common/iman.cpp common/iman.cpp
# common/metafile.cpp common/logger.cpp
# common/misc.cpp common/metafile.cpp
# common/modfile.cpp common/misc.cpp
# common/profile.cpp common/profile.cpp
common/restext.cpp common/restext.cpp
common/restext_strings.c common/restext_strings.c
common/stringutils.cpp common/stringutils.cpp
@ -191,7 +190,10 @@ ${PLATFORM_LIBS}
CBot CBot
) )
include_directories(. ${CMAKE_CURRENT_BINARY_DIR} include_directories(
.
..
${CMAKE_CURRENT_BINARY_DIR}
${SDL_INCLUDE_DIR} ${SDL_INCLUDE_DIR}
${SDL_IMAGE_INCLUDE_DIR} ${SDL_IMAGE_INCLUDE_DIR}
${SDLTTF_INCLUDE_DIR} ${SDLTTF_INCLUDE_DIR}

View File

@ -32,7 +32,7 @@
#include <stdio.h> #include <stdio.h>
template<> CApplication* CSingleton<CApplication>::mInstance = NULL; template<> CApplication* CSingleton<CApplication>::mInstance = nullptr;
//! Interval of timer called to update joystick state //! Interval of timer called to update joystick state
@ -62,8 +62,8 @@ struct ApplicationPrivate
ApplicationPrivate() ApplicationPrivate()
{ {
memset(&currentEvent, 0, sizeof(SDL_Event)); memset(&currentEvent, 0, sizeof(SDL_Event));
surface = NULL; surface = nullptr;
joystick = NULL; joystick = nullptr;
joystickTimer = 0; joystickTimer = 0;
} }
}; };
@ -76,10 +76,10 @@ CApplication::CApplication()
m_iMan = new CInstanceManager(); m_iMan = new CInstanceManager();
m_eventQueue = new CEventQueue(m_iMan); m_eventQueue = new CEventQueue(m_iMan);
m_engine = NULL; m_engine = nullptr;
m_device = NULL; m_device = nullptr;
m_robotMain = NULL; m_robotMain = nullptr;
m_sound = NULL; m_sound = nullptr;
m_keyState = 0; m_keyState = 0;
m_axeKey = Math::Vector(0.0f, 0.0f, 0.0f); m_axeKey = Math::Vector(0.0f, 0.0f, 0.0f);
@ -95,19 +95,21 @@ CApplication::CApplication()
m_dataPath = "./data"; m_dataPath = "./data";
m_language = LANG_ENGLISH;
ResetKey(); ResetKey();
} }
CApplication::~CApplication() CApplication::~CApplication()
{ {
delete m_private; delete m_private;
m_private = NULL; m_private = nullptr;
delete m_eventQueue; delete m_eventQueue;
m_eventQueue = NULL; m_eventQueue = nullptr;
delete m_iMan; delete m_iMan;
m_iMan = NULL; m_iMan = nullptr;
} }
bool CApplication::ParseArguments(int argc, char *argv[]) bool CApplication::ParseArguments(int argc, char *argv[])
@ -218,7 +220,7 @@ bool CApplication::Create()
if (! CreateVideoSurface()) if (! CreateVideoSurface())
return false; // dialog is in function return false; // dialog is in function
if (m_private->surface == NULL) if (m_private->surface == nullptr)
{ {
m_errorMessage = std::string("SDL error while setting video mode:\n") + m_errorMessage = std::string("SDL error while setting video mode:\n") +
std::string(SDL_GetError()); std::string(SDL_GetError());
@ -269,7 +271,7 @@ bool CApplication::Create()
bool CApplication::CreateVideoSurface() bool CApplication::CreateVideoSurface()
{ {
const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
if (videoInfo == NULL) if (videoInfo == nullptr)
{ {
m_errorMessage = std::string("SDL error while getting video info:\n ") + m_errorMessage = std::string("SDL error while getting video info:\n ") +
std::string(SDL_GetError()); std::string(SDL_GetError());
@ -321,44 +323,44 @@ bool CApplication::CreateVideoSurface()
void CApplication::Destroy() void CApplication::Destroy()
{ {
/*if (m_robotMain != NULL) /*if (m_robotMain != nullptr)
{ {
delete m_robotMain; delete m_robotMain;
m_robotMain = NULL; m_robotMain = nullptr;
} }
if (m_sound != NULL) if (m_sound != nullptr)
{ {
delete m_sound; delete m_sound;
m_sound = NULL; m_sound = nullptr;
}*/ }*/
if (m_engine != NULL) if (m_engine != nullptr)
{ {
m_engine->Destroy(); m_engine->Destroy();
delete m_engine; delete m_engine;
m_engine = NULL; m_engine = nullptr;
} }
if (m_device != NULL) if (m_device != nullptr)
{ {
m_device->Destroy(); m_device->Destroy();
delete m_device; delete m_device;
m_device = NULL; m_device = nullptr;
} }
if (m_private->joystick != NULL) if (m_private->joystick != nullptr)
{ {
SDL_JoystickClose(m_private->joystick); SDL_JoystickClose(m_private->joystick);
m_private->joystick = NULL; m_private->joystick = nullptr;
} }
if (m_private->surface != NULL) if (m_private->surface != nullptr)
{ {
SDL_FreeSurface(m_private->surface); SDL_FreeSurface(m_private->surface);
m_private->surface = NULL; m_private->surface = nullptr;
} }
IMG_Quit(); IMG_Quit();
@ -383,7 +385,7 @@ bool CApplication::ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig)
return false; return false;
} }
if (m_private->surface == NULL) if (m_private->surface == nullptr)
{ {
if (! restore) if (! restore)
{ {
@ -426,7 +428,7 @@ bool CApplication::OpenJoystick()
return false; return false;
m_private->joystick = SDL_JoystickOpen(m_joystick.index); m_private->joystick = SDL_JoystickOpen(m_joystick.index);
if (m_private->joystick == NULL) if (m_private->joystick == nullptr)
return false; return false;
m_joystick.axisCount = SDL_JoystickNumAxes(m_private->joystick); m_joystick.axisCount = SDL_JoystickNumAxes(m_private->joystick);
@ -437,7 +439,7 @@ bool CApplication::OpenJoystick()
m_joyButtonState = std::vector<bool>(m_joystick.buttonCount, false); m_joyButtonState = std::vector<bool>(m_joystick.buttonCount, false);
// Create a timer for polling joystick state // Create a timer for polling joystick state
m_private->joystickTimer = SDL_AddTimer(JOYSTICK_TIMER_INTERVAL, JoystickTimerCallback, NULL); m_private->joystickTimer = SDL_AddTimer(JOYSTICK_TIMER_INTERVAL, JoystickTimerCallback, nullptr);
return true; return true;
} }
@ -447,7 +449,7 @@ void CApplication::CloseJoystick()
// Timer will remove itself automatically // Timer will remove itself automatically
SDL_JoystickClose(m_private->joystick); SDL_JoystickClose(m_private->joystick);
m_private->joystick = NULL; m_private->joystick = nullptr;
} }
bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick) bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick)
@ -455,7 +457,7 @@ bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick)
if ( (newJoystick.index < 0) || (newJoystick.index >= SDL_NumJoysticks()) ) if ( (newJoystick.index < 0) || (newJoystick.index >= SDL_NumJoysticks()) )
return false; return false;
if (m_private->joystick != NULL) if (m_private->joystick != nullptr)
CloseJoystick(); CloseJoystick();
return OpenJoystick(); return OpenJoystick();
@ -464,7 +466,7 @@ bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick)
Uint32 JoystickTimerCallback(Uint32 interval, void *) Uint32 JoystickTimerCallback(Uint32 interval, void *)
{ {
CApplication *app = CApplication::GetInstancePointer(); CApplication *app = CApplication::GetInstancePointer();
if ((app == NULL) || (! app->GetJoystickEnabled())) if ((app == nullptr) || (! app->GetJoystickEnabled()))
return 0; // don't run the timer again return 0; // don't run the timer again
app->UpdateJoystick(); app->UpdateJoystick();
@ -577,7 +579,7 @@ int CApplication::Run()
{ {
bool passOn = ProcessEvent(event); bool passOn = ProcessEvent(event);
if (m_engine != NULL && passOn) if (m_engine != nullptr && passOn)
passOn = m_engine->ProcessEvent(event); passOn = m_engine->ProcessEvent(event);
if (passOn) if (passOn)
@ -602,11 +604,11 @@ int CApplication::Run()
{ {
passOn = ProcessEvent(event); passOn = ProcessEvent(event);
if (passOn && m_engine != NULL) if (passOn && m_engine != nullptr)
passOn = m_engine->ProcessEvent(event); passOn = m_engine->ProcessEvent(event);
} }
/*if (passOn && m_robotMain != NULL) /*if (passOn && m_robotMain != nullptr)
m_robotMain->ProcessEvent(event); */ m_robotMain->ProcessEvent(event); */
} }
@ -814,7 +816,7 @@ VideoQueryResult CApplication::GetVideoResolutionList(std::vector<Math::IntPoint
resolutions.clear(); resolutions.clear();
const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
if (videoInfo == NULL) if (videoInfo == nullptr)
return VIDEO_QUERY_ERROR; return VIDEO_QUERY_ERROR;
Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE;
@ -964,3 +966,13 @@ std::string CApplication::GetDataFilePath(const std::string& dirName, const std:
{ {
return m_dataPath + "/" + dirName + "/" + fileName; return m_dataPath + "/" + dirName + "/" + fileName;
} }
Language CApplication::GetLanguage()
{
return m_language;
}
void CApplication::SetLanguage(Language language)
{
m_language = language;
}

View File

@ -22,8 +22,7 @@
#pragma once #pragma once
#include "common/global.h"
#include "common/misc.h"
#include "common/singleton.h" #include "common/singleton.h"
#include "graphics/core/device.h" #include "graphics/core/device.h"
#include "graphics/engine/engine.h" #include "graphics/engine/engine.h"
@ -198,6 +197,12 @@ public:
//! Returns the full path to a file in data directory //! Returns the full path to a file in data directory
std::string GetDataFilePath(const std::string &dirName, const std::string &fileName); std::string GetDataFilePath(const std::string &dirName, const std::string &fileName);
//! Management of language
//@{
Language GetLanguage();
void SetLanguage(Language language);
//@}
protected: protected:
//! Creates the window's SDL_Surface //! Creates the window's SDL_Surface
bool CreateVideoSurface(); bool CreateVideoSurface();
@ -267,5 +272,8 @@ protected:
//! Path to directory with data files //! Path to directory with data files
std::string m_dataPath; std::string m_dataPath;
//! Application language
Language m_language;
}; };

View File

@ -14,6 +14,9 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// TODO: move to global.h after restext rewrite
#pragma once #pragma once
enum Error enum Error

View File

@ -19,6 +19,16 @@
#include "common/event.h" #include "common/event.h"
#include "common/iman.h" #include "common/iman.h"
static EventType g_uniqueEventType = EVENT_USER;
EventType GetUniqueEventType()
{
int i = (int)g_uniqueEventType+1;
g_uniqueEventType = (EventType)i;
return g_uniqueEventType;
}
CEventQueue::CEventQueue(CInstanceManager* iMan) CEventQueue::CEventQueue(CInstanceManager* iMan)

View File

@ -14,17 +14,17 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// event.h /**
* \file common/event.h
* \brief Event types, structs and event queue
*/
#pragma once #pragma once
#include <common/key.h> #include "common/key.h"
#include <common/event_ids.h> #include "common/event_ids.h"
#include <math/point.h> #include "math/point.h"
#include <string.h>
class CInstanceManager; class CInstanceManager;
@ -193,40 +193,8 @@ struct Event
}; };
/** //! Returns an unique event type (above the standard IDs)
\enum KeyRank EventType GetUniqueEventType();
\brief Slots for key assignment of user controls
*/
// TODO: move to global.h ?
enum KeyRank
{
KEYRANK_LEFT = 0,
KEYRANK_RIGHT = 1,
KEYRANK_UP = 2,
KEYRANK_DOWN = 3,
KEYRANK_GUP = 4,
KEYRANK_GDOWN = 5,
KEYRANK_CAMERA = 6,
KEYRANK_DESEL = 7,
KEYRANK_ACTION = 8,
KEYRANK_NEAR = 9,
KEYRANK_AWAY = 10,
KEYRANK_NEXT = 11,
KEYRANK_HUMAN = 12,
KEYRANK_QUIT = 13,
KEYRANK_HELP = 14,
KEYRANK_PROG = 15,
KEYRANK_VISIT = 16,
KEYRANK_SPEED10 = 17,
KEYRANK_SPEED15 = 18,
KEYRANK_SPEED20 = 19,
KEYRANK_SPEED30 = 20,
KEYRANK_AIMUP = 21,
KEYRANK_AIMDOWN = 22,
KEYRANK_CBOT = 23,
};
/** /**
@ -261,5 +229,3 @@ protected:
int m_tail; int m_tail;
int m_total; int m_total;
}; };

View File

@ -14,6 +14,9 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// TODO: move to event.h after restext rewrite
#pragma once #pragma once
/** /**

View File

@ -14,53 +14,110 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// global.h /**
* \file common/global.h
* \brief Some common, global definitions
*/
#pragma once #pragma once
#include "error_ids.h"
/**
* \enum Language
* \brief Application language
*/
enum Language
{
LANG_ENGLISH = 0,
LANG_FRENCH = 1,
LANG_GERMAN = 2,
LANG_POLISH = 3
};
/**
* \enum BuildType
* \brief Construction actions (buildings, etc.) available to user
*
* TODO: refactor
*/
enum BuildType enum BuildType
{ {
BUILD_FACTORY = (1<<0), // factory BUILD_FACTORY = (1<<0), //! < factory
BUILD_DERRICK = (1<<1), // derrick BUILD_DERRICK = (1<<1), //! < derrick
BUILD_CONVERT = (1<<2), // converter BUILD_CONVERT = (1<<2), //! < converter
BUILD_RADAR = (1<<3), // radar BUILD_RADAR = (1<<3), //! < radar
BUILD_ENERGY = (1<<4), // factory of cells BUILD_ENERGY = (1<<4), //! < factory of cells
BUILD_NUCLEAR = (1<<5), // nuclear power plant BUILD_NUCLEAR = (1<<5), //! < nuclear power plant
BUILD_STATION = (1<<6), // base station BUILD_STATION = (1<<6), //! < base station
BUILD_REPAIR = (1<<7), // repair center BUILD_REPAIR = (1<<7), //! < repair center
BUILD_TOWER = (1<<8), // defense tower BUILD_TOWER = (1<<8), //! < defense tower
BUILD_RESEARCH = (1<<9), // research center BUILD_RESEARCH = (1<<9), //! < research center
BUILD_LABO = (1<<10), // laboratory BUILD_LABO = (1<<10), //! < laboratory
BUILD_PARA = (1<<11), // lightning protection BUILD_PARA = (1<<11), //! < lightning protection
BUILD_INFO = (1<<12), // information terminal BUILD_INFO = (1<<12), //! < information terminal
BUILD_GFLAT = (1<<16), // flat floor BUILD_GFLAT = (1<<16), //! < flat floor
BUILD_FLAG = (1<<17) // puts / removes colored flag BUILD_FLAG = (1<<17) //! < puts / removes colored flag
}; };
/**
// Do not change values was because of backups (bits = ...). * \enum ResearchType
* \brief Research actions available to user
*/
enum ResearchType enum ResearchType
{ {
RESEARCH_TANK = (1<<0), // caterpillars RESEARCH_TANK = (1<<0), //! < caterpillars
RESEARCH_FLY = (1<<1), // wings RESEARCH_FLY = (1<<1), //! < wings
RESEARCH_CANON = (1<<2), // cannon RESEARCH_CANON = (1<<2), //! < cannon
RESEARCH_TOWER = (1<<3), // defense tower RESEARCH_TOWER = (1<<3), //! < defense tower
RESEARCH_ATOMIC = (1<<4), // nuclear RESEARCH_ATOMIC = (1<<4), //! < nuclear
RESEARCH_THUMP = (1<<5), // thumper RESEARCH_THUMP = (1<<5), //! < thumper
RESEARCH_SHIELD = (1<<6), // shield RESEARCH_SHIELD = (1<<6), //! < shield
RESEARCH_PHAZER = (1<<7), // phazer gun RESEARCH_PHAZER = (1<<7), //! < phazer gun
RESEARCH_iPAW = (1<<8), // legs of insects RESEARCH_iPAW = (1<<8), //! < legs of insects
RESEARCH_iGUN = (1<<9), // cannon of insects RESEARCH_iGUN = (1<<9), //! < cannon of insects
RESEARCH_RECYCLER = (1<<10), // recycler RESEARCH_RECYCLER = (1<<10), //! < recycler
RESEARCH_SUBM = (1<<11), // submarine RESEARCH_SUBM = (1<<11), //! < submarine
RESEARCH_SNIFFER = (1<<12) // sniffer RESEARCH_SNIFFER = (1<<12) //! < sniffer
}; };
/**
* \enum KeyRank
* \brief Slots for key assignment of user controls
*
* TODO: refactor
*/
enum KeyRank
{
KEYRANK_LEFT = 0,
KEYRANK_RIGHT = 1,
KEYRANK_UP = 2,
KEYRANK_DOWN = 3,
KEYRANK_GUP = 4,
KEYRANK_GDOWN = 5,
KEYRANK_CAMERA = 6,
KEYRANK_DESEL = 7,
KEYRANK_ACTION = 8,
KEYRANK_NEAR = 9,
KEYRANK_AWAY = 10,
KEYRANK_NEXT = 11,
KEYRANK_HUMAN = 12,
KEYRANK_QUIT = 13,
KEYRANK_HELP = 14,
KEYRANK_PROG = 15,
KEYRANK_VISIT = 16,
KEYRANK_SPEED10 = 17,
KEYRANK_SPEED15 = 18,
KEYRANK_SPEED20 = 19,
KEYRANK_SPEED30 = 20,
KEYRANK_AIMUP = 21,
KEYRANK_AIMDOWN = 22,
KEYRANK_CBOT = 23,
};
// TODO: move to CRobotMain
extern long g_id; // unique identifier extern long g_id; // unique identifier
extern long g_build; // constructible buildings extern long g_build; // constructible buildings
extern long g_researchDone; // research done extern long g_researchDone; // research done
extern long g_researchEnable; // research available extern long g_researchEnable; // research available
extern float g_unit; // conversion factor extern float g_unit; // conversion factor

View File

@ -14,7 +14,10 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// image.h /**
* \file common/image.h
* \brief Class for loading and saving images
*/
#pragma once #pragma once

View File

@ -16,12 +16,10 @@
// iman.cpp // iman.cpp
#include <stdio.h>
#include "common/struct.h"
#include "common/iman.h" #include "common/iman.h"
#include <cassert>
template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = nullptr; template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = nullptr;
@ -39,139 +37,91 @@ CInstanceManager* CInstanceManager::GetInstancePointer()
return mInstance; return mInstance;
} }
// Object's constructor.
CInstanceManager::CInstanceManager() CInstanceManager::CInstanceManager()
{ {
int i; for (int i = 0; i < CLASS_MAX; i++)
for ( i=0 ; i<CLASS_MAX ; i++ )
{ {
m_table[i].totalPossible = 0; m_table[i].maxCount = 0;
m_table[i].totalUsed = 0; m_table[i].usedCount = 0;
m_table[i].classPointer = 0; m_table[i].instances = nullptr;
} }
} }
// Object's destructor.
CInstanceManager::~CInstanceManager() CInstanceManager::~CInstanceManager()
{ {
int i; Flush();
for ( i=0 ; i<CLASS_MAX ; i++ )
{
if ( m_table[i].classPointer != 0 )
{
free(m_table[i].classPointer);
}
}
} }
// Empty the list of all classes.
void CInstanceManager::Flush() void CInstanceManager::Flush()
{ {
int i; for (int i = 0; i < CLASS_MAX; i++)
for ( i=0 ; i<CLASS_MAX ; i++ )
{ {
if ( m_table[i].classPointer != 0 ) if (m_table[i].instances != nullptr)
{ delete[] m_table[i].instances;
free(m_table[i].classPointer);
} m_table[i].instances = nullptr;
m_table[i].classPointer = 0;
} }
} }
// Empty all instances of a given class. void CInstanceManager::Flush(ManagedClassType classType)
void CInstanceManager::Flush(ClassType classType)
{ {
if ( classType < 0 || classType >= CLASS_MAX ) return; if (classType < 0 || classType >= CLASS_MAX) return;
if ( m_table[classType].classPointer == 0 ) return; if (m_table[classType].instances == nullptr) return;
free(m_table[classType].classPointer); delete[] m_table[classType].instances;
m_table[classType].classPointer = 0; m_table[classType].instances = nullptr;
} }
bool CInstanceManager::AddInstance(ManagedClassType classType, void* instance, int max)
// Adds a new instance of a class.
bool CInstanceManager::AddInstance(ClassType classType, void* pointer, int max)
{ {
int i; if (classType < 0 || classType >= CLASS_MAX) return false;
if ( classType < 0 || classType >= CLASS_MAX ) return false; if (m_table[classType].instances == nullptr)
if ( m_table[classType].classPointer == 0 )
{ {
m_table[classType].classPointer = static_cast<void**>( malloc(max*sizeof(void*)) ); m_table[classType].instances = new void*[max];
m_table[classType].totalPossible = max; m_table[classType].maxCount = max;
m_table[classType].totalUsed = 0; m_table[classType].usedCount = 0;
} }
if ( m_table[classType].totalUsed >= m_table[classType].totalPossible ) return false; if (m_table[classType].usedCount >= m_table[classType].maxCount) return false;
i = m_table[classType].totalUsed++; int i = m_table[classType].usedCount++;
m_table[classType].classPointer[i] = pointer; m_table[classType].instances[i] = instance;
return true; return true;
} }
// Deletes an instance of a class. bool CInstanceManager::DeleteInstance(ManagedClassType classType, void* instance)
bool CInstanceManager::DeleteInstance(ClassType classType, void* pointer)
{ {
int i; if (classType < 0 || classType >= CLASS_MAX) return false;
if ( classType < 0 || classType >= CLASS_MAX ) return false; for (int i = 0; i < m_table[classType].usedCount; i++)
for ( i=0 ; i<m_table[classType].totalUsed ; i++ )
{ {
if ( m_table[classType].classPointer[i] == pointer ) if (m_table[classType].instances[i] == instance)
{ m_table[classType].instances[i] = nullptr;
m_table[classType].classPointer[i] = 0;
}
} }
Compress(classType); Compress(classType);
return true; return true;
} }
// Seeking an existing instance. Returns 0 if it does not exist. void* CInstanceManager::SearchInstance(ManagedClassType classType, int rank)
// Must be super fast!
void* CInstanceManager::SearchInstance(ClassType classType, int rank)
{ {
#if _DEBUG if (classType < 0 || classType >= CLASS_MAX) return nullptr;
if ( classType < 0 || classType >= CLASS_MAX ) return 0; if (m_table[classType].instances == nullptr) return nullptr;
if ( m_table[classType].classPointer == 0 ) return 0; if (rank >= m_table[classType].usedCount) return nullptr;
#endif
if ( rank >= m_table[classType].totalUsed ) return 0;
return m_table[classType].classPointer[rank]; return m_table[classType].instances[rank];
} }
void CInstanceManager::Compress(ManagedClassType classType)
// Fills holes in a table.
void CInstanceManager::Compress(ClassType classType)
{ {
int i, j; if (classType < 0 || classType >= CLASS_MAX) return;
if ( classType < 0 || classType >= CLASS_MAX ) return; int j = 0;
for (int i = 0; i < m_table[classType].usedCount; i++)
j = 0;
for ( i=0 ; i<m_table[classType].totalUsed ; i++ )
{ {
if ( m_table[classType].classPointer[i] != 0 ) if (m_table[classType].instances[i] != nullptr)
{ m_table[classType].instances[j++] = m_table[classType].instances[i];
m_table[classType].classPointer[j++] = m_table[classType].classPointer[i];
}
} }
m_table[classType].totalUsed = j; m_table[classType].usedCount = j;
} }

View File

@ -14,7 +14,10 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// iman.h /**
* \file iman.h
* \brief Instance manager for managed classes
*/
#pragma once #pragma once
@ -22,36 +25,119 @@
#include <common/misc.h> #include <common/misc.h>
/**
* \enum ManagedClassType
* \brief Type of class managed by CInstanceManager
*/
struct BaseClass // TODO: remove unnecessary, refactor to singletons, move to CRobotMain, keep others?
enum ManagedClassType
{ {
int totalPossible; //! CEventQueue
int totalUsed; CLASS_EVENT = 1,
void** classPointer; //! Ui::CInterface
CLASS_INTERFACE = 2,
//! CRobotMain
CLASS_MAIN = 3,
//! Gfx::CEngine
CLASS_ENGINE = 4,
//! Gfx::CTerrain
CLASS_TERRAIN = 5,
//! CObject
CLASS_OBJECT = 6,
//! CPhysics
CLASS_PHYSICS = 7,
//! CBrain
CLASS_BRAIN = 8,
//! Gfx::CCamera
CLASS_CAMERA = 9,
//! Gfx::CLightManager
CLASS_LIGHT = 10,
//! Gfx::CParticle
CLASS_PARTICULE = 11,
//! CAuto; TODO: remove (unused)
CLASS_AUTO = 12,
//! Ui::CDisplayText
CLASS_DISPLAYTEXT = 13,
//! Gfx::CPyro
CLASS_PYRO = 14,
//! Ui::CScript; TODO: remove (unused)
CLASS_SCRIPT = 15,
//! Gfx::CText
CLASS_TEXT = 16,
//! Ui::CStudio, Ui::CDisplayText; TODO: remove (unused)
CLASS_STUDIO = 17,
//! Gfx::CWater
CLASS_WATER = 18,
//! Gfx::CCloud; TODO: remove (unused)
CLASS_CLOUD = 19,
//! CMotion; TODO: remove (unused)
CLASS_MOTION = 20,
//! CSoundInterface
CLASS_SOUND = 21,
//! Gfx::CPlanet
CLASS_PLANET = 22,
//! CTaskManager; TODO: remove (unused)
CLASS_TASKMANAGER = 23,
//! Ui::CMainDialog; TODO: remove (unused)
CLASS_DIALOG = 24,
//! Ui::CMainMap; TODO: remove (unused)
CLASS_MAP = 25,
//! Ui::CMainShort, CMainMovie; TODO: remove (unused)
CLASS_SHORT = 26,
//! Gfx::CLightning; TODO: remove (unused)
CLASS_BLITZ = 27,
//! Maximum (number of managed classes)
CLASS_MAX = 30
}; };
/**
* \enum ManagedClassInstances
* \brief Instances of class managed by CInstanceManager
*/
struct ManagedClassInstances
{
int maxCount;
int usedCount;
void** instances;
};
/**
* \class CInstanceManager
* \brief Manager for instances of certain classes
*
* Instance manager (often shortened to iMan) allows to register instances of
* classes and search them.
*/
class CInstanceManager : public CSingleton<CInstanceManager> class CInstanceManager : public CSingleton<CInstanceManager>
{ {
public: public:
CInstanceManager(); CInstanceManager();
~CInstanceManager(); ~CInstanceManager();
//! Remove all managed instances
void Flush(); void Flush();
void Flush(ClassType classType); //! Removes instances of one type of class
bool AddInstance(ClassType classType, void* pointer, int max=1); void Flush(ManagedClassType classType);
bool DeleteInstance(ClassType classType, void* pointer); //! Registers new instance of class type
void* SearchInstance(ClassType classType, int rank=0); bool AddInstance(ManagedClassType classType, void* instance, int max=1);
//! Deletes the registered instance of class type
bool DeleteInstance(ManagedClassType classType, void* instance);
//! Seeks a class instance of given type
void* SearchInstance(ManagedClassType classType, int rank=0);
static CInstanceManager& GetInstance(); static CInstanceManager& GetInstance();
static CInstanceManager* GetInstancePointer(); static CInstanceManager* GetInstancePointer();
protected: protected:
void Compress(ClassType classType); //! Fills holes in instance table
void Compress(ManagedClassType classType);
protected: protected:
BaseClass m_table[CLASS_MAX]; ManagedClassInstances m_table[CLASS_MAX];
}; };

View File

@ -14,7 +14,10 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// ioutils.h /**
* \file ioutils.h
* \brief Functions for binary I/O
*/
#pragma once #pragma once

View File

@ -1,54 +0,0 @@
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/.
// language.h
#pragma once
#define _FULL true // CoLoBoT
#define _SCHOOL false // CeeBot-A or Teen
#define _TEEN false // false for CeeBot-A, true for CeeBot-Teen
#define _EDU false
#define _PERSO false
#define _CEEBOTDEMO false
#define _NET false
#define _DEMO false // DEMO only CoLoBoT (with _Full = false)!
#define _FRENCH false
#define _ENGLISH true
#define _GERMAN false
#define _WG false
#define _POLISH false
#define _NEWLOOK false // false for CoLoBoT, true for all CeeBot
#define _SOUNDTRACKS false // always false since InitAudioTrackVolume crop in Vista
// Verifications
#if !_FULL & !_SCHOOL & !_NET & !_DEMO
-> no version chosen!
#endif
#if _SCHOOL
#if !_EDU & !_PERSO & !_CEEBOTDEMO
-> EDU or PERSO or CEEBOTDEMO?
#endif
#if _EDU & _PERSO & _CEEBOTDEMO
-> EDU and PERSO and CEEBOTDEMO not at the same time!!!
#endif
#endif

View File

@ -14,22 +14,20 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// logger.h /**
* \file common/logger.h
* \brief Class for logging information to file or console
*/
#pragma once #pragma once
#include "common/singleton.h"
#include <string> #include <string>
#include <cstdarg> #include <cstdarg>
#include <cstdio> #include <cstdio>
#include <common/singleton.h>
/**
* @file common/logger.h
* @brief Class for loggin information to file or console
*/
/** /**
* \public * \public

View File

@ -16,17 +16,15 @@
// metafile.cpp // metafile.cpp
#include <windows.h>
#include <stdio.h>
#include "common/language.h"
#include "common/metafile.h" #include "common/metafile.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if _FULL | _NET //#if _FULL | _NET
static unsigned char table_codec[23] = static unsigned char table_codec[23] =
{ {
0x85, 0x91, 0x73, 0xcf, 0xa2, 0xbb, 0xf4, 0x77, 0x85, 0x91, 0x73, 0xcf, 0xa2, 0xbb, 0xf4, 0x77,
@ -44,9 +42,9 @@ void Codec(void* buffer, int len, int start)
b[i] ^= table_codec[(start++)%23]; b[i] ^= table_codec[(start++)%23];
} }
} }
#endif //#endif
#if _SCHOOL /*#if _SCHOOL
#if _CEEBOTDEMO #if _CEEBOTDEMO
static unsigned char table_codec[136] = static unsigned char table_codec[136] =
{ {
@ -99,9 +97,9 @@ void Codec(void* buffer, int len, int start)
} }
} }
#endif #endif
#endif #endif*/
#if _DEMO /*#if _DEMO
static unsigned char table_codec[27] = static unsigned char table_codec[27] =
{ {
0x85, 0x91, 0x77, 0xcf, 0xa3, 0xbb, 0xf4, 0x77, 0x85, 0x91, 0x77, 0xcf, 0xa3, 0xbb, 0xf4, 0x77,
@ -120,7 +118,7 @@ void Codec(void* buffer, int len, int start)
b[i] ^= table_codec[(start++)%27]; b[i] ^= table_codec[(start++)%27];
} }
} }
#endif #endif*/
@ -286,7 +284,7 @@ int CMetaFile::Read(void *buffer, int size)
int CMetaFile::GetByte() int CMetaFile::GetByte()
{ {
BYTE b; int b;
if ( !m_bOpen ) return 1; if ( !m_bOpen ) return 1;
@ -303,7 +301,7 @@ int CMetaFile::GetByte()
int CMetaFile::GetWord() int CMetaFile::GetWord()
{ {
WORD w; int w;
if ( !m_bOpen ) return 1; if ( !m_bOpen ) return 1;

View File

@ -17,45 +17,20 @@
// misc.cpp // misc.cpp
#include "common/misc.h"
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <ctype.h> #include <ctype.h>
#include <direct.h>
#include <time.h> #include <time.h>
#include <d3d.h>
#include "common/struct.h"
#include "old/d3dengine.h"
#include "old/d3dmath.h"
#include "old/d3dutil.h"
#include "common/language.h"
#include "common/event.h"
#include "common/misc.h"
CMetaFile g_metafile;
static EventMsg g_uniqueEventMsg = EVENT_USER;
static bool g_bUserDir = false; static bool g_bUserDir = false;
static char g_userDir[100] = ""; static char g_userDir[100] = "";
// Gives a single user event.
EventMsg GetUniqueEventMsg()
{
int i;
i = (int)g_uniqueEventMsg+1;
g_uniqueEventMsg = (EventMsg)i;
return g_uniqueEventMsg;
}
// Returns a non-accented letter. // Returns a non-accented letter.
char RetNoAccent(char letter) char RetNoAccent(char letter)
@ -311,7 +286,8 @@ bool CopyFileToTemp(char* filename)
UserDir(dst, filename, "textures"); UserDir(dst, filename, "textures");
strcpy(g_userDir, save); strcpy(g_userDir, save);
_mkdir("temp"); //_mkdir("temp");
system("mkdir temp");
if ( !Xfer(src, dst) ) return false; if ( !Xfer(src, dst) ) return false;
strcpy(filename, dst); strcpy(filename, dst);
@ -418,24 +394,3 @@ void UserDir(char* buffer, char* dir, char* def)
} }
*buffer = 0; *buffer = 0;
} }
// Returns the letter corresponding to the language.
char RetLanguageLetter()
{
#if _FRENCH
return 'F';
#endif
#if _ENGLISH
return 'E';
#endif
#if _GERMAN | _WG
return 'D';
#endif
#if _POLISH
return 'P';
#endif
return 'X';
}

View File

@ -22,55 +22,7 @@
#include <time.h> #include <time.h>
#include "common/metafile.h" // TODO: to be removed
#include "common/event.h"
#include "common/error_ids.h"
extern CMetaFile g_metafile;
// Existing classes.
enum ClassType
{
CLASS_EVENT = 1,
CLASS_INTERFACE = 2,
CLASS_MAIN = 3,
CLASS_ENGINE = 4,
CLASS_TERRAIN = 5,
CLASS_OBJECT = 6,
CLASS_PHYSICS = 7,
CLASS_BRAIN = 8,
CLASS_CAMERA = 9,
CLASS_LIGHT = 10,
CLASS_PARTICULE = 11,
CLASS_AUTO = 12,
CLASS_DISPLAYTEXT = 13,
CLASS_PYRO = 14,
CLASS_SCRIPT = 15,
CLASS_TEXT = 16,
CLASS_STUDIO = 17,
CLASS_WATER = 18,
CLASS_CLOUD = 19,
CLASS_MOTION = 20,
CLASS_SOUND = 21,
CLASS_PLANET = 22,
CLASS_TASKMANAGER = 23,
CLASS_DIALOG = 24,
CLASS_MAP = 25,
CLASS_SHORT = 26,
CLASS_BLITZ = 27,
};
const int CLASS_MAX = 30;
// Keyboard state.
const int KS_PAGEUP = (1<<4); const int KS_PAGEUP = (1<<4);
const int KS_PAGEDOWN = (1<<5); const int KS_PAGEDOWN = (1<<5);
const int KS_SHIFT = (1<<6); const int KS_SHIFT = (1<<6);
@ -84,10 +36,7 @@ const int KS_NUMRIGHT = (1<<13);
const int KS_NUMPLUS = (1<<14); const int KS_NUMPLUS = (1<<14);
const int KS_NUMMINUS = (1<<15); const int KS_NUMMINUS = (1<<15);
// TODO: rewrite/refactor or remove
// Procedures.
extern EventType GetUniqueEventType();
extern char RetNoAccent(char letter); extern char RetNoAccent(char letter);
extern char RetToUpper(char letter); extern char RetToUpper(char letter);
@ -100,7 +49,3 @@ extern bool CopyFileListToTemp(char* filename, int* list, int total);
extern void AddExt(char* filename, char* ext); extern void AddExt(char* filename, char* ext);
extern void UserDir(bool bUser, char* dir); extern void UserDir(bool bUser, char* dir);
extern void UserDir(char* buffer, char* dir, char* def); extern void UserDir(char* buffer, char* dir, char* def);
extern char RetLanguageLetter();

View File

@ -17,7 +17,10 @@
// profile.cpp // profile.cpp
#include <common/profile.h> #include "common/profile.h"
#include <utility>
#include <cstring>
template<> CProfile* CSingleton<CProfile>::mInstance = nullptr; template<> CProfile* CSingleton<CProfile>::mInstance = nullptr;

View File

@ -14,22 +14,19 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// profile.h /**
* \file common/profile.h
* \brief Class for loading profile (currently for loading ini config file)
*/
#pragma once #pragma once
#include <cstdlib> #include "lib/simpleini/SimpleIni.h"
#include "common/singleton.h"
#include <string>
#include <vector> #include <vector>
#include <utility>
#include <lib/simpleini/SimpleIni.h>
#include <common/singleton.h>
/**
* @file common/profile.h
* @brief Class for loading profile (currently for loading ini config file)
*/
/** /**

View File

@ -14,29 +14,19 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/.// restext.cpp // * along with this program. If not, see http://www.gnu.org/licenses/.// restext.cpp
#include <libintl.h>
#include <SDL/SDL_keyboard.h>
#include "common/struct.h"
#include "common/language.h"
#include "common/misc.h"
#include "common/event.h"
#include "object/object.h"
#include "CBot/resource.h"
#include "common/restext.h" #include "common/restext.h"
#include "common/global.h"
#include "common/event.h"
#include "CBot/resource.h"
#include "object/object.h"
#include <libintl.h>
#include <SDL/SDL_keyboard.h>
// Gives the pointer to the engine.
static CD3DEngine* g_engine;
static char g_gamerName[100]; static char g_gamerName[100];
void SetEngine(CD3DEngine *engine)
{
g_engine = engine;
}
// Give the player's name.
void SetGlobalGamerName(char *name) void SetGlobalGamerName(char *name)
{ {
strcpy(g_gamerName, name); strcpy(g_gamerName, name);

View File

@ -14,30 +14,32 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// restext.h /**
* \file common/restext.h
* \brief Translation and string resource utilities
*/
#pragma once #pragma once
#include "common/global.h"
#include "common/restext_ids.h" #include "common/restext_ids.h"
/**
class CD3DEngine; * \enum ResType
* \brief Types of text resources
// Possible types of the text resources. */
enum ResType enum ResType
{ {
RES_TEXT = 0, // RT_* RES_TEXT = 0, //! < RT_*
RES_EVENT = 1, // EVENT_* (EventMsg) RES_EVENT = 1, //! < EVENT_* (EventMsg)
RES_OBJECT = 2, // OBJECT_* (ObjectType) RES_OBJECT = 2, //! < OBJECT_* (ObjectType)
RES_ERR = 3, // ERR_* (Error) RES_ERR = 3, //! < ERR_* (Error)
RES_KEY = 4, // VK_* (keys) RES_KEY = 4, //! < KEY() (keys)
RES_CBOT = 5, // TX_* (cbot.dll) RES_CBOT = 5, //! < TX_* (CBot)
}; };
extern void SetEngine(CD3DEngine *engine);
extern void SetGlobalGamerName(char *name); extern void SetGlobalGamerName(char *name);
extern bool SearchKey(char *cmd, KeyRank &key); extern bool SearchKey(char *cmd, KeyRank &key);
extern bool GetResource(ResType type, int num, char* text); extern bool GetResource(ResType type, int num, char* text);

View File

@ -14,6 +14,9 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// TODO: move to restext.h after restext rewrite
#pragma once #pragma once
enum ResTextType enum ResTextType

View File

@ -14,7 +14,10 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// singleton.h /**
* \file common/singleton.h
* \brief CSingleton base class for singletons
*/
#pragma once #pragma once

View File

@ -14,7 +14,10 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
// stringutils.h /**
* \file common/stringutils.h
* \brief Some useful string operations
*/
#pragma once #pragma once

View File

@ -1,54 +0,0 @@
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/.
// struct.h
#pragma once
#include <math/vector.h>
#define D3DFVF_VERTEX2 (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2)
struct D3DVERTEX2
{
float x,y,z;
float nx,ny,nz;
float tu, tv;
float tu2, tv2;
D3DVERTEX2() { }
D3DVERTEX2(const Math::Vector& _v, const Math::Vector& _n, float _tu=0.0f, float _tv=0.0f, float _tu2=0.0f, float _tv2=0.0f)
{
x = _v.x;
y = _v.y;
z = _v.z;
nx = _n.x;
ny = _n.y;
nz = _n.z;
tu = _tu;
tv = _tv;
tu2 = _tu2;
tv2 = _tv2;
}
};
struct ColorHSV
{
float h,s,v;
};

View File

@ -22,7 +22,7 @@
#pragma once #pragma once
#include "common/misc.h" #include "common/event.h"
#include "math/vector.h" #include "math/vector.h"

View File

@ -22,7 +22,8 @@
#pragma once #pragma once
#include "common/misc.h" #include "common/event.h"
#include "common/global.h"
#include "graphics/engine/engine.h" #include "graphics/engine/engine.h"
#include "object/object.h" #include "object/object.h"

View File

@ -19,7 +19,7 @@
#pragma once #pragma once
#include "common/misc.h" #include "common/global.h"
#include "object/object.h" #include "object/object.h"
#include "math/vector.h" #include "math/vector.h"