avoid vectors as public static global members

this can cause very bad initialization order issues.
making them private helps the compiler figure out better init order
This commit is contained in:
Tim Felgentreff 2022-03-15 23:21:58 +01:00
parent fecaaa9808
commit 617b5f0652
7 changed files with 81 additions and 19 deletions

View file

@ -71,7 +71,11 @@ class CUnitColors
public:
CUnitColors() {}
public:
void Clear();
void Set(std::vector<CColor> &colors);
private:
std::vector<CColor> Colors;
};

View file

@ -160,10 +160,6 @@ public:
IntColor Color; /// color of units on minimap
CUnitColors UnitColors; /// Unit colors for new units
std::vector<CUnit *> FreeWorkers; /// Container for free workers
// Upgrades/Allows:
CAllow Allow; /// Allowed for player
CUpgradeTimers UpgradeTimers; /// Timer for the upgrades
@ -187,8 +183,19 @@ public:
void AddUnit(CUnit &unit);
void RemoveUnit(CUnit &unit);
std::vector<CUnit *>::const_iterator FreeWorkersBegin() const;
std::vector<CUnit *>::const_iterator FreeWorkersEnd() const;
std::vector<CUnit *>::iterator FreeWorkersBegin();
std::vector<CUnit *>::iterator FreeWorkersEnd();
CUnit *GetFreeWorker(int index) const;
int GetFreeWorkersCount() const;
void UpdateFreeWorkers();
void ClearUnitColors();
void SetUnitColors(std::vector<CColor> &colors);
/// Get a resource of the player
int GetResource(const int resource, const int type);
/// Adds/subtracts some resources to/from the player store
@ -289,7 +296,9 @@ public:
void SetRevealed(const bool revealed);
private:
CUnitColors UnitColors; /// Unit colors for new units
std::vector<CUnit *> Units; /// units of this player
std::vector<CUnit *> FreeWorkers; /// Container for free workers
unsigned int Enemy; /// enemy bit field for this player
unsigned int Allied; /// allied bit field for this player
std::set<uint8_t> HasVisionFrom; /// set of player indexes that have shared their vision with this player

View file

@ -394,7 +394,7 @@ void CleanPlayers()
void FreePlayerColors()
{
for (int i = 0; i < PlayerMax; ++i) {
Players[i].UnitColors.Colors.clear();
Players[i].ClearUnitColors();
}
PlayerColorsRGB.clear();
}
@ -850,6 +850,36 @@ void CPlayer::RemoveUnit(CUnit &unit)
Assert(last == &unit || this->Units[last->PlayerSlot] == last);
}
std::vector<CUnit *>::const_iterator CPlayer::FreeWorkersBegin() const
{
return FreeWorkers.begin();
}
std::vector<CUnit *>::iterator CPlayer::FreeWorkersBegin()
{
return FreeWorkers.begin();
}
std::vector<CUnit *>::const_iterator CPlayer::FreeWorkersEnd() const
{
return FreeWorkers.end();
}
std::vector<CUnit *>::iterator CPlayer::FreeWorkersEnd()
{
return FreeWorkers.end();
}
CUnit *CPlayer::GetFreeWorker(int index) const
{
return FreeWorkers[index];
}
int CPlayer::GetFreeWorkersCount() const
{
return static_cast<int>(FreeWorkers.size());
}
void CPlayer::UpdateFreeWorkers()
{
FreeWorkers.clear();
@ -871,7 +901,6 @@ void CPlayer::UpdateFreeWorkers()
}
}
std::vector<CUnit *>::const_iterator CPlayer::UnitBegin() const
{
return Units.begin();
@ -1272,7 +1301,7 @@ void GraphicPlayerPixels(int colorIndex, const CGraphic &sprite)
void SetPlayersPalette()
{
for (int i = 0; i < PlayerMax; ++i) {
Players[i].UnitColors.Colors = PlayerColorsRGB[i];
Players[i].SetUnitColors(PlayerColorsRGB[i]);
}
}
@ -1493,4 +1522,14 @@ bool CPlayer::IsTeamed(const CUnit &unit) const
return this->IsTeamed(*unit.Player);
}
void CPlayer::ClearUnitColors()
{
UnitColors.Clear();
}
void CPlayer::SetUnitColors(std::vector<CColor> &colors)
{
UnitColors.Set(colors);
}
//@}

View file

@ -449,18 +449,18 @@ void UiToggleTerrain()
*/
void UiFindIdleWorker()
{
if (ThisPlayer->FreeWorkers.empty()) {
if (ThisPlayer->GetFreeWorkersCount() == 0) {
return;
}
CUnit *unit = ThisPlayer->FreeWorkers[0];
CUnit *unit = ThisPlayer->GetFreeWorker(0);
if (LastIdleWorker) {
const std::vector<CUnit *> &freeWorkers = ThisPlayer->FreeWorkers;
std::vector<CUnit *>::const_iterator it = std::find(freeWorkers.begin(),
freeWorkers.end(),
LastIdleWorker);
if (it != ThisPlayer->FreeWorkers.end()) {
if (*it != ThisPlayer->FreeWorkers.back()) {
unit = *(++it);
bool found = false;
for(auto it = ThisPlayer->FreeWorkersBegin(); it != ThisPlayer->FreeWorkersEnd(); ++it) {
if (found) {
unit = *it;
break;
} else if (*it == unit) {
found = true;
}
}
}

View file

@ -619,7 +619,7 @@ void DrawResources()
label.Draw(UI.Resources[ScoreCost].TextX, UI.Resources[ScoreCost].TextY + (score > 99999) * 3, score);
}
if (UI.Resources[FreeWorkersCount].TextX != -1) {
const int workers = ThisPlayer->FreeWorkers.size();
const int workers = ThisPlayer->GetFreeWorkersCount();
int textX = UI.Resources[FreeWorkersCount].TextX;
if (textX < 0) {
// XXX: this is hacky, but what use is that bit otherwise

View file

@ -719,7 +719,7 @@ static void HandleMouseOn(const PixelPos screenPos)
CGraphic* g = UI.Resources[FreeWorkersCount].G;
if (g && (x = UI.Resources[FreeWorkersCount].IconX) && (y = UI.Resources[FreeWorkersCount].IconY)) {
int textX = UI.Resources[FreeWorkersCount].TextX;
if (textX > 0 || ThisPlayer->FreeWorkers.size() > 0) {
if (textX > 0 || ThisPlayer->GetFreeWorkersCount() > 0) {
if (screenPos > PixelPos(x, y) && screenPos < PixelPos(x + g->getWidth(), y + g->getHeight())) {
ButtonAreaUnderCursor = ButtonAreaMenu;
ButtonUnderCursor = ButtonUnderFreeWorkers;

View file

@ -67,6 +67,16 @@ void CColor::Parse(lua_State *l, const int offset)
this->A = 255;
}
void CUnitColors::Clear()
{
Colors.clear();
}
void CUnitColors::Set(std::vector<CColor> &colors)
{
Colors = colors;
}
IntColor InterpolateColor(IntColor color1, IntColor color2, float fraction)
{
unsigned char r1 = (color1 >> 16) & 0xff;