-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.c
1069 lines (946 loc) · 25.4 KB
/
controls.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 "controls.h"
#define DEBUG 1
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/*creates a rectangle, according to control size, position and panel boundaries*/
void get_rect(SDL_Rect *rect_dest, control *cntrl, control *parent_panel)
{
//+ becuase cntrl offset is relative to his parent_panel
rect_dest->x = cntrl->x + parent_panel->offsetx;
rect_dest->y = cntrl->y + parent_panel->offsety;
//min in order to insure that the control won't be able to pass parent_panel borders
if (parent_panel->offsetx + parent_panel->w < rect_dest->x + cntrl->w)
rect_dest->w = parent_panel->w + parent_panel->offsetx - cntrl->x;
else
rect_dest->w = cntrl->w;
if (parent_panel->offsety + parent_panel->h < rect_dest->y + cntrl->h)
rect_dest->h = parent_panel->h + parent_panel->offsety - cntrl->y;
else
rect_dest->h = cntrl->h;
//rect_dest->w = MIN(cntrl->w, parent_panel->w - cntrl->x);
//rect_dest->h = MIN(cntrl->h, parent_panel->h - cntrl->y);
}
/* loads image/rect as surface
* saves control surface
* returns -1 on failure*/
int handle_control_surface_load(control *cntrl, control *container)
{
SDL_Surface *surface=NULL;
SDL_Rect *rect = (SDL_Rect*)malloc(sizeof(SDL_Rect));
char *sdl_err = NULL;
if (cntrl->ownSurface == NULL) //if there no surface assoicated with control yet.if it has- exit func
{
if (cntrl->is_bg_img) // control has background image
{
surface = SDL_LoadBMP(cntrl->img);//if control has background image(in case on button or panel)
if (surface == NULL)
{//if SDL_LoadBMP failed
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n",sdl_err);
SDL_FreeSurface(surface);
return -1;
}
}
else if (cntrl->is_bg_rect) // control has background color
{
surface = SDL_CreateRGBSurface(SDL_HWSURFACE,
cntrl->w, cntrl->h, 32, 0, 0, 0, 0);
if (surface == NULL)
{//if SDL_CreateRGBSurface failed
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n",sdl_err);
SDL_FreeSurface(surface);
return -1;
}
}
//save offsets and destination rectangles
if (cntrl->is_bg_rect || cntrl->is_bg_img)
{
cntrl->srfc = container->srfc;
cntrl->ownSurface=surface;
//find rect according to parent
get_rect(rect,cntrl,container);
//find picture proportions
cntrl->h = surface->h;
cntrl->w = surface->w;
//update to final position(position in relation to the window) on board
cntrl->offsetx = rect->x;
cntrl->offsety = rect->y;
cntrl->destination_rect = rect;
//set transparancy
if (cntrl->is_transparant){
if (SDL_SetColorKey(surface, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(surface->format, MAGNETAR, MAGNETAG, MAGNETAB))==-1)//evrey transparant parts of the image are MAGNETA
{//enter in case SDL_SetColorKey failed. if SDL_MapRGB will fail SDL_SetColorKey will also fail.
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n",sdl_err);
return -1;
}
}
}
else //if the control has no surface(and shouldn't have)
{
// assign all critical info - no real surface
cntrl->srfc = container->srfc;
cntrl->ownSurface=(SDL_Surface*)NULL;
get_rect(rect,cntrl,container);
cntrl->offsetx = rect->x;
cntrl->offsety = rect->y;
cntrl->destination_rect = rect;
}
}
return 1;
}
/*gets a rectnagle for the text, in the middle of parent rect
*on success return 1, on failure -1
*/
int get_text_position(SDL_Rect *rect_source, char* text,TTF_Font *font, SDL_Rect *rect_dest)
{
char *ttf_err = NULL;
int w,h;
if (TTF_SizeText(font,text,&w,&h)) {//find text size in pixels
ttf_err = TTF_GetError();
if (ttf_err != NULL)
printf("ERROR: %s\n",ttf_err);
return -1;
}
if (w > rect_source->w || h > rect_source->h)
{
printf("ERORR: text rectangle overflows from parent panel\n");
return -1;
}
rect_dest->w = w;
rect_dest->h = h;
//set in the middle of the control
rect_dest->x = rect_source->x + (rect_source->w - w)/2;
rect_dest->y = rect_source->y + (rect_source->h - h)/2;
return 1;
}
/* creates a destination rect for text,
* with lines aligned to upper left
* with offset according to previous line.
*1 on success,-1 on failure
differs from get_text_position in the fact that it doesn't place the text in the middle,but allow offset*/
int get_text_position_multi(SDL_Rect *rect_source, char* text,TTF_Font *font, SDL_Rect *rect_dest,int offset)
{
char *ttf_err;
int w,h;
if (TTF_SizeText(font,text,&w,&h)) {
ttf_err = TTF_GetError();
if (ttf_err != NULL)
printf("ERROR: %s\n",ttf_err);
return -1;
}
rect_dest->w = w;
rect_dest->h = h;
rect_dest->x = rect_source->x + (rect_source->w - w)/2;
rect_dest->y = rect_source->y+offset;
/* check for overflow */
if (rect_dest->w > rect_source->w ||
rect_dest->h > rect_source->w)
{
printf("ERORR: text rectangle overflows from parent panel\n");
return -1;
}
return 1;
}
/*assigns values to SDL_Rect struct*/
void make_rect(SDL_Rect *rect, int h, int w, int x, int y)
{
rect->h = h;
rect->w = w;
rect->x = x;
rect->y = y;
return;
}
/*draws a single line of caption to center of control*/
int draw_caption_to_control(control *cntrl)
{
char* sdl_err = NULL;
char* ttf_err = NULL;
TTF_Font *font;
SDL_Surface *surface=NULL;
SDL_Color text_color = {255, 255, 255};
SDL_Rect text_rect = {0,0,0,0};
SDL_Rect soure_rect = {0,0,0,0};
// no caption is drawn with success
if (cntrl->caption == NULL || strcmp(cntrl->caption,"")== 0)
return 1;
//make all text Calculations
make_rect(&soure_rect, cntrl->h,cntrl->w,cntrl->offsetx,cntrl->offsety);
font = TTF_OpenFont("arial.ttf", FONT_SIZE);
if (font == NULL) // unable to load font
{
ttf_err = TTF_GetError();
if (ttf_err != NULL)
printf("ERROR: %s",ttf_err);
return -1;
}
//try to make a destination rectangle for caption
if (get_text_position(&soure_rect, cntrl->caption, font, &text_rect) == -1)
return -1;
//if text surface wan't previously made-make it
if (cntrl->text_surface == NULL)
{
surface = TTF_RenderText_Blended(font, cntrl->caption, text_color);
if (surface==NULL)
{
ttf_err = TTF_GetError();
if (ttf_err != NULL)
printf("ERORR: %s\n", ttf_err);
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return -1;
}
cntrl->text_surface = surface;
}
// Blit text's surface
if (SDL_BlitSurface(cntrl->text_surface, NULL, cntrl->srfc, &text_rect) != 0)
{
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n", sdl_err);
TTF_CloseFont(font);
return -1;
}
TTF_CloseFont(font);
return 1;
}
/* regards line-breaks in controls caption
*of up-to MAX_LINES lines, adjusted to top left */
int draw_caption_to_control_multi(control *cntrl)
{
TTF_Font *font=NULL;
SDL_Surface *surface=NULL;
SDL_Color text_color = {255, 255, 255};
SDL_Rect text_rect = {0,0,0,0};
SDL_Rect soure_rect = {0,0,0,0};
char* sdl_err = NULL;
char* ttf_err = NULL;
char *lines[MAX_CAPTION_LINES];
char* token;
int i=0;
int linecount=0;
char *s=NULL;
int prev_x_offset = 0;
if (cntrl->caption == NULL || strcmp(cntrl->caption,"")== 0 )
return 1;
//make all text Calculations
/*get font */
font = TTF_OpenFont("arial.ttf", FONT_SIZE);
if (font == NULL) {
ttf_err = TTF_GetError();
if (ttf_err != NULL)
printf("ERROR: %s\n", ttf_err);
return -1;
}
/*assign a string to destory, and tokenize it*/
s = (char*)malloc(strlen(cntrl->caption) + 1);
if (s == NULL)
{
printf("ERROR: standard function malloc has failed\n");
TTF_CloseFont(font);
return -1;
}
strcpy(s,cntrl->caption);
token = strtok(s, "\n");
while (token) {
lines[linecount] = token;
linecount++;
token = strtok(NULL, "\n");
if (linecount == MAX_CAPTION_LINES-1)
return -1;
}
//record number of lines
cntrl->num_texts = linecount;
//this for makes sure that the a surface for the text will be made
for (i=0; i<linecount; i++)
{
//calculate text position
make_rect(&soure_rect, cntrl->h,cntrl->w,cntrl->offsetx,cntrl->offsety);
if (get_text_position_multi(&soure_rect, lines[i], font, &text_rect,prev_x_offset)==-1)
{
printf("ERROR: could not fit text in containing control\n");
return -1;
}
prev_x_offset = prev_x_offset + text_rect.h;
//if this line's text surface doesn't exist- make it
if (cntrl->multitext[i] == NULL)
{
surface = TTF_RenderText_Blended(font, lines[i], text_color);
if (surface==NULL){
ttf_err = TTF_GetError();
if (ttf_err != NULL)
printf("ERROR: %s\n", ttf_err);
SDL_FreeSurface(surface);
TTF_CloseFont(font);
return -1;
}
cntrl->multitext[i] = surface;
}
//blit text's surface
if (SDL_BlitSurface(cntrl->multitext[i], NULL, cntrl->srfc, &text_rect) != 0){
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n", sdl_err);
TTF_CloseFont(font);
return -1;
}
}
TTF_CloseFont(font);
return 1;
}
/* makes an element in the ui tree
return null on failure*/
element_cntrl new_control_element(control* cntrl)
{
element_cntrl elem =NULL;
elem=(element_cntrl)malloc(sizeof(struct element_s_cntrl));
if (elem==NULL)
{
printf("ERROR: standard function malloc has failed\n");
free_control(cntrl);
return NULL;
}
elem->cntrl = cntrl;
elem->next=NULL;
elem->prev=NULL;
elem->children=NULL;
elem->parent=NULL;
return elem;
}
/* makes an empty element linked list
return null on failure*/
linked_list_cntrl new_control_list()
{
linked_list_cntrl lst = (linked_list_cntrl)malloc(sizeof(struct linked_list_s_cntrl));
if (lst==NULL)
{
printf("ERROR: standard function malloc has failed\n");
free(lst);
return NULL;
}
lst->head = NULL;
lst->tail = NULL;
return lst;
}
/*adds control element to existing list*/
void add_control_element_to_list(linked_list_cntrl list, element_cntrl elem)
{
element_cntrl prev_tail;
if (list == NULL)
{
printf("ERROR: supplied list does not exist\n");
return;
}
if (elem==NULL)
{
printf("ERROR: supplied elem does not exist\n");
return;
}
if (list->head == NULL) //in case the list is empty- point both head and tail to the head
{
list->head = elem;
list->tail = elem;
list->tail->next = NULL;
}
else // add to existing list- add the element at the tail
{
prev_tail = list->tail;
prev_tail->next = elem;
elem->prev = prev_tail;
list->tail = elem;
list->tail->next = NULL;
}
}
/*sets a list of elements as children's list to the element*/
void set_list_as_children(linked_list_cntrl list, element_cntrl elem)
{
element_cntrl cur_elem=NULL,run=NULL;
if (list== NULL)
{
printf("ERROR: supplied list does not exist\n");
return;
}
if (elem== NULL)
{
printf("ERROR: supplied elem does not exists\n");
return;
}
//free prev list of the element (if he had one)
if (elem->children!=NULL){
for (run=elem->children->head;run!=NULL;run=run->next)
{
free_control_list(run);
}
free(elem->children);
}
// set list as children
elem->children = list;
// for every child, assign parent
for (cur_elem = list->head; cur_elem!= NULL ;cur_elem=cur_elem->next){
cur_elem->parent = elem;
}
}
/*gets UI tree root, draws it in DFS trasverse with the recursive function draw_ui_tree
* returns -1 on failure*/
int draw_ui_tree(element_cntrl root)
{
int ret_val = 0;
ret_val=draw_with_panel(root,root);
if (ret_val < 0)
{
printf("ERROR: could not draw a UI-Tree\n");
}
return ret_val;
}
/*recursive drawing, that passes owning panels
* to elements for overlay construction
* returns 1 on success,-1 on failure*/
int draw_with_panel(element_cntrl draw_cntrl, element_cntrl owning_panel)
{
element_cntrl cur_elem=NULL;
int err = 0;
// call the controls drawing function
if (draw_cntrl->parent== NULL)//in case of a window
{
err = draw_cntrl->cntrl->draw(draw_cntrl->cntrl,NULL);
if (err == -1)
{
return -1;
}
}
else//anything inside the window
{
err = draw_cntrl->cntrl->draw(draw_cntrl->cntrl,owning_panel->cntrl);
if (err == -1)
return -1;
}
// draw children
if (draw_cntrl->children != NULL){
for (cur_elem = draw_cntrl->children->head;
cur_elem!= NULL ;cur_elem=cur_elem->next)
{
/* if draw_cntrl is a panel,his children should be drawn relativly to him */
if (draw_cntrl->cntrl->is_panel)
{
err=draw_with_panel(cur_elem,draw_cntrl);
if (err<0)
{
printf("ERROR: failed to draw a control\n");
return -1;
}
}
/* if draw_cntrl isn't a panel,draw children relativly to his "owning_panel" */
else
{
err=draw_with_panel(cur_elem,owning_panel);
if (err<0)
{
printf("ERROR: failed to draw a control\n");
return -1;
}
}
}
return 0;
}
else{//no children
return 0;
}
}
/*init functions*/
/*makes new label. a label is a text with background. return null on failure*/
control* new_label(int x, int y, int w, int h, char *img, int R, int G, int B, int is_trans, char* caption)
{
int i=0;
control *label = (control*)malloc(sizeof(control));
if (label==NULL)
{
printf("ERROR: standard function malloc has failed\n");
return NULL;
}
label->is_button = 0;
label->is_label = 1;
label->is_panel = 0;
label->is_window = 0;
label->x = x;
label->y = y;
label->w = w;
label->h = h;
label->offsetx=0;
label->offsety=0;
label->is_transparant = is_trans;
label->R = R;
label->B = B;
label->G = G;
label->img = img;
label->draw=draw_label;
label->ownSurface = NULL;
label->caption = NULL;
if (caption != NULL)
{
label->caption = (char *) malloc (strlen (caption) + 1);
if (label->caption == NULL)
{
printf("ERROR: standard function malloc has failed");
return NULL;
}
strcpy(label->caption,caption);
}
label->pressed_button=empty_click_handle;
label->srfc=NULL;
label->button_choice=0;
label->text_surface=NULL;
label->destination_rect=NULL;
label->is_grid=0;
if (img != NULL)
{
label->is_bg_img = 1;
label->is_bg_rect = 0;
}
else
{
label->is_bg_img = 0;
label->is_bg_rect = 1;
}
label->multitext=NULL;
label->multitext = (SDL_Surface**)malloc(sizeof(SDL_Surface*)*MAX_CAPTION_LINES);
if (label->multitext==NULL)
{
printf("ERROR: standard function malloc has failed");
free(label);
return NULL;
}
for (i=0; i < MAX_CAPTION_LINES ; i++)
{
label->multitext[i] = (SDL_Surface*)NULL;
}
label->num_texts=0;
return label;
}
/*makes new button. return null on failure*/
control* new_button(int x, int y, char *img, int is_trans ,char *caption, int is_grid)
{
control *button = (control*)malloc(sizeof(control));
if (button==NULL)
{
printf("ERROR: standard function malloc has failed\n");
return NULL;
}
button->is_button = 1;
button->is_label = 0;
button->is_panel = 0;
button->is_window = 0;
button->x = x;
button->y = y;
button->w = 0;
button->h = 0;
button->offsetx=0;
button->offsety=0;
button->is_transparant = is_trans;
button->R = 0;
button->B = 0;
button->G = 0;
button->img = img;
button->draw=draw_button;
button->caption=caption;
button->ownSurface=NULL;
button->pressed_button=empty_click_handle;
button->srfc=NULL;
button->text_surface=NULL;
button->button_choice=0;
button->destination_rect=NULL;
button->is_grid=is_grid;
button->is_bg_img =1;
button->is_bg_rect =0;
button->multitext=NULL;
button->num_texts=0;
return button;
}
/**/
/* makes new panel.
* panel is a logical sub-window, meant to organise a part of the game area.
* evrey button and label must have a father(doesn't have to be direct) which is a panel.
return null on failure*/
control* new_panel(int x, int y, int w, int h, int R, int B, int G, int is_bg_rect)
{
// allocate window
control *panel = (control*)malloc(sizeof(control));
if (panel==NULL)
{
printf("ERROR: standard function malloc has failed\n");
return NULL;
}
panel->is_button = 0;
panel->is_label = 0;
panel->is_panel = 1;
panel->is_window = 0;
panel->x = x;
panel->y = y;
panel->h = h;
panel->w = w;
panel->offsetx=0;
panel->offsety=0;
panel->R = R;
panel->B = B;
panel->G = G;
panel->img = NULL;
panel->draw = draw_panel;// drawing funct
panel->ownSurface=NULL;
panel->caption=NULL;
panel->pressed_button=empty_click_handle;
panel->srfc=NULL;
panel->button_choice=0;
panel->text_surface = NULL;
panel->destination_rect=NULL;
panel->is_grid=0;
panel->is_bg_img =0;
panel->is_bg_rect=is_bg_rect;
panel->multitext=NULL;
panel->num_texts=0;
return panel;
}
/*makes new window.
the window's element is the ui tree root.
return 1 on success,-1 on failure
*/
control* new_window(int x, int y, int w, int h)
{
// allocate window
control *window = (control*)malloc(sizeof(control));
if (window==NULL)
{
printf("ERROR: standard function malloc has failed\n");
return NULL;
}
window->is_button = 0;
window->is_label = 0;
window->is_panel = 0;
window->is_window = 1;
window->x = x;
window->y = y;
window->h = h;
window->w = w;
window->offsetx=0;
window->offsety=0;
window->draw = draw_window;// drawing funct
window->ownSurface=NULL;
window->caption=NULL;
window->pressed_button=empty_click_handle;
window->srfc=NULL;
window->button_choice=0;
window->text_surface = NULL;
window->destination_rect=NULL;
window->is_grid=0;
window->is_bg_img =0;
window->is_bg_rect =0;
window->multitext=NULL;
window->num_texts=0;
return window;
}
/* drawing functions- each kind of control has its own drawing function*/
int draw_button(control *button, control *container)
{
char* sdl_err=NULL;
if (handle_control_surface_load(button,container)==-1)//the handle_control_surface_load makes evrey thing ready for bliting of the button
{
printf("ERROR: surface load failed\n");
return -1;
}
//blit the controls surface
if (SDL_BlitSurface(button->ownSurface,NULL, container->srfc, button->destination_rect) != 0)
{
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n", sdl_err);
return -1;
}
//add caption
if (button->caption != NULL){
if (draw_caption_to_control(button)==-1){
printf("ERROR: caption load failed\n");
return -1;
}
}
return 0;//sccussfuly drawn
}
int draw_label(control *label, control *container)
{
char* sdl_err = NULL;
/* the handle_control_surface_load makes evrey thing ready for bliting of the label */
if (handle_control_surface_load(label,container)==-1)
{
printf("ERROR: surface load failed\n");
return -1;
}
//blit the surface label
if (SDL_BlitSurface(label->ownSurface,NULL, container->srfc, label->destination_rect) != 0)
{
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n", sdl_err);
return -1;
}
//blit the label text (by using draw_caption_to_control_multi)
if (label->caption != NULL){
if (strchr(label->caption,'\n') != NULL)
{
if (draw_caption_to_control_multi(label)==-1){
printf("ERROR: caption load failed\n");
return -1;
}
}
else
{
if (draw_caption_to_control(label)==-1){
printf("ERROR: caption load failed\n");
return -1;
}
}
}
/* success */
return 0;
}
int draw_window(control* window, control *container)
{
char* sdl_err=NULL;
SDL_Surface *surface;
SDL_WM_SetCaption(GAME_WINDOW_NAME, GAME_WINDOW_NAME);
surface = SDL_SetVideoMode(window->w,window->h,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if (surface == NULL)
{
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n", sdl_err);
return -1;
}
window->srfc = surface;
window->ownSurface = surface;
//every time we blit a window we must make a new surface
window->x = 0;//
window->y = 0;
window->offsetx = 0;
window->offsety = 0;
return 0;
}
/*add this functionality to window as well. use it to paiunt background in white*/
int draw_panel(control* panel, control *container)
{
char* sdl_err = NULL;
if (handle_control_surface_load(panel,container)==-1)
{
printf("ERROR: loading surface failed\n");
return -1;
}
// blit panel surface
if (panel->ownSurface != NULL)
{
if ((int)SDL_FillRect(container->srfc,panel->destination_rect,
SDL_MapRGB(panel->ownSurface->format, panel->R, panel->G, panel->B)) != 0)
{
sdl_err = SDL_GetError();
if (sdl_err != NULL)
printf("ERROR: %s\n", sdl_err);
return -1;
}
}
return 0;//sccusses
}
/*DFS on UI tree, to find a pressable element, and set
* it to *target, according to final (with offset) coordinates
* if pressed control was a grid panel, we return it,
iff there is no button with same coordinates on this grid
in case the function didn't find anything it returns null in target*/
void find_element_by_coordinates(element_cntrl root,int x, int y, element_cntrl *target)
{
element_cntrl cur_elem=NULL;
*target=NULL;
if ((root->cntrl->is_grid))
{
// match coordinates
if ((x >= root->cntrl->offsetx && x <= (root->cntrl->offsetx + root->cntrl->w) &&
(y >= root->cntrl->offsety && y <= (root->cntrl->offsety + root->cntrl->h))))
{
*target = NULL;
//look for button in grid panel- if it is a grid panel(the only button with children
for (cur_elem = root->children->head;
cur_elem!= NULL ;cur_elem=cur_elem->next){
find_element_by_coordinates(cur_elem,x,y,target);
if(*target!=NULL){
break;
}
}
if (*target == NULL)//no matching button under greid or a button with no children, return grid itself
{
*target = root;
}
return;
}
}
/*if is a button and a tree leaf*/
else if (root->cntrl->is_button && root->children == NULL)
{
if ((x >= root->cntrl->offsetx && x <= (root->cntrl->offsetx + root->cntrl->w) &&
(y >= root->cntrl->offsety && y <= (root->cntrl->offsety + root->cntrl->h))))
{
*target = root;
return;
}
}
//if out of bounds-return
if(x< root->cntrl->offsetx){
return;
}
if(y<root->cntrl->offsety){
return;
}
if (x > (root->cntrl->offsetx + root->cntrl->w)){
return;
}
if (y > (root->cntrl->offsety + root->cntrl->h)){
return;
}
if (root->children == NULL){
return;//if there is no children-return
}
else{
for (cur_elem = root->children->head;
cur_elem!= NULL ;cur_elem=cur_elem->next){
find_element_by_coordinates(cur_elem,x,y,target);//check the children
if(*target!=NULL){
break;
}
}
}
return;
}
/*assume that ui_tree->children->tail is the game panel
* recursively free all controls and data structures
* associated with this panel, keeping the rest of the ui tree */
void clear_game_panel(element_cntrl ui_tree)
{
element_cntrl game_panel,pre_tail;
if(ui_tree->children==NULL ||
ui_tree->children->head==ui_tree->children->tail) //no previous game elements
{
return;
}
pre_tail=ui_tree->children->tail->prev;
// choose game panel in ui tree
game_panel=ui_tree->children->tail;
//call recursive free
free_control_list(game_panel);
//set pre_tail as new tail
pre_tail->next=NULL;
ui_tree->children->tail=pre_tail;
}
/* Recursively free all Dynamically Allocated Memory :
* SDL objects, strings, and data structure */
void free_control_list(element_cntrl node)
{
element_cntrl cur_elem=NULL,next_elem=NULL;
if (node == NULL){
return;
}
//free node children
if (node->children != NULL){
for (cur_elem=node->children->head;
cur_elem != NULL; cur_elem=next_elem){
next_elem=cur_elem->next;
free_control_list(cur_elem);
}
free(node->children);
}
//free control and node
free_control(node->cntrl);
free(node);
}
/*if a control with this function is pressed, do nothing and continue.*/
int empty_click_handle(int *choice,SDL_Event* test_event)
{
return 0;
}
void free_control(control *cntrl)
{
int i=0;
//free SDL surface associated with control
if (cntrl->ownSurface != NULL){
SDL_FreeSurface(cntrl->ownSurface);
}
cntrl->ownSurface = NULL;
if (cntrl->text_surface != NULL){
SDL_FreeSurface(cntrl->text_surface);
}
if (cntrl->destination_rect != NULL){
free(cntrl->destination_rect);
}
/*frees up texts*/
if (cntrl->multitext != NULL){
for (i=0; i<cntrl->num_texts; i++)
{
if (cntrl->multitext[i] != NULL)
SDL_FreeSurface(cntrl->multitext[i]);
}
free(cntrl->multitext);