-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdl.c
2028 lines (1626 loc) · 49.3 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
/* AF-84 game simulator, code file for SDL 1.2 and 2.0
*
* Copyright (C) 2021 Sunxu <xusun521@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdbool.h>
#include <string.h>
#if defined SDL_1
# define SDL_VER_STR "1.2"
# include "SDL/SDL.h"
//# define SDL_SCREEN_TYPE SDL_Surface*
//# define SDL_RASTER_TYPE SDL_Surface*
#elif defined SDL_2
# define SDL_VER_STR "2.0"
# include "SDL2/SDL.h"
//# define SDL_SCREEN_TYPE SDL_Window*
//# define SDL_RASTER_TYPE SDL_Texture*
#else
# error "neither SDL_1 nor SDL_2 is defined"
#endif
//#include "SDL_ttf.h"
SDL_Surface* Screen;
#if defined SDL_2
//SDL_Renderer* Renderer;
//SDL_Haptic* HapticDevice;
SDL_Window* mainwindows;
SDL_Rect mainDestRec;
#endif
SDL_Surface* RGBSurface = NULL;
/* - - - DATA DEFINITIONS - - - */
bool hiResMode = true;
bool debugcheck = false;
//处理普通飞机移动模式
bool handlePlan();
//处理小船逻辑
bool handleShip();
//初始化游戏数据
void initMainGame( bool isintinal);
//飞机图片索引,从左67.5度开始计算
SDL_Surface* planmap1 = NULL; //67.5
SDL_Surface* planmap2 = NULL; //45
SDL_Surface* planmap3 = NULL; //22.5
SDL_Surface* planmap4 = NULL; //0
SDL_Surface* planmap5 = NULL; //-22.5
SDL_Surface* planmap6 = NULL; //-45
SDL_Surface* planmap7 = NULL; //-67.5
SDL_Surface* planshoot = NULL;//击中飞机
SDL_Surface* planboom = NULL; //飞机爆炸
SDL_Surface* helicopter = NULL;//营救直升机
SDL_Surface* scoremap = NULL; // SCORE显示
SDL_Surface* levelmap = NULL; // LEVEL显示
SDL_Surface* batterymap = NULL; // 炮台
SDL_Surface* batteryshoot = NULL; // 炮台发射
SDL_Surface* shipmap = NULL; //小船
SDL_Surface* shipboom = NULL; //小船爆炸
SDL_Surface* backgroundmap = NULL; //大背景图
SDL_Surface* emptyImgae = NULL; //空白图
SDL_Surface* LifeCountImage = NULL; //生命计数图
SDL_Surface* GameOverImage = NULL; //GameOver图
SDL_Surface* SuspendImage = NULL; //暂停游戏图
SDL_Surface* MuteImage = NULL; //静音标志图
SDL_Surface* Input1Image = NULL; //输入1图
SDL_Surface* Input0Image = NULL; //输入0图
SDL_Surface* Cloud1Image = NULL; //云朵1
SDL_Surface* Cloud2Image = NULL; //云朵2
//int plan1Table[1,2,3,4];//第一列飞机图像索引,0表示不显示
//int plan2Table[2,3,4,1];//第二列飞机图像索引,0表示不显示
//int plan3Table[2,5,6,3];//第三列飞机图像索引,0表示不显示
//int freeplanTable[1,2,3,4,5,6,7];//自由飞机图像索引,0表示不显示
SDL_Surface* DigtialNumber[11]={NULL};
SDL_Surface* digitialvalue = NULL; //五位数字显示
SDL_Surface* SmallDigtialNumber[10]={NULL};
SDL_Surface* smallDigitialvalue = NULL; //三位小数字显示
#include "SDL_image.h"
//加载图像文件到surface
void LoadSurfacefromFile(SDL_Surface** desSurface, const char* filename)
{
SDL_Surface* tmppngsurf = NULL;
SDL_ClearError();
tmppngsurf = IMG_Load(filename);
if(tmppngsurf)
{
#if defined SDL_1
(*desSurface)=SDL_ConvertSurface(tmppngsurf, Screen->format, SDL_SRCCOLORKEY|SDL_RLEACCEL);
Uint32 colorkey = *((Uint32*)(*desSurface)->pixels);
SDL_SetColorKey((*desSurface), SDL_SRCCOLORKEY|SDL_RLEACCEL, colorkey);
#elif defined SDL_2
(*desSurface) = SDL_ConvertSurfaceFormat(tmppngsurf, Screen->format->format, 0);
Uint32 colorkey = *((Uint32*)(*desSurface)->pixels);
SDL_SetColorKey((*desSurface), 1, colorkey);
#else
# error "neither SDL_1 nor SDL_2 is defined"
#endif
SDL_FreeSurface(tmppngsurf);
if(debugcheck) printf("LoadSurfacefromFile OK load :%s, res:%dx%d error:%s \n", filename, (*desSurface)->w, (*desSurface)->h, SDL_GetError());
}
else
printf("Couldn't LoadSurfacefromFile:%s, error:%s\n", filename, SDL_GetError());
}
//SDL_RWops * SDLCALL SDL_RWFromMem(void *mem, int size);
//freesrc A non-zero value mean is will automatically close/free the src for you.
//SDL_Surface * SDLCALL IMG_Load_RW(SDL_RWops *src, int freesrc);
//void SDLCALL SDL_FreeRW(SDL_RWops *area);
//初始化图像内容
void initVideoSurface()
{
printf("enrty initVideoSurface\n");
LoadSurfacefromFile(&planmap1, "Plan1.png");
LoadSurfacefromFile(&planmap2, "Plan2.png");
LoadSurfacefromFile(&planmap3, "Plan3.png");
LoadSurfacefromFile(&planmap4, "Plan4.png");
LoadSurfacefromFile(&planmap5, "Plan5.png");
LoadSurfacefromFile(&planmap6, "Plan6.png");
LoadSurfacefromFile(&planmap7, "Plan7.png");
LoadSurfacefromFile(&planshoot, "PlanShoot.png");
LoadSurfacefromFile(&planboom, "PlanBoom.png");
LoadSurfacefromFile(&helicopter, "Helicopter.png");
LoadSurfacefromFile(&scoremap, "Score.png");
LoadSurfacefromFile(&levelmap, "Level.png");
LoadSurfacefromFile(&batterymap, "Battery.png");
LoadSurfacefromFile(&batteryshoot, "BatteryShoot.png");
LoadSurfacefromFile(&shipmap, "Ship.png");
LoadSurfacefromFile(&shipboom, "ShipBoom.png");
LoadSurfacefromFile(&backgroundmap, "MainBack.png");
LoadSurfacefromFile(&LifeCountImage, "Life.png");
LoadSurfacefromFile(&GameOverImage, "GameOver.png");
//SDL_Surface* tmppngsurf = IMG_Load("MainBack.png");
//backgroundmap = SDL_ConvertSurface(tmppngsurf, Screen->format, SDL_RLEACCEL);
//SDL_SetAlpha(backgroundmap, 0, 255);
//SDL_FreeSurface(tmppngsurf);
LoadSurfacefromFile(&digitialvalue, "Digtal.png");
char DigName[16];
for(int i= 0;i<10;i++)
{
sprintf(DigName,"%d.png",i);
LoadSurfacefromFile(&(DigtialNumber[i]), DigName);
}
LoadSurfacefromFile(&(DigtialNumber[10]), "Heng.png");
for(int i= 0;i<10;i++)
{
sprintf(DigName,"S%d.png",i);
LoadSurfacefromFile(&(SmallDigtialNumber[i]), DigName);
}
LoadSurfacefromFile(&smallDigitialvalue, "SDigtal.png");
LoadSurfacefromFile(&SuspendImage, "Suspend.png");
LoadSurfacefromFile(&MuteImage, "Mute.png");
LoadSurfacefromFile(&Input1Image, "Input1.png");
LoadSurfacefromFile(&Input0Image, "Input0.png");
LoadSurfacefromFile(&Cloud1Image, "Cloud1.png");
LoadSurfacefromFile(&Cloud2Image, "Cloud2.png");
}
//卸载图片资源
void unloadVideoSurface()
{
if(planmap1) SDL_FreeSurface(planmap1);
if(planmap2) SDL_FreeSurface(planmap2);
if(planmap3) SDL_FreeSurface(planmap3);
if(planmap4) SDL_FreeSurface(planmap4);
if(planmap5) SDL_FreeSurface(planmap5);
if(planmap6) SDL_FreeSurface(planmap6);
if(planmap7) SDL_FreeSurface(planmap7);
if(planshoot) SDL_FreeSurface(planshoot);
if(planboom) SDL_FreeSurface(planboom);
if(helicopter) SDL_FreeSurface(helicopter);
if(scoremap) SDL_FreeSurface(scoremap);
if(levelmap) SDL_FreeSurface(levelmap);
if(batterymap) SDL_FreeSurface(batterymap);
if(batteryshoot) SDL_FreeSurface(batteryshoot);
if(shipmap) SDL_FreeSurface(shipmap);
if(shipboom) SDL_FreeSurface(shipboom);
if(backgroundmap) SDL_FreeSurface(backgroundmap);
if(LifeCountImage) SDL_FreeSurface(LifeCountImage);
if(GameOverImage) SDL_FreeSurface(GameOverImage);
if(digitialvalue) SDL_FreeSurface(digitialvalue);
for(int i= 0;i<11;i++)
{
if(DigtialNumber[i]) SDL_FreeSurface(DigtialNumber[i]);
}
if(smallDigitialvalue) SDL_FreeSurface(smallDigitialvalue);
for(int i= 0;i<10;i++)
{
if(SmallDigtialNumber[i]) SDL_FreeSurface(SmallDigtialNumber[i]);
}
if(SuspendImage) SDL_FreeSurface(SuspendImage);
if(MuteImage) SDL_FreeSurface(MuteImage);
if(Input1Image) SDL_FreeSurface(Input1Image);
if(Input0Image) SDL_FreeSurface(Input0Image);
if(Cloud1Image) SDL_FreeSurface(Cloud1Image);
if(Cloud2Image) SDL_FreeSurface(Cloud2Image);
if(Screen) SDL_FreeSurface(Screen);
}
//原点坐标
int startposX = 0, startposY = 0;
// x,y 要加载到表面的左上角坐标
int BlitSurface(SDL_Surface* des, SDL_Surface* src, int x, int y)
{
if(des == NULL || src ==NULL )
{
//if(debugcheck) printf("Couldn't BlitSurface with NULL surface: %s\n", SDL_GetError());
return 1;
}
SDL_Rect destRec;
destRec.x=x+startposX;
destRec.y=y+startposY;
destRec.w=src->w;
destRec.h=src->h;
SDL_BlitSurface(src, NULL, des, &destRec);
return 0;
}
//------------------Voice define------------------------
#include "SDL_mixer.h"
enum GamePlayVoice{
START, //游戏开始
GAMEOVER, //游戏结束
LEVELPASS, //关卡完成
BEEP, //飞机下降
FIRE, //开火
FIREPLAN, //击中飞机
PLANBOOM, //飞机爆炸
SHIPBOOM, //小船爆炸
};
#define WAVNUM 8
static Mix_Chunk *WavChunk[WAVNUM]; //播放音效数据,可以同时播放几个,因此用数组
static Mix_Music* WavMusic[WAVNUM];
int g_EnableSound = 0;
int g_SoundVolume = 128;
//初始化声音资源
void initAudio()
{
printf("enrty initAudio\n");
int r;
r=SDL_InitSubSystem(SDL_INIT_AUDIO);
if(r<0)
{
g_EnableSound=0;
printf("Couldn't SDL_INIT_AUDIO: %s\n", SDL_GetError());
}
else
{
for(int i=0;i<WAVNUM;i++)
{
WavChunk[i] = NULL;
WavMusic[i] = NULL;
}
r = Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096);
if( r < 0 ) {
printf("Couldn't initialize SDL_Mixer: %s\n", SDL_GetError());
g_EnableSound=0;
}
else
{
SDL_ClearError();
g_EnableSound=1;
Mix_VolumeMusic(g_SoundVolume);
WavMusic[START] = Mix_LoadMUS("START.wav");
WavMusic[GAMEOVER] = Mix_LoadMUS("GAMEOVER.wav");
WavMusic[LEVELPASS] = Mix_LoadMUS("LEVELPASS.wav");
/* WavChunk[START]= Mix_LoadWAV("START.wav");
if(WavChunk[START]) Mix_VolumeChunk(WavChunk[START],g_SoundVolume);
WavChunk[GAMEOVER]= Mix_LoadWAV("GAMEOVER.wav");
if(WavChunk[GAMEOVER]) Mix_VolumeChunk(WavChunk[GAMEOVER],g_SoundVolume);
WavChunk[LEVELPASS]= Mix_LoadWAV("LEVELPASS.wav");
if(WavChunk[LEVELPASS]) Mix_VolumeChunk(WavChunk[LEVELPASS],g_SoundVolume);
*/
WavChunk[BEEP]= Mix_LoadWAV("BEEP.wav");
if(WavChunk[BEEP]) Mix_VolumeChunk(WavChunk[BEEP],g_SoundVolume);
WavChunk[FIRE]= Mix_LoadWAV("FIRE.wav");
if(WavChunk[FIRE]) Mix_VolumeChunk(WavChunk[FIRE],g_SoundVolume);
WavChunk[FIREPLAN]= Mix_LoadWAV("FIREPLAN.wav");
if(WavChunk[FIREPLAN]) Mix_VolumeChunk(WavChunk[FIREPLAN],g_SoundVolume);
WavChunk[PLANBOOM]= Mix_LoadWAV("PLANBOOM.wav");
if(WavChunk[PLANBOOM]) Mix_VolumeChunk(WavChunk[PLANBOOM],g_SoundVolume);
WavChunk[SHIPBOOM]= Mix_LoadWAV("SHIPBOOM.wav");
if(WavChunk[SHIPBOOM]) Mix_VolumeChunk(WavChunk[SHIPBOOM],g_SoundVolume);
printf("Couldn't Mix_VolumeChunk: %s\n", SDL_GetError());
}
}
}
//卸载音效资源
void unloadAudio()
{
for(int i=0;i<WAVNUM;i++){
if(WavChunk[i]){
Mix_FreeChunk(WavChunk[i]);
WavChunk[i]=NULL;
}
if(WavMusic[i])
{
Mix_FreeMusic(WavMusic[i]);
WavMusic[i] = NULL;
}
}
Mix_CloseAudio();
}
bool audioSwitch = true;
//播放音乐
int PlayMusicAudio(int pos, bool stopcurrent)
{
if(!audioSwitch) return 0;
if(g_EnableSound==0 || pos >= WAVNUM || WavMusic[pos] == NULL)
{
printf("Couldn't Play Music \n");
return 1;
}
if(Mix_PlayingMusic())
{
if(stopcurrent)
{
Mix_HaltMusic();
Mix_PlayMusic(WavMusic[pos], 1);
}
else
{
return 0;
}
}
else
{
Mix_PlayMusic(WavMusic[pos], 1);
}
return 0;
}
//播放音效
int PlayWAVAudio(int pos, int len)
{
if(!audioSwitch) return 0;
if(g_EnableSound==0 || pos >= WAVNUM || WavChunk[pos] == NULL)
{
printf("Couldn't MPlayWAVAudio \n");
return 1;
}
if(len<=0)
Mix_PlayChannel(-1, WavChunk[pos], 0); //播放音效
else
Mix_PlayChannelTimed(-1, WavChunk[pos], 0, len);
return 0;
}
//------------------Voice define End----------------------------------------
/* Binary elements (pressed or not) that are shown on the display. */
enum Element {
ELEMENT_DPAD_UP,
ELEMENT_DPAD_DOWN,
ELEMENT_DPAD_LEFT,
ELEMENT_DPAD_RIGHT,
ELEMENT_Y,
ELEMENT_B,
ELEMENT_X,
ELEMENT_A,
ELEMENT_SELECT,
ELEMENT_START,
ELEMENT_L1,
ELEMENT_R1,
ELEMENT_L2,
ELEMENT_R2,
ELEMENT_L3,
ELEMENT_R3,
ELEMENT_POWER,
#ifndef SDL_1
ELEMENT_VOLDOWN,
ELEMENT_VOLUP,
#endif
ELEMENT_COUNT
};
/* These define the keys that are not covered by JS 0, or used when the
* GCW Zero's buttons are not being mapped to JS 0. */
#ifdef SDL_1
SDLKey KeysHavingElements[] = {
SDLK_LEFT,
SDLK_RIGHT,
SDLK_UP,
SDLK_DOWN,
SDLK_LCTRL,
SDLK_LALT,
SDLK_SPACE,
SDLK_LSHIFT,
SDLK_TAB,
SDLK_BACKSPACE,
SDLK_PAGEUP,
SDLK_PAGEDOWN,
SDLK_KP_DIVIDE,
SDLK_KP_PERIOD, //小键盘的.
SDLK_ESCAPE,
SDLK_RETURN,
SDLK_HOME,
/*
SDLK_VOLUMEDOWN,
SDLK_VOLUMEUP,
*/
};
#else
SDL_Scancode KeysHavingElements[] = {
SDL_SCANCODE_LEFT,
SDL_SCANCODE_RIGHT,
SDL_SCANCODE_UP,
SDL_SCANCODE_DOWN,
SDL_SCANCODE_LCTRL,
SDL_SCANCODE_LALT,
SDL_SCANCODE_SPACE,
SDL_SCANCODE_LSHIFT,
SDL_SCANCODE_TAB, //R1
SDL_SCANCODE_BACKSPACE,
SDL_SCANCODE_PAGEUP, //R2
SDL_SCANCODE_PAGEDOWN,
SDL_SCANCODE_KP_DIVIDE,
SDL_SCANCODE_KP_PERIOD,
SDL_SCANCODE_ESCAPE,
SDL_SCANCODE_RETURN,
SDL_SCANCODE_HOME,
SDL_SCANCODE_VOLUMEDOWN,
SDL_SCANCODE_VOLUMEUP
};
#endif
/* These define the elements that should be lit up by pressing the above keys,
* ordered in the same way as in KeysHavingElements. */
enum Element KeysToElements[] = {
ELEMENT_DPAD_LEFT,
ELEMENT_DPAD_RIGHT,
ELEMENT_DPAD_UP,
ELEMENT_DPAD_DOWN,
ELEMENT_A,
ELEMENT_B,
ELEMENT_X,
ELEMENT_Y,
ELEMENT_L1,
ELEMENT_R1,
ELEMENT_L2,
ELEMENT_R2,
ELEMENT_L3,
ELEMENT_R3,
ELEMENT_SELECT,
ELEMENT_START,
ELEMENT_POWER,
#ifndef SDL_1
ELEMENT_VOLDOWN,
ELEMENT_VOLUP,
#endif
};
/* These define the elements that should be lit up by pressing JS 0's
* buttons. */
enum Element JoyButtonsToElements[] = {
ELEMENT_B,
ELEMENT_A,
ELEMENT_X,
ELEMENT_Y,
ELEMENT_L1,
ELEMENT_R1,
ELEMENT_L2,
ELEMENT_R2,
ELEMENT_DPAD_UP,
ELEMENT_DPAD_DOWN,
ELEMENT_DPAD_LEFT,
ELEMENT_DPAD_RIGHT,
ELEMENT_L3,
ELEMENT_R3,
ELEMENT_SELECT,
ELEMENT_START,
};
const char* ElementNames[] = {
"D-pad Up",
"D-pad Down",
"D-pad Left",
"D-pad Right",
"Y",
"B",
"X",
"A",
"Select",
"Start",
"L1",
"R1",
"L2",
"R2",
"L3",
"R3",
"Power",
#ifndef SDL_1
"Vol-Down",
"Vol-Up",
#endif
};
/* Last readings for all the elements and axes. */
bool ElementPressed[ELEMENT_COUNT];
bool ElementEverPressed[ELEMENT_COUNT];
bool DPadOppositeEverPressed = false;
int16_t BuiltInJS_X = 0;
int16_t BuiltInJS_Y = 0;
int16_t BuiltInJS_X2 = 0;
int16_t BuiltInJS_Y2 = 0;
struct DrawnElement {
SDL_Rect Rect;
const SDL_Color* ColorPressed;
const SDL_Color* ColorEverPressed;
};
bool HapticActive = false;
/* - - - CUSTOMISATION - - - */
#define FONT_FILE "font.ttf"
#define FONT_SIZE 12
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define INNER_SCREEN_X 84
#define INNER_SCREEN_Y 18
#define INNER_SCREEN_W 154
#define INNER_SCREEN_H 117
#define GCW_ZERO_PIC_Y 43
#define TEXT_CROSS_LX 4
#define TEXT_CROSS_Y 4
#define TEXT_ANALOG_CX 160
#define TEXT_ANALOG_Y 4
#define TEXT_GRAVITY_CX 160
#define TEXT_GRAVITY_Y 20
#define TEXT_OTHERS_RX 316
#define TEXT_OTHERS_Y 4
#define TEXT_FACE_RX 316
#define TEXT_FACE_Y 4
#define TEXT_CROSS_ERR_LX 4
#define TEXT_CROSS_ERR_Y 188
#define TEXT_EXIT_RX 316
#define TEXT_EXIT_Y 220
#define TEXT_RUMBLE_RX 316
#define TEXT_RUMBLE_Y 204
// R G B A
const SDL_Color ColorBackground = { 0, 0, 0, 255 };
const SDL_Color ColorEmptycolor = { 0, 0, 0, 0 };
const SDL_Color ColorBorder = { 255, 255, 255, 255 };
const SDL_Color ColorInnerBorder = { 128, 128, 128, 255 };
const SDL_Color ColorError = { 255, 32, 32, 255 };
const SDL_Color ColorPrompt = { 255, 255, 255, 255 };
const SDL_Color ColorNeverPressed = { 32, 32, 32, 255 };
const SDL_Color ColorCross = { 255, 255, 32, 255 };
const SDL_Color ColorAnalog = { 32, 255, 32, 255 };
const SDL_Color ColorAnalog2 = { 255, 128, 32, 255 };
const SDL_Color ColorFace = { 32, 32, 255, 255 };
const SDL_Color ColorOthers = { 255, 32, 255, 255 };
const SDL_Color ColorEverCross = { 64, 64, 32, 255 };
const SDL_Color ColorEverAnalog = { 32, 64, 32, 255 };
const SDL_Color ColorEverAnalog2 = { 64, 48, 32, 255 };
const SDL_Color ColorEverFace = { 32, 32, 64, 255 };
const SDL_Color ColorEverOthers = { 64, 32, 64, 255 };
bool MustExit(void)
{
// Start+Select allows exiting this application.
return ElementEverPressed[ELEMENT_SELECT] && ElementEverPressed[ELEMENT_START];
}
#ifdef SDL_1
# define JOYSTICK_NAME(Index) SDL_JoystickName(Index)
# define JOYSTICK_INDEX(Joystick) SDL_JoystickIndex(Joystick)
# define SDL_COLOR(Source) SDL_MapRGB(Screen->format, (Source).r, (Source).g, (Source).b)
# define MAKE_RASTER(Surface) Surface
# define PRESENT() SDL_Flip(Screen);
#else
# define JOYSTICK_NAME(Index) SDL_JoystickNameForIndex(Index)
# define JOYSTICK_INDEX(Joystick) SDL_JoystickInstanceID(Joystick)
# define SDL_COLOR(Source) (Source).r, (Source).g, (Source).b, (Source).a
# define PRESENT() SDL_UpdateWindowSurface(mainwindows);
#endif
//-----------------------------------------GameLogic---------------------------------------------
//当前模式 0-初始化 1-游戏中炮台模式 2-游戏中直升机模式
int currentmode = 0;
//输入模式 0-标准方向模式,1- 经典模式
int inputmode= 0;
//定义每关的延迟周期每秒60个周期,暂定12关 发现 40 25 60 有人过了第一关,所以降低难度作为终极关的参考参数
//110,100,90, 80, 70, 60, 50, 40,30,20, 10 ,5 //飞机下降延迟
// 90 ,80,70, 60, 50, 40, 30, 25,20,15, 6 ,3 //飞机出现延迟
//360,300,240,200,160,120,90, 60,40,30 ,20 ,10//小船前进延迟
int dashDelay[]= {0,110,100,90, 80, 70, 60, 50, 48,45,43,40,38};//飞机下降延迟
int newPlanDelay[]= {0,90 ,80 ,70, 60, 50, 40, 30, 28,26,24,22,20};//飞机出现延迟
int shipDelay[]= {0,360,300,240,200,160,120,90, 80,70,65,60,55};//小船前进延迟
int levelPlanCount[] ={0,20, 22, 25, 30, 36, 45, 60, 64,70,75,81,90}; //每阶段总飞机数量
int shootPlanDelay = 40;//被击中飞机在屏幕上显示延迟
int demoDelay = 150;//demo演示在屏幕上显示延迟
int fireDely = 30; //
int currentLife = 3; //生命数
int SCORE = 0; //当前分数
int HiSCORE = 0; //当前最高分数
int currentlevel = 1; //关卡级别
int startlevel = 1; //开始关卡级别
int HiLevel= 1; //当前最高关卡级别
int currentstage = 1; //关卡阶段
int HiStage = 1; //当前最高关卡阶段
int currentTick = 300; //当前计数周期
int planbound = 5; //击中飞机基本奖励,最终奖励乘以关卡级别
int shipbound = 8; //每推动小船一步算一次的最终奖励乘以关卡级别
int plancount;//每阶段剩余未击落飞机数量
bool suspentGame = false; //暂停游戏
//所有元素定义结构
struct ElementInfo {
int Pos; //当前标识0-表明没有使用
SDL_Surface** imageinfo; //元素图片
int posX, posY; //贴图位置
int delaytime; //当前持续周期,变为0就需要移动或者消失了
};
//飞机元素第0行不处理,Pos说明:0 不显示 每行代表一列,只有最后一行需要第6个位置 其中第四个位置固定45度图(planmap2), 第五个位置固定爆炸图
struct ElementInfo MainPlanInfo[5][6]={{{0,NULL,0,0,0}, {0,NULL,0,0,0},{0,NULL,0,0,0},{0,NULL,0,0,0},{0,NULL,0,0,0},{0,NULL,0,0,0}},
{{0,&planmap5,220,0,0}, {0,&planmap2,224,100,0},{0,&planmap2,140,180,0},{0,&planmap3,100,260,0},{0,&planboom,122,300,0},{0,&emptyImgae,0,0,0}},
{{0,&planmap2,340,0,0}, {0,&planmap4,332,100,0},{0,&planmap2,320,180,0},{0,&planmap3,260,260,0},{0,&planboom,280,300,0},{0,&emptyImgae,0,0,0}},
{{0,&planmap2,470,0,0}, {0,&planmap3,430,100,0},{0,&planmap5,460,180,0},{0,&planmap3,400,260,0},{0,&planboom,420,300,0},{0,&emptyImgae,0,0,0}},
{{0,&planmap2,570,0,0}, {0,&planmap6,530,100,0},{0,&planmap2,570,180,0},{0,&planmap3,530,260,0},{0,&planmap5,560,330,0},{0,&planmap6,586,400,0}}};
//自由飞机
//struct ElementInfo MainFreePlanInfo[]={{0,NULL,0}, {0,NULL,0},{0,NULL,0},{0,NULL,0},{0,NULL,0},{0,NULL,0}};
struct surfacePos{
int posX, posY;
};
struct surfacePos digitalPos = {10,40};
struct surfacePos scoremapPos = {10, 108};
struct surfacePos levelmapPos = {148,108};
//小船Pos说明:0 不显示
struct ElementInfo Mainshipinfo = {0,&shipmap,0,0,0};
struct surfacePos MainShipPos[]={{0,0},{80,420},{230,430},{380,430},{530,430}};
int shipflashValue = 15; //小船闪烁时间间隔
//炮台Pos说明:0-不显示 1 2 3 3 代表1234位置, 具体图片由逻辑进行提换
struct ElementInfo MainBatteryInfo={0,&batterymap,0,0,0};
struct surfacePos MainBatteryPos[]={{0,0},{70,292},{230,292},{370,292},{500,292}};
//直升机Pos说明:0-不显示
struct ElementInfo MainHelicopterInfo={0,&helicopter,0,0,0};
struct surfacePos MainHelicopterPos[]={{0,0},{80,380},{230,380},{380,380},{530,380}};
int priviousPlan=0; //上次新飞机位置 下面的表用来防止连续同一个位置出飞机
int planPosMap33[5][2]={{0,0},{2,3},{3,1},{1,2},{1,2}};
int planPosMap44[5][3]={{0,0},{2,3,4},{3,4,1},{4,1,2},{1,2,3}};
bool needrefresh = false;
//播放飞机下降音乐
bool PlayBEEP = false;
bool PlayFire = false;
bool PlayFirePlan = false;
//没有移动过救船不加分
bool batteryMoved = false;
int showScore = 0;
int demoPlanCount = 0;
#include <time.h>
//演示刷新
void demorefresh()
{
if(currentTick>demoDelay)
{
demoPlanCount = 0;
//srand((unsigned)time(NULL));
for(int i = 1; i<5; i++)
for(int j=0; j<6; j++)
{
MainPlanInfo[i][j].Pos = rand()%2;
if(MainPlanInfo[i][j].Pos) demoPlanCount++;
}
Mainshipinfo.Pos = rand()%5;
if(Mainshipinfo.Pos==1)
Mainshipinfo.imageinfo = &shipboom;
else
Mainshipinfo.imageinfo = &shipmap;
MainBatteryInfo.Pos = rand()%5;
MainHelicopterInfo.Pos = rand()%5;
currentLife = rand()%4;
showScore++;
if(showScore>2) showScore = 0; //rand()%2;
currentTick = 0;
needrefresh = true;
//PlayWAVAudio(LEVELPASS, 120*15-10);
PlayMusicAudio(LEVELPASS, false);
if(debugcheck) printf("demorefresh waite game start \n");
}
currentTick++;
}
void createValueSurface(int pre, int post)
{
//digitialvalue DigtialNumber
//char value[5];
int value=10000;
int realv =0;
if(post == 0) //输出数字
{
if(pre>99999) pre = 99999;
for (int i = 0; i<5; i++)
{
realv = pre/value;
pre = pre - realv*value;
value /=10;
BlitSurface(digitialvalue, DigtialNumber[realv], i*40, 0);
}
}
else //输出关卡
{
if(pre>99) pre =99;
if(post>99) post = 99;
value = pre/10;
realv = post/10;
BlitSurface(digitialvalue, DigtialNumber[value], 0, 0);
BlitSurface(digitialvalue, DigtialNumber[pre-value*10], 40, 0);
BlitSurface(digitialvalue, DigtialNumber[10], 80, 0);
BlitSurface(digitialvalue, DigtialNumber[realv], 120, 0);
BlitSurface(digitialvalue, DigtialNumber[post-realv*10], 160, 0);
}
}
//刷新云彩动画
bool hidePlan = false;
int currentCloudPos = 0;
void refreshCloud()
{
return;
currentCloudPos++;
hidePlan = rand()%2;
BlitSurface(RGBSurface, Cloud1Image, currentCloudPos%640, 10);
BlitSurface(RGBSurface, Cloud2Image, (currentCloudPos+200)%640, 10);
if(currentCloudPos >640) currentCloudPos = 0;
}
//刷新整个屏幕
void mainRefresh()
{
if(!needrefresh) return;
if(debugcheck) printf("mainRefresh start \n");
//准备大背景图
//SDL_FillRect (Screen, NULL, SDL_COLOR(ColorEmptycolor));
SDL_BlitSurface(backgroundmap , NULL, RGBSurface, NULL);
if(hidePlan) refreshCloud();
int i, j;
//刷新飞机
for(i = 1; i<5; i++)
{
for(j=0; j<6; j++)
{
if(MainPlanInfo[i][j].Pos)
{
if(BlitSurface(RGBSurface, *(MainPlanInfo[i][j].imageinfo), MainPlanInfo[i][j].posX, MainPlanInfo[i][j].posY))
{
if(debugcheck) printf("Couldn't BlitSurface MainPlanInfo[%d][%d]\n",i, j);
}
}
}
}
if(!hidePlan) refreshCloud();
//炮台
if(MainBatteryInfo.Pos)
{
if(BlitSurface(RGBSurface, *(MainBatteryInfo.imageinfo), MainBatteryPos[MainBatteryInfo.Pos].posX, MainBatteryPos[MainBatteryInfo.Pos].posY))
if(debugcheck) printf("Couldn't BlitSurface MainBatteryInfo at %d\n",MainBatteryInfo.Pos);
}
//直升机
if(MainHelicopterInfo.Pos)
{
if(BlitSurface(RGBSurface, *(MainHelicopterInfo.imageinfo), MainHelicopterPos[MainHelicopterInfo.Pos].posX, MainHelicopterPos[MainHelicopterInfo.Pos].posY))
if(debugcheck) printf("Couldn't BlitSurface MainHelicopterInfo at %d\n",MainHelicopterInfo.Pos);
}
//小船
if(Mainshipinfo.Pos)
{
if(BlitSurface(RGBSurface, *(Mainshipinfo.imageinfo), MainShipPos[Mainshipinfo.Pos].posX, MainShipPos[Mainshipinfo.Pos].posY))
if(debugcheck) printf("Couldn't BlitSurface Mainshipinfo at %d\n",Mainshipinfo.Pos);
}
//标志
if(showScore>0)
{
BlitSurface(RGBSurface, scoremap, scoremapPos.posX, scoremapPos.posY);
if(showScore==1)
{
if(currentmode >0)
createValueSurface(SCORE,0);
else
createValueSurface(HiSCORE, 0);
}
else if(showScore==2)
{
BlitSurface(RGBSurface, levelmap, levelmapPos.posX, levelmapPos.posY);
createValueSurface(demoPlanCount, 0);
}
}
else
{
BlitSurface(RGBSurface, levelmap, levelmapPos.posX, levelmapPos.posY);
if(currentmode >0)
createValueSurface(currentlevel,currentstage);
else
createValueSurface(HiLevel,HiStage);
}
BlitSurface(RGBSurface, digitialvalue, digitalPos.posX, digitalPos.posY);