forked from sam-itt/sofis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
541 lines (485 loc) · 15 KB
/
main.c
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <samuel.cuella@gmail.com>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "base-gauge.h"
#include "basic-hud.h"
#include "dialogs/direct-to-dialog.h"
#include "side-panel.h"
#include "map-gauge.h"
#include "resource-manager.h"
#include "sdl-colors.h"
#include "widgets/base-widget.h"
#if ENABLE_3D
#include "terrain-viewer.h"
#endif
#define ENABLE_FGCONN 1
#define ENABLE_FGTAPE 1
#define ENABLE_SENSORS 1
#define ENABLE_STRATUX 1
#define ENABLE_MOCK 1
#include "data-source.h"
#if ENABLE_FGCONN
#include "fg-data-source.h"
#endif
#if ENABLE_FGTAPE
#include "fg-tape-data-source.h"
#endif
#if ENABLE_SENSORS
#include "sensors-data-source.h"
#endif
#if ENABLE_STRATUX
#include "stratux-data-source.h"
#endif
#if ENABLE_MOCK
#include "mock-data-source.h"
#endif
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define N_COLORS 4
typedef enum{
MODE_FGREMOTE,
MODE_FGTAPE,
MODE_SENSORS,
MODE_STRATUX,
MODE_MOCK,
N_MODES
}RunningMode;
BasicHud *hud = NULL;
SidePanel *panel = NULL;
MapGauge *map = NULL;
DirectToDialog *ddt = NULL;
bool g_show3d = false;
DataSource *g_ds;
RunningMode g_mode;
/*Return true to quit the app*/
bool handle_keyboard(SDL_KeyboardEvent *event, Uint32 elapsed)
{
AttitudeData new_attitude;
LocationData new_location;
bool attitude_dirty = false;
bool location_dirty = false;
new_attitude = g_ds->attitude;
new_location = g_ds->location;
if(ddt && ddt->visible)
base_widget_handle_event(BASE_WIDGET(ddt), event);
switch(event->keysym.sym){
/*App control*/
case SDLK_ESCAPE:
if(event->state == SDL_PRESSED)
return true;
break;
case SDLK_SPACE:
if(event->state == SDL_PRESSED)
g_show3d = !g_show3d;
hud->attitude->mode = (g_show3d) ? AI_MODE_3D : AI_MODE_2D;
break;
case SDLK_RETURN:
if(event->state == SDL_PRESSED){
if(g_mode == MODE_FGTAPE)
((FGTapeDataSource*)(g_ds))->playing = !((FGTapeDataSource*)(g_ds))->playing;
}
break;
case SDLK_p:
if(event->state == SDL_PRESSED){
printf("Pitch: %f\nHeading: %f\n",
g_ds->attitude.pitch,
g_ds->attitude.heading
);
}
break;
/*MapGauge controls*/
case SDLK_KP_8: /*keypad up arrows*/
if(event->state == SDL_PRESSED){
map_gauge_manipulate_viewport(map, 0, -10, true);
}
break;
case SDLK_KP_2: /*keypad down arrows*/
if(event->state == SDL_PRESSED){
map_gauge_manipulate_viewport(map, 0, 10, true);
}
break;
case SDLK_KP_4: /*keypad left arrows*/
if(event->state == SDL_PRESSED){
map_gauge_manipulate_viewport(map, -10, 0, true);
}
break;
case SDLK_KP_6: /*keypad right arrows*/
if(event->state == SDL_PRESSED){
map_gauge_manipulate_viewport(map, 10, 0, true);
}
break;
case SDLK_KP_PLUS:
if(event->state == SDL_PRESSED){
map_gauge_set_level(map, map->level+1);
}
break;
case SDLK_KP_MINUS:
if(event->state == SDL_PRESSED){
map_gauge_set_level(map, map->level-1);
}
break;
case SDLK_KP_DIVIDE:
if(event->state == SDL_PRESSED){
map_gauge_center_on_marker(map, true);
}
break;
/*Go to dialog*/
case SDLK_g:
if(event->state == SDL_PRESSED){
if(!ddt)
ddt = direct_to_dialog_new();
else
direct_to_dialog_reset(ddt);
ddt->visible = true;
}
break;
/*Manual camera/position control*/
case SDLK_z:
if(event->state == SDL_PRESSED){
new_attitude.pitch += 1.0;
attitude_dirty = true;
}
break;
case SDLK_s:
if(event->state == SDL_PRESSED){
new_attitude.pitch -= 1.0;
attitude_dirty = true;
}
break;
case SDLK_q:
if(event->state == SDL_PRESSED){
new_attitude.roll -= 1.0;
attitude_dirty = true;
}
break;
case SDLK_d:
if(event->state == SDL_PRESSED){
new_attitude.roll += 1.0;
attitude_dirty = true;
}
break;
case SDLK_a:
if(event->state == SDL_PRESSED){
new_attitude.heading -= 1.0;
new_attitude.heading = fmodf(new_attitude.heading, 360.0);
attitude_dirty = true;
}
break;
case SDLK_e:
if(event->state == SDL_PRESSED){
new_attitude.heading += 1.0;
new_attitude.heading = fmodf(new_attitude.heading, 360.0);
attitude_dirty = true;
}
break;
case SDLK_PAGEUP:
if(event->state == SDL_PRESSED){
new_location.altitude += 10;
attitude_dirty = true;
}
break;
case SDLK_PAGEDOWN:
if(event->state == SDL_PRESSED){
new_location.altitude -= 10;
attitude_dirty = true;
}
break;
}
if(attitude_dirty)
data_source_set_attitude(g_ds, &new_attitude);
if(location_dirty)
data_source_set_location(g_ds, &new_location);
return false;
}
/*Return true to quit the app*/
bool handle_events(Uint32 elapsed)
{
SDL_Event event;
while(SDL_PollEvent(&event) == 1){
switch(event.type){
case SDL_QUIT:
return true;
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_CLOSE)
return true;
break;
case SDL_KEYUP:
case SDL_KEYDOWN:
return handle_keyboard(&(event.key), elapsed);
break;
}
}
return false;
}
const char *pretty_mode(RunningMode mode)
{
switch(mode){
case MODE_FGREMOTE:
return "FGDataSource";
case MODE_FGTAPE:
return "FGTapeDataSource";
case MODE_SENSORS:
return "SensorsDataSource";
case MODE_STRATUX:
return "StratuxDataSource";
case MODE_MOCK:
return "MockDataSource";
default:
return "Unknown!";
}
}
#if ENABLE_3D
void update_terrain_viewer_location(TerrainViewer *self, LocationData *newv)
{
double lon = fmod(newv->super.longitude+180, 360.0) - 180;
plane_set_position(self->plane, newv->super.latitude, lon, newv->altitude/3.281 + 2);
self->dirty = true;
/*printf("Position set to: lat:%f lon:%f alt:%f\n", newv->super.latitude, lon, newv->altitude);*/
/*exit(0);*/
}
void update_terrain_viewer_attitude(TerrainViewer *self, AttitudeData *newv)
{
plane_set_attitude(self->plane, newv->roll, newv->pitch, newv->heading);
self->dirty = true;
}
#endif
int main(int argc, char **argv)
{
Uint32 colors[N_COLORS];
bool done;
int i;
float oldv[5] = {0,0,0,0,0};
RenderTarget rtarget;
g_mode = MODE_FGTAPE;
if(argc > 1){
if(!strcmp(argv[1], "--sensors"))
g_mode = MODE_SENSORS;
else if(!strcmp(argv[1], "--fgtape"))
g_mode = MODE_FGTAPE;
else if(!strcmp(argv[1], "--fgremote"))
g_mode = MODE_FGREMOTE;
else if(!strcmp(argv[1], "--stratux"))
g_mode = MODE_STRATUX;
else if(!strcmp(argv[1], "--mock"))
g_mode = MODE_MOCK;
}
switch(g_mode){
case MODE_SENSORS:
g_ds = (DataSource *)sensors_data_source_new();
break;
case MODE_FGREMOTE:
g_ds = (DataSource *)fg_data_source_new(6789);
break;
case MODE_STRATUX:
g_ds = (DataSource *)stratux_data_source_new();
break;
case MODE_MOCK:
g_ds = (DataSource*)mock_data_source_new();
break;
case MODE_FGTAPE: //Fallthtough
default:
g_ds = (DataSource *)fg_tape_data_source_new("fg-io/fg-tape/dr400.fgtape", 120);
break;
}
if(!g_ds){
printf("Couldn't create DataSource (%s), bailing out\n", pretty_mode(g_mode));
exit(EXIT_FAILURE);
}
data_source_set(g_ds);
#if USE_SDL_GPU
GPU_Target* gpu_screen = NULL;
GPU_SetRequiredFeatures(GPU_FEATURE_BASIC_SHADERS);
#if USE_GLES
gpu_screen = GPU_InitRenderer(GPU_RENDERER_GLES_2, SCREEN_WIDTH, SCREEN_HEIGHT, GPU_DEFAULT_INIT_FLAGS);
#else
gpu_screen = GPU_InitRenderer(GPU_RENDERER_OPENGL_2, SCREEN_WIDTH, SCREEN_HEIGHT, GPU_DEFAULT_INIT_FLAGS);
#endif
if(gpu_screen == NULL){
GPU_LogError("Initialization Error: Could not create a renderer with proper feature support for this demo.\n");
return 1;
}
rtarget.target = gpu_screen;
#else
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 1;
}
window = SDL_CreateWindow(
"HUD testbench",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (window == NULL) {
fprintf(stderr, "could not create window: %s\n", SDL_GetError());
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
if(!screenSurface){
printf("Error: %s\n",SDL_GetError());
exit(-1);
}
rtarget.surface = screenSurface;
colors[0] = SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF);
colors[1] = SDL_MapRGB(screenSurface->format, 0xFF, 0x00, 0x00);
colors[2] = SDL_MapRGB(screenSurface->format, 0x00, 0xFF, 0x00);
colors[3] = SDL_MapRGB(screenSurface->format, 0x11, 0x56, 0xFF);
#endif
SDL_ShowCursor(SDL_DISABLE);
SDL_Rect whole = {0,0,640,480};
hud = basic_hud_new();
panel = side_panel_new(-1, -1);
SDL_Rect sprect = {0,0, base_gauge_w(BASE_GAUGE(panel)),base_gauge_h(BASE_GAUGE(panel))};
map = map_gauge_new(190,150);
map->level = 7;
SDL_Rect maprect = {SCREEN_WIDTH-200,SCREEN_HEIGHT-160,base_gauge_w(BASE_GAUGE(map)),base_gauge_h(BASE_GAUGE(map))};
SDL_Rect ddtrect ={
640/2,
480/2 - 100,
12*20,
304
};
#if ENABLE_3D
TerrainViewer *viewer;
viewer = terrain_viewer_new(-0.2);
#endif
done = false;
Uint32 ticks;
Uint32 last_ticks = 0;
Uint32 elapsed = 0;
Uint32 acc = 0;
i = 3;
Uint32 startms, dtms, last_dtms;
Uint32 nframes = 0;
Uint32 render_start, render_end;
Uint32 total_render_time = 0;
Uint32 nrender_calls = 0;
#if ENABLE_3D
g_show3d = true;
#endif
hud->attitude->mode = (g_show3d) ? AI_MODE_3D : AI_MODE_2D;
if(g_mode == MODE_FGREMOTE)
fg_data_source_banner((FGDataSource*)g_ds);
data_source_add_events_listener(g_ds, hud, 3,
ATTITUDE_DATA, basic_hud_attitude_changed,
DYNAMICS_DATA, basic_hud_dynamics_changed,
LOCATION_DATA, basic_hud_location_changed
);
data_source_add_listener(g_ds, ENGINE_DATA, &(ValueListener){
.callback = (ValueListenerFunc)side_panel_engine_data_changed,
.target = panel
});
data_source_add_events_listener(g_ds, map, 3,
LOCATION_DATA, map_gauge_location_changed,
ATTITUDE_DATA, map_gauge_attitude_changed,
ROUTE_DATA, map_gauge_route_changed
);
#if ENABLE_3D
data_source_add_events_listener(g_ds, viewer, 2,
LOCATION_DATA, update_terrain_viewer_location,
ATTITUDE_DATA, update_terrain_viewer_attitude
);
#endif
data_source_print_listener_stats(g_ds);
printf("Waiting for fix.");
do{
data_source_frame(DATA_SOURCE(g_ds), 0);
printf(".");
fflush(stdout);
sleep(1); /*sleep for 1 sec*/
}while(!DATA_SOURCE(g_ds)->has_fix);
printf("\n");
last_dtms = 0;
startms = SDL_GetTicks();
do{
ticks = SDL_GetTicks();
elapsed = ticks - last_ticks;
dtms = ticks - startms;
done = handle_events(elapsed);
if(data_source_frame(DATA_SOURCE(g_ds), dtms - last_dtms)){
last_dtms = dtms;
#if ENABLE_3D
if(last_ticks == 0){ //Do an invisible frame to trigger preload
GPU_FlushBlitBuffer(); /*begin 3*/
terrain_viewer_frame(viewer);
GPU_ResetRendererState(); /*end 3d*/
/*Reset startms as initial loading can take up a while*/
startms = SDL_GetTicks();
}
#endif
}
#if USE_SDL_GPU
GPU_ClearRGB(gpu_screen, 0x11, 0x56, 0xFF);
#else
SDL_FillRect(screenSurface, NULL, SDL_UFBLUE(screenSurface));
#endif
#if ENABLE_3D
if(g_show3d){
GPU_FlushBlitBuffer(); /*begin 3*/
glDisable(GL_BLEND);
terrain_viewer_frame(viewer);
GPU_ResetRendererState(); /*end 3d*/
}
#endif
render_start = SDL_GetTicks();
base_gauge_render(BASE_GAUGE(hud), elapsed, &(RenderContext){rtarget, &whole, NULL});
base_gauge_render(BASE_GAUGE(panel), elapsed, &(RenderContext){rtarget, &sprect, NULL});
base_gauge_render(BASE_GAUGE(map), elapsed, &(RenderContext){rtarget, &maprect, NULL});
if(ddt && ddt->visible)
base_gauge_render(BASE_GAUGE(ddt), elapsed, &(RenderContext){rtarget, &ddtrect, NULL});
render_end = SDL_GetTicks();
total_render_time += render_end - render_start;
nrender_calls++;
#if USE_SDL_GPU
GPU_Flip(gpu_screen);
#else
SDL_UpdateWindowSurface(window);
#endif
nframes++;
acc += elapsed;
if(elapsed < 20){
SDL_Delay(20 - elapsed);
}
if(acc >= 1000){ /*1sec*/
int h,m,s;
h = dtms/3600000;
dtms -= dtms/3600000 * h;
m = dtms / 60000;
dtms -= 60000 * m;
s = dtms / 1000;
printf("%02d:%02d:%02d Current FPS: %03d\r",h,m,s, (1000*nframes)/elapsed);
fflush(stdout);
nframes = 0;
acc = 0;
}
i %= N_COLORS;
last_ticks = ticks;
}while(!done);
printf("Average rendering time (%d samples): %f ticks\n", nrender_calls, total_render_time*1.0/nrender_calls);
base_gauge_free(BASE_GAUGE(hud));
base_gauge_free(BASE_GAUGE(panel));
base_gauge_free(BASE_GAUGE(map));
data_source_free(DATA_SOURCE(g_ds));
resource_manager_shutdown();
#if ENABLE_3D
terrain_viewer_free(viewer);
texture_store_shutdown();
#endif
#if USE_SDL_GPU
GPU_Quit();
#else
SDL_DestroyWindow(window);
SDL_Quit();
#endif
return 0;
}