summaryrefslogtreecommitdiff
path: root/src/main.c
blob: f30238cbdd0b607e1907898e843a85413aabf1c6 (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
#include <signal.h>
#include <time.h>

#include "common.h"  
#include "physics.h"
#include "renderer.h"
#include "ui.h"
#include "console.h"

int main(void)
{
	int rv = 0;
	r_window *first_window;
	phy_sim sim;
	SDL_Thread *sim_thread, *con_thread;

	if (r_init()) {
		con_printf("fatal error: renderer initialization failed\n");
		rv = 1;
		goto quit;
	}

	if (ui_init()) {
		con_printf("fatal error: UI initialization failed\n");
		rv = 1;
		goto quit;
	}

	signal(SIGINT, SIG_DFL);
	signal(SIGTERM, SIG_DFL);

	first_window = r_window_create();
	if (!first_window) {
		con_printf("fatal error: couldn't open first window\n");
		rv = 1;
		goto quit;
	}

	ui_renderer_window_register(first_window);

	if (phy_sim_create(&sim)) {
		con_printf("fatal error: phy_sim_create failed\n");
		rv = 1;
		goto quit;
	}

	sim_thread = SDL_CreateThread((SDL_ThreadFunction)phy_thread,
	                              "phy_thread", &sim);
	if (!sim_thread) {
		con_printf("fatal error: SDL_CreateThread failed: %s\n",
		           SDL_GetError());
		rv = 1;
		goto quit;
	}

	if (con_init()) {
		con_printf("fatal error: console initialization failed\n");
		rv = 1;
		goto quit;
	}

	con_thread = SDL_CreateThread((SDL_ThreadFunction)con_thread_f,
	                               "con_thread", &sim);
	if (!con_thread) {
		con_printf("fatal error: SDL_CreateThread failed: %s\n",
		           SDL_GetError());
		rv = 1;
		goto quit;
	}

	while (1) {
		SDL_Event event;

		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT)
				goto quit;
			else
				ui_event(&event);
		}

		ui_draw(&sim);
	}

quit:
	con_printf("main: exitting, rv=%i\n", rv);

	itc_chan_push(&sim.ctl, PHY_CMD_QUIT, NULL);
	SDL_WaitThread(sim_thread, NULL);
	phy_sim_destroy(&sim);

	ui_quit();
	r_quit();
	SDL_Quit();
	return rv;
}