Debian Codegraph @

src/cdogs/hud/hud_num_popup.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2017, 2019, 2021 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "hud_num_popup.h"
30
31 #include "actors.h"
32 #include "font.h"
33 #include "gamedata.h"
34 #include "hud_defs.h"
35 #include "player_hud.h"
36
37 // Total number of milliseconds that the numeric popup lasts for
38 #define TIMER_MS 1000
39 #define TIMER_OBJECTIVE_MS 2000
40
41 void HUDNumPopupsInit(
42 HUDNumPopups *popups, const struct MissionOptions *mission)
43 {
44 memset(popups, 0, sizeof *popups);
45 CArrayInitFillZero(
46 &popups->objective, sizeof(HUDNumPopup),
47 mission->missionData->Objectives.size);
48 }
49
50 void HUDNumPopupsTerminate(HUDNumPopups *popups)
51 {
52 CArrayTerminate(&popups->objective);
53 }
54
55 static void MergePopups(HUDNumPopup *dst, const HUDNumPopup src);
56 void HUDNumPopupsAdd(
57 HUDNumPopups *popups, const HUDNumPopupType type, const int idxOrUID,
58 const int amount)
59 {
60 HUDNumPopup s;
61 memset(&s, 0, sizeof s);
62
63 // Index
64 int localPlayerIdx = -1;
65 switch (type)
66 {
67 case NUMBER_POPUP_SCORE:
68 localPlayerIdx = FindLocalPlayerIndex(idxOrUID);
69 if (localPlayerIdx < 0)
70 {
71 // This popup was for a non-local player; abort
72 return;
73 }
74 s.u.PlayerUID = idxOrUID;
75 break;
76 case NUMBER_POPUP_OBJECTIVE:
77 s.u.ObjectiveIndex = idxOrUID;
78 break;
79 default:
80 CASSERT(false, "unknown HUD popup type");
81 break;
82 }
83
84 s.Amount = amount;
85
86 // Timers
87 switch (type)
88 {
89 case NUMBER_POPUP_SCORE:
90 s.Timer = TIMER_MS;
91 s.TimerMax = TIMER_MS;
92 break;
93 case NUMBER_POPUP_OBJECTIVE:
94 s.Timer = TIMER_OBJECTIVE_MS;
95 s.TimerMax = TIMER_OBJECTIVE_MS;
96 break;
97 default:
98 CASSERT(false, "unknown HUD popup type");
99 break;
100 }
101
102 // Merge with existing popups
103 switch (type)
104 {
105 case NUMBER_POPUP_SCORE:
106 MergePopups(&popups->score[localPlayerIdx], s);
107 break;
108 case NUMBER_POPUP_OBJECTIVE:
109 MergePopups(CArrayGet(&popups->objective, s.u.ObjectiveIndex), s);
110 break;
111 default:
112 CASSERT(false, "unknown HUD popup type");
113 break;
114 }
115 }
116 static void MergePopups(HUDNumPopup *dst, const HUDNumPopup src)
117 {
118 // Combine popup amounts
119 if (dst->Timer <= 0)
120 {
121 // Old popup finished; simply replace with new
122 dst->Amount = src.Amount;
123 }
124 else
125 {
126 // Add the updates
127 dst->Amount += src.Amount;
128 }
129 dst->Timer = src.Timer;
130 dst->TimerMax = src.TimerMax;
131 dst->u.PlayerUID = src.u.PlayerUID;
132 }
133
134 static void NumPopupUpdate(HUDNumPopup *p, const int ms);
135 void HUDPopupsUpdate(HUDNumPopups *popups, const int ms)
136 {
137 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
138 {
139 NumPopupUpdate(&popups->score[i], ms);
140 }
141 CA_FOREACH(HUDNumPopup, p, popups->objective)
142 NumPopupUpdate(p, ms);
143 CA_FOREACH_END()
144 }
145 static void NumPopupUpdate(HUDNumPopup *p, const int ms)
146 {
147 if (p->Timer > 0)
148 {
149 p->Timer -= ms;
150 }
151 }
152
153 static void DrawNumUpdate(const HUDNumPopup *p, FontOpts opts);
154
155 void HUDNumPopupsDrawPlayer(
156 const HUDNumPopups *popups, const int idx, const int drawFlags,
157 const Rect2i r)
158 {
159 const HUDNumPopup *u = &popups->score[idx];
160 if (!IsScoreNeeded(gCampaign.Entry.Mode))
161 {
162 return;
163 }
164 if (u->Amount == 0)
165 {
166 return;
167 }
168 const PlayerData *p = PlayerDataGetByUID(u->u.PlayerUID);
169 if (!IsPlayerAlive(p))
170 return;
171 const FontOpts opts = PlayerHUDGetScorePos(drawFlags, r);
172 DrawNumUpdate(u, opts);
173 }
174
175 void HUDNumPopupsDrawObjective(
176 const HUDNumPopups *popups, const int idx, const struct vec2i pos)
177 {
178 const HUDNumPopup *p = CArrayGet(&popups->objective, idx);
179 FontOpts opts = FontOptsNew();
180 opts.Area = gGraphicsDevice.cachedConfig.Res;
181 opts.Pad = pos;
182 DrawNumUpdate(p, opts);
183 }
184
185 // Parameters that define how the numeric update is animated
186 // The update animates in the following phases:
187 // 1. Pop up from text position to slightly above
188 // 2. Fall down from slightly above back to text position
189 // 3. Persist over text position
190 #define NUM_UPDATE_POP_UP_DURATION_MS 100
191 #define NUM_UPDATE_FALL_DOWN_DURATION_MS 100
192 #define NUM_UPDATE_POP_UP_HEIGHT 5
193 static void DrawNumUpdate(const HUDNumPopup *p, FontOpts opts)
194 {
195 if (p->Timer <= 0 || p->Amount == 0)
196 {
197 return;
198 }
199 color_t color = p->Amount > 0 ? colorGreen : colorRed;
200
201 struct vec2i pos = svec2i_zero();
202 char s[50];
203 sprintf(s, "%s%d", p->Amount > 0 ? "+" : "", p->Amount);
204
205 // Now animate the popup based on its stage
206 int timer = p->TimerMax - p->Timer;
207 if (timer < NUM_UPDATE_POP_UP_DURATION_MS)
208 {
209 // popup is still popping up
210 // calculate height
211 int popupHeight =
212 timer * NUM_UPDATE_POP_UP_HEIGHT / NUM_UPDATE_POP_UP_DURATION_MS;
213 opts.Pad.y -= popupHeight;
214 }
215 else if (
216 timer <
217 NUM_UPDATE_POP_UP_DURATION_MS + NUM_UPDATE_FALL_DOWN_DURATION_MS)
218 {
219 // popup is falling down
220 // calculate height
221 timer -= NUM_UPDATE_POP_UP_DURATION_MS;
222 timer = NUM_UPDATE_FALL_DOWN_DURATION_MS - timer;
223 int popupHeight = timer * NUM_UPDATE_POP_UP_HEIGHT /
224 NUM_UPDATE_FALL_DOWN_DURATION_MS;
225 opts.Pad.y -= popupHeight;
226 }
227 else
228 {
229 // Change alpha so that the popup fades away
230 color.a = (uint8_t)CLAMP(p->Timer * 255 * 2 / p->TimerMax, 0, 255);
231 }
232
233 opts.Mask = color;
234 FontStrOpt(s, pos, opts);
235 }