-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdl.c
1704 lines (1339 loc) · 40.5 KB
/
sdl.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
/*
Author: Mike Bok
Email: [email protected]
Website: www.nighsoft.net
License and Rights: Full licensing and Rights available at www.nighsoft.net.
If licensing and rights not available at www.nighsoft.net, then all the following apply as bulleted:
-all rights reserved.
-you can not modify source code in this file.
-you can compile and use this source code.
*/
#include "main.h"
#define REPEAT_CHAR_MAX 255
//int screen_flipping;
int display_box_adding;
double repeat_button_time[REPEAT_CHAR_MAX];
int button_pressed[REPEAT_CHAR_MAX];
SDL_Thread *repeat_button_thread[REPEAT_CHAR_MAX];
void add_display_box_mutex();
int repeat_button(void *nothing);
void hit_hutton(int the_char);
void clear_all_buttons_pressed();
int init_sdl()
{
int i;
if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0)
{
printf("error: could not load sdl\n");
return 0;
}
if( TTF_Init() <0)
{
printf("error: could not load sdl_ttf\n");
return 0;
}
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
//screen = SDL_SetVideoMode(800,600,0,SDL_ANYFORMAT);
if (!screen)
{
printf("error: could not set screen for 800x600 32bit\n");
return 0;
}
ttf_font = TTF_OpenFont("fonts/arial.ttf",12);
if (!ttf_font)
{
printf("error: could not load fonts/arial.ttf\n");
return 0;
}
SDL_WM_SetCaption("Project Diaspora", "Project Diaspora");
SDL_WM_SetIcon(IMG_Load("graphics/game_icon.gif"), NULL);
SDL_EnableUNICODE(SDL_ENABLE);
//mutexs
//space_mutex = SDL_CreateMutex();
flip_mutex = SDL_CreateMutex();
atexit(SDL_Quit);
//screen_flipping = 0;
display_box_adding = 0;
for(i=0;i<REPEAT_CHAR_MAX;i++)
{
repeat_button_thread[i] = 0;
repeat_button_time[i] = 0;
button_pressed[i] = 0;
}
return 1;
}
int handle_sdl_events()
{
SDL_Event event;
int exitkey = 0;
int i;
int ctrl_down = 0;
while(SDL_PollEvent(&event))
switch( event.type )
{
case SDL_QUIT:
if(map.visible || space.visible) break;
//exitkey = 1;
return 1;
break;
case SDL_KEYDOWN:
//printf("Key hit sym:%c %d:caps %d\n", event.key.keysym.sym, event.key.keysym.sym, (((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) - !(!(event.key.keysym.mod & KMOD_CAPS))));
//printf("Key hit uni:%c %d\n", event.key.keysym.unicode, event.key.keysym.unicode);
//handle_key(convert_key_sym(event.key.keysym.sym,(((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) - !(!(event.key.keysym.mod & KMOD_CAPS)))));
clear_all_buttons_pressed();
if(event.key.keysym.unicode && event.key.keysym.unicode <= 26)
handle_ctrl_key((event.key.keysym.unicode - 1) + 'a');
if(event.key.keysym.sym != 273 && event.key.keysym.sym != 274 && event.key.keysym.sym != 275 && event.key.keysym.sym != 276 && event.key.keysym.sym != 127 && event.key.keysym.sym != 280 && event.key.keysym.sym != 281)
{
button_pressed[event.key.keysym.unicode] = 1;
hit_hutton(event.key.keysym.unicode);
//printf("hit number:%d\n",event.key.keysym.unicode);
}
else
{
button_pressed[event.key.keysym.sym] = 1;
hit_hutton(event.key.keysym.sym);
//printf("hit number:%d\n",event.key.keysym.sym);
}
break;
case SDL_KEYUP:
clear_all_buttons_pressed();
if(event.key.keysym.sym != 273 && event.key.keysym.sym != 274 && event.key.keysym.sym != 275 && event.key.keysym.sym != 276 && event.key.keysym.sym != 127 && event.key.keysym.sym != 280 && event.key.keysym.sym != 281)
{
button_pressed[event.key.keysym.unicode] = 0;
//printf("unhit number:%d\n",event.key.keysym.unicode);
}
else
{
button_pressed[event.key.keysym.sym] = 0;
//printf("unhit number:%d\n",event.key.keysym.sym);
}
break;
case SDL_MOUSEBUTTONDOWN:
//printf("Click down at x:%d y:%d\n", event.button.x, event.button.y);
if(event.button.button == SDL_BUTTON_LEFT)
handle_button(event.button.x,event.button.y);
else if(event.button.button == SDL_BUTTON_RIGHT)
handle_button_right(event.button.x,event.button.y);
break;
case SDL_MOUSEBUTTONUP:
//printf("Click up at x:%d y:%d\n", event.button.x, event.button.y);
handle_button_up(event.button.x,event.button.y);
break;
case SDL_MOUSEMOTION :
//printf("Motion at x:%d y:%d\n", event.button.x, event.button.y);
handle_motion(event.motion.x,event.motion.y);
break;
}
return 0;
}
void handle_events()
{
SDL_Event event;
int exitkey = 0;
int i;
int ctrl_down = 0;
//clear this all out
for(i=0;i<REPEAT_CHAR_MAX;i++)
{
repeat_button_thread[i] = 0;
repeat_button_time[i] = 0;
button_pressed[i] = 0;
}
//SDL_WaitEvent(&event)
//SDL_PollEvent(&event)
while(!exitkey)
{
//handle some connections
handle_sockets();
//handle sdl event
if(SDL_PollEvent(&event))
switch( event.type )
{
case SDL_QUIT:
if(map.visible || space.visible) break;
exitkey = 1;
break;
case SDL_KEYDOWN:
//printf("Key hit sym:%c %d:caps %d\n", event.key.keysym.sym, event.key.keysym.sym, (((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) - !(!(event.key.keysym.mod & KMOD_CAPS))));
//printf("Key hit uni:%c %d\n", event.key.keysym.unicode, event.key.keysym.unicode);
//handle_key(convert_key_sym(event.key.keysym.sym,(((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) - !(!(event.key.keysym.mod & KMOD_CAPS)))));
clear_all_buttons_pressed();
if(event.key.keysym.unicode && event.key.keysym.unicode <= 26)
handle_ctrl_key((event.key.keysym.unicode - 1) + 'a');
if(event.key.keysym.sym != 273 && event.key.keysym.sym != 274 && event.key.keysym.sym != 275 && event.key.keysym.sym != 276 && event.key.keysym.sym != 127 && event.key.keysym.sym != 280 && event.key.keysym.sym != 281)
{
button_pressed[event.key.keysym.unicode] = 1;
hit_hutton(event.key.keysym.unicode);
//printf("hit number:%d\n",event.key.keysym.unicode);
}
else
{
button_pressed[event.key.keysym.sym] = 1;
hit_hutton(event.key.keysym.sym);
//printf("hit number:%d\n",event.key.keysym.sym);
}
break;
case SDL_KEYUP:
clear_all_buttons_pressed();
if(event.key.keysym.sym != 273 && event.key.keysym.sym != 274 && event.key.keysym.sym != 275 && event.key.keysym.sym != 276 && event.key.keysym.sym != 127 && event.key.keysym.sym != 280 && event.key.keysym.sym != 281)
{
button_pressed[event.key.keysym.unicode] = 0;
//printf("unhit number:%d\n",event.key.keysym.unicode);
}
else
{
button_pressed[event.key.keysym.sym] = 0;
//printf("unhit number:%d\n",event.key.keysym.sym);
}
break;
case SDL_MOUSEBUTTONDOWN:
//printf("Click down at x:%d y:%d\n", event.button.x, event.button.y);
if(event.button.button == SDL_BUTTON_LEFT)
handle_button(event.button.x,event.button.y);
else if(event.button.button == SDL_BUTTON_RIGHT)
handle_button_right(event.button.x,event.button.y);
break;
case SDL_MOUSEBUTTONUP:
//printf("Click up at x:%d y:%d\n", event.button.x, event.button.y);
handle_button_up(event.button.x,event.button.y);
break;
case SDL_MOUSEMOTION :
//printf("Motion at x:%d y:%d\n", event.button.x, event.button.y);
handle_motion(event.motion.x,event.motion.y);
break;
}
#ifdef WIN32
Sleep(1);
#else
usleep(1000);
#endif
}
}
void sdl_flip_mutex()
{
// while (screen_flipping)
// {
// #ifdef WIN32
// Sleep(1);
// #else
// usleep(1000);
// #endif
// }
SDL_LockMutex ( flip_mutex );
SDL_Flip(screen);
SDL_UnlockMutex ( flip_mutex );
}
void add_display_box_mutex()
{
while (display_box_adding)
{
#ifdef WIN32
Sleep(1);
#else
usleep(1000);
#endif
}
}
void clear_all_buttons_pressed()
{
int i;
for(i=0;i<REPEAT_CHAR_MAX;i++)
button_pressed[i] = 0;
}
void hit_hutton(int the_char)
{
static int the_input;
repeat_button_time[the_char] = current_time();
handle_key(the_char);
if(!repeat_button_thread[the_char])
{
the_input = the_char;
repeat_button_thread[the_char] = SDL_CreateThread(repeat_button, (void*)&the_input);
}
}
int repeat_button(void *nothing)
{
int i, j;
const int delay_time = 80;
const int delay_time_start = 250;
const int delay_time_short = delay_time / 10;
i = *((int*)nothing);
for(j=0;j<delay_time_start && button_pressed[i];j+=delay_time_short)
SDL_Delay(delay_time_short);
while(button_pressed[i])
{
SDL_Delay(delay_time_short);
if(!button_pressed[i])
break;
if(current_time() - repeat_button_time[i] < delay_time * 0.001)
continue;
if(!button_pressed[i])
break;
repeat_button_time[i] = current_time();
handle_key(i);
}
repeat_button_thread[i] = 0;
}
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
void put32pixel(SDL_Surface *surface, int x, int y, SDL_Color color)
{
SDL_PixelFormat *fmt;
//SDL_Surface *surface;
SDL_Color return_color;
Uint32 temp, *pixel;
Uint8 red, green, blue, alpha;
fmt = surface->format;
//SDL_LockSurface(surface);
//pixel = *((Uint32*)surface->pixels);
//pixel = *((Uint32*)((Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel));
//SDL_UnlockSurface(surface);
pixel = (Uint32*)((Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel);
//clear red
((Uint8*)pixel)[3] = color.r;
((Uint8*)pixel)[2] = color.g;
((Uint8*)pixel)[1] = color.b;
((Uint8*)pixel)[0] = color.unused;
}
SDL_Color get32pixel(SDL_Surface *surface, int x, int y)
{
SDL_PixelFormat *fmt;
//SDL_Surface *surface;
SDL_Color return_color;
Uint32 temp, pixel;
Uint8 red, green, blue, alpha;
fmt = surface->format;
//SDL_LockSurface(surface);
//pixel = *((Uint32*)surface->pixels);
pixel = *((Uint32*)((Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel));
//SDL_UnlockSurface(surface);
/* Get Red component */
temp = pixel & fmt->Rmask; /* Isolate red component */
temp = temp >> fmt->Rshift; /* Shift it down to 8-bit */
temp = temp << fmt->Rloss; /* Expand to a full 8-bit number */
red = (Uint8)temp;
/* Get Green component */
temp = pixel & fmt->Gmask; /* Isolate green component */
temp = temp >> fmt->Gshift; /* Shift it down to 8-bit */
temp = temp << fmt->Gloss; /* Expand to a full 8-bit number */
green = (Uint8)temp;
/* Get Blue component */
temp = pixel & fmt->Bmask; /* Isolate blue component */
temp = temp >> fmt->Bshift; /* Shift it down to 8-bit */
temp = temp << fmt->Bloss; /* Expand to a full 8-bit number */
blue = (Uint8)temp;
/* Get Alpha component */
temp = pixel & fmt->Amask; /* Isolate alpha component */
temp = temp >> fmt->Ashift; /* Shift it down to 8-bit */
temp = temp << fmt->Aloss; /* Expand to a full 8-bit number */
alpha = (Uint8)temp;
return_color.r = red;
return_color.g = green;
return_color.b = blue;
return_color.unused = alpha;
return return_color;
}
SDL_Surface *add_red_glow(SDL_Surface *the_img)
{
SDL_Surface *new_img;
SDL_Color color;
int x, y, w, h;
int xi, yi;
const int max_runs = 10;
const int glow_inc = -15;
const int glow_start = 150;
const int alpha_inc = -25;
const int alpha_start = 255;
int i, r, cur_glow, cur_alpha;
char **temp_img;
SDL_Rect rect;
if(!the_img) return NULL;
//set some stuff for ease
w = the_img->w;
h = the_img->h;
//make temp_img
temp_img = (char**)malloc(w * sizeof(char*));
for(i=0;i<w;i++)
temp_img[i] = (char*)malloc(h);
//make the 32 bit image
new_img = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA,w,h,32,0xFF000000,0x0000FF00,0x00FF0000,0x000000FF);
if(!new_img)
{
printf("unable to make a new sos glow image\n");
return NULL;
}
//put the original on top of it
rect.x = 0;
rect.y = 0;
rect.w = w;
rect.h = h;
SDL_BlitSurface(the_img, NULL, new_img, &rect);
the_img = new_img;
SDL_LockSurface(the_img);
//do it
for(r=0, cur_glow=glow_start, cur_alpha=alpha_start;r<max_runs;r++, cur_glow+=glow_inc, cur_alpha+=alpha_inc)
{
//find all the pixels we want
for(x=0;x<w;x++)
for(y=0;y<h;y++)
{
//clear this pixel out
temp_img[x][y] = 0;
color = get32pixel(the_img, x, y);
//skip if this sector is a good color
if(color.unused) continue;
//check its neighbors
for(xi=x-1;xi<=x+1;xi++)
{
//out of bounds?
if(xi<0) continue;
if(xi>=w) continue;
for(yi=y-1;yi<=y+1;yi++)
{
//out of bounds?
if(yi<0) continue;
if(yi>=h) continue;
//specials, no corners
if(xi==x-1 && yi==y-1) continue;
if(xi==x+1 && yi==y-1) continue;
if(xi==x-1 && yi==y+1) continue;
if(xi==x+1 && yi==y+1) continue;
color = get32pixel(the_img, xi, yi);
//skip if this neighbor is the alpha color
if(!color.unused) continue;
temp_img[x][y] = 1;
//cause a break out
xi=x+3;
break;
}
}
}
//set those pixels
color.r = cur_glow;
color.g = 0;
color.b = 0;
color.unused = cur_alpha;
for(x=0;x<w;x++)
for(y=0;y<h;y++)
if(temp_img[x][y])
put32pixel(the_img, x, y, color);
}
/* Unlock the surface */
SDL_UnlockSurface(the_img);
//make temp_img
for(i=0;i<w;i++)
free(temp_img[i]);
free(temp_img);
return the_img;
}
SDL_Color get_pixel(SDL_Surface *the_img, int x, int y)
{
Uint8 *ubuff8;
ubuff8 = (Uint8*) the_img->pixels;
ubuff8 += (y * the_img->pitch) + x;
return the_img->format->palette->colors[*ubuff8];
}
void set_pixel(SDL_Surface *screan, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint8 *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;
/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screan))
{
if(SDL_LockSurface(screan) < 0)
return;
}
/* Get the color */
color = SDL_MapRGB( screan->format, r, g, b );
/* How we draw the pixel depends on the bitdepth */
switch(screan->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) screan->pixels;
ubuff8 += (y * screan->pitch) + x;
*ubuff8 = color;
break;
case 2:
ubuff8 = (Uint8*) screan->pixels;
ubuff8 += (y * screan->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;
case 3:
ubuff8 = (Uint8*) screan->pixels;
ubuff8 += (y * screan->pitch) + (x*3);
if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
} else {
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}
ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;
case 4:
ubuff8 = (Uint8*) screan->pixels;
ubuff8 += (y*screan->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;
default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}
/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screan))
{
SDL_UnlockSurface(screan);
}
}
void draw_label(SDL_Surface *screan, char *message, struct label *label, int r, int g, int b)
{
SDL_Surface *text;
SDL_Color textcolor = {0,0,0,0};
text = TTF_RenderText_Solid(ttf_font, label->message, textcolor);
SDL_BlitSurface( text, NULL, screan, &label->location);
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
if(label->message != message)
strcpy(label->message, message);
text = TTF_RenderText_Solid(ttf_font, label->message, textcolor);
SDL_BlitSurface( text, NULL, screan, &label->location);
}
void draw_center_label(SDL_Surface *screan, char *message, struct label *label, int r, int g, int b)
{
SDL_Rect temp_location;
SDL_Surface *text;
SDL_Color textcolor = {0,0,0,0};
temp_location.y = label->location.y;
temp_location.w = label->location.w;
temp_location.h = label->location.h;
text = TTF_RenderText_Solid(ttf_font, label->message, textcolor);
if (text)
{
temp_location.x = label->location.x + (label->location.w / 2) - (text->w / 2); //center text
SDL_BlitSurface( text, NULL, screan, &temp_location);
}
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
if(label->message != message)
strcpy(label->message, message);
text = TTF_RenderText_Solid(ttf_font, label->message, textcolor);
if (text)
{
temp_location.x = label->location.x + (label->location.w / 2) - (text->w / 2); //center text
SDL_BlitSurface( text, NULL, screan, &temp_location);
}
}
void draw_right_label(SDL_Surface *screan, char *message, struct label *label, int r, int g, int b)
{
SDL_Rect temp_location;
SDL_Surface *text;
SDL_Color textcolor = {0,0,0,0};
temp_location.y = label->location.y;
temp_location.w = label->location.w;
temp_location.h = label->location.h;
text = TTF_RenderText_Solid(ttf_font, label->message, textcolor);
if (text)
{
temp_location.x = label->location.x + (label->location.w - text->w); //right align test
SDL_BlitSurface( text, NULL, screan, &temp_location);
}
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
if(label->message != message)
strcpy(label->message, message);
text = TTF_RenderText_Solid(ttf_font, label->message, textcolor);
if (text)
{
temp_location.x = label->location.x + (label->location.w - text->w); //right align test
SDL_BlitSurface( text, NULL, screan, &temp_location);
}
}
void draw_raw_label(SDL_Surface *screan, char *message, SDL_Rect location, int r, int g, int b)
{
SDL_Surface *text;
SDL_Color textcolor;
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
textcolor.unused = 0;
text = TTF_RenderText_Solid(ttf_font, message, textcolor);
SDL_BlitSurface( text, NULL, screan, &location);
}
void draw_raw_right_label(SDL_Surface *screan, char *message, SDL_Rect location, int r, int g, int b)
{
SDL_Rect temp_location;
SDL_Surface *text;
SDL_Color textcolor = {0,0,0,0};
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
text = TTF_RenderText_Solid(ttf_font, message, textcolor);
if (text)
{
location.x = location.x + (location.w - text->w); //right align test
SDL_BlitSurface( text, NULL, screan, &location);
}
}
void draw_raw_center_label(SDL_Surface *screan, char *message, SDL_Rect location, int r, int g, int b)
{
SDL_Surface *text;
SDL_Color textcolor;
if(message[0] == '\0') return;
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
textcolor.unused = 0;
text = TTF_RenderText_Solid(ttf_font, message, textcolor);
location.x = location.x + (location.w / 2) - (text->w / 2);
SDL_BlitSurface( text, NULL, screan, &location);
}
void draw_text_box(SDL_Surface *screan, char *message, struct textbox *text_box, int r, int g, int b)
{
int temp_width;
int temp_frame;
SDL_Surface *text;
SDL_Color textcolor = {0,0,0,0};
SDL_Rect temp_location, clr_box;
//just is needed
temp_width = text_box->location.w; //preserve the correct width
//mess with this
temp_location = text_box->location;
temp_location.y += 4;
temp_location.h = 12;
//set this
clr_box = text_box->location;
//colors!
textcolor.r = r;
textcolor.g = g;
textcolor.b = b;
//text = TTF_RenderText_Solid(ttf_font, text_box->prev_message, textcolor);
//SDL_BlitSurface( text, NULL, screan, &text_box->location);//<-changes "location" width to width of text
//clear the shit
SDL_FillRect(screan, &clr_box, SDL_MapRGB(screen->format, 0, 0, 0));
//strcpy(text_box->prev_message, message);
//set the text
text = TTF_RenderText_Solid(ttf_font, message, textcolor);
if(!text) return;
//set the offset
temp_location.x = text_box->last_shift;
temp_location.y = 0;
temp_location.h = text->h;
if(text_box->cursor_loc - text_box->last_shift > temp_location.w)
temp_location.x = text_box->cursor_loc - temp_location.w;
if(text_box->cursor_loc - 3 < text_box->last_shift)
temp_location.x = text_box->cursor_loc - 3;
if(temp_location.x < 0) temp_location.x = 0;
text_box->last_shift = temp_location.x;
//draw
SDL_BlitSurface( text, &temp_location, screan, &text_box->location);//<-changes "location" width to width of text
//do this
text_box->location.w = temp_width;
}
void clear_text_box(SDL_Surface *screan, struct textbox *text_box)
{
text_box->message[0] = '\0';
text_box->cursor_loc = 0;
text_box->last_shift = 0;
if (screan) //if not NULL
{
if (text_box->selected)
{
draw_text_box(screan, "|", text_box, 255, 255, 255);
//3;
}
else
{
draw_text_box(screan, "", text_box, 255, 255, 255);
}
}
else
{
text_box->prev_message[0] = '\0';
text_box->selected = 0;
}
text_box->cursor = 0;
text_box->char_amt = 0;
}
void set_text_box_text(SDL_Surface *screan, struct textbox *text_box, char *message)
{
if(text_box->message != message)
strcpy(text_box->message, message);
text_box->char_amt = strlen(message);
text_box->cursor = text_box->char_amt;
text_box->last_shift = 0;
if (screan) //if not NULL
{
if (text_box->selected)
select_text_box(screan,text_box);
else
unselect_text_box(screan,text_box);
}
else
{
;//text_box->prev_message[0] = '\0';
}
}
void add_text_box(SDL_Surface *screan, struct textbox *text_box, int c)
{
char temp_message[405] = "";
char temp_left[405];
int i, w, h;
switch (c)
{
case 8: //backspace
if (text_box->cursor == 0) return;
text_box->cursor = text_box->cursor - 1;
text_box->char_amt = text_box->char_amt - 1;
if (text_box->cursor != 0)
{
left(temp_left, text_box->message, text_box->cursor);
strcat(temp_message, temp_left);
}
if (text_box->cursor != text_box->char_amt)
{
right(temp_left, text_box->message, text_box->cursor + 1);
strcat(temp_message, temp_left);
}
strcpy(text_box->message, temp_message);
text_box->message[text_box->char_amt] = '\0';
if (text_box->pass_char == '\0')
{
if (text_box->cursor != 0)
{
left(temp_left, text_box->message, text_box->cursor);
strcat(temp_message, temp_left);
}
temp_message[text_box->cursor] = '|';
temp_message[text_box->cursor + 1] = '\0';
TTF_SizeText(ttf_font, temp_message, &(text_box->cursor_loc), &h);
right(temp_left, text_box->message, text_box->cursor);
strcat(temp_message, temp_left);
}
else
{
for(i=0;i<text_box->cursor;i++)
temp_message[i] = text_box->pass_char;
temp_message[i] = '|';
temp_message[i+1] = '\0';
TTF_SizeText(ttf_font, temp_message, &(text_box->cursor_loc), &h);
for(i++;i<text_box->char_amt + 1;i++)
temp_message[i] = text_box->pass_char;
temp_message[i] = '\0';
}
draw_text_box(screan, temp_message, text_box, 250, 250, 250);
break;
case 127: //delete
if (text_box->cursor == text_box->char_amt) return;
text_box->cursor = text_box->cursor;
text_box->char_amt = text_box->char_amt - 1;
if (text_box->cursor != 0)
{
left(temp_left, text_box->message, text_box->cursor);
strcat(temp_message, temp_left);
}
if (text_box->cursor != text_box->char_amt)
{
right(temp_left, text_box->message, text_box->cursor + 1);
strcat(temp_message, temp_left);
}
strcpy(text_box->message, temp_message);
text_box->message[text_box->char_amt] = '\0';
if (text_box->pass_char == '\0')
{
if (text_box->cursor != 0)
{
left(temp_left, text_box->message, text_box->cursor);