Use std::vector<CCountryFlag> instead of sorted_array

This commit is contained in:
Robert Müller 2022-05-23 19:51:54 +02:00 committed by heinrich5991
parent 295c395c8c
commit 0032f6f2c2
4 changed files with 21 additions and 21 deletions

View file

@ -81,15 +81,15 @@ void CCountryFlags::LoadCountryflagsIndexfile()
str_format(aBuf, sizeof(aBuf), "loaded country flag '%s'", aOrigin);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aBuf);
}
m_aCountryFlags.add_unsorted(CountryFlag);
m_aCountryFlags.push_back(CountryFlag);
}
io_close(File);
m_aCountryFlags.sort_range();
std::sort(m_aCountryFlags.begin(), m_aCountryFlags.end());
// find index of default item
int DefaultIndex = 0, Index = 0;
for(sorted_array<CCountryFlag>::range r = m_aCountryFlags.all(); !r.empty(); r.pop_front(), ++Index)
if(r.front().m_CountryCode == -1)
size_t DefaultIndex = 0;
for(size_t Index = 0; Index < m_aCountryFlags.size(); ++Index)
if(m_aCountryFlags[Index].m_CountryCode == -1)
{
DefaultIndex = Index;
break;
@ -97,11 +97,11 @@ void CCountryFlags::LoadCountryflagsIndexfile()
// init LUT
if(DefaultIndex != 0)
for(int &CodeIndexLUT : m_CodeIndexLUT)
for(size_t &CodeIndexLUT : m_CodeIndexLUT)
CodeIndexLUT = DefaultIndex;
else
mem_zero(m_CodeIndexLUT, sizeof(m_CodeIndexLUT));
for(int i = 0; i < m_aCountryFlags.size(); ++i)
for(size_t i = 0; i < m_aCountryFlags.size(); ++i)
m_CodeIndexLUT[maximum(0, (m_aCountryFlags[i].m_CountryCode - CODE_LB) % CODE_RANGE)] = i;
}
@ -110,13 +110,13 @@ void CCountryFlags::OnInit()
// load country flags
m_aCountryFlags.clear();
LoadCountryflagsIndexfile();
if(!m_aCountryFlags.size())
if(m_aCountryFlags.empty())
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "countryflags", "failed to load country flags. folder='countryflags/'");
CCountryFlag DummyEntry;
DummyEntry.m_CountryCode = -1;
mem_zero(DummyEntry.m_aCountryCodeString, sizeof(DummyEntry.m_aCountryCodeString));
m_aCountryFlags.add(DummyEntry);
m_aCountryFlags.push_back(DummyEntry);
}
m_FlagsQuadContainerIndex = Graphics()->CreateQuadContainer(false);
@ -126,7 +126,7 @@ void CCountryFlags::OnInit()
Graphics()->QuadContainerUpload(m_FlagsQuadContainerIndex);
}
int CCountryFlags::Num() const
size_t CCountryFlags::Num() const
{
return m_aCountryFlags.size();
}
@ -136,9 +136,9 @@ const CCountryFlags::CCountryFlag *CCountryFlags::GetByCountryCode(int CountryCo
return GetByIndex(m_CodeIndexLUT[maximum(0, (CountryCode - CODE_LB) % CODE_RANGE)]);
}
const CCountryFlags::CCountryFlag *CCountryFlags::GetByIndex(int Index) const
const CCountryFlags::CCountryFlag *CCountryFlags::GetByIndex(size_t Index) const
{
return &m_aCountryFlags[maximum(0, Index % m_aCountryFlags.size())];
return &m_aCountryFlags[Index % m_aCountryFlags.size()];
}
void CCountryFlags::Render(int CountryCode, const ColorRGBA *pColor, float x, float y, float w, float h)

View file

@ -2,9 +2,9 @@
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#ifndef GAME_CLIENT_COMPONENTS_COUNTRYFLAGS_H
#define GAME_CLIENT_COMPONENTS_COUNTRYFLAGS_H
#include <base/tl/sorted_array.h>
#include <base/vmath.h>
#include <game/client/component.h>
#include <vector>
class CCountryFlags : public CComponent
{
@ -21,9 +21,9 @@ public:
virtual int Sizeof() const override { return sizeof(*this); }
void OnInit() override;
int Num() const;
size_t Num() const;
const CCountryFlag *GetByCountryCode(int CountryCode) const;
const CCountryFlag *GetByIndex(int Index) const;
const CCountryFlag *GetByIndex(size_t Index) const;
void Render(int CountryCode, const ColorRGBA *pColor, float x, float y, float w, float h);
private:
@ -33,8 +33,8 @@ private:
CODE_UB = 999,
CODE_RANGE = CODE_UB - CODE_LB + 1,
};
sorted_array<CCountryFlag> m_aCountryFlags;
int m_CodeIndexLUT[CODE_RANGE];
std::vector<CCountryFlag> m_aCountryFlags;
size_t m_CodeIndexLUT[CODE_RANGE];
int m_FlagsQuadContainerIndex;

View file

@ -1871,13 +1871,13 @@ int CMenus::Render()
int OldSelected = -1;
UiDoListboxStart(&s_ScrollValue, &Box, 50.0f, Localize("Country / Region"), "", m_pClient->m_CountryFlags.Num(), 6, OldSelected, s_ScrollValue);
for(int i = 0; i < m_pClient->m_CountryFlags.Num(); ++i)
for(size_t i = 0; i < m_pClient->m_CountryFlags.Num(); ++i)
{
const CCountryFlags::CCountryFlag *pEntry = m_pClient->m_CountryFlags.GetByIndex(i);
if(pEntry->m_CountryCode == CurSelection)
OldSelected = i;
CListboxItem Item = UiDoListboxNextItem(&pEntry->m_CountryCode, OldSelected == i);
CListboxItem Item = UiDoListboxNextItem(&pEntry->m_CountryCode, OldSelected >= 0 && (size_t)OldSelected == i);
if(Item.m_Visible)
{
CUIRect Label;

View file

@ -408,13 +408,13 @@ void CMenus::RenderSettingsPlayer(CUIRect MainView)
int OldSelected = -1;
UiDoListboxStart(&s_ScrollValue, &MainView, 50.0f, Localize("Country / Region"), "", m_pClient->m_CountryFlags.Num(), 6, OldSelected, s_ScrollValue);
for(int i = 0; i < m_pClient->m_CountryFlags.Num(); ++i)
for(size_t i = 0; i < m_pClient->m_CountryFlags.Num(); ++i)
{
const CCountryFlags::CCountryFlag *pEntry = m_pClient->m_CountryFlags.GetByIndex(i);
if(pEntry->m_CountryCode == *pCountry)
OldSelected = i;
CListboxItem Item = UiDoListboxNextItem(&pEntry->m_CountryCode, OldSelected == i, s_ListBoxUsed);
CListboxItem Item = UiDoListboxNextItem(&pEntry->m_CountryCode, OldSelected >= 0 && (size_t)OldSelected == i, s_ListBoxUsed);
if(Item.m_Visible)
{
Item.m_Rect.Margin(5.0f, &Item.m_Rect);