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