Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
C++11/14, Points and Rect used and duplicate code removed
  • Loading branch information
Flamefire committed Jan 21, 2019
1 parent f33510a commit 09d731a
Show file tree
Hide file tree
Showing 28 changed files with 1,049 additions and 1,219 deletions.
156 changes: 78 additions & 78 deletions CDebug.cpp

Large diffs are not rendered by default.

81 changes: 36 additions & 45 deletions CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@ namespace bfs = boost::filesystem;
//#include <vld.h>

CGame::CGame()
: GameResolution(1024, 768), fullscreen(false), Running(true), showLoadScreen(true), Surf_Display(nullptr), Surf_DisplayGL(nullptr)
{
GameResolutionX = 1024;
GameResolutionY = 768;
MenuResolutionX = 640;
MenuResolutionY = 480;
fullscreen = false;
showLoadScreen = true;

#ifdef _ADMINMODE
FrameCounter = 0;
RegisteredCallbacks = 0;
Expand All @@ -32,22 +26,19 @@ CGame::CGame()

msWait = 0;

Surf_Display = nullptr;
Surf_DisplayGL = nullptr;
Running = true;
// mouse cursor data
Cursor.x = 0;
Cursor.y = 0;
Cursor.clicked = false;
Cursor.button.left = false;
Cursor.button.right = false;

for(int i = 0; i < MAXMENUS; i++)
Menus[i] = nullptr;
for(int i = 0; i < MAXWINDOWS; i++)
Windows[i] = nullptr;
for(int i = 0; i < MAXCALLBACKS; i++)
Callbacks[i] = nullptr;
for(auto& Menu : Menus)
Menu = nullptr;
for(auto& Window : Windows)
Window = nullptr;
for(auto& Callback : Callbacks)
Callback = nullptr;
MapObj = nullptr;
}

Expand Down Expand Up @@ -78,35 +69,35 @@ bool CGame::RegisterMenu(CMenu* Menu)
{
bool success = false;

if(Menu == nullptr)
if(!Menu)
return success;
for(int i = 0; i < MAXMENUS; i++)
for(auto& i : Menus)
{
if(!success && Menus[i] == nullptr)
if(!success && !i)
{
Menus[i] = Menu;
Menus[i]->setActive();
i = Menu;
i->setActive();
success = true;
#ifdef _ADMINMODE
RegisteredMenus++;
#endif
} else if(Menus[i] != nullptr)
Menus[i]->setInactive();
} else if(i)
i->setInactive();
}
return success;
}

bool CGame::UnregisterMenu(CMenu* Menu)
{
if(Menu == nullptr)
if(!Menu)
return false;
for(int i = 0; i < MAXMENUS; i++)
{
if(Menus[i] == Menu)
{
for(int j = i - 1; j >= 0; j--)
{
if(Menus[j] != nullptr)
if(Menus[j])
{
Menus[j]->setActive();
break;
Expand All @@ -129,34 +120,34 @@ bool CGame::RegisterWindow(CWindow* Window)
int highestPriority = 0;

// first find the highest priority
for(int i = 0; i < MAXWINDOWS; i++)
for(auto& Window : Windows)
{
if(Windows[i] != nullptr && Windows[i]->getPriority() > highestPriority)
highestPriority = Windows[i]->getPriority();
if(Window && Window->getPriority() > highestPriority)
highestPriority = Window->getPriority();
}

if(Window == nullptr)
if(!Window)
return success;
for(int i = 0; i < MAXWINDOWS; i++)
for(auto& i : Windows)
{
if(!success && Windows[i] == nullptr)
if(!success && !i)
{
Windows[i] = Window;
Windows[i]->setActive();
Windows[i]->setPriority(highestPriority + 1);
i = Window;
i->setActive();
i->setPriority(highestPriority + 1);
success = true;
#ifdef _ADMINMODE
RegisteredWindows++;
#endif
} else if(Windows[i] != nullptr)
Windows[i]->setInactive();
} else if(i)
i->setInactive();
}
return success;
}

bool CGame::UnregisterWindow(CWindow* Window)
{
if(Window == nullptr)
if(!Window)
return false;
for(int i = 0; i < MAXWINDOWS; i++)
{
Expand All @@ -183,13 +174,13 @@ bool CGame::UnregisterWindow(CWindow* Window)

bool CGame::RegisterCallback(void (*callback)(int))
{
if(callback == nullptr)
if(!callback)
return false;
for(int i = 0; i < MAXCALLBACKS; i++)
for(auto& Callback : Callbacks)
{
if(Callbacks[i] == nullptr)
if(!Callback)
{
Callbacks[i] = callback;
Callback = callback;
#ifdef _ADMINMODE
RegisteredCallbacks++;
#endif
Expand All @@ -201,13 +192,13 @@ bool CGame::RegisterCallback(void (*callback)(int))

bool CGame::UnregisterCallback(void (*callback)(int))
{
if(callback == nullptr)
if(!callback)
return false;
for(int i = 0; i < MAXCALLBACKS; i++)
for(auto& Callback : Callbacks)
{
if(Callbacks[i] == callback)
if(Callback == callback)
{
Callbacks[i] = nullptr;
Callback = nullptr;
#ifdef _ADMINMODE
RegisteredCallbacks--;
#endif
Expand Down
10 changes: 3 additions & 7 deletions CGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ class CGame
friend class CDebug;

public:
int GameResolutionX;
int GameResolutionY;
// MenuResolution is old and not used anymore
int MenuResolutionX;
int MenuResolutionY;
Extent GameResolution;
bool fullscreen;

bool Running;
Expand Down Expand Up @@ -63,6 +59,7 @@ class CGame
int Execute();

bool Init();
bool ReCreateWindow();

void EventHandling(SDL_Event* Event);

Expand All @@ -83,8 +80,7 @@ class CGame
void delMapObj();
SDL_Surface* getDisplaySurface() { return Surf_Display; };
SDL_Surface* getDisplayGLSurface() { return Surf_DisplayGL; };
int getResX() { return GameResolutionX; }
int getResY() { return GameResolutionY; }
auto getRes() { return GameResolution; }
};

#endif
18 changes: 9 additions & 9 deletions CGame_Cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@
void CGame::Cleanup()
{
// unregister menus
for(int i = 0; i < MAXMENUS; i++)
for(auto& Menu : Menus)
{
if(Menus[i] != nullptr)
CSurface::Draw(Surf_Display, Menus[i]->getSurface(), 0, 0);
if(Menu)
CGame::UnregisterMenu(Menu);
}
// unregister windows
for(int i = 0; i < MAXWINDOWS; i++)
for(auto& Window : Windows)
{
if(Windows[i] != nullptr)
CSurface::Draw(Surf_Display, Windows[i]->getSurface(), 0, 0);
if(Window)
CGame::UnregisterWindow(Window);
}

// free all picture surfaces
for(int i = 0; i < MAXBOBBMP; i++)
{
if(global::bmpArray[i].surface != nullptr)
if(global::bmpArray[i].surface)
SDL_FreeSurface(global::bmpArray[i].surface);
}
// free all shadow surfaces
for(int i = 0; i < MAXBOBSHADOW; i++)
{
if(global::shadowArray[i].surface != nullptr)
if(global::shadowArray[i].surface)
SDL_FreeSurface(global::shadowArray[i].surface);
}

SDL_FreeSurface(Surf_Display);
if(Surf_DisplayGL != nullptr)
if(Surf_DisplayGL)
SDL_FreeSurface(Surf_DisplayGL);

SDL_Quit();
Expand Down
Loading

0 comments on commit 09d731a

Please sign in to comment.