OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4LoaderScreen.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2003-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
17 
18 #include "C4Include.h"
19 #include "gui/C4LoaderScreen.h"
20 
21 #include "c4group/C4Components.h"
22 #include "c4group/C4GroupSet.h"
23 #include "graphics/C4Draw.h"
25 #include "lib/C4LogBuf.h"
26 #include "lib/C4Random.h"
27 
29 {
30  // zero fields
31  szInfo=nullptr;
32  fBlackScreen = false;
33 }
34 
36 {
37  // clear fields
38  if (szInfo) delete [] szInfo;
39 }
40 
41 bool C4LoaderScreen::Init(const char *szLoaderSpec)
42 {
43  // Determine loader specification
44  if (!szLoaderSpec || !szLoaderSpec[0])
45  szLoaderSpec = "Loader*";
46  char szLoaderSpecPng[128 + 1 + 4], szLoaderSpecBmp[128 + 1 + 4];
47  char szLoaderSpecJpg[128 + 1 + 4], szLoaderSpecJpeg[128 + 1 + 5];
48  SCopy(szLoaderSpec, szLoaderSpecPng); DefaultExtension(szLoaderSpecPng, "png");
49  SCopy(szLoaderSpec, szLoaderSpecBmp); DefaultExtension(szLoaderSpecBmp, "bmp");
50  SCopy(szLoaderSpec, szLoaderSpecJpg); DefaultExtension(szLoaderSpecJpg, "jpg");
51  SCopy(szLoaderSpec, szLoaderSpecJpeg); DefaultExtension(szLoaderSpecJpeg, "jpeg");
52  int iLoaders=0;
53  C4Group *pGroup=nullptr,*pChosenGrp=nullptr;
54  char ChosenFilename[_MAX_PATH+1];
55  // query groups of equal priority in set
56  while ((pGroup=Game.GroupSet.FindGroup(C4GSCnt_Loaders, pGroup, true)))
57  {
58  iLoaders+=SeekLoaderScreens(*pGroup, szLoaderSpecPng, iLoaders, ChosenFilename, &pChosenGrp);
59  iLoaders+=SeekLoaderScreens(*pGroup, szLoaderSpecJpeg, iLoaders, ChosenFilename, &pChosenGrp);
60  iLoaders+=SeekLoaderScreens(*pGroup, szLoaderSpecJpg, iLoaders, ChosenFilename, &pChosenGrp);
61  // lower the chance for any loader other than png
62  iLoaders*=2;
63  iLoaders+=SeekLoaderScreens(*pGroup, szLoaderSpecBmp, iLoaders, ChosenFilename, &pChosenGrp);
64  }
65  // nothing found? seek in main gfx grp
66  C4Group GfxGrp;
67  if (!iLoaders)
68  {
69  // open it
70  GfxGrp.Close();
71  if (!Reloc.Open(GfxGrp, C4CFN_Graphics))
72  {
73  LogFatal(FormatString(LoadResStr("IDS_PRC_NOGFXFILE"),C4CFN_Graphics,GfxGrp.GetError()).getData());
74  return false;
75  }
76  // seek for png-loaders
77  iLoaders=SeekLoaderScreens(GfxGrp, szLoaderSpecPng, iLoaders, ChosenFilename, &pChosenGrp);
78  iLoaders+=SeekLoaderScreens(GfxGrp, szLoaderSpecJpg, iLoaders, ChosenFilename, &pChosenGrp);
79  iLoaders+=SeekLoaderScreens(GfxGrp, szLoaderSpecJpeg, iLoaders, ChosenFilename, &pChosenGrp);
80  iLoaders*=2;
81  // seek for bmp-loaders
82  iLoaders+=SeekLoaderScreens(GfxGrp, szLoaderSpecBmp, iLoaders, ChosenFilename, &pChosenGrp);
83  // Still nothing found: fall back to general loader spec in main graphics group
84  if (!iLoaders)
85  {
86  iLoaders = SeekLoaderScreens(GfxGrp, "Loader*.png", 0, ChosenFilename, &pChosenGrp);
87  iLoaders += SeekLoaderScreens(GfxGrp, "Loader*.jpg", iLoaders, ChosenFilename, &pChosenGrp);
88  iLoaders += SeekLoaderScreens(GfxGrp, "Loader*.jpeg", iLoaders, ChosenFilename, &pChosenGrp);
89  }
90  // Not even default loaders available? Fail.
91  if (!iLoaders)
92  {
93  LogFatal(FormatString("No loaders found for loader specification: %s/%s/%s/%s", szLoaderSpecPng, szLoaderSpecBmp, szLoaderSpecJpg, szLoaderSpecJpeg).getData());
94  return false;
95  }
96  }
97 
98  // load loader
100  if (!fctBackground.Load(*pChosenGrp,ChosenFilename, C4FCT_Full,C4FCT_Full,true,0)) return false;
101 
102  // load info
103  if (szInfo) { delete [] szInfo; szInfo=nullptr; }
104 
105  // done, success!
106  return true;
107 }
108 
110 {
111  // enabled/disables drawing of loader screen
112  fBlackScreen = fIsBlack;
113  // will be updated when drawn next time
114 }
115 
116 int C4LoaderScreen::SeekLoaderScreens(C4Group &rFromGrp, const char *szWildcard, int iLoaderCount, char *szDstName, C4Group **ppDestGrp)
117 {
118  bool fFound;
119  int iLocalLoaders=0;
120  char Filename[_MAX_PATH+1];
121  for (fFound=rFromGrp.FindEntry(szWildcard, Filename); fFound; fFound=rFromGrp.FindNextEntry(szWildcard, Filename))
122  {
123  // loader found; choose it, if Daniel wants it that way
124  ++iLocalLoaders;
125  if (!UnsyncedRandom(++iLoaderCount))
126  {
127  // copy group and path
128  *ppDestGrp=&rFromGrp;
129  SCopy(Filename, szDstName, _MAX_PATH);
130  }
131  }
132  return iLocalLoaders;
133 }
134 
135 void C4LoaderScreen::Draw(C4Facet &cgo, int iProgress, C4LogBuffer *pLog, int Process)
136 {
137  // simple black screen loader?
138  if (fBlackScreen)
139  {
140  pDraw->FillBG();
141  return;
142  }
143  // cgo.X/Y is assumed 0 here...
144  // fixed positions for now
145  int iHIndent=20;
146  int iVIndent=20;
147  int iLogBoxHgt=84;
148  int iLogBoxMargin=2;
149  int iVMargin=5;
150  int iProgressBarHgt=15;
153  float fLogBoxFontZoom=1.0f;
154  // Background (loader)
156  // draw scenario title
157  pDraw->StringOut(Game.ScenarioTitle.getData(), TitleFont, 1.0f, cgo.Surface, cgo.Wdt-iHIndent, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin-iProgressBarHgt-iVMargin-TitleFont.GetLineHeight(), 0xdddddddd, ARight, false);
158  //
159  // draw progress bar
160  pDraw->DrawBoxDw(cgo.Surface, iHIndent, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin-iProgressBarHgt, cgo.Wdt-iHIndent, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin, 0xb0000000);
161  int iProgressBarWdt=cgo.Wdt-iHIndent*2-2;
163  {
164  ::GraphicsResource.fctProgressBar.DrawX(cgo.Surface, iHIndent+1, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin-iProgressBarHgt+1, iProgressBarWdt*iProgress/100, iProgressBarHgt-2);
165  }
166  else
167  {
168  pDraw->DrawBoxDw(cgo.Surface, iHIndent+1, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin-iProgressBarHgt+1, iHIndent+1+iProgressBarWdt*iProgress/100, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin-1, 0xb0ff0000);
169  }
170  pDraw->StringOut(FormatString("%i%%", iProgress).getData(), rProgressBarFont, 1.0f, cgo.Surface,
171  cgo.Wdt/2, cgo.Hgt-iVIndent-iLogBoxHgt-iVMargin-rProgressBarFont.GetLineHeight()/2-iProgressBarHgt/2, 0xffffffff,
172  ACenter, true);
173  // draw log box
174  if (pLog)
175  {
176  pDraw->DrawBoxDw(cgo.Surface, iHIndent, cgo.Hgt-iVIndent-iLogBoxHgt, cgo.Wdt-iHIndent, cgo.Hgt-iVIndent, 0x7f000000);
177  int iLineHgt=int(fLogBoxFontZoom*LogFont.GetLineHeight()); if (!iLineHgt) iLineHgt=5;
178  int iLinesVisible = (iLogBoxHgt-2*iLogBoxMargin)/iLineHgt;
179  int iX = iHIndent+iLogBoxMargin;
180  int iY = cgo.Hgt-iVIndent-iLogBoxHgt+iLogBoxMargin;
181  int32_t w,h;
182  for (int i = -iLinesVisible; i < 0; ++i)
183  {
184  const char *szLine = pLog->GetLine(i, nullptr, nullptr, nullptr);
185  if (!szLine || !*szLine) continue;
186  LogFont.GetTextExtent(szLine, w,h, true);
187  pDraw->TextOut(szLine,LogFont,fLogBoxFontZoom,cgo.Surface,iX,iY);
188  iY += h;
189  }
190  // append process text
191  if (Process)
192  {
193  iY -= h; iX += w;
194  pDraw->TextOut(FormatString("%i%%", (int) Process).getData(),LogFont,fLogBoxFontZoom,cgo.Surface,iX,iY);
195  }
196  }
197 }
const char * getData() const
Definition: StdBuf.h:442
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
int GetLineHeight() const
Definition: C4FontLoader.h:125
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
C4FacetSurface fctBackground
const char * GetLine(int iLineIndex, CStdFont **ppFont, DWORD *pdwClr, bool *pNewParagraph) const
Definition: C4LogBuf.cpp:244
C4Game Game
Definition: C4Globals.cpp:52
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
C4GroupSet GroupSet
Definition: C4Game.h:87
bool Load(C4Group &hGroup, const char *szName, int iWdt, int iHgt, bool fNoErrIfNotFound, int iFlags)
Definition: C4FacetEx.cpp:78
StdCopyStrBuf ScenarioTitle
Definition: C4Game.h:103
bool TextOut(const char *szText, CStdFont &rFont, float fZoom, C4Surface *sfcDest, float iTx, float iTy, DWORD dwFCol=0xffffffff, BYTE byForm=ALeft, bool fDoMarkup=true)
Definition: C4Draw.cpp:567
const int ARight
Definition: C4Surface.h:41
bool GetTextExtent(const char *szText, int32_t &rsx, int32_t &rsy, bool fCheckMarkup=true)
#define _MAX_PATH
C4GraphicsResource GraphicsResource
#define C4GSCnt_Loaders
Definition: C4GroupSet.h:35
bool StringOut(const char *szText, CStdFont &rFont, float fZoom, C4Surface *sfcDest, float iTx, float iTy, DWORD dwFCol=0xffffffff, BYTE byForm=ALeft, bool fDoMarkup=true)
Definition: C4Draw.cpp:576
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
void Draw(C4Facet &cgo, int iProgress=0, class C4LogBuffer *pLog=nullptr, int Process=0)
const int ACenter
Definition: C4Surface.h:41
void DrawX(C4Surface *sfcTarget, float iX, float iY, float iWdt, float iHgt, int32_t iPhaseX=0, int32_t iPhaseY=0) const
Definition: C4Facet.cpp:358
void DrawBoxDw(C4Surface *sfcDest, int iX1, int iY1, int iX2, int iY2, DWORD dwClr)
Definition: C4Draw.cpp:846
bool Close()
Definition: C4Group.cpp:755
C4Draw * pDraw
Definition: C4Draw.cpp:42
bool Open(C4Group &hGroup, const char *filename) const
Definition: C4Reloc.cpp:82
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:227
void DefaultExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:274
void DrawFullScreen(C4Facet &cgo)
Definition: C4Facet.cpp:184
C4Reloc Reloc
Definition: C4Reloc.cpp:21
virtual void FillBG(DWORD dwClr=0)=0
#define C4CFN_Graphics
Definition: C4Components.h:28
const char * GetError()
Definition: C4Group.cpp:504
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
float Hgt
Definition: C4Facet.h:118
bool Init(const char *szLoaderSpec)
C4Group * FindGroup(int32_t Contents, C4Group *pAfter=nullptr, bool fSamePrio=false)
Definition: C4GroupSet.cpp:155
void SetBlackScreen(bool fIsBlack)
C4Surface * Surface
Definition: C4Facet.h:117
void SetBackground()
Definition: C4Surface.h:92
const int C4FCT_Full
Definition: C4FacetEx.h:26
float Wdt
Definition: C4Facet.h:118
C4Surface & GetFace()
Definition: C4FacetEx.h:52
int SeekLoaderScreens(C4Group &rFromGrp, const char *szWildcard, int iLoaderCount, char *szDstName, C4Group **ppDestGrp)
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270