OpenClonk
C4Achievement.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2014-2017, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 #include "C4Include.h"
16 #include "player/C4Achievement.h"
17 
18 #include "c4group/C4Components.h"
19 #include "graphics/C4FacetEx.h"
20 
21 /* C4AchievementGraphics */
22 
24 {
25  // Load all graphics matching achievement filename and register them to map
26  char FileName[_MAX_FNAME];
27  File.ResetSearch();
28  while (File.FindNextEntry(C4CFN_Achievements, FileName))
29  {
30  C4FacetSurface *new_fct = new C4FacetSurface();
31  if (!new_fct->Load(File, FileName, C4FCT_Height, C4FCT_Full, false, 0))
32  {
33  delete new_fct;
34  LogF(LoadResStr("IDS_PRC_NOGFXFILE"), FileName, LoadResStr("IDS_ERR_NOFILE"));
35  return false;
36  }
37  // Register under filename excluding the leading "Achv" part. Delete any existing file with same name.
38  RemoveExtension(FileName);
39  int32_t id_offset = SCharPos('*', C4CFN_Achievements); assert(id_offset>=0);
40  StdCopyStrBuf sFileName(FileName + id_offset);
41  auto i = Graphics.find(sFileName);
42  if (i != Graphics.end()) delete i->second;
43  Graphics[sFileName] = new_fct;
44  }
45  // done. success no matter how many files were loaded.
46  return true;
47 }
48 
50 {
51  int32_t idNewGrp=0;
52  C4Group *pGrp = Files.FindEntry(C4CFN_Achievements, nullptr, &idNewGrp);
53  if (!pGrp) return true; // no achievement gfx. That's OK.
54  if (idNewGrp == idGrp) return true; // no update
55  idGrp = idNewGrp;
56  // OK, load from this group
57  return Init(*pGrp);
58 }
59 
61 {
62  for (auto & Graphic : Graphics)
63  delete Graphic.second;
64  Graphics.clear();
65  idGrp = 0;
66 }
67 
69 {
70  auto i = Graphics.find(StdCopyStrBuf(name));
71  if (i != Graphics.end()) return i->second; else return nullptr;
72 }
#define C4CFN_Achievements
Definition: C4Components.h:90
const int C4FCT_Full
Definition: C4FacetEx.h:26
const int C4FCT_Height
Definition: C4FacetEx.h:27
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:262
#define _MAX_FNAME
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:239
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:303
C4FacetSurface * FindByName(const char *name) const
bool Init(C4Group &File)
bool Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
Definition: C4FacetEx.cpp:84
bool FindNextEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr, bool start_at_filename=false)
Definition: C4Group.cpp:2217
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1316
C4Group * FindEntry(const char *szWildcard, int32_t *pPriority=nullptr, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:175