-
Notifications
You must be signed in to change notification settings - Fork 0
/
kys_engine.pas
3964 lines (3620 loc) · 106 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;
{$i macro.inc}
interface
uses
{$IFDEF fpc}
LConvEncoding,
LCLType,
LCLIntf,
{$ENDIF}
{$IFDEF mswindows}
Windows,
//xVideo,
avcodec,
avformat,
swscale,
avutil,
{$ENDIF}
Classes,
SysUtils,
SDL2_TTF,
SDL2_image,
SDL2,
kys_type,
kys_main,
gl,
Dialogs,
bassmidi,
bass,
Math,
unzip,
ziputils;
function EventFilter(p: pointer; e: PSDL_Event): longint; cdecl;
//音频子程
procedure InitialMusic;
procedure FreeAllMusic;
procedure PlayMP3(MusicNum, times: integer; frombeginning: integer = 1); overload;
procedure PlayMP3(filename: pchar; times: integer); overload;
procedure StopMP3(frombeginning: integer = 1);
procedure PlaySound(SoundNum, times: integer); overload;
procedure PlaySound(SoundNum, times, x, y, z: integer); overload;
procedure PlaySoundA(SoundNum, times: integer); overload;
procedure PlaySound(SoundNum: integer); overload;
procedure PlaySound(filename: pchar; times: integer); overload;
//基本绘图子程
function GetPixel(surface: PSDL_Surface; x: integer; y: integer): uint32;
procedure PutPixel(surface: PSDL_Surface; x: integer; y: integer; pixel: uint32);
function ColColor(num: integer): uint32;
procedure DrawRectangle(x, y, w, h: integer; colorin, colorframe: uint32; alpha: integer);
procedure DrawRectangleWithoutFrame(x, y, w, h: integer; colorin: uint32; alpha: integer);
procedure DrawItemFrame(x, y: integer; realcoord: integer = 0);
//画RLE8图片的子程
function JudgeInScreen(px, py, w, h, xs, ys: integer): boolean; overload;
function JudgeInScreen(px, py, w, h, xs, ys, xx, yy, xw, yh: integer): boolean; overload;
function GetPositionOnScreen(x, y, CenterX, CenterY: integer): TPosition;
//显示文字的子程
function Big5ToUnicode(str: pchar): WideString;
function GBKToUnicode(str: pchar): WideString;
function PCharToUnicode(str: pchar; len: integer = -1): WideString;
function UnicodeToBig5(str: pWideChar): string;
function UnicodeToGBK(str: pWideChar): string;
procedure DrawText(word: puint16; x_pos, y_pos: integer; color: uint32; engwidth: integer = -1);
procedure DrawEngText(word: puint16; x_pos, y_pos: integer; color: uint32);
procedure DrawShadowText(word: puint16; x_pos, y_pos: integer; color1, color2: uint32;
Tex: PSDL_Texture = nil; Sur: PSDL_Surface = nil; realPosition: integer = 0; eng: integer = 0); overload;
procedure DrawEngShadowText(word: puint16; x_pos, y_pos: integer; color1, color2: uint32;
Tex: PSDL_Texture = nil; Sur: PSDL_Surface = nil);
procedure DrawBig5Text(sur: PSDL_Surface; str: pchar; x_pos, y_pos: integer; color: uint32);
procedure DrawBig5ShadowText(word: pchar; x_pos, y_pos: integer; color1, color2: uint32);
procedure DrawTextWithRect(word: puint16; x, y, w: integer; color1, color2: uint32;
alpha: integer = 50; Refresh: integer = 1);
procedure DrawGBKShadowText(word: pchar; x_pos, y_pos: integer; color1, color2: uint32);
procedure DrawU16ShadowText(word: pchar; x_pos, y_pos: integer; color1, color2: uint32);
function Simplified2Traditional(mSimplified: ansistring): ansistring;
procedure DrawPartPic(pic: pointer; x, y, w, h, x1, y1: integer);
function Traditional2Simplified(mTraditional: string): string;
procedure PlayBeginningMovie;
procedure SDL_UpdateRect2(scr1: PSDL_Surface; x, y, w, h: integer);
procedure SDL_GetMouseState2(var x, y: integer);
procedure DestroyRenderTextures;
procedure CreateAssistantRenderTextures;
procedure CreateMainRenderTextures;
procedure ResizeWindow(w, h: integer);
procedure ResizeSimpleText(initial: integer = 0);
procedure SwitchFullscreen;
procedure QuitConfirm;
function JoyAxisMouse(interval: uint32; param: pointer): uint32;
function CheckBasicEvent: uint32;
procedure ChangeCol;
//用于读取的子程
procedure InitialPicArrays;
procedure ReadTiles;
function LoadPNGTilesThread(Data: pointer): longint; cdecl;
function ReadFileToBuffer(p: pchar; filename: string; size, malloc: integer): pchar;
procedure FreeFileBuffer(var p: pchar);
function LoadIdxGrp(stridx, strgrp: string; var idxarray: TIntArray; var grparray: TByteArray): integer;
function LoadPNGTiles(path: string; var PNGIndexArray: TPNGIndexArray; var TextureArray: TTextureArray;
var SurfaceArray: TSurfaceArray; LoadPic: integer = 1): integer; overload;
procedure LoadOnePNGTexture(path: string; p: pchar; var PNGIndex: TPNGIndex; forceLoad: integer = 0); overload;
function LoadTileFromFile(filename: string; pt: PPSDL_Texture; ps: PPSDL_Surface; usesur: integer;
var w, h: integer): boolean;
function LoadTileFromMem(p: pchar; len: integer; pt: PPSDL_Texture; ps: PPSDL_Surface;
usesur: integer; var w, h: integer): boolean;
function LoadStringFromIMZMEM(path: string; p: pchar; num: integer): string;
function LoadStringFromZIP(zfilename, filename: string): string;
//function LoadSurfaceFromZIPFile(zipFile: unzFile; filename: string): PSDL_Surface;
//procedure FreeAllSurface;
procedure DestroyAllTextures(all: integer = 1);
procedure DestroyFontTextures();
procedure DrawPNGTile(render: PSDL_Renderer; PNGIndex: TPNGIndex; FrameNum: integer; px, py: integer); overload;
procedure DrawPNGTile(render: PSDL_Renderer; PNGIndex: TPNGIndex; FrameNum: integer;
px, py: integer; region: psdl_rect; shadow, alpha: integer; mixColor: uint32; mixAlpha: integer;
scalex, scaley, angle: real; center: PSDL_Point); overload;
procedure DrawPNGTileS(scr: PSDL_Surface; PNGIndex: TPNGIndex; FrameNum: integer; px, py: integer;
region: psdl_rect; shadow, alpha: integer; mixColor: uint32; mixAlpha: integer;
scalex, scaley, angle: real); overload;
function CopyIndexSurface(PNGIndexArray: TPNGIndexArray; i: integer): PSDL_Surface;
procedure PlayMovie(const filename: string; fullwindow: integer);
procedure Big5ToGBK(p: pchar);
procedure Big5ToU16(p: pchar);
//字串的绘制长度
function DrawLength(str: WideString): integer; overload;
function DrawLength(p: pWideChar): integer; overload;
function DrawLength(p: pchar): integer; overload;
//颜色
function MapRGBA(r, g, b: byte; a: byte = 255): uint32;
procedure GetRGBA(color: uint32; r, g, b: pbyte; a: pbyte = nil);
//屏幕控制相关
procedure SetFontSize(Chnsize, engsize: integer; force: integer = 0);
procedure ResetFontSize;
procedure LoadTeamSimpleStatus(var max: integer);
procedure DrawSimpleStatusByTeam(i, px, py: integer; mixColor: uint32; mixAlpha: integer);
procedure FreeTeamSimpleStatus(var SimpleStatus: array of PSDL_Surface);
procedure TransBlackScreen;
procedure RecordFreshScreen; overload;
procedure LoadFreshScreen; overload;
procedure RecordFreshScreen(x, y, w, h: integer); overload;
procedure LoadFreshScreen(x, y: integer); overload;
procedure FreeFreshScreen;
procedure UpdateAllScreen;
procedure CleanTextScreen; overload;
procedure CleanTextScreenRect(x, y, w, h: integer); overload;
//清键值
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 GetRealRect(var x, y, w, h: integer; force: integer = 0): TSDL_Rect; overload;
function GetRealRect(rect: TSDL_Rect; force: integer = 0): TSDL_Rect; overload;
function KeepRatioScale(w1, h1, w2, h2: integer): TStretchInfo;
//这两个可能是加密时用过
procedure swap(var x, y: byte); overload;
procedure swap(var x, y: uint32); overload;
//几个数学函数
function round(x: real): integer;
function RegionParameter(x, x1, x2: integer): integer;
function LinearInsert(x, x1, x2: real; y1, y2: integer): integer;
procedure QuickSort(var a: array of integer; l, r: Integer);
procedure QuickSortB(var a: array of TBuildInfo; l, r: integer);
//计时, 测速用
procedure tic;
procedure toc;
implementation
uses
kys_battle,
kys_draw;
function EventFilter(p: pointer; e: PSDL_Event): longint; cdecl;
begin
Result := 1;
if (e.type_ >= $700) and (e.type_ <= $702) then
Result := 0;
end;
procedure InitialMusic;
var
i: integer;
str: string;
sf: BASS_MIDI_FONT;
Flag: longword;
begin
BASS_Set3DFactors(1, 0, 0);
sf.font := BASS_MIDI_FontInit(pchar(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 BASS_SAMPLE_MONO or Flag;
for i := 0 to High(Music) do
begin
str := AppPath + 'music/' + IntToStr(i) + '.mp3';
if FileExists(pchar(str)) then
begin
try
Music[i] := BASS_StreamCreateFile(False, pchar(str), 0, 0, 0);
finally
end;
end
else
begin
str := AppPath + 'music/' + IntToStr(i) + '.mid';
if FileExists(pchar(str)) then
begin
try
Music[i] := BASS_MIDI_StreamCreateFile(False, pchar(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 := 0 to High(Esound) do
begin
str := AppPath + 'sound/e' + IntToStr(i) + '.wav';
if FileExists(pchar(str)) then
ESound[i] := BASS_SampleLoad(False, pchar(str), 0, 0, 1, Flag)
else
ESound[i] := 0;
//showmessage(inttostr(esound[i]));
end;
for i := 0 to High(Asound) do
begin
str := AppPath + formatfloat('sound/atk00', i) + '.wav';
if FileExists(pchar(str)) then
ASound[i] := BASS_SampleLoad(False, pchar(str), 0, 0, 1, Flag)
else
ASound[i] := 0;
end;
//BASS_MIDI_FontFree(sf.font);
end;
procedure FreeAllMusic;
var
i: integer;
begin
for i := 0 to High(Music) do
begin
if Music[i] <> 0 then
BASS_StreamFree(Music[i]);
end;
for i := 0 to High(Asound) do
begin
if Asound[i] <> 0 then
BASS_SampleFree(Asound[i]);
end;
for i := 0 to High(Esound) do
begin
if Esound[i] <> 0 then
BASS_SampleFree(Esound[i]);
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 in [Low(Music)..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: pchar; 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 PlaySound(SoundNum, times: integer); overload;
var
ch: HCHANNEL;
repeatable: boolean;
begin
if times = -1 then
repeatable := True
else
repeatable := False;
if (SoundNum in [Low(Esound)..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_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 PlaySound(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 in [Low(Esound)..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 SOUND3D = 1 then
begin
pos.x := x / 50.0;
pos.y := y / 50.0;
pos.z := z / 50.0;
//posvec.x := x;
//posvec.y := y;
//posvec.z := z;
//posvel.x := -x / 50.0;
//posvel.y := -y / 50.0;
//posvel.z := -z / 50.0;
if (x = 0) and (y = 0) and (z = 0) then
begin
pos.z := 0.1;
//posvel.z := -0.2;
end;
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); overload;
var
ch: HCHANNEL;
repeatable: boolean;
begin
if times = -1 then
repeatable := True
else
repeatable := False;
if (SoundNum in [Low(Asound)..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(Asound[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 PlaySound(SoundNum: integer); overload;
begin
PlaySound(Soundnum, 0);
end;
procedure PlaySound(filename: pchar; times: integer); overload;
begin
{if fileexists(filename) then
begin
Sound := Mix_LoadWav(filename);
Mix_PlayChannel(-1, sound, times);
end;}
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
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
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;
//取调色板的颜色, 视频系统是32位色, 但很多时候仍需要原调色板的颜色
function ColColor(num: integer): uint32;
begin
//result.
ColColor := MapRGBA(Acol[num * 3] * 4, Acol[num * 3 + 1] * 4, Acol[num * 3 + 2] * 4);
end;
//判断像素是否在屏幕内
function JudgeInScreen(px, py, w, h, xs, ys: integer): boolean; overload;
begin
Result := False;
//if (px - xs + w >= 0) and (px - xs < screen.w) and (py - ys + h >= 0) and (py - ys < screen.h) then
Result := True;
end;
//判断像素是否在指定范围内(重载)
function JudgeInScreen(px, py, w, h, xs, ys, xx, yy, xw, yh: integer): boolean; overload;
begin
Result := False;
if (px - xs + w >= xx) and (px - xs < xx + xw) and (py - ys + h >= yy) and (py - ys < yy + yh) then
Result := True;
end;
//获取游戏中坐标在屏幕上的位置
function GetPositionOnScreen(x, y, CenterX, CenterY: integer): TPosition;
begin
Result.x := -(x - CenterX) * 18 + (y - CenterY) * 18 + CENTER_X;
Result.y := (x - CenterX) * 9 + (y - CenterY) * 9 + CENTER_Y;
if needOffset <> 0 then
begin
Result.x := Result.x - offsetX;
Result.y := Result.y - offsetY;
end;
end;
//big5转为unicode
function Big5ToUnicode(str: pchar): WideString;
var
len: integer;
begin
{$IFDEF fpc}
Result := UTF8Decode(CP950ToUTF8(str));
{$ELSE}
len := MultiByteToWideChar(950, 0, pchar(str), -1, nil, 0);
setlength(Result, len - 1);
MultiByteToWideChar(950, 0, pchar(str), length(str), pWideChar(Result), len + 1);
{$ENDIF}
end;
function GBKToUnicode(str: pchar): WideString;
var
len: integer;
begin
{$IFDEF fpc}
Result := UTF8Decode(CP936ToUTF8(str));
{$ELSE}
len := MultiByteToWideChar(950, 0, pchar(str), -1, nil, 0);
setlength(Result, len - 1);
MultiByteToWideChar(950, 0, pchar(str), length(str), pWideChar(Result), len + 1);
{$ENDIF}
end;
function PCharToUnicode(str: pchar; len: integer = -1): WideString;
begin
Result := pWideChar(str);
if len >= 0 then
begin
if length(Result) > len then
setlength(Result, len);
end;
end;
//unicode转为big5, 仅用于输入姓名
function UnicodeToBig5(str: pWideChar): string;
var
len: integer;
begin
{$IFDEF fpc}
Result := UTF8ToCP950((str));
{$ELSE}
len := WideCharToMultiByte(950, 0, pWideChar(str), -1, nil, 0, nil, nil);
setlength(Result, len + 1);
WideCharToMultiByte(950, 0, pWideChar(str), -1, pchar(Result), len + 1, nil, nil);
{$ENDIF}
end;
//unicode转为GBK, 仅用于输入姓名
function UnicodeToGBK(str: pWideChar): string;
var
len: integer;
begin
{$IFDEF fpc}
Result := UTF8ToCP936((str));
{$ELSE}
len := WideCharToMultiByte(936, 0, pWideChar(str), -1, nil, 0, nil, nil);
setlength(Result, len + 1);
WideCharToMultiByte(936, 0, pWideChar(str), -1, pchar(Result), len + 1, nil, nil);
{$ENDIF}
end;
//繁体汉字转化成简体汉字
function Traditional2Simplified(mTraditional: string): string; //返回繁体字符串
var
L: integer;
begin
L := Length(mTraditional);
SetLength(Result, L + 1);
Result[L + 1] := char(0);
{$IFDEF windows}
if L > 0 then
LCMapString(GetUserDefaultLCID,
$02000000, pchar(mTraditional), L, @Result[1], L);
{$ELSE}
Result := mTraditional;
{$ENDIF}
end; { Traditional2Simplified }
//生成或查找已知纹理, 返回其指针, 是否销毁由调用者决定
//返回值为建议是否销毁是否已经保存
function CreateFontTile(num: uint16; usesur: integer; var p: pointer; var w, h: integer): boolean;
var
size0, size: integer;
pfont: PTTF_Font;
needcreate: boolean;
whitecolor: uint32 = $ffffffff;
word: array[0..2] of uint16 = (32, 0, 0);
src, dst: TSDL_Rect;
sur, tempsur: PSDL_Surface;
tex, temptex: PSDL_Texture;
begin
Result := False;
//是否可能是已有纹理
if num >= $1000 then
begin
size0 := CHINESE_FONT_SIZE;
size := CHINESE_FONT_REALSIZE;
pfont := font;
src.x := CHNFONT_SPACEWIDTH;
src.y := 0;
word[1] := num;
end
else
begin
size0 := ENGLISH_FONT_SIZE;
size := ENGLISH_FONT_REALSIZE;
pfont := engfont;
src.x := 0;
src.y := 0;
word[0] := num;
end;
//可以直接查找的情况, 其他情况需要创建
if (size = size0) and (CharSize[num] = size0) then
begin
case usesur of
0:
begin
p := CharTex[num];
SDL_QueryTexture(CharTex[num], nil, nil, @w, @h);
end;
else
begin
p := CharSur[num];
w := CharSur[num].w;
h := CharSur[num].h;
end;
end;
Result := True;
end
else
begin
tempsur := TTF_RenderUNICODE_blended(pfont, @word[0], TSDL_Color(whitecolor));
src.w := tempsur.w - src.x;
src.h := tempsur.h;
dst.x := 0;
dst.y := 0;
dst.w := src.w;
dst.h := src.h;
w := src.w;
h := src.h;
sur := SDL_CreateRGBSurface(0, dst.w, dst.h, 32, RMASK, GMASK, BMASK, AMASK);
SDL_SetSurfaceBlendMode(tempsur, SDL_BLENDMODE_NONE);
SDL_UpperBlit(tempsur, @src, sur, @dst);
try
SDL_FreeSurface(tempsur);
except
writeln('Free font surface ', widechar(num), size0, ' failed.');
end;
if usesur = 0 then
begin
tex := SDL_CreateTextureFromSurface(render, sur);
SDL_FreeSurface(sur);
p := Tex;
//检查是否需要保存
if size = size0 then
begin
write(widechar(num));
if CharSize[num] > 0 then
SDL_DestroyTexture(CharTex[num]);
CharTex[num] := tex;
CharSize[num] := size0;
Result := True;
end;
end
else
begin
p := pointer(Sur);
if size = size0 then
begin
write(widechar(num));
if CharSize[num] > 0 then
SDL_FreeSurface(CharSur[num]);
CharSur[num] := sur;
CharSize[num] := size0;
Result := True;
end;
end;
end;
end;
//显示unicode文字
//engsize如果未指定则按照中文宽度一半
procedure DrawText(word: puint16; x_pos, y_pos: integer; color: uint32; engwidth: integer = -1);
var
dest, src, dst: TSDL_Rect;
tempcolor, whitecolor: TSDL_Color;
len, i, k: integer;
word0: array[0..2] of uint16 = (32, 0, 0);
word1: ansistring;
word2: WideString;
p1: pbyte;
p2: pbyte;
t: WideString;
Sur: PSDL_Surface;
Tex, ptex: PSDL_Texture;
r, g, b, size: byte;
saved: boolean;
p: pointer;
w, h: integer;
begin
{$IFDEF fpc}
//widestring在fpc中的默认赋值动作是将utf8码每字节间插入一个00.
//此处删除这些0, 同时统计这些0的数目, 若与字串长度相同
//即认为是一个纯英文字串, 或者是一个直接赋值的widestring,
//需要再编码为Unicode, 否则即认为已经是Unicode
len := length(pWideChar(word));
setlength(word1, len * 2 + 1);
p1 := @word1[1];
p2 := pbyte(word);
k := 0;
for i := 0 to len - 1 do
begin
p1^ := p2^;
Inc(p1);
Inc(p2);
if p2^ = 0 then
begin
k := k + 1;
Inc(p2);
end
else
begin
p1^ := p2^;
Inc(p1);
Inc(p2);
end;
end;
p1^ := 0;
if k >= len then
begin
word2 := UTF8Decode(word1);
word := @word2[1];
end;
{$ELSE}
//word2 := UTF8Decode(string(word));
//word := @word2[1];
{$ENDIF}
GetRGBA(color, @r, @g, @b);
tempcolor.r := r;
tempcolor.g := g;
tempcolor.b := b;
tempcolor.a := 255;
if engwidth <= 0 then
engwidth := CHINESE_FONT_REALSIZE div 2;
if SIMPLE = 1 then
begin
t := Traditional2Simplified(pWideChar(word));
word := puint16(t);
end;
dest.x := x_pos;
dest.y := y_pos;
//如果当前为标准字号, 则创建纹理, 否则临时生成表面
while word^ > 0 do
begin
i := word^;
word0[1] := i;
Inc(word);
if i >= $1000 then
dest.y := y_pos
else
dest.y := y_pos + 4;
saved := CreateFontTile(i, SW_SURFACE, p, w, h);
dest.w := w;
dest.h := h;
case SW_SURFACE of
0:
begin
tex := PSDL_Texture(p);
SDL_SetTextureColorMod(tex, r, g, b);
if TEXT_LAYER = 1 then
begin
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_MOD);
SDL_RenderCopy(render, tex, nil, @dest);
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
end;
SDL_RenderCopy(render, tex, nil, @dest);
if not saved then
SDL_DestroyTexture(tex);
end;
else
begin
sur := PSDL_Surface(p);
SDL_SetSurfaceColorMod(sur, r, g, b);
if TEXT_LAYER = 1 then
begin
SDL_SetSurfaceBlendMode(sur, SDL_BLENDMODE_MOD);
SDL_UpperBlit(sur, nil, CurTargetSurface, @dest);
end;
SDL_SetSurfaceBlendMode(sur, SDL_BLENDMODE_BLEND);
SDL_UpperBlit(sur, nil, CurTargetSurface, @dest);
end;
end;
if i >= $1000 then
dest.x := dest.x + CHINESE_FONT_REALSIZE
else
dest.x := dest.x + engwidth;
end;
HaveText := 1;
end;
//显示英文
procedure DrawEngText(word: puint16; x_pos, y_pos: integer; color: uint32);
var
dest: TSDL_Rect;
Text: PSDL_Surface;
tempcolor: TSDL_Color;
tex: PSDL_Texture;
r, g, b: byte;
str: WideString = ' ';
begin
if (ENGLISH_FONT_SIZE = ENGLISH_FONT_REALSIZE) then
DrawText(word, x_pos, y_pos - 4, color, -1)
else
begin
//Text := TTF_RenderUNICODE_solid(engfont, @str[1], tempcolor);
DrawText(word, x_pos, y_pos - 4, color, ENGLISH_FONT_REALSIZE div 2 + 1);
//sdl_freesurface(Text);
end;
end;
//显示unicode中文阴影文字, 即将同样内容显示2次, 间隔1像素
procedure DrawShadowText(word: puint16; x_pos, y_pos: integer; color1, color2: uint32;
Tex: PSDL_Texture = nil; Sur: PSDL_Surface = nil; realPosition: integer = 0; eng: integer = 0); overload;
var
w, h: integer;
ptex: PSDL_Texture;
pscreen: PSDL_Surface;
changed: boolean = False;
begin
if SW_SURFACE = 0 then
begin
ptex := SDL_GetRenderTarget(render);
if Tex = nil then
begin
if TEXT_LAYER <> 0 then
SDL_SetRenderTarget(render, TextScreenTex);
end
else
SDL_SetRenderTarget(render, Tex);
end
else
begin
pscreen := CurTargetSurface;
if Sur = nil then
begin
if TEXT_LAYER <> 0 then
CurTargetSurface := TextScreen;
end
else
CurTargetSurface := Sur;
end;
if realPosition = 0 then
GetRealRect(x_pos, y_pos, w, h);
if eng = 0 then
begin
DrawText(word, x_pos + 1, y_pos, color2);
DrawText(word, x_pos, y_pos, color1);
end
else
begin
DrawEngText(word, x_pos + 1, y_pos, color2);
DrawEngText(word, x_pos, y_pos, color1);
end;
//if changed then
if SW_SURFACE = 0 then
SDL_SetRenderTarget(render, ptex)
else
CurTargetSurface := pscreen;
end;
//显示英文阴影文字
procedure DrawEngShadowText(word: puint16; x_pos, y_pos: integer; color1, color2: uint32;
Tex: PSDL_Texture = nil; Sur: PSDL_Surface = nil);
begin
DrawShadowText(word, x_pos, y_pos + 4, color1, color2, Tex, Sur, 0, 1);
end;
//显示big5文字
procedure DrawBig5Text(sur: PSDL_Surface; str: pchar; x_pos, y_pos: integer; color: uint32);
var
len: integer;