2011-03-16 11:09:22 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
|
|
#include <base/math.h>
|
|
|
|
#include <base/system.h>
|
|
|
|
|
|
|
|
#include <engine/console.h>
|
|
|
|
#include <engine/graphics.h>
|
2011-12-10 17:23:29 +00:00
|
|
|
#include <engine/shared/config.h>
|
2011-03-16 11:09:22 +00:00
|
|
|
#include <engine/shared/linereader.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <engine/storage.h>
|
2011-03-16 11:09:22 +00:00
|
|
|
|
|
|
|
#include "countryflags.h"
|
|
|
|
|
2021-09-13 21:14:04 +00:00
|
|
|
#include <game/client/render.h>
|
|
|
|
|
2011-03-16 11:09:22 +00:00
|
|
|
void CCountryFlags::LoadCountryflagsIndexfile()
|
|
|
|
{
|
2021-12-17 20:58:28 +00:00
|
|
|
IOHANDLE File = Storage()->OpenFile("countryflags/index.txt", IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_ALL);
|
2011-03-16 11:09:22 +00:00
|
|
|
if(!File)
|
|
|
|
{
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", "couldn't open index file");
|
|
|
|
return;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-16 11:09:22 +00:00
|
|
|
char aOrigin[128];
|
|
|
|
CLineReader LineReader;
|
|
|
|
LineReader.Init(File);
|
|
|
|
char *pLine;
|
|
|
|
while((pLine = LineReader.Get()))
|
|
|
|
{
|
|
|
|
if(!str_length(pLine) || pLine[0] == '#') // skip empty lines and comments
|
|
|
|
continue;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-16 11:09:22 +00:00
|
|
|
str_copy(aOrigin, pLine, sizeof(aOrigin));
|
|
|
|
char *pReplacement = LineReader.Get();
|
|
|
|
if(!pReplacement)
|
|
|
|
{
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", "unexpected end of index file");
|
|
|
|
break;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-16 11:09:22 +00:00
|
|
|
if(pReplacement[0] != '=' || pReplacement[1] != '=' || pReplacement[2] != ' ')
|
|
|
|
{
|
|
|
|
char aBuf[128];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "malform replacement for index '%s'", aOrigin);
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aBuf);
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
int CountryCode = str_toint(pReplacement + 3);
|
2011-06-29 20:27:32 +00:00
|
|
|
if(CountryCode < CODE_LB || CountryCode > CODE_UB)
|
|
|
|
{
|
|
|
|
char aBuf[128];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "country code '%i' not within valid code range [%i..%i]", CountryCode, CODE_LB, CODE_UB);
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aBuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-16 11:09:22 +00:00
|
|
|
// load the graphic file
|
|
|
|
char aBuf[128];
|
|
|
|
CImageInfo Info;
|
2021-06-07 23:06:07 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "countryflags/%s.png", aOrigin);
|
|
|
|
if(!Graphics()->LoadPNG(&Info, aBuf, IStorage::TYPE_ALL))
|
2011-03-16 11:09:22 +00:00
|
|
|
{
|
2021-06-07 23:06:07 +00:00
|
|
|
char aMsg[128];
|
|
|
|
str_format(aMsg, sizeof(aMsg), "failed to load '%s'", aBuf);
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aMsg);
|
|
|
|
continue;
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add entry
|
|
|
|
CCountryFlag CountryFlag;
|
2011-06-29 20:27:32 +00:00
|
|
|
CountryFlag.m_CountryCode = CountryCode;
|
2011-07-31 00:04:46 +00:00
|
|
|
str_copy(CountryFlag.m_aCountryCodeString, aOrigin, sizeof(CountryFlag.m_aCountryCodeString));
|
2022-03-22 19:24:27 +00:00
|
|
|
CountryFlag.m_Texture = Graphics()->LoadTextureRaw(Info.m_Width, Info.m_Height, Info.m_Format, Info.m_pData, Info.m_Format, 0, aOrigin);
|
2021-06-07 23:06:07 +00:00
|
|
|
Graphics()->FreePNG(&Info);
|
2012-08-12 10:41:50 +00:00
|
|
|
|
2011-12-10 17:23:29 +00:00
|
|
|
if(g_Config.m_Debug)
|
|
|
|
{
|
|
|
|
str_format(aBuf, sizeof(aBuf), "loaded country flag '%s'", aOrigin);
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aBuf);
|
|
|
|
}
|
2022-06-11 19:38:18 +00:00
|
|
|
m_vCountryFlags.push_back(CountryFlag);
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
|
|
|
io_close(File);
|
2022-06-11 19:38:18 +00:00
|
|
|
std::sort(m_vCountryFlags.begin(), m_vCountryFlags.end());
|
2011-06-29 20:27:32 +00:00
|
|
|
|
2012-02-05 14:53:22 +00:00
|
|
|
// find index of default item
|
2022-05-23 17:51:54 +00:00
|
|
|
size_t DefaultIndex = 0;
|
2022-06-11 19:38:18 +00:00
|
|
|
for(size_t Index = 0; Index < m_vCountryFlags.size(); ++Index)
|
|
|
|
if(m_vCountryFlags[Index].m_CountryCode == -1)
|
2012-02-05 14:53:22 +00:00
|
|
|
{
|
|
|
|
DefaultIndex = Index;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-09 00:08:14 +00:00
|
|
|
|
2012-02-05 14:53:22 +00:00
|
|
|
// init LUT
|
|
|
|
if(DefaultIndex != 0)
|
2022-05-23 17:51:54 +00:00
|
|
|
for(size_t &CodeIndexLUT : m_CodeIndexLUT)
|
2020-10-26 14:14:07 +00:00
|
|
|
CodeIndexLUT = DefaultIndex;
|
2012-02-05 14:53:22 +00:00
|
|
|
else
|
|
|
|
mem_zero(m_CodeIndexLUT, sizeof(m_CodeIndexLUT));
|
2022-06-11 19:38:18 +00:00
|
|
|
for(size_t i = 0; i < m_vCountryFlags.size(); ++i)
|
|
|
|
m_CodeIndexLUT[maximum(0, (m_vCountryFlags[i].m_CountryCode - CODE_LB) % CODE_RANGE)] = i;
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCountryFlags::OnInit()
|
|
|
|
{
|
|
|
|
// load country flags
|
2022-06-11 19:38:18 +00:00
|
|
|
m_vCountryFlags.clear();
|
2011-03-16 11:09:22 +00:00
|
|
|
LoadCountryflagsIndexfile();
|
2022-06-11 19:38:18 +00:00
|
|
|
if(m_vCountryFlags.empty())
|
2011-03-16 11:09:22 +00:00
|
|
|
{
|
|
|
|
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "countryflags", "failed to load country flags. folder='countryflags/'");
|
|
|
|
CCountryFlag DummyEntry;
|
|
|
|
DummyEntry.m_CountryCode = -1;
|
2011-08-01 19:03:51 +00:00
|
|
|
mem_zero(DummyEntry.m_aCountryCodeString, sizeof(DummyEntry.m_aCountryCodeString));
|
2022-06-11 19:38:18 +00:00
|
|
|
m_vCountryFlags.push_back(DummyEntry);
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
2021-09-13 21:14:04 +00:00
|
|
|
|
|
|
|
m_FlagsQuadContainerIndex = Graphics()->CreateQuadContainer(false);
|
|
|
|
Graphics()->SetColor(1.f, 1.f, 1.f, 1.f);
|
|
|
|
Graphics()->QuadsSetSubset(0, 0, 1, 1);
|
|
|
|
RenderTools()->QuadContainerAddSprite(m_FlagsQuadContainerIndex, 0, 0, 1, 1);
|
|
|
|
Graphics()->QuadContainerUpload(m_FlagsQuadContainerIndex);
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 17:51:54 +00:00
|
|
|
size_t CCountryFlags::Num() const
|
2011-03-16 11:09:22 +00:00
|
|
|
{
|
2022-06-11 19:38:18 +00:00
|
|
|
return m_vCountryFlags.size();
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 20:27:32 +00:00
|
|
|
const CCountryFlags::CCountryFlag *CCountryFlags::GetByCountryCode(int CountryCode) const
|
2011-03-16 11:09:22 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
return GetByIndex(m_CodeIndexLUT[maximum(0, (CountryCode - CODE_LB) % CODE_RANGE)]);
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 17:51:54 +00:00
|
|
|
const CCountryFlags::CCountryFlag *CCountryFlags::GetByIndex(size_t Index) const
|
2011-03-16 11:09:22 +00:00
|
|
|
{
|
2022-06-11 19:38:18 +00:00
|
|
|
return &m_vCountryFlags[Index % m_vCountryFlags.size()];
|
2011-03-16 11:09:22 +00:00
|
|
|
}
|
2012-01-08 17:16:38 +00:00
|
|
|
|
2019-04-26 21:47:34 +00:00
|
|
|
void CCountryFlags::Render(int CountryCode, const ColorRGBA *pColor, float x, float y, float w, float h)
|
2012-01-08 17:16:38 +00:00
|
|
|
{
|
|
|
|
const CCountryFlag *pFlag = GetByCountryCode(CountryCode);
|
2012-08-12 12:02:50 +00:00
|
|
|
if(pFlag->m_Texture.IsValid())
|
2012-01-08 17:16:38 +00:00
|
|
|
{
|
|
|
|
Graphics()->TextureSet(pFlag->m_Texture);
|
2021-09-13 21:14:04 +00:00
|
|
|
Graphics()->SetColor(*pColor);
|
|
|
|
Graphics()->RenderQuadContainerEx(m_FlagsQuadContainerIndex, 0, -1, x, y, w, h);
|
2012-01-08 17:16:38 +00:00
|
|
|
}
|
|
|
|
}
|