-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2686 lines (2182 loc) · 79.9 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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL.h"
//#include <SDL/SDL_ttf.h>
#include <math.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#endif
#ifdef __linux
#include <sys/stat.h>
#endif
int last_got_accel;
float cur_accel_values[3];
float last_accel_values[3];
float accel_per_sec[3];
//debug stuff
int last_time_mouse_down;
float menus_scale=1;
int windows_x_len=1000,windows_y_len=540;
//int windows_x_len=480,windows_y_len=320;
//int windows_x_len=1930,windows_y_len=1000;
//int windows_x_len=100,windows_y_len=100;
int real_windows_x_len,real_windows_y_len;
int pitch;
int *screen_buffer;
Uint8 *blur_buffer;
SDL_Texture* rendering_texture;
SDL_Renderer *renderer;
int x_mouse_pos,y_mouse_pos,mouse_delta_x,mouse_delta_y,button_l=0,button_r=0,have_mouse_focus=0,l_shift,l_ctrl,last_mouse_click;
int old_mouse_x,old_mouse_y;
int last_time_stamp;
int last_delta_check_time;
int last_gesture_time;
int ball_time_begin;
int ball_time_end;
int attractors_no;
int menus_no;
int particles_no=300000;
int gravity_map_changed=1;
int done;
int have_conf;
int last_time_brush;
int have_colors=0;
int use_random_mode=0;
int last_time_random;
//double gravity_constant=0.0673;
float gravity_constant=0.11973;
float border_attenuation=0.3f;
int brush_distance=150;
int brush_force=100000;
int selected_attractor;
Uint8 color_r1=128;
Uint8 color_g1=128;
Uint8 color_b1=128;
Uint8 color_r2=200;
Uint8 color_g2=200;
Uint8 color_b2=200;
//menus
int main_menu_id=0;
int attractors_menu_id=1;
int edit_attractor_menu_id=-1;
int particles_no_slider;
int border_reflect_checkbox;
int liquid_mode_checkbox;
int random_mode_checkbox;
int border_speed_slider;
int brush_distance_slider;
int brush_force_slider;
int gravity_slider;
int attractor_gravity_slider;
int color_r1_slider;
int color_g1_slider;
int color_b1_slider;
int color_r2_slider;
int color_g2_slider;
int color_b2_slider;
int move_attractor_menu_id;
int new_attractor_menu_id;
int exit_menu_id;
int colors_menu_id;
int confirm_remove_menu_id;
int particle_red=40;
int particle_green=50;
int particle_blue=60;
int particle_alpha=60;
float pressure=-1;
char config_path[256];
#define MAX_PARTICLES 1000000
#define MAX_SQRTS 3500000
#define MAX_ATTRACTORS 50
#define MAX_COLORS 20
int colors[MAX_COLORS]={0x01056021,0x02158025,0x03189028,0x041E9C2F,0x0545A533,0x0648AF36,0x074AB23D,0x084CB441,0x094EB944,0x0a51BE47,
0x0b5AC84C,0x0c64CD50,0x0d67D453,0x0e69da5b,0x0f69e162,0x1069e669,0x1169e66c,0x1269eb70,0x1369ef74,0x1369FF80};
float sqrts[MAX_SQRTS];
#define BORDER_REFLECT 0
#define BORDER_WARP 1
int border_behaviour=BORDER_REFLECT;
int draw_tails=0;
float finger_delta_x,finger_delta_y;
int pixels_finger_delta_x,pixels_finger_delta_y;
int blur_divider=6;
typedef struct
{
float x;
float y;
}gravity;
gravity ground_gravity;
gravity* acceleration_map;
typedef struct
{
int x;
int y;
float mass;
}attractor;
attractor attractors[MAX_ATTRACTORS];
typedef struct
{
int time_begin;
int time_end;
int no;
}thread_time;
thread_time thread_times[4];
typedef struct
{
float x;
float y;
float speed_x;
float speed_y;
}particle;
particle particles[MAX_PARTICLES];
#define MAX_MENUS 10
#define MAX_BUTTONS 5
#define MAX_CHECK_BOXES 10
#define MAX_HORIZONTAL_SLIDERS 10
typedef struct
{
int in_use;
int x;
int y;
int x_len;
int y_len;
int cur_value;
int min_value;
int max_value;
}horizontal_slider;
typedef struct
{
int in_use;
int x;
int y;
int x_len;
int y_len;
int selected;
char text[64];
}checkbox;
typedef struct
{
int x;
int y;
int x_len;
int y_len;
char text[32];
void (*on_click)();
}button;
typedef struct
{
int in_use;
int x;
int y;
int x_len;
int y_len;
void (*on_click)();
void (*display_menu)();
char title[128];
int all_screen;
button buttons[MAX_BUTTONS];
checkbox checkboxs[MAX_CHECK_BOXES];
horizontal_slider horizontal_sliders[MAX_HORIZONTAL_SLIDERS];
}menu;
menu menus[MAX_MENUS];
typedef struct
{
int w;
int h;
SDL_Texture* cur_texture;
}font;
int last_loaded_font;
#define MAX_FONTS 10
font fonts[MAX_FONTS];
static int draw_particles_thread(void* ptr);
static int particle_repulse_by_mouse_thread(void *ptr);
static int build_acceleration_map_thread(void *ptr);
void display_main_menu(int menu_id);
void on_click_main_menu(int menu_id);
void click_ok_main_menu();
void click_cancel_main_menu();
void print_string(char *str, int font, int char_xscreen, int char_yscreen);
void init_particles();
void click_reset_button();
void click_attractors_button();
void display_attractors_menu(int menu_id);
void on_click_attractors_menu(int menu_id);
void click_attractors_done();
void click_attractors_new_attractor();
void on_click_edit_attractor_menu(int menu_id);
void display_edit_attractor_menu(int menu_id);
void click_edit_move();
void click_edit_done();
void click_edit_remove();
void build_acceleration_map();
int add_horizontal_slider_to_menu(int menu_id, int scale, int cur_value, int min_value, int max_value, int x, int y, int x_len, int y_len);
int add_menu(void *on_click, void *display_menu, int x, int y, int x_len, int y_len, int in_use, int all_screen, char *title);
int add_button_to_menu(int menu_id, int scale, void *click_func, int x, int y, int x_len, int y_len, char *text);
void on_click_move_attractors();
void display_move_attractors();
void on_click_new_attractors();
void display_new_attractors();
void on_click_exit_menu(int menu_id);
void display_exit_menu(int menu_id);
void click_yes_exit_menu();
void click_no_exit_menu();
static int build_sqrts_thread(void *ptr);
void click_colors_main_menu();
void display_colors_menu(int menu_id);
void on_click_colors_menu(int menu_id);
void click_colors_ok();
void click_attractors_remove_all();
void display_remove_menu(int menu_id);
void on_click_remove_menu(int menu_id);
void click_yes_remove_menu();
void click_no_remove_menu();
void build_colors(int r1, int g1, int b1, int r2, int g2, int b2);
static int all_particles_attract_thread(void *ptr);
void get_configh_path()
{
#ifdef _WIN32
SHGetFolderPath(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, config_path);
#elif __ANDROID__
strcpy(config_path,SDL_AndroidGetInternalStoragePath());
strcat(config_path,"/");
#else
strcpy(config_path,getenv("HOME"));
#endif
#ifndef __ANDROID__
strcat(config_path,"/.million_particles/");
#ifndef _WIN32
mkdir(config_path, (S_IRWXU|S_IRWXG|S_IRWXO));
#else
mkdir(config_path);
#endif
#endif //android
}
void save_config()
{
int i;
FILE * f;
void *conf;
char fn[256];
sprintf(fn,"%sparticles.cfg",config_path);
f = fopen(fn, "wb");
if(!f)return;
conf=malloc(1000);
memset(conf,0,250);
*((Uint32 *)(conf+0))=particles_no;
*((float *)(conf+4))=gravity_constant;
*((float *)(conf+8))=border_attenuation;
*((Uint32 *)(conf+12))=brush_distance;
*((float *)(conf+16))=brush_force;
*((Uint32 *)(conf+20))=border_behaviour;
*((Uint32 *)(conf+24))=draw_tails;
*((Uint32 *)(conf+30))=attractors_no;
*((Uint32 *)(conf+34))=have_colors;
if(have_colors)
{
*((Uint8 *)(conf+38))=menus[colors_menu_id].horizontal_sliders[color_r1_slider].cur_value;
*((Uint8 *)(conf+39))=menus[colors_menu_id].horizontal_sliders[color_g1_slider].cur_value;
*((Uint8 *)(conf+40))=menus[colors_menu_id].horizontal_sliders[color_b1_slider].cur_value;
*((Uint8 *)(conf+41))=menus[colors_menu_id].horizontal_sliders[color_r2_slider].cur_value;
*((Uint8 *)(conf+42))=menus[colors_menu_id].horizontal_sliders[color_g2_slider].cur_value;
*((Uint8 *)(conf+43))=menus[colors_menu_id].horizontal_sliders[color_b2_slider].cur_value;
}
//reserved up to 99
//write the header and reserved area
fwrite(conf,100,4,f);
for(i=0;i<attractors_no;i++)
{
*((float *)(conf+400+i*12+0))=attractors[i].mass;
*((Uint32 *)(conf+400+i*12+4))=attractors[i].x;
*((Uint32 *)(conf+400+i*12+8))=attractors[i].y;
}
//write the objects
fwrite((conf+400),attractors_no,sizeof(attractor),f);
fclose(f);
free(conf);
}
int load_config()
{
int i;
FILE * f;
void *conf;
char fn[256];
sprintf(fn,"%sparticles.cfg",config_path);
f = fopen(fn, "rb");
if(!f)return 0;
conf=malloc(1000);
fread(conf,100,4,f);
particles_no=*((Uint32 *)(conf+0));
gravity_constant=*((float *)(conf+4));
border_attenuation=*((float *)(conf+8));
brush_distance=*((Uint32 *)(conf+12));
brush_force=*((float *)(conf+16));
border_behaviour=*((Uint32 *)(conf+20));
draw_tails=*((Uint32 *)(conf+24));
attractors_no=*((Uint32 *)(conf+30));
have_colors=*((Uint32 *)(conf+34));
if(have_colors)
{
color_r1=*((Uint8 *)(conf+38));
color_g1=*((Uint8 *)(conf+39));
color_b1=*((Uint8 *)(conf+40));
color_r2=*((Uint8 *)(conf+41));
color_g2=*((Uint8 *)(conf+42));
color_b2=*((Uint8 *)(conf+43));
build_colors(color_r1,color_g1,color_b1,color_r2,color_g2,color_b2);
}
//reserved up to 99
fread((conf+400),attractors_no,sizeof(attractor),f);
for(i=0;i<attractors_no;i++)
{
attractors[i].mass=*((float *)(conf+400+i*12+0));
attractors[i].x=*((Uint32 *)(conf+400+i*12+4));
attractors[i].y=*((Uint32 *)(conf+400+i*12+8));
}
//write the objects
fclose(f);
free(conf);
return 1;
}
void set_attractor_color(int attractor_id)
{
if(attractors[attractor_id].mass<0)
SDL_SetRenderDrawColor(renderer,30,50,255,255);
else
if(attractors[attractor_id].mass>0)
SDL_SetRenderDrawColor(renderer,255,50,30,255);
else
SDL_SetRenderDrawColor(renderer,255,255,255,255);
}
void check_buttons(int menu_id)
{
int i;
int x_offset;
int y_offset;
if(SDL_GetTicks()-last_time_mouse_down>3)return;
last_time_mouse_down-=10;//to avoid double clicks
x_offset=menus[menu_id].x;
y_offset=menus[menu_id].y;
for(i=0;i<MAX_BUTTONS;i++)
if(menus[menu_id].buttons[i].on_click)
if(x_mouse_pos>=menus[menu_id].buttons[i].x+x_offset && x_mouse_pos<=menus[menu_id].buttons[i].x+menus[menu_id].buttons[i].x_len+x_offset && y_mouse_pos>=menus[menu_id].buttons[i].y+y_offset
&& y_mouse_pos<=menus[menu_id].buttons[i].y+menus[menu_id].buttons[i].y_len+y_offset)
menus[menu_id].buttons[i].on_click();
}
int check_click_on_menu()
{
int i;
for(i=0;i<menus_no;i++)
if(menus[i].in_use)
{
if((x_mouse_pos>=menus[i].x && x_mouse_pos<=menus[i].x+menus[i].x_len && y_mouse_pos>=menus[i].y
&& y_mouse_pos<=menus[i].y+menus[i].y_len) || menus[i].all_screen)
{
menus[i].on_click(i);
last_time_brush=SDL_GetTicks()+150;//make it not draw the brush right away if we click on the OK, Cancel, ETC. buttons
return 1;
}
}
return 0;
}
void click_ok_main_menu()
{
float diff;
if(menus[main_menu_id].checkboxs[border_reflect_checkbox].selected)
border_behaviour=BORDER_REFLECT;
else
border_behaviour=BORDER_WARP;
diff=gravity_constant-menus[main_menu_id].horizontal_sliders[gravity_slider].cur_value/1000.0f;
draw_tails=menus[main_menu_id].checkboxs[liquid_mode_checkbox].selected;
use_random_mode=menus[main_menu_id].checkboxs[random_mode_checkbox].selected;
particles_no=menus[main_menu_id].horizontal_sliders[particles_no_slider].cur_value;
border_attenuation=menus[main_menu_id].horizontal_sliders[border_speed_slider].cur_value/100.0f;
brush_distance=menus[main_menu_id].horizontal_sliders[brush_distance_slider].cur_value;
brush_force=menus[main_menu_id].horizontal_sliders[brush_force_slider].cur_value;
gravity_constant=menus[main_menu_id].horizontal_sliders[gravity_slider].cur_value/1000.0f;
if(diff>0.001f || diff<-0.001f || gravity_map_changed)
build_acceleration_map();
SDL_Log("gravity_constant:%f gravity_constant_sld:%f diff:%f",gravity_constant,menus[main_menu_id].horizontal_sliders[gravity_slider].cur_value/1000.0f,diff);
menus[main_menu_id].in_use=0;
}
void click_colors_main_menu()
{
menus[main_menu_id].in_use=0;
menus[colors_menu_id].in_use=1;
}
void click_yes_exit_menu()
{
done=1;
}
void click_no_exit_menu()
{
menus[exit_menu_id].in_use=0;
menus[main_menu_id].in_use=1;
}
void click_yes_remove_menu()
{
menus[confirm_remove_menu_id].in_use=0;
menus[attractors_menu_id].in_use=1;
attractors_no=0;
build_acceleration_map();
}
void click_no_remove_menu()
{
menus[confirm_remove_menu_id].in_use=0;
menus[attractors_menu_id].in_use=1;
}
void click_cancel_main_menu()
{
menus[main_menu_id].in_use=0;
}
void click_attractors_button()
{
menus[main_menu_id].in_use=0;
menus[attractors_menu_id].in_use=1;
gravity_map_changed=0;
}
void click_reset_button()
{
init_particles();
//menus[main_menu_id].in_use=0;
}
void click_attractors_done()
{
menus[attractors_menu_id].in_use=0;
menus[main_menu_id].in_use=1;
}
void click_attractors_remove_all()
{
menus[attractors_menu_id].in_use=0;
menus[confirm_remove_menu_id].in_use=1;
}
void click_attractors_new_attractor()
{
menus[attractors_menu_id].in_use=0;
menus[new_attractor_menu_id].in_use=1;
}
void click_edit_move()
{
menus[edit_attractor_menu_id].in_use=0;
menus[move_attractor_menu_id].in_use=1;
}
void build_colors(int r1, int g1, int b1, int r2, int g2, int b2)
{
int i;
float red_ratio,green_ratio,blue_ratio,cur_red,cur_green,cur_blue;
Uint8 *rgb_ptr = (Uint8 *)colors;
Uint8 red_color[MAX_COLORS];
Uint8 green_color[MAX_COLORS];
Uint8 blue_color[MAX_COLORS];
//int colors[MAX_COLORS]={0x01056021,0x02158025,0x03189028,0x041E9C2F,0x0545A533,0x0648AF36,0x074AB23D,0x084CB441,0x094EB944,0x0a51BE47,
//0x0b5AC84C,0x0c64CD50,0x0d67D453,0x0e69da5b,0x0f69e162,0x1069e669,0x1169e66c,0x1269eb70,0x1369ef74,0x1369FF80};
red_color[0]=r1;
red_color[1]=r1+((r2-r1)/(float)(MAX_COLORS))*MAX_COLORS/6;
r1=red_color[1];
red_color[2]=r1+((r2-r1)/(float)(MAX_COLORS))*MAX_COLORS/8;
r1=red_color[2];
red_color[3]=r1+((r2-r1)/(float)(MAX_COLORS))*MAX_COLORS/8;
r1=red_color[3];
cur_red=r1;
red_ratio=(r2-r1)/(float)(MAX_COLORS-2);
green_color[0]=g1;
green_color[1]=g1+((g2-g1)/(float)(MAX_COLORS))*MAX_COLORS/8;
g1=green_color[1];
green_color[2]=g1+((g2-g1)/(float)(MAX_COLORS))*MAX_COLORS/8;
g1=green_color[2];
green_color[3]=g1+((g2-g1)/(float)(MAX_COLORS))*MAX_COLORS/8;
g1=green_color[3];
cur_green=g1;
green_ratio=(g2-g1)/(float)(MAX_COLORS-2);
blue_color[0]=b1;
blue_color[1]=b1+((b2-b1)/(float)(MAX_COLORS))*MAX_COLORS/8;
b1=blue_color[1];
blue_color[2]=b1+((b2-b1)/(float)(MAX_COLORS))*MAX_COLORS/8;
b1=blue_color[2];
blue_color[3]=b1+((b2-b1)/(float)(MAX_COLORS))*MAX_COLORS/8;
b1=blue_color[3];
cur_blue=b1;
blue_ratio=(b2-b1)/(float)(MAX_COLORS-2);
for(i=4;i<MAX_COLORS;i++)
{
cur_red+=red_ratio;
red_color[i]=cur_red;
cur_green+=green_ratio;
green_color[i]=cur_green;
cur_blue+=blue_ratio;
blue_color[i]=cur_blue;
}
for(i=0;i<MAX_COLORS;i++)
{
rgb_ptr[i*4+2]=red_color[i];
rgb_ptr[i*4+1]=green_color[i];
rgb_ptr[i*4+0]=blue_color[i];
rgb_ptr[i*4+3]=i+1;
SDL_Log("cur_red:%i",red_color[i]);
SDL_Log("cur_green:%i",green_color[i]);
SDL_Log("cur_blue:%i",blue_color[i]);
}
rgb_ptr[(MAX_COLORS-1)*4+3]=MAX_COLORS-1;//cred
}
void click_colors_ok()
{
menus[colors_menu_id].in_use=0;
menus[main_menu_id].in_use=1;
build_colors(menus[colors_menu_id].horizontal_sliders[color_r1_slider].cur_value,
menus[colors_menu_id].horizontal_sliders[color_g1_slider].cur_value,
menus[colors_menu_id].horizontal_sliders[color_b1_slider].cur_value,
menus[colors_menu_id].horizontal_sliders[color_r2_slider].cur_value,
menus[colors_menu_id].horizontal_sliders[color_g2_slider].cur_value,
menus[colors_menu_id].horizontal_sliders[color_b2_slider].cur_value);
have_colors=1;
}
void click_edit_done()
{
menus[edit_attractor_menu_id].in_use=0;
menus[attractors_menu_id].in_use=1;
gravity_map_changed=1;
attractors[selected_attractor].mass=menus[edit_attractor_menu_id].horizontal_sliders[attractor_gravity_slider].cur_value;
}
void click_edit_remove()
{
attractors[selected_attractor].mass=attractors[attractors_no-1].mass;
attractors[selected_attractor].x=attractors[attractors_no-1].x;
attractors[selected_attractor].y=attractors[attractors_no-1].y;
attractors_no--;
menus[edit_attractor_menu_id].in_use=0;
menus[attractors_menu_id].in_use=1;
gravity_map_changed=1;
}
void display_horizontal_slider(int menu_id, int slider_id, int font_size, char * str)
{
SDL_Rect rect;
int numbers_len;
int min_no;
int max_no;
int cur_no;
float ratio;
if(menus_scale<=0.9f)
font_size=0;
rect.x=menus[menu_id].horizontal_sliders[slider_id].x+menus[menu_id].x;
rect.y=menus[menu_id].horizontal_sliders[slider_id].y+menus[menu_id].y+(menus[menu_id].horizontal_sliders[slider_id].y_len-5)/2;
rect.w=menus[menu_id].horizontal_sliders[slider_id].x_len;
rect.h=5;
SDL_SetRenderDrawColor(renderer,160,160,160,255);
SDL_RenderFillRect(renderer, &rect);
print_string(str,font_size,rect.x+rect.w+5,rect.y+(rect.h-fonts[font_size].h)/2);
//draw the cursor
min_no=menus[menu_id].horizontal_sliders[slider_id].min_value;
max_no=menus[menu_id].horizontal_sliders[slider_id].max_value;
cur_no=menus[menu_id].horizontal_sliders[slider_id].cur_value;
numbers_len=max_no-min_no;
ratio=(float)numbers_len/(float)menus[menu_id].horizontal_sliders[slider_id].x_len;
rect.x=(cur_no-min_no)/ratio+menus[menu_id].horizontal_sliders[slider_id].x+menus[menu_id].x-5;
rect.y=menus[menu_id].horizontal_sliders[slider_id].y+menus[menu_id].y;
rect.w=10;
rect.h=menus[menu_id].horizontal_sliders[slider_id].y_len;
SDL_SetRenderDrawColor(renderer,20,190,255,255);
SDL_RenderFillRect(renderer, &rect);
}
void display_button(int menu_id,int button_id,int font_size)
{
SDL_Rect rect;
int text_x_len;
if(menus_scale<=0.9)
font_size=0;
rect.x=menus[menu_id].buttons[button_id].x+menus[menu_id].x;
rect.y=menus[menu_id].buttons[button_id].y+menus[menu_id].y;
rect.w=menus[menu_id].buttons[button_id].x_len;
rect.h=menus[menu_id].buttons[button_id].y_len;
SDL_RenderDrawRect(renderer, &rect);
text_x_len=strlen(menus[menu_id].buttons[button_id].text)*fonts[font_size].w/96;
print_string(menus[menu_id].buttons[button_id].text,font_size,rect.x+(rect.w-text_x_len)/2,rect.y+(rect.h-fonts[font_size].h)/2);
}
void display_checkboxs(int menu_id,int checkboxs_id,int font_size)
{
SDL_Rect rect;
if(menus_scale<=0.9f)
font_size=0;
rect.x=menus[menu_id].checkboxs[checkboxs_id].x+menus[menu_id].x;
rect.y=menus[menu_id].checkboxs[checkboxs_id].y+menus[menu_id].y;
rect.w=menus[menu_id].checkboxs[checkboxs_id].x_len;
rect.h=menus[menu_id].checkboxs[checkboxs_id].y_len;
SDL_RenderDrawRect(renderer, &rect);
print_string(menus[menu_id].checkboxs[checkboxs_id].text,font_size,rect.x+rect.w+5,rect.y+(rect.h-fonts[font_size].h)/2);
if(menus[menu_id].checkboxs[checkboxs_id].selected)
{
rect.x+=3;
rect.y+=3;
rect.w-=6;
rect.h-=6;
SDL_RenderFillRect(renderer, &rect);
}
}
void display_main_menu(int menu_id)
{
SDL_Rect rect;
// int font_size=0;
// int text_x_len;
char str[128];
int i;
rect.x=menus[menu_id].x;
rect.y=menus[menu_id].y;
rect.w=menus[menu_id].x_len;
rect.h=menus[menu_id].y_len;
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderDrawRect(renderer, &rect);
//text_x_len=strlen(menus[menu_id].title)*fonts[font_size].w/96;
//print_string(menus[menu_id].title,font_size,rect.x+(rect.w-text_x_len)/2,rect.y);
//display buttons
for(i=0;i<MAX_BUTTONS;i++)
if(menus[menu_id].buttons[i].on_click)
display_button(menu_id,i,1);
//display checkboxes
for(i=0;i<MAX_CHECK_BOXES;i++)
if(menus[menu_id].checkboxs[i].in_use)
display_checkboxs(menu_id,i,1);
//display the sliders
sprintf(str,"%i particles",menus[menu_id].horizontal_sliders[particles_no_slider].cur_value);
display_horizontal_slider(0,particles_no_slider,1,str);
sprintf(str,"%.2f border speed",(float)menus[menu_id].horizontal_sliders[border_speed_slider].cur_value/100.0f);
display_horizontal_slider(0,border_speed_slider,1,str);
sprintf(str,"%i size",menus[menu_id].horizontal_sliders[brush_distance_slider].cur_value);
display_horizontal_slider(0,brush_distance_slider,1,str);
sprintf(str,"%i force",menus[menu_id].horizontal_sliders[brush_force_slider].cur_value);
display_horizontal_slider(0,brush_force_slider,1,str);
sprintf(str,"%i speed",menus[menu_id].horizontal_sliders[gravity_slider].cur_value);
display_horizontal_slider(0,gravity_slider,1,str);
}
void display_colors_menu(int menu_id)
{
SDL_Rect rect;
int font_size=1;
// int text_x_len;
char str[128];
int i;
if(menus_scale<0.9f)font_size=0;
rect.x=menus[menu_id].x;
rect.y=menus[menu_id].y;
rect.w=menus[menu_id].x_len;
rect.h=menus[menu_id].y_len;
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderDrawRect(renderer, &rect);
for(i=0;i<MAX_BUTTONS;i++)
if(menus[menu_id].buttons[i].on_click)
display_button(menu_id,i,1);
str[0]=0;
//sprintf(str,"%i R1",menus[menu_id].horizontal_sliders[color_r1_slider].cur_value);
display_horizontal_slider(menu_id,color_r1_slider,1,str);
rect.x=menus[menu_id].horizontal_sliders[color_r1_slider].x+menus[menu_id].horizontal_sliders[color_r1_slider].x_len+menus[menu_id].x+20;
rect.y=menus[menu_id].horizontal_sliders[color_r1_slider].y+menus[menu_id].y;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,menus[menu_id].horizontal_sliders[color_r1_slider].cur_value,0,0,255);
SDL_RenderFillRect(renderer, &rect);
//sprintf(str,"%i G1",menus[menu_id].horizontal_sliders[color_g1_slider].cur_value);
display_horizontal_slider(menu_id,color_g1_slider,1,str);
rect.x=menus[menu_id].horizontal_sliders[color_g1_slider].x+menus[menu_id].horizontal_sliders[color_g1_slider].x_len+menus[menu_id].x+20;
rect.y=menus[menu_id].horizontal_sliders[color_g1_slider].y+menus[menu_id].y;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,0,menus[menu_id].horizontal_sliders[color_g1_slider].cur_value,0,255);
SDL_RenderFillRect(renderer, &rect);
//sprintf(str,"%i B1",menus[menu_id].horizontal_sliders[color_b1_slider].cur_value);
display_horizontal_slider(menu_id,color_b1_slider,1,str);
rect.x=menus[menu_id].horizontal_sliders[color_b1_slider].x+menus[menu_id].horizontal_sliders[color_b1_slider].x_len+menus[menu_id].x+20;
rect.y=menus[menu_id].horizontal_sliders[color_b1_slider].y+menus[menu_id].y;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,0,0,menus[menu_id].horizontal_sliders[color_b1_slider].cur_value,255);
SDL_RenderFillRect(renderer, &rect);
//sprintf(str,"%i R2",menus[menu_id].horizontal_sliders[color_r2_slider].cur_value);
display_horizontal_slider(menu_id,color_r2_slider,1,str);
rect.x=menus[menu_id].horizontal_sliders[color_r2_slider].x+menus[menu_id].horizontal_sliders[color_r2_slider].x_len+menus[menu_id].x+20;
rect.y=menus[menu_id].horizontal_sliders[color_r2_slider].y+menus[menu_id].y;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,menus[menu_id].horizontal_sliders[color_r2_slider].cur_value,0,0,255);
SDL_RenderFillRect(renderer, &rect);
//sprintf(str,"%i G2",menus[menu_id].horizontal_sliders[color_g2_slider].cur_value);
display_horizontal_slider(menu_id,color_g2_slider,1,str);
rect.x=menus[menu_id].horizontal_sliders[color_g2_slider].x+menus[menu_id].horizontal_sliders[color_g2_slider].x_len+menus[menu_id].x+20;
rect.y=menus[menu_id].horizontal_sliders[color_g2_slider].y+menus[menu_id].y;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,0,menus[menu_id].horizontal_sliders[color_g2_slider].cur_value,0,255);
SDL_RenderFillRect(renderer, &rect);
//sprintf(str,"%i B2",menus[menu_id].horizontal_sliders[color_b2_slider].cur_value);
display_horizontal_slider(menu_id,color_b2_slider,1,str);
rect.x=menus[menu_id].horizontal_sliders[color_b2_slider].x+menus[menu_id].horizontal_sliders[color_b2_slider].x_len+menus[menu_id].x+20;
rect.y=menus[menu_id].horizontal_sliders[color_b2_slider].y+menus[menu_id].y;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,0,0,menus[menu_id].horizontal_sliders[color_b2_slider].cur_value,255);
SDL_RenderFillRect(renderer, &rect);
rect.x=menus[menu_id].x+15;
rect.y=menus[menu_id].horizontal_sliders[color_b1_slider].y+menus[menu_id].y+60*menus_scale;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,menus[menu_id].horizontal_sliders[color_r1_slider].cur_value,menus[menu_id].horizontal_sliders[color_g1_slider].cur_value,menus[menu_id].horizontal_sliders[color_b1_slider].cur_value,255);
SDL_RenderFillRect(renderer, &rect);
print_string("First color",font_size,rect.x+rect.w+5,rect.y+(rect.h-fonts[font_size].h)/2);
rect.x=menus[menu_id].horizontal_sliders[color_b2_slider].x+menus[menu_id].x;
rect.y=menus[menu_id].horizontal_sliders[color_b2_slider].y+menus[menu_id].y+60*menus_scale;
rect.w=50*menus_scale;
rect.h=40*menus_scale;
SDL_SetRenderDrawColor(renderer,menus[menu_id].horizontal_sliders[color_r2_slider].cur_value,menus[menu_id].horizontal_sliders[color_g2_slider].cur_value,menus[menu_id].horizontal_sliders[color_b2_slider].cur_value,255);
SDL_RenderFillRect(renderer, &rect);
print_string("Last color",font_size,rect.x+rect.w+5,rect.y+(rect.h-fonts[font_size].h)/2);
}
void display_attractors_menu(int menu_id)
{
SDL_Rect rect;
int i;
for(i=0;i<attractors_no;i++)
{
set_attractor_color(i);
rect.x=((float)real_windows_x_len/(float)windows_x_len)*attractors[i].x-20;
rect.y=((float)real_windows_y_len/(float)windows_y_len)*attractors[i].y-20;
rect.w=40;
rect.h=40;
SDL_RenderDrawRect(renderer, &rect);
}
SDL_SetRenderDrawColor(renderer,0,0,0,255);
for(i=0;i<MAX_BUTTONS;i++)
if(menus[menu_id].buttons[i].on_click)
display_button(menu_id,i,1);
}
void check_checkboxes(int menu_id)
{
int i;
int x,y,x_len,y_len;
if(SDL_GetTicks()-last_time_mouse_down>10)return;
for(i=0;i<MAX_CHECK_BOXES;i++)
if(menus[menu_id].checkboxs[i].in_use)
{
x=menus[menu_id].checkboxs[i].x+menus[menu_id].x;
x_len=menus[menu_id].checkboxs[i].x_len;
y=menus[menu_id].checkboxs[i].y+menus[menu_id].y;
y_len=menus[menu_id].checkboxs[i].y_len;
if(x_mouse_pos>=x && x_mouse_pos<=x+x_len && y_mouse_pos>=y && y_mouse_pos<=y_len+y)
menus[menu_id].checkboxs[i].selected=!menus[menu_id].checkboxs[i].selected;
}
}
void check_horizontal_sliders(int menu_id)
{
int i;
int x,y,x_len,y_len;
int numbers_len;
int min_no;
int max_no;
int mouse_cursor_len;
float ratio;
for(i=0;i<MAX_CHECK_BOXES;i++)
if(menus[menu_id].horizontal_sliders[i].in_use)
{
x=menus[menu_id].horizontal_sliders[i].x+menus[menu_id].x;
x_len=menus[menu_id].horizontal_sliders[i].x_len;
y=menus[menu_id].horizontal_sliders[i].y+menus[menu_id].y;
y_len=menus[menu_id].horizontal_sliders[i].y_len;
if(x_mouse_pos>=x-10 && x_mouse_pos<=x+x_len+10 && y_mouse_pos>=y && y_mouse_pos<=y_len+y)
{
min_no=menus[menu_id].horizontal_sliders[i].min_value;
max_no=menus[menu_id].horizontal_sliders[i].max_value;
numbers_len=max_no-min_no;
ratio=(float)numbers_len/(float)x_len;
mouse_cursor_len=x_mouse_pos-x;
if(x_mouse_pos>x_len+x)
menus[menu_id].horizontal_sliders[i].cur_value=max_no;
else
if(x_mouse_pos<x)
menus[menu_id].horizontal_sliders[i].cur_value=min_no;
else
menus[menu_id].horizontal_sliders[i].cur_value=mouse_cursor_len*ratio+min_no;
}
}
}
void on_click_edit_attractor_menu(int menu_id)
{
check_buttons(menu_id);
check_horizontal_sliders(menu_id);
}
void on_click_main_menu(int menu_id)
{
check_buttons(menu_id);
check_checkboxes(menu_id);
check_horizontal_sliders(menu_id);
}
void on_click_colors_menu(int menu_id)
{
check_horizontal_sliders(menu_id);
check_buttons(menu_id);
}
void on_click_remove_menu(int menu_id)
{
check_buttons(menu_id);
}
void on_click_exit_menu(int menu_id)
{
check_buttons(menu_id);
}
void on_click_attractors_menu(int menu_id)
{
int i;
int x,y,x_len,y_len;