plot_sdl.c 90.2 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * SDL functions (plot curves and handles keyboard, joystick... inputs) functions
 *
 * author: Nicolas Van der Noot
 */

#ifdef SDL

9
#include "useful_functions.h"
10
#include "plot_sdl.h"
Timothee Habra's avatar
Timothee Habra committed
11
#include "cmake_sdl_config.h"
12
13
14
15
#include "time_functions.h"
#include "events_sdl.h"
#include "set_plot.h"
#include "realtime_functions.h"
16
#include "mbs_message.h"
17

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
18
19
20
21
#ifdef JAVA
#include "java_functions.h"
#endif

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
22
23
24
25
#ifdef OPEN_GL
#include "open_gl_c_int.h"
#endif

26
27
28
#define TIME_NO_INTERACTION_BREAK 3e6 ///< time eith no interaction to go on break mode [us]
#define TIME_SDL_DELAY 25             ///< time delay for SDL

Timothee Habra's avatar
Timothee Habra committed
29
//#define SDL_FILES_PATH ROBOTRAN_SOURCE_DIR"/mbs_common/mbs_realtime/sdl/SDL_files"
30

31
32
33
34
35
/*! \brief initialize the Screen_sdl structure
 * 
 * \param[in] options real-time options
 * \return the initialized SDL main data structure
 */
36
Screen_sdl* init_screen_sdl(Realtime_option *options)
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
{
    // -- Variables declaration -- //

    int i, j;
    int screen_width, screen_height;
    int max_nb_curves, buffer_size;

    #ifdef UNIX
    int x_SDL_position;
    int min_x_SDL_position;
    #endif

    double t0;

    // genral structure
    Screen_sdl *screen_sdl;

    // SDL window
    SDL_Window *win;

    // SDL Renderer
    SDL_Renderer *ren;

    // SDL colors
    SDL_Color text_color;

63
#ifdef UNIX
64
    // display information
65
66
    SDL_DisplayMode current;
#endif    
67
68
69
70
71
72
73
74

    // -- Structure initialization -- //

    screen_sdl = (Screen_sdl*) malloc(sizeof(Screen_sdl));

    screen_width  = options->screen_width;
    screen_height = options->screen_height;

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
75
76
    max_nb_curves = options->max_nb_curves;
    buffer_size   = options->buffer_size;
77
78

    // initialize SDL
79
    SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1");
80
81
82
83
84
85
86
87
88
89
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) != 0)
    {
        log_SDL_error("SDL_Init");
    }

    screen_sdl->nb_joysticks = SDL_NumJoysticks();

    if (screen_sdl->nb_joysticks >= 1)
    {
        #ifdef PRINT_REPORT
90
        mbs_msg("Number of joysticks detected: %d\n\n", screen_sdl->nb_joysticks);
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
        #endif

        screen_sdl->joysticks = (SDL_Joystick**) malloc(screen_sdl->nb_joysticks*sizeof(SDL_Joystick*));

        for(i=0; i<screen_sdl->nb_joysticks; i++)
        {
            screen_sdl->joysticks[i] = SDL_JoystickOpen(i);

            if ( screen_sdl->joysticks[i] == NULL )
            {
                fprintf(stderr,"Error to open the joystick !\n");
            }
        }

        SDL_JoystickEventState(SDL_ENABLE);
    }

    #ifdef UNIX

    // display information
    if(SDL_GetCurrentDisplayMode(0, &current))
    {
        log_SDL_error("SDL_GetCurrentDisplayMode");
    }

    // SDL window X position
    x_SDL_position     = (int) (0.75 * current.w - screen_width/2.0);
    min_x_SDL_position = current.w / 2;

    if (x_SDL_position < min_x_SDL_position)
    {
        x_SDL_position = min_x_SDL_position;
    }

    // window
    win = SDL_CreateWindow(
        "Real-time graph",                   // window title
        x_SDL_position,                      // initial x position
        SDL_WINDOWPOS_CENTERED,              // initial y position
        screen_width,                        // width, in pixels
        screen_height,                       // height, in pixels
        SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL // flags
    );

    #else

    // window
    win = SDL_CreateWindow(
        "Real-time graph",                   // window title
        SDL_WINDOWPOS_CENTERED,              // initial x position
        SDL_WINDOWPOS_CENTERED,              // initial y position
        screen_width,                        // width, in pixels
        screen_height,                       // height, in pixels
        SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL // flags
    );

    #endif

    // pixel size related
    screen_sdl->curve_width   = options->curve_width;
    screen_sdl->screen_width  = options->screen_width;
    screen_sdl->screen_height = options->screen_height;

    // screen width related
    screen_sdl->plot_x_end = (screen_width - SCREEN_WIDTH_LOSS);
    screen_sdl->screen_plot_width = (screen_sdl->plot_x_end - PLOT_X_START);
    screen_sdl->x_label_width = (screen_sdl->screen_plot_width / X_LABEL_NB);

    // screen height related
    screen_sdl->plot_y_end = (screen_height - SCREEN_BOTTOM_HEIGHT_LOSS);
    screen_sdl->plot_y_mid = ((PLOT_Y_START + screen_sdl->plot_y_end) / 2);
    screen_sdl->screen_plot_height = (screen_sdl->plot_y_end - PLOT_Y_START);
    screen_sdl->y_label_height = (screen_sdl->screen_plot_height / Y_LABEL_NB);

    // bottom indications: y axis
    screen_sdl->bottom_mid_y_indications = (screen_height - (SCREEN_BOTTOM_HEIGHT_LOSS / 2));
    screen_sdl->scaling_indications = (screen_sdl->bottom_mid_y_indications + 3);
    screen_sdl->x_axis_indication = (screen_sdl->bottom_mid_y_indications - 8);
    screen_sdl->y_axis_indication = (screen_sdl->bottom_mid_y_indications + 10);
    screen_sdl->speed_indication_1 = (screen_sdl->bottom_mid_y_indications - 4);
    screen_sdl->speed_indication_2 = (screen_sdl->bottom_mid_y_indications + 14);
    screen_sdl->legend_speed_indication_1 = (screen_sdl->speed_indication_1 - 5);
    screen_sdl->legend_speed_indication_2 = (screen_sdl->speed_indication_2 - 5);
    screen_sdl->scaling_bottom_indications = (screen_height - (SCREEN_BOTTOM_HEIGHT_LOSS / 3)+2);
    screen_sdl->scaling_bottom_arrow_indications_1 = (screen_sdl->scaling_bottom_indications + 2);
    screen_sdl->scaling_bottom_arrow_indications_2 = (screen_sdl->scaling_bottom_indications);
    screen_sdl->scaling_bottom_arrow_indications_3 = (screen_sdl->scaling_bottom_indications + 5);
    screen_sdl->scaling_up_indications = (screen_height - ((2*SCREEN_BOTTOM_HEIGHT_LOSS) / 3));
    screen_sdl->scaling_up_indications_2 = (screen_sdl->scaling_up_indications + 2);

    if (win == NULL) 
    {
        log_SDL_error("SDL_CreateWindow");
    }
    screen_sdl->win = win;

    // renderer
    ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE);

    // the next line is faster, but it causes problems on some computers
    //ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); 
    if (ren == NULL)
    {
        log_SDL_error("SDL_CreateRenderer");
    }
    SDL_RenderClear(ren);
    screen_sdl->ren = ren;

    // TTF (SDL text)
    if (TTF_Init() != 0)
    {
        log_SDL_error("TTF_Init");
    }

    // font for thes axes
206
    screen_sdl->font_axis = init_font(SDL_FILES_PATH"/HunDIN1451.ttf", FONT_AXIS_SIZE);
207
208

    // font for the labels
209
    screen_sdl->font_label = init_font(SDL_FILES_PATH"/MonospaceTypewriter.ttf", FONT_LABEL_SIZE);
210
211

    // font for the small labels
212
    screen_sdl->font_small_label = init_font(SDL_FILES_PATH"/MonospaceTypewriter.ttf", FONT_SMALL_LABEL_SIZE);
213
214

    // font for the scaling
215
    screen_sdl->font_scaling = init_font(SDL_FILES_PATH"/arrow_7.ttf", FONT_SCALING);
216
217

    // font for the arrows
218
    screen_sdl->font_arrows = init_font(SDL_FILES_PATH"/Arrows.ttf", FONT_ARROWS);
219
220

    // font for the play icon
221
    screen_sdl->font_play = init_font(SDL_FILES_PATH"/Arrows_tbf.ttf", FONT_PLAY_BREAK);
222
223

    // font for the horizontal mouse
224
    screen_sdl->font_hor_mouse = init_font(SDL_FILES_PATH"/Arrows_tbf.ttf", FONT_HOR_MOUSE);
225
226

    // font for the mouse (horizontal and vertical)
227
    screen_sdl->font_mouse = init_font(SDL_FILES_PATH"/arrow_7.ttf", FONT_MOUSE);
228
229

    // font for the break icon
230
    screen_sdl->font_break = init_font(SDL_FILES_PATH"/PIZZADUDEBULLETS.ttf", FONT_PLAY_BREAK);
231
232

    // font for the speed of the simulation
233
    screen_sdl->font_speed = init_font(SDL_FILES_PATH"/Arrows.ttf", FONT_SPEED);
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


    // text color: black
    text_color.r = 0;
    text_color.g = 0;
    text_color.b = 0;
    text_color.a = 0;
    screen_sdl->text_color = text_color;

    // index of the sdl window and vectors
    screen_sdl->index_vec = 0;
    screen_sdl->index_sdl = 0;

    // time for a pixel
    screen_sdl->tsim_pixel = 1.0 / options->fqc_screen;

    // x axis bounds
    screen_sdl->x_min = 0.0;
    screen_sdl->x_max = 0.0;

    // y axis bounds
    screen_sdl->y_min = options->y_min_init;
    screen_sdl->y_max = options->y_max_init;

    screen_sdl->last_y_min = screen_sdl->y_min;
    screen_sdl->last_y_max = screen_sdl->y_max;

    screen_sdl->last_legend_x_min = screen_sdl->x_min;
    screen_sdl->last_legend_x_max = screen_sdl->x_max;

    screen_sdl->last_legend_y_min = screen_sdl->y_min;
    screen_sdl->last_legend_y_max = screen_sdl->y_max;

    // time limits
    t0 = options->t0;

    screen_sdl->min_tsim = t0;
    screen_sdl->max_tsim = t0 + SAFETY_TIME;

    screen_sdl->min_tsim_index = 0;
    screen_sdl->max_tsim_index = buffer_size-1;
    
    screen_sdl->flag_started = 0;
    screen_sdl->flag_buffer_round = 0;

    // x label
    screen_sdl->x_label_print_count = 0;
    screen_sdl->last_x_label_flag   = 0;

    // scaling
    screen_sdl->scaling_flag      = 0;
    screen_sdl->last_scaling_flag = 0;

    // break
    screen_sdl->break_plot_flag = 0;

    // string exponents
    screen_sdl->expo_y = 0;
    screen_sdl->expo_x = 0;

    // flag: horizontal plot
    screen_sdl->hor_plot_scaling = 0;

    // flag: bottom indications
    screen_sdl->bottom_flag = 1;

    // flags: modify y axis
    screen_sdl->increase_plot_y_diff_flag = 0;
    screen_sdl->decrease_plot_y_diff_flag = 0;
    screen_sdl->up_plot_y_flag = 0;
    screen_sdl->bottom_plot_y_flag = 0;

    // flags: modify x axis
    screen_sdl->increase_plot_x_diff_flag = 0;
    screen_sdl->decrease_plot_x_diff_flag = 0;
    screen_sdl->left_plot_y_flag  = 0;
    screen_sdl->right_plot_y_flag = 0;

    screen_sdl->cur_nb_curves = 0;

    // full vectors save 
    screen_sdl->y_save = (double**) malloc(max_nb_curves * sizeof(double*));

    screen_sdl->previous_y_vec = (double*) malloc(max_nb_curves * sizeof(double));

    for(i=0; i<max_nb_curves; i++)
    {
        screen_sdl->previous_y_vec[i] = 0.0;
    }

    // y values tab for SDL
    screen_sdl->y_tab = (double**) malloc(max_nb_curves * sizeof(double*));
    for (i=0; i<max_nb_curves; i++)
    {
        screen_sdl->y_tab[i] = (double*) malloc(screen_sdl->screen_plot_width * sizeof(double));

        for(j=0; j<screen_sdl->screen_plot_width; j++)
        {
            screen_sdl->y_tab[i][j] = 0.0;
        }
    }

    // minimal and maximal values (for auto-scaling)
    screen_sdl->y_tab_min = (double*) malloc(max_nb_curves * sizeof(double));
    screen_sdl->y_tab_max = (double*) malloc(max_nb_curves * sizeof(double));

    screen_sdl->scaling_expo_mult = (int*) malloc(max_nb_curves*sizeof(int));
    screen_sdl->last_scaling_expo_mult = (int*) malloc(max_nb_curves*sizeof(int));

    screen_sdl->scaling_offset  = (double*) malloc(max_nb_curves*sizeof(double));
    screen_sdl->last_scaling_offset  = (double*) malloc(max_nb_curves*sizeof(double));
    
    for(i=0; i<max_nb_curves; i++)
    {
        screen_sdl->y_tab_min[i] = START_VECTOR;
        screen_sdl->y_tab_max[i] = START_VECTOR;

        screen_sdl->scaling_expo_mult[i]      = 0;
        screen_sdl->last_scaling_expo_mult[i] = 0;

        screen_sdl->scaling_offset[i]      = 0.0;
        screen_sdl->last_scaling_offset[i] = 0.0;
    }

    // full time save
    screen_sdl->tsim_save = (double*) malloc(buffer_size * sizeof(double));

    for(i=0; i<buffer_size; i++)
    {
        screen_sdl->tsim_save[i] = t0;
    }

    // buffer size
    screen_sdl->buffer_size = buffer_size;

    // time vector for SDL
    screen_sdl->t_vec = (double*) malloc(screen_sdl->screen_plot_width * sizeof(double));
    for (i=0; i<screen_sdl->screen_plot_width; i++)
    {
        screen_sdl->t_vec[i] = t0;
    }

    // colors
    for (i=0; i<NB_COLORS_SDL; i++)
    {
        screen_sdl->color_tab[i] = options->color_tab[i];
    }   

    screen_sdl->last_color = -1;

    // auto-scaling
    screen_sdl->plot_auto_scaling        = 0;
    screen_sdl->signal_auto_scaling      = 0;
    screen_sdl->last_plot_auto_scaling   = 0;
    screen_sdl->last_signal_auto_scaling = 0;
    screen_sdl->change_mouse_wheel_flag  = 0;

    screen_sdl->nb_legends = 0;
    screen_sdl->max_nb_legends = options->max_nb_legends;

    screen_sdl->max_nb_curves = max_nb_curves;

    // auto plot
    init_set_plot(screen_sdl);

    // clean the screen for the first time
    refresh_screen(screen_sdl);

    // return structure
    return screen_sdl;
}

/*! \brief update for new user curves
 * 
 * \param[in,out] screen_sdl SDL main data structure
 * \param[in] nb_new_curves number of new curves
410
 * \param[in] y_curves current y values
411
 */
412
void new_curves_save(Screen_sdl *screen_sdl, int nb_new_curves, double *y_curves)
413
414
415
416
417
418
419
420
421
422
423
{
    int i, j;
    int cur_nb_curves, buffer_size;

    cur_nb_curves   = screen_sdl->cur_nb_curves;
    buffer_size = screen_sdl->buffer_size;

    for(i=cur_nb_curves; i<cur_nb_curves+nb_new_curves; i++)
    {
        screen_sdl->y_save[i] = (double*) malloc(buffer_size * sizeof(double));

424
        for(j=0; j<=screen_sdl->min_tsim_index; j++)
425
        {
426
427
428
429
430
431
            screen_sdl->y_save[i][j] = y_curves[i];
        }

        for(j=screen_sdl->min_tsim_index+1; j<buffer_size; j++)
        {
            screen_sdl->y_save[i][j] = 0.0;
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
        }
    }
        
    screen_sdl->cur_nb_curves += nb_new_curves;
}

/*! \brief initialize a new Font variable (pointer)
 * 
 * \param[in] folder folder with Fonts
 * \param[in] font_size font size
 * \return requested Font variable (pointer)
 */
TTF_Font* init_font(char *folder, int font_size)
{
    TTF_Font* font;

    font = TTF_OpenFont(folder, font_size);

    if (font == NULL)
    {
        log_SDL_error("TTF_OpenFont");
    }

    return font;
}

/*! \brief free the main SDL structure
 * 
 * \param[out] screen_sdl SDL main data structure
 */
void free_screen_sdl(Screen_sdl *screen_sdl)
{
    // variable declaration
    int i;

    // fonts
    TTF_CloseFont(screen_sdl->font_label);
    TTF_CloseFont(screen_sdl->font_small_label);
    TTF_CloseFont(screen_sdl->font_axis);
    TTF_CloseFont(screen_sdl->font_scaling);
    TTF_CloseFont(screen_sdl->font_arrows);
    TTF_CloseFont(screen_sdl->font_play);
    TTF_CloseFont(screen_sdl->font_hor_mouse);
    TTF_CloseFont(screen_sdl->font_mouse);
    TTF_CloseFont(screen_sdl->font_break);
    TTF_CloseFont(screen_sdl->font_speed);

    // joystick
    if (screen_sdl->nb_joysticks >= 1)
    {
        for(i=0; i<screen_sdl->nb_joysticks; i++)
        {
            SDL_JoystickClose(screen_sdl->joysticks[i]);
        }
        free(screen_sdl->joysticks);
    }

    // SDL main variables
    SDL_DestroyRenderer(screen_sdl->ren);
    SDL_DestroyWindow(screen_sdl->win);
    SDL_Quit();

    free(screen_sdl->tsim_save);    

    free(screen_sdl->t_vec);

    
    for (i=0; i<screen_sdl->max_nb_curves; i++)
    {
        free(screen_sdl->y_tab[i]);
    }

    for (i=0; i<screen_sdl->cur_nb_curves; i++)
    {
        free(screen_sdl->y_save[i]);
    }

    free(screen_sdl->y_tab);
    free(screen_sdl->y_save);
    free(screen_sdl->previous_y_vec);
    free(screen_sdl->y_tab_min);
    free(screen_sdl->y_tab_max);
    free(screen_sdl->scaling_expo_mult);
    free(screen_sdl->last_scaling_expo_mult);
    free(screen_sdl->scaling_offset);
    free(screen_sdl->last_scaling_offset);

    free_set_plot();

    if (screen_sdl->nb_legends)
    {
        free(screen_sdl->pixel_pos_label);
    }

    free(screen_sdl);
}

/*! \brief show an error message and exit the simulation
 * 
 * \param[in] SDL_function SDL function name
 */
void log_SDL_error(char *SDL_function)
{
535
    mbs_msg("\t >SDL> %s error: %s\n",SDL_function, SDL_GetError());
536
    mbs_error_msg("[%d] in log_SDL_error !! \n", -410);
537
538
539
540
541
542
543
544
545
546
547
548
549
}

/*! \brief change the color in SDL
 * 
 * \param[in,out] screen_sdl SDL main data structure
 * \param[in] color_num color number
 */
void SDL_set_color(Screen_sdl *screen_sdl, int color_num)
{
    // variable decalration
    SDL_Renderer *ren;

    // safety
550
    while(color_num >= NB_COLORS_TOT_SDL)
551
    {
552
        color_num -= NB_COLORS_TOT_SDL;
553
554
555
    }
    while(color_num < 0)
    {
556
        color_num += NB_COLORS_TOT_SDL;
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
    }
    
    // no change in the color since previous time
    if (screen_sdl->last_color == color_num)
    {
        return;
    }

    screen_sdl->last_color = color_num;

    ren = screen_sdl->ren;

    // select the good color
    switch (color_num)
    {
        case WHITE_SDL:
            SDL_SetRenderDrawColor(ren, 255, 255, 255, 0);
            break;

        case BLACK_SDL:
            SDL_SetRenderDrawColor(ren, 0, 0, 0, 1);
            break;

        case LIGHT_GRAY_SDL:
            SDL_SetRenderDrawColor(ren, 220, 212, 212, 0);
            break;

        case BLUE_SDL:
            SDL_SetRenderDrawColor(ren, 0, 0, 255, 0);
            break;

        case RED_SDL:
            SDL_SetRenderDrawColor(ren, 255, 0, 0, 0);
            break;

        case DARK_GREEN_SDL:
            SDL_SetRenderDrawColor(ren, 33, 75, 31, 0);
            break;

        case PURPLE_SDL:
            SDL_SetRenderDrawColor(ren, 131, 15, 246, 0);
            break;

        case TURQUOISE_SDL:
            SDL_SetRenderDrawColor(ren, 15, 246, 238, 0);
            break;

        case ORANGE_SDL:
            SDL_SetRenderDrawColor(ren, 243, 134, 26, 0);
            break;

        case LIGHT_BLUE_SDL:
            SDL_SetRenderDrawColor(ren, 15, 146, 246, 0);
            break;

        case PINK_SDL:
            SDL_SetRenderDrawColor(ren, 246, 15, 215, 0);
            break;              

        case LIGHT_GREEN_SDL:
            SDL_SetRenderDrawColor(ren, 23, 246, 15, 0);
            break;

        case DARK_YELLOW_SDL:
            SDL_SetRenderDrawColor(ren, 175, 165, 27, 0);
            break;

        case YELLOW_SDL:
            SDL_SetRenderDrawColor(ren, 239, 225, 36, 0);
            break;

        case GREEN_SDL:
            SDL_SetRenderDrawColor(ren, 0, 255, 0, 0);
            break;
    
        default:
            break;
    }
}

/*! \brief refresh the screen with the white color
 * 
 * \param[in,out] screen_sdl SDL main data structure
 */
void refresh_screen(Screen_sdl *screen_sdl)
{
    SDL_set_color(screen_sdl, WHITE_SDL);
    SDL_RenderClear(screen_sdl->ren);
}

/*! \brief draw a pixel, coordinates: i, j
 * 
 * \param[in] ren SDL renderer
 * \param[in] i x coordinate
 * \param[in] j y coordinate
 *
 * For some mysterious reasons, using the 'SDL_RenderFillRect' 
 * function with 1x1 rectangles is 2 times faster than using 
 * the 'SDL_RenderDrawPoint' function
 */
void draw_pixel(SDL_Renderer *ren, int i, int j)
{
    SDL_Rect rectangle;
    
    rectangle.x = i;
    rectangle.y = j;
    rectangle.w = 1;
    rectangle.h = 1;
    SDL_RenderFillRect(ren, &rectangle);
}

/*! \brief draw a rectangle (x axis: [x1 ; x2] , y axis: [y1 ; y2])
 * 
 * \pre x1 <= x2
 * \pre y1 <= y2
 * \param[in] ren SDL renderer
 * \param[in] x1 min x coordinate
 * \param[in] x2 max x coordinate
 * \param[in] y1 min y coordinate
 * \param[in] y2 max y coordinate
 */
void draw_rectangle(SDL_Renderer *ren, int x1, int x2, int y1, int y2)
{
    SDL_Rect rect;

    rect.x = x1;
    rect.y = y1;
    rect.w = x2 - x1 + 1;
    rect.h = y2 - y1 + 1;
    SDL_RenderFillRect(ren, &rect);
}

/*! \brief draw a horizontal line (x axis: [x1 ; x2] , y axis: y)
 * 
 * \pre x1 <= x2
 * \param[in] ren SDL renderer
 * \param[in] x1 min x coordinate
 * \param[in] x2 max x coordinate
 * \param[in] y y coordinate
 */
void draw_horizontal_line(SDL_Renderer *ren, int x1, int x2, int y)
{
    SDL_RenderDrawLine(ren, x1, y, x2, y);
}

/*! \brief draw a vertical line (x axis: x , y axis: [y1 ; y2])
 * 
 * \pre y1 <= y2
 * \param[in] ren SDL renderer
 * \param[in] x x coordinate
 * \param[in] y1 min y coordinate
 * \param[in] y2 max y coordinate
 */
void draw_vertical_line(SDL_Renderer *ren, int x, int y1, int y2)
{
    SDL_RenderDrawLine(ren, x, y1, x, y2);
}

/*! \brief print a text
 * 
 * \param[in] str string to print
 * \param[in] font Font to use
 * \param[in] txt_col text color
 * \param[in] ren SDL renderer 
 * \param[in] x x position
 * \param[in] y y position
 * \param[in] pos_flag position flag
 */
void print_text_sdl(char *str, TTF_Font *font, SDL_Color txt_col, SDL_Renderer *ren, int x, int y, int pos_flag)
{
    SDL_Texture *text_texture;
    
    text_texture = renderText(font, str, txt_col, ren);
    renderTexture(text_texture, ren, x, y, pos_flag);
    SDL_DestroyTexture(text_texture);
}

/*! \brief apply a texture at a given corrdinate point
 * 
 * \param[in] tex SDL texture
 * \param[in] ren SDL renderer 
 * \param[in] x x position
 * \param[in] y y position
 * \param[in] pos_flag position flag
 */
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int pos_flag)
{
    //Setup the destination rectangle to be at the position we want
    SDL_Rect dst;
    SDL_Rect clip;

    //Query the texture to get its width and height to use
    SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);  

    clip.x = 0;
    clip.y = 0;

    clip.w = dst.w;
    clip.h = dst.h;

    switch (pos_flag)
    {
        case 1: // position: right-bottom corner
            dst.x = x - dst.w;
            dst.y = y - dst.h;
            break;

        case 2: // position: left-mid height
            dst.x = x;
            dst.y = y - (dst.h / 2);
            break;
    
        default:
            dst.x = x;
            dst.y = y;
            break;
    }

    SDL_RenderCopy(ren, tex,  &clip, &dst);
}

/*! \brief create a texture for a message
 * 
 * \param[in] font requested Font
 * \param[in] message requested message
 * \param[in] text_color text color
 * \param[in] ren SDL renderer
 * \return requested message texture
 */
SDL_Texture* renderText(TTF_Font *font, char *message, SDL_Color text_color, SDL_Renderer *ren)
{
    SDL_Surface *text_surf;
    SDL_Texture *text_texture;

    text_surf = TTF_RenderText_Solid(font, message, text_color);
    if (text_surf == NULL)
    {
        log_SDL_error("TTF_RenderText_Solid");
    }

    text_texture = SDL_CreateTextureFromSurface(ren, text_surf);

    if (text_texture == NULL)
    {
        log_SDL_error("SDL_CreateTextureFromSurface");
    }

    SDL_FreeSurface(text_surf);

    return text_texture;
}

/*! \brief update the vectors and tabs used for the plots
 * 
 * \param[in,out] realtime real-time structure
 * \param[in] t curent simulation time [s]
 *
 * the min-max vectors used for the auto-scaling are also updated
 */
void update_plot_vectors(Simu_realtime *realtime, double t)
{
    Screen_sdl *screen_sdl;

    screen_sdl = realtime->ext->sdl->screen_sdl;

    screen_sdl->index_vec++;

    if (screen_sdl->index_vec >= screen_sdl->screen_plot_width)
    {
        screen_sdl->index_vec -= screen_sdl->screen_plot_width;
    }

    screen_sdl->t_vec[screen_sdl->index_vec] = t;
    
    update_scale_signals(screen_sdl, realtime, 0);
}

834
/*! \brief update 'y_tab' and 't_vec' for plot data during the breaks
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
 * 
 * \param[in,out] screen_sdl SDL main data structure
 */
void update_y_tab(Screen_sdl *screen_sdl)
{
    // -- Variables declaration -- //

    int i, j;
    int nb_curves;
    int prev_index, next_index;
    int buffer_size;
    int min_tsim_index, max_tsim_index;
    int flag_buffer_round;

    double weight_prev;
    double x_min, x_max, diff_x, step_x;
    double request_tsim;
    double tsim_prev, tsim_next;
    double min_tsim, max_tsim;
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
854
    double delta_tsim;
855
856
857
858
859
860
861
862
863
864
865
866
867
868

    double *tsim_save;
    double **y_save;
    double **y_tab;
    double *t_vec;

    AutoPlot *auto_plot;

    // -- Variables initialization -- //

    auto_plot = screen_sdl->auto_plot;

    nb_curves  = auto_plot->nb;

Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
869
870
871
872
873
874
    min_tsim = screen_sdl->min_tsim;
    max_tsim = screen_sdl->max_tsim;

    t_vec = screen_sdl->t_vec;

    // no curves: just fill t_vec with time from origin
875
876
    if (!nb_curves)
    {
Nicolas Van der Noot's avatar
Nicolas Van der Noot committed
877
878
879
880
881
882
883
884
885
886
887
        delta_tsim = (max_tsim - min_tsim) / (screen_sdl->screen_plot_width - 1);

        // safety
        if (delta_tsim <= 0.0) { return; }

        for(i=0; i<screen_sdl->screen_plot_width; i++)
        {
            t_vec[i] = min_tsim + i * delta_tsim;
        }

        screen_sdl->index_vec = screen_sdl->screen_plot_width-1;
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        return;
    }

    tsim_save  = screen_sdl->tsim_save;
    y_save = screen_sdl->y_save;

    min_tsim_index = screen_sdl->min_tsim_index;
    max_tsim_index = screen_sdl->max_tsim_index;

    buffer_size = screen_sdl->buffer_size;

    x_min = screen_sdl->x_min;
    x_max = screen_sdl->x_max;

    y_tab = screen_sdl->y_tab;

    flag_buffer_round = screen_sdl->flag_buffer_round;

    if (!max_tsim)
    {
        return;
    }

    // safety if not yet updated
    if (!screen_sdl->flag_started)
    {
        for(i=0; i<screen_sdl->screen_plot_width; i++)
        {
            t_vec[i] = 0.0;

            for(j=0; j<nb_curves; j++)
            {
                y_tab[j][i] = 0.0;
            }
        }

        // update the index for SDL
        screen_sdl->index_vec = screen_sdl->screen_plot_width - 1;

        return;
    }

    // step in the x axis for each pixel
    diff_x = x_max - x_min;
    step_x = diff_x / screen_sdl->screen_plot_width;

    // tsim value expected
    request_tsim = x_min + (step_x / 2.0);

    // loop on all the pixels
    for(i=0; i<screen_sdl->screen_plot_width; i++)
    {
        guess_tsim_index(request_tsim, tsim_save, min_tsim_index, max_tsim_index, min_tsim, max_tsim, buffer_size, flag_buffer_round, &prev_index, &next_index);

        if (prev_index == next_index)
        {
            // loop on all the curves
            for(j=0; j<nb_curves; j++)
            {
                y_tab[j][i] = y_save[j][prev_index];
            }

            // time vector
            t_vec[i] = tsim_save[prev_index];
        }
        else
        {
            // time
            tsim_prev = tsim_save[prev_index];
            tsim_next = tsim_save[next_index];

            // weight for the linear interpolation
            if (tsim_next > tsim_prev)
            {
                weight_prev = (tsim_next - request_tsim) / (tsim_next - tsim_prev);
            }
            else
            {
                weight_prev = 0.0;
            }

            // loop on all the curves
            for(j=0; j<nb_curves; j++)
            {
                y_tab[j][i] = weight_prev * y_save[j][prev_index] + (1 - weight_prev) * y_save[j][next_index];
            }

            // time vector
            t_vec[i] = weight_prev * tsim_prev + (1 - weight_prev) * tsim_next;
        }

        // new time value
        request_tsim += step_x;
    }

    // update the index for SDL
    screen_sdl->index_vec = screen_sdl->screen_plot_width - 1;
}

/*! \brief update legends for new number of curves
 * 
 * \param[in,out] realtime real-time structure
 * \param[in] new_nb_legends new number of curves
 */
void update_sdl_legend(Simu_realtime *realtime, int new_nb_legends)
{
    int i;
    int nb_legends;
    int space_up_label;

    Screen_sdl *screen_sdl;

    screen_sdl = realtime->ext->sdl->screen_sdl;