diff options
| author | oy <Tom_Adams@web.de> | 2011-03-16 12:09:22 +0100 |
|---|---|---|
| committer | oy <Tom_Adams@web.de> | 2011-03-16 12:09:22 +0100 |
| commit | 156e23b5923af300595704bb4fe6999deb4871a8 (patch) | |
| tree | 47c20236a5bcc3493c795c3d6e638b2893ea0f49 /src/game/client/components/countryflags.cpp | |
| parent | 30d9c9f4d9e46fec403f3fa810898017a18e93b6 (diff) | |
| download | zcatch-156e23b5923af300595704bb4fe6999deb4871a8.tar.gz zcatch-156e23b5923af300595704bb4fe6999deb4871a8.zip | |
added loading/support for country flags to the client
Diffstat (limited to 'src/game/client/components/countryflags.cpp')
| -rw-r--r-- | src/game/client/components/countryflags.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/game/client/components/countryflags.cpp b/src/game/client/components/countryflags.cpp new file mode 100644 index 00000000..fd6e31d1 --- /dev/null +++ b/src/game/client/components/countryflags.cpp @@ -0,0 +1,105 @@ +/* (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> +#include <engine/storage.h> +#include <engine/shared/linereader.h> + +#include "countryflags.h" + + +void CCountryFlags::LoadCountryflagsIndexfile() +{ + IOHANDLE File = Storage()->OpenFile("countryflags/index.txt", IOFLAG_READ, IStorage::TYPE_ALL); + if(!File) + { + Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", "couldn't open index file"); + return; + } + + 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; + + 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; + } + + 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; + } + + // load the graphic file + char aBuf[128]; + str_format(aBuf, sizeof(aBuf), "countryflags/%s.png", aOrigin); + CImageInfo Info; + if(!Graphics()->LoadPNG(&Info, aBuf, IStorage::TYPE_ALL)) + { + char aMsg[128]; + str_format(aMsg, sizeof(aMsg), "failed to load '%s'", aBuf); + Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aMsg); + continue; + } + + // add entry + CCountryFlag CountryFlag; + CountryFlag.m_CountryCode = str_toint(pReplacement); + CountryFlag.m_Texture = Graphics()->LoadTextureRaw(Info.m_Width, Info.m_Height, Info.m_Format, Info.m_pData, Info.m_Format, 0); + mem_free(Info.m_pData); + str_format(aBuf, sizeof(aBuf), "loaded country flag '%s'", aOrigin); + Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "countryflags", aBuf); + m_aCountryFlags.add(CountryFlag); + } + io_close(File); +} + +void CCountryFlags::OnInit() +{ + // load country flags + m_aCountryFlags.clear(); + LoadCountryflagsIndexfile(); + if(!m_aCountryFlags.size()) + { + Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "countryflags", "failed to load country flags. folder='countryflags/'"); + CCountryFlag DummyEntry; + DummyEntry.m_CountryCode = -1; + DummyEntry.m_Texture = -1; + m_aCountryFlags.add(DummyEntry); + } +} + +int CCountryFlags::Num() const +{ + return m_aCountryFlags.size(); +} + +const CCountryFlags::CCountryFlag *CCountryFlags::Get(int Index) const +{ + return &m_aCountryFlags[Index%m_aCountryFlags.size()]; +} + +int CCountryFlags::Find(int CountryCode) const +{ + for(int i = 0; i < m_aCountryFlags.size(); ++i) + { + if(m_aCountryFlags[i].m_CountryCode == CountryCode) + return i; + } + return -1; +} |