1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (C) 2018 Vito Caputo - <vcaputo@pengaru.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <SDL.h>
#include <SDL_mixer.h>
#include <math.h>
#include "aabb.h"
#include "macros.h"
#include "whale.h"
#include "stage.h"
#include "svg-node.h"
/* the opening credits */
void whale_credits_event(whale_t *whale, SDL_Event *ev)
{
/* no events handled in credits, their brief */
}
typedef enum credits_fsm_t {
CREDITS_FSM_FADEOUT,
CREDITS_FSM_FADEIN,
CREDITS_FSM_PLANETS_FADEOUT,
CREDITS_FSM_CREDITS_FADEOUT,
} credits_fsm_t;
void whale_credits_update(whale_t *whale)
{
static stage_node_t *bobby, *crater, *planets, *credits;
static unsigned initialized;
static credits_fsm_t state;
Uint32 now = whale_ticks(whale, WHALE_TICKS_TIMER);
stage_t *stage = whale_get_stage(whale);
if (!initialized) {
whale_ticks_reset(whale, WHALE_TICKS_TIMER); /* get ticks approximately aligned with credits start */
bobby = stage_node_lookup_name(stage, "bobby");
fatal_if(!bobby, "Unable to lookup whale node");
crater = stage_node_lookup_name(stage, "crater");
fatal_if(!crater, "Unable to lookup crater node");
planets = stage_node_lookup_name(stage, "planets");
fatal_if(!crater, "Unable to lookup planets node");
credits = svg_node_new_file(stage, "credits", "assets/credits.svg", 0, (aabb_t){{-.8f, -.8f}, {.8f, .8f}});
fatal_if(!credits, "Unable to load credits svg");
now = whale_ticks(whale, WHALE_TICKS_TIMER);
initialized = 1;
}
switch (state) {
case CREDITS_FSM_FADEOUT:
if (now < 2000) {
float t = 1.0f - ((float)now) * (1.0f / 2000.0f);
stage_node_set_alpha(stage, bobby, t);
stage_node_set_alpha(stage, crater, t);
} else {
stage_node_free(stage, bobby);
stage_node_free(stage, crater);
stage_node_set_active(stage, credits);
state++;
}
break;
case CREDITS_FSM_FADEIN:
if (now < 4000) {
stage_node_set_alpha(stage, planets, 1.0f - ((float)(now - 2000)) * (1.0f / 15000.0f));
stage_node_set_alpha(stage, credits, ((float)(now - 2000)) * (1.0f / 2000.0f));
} else {
stage_node_set_alpha(stage, credits, 1.0f);
state++;
}
break;
case CREDITS_FSM_PLANETS_FADEOUT:
if (now < 17000) {
stage_node_set_alpha(stage, planets, 1.0f - ((float)(now - 2000)) * (1.0f / 15000.0f));
} else {
stage_node_free(stage, planets);
state++;
}
break;
case CREDITS_FSM_CREDITS_FADEOUT:
if (now < 20000) {
stage_node_set_alpha(stage, credits, 1.0f - ((float)(now - 17000)) * (1.0f / 3000.0f));
} else {
stage_node_free(stage, credits);
exit(0);
}
break;
default:
assert(0);
}
if (!Mix_PlayingMusic() || now > 20000)
exit(0);
}
|