summaryrefslogtreecommitdiff
path: root/src/whale.c
blob: 17cbb5d2f845acfc410be5dbaa2a9dbf118e2223 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *  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 <SDL.h>
#include <SDL_mixer.h>
#include <assert.h>
#include <stdlib.h>	/* exit, atexit */

#include "credits.h"
#include "whale.h"
#include "game.h"
#include "intro.h"
#include "macros.h"
#include "stage.h"

/* whale - Blender 2018 entry */
/* whales / doing astrophysics / crater lake */

#define DEFAULT_WIDTH	800
#define DEFAULT_HEIGHT	450
#define WINDOW_FLAGS	(SDL_WINDOW_RESIZABLE)

typedef struct whale_t {
	struct {
		SDL_Window	*window;
		SDL_Renderer	*renderer;

		Mix_Music	*music;
		Uint32		tick_offsets[2];
	} sdl;

	whale_context_t	context;
	stage_t		*stage;
} whale_t;


stage_t * whale_get_stage(const whale_t *whale)
{
	return whale->stage;
}


void whale_set_music(whale_t *whale, const char *file, unsigned flags)
{
	int	loop = (flags & WHALE_MUSIC_FLAG_LOOP) ? -1 : 0;
	int	fade = (flags & WHALE_MUSIC_FLAG_FADEIN);

	if (whale->sdl.music) {
		Mix_HaltMusic();
		Mix_FreeMusic(whale->sdl.music);
		whale->sdl.music = NULL;
	}

	whale->sdl.music = Mix_LoadMUS(file);
	fatal_if(!whale->sdl.music,
		"Unable to load music \"%s\"", file);
	Mix_VolumeMusic(32);	/* XXX: just hard-coding music volume for now */
	if (fade) {
		fatal_if(Mix_FadeInMusic(whale->sdl.music, loop, 1000),
			"Unable to play music");
	} else {
		fatal_if(Mix_PlayMusic(whale->sdl.music, loop),
			"Unable to play music");
	}
}


void whale_set_context(whale_t *whale, whale_context_t context)
{
	whale->context = context;
}


/* return ticks counter */
unsigned whale_ticks(whale_t *whale, whale_ticks_t type)
{
	return SDL_GetTicks() - whale->sdl.tick_offsets[type];
}


/* reset ticks counter to begin counting from now */
void whale_ticks_reset(whale_t *whale, whale_ticks_t type)
{
	whale->sdl.tick_offsets[type] = SDL_GetTicks();
}


static void whale_startup(whale_t *whale)
{
	fatal_if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0,
		"Unable to initialize SDL");
	fatal_if(atexit(SDL_Quit),
		"Unable to set exit handler");

	fatal_if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) == -1,
		"Unable to open audio");

	whale->sdl.window = SDL_CreateWindow("whale",
				SDL_WINDOWPOS_UNDEFINED,
				SDL_WINDOWPOS_UNDEFINED,
				DEFAULT_WIDTH, DEFAULT_HEIGHT,
				WINDOW_FLAGS);
	fatal_if(!whale->sdl.window,
		"Unable to create SDL window");

	whale->sdl.renderer = SDL_CreateRenderer(whale->sdl.window, -1,
				SDL_RENDERER_PRESENTVSYNC);
	fatal_if(!whale->sdl.renderer,
		"Unable to create SDL renderer");

	whale->stage = stage_new(whale->sdl.renderer, 1.7777f);
	fatal_if(!whale->stage,
		"Unable to create new stage");
}


static void whale_update(whale_t *whale)
{
	switch (whale->context) {
	case WHALE_CONTEXT_INTRO:
		whale_intro_update(whale);
		break;

	case WHALE_CONTEXT_GAME:
		whale_game_update(whale);
		break;

	case WHALE_CONTEXT_CREDITS:
		whale_credits_update(whale);
		break;

	default:
		assert(0);
	}

	stage_render(whale->stage);
	SDL_RenderPresent(whale->sdl.renderer);
}


static void whale_event(whale_t *whale, SDL_Event *event)
{
	switch (whale->context) {
	case WHALE_CONTEXT_INTRO:
		whale_intro_event(whale, event);
		break;

	case WHALE_CONTEXT_GAME:
		whale_game_event(whale, event);
		break;

	case WHALE_CONTEXT_CREDITS:
		whale_credits_event(whale, event);
		break;

	default:
		assert(0);
	}
}


static int whale_shutdown(whale_t *whale)
{
	stage_free(whale->stage);
	SDL_DestroyRenderer(whale->sdl.renderer);
	SDL_DestroyWindow(whale->sdl.window);
	Mix_CloseAudio();

	return EXIT_SUCCESS;
}


static void whale_loop(whale_t *whale)
{
	for (;;) {
		SDL_Event	ev;

		while (SDL_PollEvent(&ev)) {
			switch (ev.type) {
			case SDL_APP_TERMINATING:
			case SDL_QUIT:
				return;

			case SDL_WINDOWEVENT:
				break;

			case SDL_MOUSEMOTION:
				break;

			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				break;

			case SDL_MOUSEWHEEL:
				break;

			case SDL_FINGERDOWN:
			case SDL_FINGERUP:
			case SDL_FINGERMOTION:
				break;

			case SDL_KEYDOWN:
			case SDL_KEYUP:
				break;

			default:
				break;
			}

			whale_event(whale, &ev);
		}

		whale_update(whale);
	}
}


int main(int argc, char *argv[])
{
	whale_t	whale = {};

	whale_startup(&whale);
	whale_loop(&whale);

	return whale_shutdown(&whale);
}
© All Rights Reserved