aboutsummaryrefslogtreecommitdiff
path: root/subprojects/nk_pugl/example/example.c
blob: 1c505daaa54659a269ee67fab767c5a50627e4a8 (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
/*
 * Copyright (c) 2016-2020 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 <unistd.h>
#include <signal.h>
#include <time.h>
#include <limits.h>

#define LEN NK_LEN
#define MAX NK_MAX

#define NK_PUGL_IMPLEMENTATION
#include <nk_pugl/nk_pugl.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
#include "nuklear/demo/overview.c"
#pragma GCC diagnostic pop

static volatile sig_atomic_t done = 0;

static void
_sigint(int signum __attribute__((unused)))
{
	done = 1;
}

static void
_expose(struct nk_context *ctx, struct nk_rect wbounds __attribute__((unused)),
	void *data __attribute__((unused)))
{
	overview(ctx);
}

int
main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
{
	static nk_pugl_window_t win;
	nk_pugl_config_t *cfg = &win.cfg;

	cfg->width = 1280;
	cfg->height = 720;
	cfg->resizable = true;
	cfg->parent = 0;
	cfg->threads = false;
	cfg->ignore = false;
	cfg->class = "nk_pugl_example";
	cfg->title = "Nk Pugl Example";
	cfg->expose = _expose;
	cfg->data = NULL;
	cfg->font.face = "./Cousine-Regular.ttf";
	cfg->font.size = 13;

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

	const intptr_t widget = nk_pugl_init(&win);
	if(!widget)
	{
		return 1;
	}

	nk_pugl_show(&win);

#if !defined(_WIN32) && !defined(__APPLE__)
	struct timespec tm;
	clock_gettime(CLOCK_MONOTONIC, &tm);
#endif

	while(!done)
	{
#if !defined(_WIN32) && !defined(__APPLE__)
		if(clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tm, NULL) != 0)
		{
			continue;
		}

#define NANOS 1000000000
		tm.tv_nsec += NANOS / 25;
		while(tm.tv_nsec >= NANOS)
		{
			tm.tv_sec += 1;
			tm.tv_nsec -= NANOS;
		}
#undef NANOS
#else
		usleep(1000000 / 25);
#endif

		done = nk_pugl_process_events(&win);
	}

	nk_pugl_hide(&win);

	nk_pugl_shutdown(&win);

	return 0;
}