OpenClonk
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 "game/C4Application.h"
23 #include "game/C4GraphicsSystem.h"
24 #include "graphics/C4FontLoader.h"
26 #include "gui/C4StartupAboutDlg.h"
27 #include "gui/C4StartupLegalDlg.h"
28 #include "gui/C4StartupMainDlg.h"
29 #include "gui/C4StartupNetDlg.h"
31 #include "gui/C4StartupPlrSelDlg.h"
33 #include "gui/C4StartupModsDlg.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(fctAboutTitles, "StartupAboutTitles")) 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 
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 #ifdef USE_CONSOLE
142  return nullptr;
143 #else
144  // can't go back twice, because dialog is not remembered: Always go back to main in this case
145  if (eToDlg == SDID_Back && (fLastDlgWasBack || !pLastDlg)) eToDlg = SDID_Main;
146  fLastDlgWasBack = false;
147  // create new dialog
148  C4StartupDlg *pToDlg = nullptr;
149  switch (eToDlg)
150  {
151  case SDID_Main:
152  pToDlg = new C4StartupMainDlg();
153  break;
154  case SDID_ScenSel:
155  pToDlg = new C4StartupScenSelDlg(false);
156  break;
157  case SDID_ScenSelNetwork:
158  pToDlg = new C4StartupScenSelDlg(true);
159  break;
160  case SDID_NetJoin:
161  pToDlg = new C4StartupNetDlg();
162  break;
163  case SDID_Options:
164  pToDlg = new C4StartupOptionsDlg();
165  break;
166  case SDID_About:
167  pToDlg = new C4StartupAboutDlg();
168  break;
169  case SDID_Legal:
170  pToDlg = new C4StartupLegalDlg();
171  break;
172  case SDID_PlrSel:
173  pToDlg = new C4StartupPlrSelDlg();
174  break;
175  case SDID_Mods:
176  pToDlg = new C4StartupModsDlg();
177  break;
178  case SDID_Back:
179  pToDlg = pLastDlg;
180  fLastDlgWasBack = true;
181  break;
182  };
183  assert(pToDlg);
184  if (!pToDlg) return nullptr;
185  if (pToDlg != pLastDlg)
186  {
187  // remember current position
188  eLastDlgID = eToDlg;
189  // kill any old dialog
190  if (pLastDlg) delete pLastDlg;
191  }
192  // retain current dialog as last, so it can fade out and may be used later
193  if ((pLastDlg = pCurrDlg))
194  {
195  if (fFade)
196  {
197  if (!pLastDlg->IsShown()) pLastDlg->Show(::pGUI, false);
198  pLastDlg->FadeOut(true);
199  }
200  else
201  {
202  delete pLastDlg;
203  pLastDlg = nullptr;
204  }
205  }
206  // Okay; now using this dialog
207  pCurrDlg = pToDlg;
208  // go to dialog subscreen
209  if (szSubDialog) pCurrDlg->SetSubscreen(szSubDialog);
210  // fade in new dlg
211  if (fFade)
212  {
213  if (!pToDlg->FadeIn(::pGUI))
214  {
215  delete pToDlg; pCurrDlg=nullptr;
216  return nullptr;
217  }
218  }
219  else
220  {
221  if (!pToDlg->Show(::pGUI, true))
222  {
223  delete pToDlg; pCurrDlg=nullptr;
224  return nullptr;
225  }
226  }
227  return pToDlg;
228 #endif
229 }
230 
232 {
233  assert(!fInStartup);
234  assert(::pGUI);
235  // now in startup!
236  fInStartup = true;
237  fLastDlgWasBack = false;
238 
240 
241  // clear any previous
242  if (pLastDlg) { delete pLastDlg; pLastDlg = nullptr; }
243  if (pCurrDlg) { delete pCurrDlg; pCurrDlg = nullptr; }
244 
245  // start with the last dlg that was shown - at first startup main dialog
246  SwitchDialog(eLastDlgID, true, sSubDialog.getData());
247 
248  // show error dlg if restart
250  {
251  Game.fQuitWithError = false;
252  // preferred: Show fatal error
253  const char *szErr = GetFatalError();
254  if (szErr)
255  {
256  ::pGUI->ShowMessage(szErr, LoadResStr("IDS_DLG_LOG"), C4GUI::Ico_Error);
257  }
258  else
259  {
260  // fallback to showing complete log
261  StdStrBuf sLastLog;
263  if (!sLastLog.isNull())
264  ::pGUI->ShowRemoveDlg(new C4GUI::InfoDialog(LoadResStr("IDS_DLG_LOG"), 10, sLastLog));
265  }
266  ResetFatalError();
267  }
268 }
269 
271 {
272  // check whether startup was aborted
273  delete pLastDlg; pLastDlg = nullptr;
274  if (pCurrDlg)
275  {
276  // deinit last shown dlg
277  if (pCurrDlg->IsShown())
278  {
279  pCurrDlg->Close(true);
280  }
281  delete pCurrDlg;
282  pCurrDlg = nullptr;
283  }
284 
285  // now no more in startup!
286  fInStartup = false;
287 
288  // after startup: cleanup
289  ::pGUI->CloseAllDialogs(true);
290 }
291 
293 {
294  if (pInstance) pInstance->DontStartup();
295 }
296 
298 {
299  // create and load startup data if not done yet
300  assert(::pGUI);
301  if (!pInstance)
302  {
303  Game.SetInitProgress(36.0f);
304  C4Startup *pStartup = new C4Startup();
305  Game.SetInitProgress(37.0f);
306  // load startup specific gfx
307  if (!pStartup->Graphics.Init())
308  { LogFatal(LoadResStr("IDS_ERR_NOGFXSYS")); delete pStartup; return nullptr; }
309  }
310  return pInstance;
311 }
312 
314 {
315  // make sure startup data is destroyed
316  if (pInstance) { delete pInstance; pInstance=nullptr; }
317 }
318 
320 {
321  // ensure gfx are loaded
322  C4Startup *pStartup = EnsureLoaded();
323  if (!pStartup)
324  {
325  Application.Quit();
326  return;
327  }
328  // exec it
329  pStartup->DoStartup();
330 }
331 
332 bool C4Startup::SetStartScreen(const char *szScreen, const char *szSubDialog)
333 {
334  sSubDialog.Clear();
335  if (szSubDialog != nullptr)
336  sSubDialog = szSubDialog;
337 
338  // set dialog ID to be shown to specified value
339  if (SEqualNoCase(szScreen, "main"))
340  eLastDlgID = SDID_Main;
341  if (SEqualNoCase(szScreen, "scen"))
342  eLastDlgID = SDID_ScenSel;
343  if (SEqualNoCase(szScreen, "netscen"))
344  eLastDlgID = SDID_ScenSelNetwork;
345  else if (SEqualNoCase(szScreen, "net"))
346  eLastDlgID = SDID_NetJoin;
347  else if (SEqualNoCase(szScreen, "mods"))
348  eLastDlgID = SDID_Mods;
349  else if (SEqualNoCase(szScreen, "options"))
350  eLastDlgID = SDID_Options;
351  else if (SEqual2NoCase(szScreen, "options-"))
352  {
353  eLastDlgID = SDID_Options;
354  // subscreen of options
355  sSubDialog.Copy(szScreen+8);
356  }
357  else if (SEqualNoCase(szScreen, "plrsel"))
358  eLastDlgID = SDID_PlrSel;
359  else if (SEqualNoCase(szScreen, "about"))
360  eLastDlgID = SDID_About;
361  else return false;
362  return true;
363 }
364 
366 {
367  // forward message to current dialog
368  if (pCurrDlg) pCurrDlg->OnKeyboardLayoutChanged();
369 }
370 
372 {
373  // forward message to current dialog
374  if (pCurrDlg) pCurrDlg->OnLeagueOptionChanged();
375 }
C4Config Config
Definition: C4Config.cpp:930
const int C4FCT_Full
Definition: C4FacetEx.h:26
C4FontLoader FontLoader
C4Game Game
Definition: C4Globals.cpp:52
C4Application Application
Definition: C4Globals.cpp:44
C4GraphicsSystem GraphicsSystem
Definition: C4Globals.cpp:51
C4GraphicsResource GraphicsResource
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
void ResetFatalError()
Definition: C4Log.cpp:252
bool GetLogSection(size_t iStart, size_t iLength, StdStrBuf &rsOut)
Definition: C4Log.cpp:304
const char * GetFatalError()
Definition: C4Log.cpp:257
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:239
@ C4StartupAboutTitleCount
Definition: C4Startup.h:44
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:213
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:226
void Quit() override
char RXFontName[CFG_MaxString+1]
Definition: C4Config.h:42
int32_t RXFontSize
Definition: C4Config.h:43
C4ConfigGeneral General
Definition: C4Config.h:255
C4Surface * Surface
Definition: C4Facet.h:117
float Hgt
Definition: C4Facet.h:118
float Wdt
Definition: C4Facet.h:118
void Set(const C4Facet &cpy)
Definition: C4FacetEx.h:46
bool InitFont(CStdFont *Font, const char *szFontName, FontType eType, int32_t iSize, C4GroupSet *pGfxGroups, bool fDoShadow=true)
void FadeOut(bool fCloseWithOK)
void Close(bool fOK)
bool IsShown()
Definition: C4Gui.h:2148
bool FadeIn(Screen *pOnScreen)
bool Show(Screen *pOnScreen, bool fCB)
bool ShowRemoveDlg(Dialog *pDlg)
bool ShowMessage(const char *szMessage, const char *szCaption, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
void CloseAllDialogs(bool fWithOK)
Definition: C4Gui.cpp:704
void SetInitProgress(float to_progress)
Definition: C4Game.cpp:4207
size_t StartupLogPos
Definition: C4Game.h:142
size_t QuitLogPos
Definition: C4Game.h:142
bool fQuitWithError
Definition: C4Game.h:143
bool LoadFile(C4FacetID &fct, const char *szName, C4GroupSet &rGfxSet, int32_t iWdt, int32_t iHgt, bool fNoWarnIfNotFound, int iFlags)
virtual bool SetSubscreen(const char *szToScreen)
Definition: C4Startup.h:100
virtual void OnLeagueOptionChanged()
Definition: C4Startup.h:102
virtual void OnKeyboardLayoutChanged()
Definition: C4Startup.h:101
C4FacetID fctNetGetRef
Definition: C4Startup.h:86
C4FacetID fctStartupLogo
Definition: C4Startup.h:59
C4FacetID fctScenSelIcons
Definition: C4Startup.h:73
C4GUI::DynBarFacet barMainButtonsDown
Definition: C4Startup.h:63
C4GUI::ScrollBarFacets sfctBookScrollB
Definition: C4Startup.h:67
C4FacetID fctContext
Definition: C4Startup.h:80
C4FacetID fctScenSelTitleOverlay
Definition: C4Startup.h:75
C4GUI::ScrollBarFacets sfctBookScroll
Definition: C4Startup.h:67
C4FacetID fctBookScroll
Definition: C4Startup.h:66
C4FacetID fctOptionsTabClip
Definition: C4Startup.h:83
C4FacetID fctOptionsIcons
Definition: C4Startup.h:83
C4FacetID fctMainButtons
Definition: C4Startup.h:62
CStdFont BookFontTitle
Definition: C4Startup.h:77
C4GUI::ScrollBarFacets sfctBookScrollR
Definition: C4Startup.h:67
C4FacetID fctPlrPropBG
Definition: C4Startup.h:55
CStdFont & GetBlackFontByHeight(int32_t iHgt, float *pfZoom)
Definition: C4Startup.cpp:98
CStdFont BookFontCapt
Definition: C4Startup.h:77
CStdFont BookFont
Definition: C4Startup.h:77
C4FacetID fctAboutTitles
Definition: C4Startup.h:56
C4FacetID fctDlgPaper
Definition: C4Startup.h:57
C4GUI::ScrollBarFacets sfctBookScrollG
Definition: C4Startup.h:67
CStdFont BookSmallFont
Definition: C4Startup.h:77
C4GUI::DynBarFacet barMainButtons
Definition: C4Startup.h:63
C4FacetID fctMainButtonsDown
Definition: C4Startup.h:62
friend class C4StartupLegalDlg
Definition: C4Startup.h:135
void OnLeagueOptionChanged()
Definition: C4Startup.cpp:371
friend class C4StartupMainDlg
Definition: C4Startup.h:129
void OnKeyboardLayoutChanged()
Definition: C4Startup.cpp:365
friend class C4StartupModsDlg
Definition: C4Startup.h:133
void DontStartup()
Definition: C4Startup.cpp:270
friend class C4StartupNetDlg
Definition: C4Startup.h:130
friend class C4StartupScenSelDlg
Definition: C4Startup.h:131
@ SDID_PlrSel
Definition: C4Startup.h:114
@ SDID_Legal
Definition: C4Startup.h:114
@ SDID_Options
Definition: C4Startup.h:114
@ SDID_NetJoin
Definition: C4Startup.h:114
@ SDID_ScenSel
Definition: C4Startup.h:114
@ SDID_ScenSelNetwork
Definition: C4Startup.h:114
@ SDID_About
Definition: C4Startup.h:114
friend class C4StartupAboutDlg
Definition: C4Startup.h:134
friend class C4StartupOptionsDlg
Definition: C4Startup.h:132
friend class C4StartupPlrSelDlg
Definition: C4Startup.h:136
class C4StartupDlg * SwitchDialog(DialogID eToDlg, bool fFade=true, const char *szSubDialog=nullptr)
Definition: C4Startup.cpp:139
C4StartupGraphics Graphics
Definition: C4Startup.h:112
static void Unload()
Definition: C4Startup.cpp:313
static C4Startup * EnsureLoaded()
Definition: C4Startup.cpp:297
static bool SetStartScreen(const char *szScreen, const char *szSubDialog=nullptr)
Definition: C4Startup.cpp:332
static void InitStartup()
Definition: C4Startup.cpp:319
void DoStartup()
Definition: C4Startup.cpp:231
static void CloseStartup()
Definition: C4Startup.cpp:292
int Wdt
Definition: C4Surface.h:65
int Hgt
Definition: C4Surface.h:65
int GetLineHeight() const
Definition: C4FontLoader.h:125
const char * getData() const
Definition: StdBuf.h:442
bool isNull() const
Definition: StdBuf.h:441
void Copy()
Definition: StdBuf.h:467
void Clear()
Definition: StdBuf.h:466
@ Ico_Error
Definition: C4Gui.h:652
void SetHorizontal(C4Surface &rBySfc, int iHeight=0, int iBorderWidth=0)
Definition: C4Gui.cpp:119
void Set(const C4Facet &rByFct, int32_t iPinIndex=0)
Definition: C4Gui.cpp:136