Debian Codegraph @

src/loading_screens.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2022 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "loading_screens.h"
29
30 #include <cdogs/draw/draw.h>
31 #include <cdogs/files.h>
32 #include <cdogs/font.h>
33 #include <cdogs/map_build.h>
34 #include <cdogs/pic_manager.h>
35 #include <cdogs/pics.h>
36 #include <cdogs/tile_class.h>
37
38 LoadingScreen gLoadingScreen;
39
40 void LoadingScreenInit(LoadingScreen *l, GraphicsDevice *g)
41 {
42 memset(l, 0, sizeof *l);
43 l->g = g;
44 }
45 void LoadingScreenTerminate(LoadingScreen *l)
46 {
47 CArrayTerminate(&l->panelIndices);
48 }
49
50 static void LazyLoad(LoadingScreen *l)
51 {
52 if (l->logo == NULL)
53 {
54 l->logo = PicManagerGetPic(&gPicManager, "logo");
55 }
56 if (l->panel == NULL)
57 {
58 l->panel = PicManagerGetPic(&gPicManager, "bigpanel");
59 }
60
61 // Init panel array large enough to cover the entire screen
62 if (l->panel)
63 {
64 const struct vec2i panelsSize = svec2i_add(svec2i_divide(l->g->cachedConfig.Res, l->panel->size), svec2i_one());
65 if (panelsSize.x * panelsSize.y != (int)l->panelIndices.size)
66 {
67 CArrayTerminate(&l->panelIndices);
68 CArrayInit(&l->panelIndices, sizeof(int));
69 for (int i = 0; i < panelsSize.x * panelsSize.y; i++)
70 {
71 CArrayPushBack(&l->panelIndices, &i);
72 }
73 CArrayShuffle(&l->panelIndices);
74 }
75 }
76
77 if (gSoundDevice.isInitialised && l->sndTick == NULL)
78 {
79 l->sndTick = StrSound("click");
80 }
81 }
82
83 static void LoadingScreenDrawInner(
84 LoadingScreen *l, const char *loadingText, const float showPct)
85 {
86 LazyLoad(l);
87
88 if (l->panel)
89 {
90 const int stride = l->g->cachedConfig.Res.x / l->panel->size.x + 1;
91 CA_FOREACH(const int, panelIdx, l->panelIndices)
92 if (_ca_index > (int)(l->panelIndices.size * showPct))
93 {
94 break;
95 }
96 const int y = *panelIdx / stride;
97 const int x = *panelIdx - (y * stride);
98 PicRender(
99 l->panel, l->g->gameWindow.renderer, svec2i_multiply(svec2i(x, y), l->panel->size), colorWhite, 0,
100 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
101 CA_FOREACH_END()
102 }
103
104 if (l->logo)
105 {
106 const struct vec2i pos = svec2i(
107 CENTER_X(svec2i_zero(), l->g->cachedConfig.Res, l->logo->size.x),
108 CENTER_Y(
109 svec2i_zero(), svec2i_scale_divide(l->g->cachedConfig.Res, 2),
110 l->logo->size.y));
111 PicRender(
112 l->logo, l->g->gameWindow.renderer, pos, colorWhite, 0,
113 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
114 }
115
116 FontOpts opts = FontOptsNew();
117 opts.HAlign = ALIGN_CENTER;
118 opts.VAlign = ALIGN_CENTER;
119 opts.Area = svec2i(l->g->cachedConfig.Res.x, l->g->cachedConfig.Res.y / 2);
120 FontStrOpt(loadingText, svec2i(0, l->g->cachedConfig.Res.y / 2), opts);
121 }
122 void LoadingScreenDraw(
123 LoadingScreen *l, const char *loadingText, const float showPct)
124 {
125 WindowContextPreRender(&l->g->gameWindow);
126
127 LoadingScreenDrawInner(l, loadingText, showPct);
128
129 WindowContextPostRender(&l->g->gameWindow);
130
131 SoundPlayAtPlusDistance(&gSoundDevice, l->sndTick, gSoundDevice.earLeft1, 255);
132
133 SDL_Delay(70);
134 }
135
136 typedef struct
137 {
138 LoadingScreen *l;
139 bool ascending;
140 const char *loadingText;
141 float showPct;
142 GameLoopData *nextLoop;
143 bool removeParent;
144 int count;
145 } ScreenLoadingData;
146 static void LoopTerminate(GameLoopData *data);
147 static GameLoopResult LoopUpdate(GameLoopData *data, LoopRunner *l);
148 static void LoopDraw(GameLoopData *data);
149
150 GameLoopData *ScreenLoading(
151 const char *loadingText, const bool ascending, GameLoopData *nextLoop, const bool removeParent)
152 {
153 ScreenLoadingData *sData;
154 CCALLOC(sData, sizeof *sData);
155 sData->l = &gLoadingScreen;
156 sData->loadingText = loadingText;
157 sData->ascending = ascending;
158 sData->removeParent = removeParent;
159 sData->showPct = ascending ? 0.0f : 1.0f;
160 sData->nextLoop = nextLoop;
161 GameLoopData *data = GameLoopDataNew(
162 sData, LoopTerminate, NULL, NULL, NULL, LoopUpdate, LoopDraw);
163 data->DrawParent = true;
164 return data;
165 }
166
167 static void LoopTerminate(GameLoopData *data)
168 {
169 ScreenLoadingData *sData = data->Data;
170 CFREE(sData);
171 }
172 static GameLoopResult LoopUpdate(GameLoopData *data, LoopRunner *l)
173 {
174 ScreenLoadingData *sData = data->Data;
175 const bool complete = sData->showPct == (sData->ascending ? 1.0f : 0.0f);
176 if ((sData->count % 2) == 0)
177 {
178 SoundPlayAtPlusDistance(&gSoundDevice, sData->l->sndTick, gSoundDevice.earLeft1, 255);
179 }
180 sData->count++;
181 if (complete)
182 {
183 // Remove current loop, but copy its data before destroying it
184 ScreenLoadingData sDataCopy = *sData;
185 sData = NULL;
186 LoopRunnerPop(l);
187 if (sDataCopy.ascending)
188 {
189 if (sDataCopy.removeParent)
190 {
191 LoopRunnerPop(l);
192 }
193 if (sDataCopy.nextLoop)
194 {
195 LoopRunnerPush(l, sDataCopy.nextLoop);
196 // Show a loading screen with tiles animating out
197 LoopRunnerPush(l, ScreenLoading(sDataCopy.loadingText, false, NULL, false));
198 }
199 }
200 return UPDATE_RESULT_OK;
201 }
202 sData->showPct += sData->ascending ? 0.04f : -0.06f;
203 sData->showPct = CLAMP(sData->showPct, 0.0f, 1.0f);
204 return UPDATE_RESULT_DRAW;
205 }
206 static void LoopDraw(GameLoopData *data)
207 {
208 ScreenLoadingData *sData = data->Data;
209 LoadingScreenDrawInner(sData->l, sData->loadingText, sData->showPct);
210 }