summaryrefslogtreecommitdiff
path: root/src/pig.c
blob: ece968c48ccf4bdd2b0b4df545f118f7710a041d (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 *  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 <assert.h>
#include <SDL.h>
#include <stdlib.h> // for rand()

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

#include "checker-node.h"
#include "clear-node.h"
#include "glad.h"
#include "m4f.h"
#include "macros.h"
#include "sad-node.h"
#include "shader-node.h"
#include "shader.h"
#include "v3f.h"

#define PIG_DEFAULT_WIDTH	640
#define PIG_DEFAULT_HEIGHT	480
#define PIG_DEFAULT_BPM		"125"
#define PIG_DEFAULT_RPB		"8"

//#define PIG_WINDOW_FLAGS	(SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL)
#define PIG_WINDOW_FLAGS	(SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI)

#if 0
#define PIG_ASPECT_RATIO	.7
#endif

typedef struct pig_t {
	SDL_Window	*window;
	SDL_GLContext	*gl;
	stage_t		*stage;
	stage_t		*sadface;
	unsigned	window_width, window_height;
	unsigned	windowed:1;
	m4f_t		transform;
	float		seed;
	v3f_t		color;

	struct {
		int			active;		/* is sync/rocket active? */
		char			*host;
		struct sync_device	*dev;
		int			beats_per_min;	/* BPM (PIG_DEFAULT_BPM, PIG_ROCKET_BPM) */
		int			rows_per_beat;	/* rows per beat (PIG_DEFAULT_RPB, PIG_ROCKET_RPB) */
		int			ms_per_row;	/* (1000 * 60) / (beats_per_min * rows_per_beat) */
		int			row;		/* current row */
		int			paused;
	} sync;
} pig_t;


static inline float randf(void)
{
	return rand() * (1.f / (float)RAND_MAX);
}


static void randomize_color(v3f_t *color)
{
	color->x = randf();
	color->y = randf();
	color->z = randf();

	if (v3f_length(color) < .8f)
		*color = v3f_normalize(color);
}


static void pig_sync_connect(pig_t *pig)
{
	if (sync_tcp_connect(pig->sync.dev, pig->sync.host, SYNC_DEFAULT_PORT))
		warn_if(pig->sync.active,
			"unable to connect rocket sync to \"%s\"", pig->sync.host);
	else
		pig->sync.active = 1;
}


static void pig_sync_setup(pig_t *pig)
{
	char	*sync_host = "localhost";
	char	*dev_name = "pig";
	char	*bpm = PIG_DEFAULT_BPM;
	char	*rpb = PIG_DEFAULT_RPB;
	char	*env;

	if ((env = getenv("PIG_ROCKET_HOST")))
		sync_host = env;

	if ((env = getenv("PIG_ROCKET_NAME")))
		dev_name = env;

	if ((env = getenv("PIG_ROCKET_BPM")))
		bpm = env;

	if ((env = getenv("PIG_ROCKET_RPB")))
		rpb = env;

	pig->sync.host = sync_host;
	pig->sync.beats_per_min = atoi(bpm);
	pig->sync.rows_per_beat = atoi(rpb);
	pig->sync.ms_per_row = (60 * 1000) / (pig->sync.beats_per_min * pig->sync.rows_per_beat);

	fatal_if(!(pig->sync.dev = sync_create_device(dev_name)),
		"unable to create rocket sync device \"%s\"", dev_name);

	pig_sync_connect(pig);
}


static void pig_sync_pause(void *ctxt, int flag)
{
	pig_t	*pig = ctxt;

	pig->sync.paused = flag;
}


static void pig_sync_set_row(void *ctxt, int row)
{
	pig_t	*pig = ctxt;

	pig->sync.row = row;
}


static int pig_sync_is_playing(void *ctxt)
{
	pig_t	*pig = ctxt;

	return !pig->sync.paused;
}


static void pig_sync_update(pig_t *pig)
{
	static struct sync_cb pig_sync_cb = {
		pig_sync_pause,
		pig_sync_set_row,
		pig_sync_is_playing,
	};

	if (sync_update(pig->sync.dev, pig->sync.row, &pig_sync_cb, pig))
		pig_sync_connect(pig);
}


static void pig_uniforms_func(shader_t *shader, void *uniforms_ctxt, void *render_ctxt, unsigned n_uniforms, const int *uniforms, const m4f_t *model_x, float alpha)
{
	play_t			*play = render_ctxt;
	pig_t			*pig = uniforms_ctxt;
	unsigned		t0, t1;
	float			r = randf();
	const shader_uniform_t	*active_uniforms;
	int			n_active_uniforms;

	if (play_ticks_elapsed(play, PLAY_TICKS_TIMER2, 1000)) {
		if (shader_reload_files(shader) < 0)
			stage_set_active(pig->sadface, 1);
		else
			stage_set_active(pig->sadface, 0);
	}

	/* this one just keeps increasing */
	t0 = play_ticks(play, PLAY_TICKS_TIMER0);

	/* this one resets every second and gets constrained to 0.f-1.f */
	t1 = play_ticks(play, PLAY_TICKS_TIMER1);
	if (t1 > 1000) {
		randomize_color(&pig->color);

		pig->seed = randf();
		t1 = play_ticks_reset(play, PLAY_TICKS_TIMER1);
	}

	if (pig->sync.active) {
		/* another timer advances the rkt row */
		if (play_ticks_elapsed(play, PLAY_TICKS_TIMER3, pig->sync.ms_per_row)) {
			if (!pig->sync.paused)
				pig->sync.row++;
		}

		pig_sync_update(pig);
	} else {
		/* periodically check if rkt editor is up */
		if (play_ticks_elapsed(play, PLAY_TICKS_TIMER3, 5000))
			pig_sync_connect(pig);
	}

	glUniform1f(uniforms[0], alpha);
	glUniformMatrix4fv(uniforms[1], 1, GL_FALSE, &model_x->m[0][0]);	// TODO: make transform manipulatable
	glUniform3f(uniforms[2], pig->color.x, pig->color.y, pig->color.z);	// TODO: make color configurable
	glUniform1f(uniforms[3], (float)t0 * .001f);
	glUniform1f(uniforms[4], (float)t1 * .001f);
	glUniform1f(uniforms[5], pig->seed);
	glUniform1f(uniforms[6], r);

	if (pig->sync.active) {
		/* look for float uniforms and ensure they're setup as sync tracks */
		/* TODO: we should probably handle int uniforms as well, just round what sync_get_val() gives back? */
		/* TODO: currently this is done as an override of the builtins, but it would be nice if sync_get_val()
		 * had some concept of being enabled; if the track is completely empty, sync_get_val() should give NaN
		 * or something, then we skip the glUniform1f() altogether, so an empty track could be used to leave
		 * the builtin value if applicable, while still allowing exposing those builtin uniforms to sequencing
		 * if desired.
		 */
		shader_active_uniforms(shader, &n_active_uniforms, &active_uniforms);
		for (int i = 0; i < n_active_uniforms; i++) {
			if (active_uniforms[i].type == GL_FLOAT) {
				const struct sync_track	*track = NULL;

				fatal_if(!(track = sync_get_track(pig->sync.dev, active_uniforms[i].name)),
					"unable to get track");

				glUniform1f(active_uniforms[i].location, sync_get_val(track, pig->sync.row));
			}
		}
	}
}

static const char *pig_uniforms[] = {
	"alpha",
	"model_x",
	"color",
	"time",
	"T",
	"seed",
	"rand"
};



static void * pig_init(play_t *play, int argc, char *argv[], unsigned flags)
{
	pig_t	*pig;

	fatal_if(argc != 3, "Usage: %s vertex.shader fragment.shader", argv[0]);

	pig = calloc(1, sizeof(pig_t));
	fatal_if(!pig, "Unable to allocate pig_t");

	pig->window_width = PIG_DEFAULT_WIDTH;
	pig->window_height =  PIG_DEFAULT_HEIGHT;
	pig->windowed = 1;

	fatal_if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) < 0,
		"Unable to set GL core profile attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2) < 0,
		"Unable to set GL major version attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1) < 0,
		"Unable to set GL minor version attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0,
		"Unable to set GL doublebuffer attribute");

	fatal_if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0,
		"Unable to set GL red size attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0,
		"Unable to set GL green size attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0,
		"Unable to set GL blue size attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) < 0,	/* this in particular is required for cache-node.c to work w/alpha */
		"Unable to set GL alpha size attribute");

//#define MSAA_RENDER_TARGET
#ifdef MSAA_RENDER_TARGET
	fatal_if(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4) < 0,
		"Unable to et GL multisample samples attribute");
	fatal_if(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) < 0,
		"Unable to set GL multisample buffers attribute");
#endif

/* On older SDL versions, this define is missing at compile-time,
 * but we can just provide it here and still try the sethint call,
 * just in case the runtime SDL version knows how to handle it.
 */
#ifndef SDL_HINT_TOUCH_MOUSE_EVENTS
#define SDL_HINT_TOUCH_MOUSE_EVENTS "SDL_TOUCH_MOUSE_EVENTS"
#endif

	warn_if(!SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0"),
		"Unable to suppress synthetic mouse events on touch");

	pig->window = SDL_CreateWindow("pig",
				SDL_WINDOWPOS_CENTERED,
				SDL_WINDOWPOS_CENTERED,
				pig->window_width, pig->window_height,
				PIG_WINDOW_FLAGS | (pig->windowed ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP));

	if (!pig->window) {
		fatal_if(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0) < 0,
			"Unable to clear GL multisample samples attribute");
		fatal_if(SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0) < 0,
			"Unable to clear GL multisample buffers attribute");

		pig->window = SDL_CreateWindow("pig",
					SDL_WINDOWPOS_CENTERED,
					SDL_WINDOWPOS_CENTERED,
					pig->window_width, pig->window_height,
					PIG_WINDOW_FLAGS | (pig->windowed ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP));

		fatal_if(!pig->window,
			"Unable to create SDL window");
	}

	pig->gl = SDL_GL_CreateContext(pig->window);
	fatal_if(!pig->gl,
		"Unable to create GL context");

	fatal_if(SDL_GL_SetSwapInterval(1) < 0,
		"Unable to enable vsync");

	fatal_if(!gladLoadGL(),
		"Failed to initialize GLAD OpenGL extension loader");

	//SDL_ShowCursor(SDL_DISABLE);

#ifdef MSAA_RENDER_TARGET
	glEnable(GL_MULTISAMPLE);
#endif

	pig->transform = m4f_identity();
	pig->seed = randf();
	randomize_color(&pig->color);

	pig->stage = stage_new(&(stage_conf_t){.name = "pig", .active = 1, .alpha = 1.f}, NULL, NULL);
	fatal_if(!pig->stage, "Unable to create new stage");

	(void) clear_node_new(&(stage_conf_t){
			.parent = pig->stage,
			.name = "gl-clear-pig",
			.active = 1
		}
	);

	(void) checker_node_new(&(stage_conf_t){
			.parent = pig->stage,
			.name = "checkers",
			.layer = 1,
			.active = 1,
			.alpha = 1.f,
		},
		&pig->transform,
		&pig->color
	);

	pig->sadface = sad_node_new(&(stage_conf_t){
			.parent = pig->stage,
			.name = "sadface",
			.layer = 3,
			.active = 0,
			.alpha = 1.f,
		},
		&pig->transform,
		&pig->color
	);

	(void) shader_node_new_files(&(stage_conf_t){
			.parent = pig->stage,
			.name = "shader",
			.layer = 2,
			.active = 1,
			.alpha = 1.f,
		},
		argv[1], argv[2],
		&pig->transform,
		pig_uniforms_func,
		pig,
		NELEMS(pig_uniforms),
		pig_uniforms
	);

	pig_sync_setup(pig);

	return pig;
}


static void pig_update(play_t *play, void *context)
{
	pig_t	*pig = context;

	stage_dirty(pig->stage);
}


static void pig_render(play_t *play, void *context)
{
	pig_t	*pig = context;

	if (stage_render(pig->stage, play)) {
		SDL_GL_SwapWindow(pig->window);
	} else {
		SDL_Delay(100);	// FIXME: this should be computed
	}
}


static void pig_dispatch(play_t *play, void *context, SDL_Event *event)
{
	pig_t	*pig = context;

	if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_RESIZED) {
		int	w, h;

		/* on highdpi the window size and pixels are decoupled, so ignore what
		 * the event contains and query the canvas size.
		 */
		SDL_GL_GetDrawableSize(pig->window, &w, &h);
		glViewport(0, 0, w, h);
		stage_dirty(pig->stage);
	}
}


const play_ops_t	pig_ops = {
	.init = pig_init,
	.update = pig_update,
	.render = pig_render,
	.dispatch = pig_dispatch,
//	.shutdown = pig_shutdown,
};
© All Rights Reserved