OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4GraphicsResource.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Loads all standard graphics from Graphics.ocg */
19 
20 #include "C4Include.h"
23 
24 #include "object/C4DefList.h"
25 #include "graphics/C4FontLoader.h"
26 #include "lib/C4Log.h"
27 #include "game/C4Game.h"
28 #include "c4group/C4Components.h"
29 #include "graphics/C4DrawGL.h"
30 
31 /* C4GraphicsResource */
32 
34  idSfcCaption(0), idSfcButton(0), idSfcButtonD(0), idSfcScroll(0), idSfcContext(0),
35  CaptionFont(FontCaption), TitleFont(FontTitle), TextFont(FontRegular), MiniFont(FontTiny), TooltipFont(FontTooltip)
36 {
37  Default();
38 }
39 
41 {
42  Clear();
43 }
44 
46 {
47  fInitialized = false;
48 
50  idSfcControl = 0;
52  idSfcClonkSkins = 0;
53 
55  fctFlag.Default();
57  fctCrew.Default();
59  fctRank.Default();
60  fctFire.Default();
65  fctMenu.Default();
67  fctLogo.Default();
70  fctArrow.Default();
71  fctExit.Default();
72  fctHand.Default();
74  fctBuild.Default();
75 
79 
83  fctKey.Default();
85  fctMouse.Default();
86 
88 
89  iNumRanks=1;
91 }
92 
94 {
95  fInitialized = false;
96 
97  sfcControl.Clear();
98  idSfcControl = 0;
100  idSfcClonkSkins = 0;
101 
102  fctCrewClr.Clear();
103  fctFlagClr.Clear();
105 
106  fctPlayer.Clear();
107  fctFlag.Clear();
108  fctCrew.Clear();
109  fctWealth.Clear();
110  fctRank.Clear();
111  fctFire.Clear();
113  fctCaptain.Clear();
116  fctMenu.Clear();
118  fctLogo.Clear();
120  fctEnergy.Clear();
121  fctOptions.Clear();
122  fctArrow.Clear();
123  fctExit.Clear();
124  fctHand.Clear();
125  fctGamepad.Clear();
126  fctBuild.Clear();
128  // GUI data
135  fctSubmenu.Clear();
136  fctCheckbox.Clear();
140 
141 
142  // unhook deflist from font
143  FontRegular.SetCustomImages(nullptr);
144 
146 
147  // closing the group set will also close the graphics.ocg
148  // this is just for games that failed to init
149  // normally, this is done after successful init anyway
150  CloseFiles();
151 }
152 
154 {
155  // this regards scenario-specific fonts or overloads in Extra.ocg
156  const char *szFont;
157  if (!Game.C4S.Head.Font.empty()) szFont = Game.C4S.Head.Font.c_str(); else szFont = Config.General.RXFontName;
166  if (!::FontLoader.InitFont(&FontTooltip, szFont, C4FontLoader::C4FT_Main, Config.General.RXFontSize, &Files, false)) return false;
168  // assign def list as custom image source
170  return true;
171 }
172 
174 {
175  FontRegular.Clear();
176  FontTitle.Clear();
177  FontCaption.Clear();
178  FontTiny.Clear();
179  FontTooltip.Clear();
180 }
181 
183 {
184  if (!RegisterGlobalGraphics())
185  return false;
186  // update group set
187  if (!RegisterMainGroups())
188  {
189  LogFatal(LoadResStr("IDS_ERR_GFX_REGISTERMAIN"));
190  return false;
191  }
192 
193 #ifndef USE_CONSOLE
194  // Pre-load all shader files
196  if (!pGL->InitShaders(&Files))
197  {
198  LogFatal(LoadResStr("IDS_ERR_GFX_INITSHADERS"));
199  return false;
200  }
201 #endif
202 
203  Game.SetInitProgress(11.0f);
204  ProgressStart = 12.0f; ProgressIncrement = 0.35f; // TODO: This should be changed so that it stops at 25%, no matter how many graphics we load.
205  // The progress bar is the only graphic besides the background that is
206  // used during startup, so load it early
207  if (!LoadFile(fctProgressBar, "GUIProgress", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
209 
210  if (!InitFonts()) return false;
211 
212  // load GUI files
213  if (!LoadFile(sfcCaption, "GUICaption", Files, idSfcCaption, 0)) return false;
215  if (!LoadFile(sfcButton, "GUIButton", Files, idSfcButton, 0)) return false;
217  if (!LoadFile(sfcButtonD, "GUIButtonDown", Files, idSfcButtonD, 0)) return false;
219  if (!LoadFile(fctButtonHighlight, "GUIButtonHighlight", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
220  if (!LoadFile(fctButtonHighlightRound, "GUIButtonHighlightRound", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
221  if (!LoadFile(fctIcons, "GUIIcons", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
223  if (!LoadFile(fctIconsEx, "GUIIcons2", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
225  if (!LoadFile(fctControllerIcons, "ControllerIcons", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
227  if (!LoadFile(sfcScroll, "GUIScroll", Files, idSfcScroll, 0)) return false;
228  sfctScroll.Set(C4Facet(&sfcScroll,0,0,32,32));
229  if (!LoadFile(sfcContext, "GUIContext", Files, idSfcContext, 0)) return false;
230  fctContext.Set(&sfcContext,0,0,16,16);
231  if (!LoadFile(fctSubmenu, "GUISubmenu", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
232  if (!LoadFile(fctCheckbox, "GUICheckbox", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
234  if (!LoadFile(fctBigArrows, "GUIBigArrows", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
236 
237  // Control
238  if (!LoadFile(sfcControl, "Control", Files, idSfcControl, 0)) return false;
239  fctKeyboard.Set(&sfcControl,0,0,80,36);
240  fctCommand.Set(&sfcControl,0,36,32,32);
241  fctKey.Set(&sfcControl,0,100,64,64);
242  fctOKCancel.Set(&sfcControl,128,100,32,32);
243  fctMouse.Set(&sfcControl,198,100,32,32);
244 
245  // Clonk style selection
246  if (!LoadFile(sfcClonkSkins, "ClonkSkins", Files, idSfcClonkSkins, 0)) return false;
247  fctClonkSkin.Set(&sfcClonkSkins,0,0,64,64);
248 
249  // Facet bitmap resources
250  if (!LoadFile(fctFire, "Fire", Files, C4FCT_Height, C4FCT_Full, false, 0)) return false;
251  if (!LoadFile(fctBackground, "Background", Files, C4FCT_Full, C4FCT_Full, false, C4SF_Tileable)) return false; // tileable
252  if (!LoadFile(fctFlag, "Flag", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false; // (new format)
253  if (!LoadFile(fctCrew, "Crew", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false; // (new format)
254  if (!LoadFile(fctWealth, "Wealth", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false; // (new)
255  if (!LoadFile(fctPlayer, "Player", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false; // (new format)
256  if (!LoadFile(fctRank, "Rank", Files, C4FCT_Height, C4FCT_Full, false, 0)) return false;
257  if (!LoadFile(fctCaptain, "Captain", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
258  if (!LoadCursorGfx()) return false;
259  if (!LoadFile(fctSelectMark, "SelectMark", Files, C4FCT_Height, C4FCT_Full, false, 0)) return false;
260  if (!LoadFile(fctMenu, "Menu", Files, 35, 35, false, 0)) return false;
261  if (!LoadFile(fctLogo, "Logo", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
262  if (!LoadFile(fctConstruction,"Construction", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false; // (new)
263  if (!LoadFile(fctEnergy, "Energy", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false; // (new)
264  if (!LoadFile(fctOptions, "Options", Files, C4FCT_Height, C4FCT_Full, false, 0)) return false;
265  if (!LoadFile(fctUpperBoard, "UpperBoard", Files, C4FCT_Full, C4FCT_Full, false, C4SF_Tileable)) return false; // tileable
266  if (!LoadFile(fctArrow, "Arrow", Files, C4FCT_Height, C4FCT_Full, false, 0)) return false;
267  if (!LoadFile(fctExit, "Exit", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
268  if (!LoadFile(fctHand, "Hand", Files, C4FCT_Height, C4FCT_Full, false, 0)) return false;
269  if (!LoadFile(fctGamepad, "Gamepad", Files, 80, C4FCT_Full, false, 0)) return false;
270  if (!LoadFile(fctBuild, "Build", Files, C4FCT_Full, C4FCT_Full, false, 0)) return false;
271  if (!LoadFile(fctTransformKnob,"TransformKnob",Files,C4FCT_Full, C4FCT_Full, false, 0)) return false;
272 
273  // achievements
274  if (!Achievements.Init(Files)) return false;
275 
276  // create ColorByOwner overlay surfaces
278  {
279  if (!fctCrewClr.CreateClrByOwner(fctCrew.Surface)) { LogFatal("ClrByOwner error! (1)"); return false; }
283  }
285  {
286  if (!fctFlagClr.CreateClrByOwner(fctFlag.Surface)) { LogFatal("ClrByOwner error! (1)"); return false; }
290  }
292  {
293  if (!fctPlayerClr.CreateClrByOwner(fctPlayer.Surface)) { LogFatal("ClrByOwner error! (1)"); return false; }
297  }
298 
299  // get number of ranks
300  int32_t Q; fctRank.GetPhaseNum(iNumRanks, Q);
301  if (!iNumRanks) iNumRanks=1;
302 
303  // CloseFiles() must not be called now:
304  // The sky still needs to be loaded from the global graphics
305  // group in C4Game::InitGame -> C4Sky::Init so we need to keep the group(s) open.
306  // In activated NETWORK2, the files mustn't be closed either, because there will be
307  // multiple calls to this function to allow overloadings
308 
309  // mark initialized
310  fInitialized = true;
311 
312  return true;
313 }
314 
316 {
317  // Determine appropriate GFX file by screen resolution
318  const char *szCursorFilename;
319  szCursorFilename = "Cursor";
320  if (!LoadFile(fctMouseCursor, szCursorFilename, Files, C4FCT_Height, C4FCT_Full, false, 0))
321  return false;
322  return true;
323 }
324 
326 {
327  // Create main gfx group - register with fixed ID 1, to prevent unnecessary font reloading.
328  // FontLoader-initializations always check whether the font has already been initialized
329  // with the same parameters. If the game is simply reloaded in console-mode, this means
330  // that fonts are not reinitialized. This will also apply for InGame-scenario switches yet
331  // to be implemented.
332  // Bitmap fonts from other groups are always reloaded, because the group indices of the gfx
333  // group set are not reset, and will then differ for subsequent group registrations.
334  // Resetting the group index of the gfx group set at game reset would cause problems if a
335  // scenario with its own font face is being closed, and then another scenario with another,
336  // overloaded font face is opened. The group indices could match and the old font would
337  // then be kept.
338  // The cleanest alternative would be to reinit all the fonts whenever a scenario is reloaded
339  // FIXME: Test whether vector fonts from a scenario are correctly reloaded
340  C4Group *pMainGfxGrp = new C4Group();
341  if (!Reloc.Open(*pMainGfxGrp, C4CFN_Graphics) || !Files.RegisterGroup(*pMainGfxGrp, true, C4GSPrio_Base, C4GSCnt_Graphics, 1))
342  {
343  // error
344  LogFatal(FormatString(LoadResStr("IDS_PRC_NOGFXFILE"),C4CFN_Graphics,pMainGfxGrp->GetError()).getData());
345  delete pMainGfxGrp;
346  return false;
347  }
348  return true;
349 }
350 
352 {
353  // register main groups
356  return true;
357 }
358 
360 {
361  // closes main gfx group; releases dependencies into game group set
362  Files.Clear();
364 }
365 
366 static C4Group *FindSuitableFile(const char *szName, C4GroupSet &rGfxSet, char *szFileName, int32_t * pID)
367 {
368  const char * const extensions[] = { "bmp", "jpeg", "jpg", "png", nullptr };
369 
370  return rGfxSet.FindSuitableFile(szName, extensions, szFileName, pID);
371 }
372 
373 bool C4GraphicsResource::LoadFile(C4FacetID &fct, const char *szName, C4GroupSet &rGfxSet, int32_t iWdt, int32_t iHgt, bool fNoWarnIfNotFound, int iFlags)
374 {
375  char FileName[_MAX_FNAME]; int32_t ID = 0;
376  C4Group *pGrp = FindSuitableFile(szName, rGfxSet, FileName, &ID);
377  if (!pGrp)
378  {
379  // FIXME: Use LogFatal here
380  if (!fNoWarnIfNotFound)
381  {
382  LogF(LoadResStr("IDS_PRC_NOGFXFILE"), szName, LoadResStr("IDS_PRC_FILENOTFOUND"));
383  }
384  return false;
385  }
386  // check group
387  if (fct.idSourceGroup == ID)
388  // already up-to-date
389  return true;
390  // load
391  if (!fct.Load(*pGrp, FileName, iWdt, iHgt, false, iFlags))
392  {
393  LogF(LoadResStr("IDS_PRC_NOGFXFILE"), FileName, LoadResStr("IDS_ERR_NOFILE"));
394  return false;
395  }
396  fct.idSourceGroup = ID;
399  return true;
400 }
401 
402 bool C4GraphicsResource::LoadFile(C4Surface& sfc, const char *szName, C4GroupSet &rGfxSet, int32_t &ridCurrSfc, int iFlags)
403 {
404  // find
405  char FileName[_MAX_FNAME]; int32_t ID = 0;
406  C4Group *pGrp = FindSuitableFile(szName, rGfxSet, FileName, &ID);
407  if (!pGrp)
408  {
409  LogF(LoadResStr("IDS_PRC_NOGFXFILE"), szName, LoadResStr("IDS_PRC_FILENOTFOUND"));
410  return false;
411  }
412  // check group
413  if (ID == ridCurrSfc)
414  // already up-to-date
415  return true;
416  // load
417  if (!sfc.Load(*pGrp, FileName, false, false, iFlags))
418  {
419  LogF(LoadResStr("IDS_PRC_NOGFXFILE"), FileName, LoadResStr("IDS_ERR_NOFILE"));
420  return false;
421  }
422  ridCurrSfc = ID;
425  return true;
426 }
427 
428 CStdFont &C4GraphicsResource::GetFontByHeight(int32_t iHgt, float *pfZoom)
429 {
430  // get optimal font for given control size
431  CStdFont *pUseFont;
432  if (iHgt <= MiniFont.GetLineHeight()) pUseFont = &MiniFont;
433  else if (iHgt <= TextFont.GetLineHeight()) pUseFont = &TextFont;
434  else if (iHgt <= CaptionFont.GetLineHeight()) pUseFont = &CaptionFont;
435  else pUseFont = &TitleFont;
436  // determine zoom
437  if (pfZoom)
438  {
439  int32_t iLineHgt = pUseFont->GetLineHeight();
440  if (iLineHgt)
441  *pfZoom = (float) iHgt / (float) iLineHgt;
442  else
443  *pfZoom = 1.0f; // error
444  }
445  return *pUseFont;
446 }
447 
449 {
450  if(!fInitialized) return false;
451  // reload any files that depend on the current resolution
452  // reloads the cursor
453 
454  // Re-open the graphics files if they are not open anymore -- this
455  // happens when the game is running.
456  // Note also that at the moment there are no resolution dependent
457  // graphics files...
458  const bool hadGroupsRegistered = (idRegisteredMainGroupSetFiles != -1);
459  if(!hadGroupsRegistered)
460  {
463  }
464 
466  const bool result = true;
467 
468  if(!hadGroupsRegistered)
469  CloseFiles();
470 
471  return result;
472 }
473 
void SetInitProgress(float fToProgress)
Definition: C4Game.cpp:3389
C4Config Config
Definition: C4Config.cpp:837
int GetLineHeight() const
Definition: C4FontLoader.h:132
C4GUI::DynBarFacet barCaption
bool RegisterGroup(C4Group &rGroup, bool fOwnGrp, int32_t Priority, int32_t Contents, bool fCheckContent=true)
Definition: C4GroupSet.cpp:90
C4Game Game
Definition: C4Globals.cpp:52
C4GUI::DynBarFacet barButtonD
C4Scenario C4S
Definition: C4Game.h:76
int32_t PreCacheEntries(const char *szEntryMask)
Definition: C4GroupSet.cpp:377
C4GroupSet GroupSet
Definition: C4Game.h:89
C4ConfigGeneral General
Definition: C4Config.h:252
bool Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
Definition: C4FacetEx.cpp:78
bool GetPhaseNum(int32_t &rX, int32_t &rY)
Definition: C4Facet.cpp:472
C4Group * FindSuitableFile(const char *szName, const char *const extensions[], char *szFileName, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:193
#define C4GUI_IconExHgt
Definition: C4Gui.h:96
#define C4GUI_IconExWdt
Definition: C4Gui.h:95
void Default()
Definition: C4Surface.cpp:81
bool RegisterGroups(const C4GroupSet &rCopy, int32_t Contents, const char *szFilename=nullptr, int32_t iMaxSkipID=0)
Definition: C4GroupSet.cpp:130
CStdFont & GetFontByHeight(int32_t iHgt, float *pfZoom=nullptr)
const int C4FCT_Height
Definition: C4FacetEx.h:27
#define C4GSCnt_Graphics
Definition: C4GroupSet.h:34
void Set(C4Surface &rSfc)
Definition: C4Facet.cpp:459
int Hgt
Definition: C4Surface.h:67
C4GraphicsResource GraphicsResource
void Set(const C4Facet &cpy)
Definition: C4FacetEx.h:46
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
void SetCustomImages(CustomImages *pHandler)
Definition: C4FontLoader.h:193
bool LoadFile(C4FacetID &fct, const char *szName, C4GroupSet &rGfxSet, int32_t iWdt, int32_t iHgt, bool fNoWarnIfNotFound, int iFlags)
#define C4CFN_ShaderFiles
Definition: C4Components.h:182
bool Init(C4Group &File)
#define C4GUI_IconWdt
Definition: C4Gui.h:93
void Clear()
Definition: C4Surface.cpp:136
void Default()
Definition: C4Facet.cpp:31
bool CreateClrByOwner(C4Surface *pBySurface)
Definition: C4FacetEx.cpp:67
const int C4SF_Tileable
Definition: C4Surface.h:51
#define C4GUI_IconHgt
Definition: C4Gui.h:94
C4FacetID fctButtonHighlightRound
int32_t GetLastID()
Definition: C4GroupSet.h:105
#define C4GUI_ControllerIconWdt
Definition: C4Gui.h:97
bool Open(C4Group &hGroup, const char *filename) const
Definition: C4Reloc.cpp:69
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230
C4GUI::DynBarFacet barButton
#define _MAX_FNAME
void Set(const C4Facet &rByFct, int32_t iPinIndex=0)
Definition: C4Gui.cpp:137
void Clear()
Definition: C4FacetEx.h:76
void Clear()
int32_t idSourceGroup
Definition: C4FacetEx.h:71
C4Reloc Reloc
Definition: C4Reloc.cpp:22
bool Load(C4Group &hGroup, const char *szFilename, bool fOwnPal, bool fNoErrIfNotFound, int iFlags)
#define C4CFN_Graphics
Definition: C4Components.h:28
void Clear()
Definition: C4GroupSet.cpp:52
const char * GetError()
Definition: C4Group.cpp:504
void SetHorizontal(C4Surface &rBySfc, int iHeight=0, int iBorderWidth=0)
Definition: C4Gui.cpp:120
char RXFontName[CFG_MaxString+1]
Definition: C4Config.h:42
C4FontLoader FontLoader
float Hgt
Definition: C4Facet.h:120
int32_t RXFontSize
Definition: C4Config.h:43
int32_t idRegisteredMainGroupSetFiles
std::string Font
Definition: C4Scenario.h:66
C4AchievementGraphics Achievements
bool InitShaders(C4GroupSet *pGroups)
Definition: C4DrawGL.cpp:724
C4GUI::ScrollBarFacets sfctScroll
C4DefList Definitions
Definition: C4Globals.cpp:49
CStdGL * pGL
Definition: C4DrawGL.cpp:914
C4SHead Head
Definition: C4Scenario.h:230
C4Surface * Surface
Definition: C4Facet.h:119
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253
const int C4FCT_Full
Definition: C4FacetEx.h:26
float Wdt
Definition: C4Facet.h:120
void Default()
Definition: C4FacetEx.h:75
#define C4GUI_ControllerIconHgt
Definition: C4Gui.h:98
#define C4GSPrio_Base
Definition: C4GroupSet.h:23
bool InitFont(CStdFont *Font, const char *szFontName, FontType eType, int32_t iSize, C4GroupSet *pGfxGroups, bool fDoShadow=true)
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:277
void Clear()
Definition: C4Gui.h:355