aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/example/d2tk_pugl.c
blob: 4c9bc2e8f059da28525a898456309098204a262a (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
/*
 * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <signal.h>

#include <d2tk/frontend_pugl.h>
#include "example/example.h"

typedef struct _app_t app_t;

struct _app_t {
	d2tk_pugl_t *dpugl;
};

static sig_atomic_t done = false;

static void
_sig(int signum __attribute__((unused)))
{
	done = true;
}

static int
_expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
{
	app_t *app = data;
	d2tk_pugl_t *dpugl = app->dpugl;
	d2tk_base_t *base = d2tk_pugl_get_base(dpugl);

	d2tk_example_run(base, w, h);

	return EXIT_SUCCESS;
}

int
main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
{
	static app_t app;

	const float scale = d2tk_pugl_get_scale();
	d2tk_coord_t w = scale * 1280;
	d2tk_coord_t h = scale * 720;

	int c;
	while( (c = getopt(argc, argv, "w:h:")) != -1)
	{
		switch(c)
		{
			case 'w':
			{
				w = atoi(optarg);
			} break;
			case 'h':
			{
				h = atoi(optarg);
			} break;

			default:
			{
				fprintf(stderr, "Usage: %s\n"
					"  -w  width\n"
					"  -h  height\n\n",
					argv[0]);
			} return EXIT_FAILURE;
		}
	}

	const d2tk_pugl_config_t config = {
		.parent = 0,
		.bundle_path = "./",
		.min_w = w/4,
		.min_h = h/4,
		.w = w,
		.h = h,
		.fixed_size = false,
		.fixed_aspect = false,
		.expose = _expose,
		.data = &app
	};

	signal(SIGINT, _sig);
	signal(SIGTERM, _sig);
#if !defined(_WIN32)
	signal(SIGQUIT, _sig);
	signal(SIGKILL, _sig);
#endif

	app.dpugl = d2tk_pugl_new(&config, NULL);
	if(app.dpugl)
	{
		d2tk_example_init();

		d2tk_pugl_run(app.dpugl, &done);

		d2tk_pugl_free(app.dpugl);

		d2tk_example_deinit();

		return EXIT_SUCCESS;
	}

	return EXIT_FAILURE;
}