OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Startup.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // Startup screen for non-parameterized engine start
17 
18 #include "C4Include.h"
20 #include "gui/C4Startup.h"
21 
22 #include "graphics/C4FontLoader.h"
23 #include "gui/C4StartupMainDlg.h"
25 #include "gui/C4StartupNetDlg.h"
27 #include "gui/C4StartupAboutDlg.h"
28 #include "gui/C4StartupPlrSelDlg.h"
29 #include "game/C4Game.h"
30 #include "game/C4Application.h"
31 #include "lib/C4Log.h"
33 #include "game/C4GraphicsSystem.h"
34 
35 bool C4StartupGraphics::LoadFile(C4FacetID &rToFct, const char *szFilename)
36 {
38 }
39 
41 {
44  // load startup specific graphics from gfxsys groupset
45  Game.SetInitProgress(38.0f);
46  if (!LoadFile(fctDlgPaper, "StartupDlgPaper")) return false;
47  if (!LoadFile(fctPlrPropBG, "StartupPlrPropBG")) return false;
48  if (!LoadFile(fctAboutBG, "StartupAboutBG")) return false;
50  if (!LoadFile(fctStartupLogo, "StartupLogo")) return false;
53  if (!LoadFile(fctMainButtons, "StartupBigButton")) return false;
55  if (!LoadFile(fctMainButtonsDown, "StartupBigButtonDown")) return false;
57  if (!LoadFile(fctBookScroll, "StartupBookScroll")) return false;
62  if (!LoadFile(fctContext, "StartupContext")) return false;
64  if (!LoadFile(fctScenSelIcons, "StartupScenSelIcons")) return false;
65  fctScenSelIcons.Wdt = fctScenSelIcons.Hgt; // icon width is determined by icon height
66  if (!LoadFile(fctScenSelTitleOverlay, "StartupScenSelTitleOv")) return false;
67  if (!LoadFile(fctOptionsIcons, "StartupOptionIcons")) return false;
69  if (!LoadFile(fctOptionsTabClip, "StartupTabClip")) return false;
70  if (!LoadFile(fctNetGetRef, "StartupNetGetRef")) return false;
71  fctNetGetRef.Wdt = 40;
72 #ifndef USE_CONSOLE
73  if (!InitFonts()) return false;
74 #endif
75  Game.SetInitProgress(100);
76  return true;
77 }
78 
79 #ifndef USE_CONSOLE
81 {
82  const char *szFont = Config.General.RXFontName;
84  { LogFatal("Font Error (1)"); return false; }
87  { LogFatal("Font Error (2)"); return false; }
90  { LogFatal("Font Error (3)"); return false; }
93  { LogFatal("Font Error (4)"); return false; }
94  return true;
95 }
96 #endif
97 
99 {
100  // get optimal font for given control size
101  CStdFont *pUseFont;
102  if (iHgt <= BookSmallFont.GetLineHeight()) pUseFont = &BookSmallFont;
103  else if (iHgt <= BookFont.GetLineHeight()) pUseFont = &BookFont;
104  else if (iHgt <= BookFontCapt.GetLineHeight()) pUseFont = &BookFontCapt;
105  else pUseFont = &BookFontTitle;
106  // determine zoom
107  if (pfZoom)
108  {
109  int32_t iLineHgt = pUseFont->GetLineHeight();
110  if (iLineHgt)
111  *pfZoom = (float) iHgt / (float) iLineHgt;
112  else
113  *pfZoom = 1.0f; // error
114  }
115  return *pUseFont;
116 }
117 
118 // statics
119 C4Startup::DialogID C4Startup::eLastDlgID = C4Startup::SDID_Main;
120 StdCopyStrBuf C4Startup::sSubDialog = StdCopyStrBuf();
121 
122 // startup singleton instance
123 C4Startup *C4Startup::pInstance = nullptr;
124 
125 C4Startup::C4Startup() : fInStartup(false), pLastDlg(nullptr), pCurrDlg(nullptr)
126 {
127  // must be single!
128  assert(!pInstance);
129  pInstance = this;
130 }
131 
133 {
134  pInstance = nullptr;
135  delete pLastDlg;
136  delete pCurrDlg;
137 }
138 
139 C4StartupDlg *C4Startup::SwitchDialog(DialogID eToDlg, bool fFade, const char *szSubDialog)
140 {
141  // can't go back twice, because dialog is not remembered: Always go back to main in this case
142  if (eToDlg == SDID_Back && (fLastDlgWasBack || !pLastDlg)) eToDlg = SDID_Main;
143  fLastDlgWasBack = false;
144  // create new dialog
145  C4StartupDlg *pToDlg = nullptr;
146  switch (eToDlg)
147  {
148  case SDID_Main:
149  pToDlg = new C4StartupMainDlg();
150  break;
151  case SDID_ScenSel:
152  pToDlg = new C4StartupScenSelDlg(false);
153  break;
154  case SDID_ScenSelNetwork:
155  pToDlg = new C4StartupScenSelDlg(true);
156  break;
157  case SDID_NetJoin:
158  pToDlg = new C4StartupNetDlg();
159  break;
160  case SDID_Options:
161  pToDlg = new C4StartupOptionsDlg();
162  break;
163  case SDID_About:
164  pToDlg = new C4StartupAboutDlg();
165  break;
166  case SDID_PlrSel:
167  pToDlg = new C4StartupPlrSelDlg();
168  break;
169  case SDID_Back:
170  pToDlg = pLastDlg;
171  fLastDlgWasBack = true;
172  break;
173  };
174  assert(pToDlg);
175  if (!pToDlg) return nullptr;
176  if (pToDlg != pLastDlg)
177  {
178  // remember current position
179  eLastDlgID = eToDlg;
180  // kill any old dialog
181  if (pLastDlg) delete pLastDlg;
182  }
183  // retain current dialog as last, so it can fade out and may be used later
184  if ((pLastDlg = pCurrDlg))
185  {
186  if (fFade)
187  {
188  if (!pLastDlg->IsShown()) pLastDlg->Show(::pGUI, false);
189  pLastDlg->FadeOut(true);
190  }
191  else
192  {
193  delete pLastDlg;
194  pLastDlg = nullptr;
195  }
196  }
197  // Okay; now using this dialog
198  pCurrDlg = pToDlg;
199  // go to dialog subscreen
200  if (szSubDialog) pCurrDlg->SetSubscreen(szSubDialog);
201  // fade in new dlg
202  if (fFade)
203  {
204  if (!pToDlg->FadeIn(::pGUI))
205  {
206  delete pToDlg; pCurrDlg=nullptr;
207  return nullptr;
208  }
209  }
210  else
211  {
212  if (!pToDlg->Show(::pGUI, true))
213  {
214  delete pToDlg; pCurrDlg=nullptr;
215  return nullptr;
216  }
217  }
218  return pToDlg;
219 }
220 
222 {
223  assert(!fInStartup);
224  assert(::pGUI);
225  // now in startup!
226  fInStartup = true;
227  fLastDlgWasBack = false;
228 
230 
231  // clear any previous
232  if (pLastDlg) { delete pLastDlg; pLastDlg = nullptr; }
233  if (pCurrDlg) { delete pCurrDlg; pCurrDlg = nullptr; }
234 
235  // start with the last dlg that was shown - at first startup main dialog
236  SwitchDialog(eLastDlgID, true, sSubDialog.getData());
237 
238  // show error dlg if restart
240  {
241  Game.fQuitWithError = false;
242  // preferred: Show fatal error
243  const char *szErr = GetFatalError();
244  if (szErr)
245  {
246  ::pGUI->ShowMessage(szErr, LoadResStr("IDS_DLG_LOG"), C4GUI::Ico_Error);
247  }
248  else
249  {
250  // fallback to showing complete log
251  StdStrBuf sLastLog;
253  if (!sLastLog.isNull())
254  ::pGUI->ShowRemoveDlg(new C4GUI::InfoDialog(LoadResStr("IDS_DLG_LOG"), 10, sLastLog));
255  }
256  ResetFatalError();
257  }
258 }
259 
261 {
262  // check whether startup was aborted
263  delete pLastDlg; pLastDlg = nullptr;
264  if (pCurrDlg)
265  {
266  // deinit last shown dlg
267  if (pCurrDlg->IsShown())
268  {
269  pCurrDlg->Close(true);
270  }
271  delete pCurrDlg;
272  pCurrDlg = nullptr;
273  }
274 
275  // now no more in startup!
276  fInStartup = false;
277 
278  // after startup: cleanup
279  ::pGUI->CloseAllDialogs(true);
280 }
281 
283 {
284  if (pInstance) pInstance->DontStartup();
285 }
286 
288 {
289  // create and load startup data if not done yet
290  assert(::pGUI);
291  if (!pInstance)
292  {
293  Game.SetInitProgress(36.0f);
294  C4Startup *pStartup = new C4Startup();
295  Game.SetInitProgress(37.0f);
296  // load startup specific gfx
297  if (!pStartup->Graphics.Init())
298  { LogFatal(LoadResStr("IDS_ERR_NOGFXSYS")); delete pStartup; return nullptr; }
299  }
300  return pInstance;
301 }
302 
304 {
305  // make sure startup data is destroyed
306  if (pInstance) { delete pInstance; pInstance=nullptr; }
307 }
308 
310 {
311  // ensure gfx are loaded
312  C4Startup *pStartup = EnsureLoaded();
313  if (!pStartup)
314  {
315  Application.Quit();
316  return;
317  }
318  // exec it
319  pStartup->DoStartup();
320 }
321 
322 bool C4Startup::SetStartScreen(const char *szScreen)
323 {
324  sSubDialog.Clear();
325  // set dialog ID to be shown to specified value
326  if (SEqualNoCase(szScreen, "main"))
327  eLastDlgID = SDID_Main;
328  if (SEqualNoCase(szScreen, "scen"))
329  eLastDlgID = SDID_ScenSel;
330  if (SEqualNoCase(szScreen, "netscen"))
331  eLastDlgID = SDID_ScenSelNetwork;
332  else if (SEqualNoCase(szScreen, "net"))
333  eLastDlgID = SDID_NetJoin;
334  else if (SEqualNoCase(szScreen, "options"))
335  eLastDlgID = SDID_Options;
336  else if (SEqual2NoCase(szScreen, "options-"))
337  {
338  eLastDlgID = SDID_Options;
339  // subscreen of options
340  sSubDialog.Copy(szScreen+8);
341  }
342  else if (SEqualNoCase(szScreen, "plrsel"))
343  eLastDlgID = SDID_PlrSel;
344  else if (SEqualNoCase(szScreen, "about"))
345  eLastDlgID = SDID_About;
346  else return false;
347  return true;
348 }
349 
351 {
352  // forward message to current dialog
353  if (pCurrDlg) pCurrDlg->OnKeyboardLayoutChanged();
354 }
355 
357 {
358  // forward message to current dialog
359  if (pCurrDlg) pCurrDlg->OnLeagueOptionChanged();
360 }
const char * getData() const
Definition: StdBuf.h:450
void SetInitProgress(float fToProgress)
Definition: C4Game.cpp:3389
C4FacetID fctStartupLogo
Definition: C4Startup.h:47
CStdFont BookFontTitle
Definition: C4Startup.h:65
C4GUI::DynBarFacet barMainButtonsDown
Definition: C4Startup.h:51
C4Config Config
Definition: C4Config.cpp:837
int GetLineHeight() const
Definition: C4FontLoader.h:132
friend class C4StartupMainDlg
Definition: C4Startup.h:117
C4FacetID fctDlgPaper
Definition: C4Startup.h:45
C4Game Game
Definition: C4Globals.cpp:52
void Clear()
Definition: StdBuf.h:474
C4ConfigGeneral General
Definition: C4Config.h:252
C4GUI::ScrollBarFacets sfctBookScrollB
Definition: C4Startup.h:55
C4FacetID fctScenSelTitleOverlay
Definition: C4Startup.h:63
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:177
static void Unload()
Definition: C4Startup.cpp:303
C4FacetID fctPlrPropBG
Definition: C4Startup.h:43
void Close(bool fOK)
bool ShowRemoveDlg(Dialog *pDlg)
friend class C4StartupOptionsDlg
Definition: C4Startup.h:120
virtual void OnKeyboardLayoutChanged()
Definition: C4Startup.h:89
void OnKeyboardLayoutChanged()
Definition: C4Startup.cpp:350
static bool SetStartScreen(const char *szScreen)
Definition: C4Startup.cpp:322
C4GraphicsResource GraphicsResource
void Set(const C4Facet &cpy)
Definition: C4FacetEx.h:46
C4FacetID fctScenSelIcons
Definition: C4Startup.h:61
bool IsShown()
Definition: C4Gui.h:2147
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
size_t StartupLogPos
Definition: C4Game.h:143
C4GUI::ScrollBarFacets sfctBookScrollG
Definition: C4Startup.h:55
bool Show(Screen *pOnScreen, bool fCB)
bool LoadFile(C4FacetID &fct, const char *szName, C4GroupSet &rGfxSet, int32_t iWdt, int32_t iHgt, bool fNoWarnIfNotFound, int iFlags)
void OnLeagueOptionChanged()
Definition: C4Startup.cpp:356
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1194
virtual void Quit()
void DoStartup()
Definition: C4Startup.cpp:221
CStdFont BookSmallFont
Definition: C4Startup.h:65
C4GUI::ScrollBarFacets sfctBookScrollR
Definition: C4Startup.h:55
C4FacetID fctOptionsIcons
Definition: C4Startup.h:71
C4FacetID fctMainButtonsDown
Definition: C4Startup.h:50
C4GraphicsSystem GraphicsSystem
Definition: C4Globals.cpp:51
friend class C4StartupNetDlg
Definition: C4Startup.h:118
friend class C4StartupScenSelDlg
Definition: C4Startup.h:119
bool fQuitWithError
Definition: C4Game.h:144
class C4StartupDlg * SwitchDialog(DialogID eToDlg, bool fFade=true, const char *szSubDialog=nullptr)
Definition: C4Startup.cpp:139
C4GUI::ScrollBarFacets sfctBookScroll
Definition: C4Startup.h:55
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230
void Set(const C4Facet &rByFct, int32_t iPinIndex=0)
Definition: C4Gui.cpp:137
void FadeOut(bool fCloseWithOK)
CStdFont BookFont
Definition: C4Startup.h:65
C4FacetID fctBookScroll
Definition: C4Startup.h:54
static void InitStartup()
Definition: C4Startup.cpp:309
void DontStartup()
Definition: C4Startup.cpp:260
void SetHorizontal(C4Surface &rBySfc, int iHeight=0, int iBorderWidth=0)
Definition: C4Gui.cpp:120
bool ShowMessage(const char *szMessage, const char *szCaption, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
void CloseAllDialogs(bool fWithOK)
Definition: C4Gui.cpp:707
bool GetLogSection(size_t iStart, size_t iLength, StdStrBuf &rsOut)
Definition: C4Log.cpp:295
size_t QuitLogPos
Definition: C4Game.h:143
char RXFontName[CFG_MaxString+1]
Definition: C4Config.h:42
C4FontLoader FontLoader
float Hgt
Definition: C4Facet.h:120
int32_t RXFontSize
Definition: C4Config.h:43
bool FadeIn(Screen *pOnScreen)
const char * GetFatalError()
Definition: C4Log.cpp:248
virtual void OnLeagueOptionChanged()
Definition: C4Startup.h:90
bool isNull() const
Definition: StdBuf.h:449
C4FacetID fctContext
Definition: C4Startup.h:68
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:190
friend class C4StartupAboutDlg
Definition: C4Startup.h:121
CStdFont & GetBlackFontByHeight(int32_t iHgt, float *pfZoom)
Definition: C4Startup.cpp:98
static void CloseStartup()
Definition: C4Startup.cpp:282
static C4Startup * EnsureLoaded()
Definition: C4Startup.cpp:287
void ResetFatalError()
Definition: C4Log.cpp:243
C4FacetID fctNetGetRef
Definition: C4Startup.h:74
C4Surface * Surface
Definition: C4Facet.h:119
CStdFont BookFontCapt
Definition: C4Startup.h:65
C4FacetID fctOptionsTabClip
Definition: C4Startup.h:71
void SetBackground()
Definition: C4Surface.h:94
C4FacetID fctAboutBG
Definition: C4Startup.h:44
friend class C4StartupPlrSelDlg
Definition: C4Startup.h:122
const int C4FCT_Full
Definition: C4FacetEx.h:26
void Copy()
Definition: StdBuf.h:475
float Wdt
Definition: C4Facet.h:120
C4GUI::DynBarFacet barMainButtons
Definition: C4Startup.h:51
C4StartupGraphics Graphics
Definition: C4Startup.h:100
C4Application Application
Definition: C4Globals.cpp:44
C4Surface & GetFace()
Definition: C4FacetEx.h:52
C4FacetID fctMainButtons
Definition: C4Startup.h:50
virtual bool SetSubscreen(const char *szToScreen)
Definition: C4Startup.h:88
bool InitFont(CStdFont *Font, const char *szFontName, FontType eType, int32_t iSize, C4GroupSet *pGfxGroups, bool fDoShadow=true)