summaryrefslogtreecommitdiff
path: root/src/hungrycat.c
blob: d31b8e3ca34c8bc5f94756c679c3a8eb0e21f46c (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 *  Copyright (C) 2020 - 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 <SDL.h>
#include <assert.h>

#include <play.h>
#include <stage.h>

#include "glad.h"
#include "hungrycat-node.h"
#include "m4f.h"
#include "m4f-3dx.h"
#include "macros.h"
#include "sars.h"

#define HUNGRYCAT_FADE_MS	3333

#define HUNGRYCAT_FADEIN_MS	HUNGRYCAT_FADE_MS
#define HUNGRYCAT_SHOW_MS	HUNGRYCAT_FADE_MS
#define HUNGRYCAT_FADEOUT_MS	HUNGRYCAT_FADE_MS

#define HUNGRYCAT_FADE_TIMER	PLAY_TICKS_TIMER0

typedef enum hungrycat_state_t {
	HUNGRYCAT_STATE_DELAY,
	HUNGRYCAT_STATE_FADEIN,
	HUNGRYCAT_STATE_SHOW,
	HUNGRYCAT_STATE_FADEOUT,
	HUNGRYCAT_STATE_LEAVE,
} hungrycat_state_t;

typedef struct hungrycat_t {
	hungrycat_state_t	state;
	stage_t			*node;
	m4f_t			model_x;
} hungrycat_t;


static void * hungrycat_init(play_t *play, int argc, char *argv[], unsigned flags)
{
	sars_t		*sars = play_context(play, SARS_CONTEXT_SARS);
	hungrycat_t	*hungrycat;

	hungrycat = calloc(1, sizeof(hungrycat_t));
	fatal_if(!hungrycat, "Unable to allocate hungrycat_t");

	hungrycat->node = hungrycat_node_new(&(stage_conf_t){ .parent = sars->stage, .name = "hungrycat", .active = 1 }, &sars->projection_x, &hungrycat->model_x);
	fatal_if(!hungrycat->node, "Unable to create hungrycat->node");

	hungrycat->model_x = m4f_identity();
	hungrycat->model_x = m4f_scale(&hungrycat->model_x, &(v3f_t){1.f, .25f, .5f});

	return hungrycat;
}


static void hungrycat_enter(play_t *play, void *context)
{
	hungrycat_t	*hungrycat = context;

	assert(hungrycat);

	play_ticks_reset(play, HUNGRYCAT_FADE_TIMER);
}


static float fade_t(play_t *play)
{
	return (1.f / (float)HUNGRYCAT_FADE_MS) * (float)play_ticks(play, HUNGRYCAT_FADE_TIMER);
}


static void hungrycat_update(play_t *play, void *context)
{
	sars_t		*sars = play_context(play, SARS_CONTEXT_SARS);
	hungrycat_t	*hungrycat = context;

	assert(hungrycat);

	switch (hungrycat->state) {
	case HUNGRYCAT_STATE_DELAY:
		stage_dirty(hungrycat->node);
		if (!play_ticks_elapsed(play, HUNGRYCAT_FADE_TIMER, sars->delay_seconds * 1000))
			break;

		play_music_set(play, 0, "assets/hungrycat.ogg");
		hungrycat->state = HUNGRYCAT_STATE_FADEIN;
		break;

	case HUNGRYCAT_STATE_FADEIN:
		stage_dirty(hungrycat->node);
		if (!play_ticks_elapsed(play, HUNGRYCAT_FADE_TIMER, HUNGRYCAT_FADE_MS)) {
			stage_set_alpha(hungrycat->node, fade_t(play));
			break;
		}

		stage_set_alpha(hungrycat->node, 1.f);
		hungrycat->state = HUNGRYCAT_STATE_SHOW;
		break;

	case HUNGRYCAT_STATE_SHOW:
		if (!play_ticks_elapsed(play, HUNGRYCAT_FADE_TIMER, HUNGRYCAT_FADE_MS))
			break;

		hungrycat->state = HUNGRYCAT_STATE_FADEOUT;
		break;

	case HUNGRYCAT_STATE_FADEOUT:
		stage_dirty(hungrycat->node);
		if (!play_ticks_elapsed(play, HUNGRYCAT_FADE_TIMER, HUNGRYCAT_FADE_MS)) {
			stage_set_alpha(hungrycat->node, 1.f - fade_t(play));
			break;
		}

		stage_set_alpha(hungrycat->node, 0.f);
		hungrycat->state = HUNGRYCAT_STATE_LEAVE;
		break;

	case HUNGRYCAT_STATE_LEAVE:
		play_context_enter(play, SARS_CONTEXT_GAME);
		break;

	default:
		assert(0);
	}
}


static void hungrycat_leave(play_t *play, void *context)
{
	hungrycat_t	*hungrycat = context;

	assert(hungrycat);

	/* we never reenter this context since it's just a splash, so
	 * the context leave is effectively the shutdown, cleanup.
	 */
	stage_free(hungrycat->node);
	free(hungrycat);
	/* XXX: this is icky though, play_context(SARS_CONTEXT_HUNGRYCAT) will
	 * return a dangling pointer!  Not that it occurs anywhere though.
	 */
}


static void hungrycat_dispatch(play_t *play, void *context, SDL_Event *event)
{
	hungrycat_t	*hungrycat = context;

	assert(hungrycat);

	/* global handlers */
	sars_dispatch(play, context, event);

	switch (event->type) {
	case SDL_KEYDOWN:
		if (event->key.keysym.sym == SDLK_ESCAPE)
			exit(0);
		/* fallthrough */
	case SDL_FINGERDOWN:
		if (hungrycat->state != HUNGRYCAT_STATE_DELAY)
			play_context_enter(play, SARS_CONTEXT_GAME);
		break;

	default:
		break;
	}
}


const play_ops_t	hungrycat_ops = {
	.init = hungrycat_init,
	.update = hungrycat_update,
	.render = sars_render,
	.dispatch = hungrycat_dispatch,
	.enter = hungrycat_enter,
	.leave = hungrycat_leave,
};
© All Rights Reserved