-
Notifications
You must be signed in to change notification settings - Fork 36
/
kys_engine.pas
2411 lines (2170 loc) · 73.3 KB
/
kys_engine.pas
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
unit kys_engine;
//{$MODE Delphi}
interface
uses
SysUtils,
{$IFDEF fpc}
LConvEncoding,
LCLType,
LCLIntf,
{$ENDIF}
{$IFDEF mswindows}
Windows,
{$ENDIF}
Math,
Dialogs,
SDL2_TTF,
SDL2_image,
SDL2,
bassmidi,
bass,
//ziputils,
//unzip,
kys_main,
kys_type,
mythoutput;
function EventFilter(p: pointer; e: PSDL_Event): longint; cdecl;
//音频子程
procedure InitialMusic;
procedure PlayMP3(MusicNum, times: integer; frombeginning: integer = 1); overload;
procedure PlayMP3(filename: PAnsiChar; times: integer); overload;
procedure StopMP3(frombeginning: integer = 1);
procedure PlaySoundE(SoundNum, times: integer); overload;
procedure PlaySoundE(SoundNum: integer); overload;
procedure PlaySoundE(SoundNum, times, x, y, z: integer); overload;
//procedure PlaySoundE(filename: PAnsiChar; times: integer); overload;
procedure PlaySoundA(SoundNum, times: integer);
//用于读取的子程
procedure ReadTiles;
function ReadFileToBuffer(p: PAnsiChar; filename: AnsiString; size, malloc: integer): PAnsiChar;
procedure FreeFileBuffer(var p: PAnsiChar);
function LoadIdxGrp(stridx, strgrp: AnsiString; var idxarray: TIntArray; var grparray: TByteArray): integer;
function LoadPNGTiles(path: AnsiString; var PNGIndexArray: TPNGIndexArray; var SurfaceArray: TSurfaceArray;
LoadPic: integer = 1): integer;
procedure LoadOnePNGTile(path: AnsiString; p: PAnsiChar; filenum: integer; var PNGIndex: TPNGIndex;
SurfacePointer: PPSDL_Surface; forceLoad: integer = 0);
function LoadSurfaceFromFile(filename: AnsiString): PSDL_Surface;
function LoadSurfaceFromMem(p: PAnsiChar; len: integer): PSDL_Surface;
//function LoadSurfaceFromZIPFile(zipFile: unzFile; filename: AnsiString): PSDL_Surface;
procedure FreeAllSurface;
//基本绘图子程
function GetPixel(surface: PSDL_Surface; x: integer; y: integer): uint32; inline;
procedure PutPixel(surface: PSDL_Surface; x: integer; y: integer; pixel: uint32); inline;
procedure display_bmp(file_name: PAnsiChar; x, y: integer);
procedure display_img(file_name: PAnsiChar; x, y: integer);
function ColColor(num: byte): uint32; inline;
procedure DrawRectangle(sur: PSDL_Surface; x, y, w, h: integer; colorin, colorframe: uint32; alpha: integer);
procedure DrawRectangleWithoutFrame(sur: PSDL_Surface; x, y, w, h: integer; colorin: uint32; alpha: integer);
//画RLE8图片的子程
function JudgeInScreen(px, py, w, h, xs, ys: integer): boolean; overload; inline;
function JudgeInScreen(px, py, w, h, xs, ys, xx, yy, xw, yh: integer): boolean; overload; inline;
procedure DrawRLE8Pic(colorPanel: PAnsiChar; num, px, py: integer; Pidx: Pinteger; Ppic: PByte;
RectArea: PAnsiChar; Image: PSDL_Surface; widthI, heightI, sizeI: integer; shadow: integer); overload; inline;
procedure DrawRLE8Pic(colorPanel: PAnsiChar; num, px, py: integer; Pidx: Pinteger; Ppic: PByte;
RectArea: PAnsiChar; Image: PSDL_Surface; widthI, heightI, sizeI: integer; shadow, alpha: integer); overload; inline;
procedure DrawRLE8Pic(colorPanel: PAnsiChar; num, px, py: integer; Pidx: Pinteger; Ppic: PByte;
RectArea: PAnsiChar; Image: PSDL_Surface; widthI, heightI, sizeI: integer; shadow, alpha: integer;
BlockImageW: PAnsiChar; BlockPosition: PAnsiChar; widthW, heightW, sizeW: integer; depth: integer;
mixColor: uint32; mixAlpha: integer); overload;
function GetPositionOnScreen(x, y, CenterX, CenterY: integer): TPosition;
//显示文字的子程
function Big5ToUnicode(str: PAnsiChar; len: integer = -1): WideString;
function UnicodeToBig5(str: PWideChar): AnsiString;
procedure DrawText(sur: PSDL_Surface; word: puint16; x_pos, y_pos: integer; color: uint32);
procedure DrawEngText(sur: PSDL_Surface; word: puint16; x_pos, y_pos: integer; color: uint32);
procedure DrawShadowText(sur: PSDL_Surface; word: puint16; x_pos, y_pos: integer; color1, color2: uint32); overload;
procedure DrawShadowText(word: puint16; x_pos, y_pos: integer; color1, color2: uint32); overload;
procedure DrawEngShadowText(sur: PSDL_Surface; word: puint16; x_pos, y_pos: integer; color1, color2: uint32);
procedure DrawBig5Text(sur: PSDL_Surface; str: PAnsiChar; x_pos, y_pos: integer; color: uint32);
procedure DrawBig5ShadowText(sur: PSDL_Surface; word: PAnsiChar; x_pos, y_pos: integer; color1, color2: uint32);
procedure DrawTextWithRect(word: puint16; x, y, w: integer; color1, color2: uint32); overload;
procedure DrawTextWithRect(sur: PSDL_Surface; word: puint16; x, y, w: integer; color1, color2: uint32); overload;
//PNG贴图相关的子程
procedure DrawPNGTile(PNGIndex: TPNGIndex; FrameNum: integer; RectArea: PAnsiChar; scr: PSDL_Surface;
px, py: integer); overload;
procedure DrawPNGTile(PNGIndex: TPNGIndex; FrameNum: integer; RectArea: PAnsiChar; scr: PSDL_Surface;
px, py: integer; shadow, alpha: integer; mixColor: uint32; mixAlpha: integer); overload;
procedure DrawPNGTile(PNGIndex: TPNGIndex; FrameNum: integer; RectArea: PAnsiChar; scr: PSDL_Surface;
px, py: integer; shadow, alpha: integer; mixColor: uint32; mixAlpha: integer; depth: integer;
BlockImgR: PAnsiChar; Width, Height, size, leftupx, leftupy: integer); overload;
procedure SetPNGTileBlock(PNGIndex: TPNGIndex; px, py, depth: integer; BlockImageW: PAnsiChar;
Width, Height, size: integer);
//用于系统响应的子程
procedure ChangeCol;
procedure SDL_UpdateRect2(scr1: PSDL_Surface; x, y, w, h: integer);
procedure SDL_GetMouseState2(var x, y: integer);
procedure ResizeWindow(w, h: integer);
procedure SwitchFullscreen;
procedure QuitConfirm;
function CheckBasicEvent: uint32;
function AngleToDirection(y, x: real): integer;
function DrawLength(str: WideString): integer; overload;
function DrawLength(p: PWideChar): integer; overload;
function DrawLength(p: PAnsiChar): integer; overload;
function round(x: real): integer;
procedure swap(var x, y: uint32); overload;
procedure UpdateAllScreen;
procedure TransBlackScreen;
procedure CleanKeyValue;
procedure GetMousePosition(var x, y: integer; x0, y0: integer; yp: integer = 0);
function MouseInRegion(x, y, w, h: integer): boolean; overload;
function MouseInRegion(x, y, w, h: integer; var x1, y1: integer): boolean; overload;
function RegionParameter(x, x1, x2: integer): integer;
procedure QuickSortB(var a: array of TBuildInfo; l, r: integer);
//计时, 测速用
procedure tic;
procedure toc;
procedure Message(formatstring: AnsiString; content: array of const; cr: boolean = True); overload;
procedure Message(formatstring: string = ''; cr: boolean = True); overload;
implementation
uses
kys_draw;
function EventFilter(p: pointer; e: PSDL_Event): longint; cdecl;
begin
Result := 1;
{or (e.type_ = SDL_FINGERMOTION)}
case e.type_ of
SDL_FINGERUP, SDL_FINGERDOWN, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP:
Result := 0;
SDL_FINGERMOTION:
if CellPhone = 0 then
Result := 0;
end;
end;
procedure InitialMusic;
var
i: integer;
str: AnsiString;
sf: BASS_MIDI_FONT;
Flag: longword;
begin
BASS_Set3DFactors(1, 0, 0);
sf.font := BASS_MIDI_FontInit(PAnsiChar(AppPath + 'music/mid.sf2'), 0);
BASS_MIDI_StreamSetFonts(0, sf, 1);
sf.preset := -1; // use all presets
sf.bank := 0;
Flag := 0;
if SOUND3D = 1 then
Flag := BASS_SAMPLE_3D or Flag;
for i := low(Music) to high(Music) do
begin
str := AppPath + 'music/' + IntToStr(i) + '.mp3';
if FileExists(PAnsiChar(str)) then
begin
try
Music[i] := BASS_StreamCreateFile(False, PAnsiChar(str), 0, 0, 0);
finally
end;
end
else
begin
str := AppPath + 'music/' + IntToStr(i) + '.mid';
if FileExists(PAnsiChar(str)) then
begin
try
Music[i] := BASS_MIDI_StreamCreateFile(False, PAnsiChar(str), 0, 0, 0, 0);
BASS_MIDI_StreamSetFonts(Music[i], sf, 1);
//showmessage(inttostr(Music[i]));
finally
end;
end
else
Music[i] := 0;
end;
end;
for i := low(ESound) to high(ESound) do
begin
str := AppPath + formatfloat('sound/e00', i) + '.wav';
if FileExists(PAnsiChar(str)) then
ESound[i] := BASS_SampleLoad(False, PAnsiChar(str), 0, 0, 1, Flag)
else
ESound[i] := 0;
//showmessage(inttostr(esound[i]));
end;
for i := low(ASound) to high(ASound) do
begin
str := AppPath + formatfloat('sound/atk00', i) + '.wav';
if FileExists(PAnsiChar(str)) then
ASound[i] := BASS_SampleLoad(False, PAnsiChar(str), 0, 0, 1, Flag)
else
ASound[i] := 0;
end;
end;
//播放mp3音乐
procedure PlayMP3(MusicNum, times: integer; frombeginning: integer = 1); overload;
var
repeatable: boolean;
begin
if times = -1 then
repeatable := True
else
repeatable := False;
try
if (MusicNum >= Low(Music)) and (MusicNum <= High(Music)) and (VOLUME > 0) then
if Music[MusicNum] <> 0 then
begin
//BASS_ChannelSlideAttribute(Music[nowmusic], BASS_ATTRIB_VOL, 0, 1000);
BASS_ChannelStop(Music[nowmusic]);
if frombeginning = 1 then
BASS_ChannelSetPosition(Music[nowmusic], 0, BASS_POS_BYTE);
BASS_ChannelSetAttribute(Music[MusicNum], BASS_ATTRIB_VOL, VOLUME / 100.0);
if SOUND3D = 1 then
begin
//BASS_SetEAXParameters(EAX_ENVIRONMENT_UNDERWATER, -1, 0, 0);
BASS_Apply3D();
end;
if repeatable then
BASS_ChannelFlags(Music[MusicNum], BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP)
else
BASS_ChannelFlags(Music[MusicNum], 0, BASS_SAMPLE_LOOP);
BASS_ChannelPlay(Music[MusicNum], False);
nowmusic := musicnum;
end;
finally
end;
end;
procedure PlayMP3(filename: PAnsiChar; times: integer); overload;
begin
//if fileexists(filename) then
//begin
//Music := Mix_LoadMUS(filename);
//Mix_volumemusic(MIX_MAX_VOLUME div 3);
//Mix_PlayMusic(music, times);
//end;
end;
//停止当前播放的音乐
procedure StopMP3(frombeginning: integer = 1);
begin
BASS_ChannelStop(Music[nowmusic]);
if frombeginning = 1 then
BASS_ChannelSetPosition(Music[nowmusic], 0, BASS_POS_BYTE);
end;
//播放wav音效
procedure PlaySoundE(SoundNum, times: integer); overload;
var
ch: HCHANNEL;
repeatable: boolean;
begin
if times = -1 then
repeatable := True
else
repeatable := False;
if (SoundNum >= Low(Esound)) and (SoundNum <= High(Esound)) and (VOLUME > 0) then
if Esound[SoundNum] <> 0 then
begin
//Mix_VolumeChunk(Esound[SoundNum], Volume);
//Mix_PlayChannel(-1, Esound[SoundNum], 0);
BASS_SampleStop(Esound[soundnum]);
ch := BASS_SampleGetChannel(Esound[soundnum], False);
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, VOLUMEWAV / 100.0);
if repeatable then
BASS_ChannelFlags(ch, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP)
else
BASS_ChannelFlags(ch, 0, BASS_SAMPLE_LOOP);
BASS_ChannelPlay(ch, repeatable);
end;
end;
procedure PlaySoundE(SoundNum: integer); overload;
begin
PlaySoundE(Soundnum, 0);
end;
procedure PlaySoundE(SoundNum, times, x, y, z: integer); overload;
var
ch: HCHANNEL;
repeatable: boolean;
pos, posvec, posvel: BASS_3DVECTOR;
//音源的位置, 向量, 速度
//p: PSource;
begin
if times = -1 then
repeatable := True
else
repeatable := False;
if (SoundNum >= Low(Esound)) and (SoundNum <= High(Esound)) and (VOLUMEWAV > 0) then
if Esound[SoundNum] <> 0 then
begin
//Mix_VolumeChunk(Esound[SoundNum], Volume);
//Mix_PlayChannel(-1, Esound[SoundNum], 0);
BASS_SampleStop(Esound[soundnum]);
ch := BASS_SampleGetChannel(Esound[soundnum], False);
//BASS_ChannelSet3DAttributes(ch, BASS_3DMODE_RELATIVE, -1, -1, -1, -1, -1);
if ch = 0 then
ShowMessage(IntToStr(BASS_ErrorGetCode));
if SOUND3D = 1 then
begin
pos.x := x * 100;
pos.y := y * 100;
pos.z := z * 100;
posvec.x := x;
posvec.y := y;
posvec.z := z;
posvel.x := -x * 100;
posvel.y := -y * 100;
posvel.z := -z * 100;
BASS_ChannelSet3DPosition(ch, pos, posvec, posvel);
BASS_Apply3D();
end;
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, VOLUMEWAV / 100.0);
if repeatable then
BASS_ChannelFlags(ch, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP)
else
BASS_ChannelFlags(ch, 0, BASS_SAMPLE_LOOP);
BASS_ChannelPlay(ch, repeatable);
//BASS_Apply3D();
end;
end;
procedure PlaySoundA(SoundNum, times: integer);
var
ch: HCHANNEL;
repeatable: boolean;
begin
if times = -1 then
repeatable := True
else
repeatable := False;
if (SoundNum >= Low(Asound)) and (SoundNum <= High(Asound)) and (VOLUMEWAV > 0) then
if Asound[SoundNum] <> 0 then
begin
//Mix_VolumeChunk(Esound[SoundNum], Volume);
//Mix_PlayChannel(-1, Esound[SoundNum], 0);
BASS_SampleStop(Esound[soundnum]);
ch := BASS_SampleGetChannel(Asound[soundnum], False);
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, VOLUMEWAV / 100.0);
if repeatable then
BASS_ChannelFlags(ch, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP)
else
BASS_ChannelFlags(ch, 0, BASS_SAMPLE_LOOP);
BASS_ChannelPlay(ch, repeatable);
end;
end;
{procedure InitialMusic;
var
i: integer;
str: AnsiString;
begin
for i := 0 to 23 do
begin
str := AppPath + 'music/' + inttostr(i) + '.mid';
if FileExists(PAnsiChar(str)) then
begin
Music[i] := Mix_LoadMUS(PAnsiChar(str));
end
else
Music[i] := nil;
end;
for i := 0 to 52 do
begin
str := AppPath + formatfloat('sound/e00', i) + '.wav';
if FileExists(PAnsiChar(str)) then
ESound[i] := Mix_LoadWav(PAnsiChar(str))
else
ESound[i] := nil;
end;
for i := 0 to 24 do
begin
str := AppPath + formatfloat('sound/atk00', i) + '.wav';
if FileExists(PAnsiChar(str)) then
ASound[i] := Mix_LoadWav(PAnsiChar(str))
else
ASound[i] := nil;
end;
end;
//播放mp3音乐
procedure PlayMP3(MusicNum, times: integer); overload;
begin
if MusicNum in [Low(Music)..High(Music)] then
begin
if Music[MusicNum] <> nil then
begin
Mix_PlayMusic(Music[MusicNum], times);
end;
end;
end;
procedure PlayMP3(filename: PAnsiChar; times: integer); overload;
begin
//if fileexists(filename) then
//begin
//Music := Mix_LoadMUS(filename);
//Mix_volumemusic(MIX_MAX_VOLUME div 3);
//Mix_PlayMusic(music, times);
//end;
end;
//停止当前播放的音乐
procedure StopMP3;
begin
Mix_HaltMusic;
end;
//播放eft音效
procedure PlaySoundE(SoundNum, times: integer); overload;
begin
if SoundNum in [Low(Esound)..High(Esound)] then
if Esound[SoundNum] <> nil then
Mix_PlayChannel(-1, Esound[SoundNum], times);
end;
procedure PlaySoundE(SoundNum: integer); overload;
begin
if SoundNum in [Low(Esound)..High(Esound)] then
if Esound[SoundNum] <> nil then
Mix_PlayChannel(-1, Esound[SoundNum], 0);
end;
procedure PlaySoundE(filename: PAnsiChar; times: integer); overload;
begin
if fileexists(filename) then
begin
Sound := Mix_LoadWav(filename);
Mix_PlayChannel(-1, sound, times);
end;
end;
//播放atk音效
procedure PlaySoundA(SoundNum, times: integer);
begin
if SoundNum in [Low(ASound)..High(ASound)] then
if ASound[SoundNum] <> nil then
Mix_PlayChannel(-1, ASound[SoundNum], times);
end;}
procedure ReadTiles;
var
i: integer;
begin
if PNG_TILE = 0 then
begin
if IsConsole then
writeln('Reading idx and grp files...');
MPicAmount := LoadIdxGrp('resource/mmap.idx', 'resource/mmap.grp', MIdx, MPic);
SPicAmount := LoadIdxGrp('resource/sdx', 'resource/smp', SIdx, SPic);
BPicAmount := LoadIdxGrp('resource/wdx', 'resource/wmp', WIdx, WPic);
EPicAmount := LoadIdxGrp('resource/eft.idx', 'resource/eft.grp', EIdx, EPic);
//LoadIdxGrp('resource/hdgrp.idx', 'resource/hdgrp.grp', HIdx, HPic);
CPicAmount := LoadIdxGrp('resource/cloud.idx', 'resource/cloud.grp', CIdx, CPic);
HPicAmount := LoadIdxGrp('resource/hdgrp.idx', 'resource/hdgrp.grp', HIdx, HPic);
end;
if PNG_TILE > 0 then
begin
MPicAmount := LoadPNGTiles('resource/mmap', MPNGIndex, MPNGTile, 1);
SPicAmount := LoadPNGTiles('resource/smap', SPNGIndex, SPNGTile, 1);
{for i := BeginScenceRolePic to BeginScenceRolePic + 27 do
LoadOnePNGTile('resource/smap', nil,i, SPNGIndex[i], @SPNGTile[0]);
for i := 3410 to 4102 do
LoadOnePNGTile('resource/smap', nil,i, SPNGIndex[i], @SPNGTile[0]);}
BPicAmount := LoadPNGTiles('resource/wmap', BPNGIndex, BPNGTile, 1);
EPicAmount := LoadPNGTiles('resource/eft', EPNGIndex, EPNGTile, 1);
CPicAmount := LoadPNGTiles('resource/cloud', CPNGIndex, CPNGTile, 1);
end;
if BIG_PNG_TILE > 0 then
begin
{MMapSurface := LoadSurfaceFromFile(AppPath + 'resource/bigpng/mmap.png');
if MMapSurface <> nil then
writeln('Main map loaded.');}
end;
end;
//读入文件到缓冲区
//当读入的位置并非变长数据时, 务必设置 malloc = 0!
//size小于0时, 则读整个文件.
function ReadFileToBuffer(p: PAnsiChar; filename: AnsiString; size, malloc: integer): PAnsiChar;
var
i: integer;
begin
i := FileOpen(filename, fmopenread);
if i > 0 then
begin
if size < 0 then
size := FileSeek(i, 0, 2);
if malloc = 1 then
begin
//GetMem(result, size + 4);
{$ifdef fpc}
Result := StrAlloc(size + 4);
{$else}
Result := AnsiStrAlloc(size + 4);
{$endif}
p := Result;
//writeln(StrBufSize(p));
end;
FileSeek(i, 0, 0);
FileRead(i, p^, size);
FileClose(i);
end
else
if malloc = 1 then
Result := nil;
end;
procedure FreeFileBuffer(var p: PAnsiChar);
begin
if p <> nil then
StrDispose(p);
p := nil;
end;
function LoadIdxGrp(stridx, strgrp: AnsiString; var idxarray: TIntArray; var grparray: TByteArray): integer;
var
idx, grp, len, tnum: integer;
begin
grp := FileOpen(AppPath + strgrp, fmopenread);
len := FileSeek(grp, 0, 2);
setlength(grparray, len + 4);
FileSeek(grp, 0, 0);
FileRead(grp, grparray[0], len);
FileClose(grp);
idx := FileOpen(AppPath + stridx, fmopenread);
tnum := FileSeek(idx, 0, 2) div 4;
setlength(idxarray, tnum + 1);
FileSeek(idx, 0, 0);
FileRead(idx, idxarray[0], tnum * 4);
FileClose(idx);
Result := tnum;
end;
//为了提高启动的速度, M之外的贴图均仅读入基本信息, 需要时才实际载入图, 并且游戏过程中通常不再释放资源
function LoadPNGTiles(path: AnsiString; var PNGIndexArray: TPNGIndexArray; var SurfaceArray: TSurfaceArray;
LoadPic: integer = 1): integer;
var
i, j, k, state, size, Count, pngoff: integer;
//zipFile: unzFile;
//info: unz_file_info;
offset: array of smallint;
p: PAnsiChar;
begin
//载入偏移值文件, 计算贴图的最大数量
size := 0;
Result := 0;
p := nil;
if PNG_TILE = 2 then
begin
if IsConsole then
writeln('Searching imz file... ', path);
p := ReadFileToBuffer(nil, AppPath + path + '.imz', -1, 1);
if p <> nil then
begin
Result := pinteger(p)^;
//最大的有帧数的数量作为贴图的最大编号
for i := Result - 1 downto 0 do
begin
if pinteger(p + pinteger(p + 4 + i * 4)^ + 4)^ > 0 then
begin
Result := i + 1;
break;
end;
end;
//初始化贴图索引, 并计算全部帧数和
setlength(PNGIndexArray, Result);
Count := 0;
for i := 0 to Result - 1 do
begin
pngoff := pinteger(p + 4 + i * 4)^;
with PNGIndexArray[i] do
begin
Num := Count;
x := psmallint(p + pngoff)^;
y := psmallint(p + pngoff + 2)^;
Frame := pinteger(p + pngoff + 4)^;
Count := Count + frame;
CurPointer := nil;
Loaded := 0;
end;
end;
end
else
if IsConsole then
writeln('Can''t find imz file.');
end;
if (PNG_TILE = 1) or (p = nil) then
begin
if IsConsole then
writeln('Searching index of png files... ', path + '/index.ka');
path := path + '/';
p := ReadFileToBuffer(nil, AppPath + path + '/index.ka', -1, 1);
size := StrBufSize(p);
setlength(offset, size div 2 + 2);
move(p^, offset[0], size);
FreeFileBuffer(p);
for i := size div 4 downto 0 do
begin
if FileExists(AppPath + path + IntToStr(i) + '.png') or FileExists(AppPath + path +
IntToStr(i) + '_0.png') then
begin
Result := i + 1;
break;
end;
end;
//贴图的数量是有文件存在的最大数量
setlength(PNGIndexArray, Result);
//计算合法贴图文件的总数, 同时指定每个图的索引数据
Count := 0;
for i := 0 to Result - 1 do
begin
with PNGIndexArray[i] do
begin
Num := -1;
Frame := 0;
CurPointer := nil;
if FileExists(AppPath + path + IntToStr(i) + '.png') then
begin
Num := Count;
Frame := 1;
Count := Count + 1;
end
else
begin
k := 0;
while FileExists(AppPath + path + IntToStr(i) + '_' + IntToStr(k) + '.png') do
begin
k := k + 1;
if k = 1 then
Num := Count;
Count := Count + 1;
end;
Frame := k;
end;
x := offset[i * 2];
y := offset[i * 2 + 1];
Loaded := 0;
UseGRP := 0;
end;
end;
end;
if IsConsole then
writeln(Result, ' index, ', Count, ' real titles. Now loading...');
setlength(SurfaceArray, Count);
for i := 0 to Count - 1 do
SurfaceArray[i] := nil;
if LoadPic = 1 then
begin
for i := 0 to Result - 1 do
begin
LoadOnePNGTile(path, p, i, PNGIndexArray[i], @SurfaceArray[0], 1);
end;
end;
FreeFileBuffer(p);
end;
procedure LoadOnePNGTile(path: AnsiString; p: PAnsiChar; filenum: integer; var PNGIndex: TPNGIndex;
SurfacePointer: PPSDL_Surface; forceLoad: integer = 0);
var
j, k, index, len, off: integer;
tempscr: PSDL_Surface;
frommem: boolean;
begin
SDL_PollEvent(@event);
CheckBasicEvent;
frommem := ((PNG_TILE = 2) and (p <> nil));
if not frommem then
path := path + '/';
with PNGIndex do
begin
if ((Loaded = 0) or (forceLoad = 1)) and (Num >= 0) and (Frame > 0) then
begin
Loaded := 1;
Inc(SurfacePointer, Num);
CurPointer := SurfacePointer;
if Frame = 1 then
begin
if frommem then
begin
off := pinteger(p + 4 + filenum * 4)^ + 8;
index := pinteger(p + off)^;
len := pinteger(p + off + 4)^;
SurfacePointer^ := LoadSurfaceFromMem(p + index, len);
end
else
SurfacePointer^ := LoadSurfaceFromFile(AppPath + path + IntToStr(filenum) + '.png');
if SurfacePointer^ = nil then
SurfacePointer^ := LoadSurfaceFromFile(AppPath + path + IntToStr(filenum) + '_0.png');
end;
if Frame > 1 then
begin
for j := 0 to Frame - 1 do
begin
if frommem then
begin
off := pinteger(p + 4 + filenum * 4)^ + 8;
index := pinteger(p + off + j * 8)^;
len := pinteger(p + off + j * 8 + 4)^;
SurfacePointer^ := LoadSurfaceFromMem(p + index, len);
end
else
SurfacePointer^ := LoadSurfaceFromFile(AppPath + path + IntToStr(filenum) + '_' + IntToStr(j) + '.png');
Inc(SurfacePointer, 1);
end;
end;
end;
end;
end;
function LoadSurfaceFromFile(filename: AnsiString): PSDL_Surface;
var
tempscr: PSDL_Surface;
begin
Result := nil;
if FileExists(filename) then
begin
tempscr := IMG_Load(PAnsiChar(filename));
Result := SDL_ConvertSurface(tempscr, screen.format, 0);
SDL_FreeSurface(tempscr);
end;
end;
function LoadSurfaceFromMem(p: PAnsiChar; len: integer): PSDL_Surface;
var
tempscr: PSDL_Surface;
tempRWops: PSDL_RWops;
begin
Result := nil;
tempRWops := SDL_RWFromMem(p, len);
tempscr := IMG_LoadPNG_RW(tempRWops);
Result := SDL_ConvertSurface(tempscr, screen.format, 0);
SDL_FreeSurface(tempscr);
SDL_FreeRW(tempRWops);
end;
{function LoadSurfaceFromZIPFile(zipFile: unzFile; filename: AnsiString): PSDL_Surface;
var
//archiver: unzFile;
//info: unz_file_info;
buffer: PAnsiChar;
begin
end;}
procedure FreeAllSurface;
var
i, j: integer;
begin
for i := 0 to high(MPNGTile) do
SDL_FreeSurface(MPNGTile[i]);
for i := 0 to high(SPNGTile) do
SDL_FreeSurface(SPNGTile[i]);
for i := 0 to high(BPNGTile) do
SDL_FreeSurface(BPNGTile[i]);
for i := 0 to high(EPNGTile) do
SDL_FreeSurface(EPNGTile[i]);
for i := 0 to high(CPNGTile) do
SDL_FreeSurface(CPNGTile[i]);
for i := 0 to high(TitlePNGTile) do
SDL_FreeSurface(TitlePNGTile[i]);
for i := 0 to high(FPNGTile) do
for j := 0 to high(FPNGTile[i]) do
SDL_FreeSurface(FPNGTile[i, j]);
SDL_FreeSurface(screen);
SDL_FreeSurface(prescreen);
SDL_FreeSurface(ImgScence);
SDL_FreeSurface(ImgScenceBack);
SDL_FreeSurface(ImgBField);
SDL_FreeSurface(ImgBBuild);
end;
//获取某像素信息
function GetPixel(surface: PSDL_Surface; x: integer; y: integer): uint32;
type
TByteArray = array[0..2] of byte;
PByteArray = ^TByteArray;
var
bpp: integer;
p: PInteger;
begin
if (x >= 0) and (x < surface.w) and (y >= 0) and (y < surface.h) then
begin
Result := puint32(NativeUInt(surface.pixels) + y * surface.pitch + x * 4)^;
{bpp := surface.format.BytesPerPixel;
// Here p is the address to the pixel we want to retrieve
p := Pointer(uint32(surface.pixels) + y * surface.pitch + x * bpp);
case bpp of
1:
Result := longword(p^);
2:
Result := puint16(p)^;
3:
if (SDL_BYTEORDER = SDL_BIG_ENDIAN) then
Result := PByteArray(p)[0] shl 16 or PByteArray(p)[1] shl 8 or PByteArray(p)[2]
else
Result := PByteArray(p)[0] or PByteArray(p)[1] shl 8 or PByteArray(p)[2] shl 16;
4:
Result := puint32(p)^;
else
Result := 0; // shouldn't happen, but avoids warnings
end;}
end;
end;
//画像素
procedure PutPixel(surface: PSDL_Surface; x: integer; y: integer; pixel: uint32);
type
TByteArray = array[0..2] of byte;
PByteArray = ^TByteArray;
var
bpp: integer;
p: PInteger;
begin
if (x >= 0) and (x < surface.w) and (y >= 0) and (y < surface.h) then
begin
puint32(NativeUInt(surface.pixels) + y * surface.pitch + x * 4)^ := pixel;
{bpp := surface.format.BytesPerPixel;
// Here p is the address to the pixel we want to set
p := Pointer(uint32(surface.pixels) + y * surface.pitch + x * bpp);
case bpp of
1:
longword(p^) := pixel;
2:
puint16(p)^ := pixel;
3:
if (SDL_BYTEORDER = SDL_BIG_ENDIAN) then
begin
PByteArray(p)[0] := (pixel shr 16) and $FF;
PByteArray(p)[1] := (pixel shr 8) and $FF;
PByteArray(p)[2] := pixel and $FF;
end
else
begin
PByteArray(p)[0] := pixel and $FF;
PByteArray(p)[1] := (pixel shr 8) and $FF;
PByteArray(p)[2] := (pixel shr 16) and $FF;
end;
4:
puint32(p)^ := pixel;
end;}
end;
end;
//显示bmp文件
procedure display_bmp(file_name: PAnsiChar; x, y: integer);
var
image: PSDL_Surface;
dest: TSDL_Rect;
begin
if FileExists(file_name) { *Converted from FileExists* } then
begin
image := SDL_LoadBMP(file_name);
if (image = nil) then
begin
//MessageBox(0, PAnsiChar(Format('Couldn''t load %s : %s', [file_name, SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
exit;
end;
dest.x := x;
dest.y := y;
//if (SDL_BlitSurface(image, nil, screen, @dest) < 0) then
// MessageBox(0, PAnsiChar(Format('BlitSurface error : %s', [SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
//SDL_UpdateRect2(screen, 0, 0, image.w, image.h);
SDL_FreeSurface(image);
end;
end;
//显示tif, png, jpg等格式图片
procedure display_img(file_name: PAnsiChar; x, y: integer);
var
image: PSDL_Surface;
dest: TSDL_Rect;
begin
if FileExists(file_name) { *Converted from FileExists* } then
begin
image := IMG_Load(file_name);
if (image = nil) then
begin
//MessageBox(0, PAnsiChar(Format('Couldn''t load %s : %s', [file_name, SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
exit;
end;
dest.x := x;
dest.y := y;
SDL_BlitSurface(image, nil, screen, @dest);
// MessageBox(0, PAnsiChar(Format('BlitSurface error : %s', [SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
//SDL_UpdateRect2(screen, 0, 0, image.w, image.h);
SDL_FreeSurface(image);
end;
end;
//取调色板的颜色, 视频系统为32位色, 但很多时候仍需要原调色板的颜色
function ColColor(num: byte): uint32;
begin
//{$IFDEF darwin}
//colcolor := SDL_mapRGB(screen.format, Acol[num * 3 + 0] * 4, Acol[num * 3 + 1] * 4, Acol[num * 3 + 2] * 4);
//{$ELSE}
//if (num >= 0) and (num <= 255) then
Result := SDL_MapRGB(screen.format, Acol[num * 3] * 4, Acol[num * 3 + 1] * 4, Acol[num * 3 + 2] * 4);
//else
//Result := 0;
//{$ENDIF}
end;
//判断像素是否在屏幕内
function JudgeInScreen(px, py, w, h, xs, ys: integer): boolean;
begin
Result := (px - xs + w >= 0) and (px - xs < screen.w) and (py - ys + h >= 0) and (py - ys < screen.h);
end;
//判断像素是否在指定范围内(重载)