Skip to content

Commit

Permalink
Merge pull request #539 from Wargus/clean-up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 12, 2023
2 parents d38c65b + 655494b commit 5938c70
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 185 deletions.
19 changes: 4 additions & 15 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "version.h"
#include "video.h"

#include <memory>
#include <SDL_image.h>

extern void CleanGame();
Expand All @@ -99,7 +100,6 @@ bool UseHPForXp = false; /// true if gain XP by dealing damage, fal
----------------------------------------------------------------------------*/

extern gcn::Gui *Gui;
static std::vector<gcn::Container *> Containers;

/**
** Save game settings.
Expand All @@ -120,14 +120,11 @@ void CreateGame(const fs::path &filename, CMap *map);

void StartMap(const std::string &filename, bool clean)
{
std::string nc, rc;

gcn::Widget *oldTop = Gui->getTop();
gcn::Container *container = new gcn::Container();
Containers.push_back(container);
auto container = std::make_unique<gcn::Container>();
container->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height));
container->setOpaque(false);
Gui->setTop(container);
Gui->setTop(container.get());

NetConnectRunning = 0;
InterfaceState = IfaceState::Normal;
Expand All @@ -137,6 +134,7 @@ void StartMap(const std::string &filename, bool clean)
if (clean) {
CleanPlayers();
}
std::string nc, rc;
GetDefaultTextColors(nc, rc);

CreateGame(filename, &Map);
Expand All @@ -156,15 +154,6 @@ void StartMap(const std::string &filename, bool clean)
SetDefaultTextColors(nc, rc);

Gui->setTop(oldTop);
ranges::erase(Containers, container);
delete container;
}

void FreeAllContainers()
{
for (auto *p : Containers) {
delete p;
}
}

/*----------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions src/include/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ extern void LuaRegisterModules(); /// Register lua script of each modules
extern void LoadModules(); /// Load all modules
extern void CleanModules(); /// Cleanup all modules

extern void FreeAllContainers();

extern void SaveGameSettings(CFile &file); /// Save game settings

extern std::string GameName; /// Name of the game
Expand Down
5 changes: 2 additions & 3 deletions src/include/iolib.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class CFile
public:
CFile();
~CFile();
CFile(const CFile &) = delete;
const CFile &operator = (const CFile &) = delete;

int open(const char *name, long flags);
int close();
Expand All @@ -117,9 +119,6 @@ class CFile
static SDL_RWops *to_SDL_RWops(std::unique_ptr<CFile> file);

int printf(const char *format, ...) PRINTF_VAARG_ATTRIBUTE(2, 3); // Don't forget to count this
private:
CFile(const CFile &rhs); // No implementation
const CFile &operator = (const CFile &rhs); // No implementation
private:
class PImpl;
PImpl *pimpl;
Expand Down
3 changes: 2 additions & 1 deletion src/include/spells.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "unitsound.h"
#include "vec2i.h"

#include <memory>
#include <string_view>
#include <variant>

Expand Down Expand Up @@ -237,7 +238,7 @@ class SpellType
/**
** Define the names and effects of all available spells.
*/
extern std::vector<SpellType *> SpellTypeTable;
extern std::vector<std::unique_ptr<SpellType>> SpellTypeTable;


/*----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions src/include/upgrade.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

//@{

#include <memory>

/*----------------------------------------------------------------------------
-- Declarations
----------------------------------------------------------------------------*/
Expand All @@ -50,7 +52,7 @@ class CUpgradeModifier;
/// How many upgrades modifiers supported
#define UPGRADE_MODIFIERS_MAX (UpgradeMax * 4)

extern CUpgradeModifier *UpgradeModifiers[UPGRADE_MODIFIERS_MAX];
extern std::unique_ptr<CUpgradeModifier> UpgradeModifiers[UPGRADE_MODIFIERS_MAX];

extern int NumUpgradeModifiers;

Expand Down Expand Up @@ -88,7 +90,7 @@ extern void UpgradeLost(CPlayer &player, int id);
/// Apply researched upgrades when map is loading
extern void ApplyUpgrades();

extern void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier *um); /// Apply upgrade modifier of an individual upgrade
extern void ApplyIndividualUpgradeModifier(CUnit &unit, const CUpgradeModifier &um); /// Apply upgrade modifier of an individual upgrade
extern void IndividualUpgradeAcquire(CUnit &unit, const CUpgrade *upgrade); /// Make a unit acquire in individual upgrade
extern void IndividualUpgradeLost(CUnit &unit, const CUpgrade *upgrade); /// Make a unit lose in individual upgrade

Expand Down
2 changes: 1 addition & 1 deletion src/include/upgrade_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CUnitStats
CUnitStats() = default;
~CUnitStats() = default;

CUnitStats &operator=(const CUnitStats &rhs);
CUnitStats &operator=(const CUnitStats &) = default;

bool operator == (const CUnitStats &rhs) const;
bool operator != (const CUnitStats &rhs) const;
Expand Down
10 changes: 5 additions & 5 deletions src/spell/script_spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,19 @@ static int CclDefineSpell(lua_State *l)
const auto it = ranges::find(SpellTypeTable, identname, &SpellType::Ident);
SpellType *spell = nullptr;
if (it != SpellTypeTable.end()) {
spell = *it;
spell = it->get();
DebugPrint("Redefining spell-type '%s'\n", identname.data());
} else {
spell = new SpellType(SpellTypeTable.size(), std::string{identname});
SpellTypeTable.push_back(std::make_unique<SpellType>(SpellTypeTable.size(), std::string{identname}));
spell = SpellTypeTable.back().get();
for (CUnitType *unitType : UnitTypes) { // adjust array for caster already defined
if (!unitType->CanCastSpell.empty()) {
unitType->CanCastSpell.resize(SpellTypeTable.size() + 1);
unitType->CanCastSpell.resize(SpellTypeTable.size());
}
if (!unitType->AutoCastActive.empty()) {
unitType->AutoCastActive.resize(SpellTypeTable.size() + 1);
unitType->AutoCastActive.resize(SpellTypeTable.size());
}
}
SpellTypeTable.push_back(spell);
}
for (int i = 1; i < args; ++i) {
std::string_view value = LuaToString(l, i + 1);
Expand Down
5 changes: 1 addition & 4 deletions src/spell/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
/**
** Define the names and effects of all im play available spells.
*/
std::vector<SpellType *> SpellTypeTable;
std::vector<std::unique_ptr<SpellType>> SpellTypeTable;


/*----------------------------------------------------------------------------
Expand Down Expand Up @@ -561,9 +561,6 @@ int SpellCast(CUnit &caster, const SpellType &spell, CUnit *target, const Vec2i
void CleanSpells()
{
DebugPrint("Cleaning spells.\n");
for (SpellType *spell : SpellTypeTable) {
delete spell;
}
SpellTypeTable.clear();
}

Expand Down
6 changes: 2 additions & 4 deletions src/stratagus/iolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class CFile::PImpl
public:
PImpl();
~PImpl();
PImpl(const PImpl &) = delete;
const PImpl &operator=(const PImpl &) = delete;

int open(const char *name, long flags);
int close();
Expand All @@ -80,10 +82,6 @@ class CFile::PImpl
long tell();
int write(const void *buf, size_t len);

private:
PImpl(const PImpl &rhs); // No implementation
const PImpl &operator = (const PImpl &rhs); // No implementation

private:
ClfType cl_type; /// type of CFile
FILE *cl_plain; /// standard file pointer
Expand Down
1 change: 0 additions & 1 deletion src/stratagus/stratagus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ void Exit(int err)
FreeGraphics();
FreePlayerColors();
FreeButtonStyles();
FreeAllContainers();
freeGuichan();
fprintf(stdout, "Frames %lu, Slow frames %ld = %ld%%\n",
FrameCounter, SlowFrameCounter,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ static int SendSpellCast(const Vec2i &tilePos)
continue;
}
// CursorValue here holds the spell type id
const SpellType *spell = SpellTypeTable[CursorValue];
const SpellType *spell = SpellTypeTable.at(CursorValue).get();
if (!spell) {
fprintf(stderr, "unknown spell-id: %d\n", CursorValue);
ExitFatal(1);
Expand Down
8 changes: 4 additions & 4 deletions src/ui/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ int CPopupContentTypeCosts::GetWidth(const ButtonAction &button, int *Costs) con
}
if (Costs[ManaResCost]) {
const CGraphic *G = UI.Resources[ManaResCost].G;
const SpellType *spell = SpellTypeTable[button.Value];
const SpellType &spell = *SpellTypeTable.at(button.Value);

if (spell->ManaCost) {
if (spell.ManaCost) {
popupWidth = 10;
if (UI.Resources[ManaResCost].IconWidth != -1) {
popupWidth += (UI.Resources[ManaResCost].IconWidth + 5);
Expand All @@ -259,8 +259,8 @@ int CPopupContentTypeCosts::GetWidth(const ButtonAction &button, int *Costs) con
popupWidth += (G->Width + 5);
}
}
popupWidth += font.Width(spell->ManaCost);
popupWidth = std::max<int>(popupWidth, font.Width(spell->Name) + 10);
popupWidth += font.Width(spell.ManaCost);
popupWidth = std::max<int>(popupWidth, font.Width(spell.Name) + 10);
} else {
popupWidth = font.Width(button.Hint) + 10;
}
Expand Down
2 changes: 1 addition & 1 deletion src/unit/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ void CUnit::ChangeOwner(CPlayer &newplayer)
//apply the upgrades of the new player, if the old one doesn't have that upgrade
for (int z = 0; z < NumUpgradeModifiers; ++z) {
if (oldplayer->Allow.Upgrades[UpgradeModifiers[z]->UpgradeId] != 'R' && newplayer.Allow.Upgrades[UpgradeModifiers[z]->UpgradeId] == 'R' && UpgradeModifiers[z]->ApplyTo[Type->Slot] == 'X') { //if the old player doesn't have the modifier's upgrade, and the upgrade is applicable to the unit
ApplyIndividualUpgradeModifier(*this, UpgradeModifiers[z]); //apply the upgrade to this unit only
ApplyIndividualUpgradeModifier(*this, *UpgradeModifiers[z]); //apply the upgrade to this unit only
}
}

Expand Down
Loading

0 comments on commit 5938c70

Please sign in to comment.