-
Notifications
You must be signed in to change notification settings - Fork 34
/
FileHandling.pas
2332 lines (2244 loc) · 84.7 KB
/
FileHandling.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 FileHandling;
interface
uses Windows, LightAdjust, SysUtils, Graphics, jpeg, TypeDefinitions, pngimage,
SyncObjs, Classes, Controls, Dialogs, vcl.ExtDlgs;
procedure LoadFullM3I(var Header: TMandHeader10; Filename: String);
function LoadParameter(var Para10: TMandHeader10; FileName: String; Verbose: LongBool): Boolean;
//function LoadParsFromStream(var Para10: TMandHeader10; var Stream: TStream): Boolean;
procedure ConvertFromOldLightParas(var Light8: TLightingParas8; fSize: Integer);
procedure SaveJPEGfromBMP(FileName: String; bmp: TBitmap; Quality: Integer);
procedure SaveBMP(FileName: String; bmp: TBitmap; pf: TPixelFormat);
procedure SavePNG(FileName: String; bmp: TBitmap; SaveTXTparas: Boolean);
procedure CopyHeaderAsTextToClipBoard(para: TPMandHeader10; Titel: String);
function GetHeaderFromText(Text: AnsiString; var para: TMandHeader10; var Titel: String): LongBool;
procedure LoadAccPreset(nr: Integer);
procedure SaveAccPreset(nr: Integer);
function LoadColPreset(nr: Integer): Boolean;
procedure SaveColPreset(nr: Integer);
procedure LoadIni;
procedure SaveIni(ForceSave: LongBool);
procedure SetSaveDialogNames(Name: String);
function ConvertHeaderFromOldParas(var Para8: TMandHeader8; LoadFs: LongBool): LongBool;
procedure LoadHAddon(var f: file; HA: PTHeaderCustomAddon);
procedure UpdateLightVersion8(var Light8: TLightingParas8);
function ConvertColPreset16To20(P16: TPLpreset16): TLpreset20;
function ConvertColPreset164To20(var P164: TLpreset164): TLpreset20;
procedure ConvertLight8to9(Light8: TLightingParas8; var Light9: TLightingParas9);
procedure UpdateFormulaOptionTo20(PCFAddon: PTHeaderCustomAddon);
//function LoadBackgroundPic(FileName: String; Smooth, Convert2Spherical: LongBool): LongBool;
procedure Make8bitGreyscalePalette(var ABmp: TBitmap);
function GetLightParaFile(FileName: String; var LightParas: TLightingParas9; bKeepLights: LongBool): LongBool;
procedure UpdateFormulaOptionAbove20(var para: TMandHeader10);
procedure ParseExternFormulas(PCFAddon: PTHeaderCustomAddon);
function GetFileModDate(filename: string): TDatetime;
function LoadBackgroundPicT(Light: TPLightingParas9): Boolean;
procedure UpdateLightParasAbove3(var Light: TLightingParas9);
procedure StoreFavouriteStatus(formulaname: String; Status: Integer);
procedure SaveZBuf(FileName: String; stype: Integer; const ZOffset, ZScale: double; const InvertZBuffer:boolean); //stype: 0=png, 1=bmp
procedure SdoAA;
function GetDirectory(IniDir: String; AOwner: TWinControl): String;
procedure Save1bitPNG(FileName: String; bmp: TBitmap);
function ChangeFileExtSave(FileName, Ext: String): String;
function FileIsBigger1(Fname: String): LongBool;
procedure SaveBMP2FStream(FileName: String; bmp: TBitmap; pf: TPixelFormat; FS: TFileStream);
procedure SavePNG2FStream(FileName: String; bmp: TBitmap; FS: TFileStream);
procedure SaveJPEGfromBMP2FStream(FileName: String; bmp: TBitmap; Quality: Integer; FS: TFileStream);
procedure StoreHistoryPars(var pars: TMandHeader10);
procedure UpdatePresetFrom20(var p20: TLpreset20);
procedure ChangeFName(SaveDialog: TSaveDialog; FName: String);
procedure SetDialogName(SaveDialog: TSaveDialog; Name: String); overload;
procedure SetDialogName(OpenDialog: TOpenDialog; Name: String); overload;
procedure SetDialogName(OpenPicDialog: TOpenPictureDialog; Name: String); overload;
procedure SetDialogDirectory(SaveDialog: TSaveDialog; FDir: String); overload;
procedure SetDialogDirectory(OpenDialog: TOpenDialog; FDir: String); overload;
procedure SetDialogDirectory(OpenPicDialog: TOpenPictureDialog; FDir: String); overload;
function MakeTextparas(para: TPMandHeader10; Titel: String): AnsiString;
var IniVal: array[0..37] of String = ('81','5','20','5','30','30','60','50','10', '3','0','0:0','','0','1','1','0','','',
'No','No','Auto','','No','65 100','779 671','844 100','844 100','844 100','-1','No','Yes','','','0','Glossy' (*'Windows'*), '100%' , '');
IniDirs: array[0..12] of String = ('','','','','','','','','','','','',''); //M3Idir, M3Pdir, BMPdir, FormulaDir, M3Adir, AniOut, BGpic, Lightparas, BigRenders, LightMaps, voxel, M3C, Meshes
IniHigherVersion: array of String;
IniFileDate: TDateTime;
HistoryFolder: String;
ACCPresetFutureAddons: array[0..3] of array[0..4] of String;
ACCaddedStrings: array[0..3] of Integer = (0,0,0,0);
LastHisParSaveTime: TDateTime;
LHPSLight: TLightingParas9;
const
IniMax: Integer = 37;
IniItem: array[0..37] of String = ('StickOption','MandRotDeg','NavSlideStep',
'NavLookAngle','NavFarPlane','NavFOVy','NavMaxIts','AniFrameCount','AniPrevFPS', //NavFarPlane not used anymore
'AniSmoothPar','NaviAZERTY','UserAspect','M3LFolder','ImageSharp','NavFkey',
'ScaleDEstop','SaveImagePNG','BigRendersFolder','LightMapsFolder','NavHiQ',
'NavDoubleClickMode','ThreadCount','VoxelFolder','SavePNGtextPars','m3dPos',
'm3dSize','FormulaPos','LightPos','PostpPos','ThreadPriority','DisableTBoost',
'SaveImgInM3I','M3CFolder','Author','NaviPanelShow','VisualTheme','NaviSize',
'MeshesFolder');
AccPresetFileNames: array[0..3] of String = ('preset_preview.txt',
'preset_video.txt', 'preset_mid.txt', 'preset_high.txt');
AccPresetItemNames: array[0..7] of String = ('SmoothNormals', 'DEstop',
'DEaccuracy', 'BinSearch', 'ImageWidth', 'ImageScale', 'RayStepFactor', 'RayLimiter');
actMandId: Integer = 44;
actLightId: Integer = 7; //0..7 only!
actLightIdEx: Integer = 8; //Byte 0..255 new Lightversion
actPresetVersion: Integer = 5;
implementation
uses Mand, ImageProcess, Clipbrd, DivUtils, Math, CustomFormulas, HeaderTrafos,
Animation, FormulaGUI, Navigator, AniPreviewWindow, Interpolation, Tiling,
Math3D, Forms, Maps, Undo, Vcl.Themes, ZBuf16BitGen;
function FileIsBigger1(Fname: String): LongBool;
var F: TSearchRec;
begin
Result := (FindFirst(Fname, faAnyFile, F) = 0) and (F.Size > 1);
FindClose(F);
end;
function ChangeFileExtSave(FileName, Ext: String): String;
var se: String;
const cExtToCh: String = '.BMP.PNG.JPG.JPE.TXT.JPEG.TIF.TIFF.BIG.M3A.M3P.M3I.M3L.M3V.M3C.PGM';
begin
se := UpperCase(ExtractFileExt(FileName));
if Pos(se, cExtToCh) > 0 then Result := ChangeFileExt(FileName, Ext) else
if Ext > '' then
begin
if (Length(FileName) > 0) and (FileName[Length(FileName)] = '.') then
Delete(FileName, Length(FileName), 1);
Result := FileName + Ext;
end
else Result := FileName;
end;
function GetDirectory(IniDir: String; AOwner: TWinControl): String;
var OD: TOpenDialog;
begin
{ Result := IniDir;
SelectDirectory('Select a directory', ExtractFileDrive(IniDir), Result, [], AOwner);
Result := IncludeTrailingPathDelimiter(Result); }
OD := TOpenDialog.Create(AOwner);
try
OD.InitialDir := IniDir;
OD.FileName := {IncludeTrailingPathDelimiter(IniDir) +} 'Go to folder and press open';
if OD.Execute then
Result := IncludeTrailingPathDelimiter(ExtractFileDir(OD.FileName))
else Result := IniDir;
finally
OD.Free;
end;
end;
procedure UpdateFormulaOptionAbove20(var para: TMandHeader10); //+header update
var i, j: Integer;
d: Double;
Q: TQuaternion;
tmpFormula: THAformula;
const M3dBeta1796: Single = 1.796;
begin
with PTHeaderCustomAddon(para.PCFAddon)^ do
begin
for i := 0 to 5 do
begin
j := Formulas[i].iFnr;
if (j = 5) and (Formulas[i].iOptionCount < 6) then //bulbox, added second r parameter
begin
Formulas[i].dOptionValue[5] := Formulas[i].dOptionValue[4];
Formulas[i].byOptionType[5] := 9;
Formulas[i].iOptionCount := 6;
end;
end;
end;
with para do
begin
ParseExternFormulas(PTHeaderCustomAddon(PCFAddon));
if MandId < 21 then
begin
if mZstepDiv < s001 then mZstepDiv := s03;
bPlanarOptic := 0;
end;
if MandId < 22 then
begin
byColor2Option := 0;
dXWrot := 0;
dYWrot := 0;
dZWrot := 0;
sColorMul := 1;
dJw := 0;
Light.TBoptions := (Light.TBoptions and $E07FFFFF) or (((Light.TBoptions and $1F000000) shr 1) or $10000000);
//light.TBoptions bit 24..29 Gamma 0..63 -> -32..31 = gamma 0.5..2 bit21-23version
end;
if MandId < 23 then sRaystepLimiter := 1;
if MandId < 24 then
begin
StereoScreenWidth := 1;
StereoScreenDistance := 2;
StereoMinDistance := s05;
end;
if MandId < 25 then HSmaxLengthMultiplier := 1;
if MandId < 26 then AODEdithering := 0;
if MandId < 27 then
begin
SRamount := s05;
SRreflectioncount := 1;
bCalcSRautomatic := 0;
byCalcNsOnZBufAuto := 0;
end;
if MandId < 28 then SSAORcount := 1;
if MandId < 29 then
begin
sDOFclipR := 25;
sDOFaperture := 0.02;
end;
if MandId < 30 then sDEAOmaxL := 1;
if MandId < 31 then sDEcombS := s05;
if MandId < 32 then sDOFZsharp2 := sDOFZsharp;
if MandId < 33 then iMaxItsF2 := Iterations;
if MandId < 34 then DEmixColorOption := 0;
if MandId < 35 then
begin
TilingOptions := 0;
bDFogIt := 0;
end;
if MandId < 36 then
begin
iAmbCalcTime := 0;
bSSAO24BorderMirrorSize := 0;
iOptions := iOptions and $7FFFFFFB;
end;
bStereoMode := 0; //?
if sM3dVersion = M3dBeta1796 then
begin
Light.Lights[2].FreeByte := 1;
Light.Lights[3].AdditionalByteEx := 77;
Light.Lights[0].AdditionalByteEx := 3;
end;
if MandId < 37 then
begin
sTransmissionAbsorption := 0.2;
sTRIndex := 1.5;
end;
if MandId < 38 then sTRscattering := 1;
if MandId < 39 then
begin
sFmixPow := 2;
iReflectsCalcTime := 0;
end;
if MandId < 40 then
begin
MCSoftShadowRadius := SingleToShortFloat(1);
MCDepth := 3;
MCcontrast := 128;
bCalc1HSsoft := 0;
MCoptions := 2; //secantmode
MCdiffReflects := 0;
iOptions := iOptions and $7FFFFFFD; //turn off Shortdistance check DE
end;
if MandId < 42 then
with PTHeaderCustomAddon(para.PCFAddon)^ do
begin
bNewOptions := 0;
MCoptions := MCoptions or 16; //bokeh2
bVolLightNr := Min(5, bVolLightNr); //was: repeatfrom
j := 5;
while (j > 0) and (Formulas[j].iItCount = 0) do Dec(j);
if bOptions1 <> 0 then bHybOpt1 := 0
else bHybOpt1 := j or (bVolLightNr shl 4); //end1, repeat1 2x 4bit
bHybOpt2 := $151;
if bOptions1 = 2 then //DEcomb
begin
bHybOpt2 := 1 or (j shl 4) or (Max(1, bVolLightNr) shl 8); //start2, end2, repeat2 3x 4bit
Formulas[0].iItCount := 1;
// if (bOptions3 = 5) and (Formula[0] = dIFS) then make alternating...
if not (bOptions3 in [2,6]) then FlipInt(iMaxItsF2, Iterations);
end
else bOptions3 := 0;
if bOptions3 in [2,6] then //invmax: inv F1 and comb max; Mix2: first F2..F6 then F1
begin
tmpFormula := Formulas[0];
for i := 0 to j - 1 do Formulas[i] := Formulas[i + 1];
Formulas[j] := tmpFormula;
if bOptions3 = 6 then bOptions3 := 5;
bHybOpt1 := Max(0, j - 1) or (Max(0, bVolLightNr - 1) shl 4); //end1, repeat1 2x 4bit
bHybOpt2 := j or (j shl 4) or (j shl 8); //start2, end2, repeat2 3x 4bit
end;
end;
if MandId < 43 then
begin
bMCSaturation := 32;
bColorOnIt := 0;
if byColor2Option = 5 then bColorOnIt := 1 else
if byColor2Option = 6 then byColor2Option := 5;
end;
if (MandId > 43) and ((bNewOptions and 1) <> 0) then
begin //change Quaternion back to RotationMatrix
Q := TPQuaternion(@hVGrads)^;
CreateMatrixFromQuat(hVGrads, Q);
d := 2.1345 / (dZoom * Width); //StepWidth, not needed in header!
hVGrads := NormaliseMatrixTo(d, @hVGrads);
end;
if MandId < 44 then
begin
bVolLightNr := 2 shl 4;
end;
if MandId > 35 then Mand3DForm.OutMessage('The parameters were made with program version ' + ProgramVersionStr(sM3dVersion, M3dSubRevision))
else if MandId > actMandId then Mand3DForm.OutMessage('A correct rendering could be not possible.');
sM3dVersion := M3dVersion;
end;
end;
procedure UpdateLightParasAbove3(var Light: TLightingParas9);
var ver, verex, i: Integer;
sv: TSVec;
begin
with Light do
begin
verex := 0;
ver := (TBoptions shr 20) and 7;
if ver < 4 then
begin
DynFogCol2[0] := DynFogR;
DynFogCol2[1] := DynFogG;
DynFogCol2[2] := DynFogB;
end;
if ver < 5 then
begin //TB6: 53 dynFog TB14: 0 colVaronZ
TBpos[6] := Max(0, Min(159, (TBpos[6] - 53) * 2 + 53)); //0..159
VarColZpos := Max(-120, Min(360, VarColZpos * 2)); //-120..360
end;
if ver < 6 then
begin
bColorMap := 0;
RoughnessFactor := 255;
PicOffsetZ := (Integer(PicOffsetZ) + 128) and 255;
for i := 0 to 5 do Lights[i].Loption := Lights[i].Loption and $FD; // bit2: lightmap; -> no lightmaps in earlier versions
end;
if ver < 7 then
begin //Double7 + byte for map>255, initiate mapnrEx to 0
for i := 0 to 5 do
begin
Lights[i].LightMapNr := Lights[i].LightMapNr and $FF;
Lights[i].AdditionalByteEx := 0; //diffnr ex in Lights[1]
Lights[i].FreeByte := 0;
end;
end
else verex := Lights[0].AdditionalByteEx;
TBoptions := (TBoptions and $FF8FFFFF) or (actLightId shl 20);
if verex < 1 then
begin
Lights[0].FreeByte := 0; //used for blend dynfog option (bit1)!
end;
if verex < 2 then
begin
Lights[2].AdditionalByteEx := 30; //diffmap scaling
Lights[1].FreeByte := (AdditionalOptions shr 1) and 1; //diffmap optionsEx=RG1.itemindex, here just "on normals"
AdditionalOptions := AdditionalOptions and $FB; // ((Ord(CheckBox18.Checked) and 1) shl 2); was: rel to object, now: comb y + col
end;
if verex < 3 then
begin
Lights[3].AdditionalByteEx := 128; //amount of diffuse shadowing
Lights[2].FreeByte := 0; //iExModes
end;
if verex < 5 then
begin
// spec colors with transparency amount, ini with 0
for i := 0 to 3 do ICols[i].Color := ICols[i].Color and $FFFFFF;
for i := 0 to 9 do LCols[i].ColorSpe := (LCols[i].ColorSpe and $FFFFFF) or
(MaxOfColor(LCols[i].ColorSpe) shl 24);
end;
if verex < 6 then
begin
i := (AdditionalOptions shr 4) and 7;
if i <> 0 then i := 16;
AdditionalOptions := (AdditionalOptions and $8F) or i; //fit-border width on bg image load
// showmessage(IntToStr(Lights[0].Loption and 6)+','+IntToStr(Lights[1].Loption and 6));
for i := 0 to 5 do if (Lights[i].Loption and 6) <> 4 then
Lights[i].Lamp := Word(SingleToShortFloat(1));
Lights[4].AdditionalByteEx := 40; //BGscale = power(1.04, abe - 40);
for i := 0 to 3 do
begin
sv := ColToSVec(ICols[i].Color, False);
ICols[i].Color := (ICols[i].Color and $FFFFFF) or (Round(YofSVec(@sv) * 255) shl 24);
end;
end;
if verex < 7 then
begin
for i := 0 to 5 do Lights[i].LFunction := Lights[i].LFunction and $3F;
end;
if verex < 8 then
begin
Light.Lights[0].FreeByte := Light.Lights[0].FreeByte and 1;
Light.Lights[3].FreeByte := 0; //No col-ipol
end;
Lights[0].AdditionalByteEx := actLightIdEx;
end;
end;
procedure CopyLights(srcLights, destLights: TP6Lights);
var b1, b2: array[0..5] of Byte;
i: Integer;
begin
for i := 0 to 5 do b1[i] := destLights[i].FreeByte; //save+restore additional bytes that are not related to the lights
for i := 0 to 5 do b2[i] := destLights[i].AdditionalByteEx;
FastMove(srcLights^, destLights^, SizeOf(T6Lights));
for i := 0 to 5 do destLights[i].FreeByte := b1[i];
for i := 0 to 5 do destLights[i].AdditionalByteEx := b2[i];
end;
function GetLightParaFile(FileName: string; var LightParas: TLightingParas9; bKeepLights: LongBool): LongBool;
var f: file;
tLight: TLightingParas8;
Lights: T6Lights; // Light sources 6*32=192 bytes
begin
Result := False;
if FileExists(FileName) then
begin
FastMove(LightParas.Lights, Lights, SizeOf(T6Lights));
AssignFile(f, FileName);
try
Reset(f, 1);
if FileSize(f) < SizeOf(LightParas) then
begin
BlockRead(f, tLight, Min(FileSize(f), SizeOf(tLight)));
ConvertFromOldLightParas(tLight, FileSize(f));
UpdateLightVersion8(tLight);
ConvertLight8to9(tLight, LightParas);
end
else BlockRead(f, LightParas, Min(FileSize(f), SizeOf(LightParas)));
UpdateLightParasAbove3(LightParas);
if bKeepLights then CopyLights(@Lights, @LightParas.Lights);
Result := True;
finally
CloseFile(f);
end;
end;
end;
procedure SetDialogDirectory(SaveDialog: TSaveDialog; FDir: String); overload;
begin
SaveDialog.FileName := ExtractFileName(SaveDialog.FileName);
SaveDialog.InitialDir := FDir;
end;
procedure SetDialogDirectory(OpenDialog: TOpenDialog; FDir: String); overload;
begin
OpenDialog.FileName := ExtractFileName(OpenDialog.FileName);
OpenDialog.InitialDir := FDir;
end;
procedure SetDialogDirectory(OpenPicDialog: TOpenPictureDialog; FDir: String); overload;
begin
OpenPicDialog.FileName := ExtractFileName(OpenPicDialog.FileName);
OpenPicDialog.InitialDir := FDir;
end;
function ExtractFileDirSave(FName: String): String;
begin
if Length(FName) = 0 then Result := '' else
begin
Result := ExtractFileDir(FName);
if Result > '' then Result := IncludeTrailingPathDelimiter(Result);
end;
end;
procedure ChangeFName(SaveDialog: TSaveDialog; FName: String);
var s: String;
begin
s := ExtractFileDirSave(SaveDialog.FileName);
if s > '' then SaveDialog.InitialDir := s;
SaveDialog.FileName := {ExtractFileDirSave(SaveDialog.FileName) +} FName;
end;
procedure SetSaveDialogNames(Name: String);
var s: String;
begin
s := ChangeFileExtSave(ExtractFileName(Name), '');
with Mand3DForm do
begin
ChangeFName(SaveDialog1, s);
ChangeFName(SaveDialog2, s);
ChangeFName(SaveDialog3, s);
ChangeFName(SaveDialog4, s);
ChangeFName(SaveDialog6, s);
ChangeFName(LightAdjustForm.SaveDialog1, s);
Caption := s;
end;
end;
procedure SetDialogName(SaveDialog: TSaveDialog; Name: String); overload;
begin
ChangeFName(SaveDialog, ChangeFileExtSave(ExtractFileName(Name), ''));
end;
procedure SetDialogName(OpenDialog: TOpenDialog; Name: String); overload;
var s: String;
begin
s := ExtractFileDirSave(OpenDialog.FileName);
if s > '' then OpenDialog.InitialDir := s;
OpenDialog.FileName := ChangeFileExtSave(ExtractFileName(Name), '');
end;
procedure SetDialogName(OpenPicDialog: TOpenPictureDialog; Name: String); overload;
var s: String;
begin
s := ExtractFileDirSave(OpenPicDialog.FileName);
if s > '' then OpenPicDialog.InitialDir := s;
OpenPicDialog.FileName := ChangeFileExtSave(ExtractFileName(Name), '');
end;
procedure StoreFavouriteStatus(formulaname: String; Status: Integer);
var M: TStringList;
i: Integer;
bNotFound: LongBool;
begin
M := TStringList.Create;
try
// M.LoadFromFile(AppDataDir + IncludeTrailingPathDelimiter('Formulas') + 'FavouriteList.txt');
M.LoadFromFile(IncludeTrailingPathDelimiter(IniDirs[3]) + 'FavouriteList.txt');
except
M.Clear;
end;
bNotFound := True;
for i := 1 to M.Count do
if SameText(StrLastWords(M.Strings[i - 1]), formulaname) then
begin
if Status = 1 then
begin
M.Delete(i - 1);
Break;
end
else M.Strings[i - 1] := IntToStr(Status) + ' ' + formulaname;
bNotFound := False;
end;
if bNotFound then M.Add(IntToStr(Status) + ' ' + formulaname);
// M.SaveToFile(AppDataDir + IncludeTrailingPathDelimiter('Formulas') + 'FavouriteList.txt');
M.SaveToFile(IncludeTrailingPathDelimiter(IniDirs[3]) + 'FavouriteList.txt');
M.Clear;
M.Free;
end;
function GetFileModDate(filename: string): TDatetime;
var F: TSearchRec;
sysTime: TSystemTime;
begin
if (FindFirst(filename, faAnyFile, F) = 0) and
FileTimeToSystemTime(F.FindData.ftLastWriteTime, sysTime) then
Result := SystemTimeToDateTime(sysTime)
else
Result := 0;
FindClose(F);
end;
procedure StoreHistoryPars(var pars: TMandHeader10);
var tmpHF: String;
dt: TDateTime;
f: file;
begin //todo: store last header+haddon, comparemem of both if changed then save ... +verify every 5 minutes (lighting+postpro changes)
try
dt := Now;
tmpHF := IncludeTrailingPathDelimiter(HistoryFolder) + DateToStrHistory(dt);
if not DirectoryExists(tmpHF) then CreateDir(tmpHF);
tmpHF := IncludeTrailingPathDelimiter(tmpHF) + TimeToStrHistory(dt);
if Mand3DForm.Caption <> 'Mandelbulb 3D' then tmpHF := tmpHF + ' ' + ChangeFileExt(Mand3DForm.Caption, '');
tmpHF := tmpHF + '.m3p';
try
AssignFile(f, tmpHF);
Rewrite(f, 1);
InsertAuthorsToPara(@pars, Mand3DForm.Authors);
try
BlockWrite(f, pars, SizeOf(pars));
finally
Mand3DForm.IniMHeader;
end;
PTHeaderCustomAddon(pars.PCFAddon)^.bHCAversion := 16;
BlockWrite(f, PTHeaderCustomAddon(pars.PCFAddon)^, SizeOf(THeaderCustomAddon));
finally
CloseFile(f);
end;
LastHisParSaveTime := Now;
except
end;
end;
procedure LoadIni;
var i: Integer;
s, sa: String;
f: TextFile;
bFound, bCopyFile: LongBool;
begin
SetLength(IniHigherVersion, 0);
for i := 0 to high(IniDirs) do IniDirs[i] := AppFolder;
IniDirs[1] := AppFolder + 'M3Parameter';
IniDirs[3] := AppFolder + 'M3Formulas';
IniDirs[8] := AppFolder + 'BigRenders';
IniDirs[9] := AppFolder + 'M3Maps';
IniDirs[12] := AppFolder + 'Meshes';
//new: local ini in appdata folder:
s := AppFolder + 'Mandelbulb3D.ini';
bCopyFile := False;
HistoryFolder := AppFolder + 'History';
if not DirectoryExists(HistoryFolder) then CreateDir(HistoryFolder);
sa := AppDataDir + 'Mandelbulb3D.ini';
if (not FileExists(s)) and FileExists(sa) then
begin
if MessageDlg('The m3d Ini-file is now stored in the application folder.' + #13#10 +
'Do you want to copy the existing Ini-file from the appdata folder?' + #13#10 +
'Note: If you want a completly new installation and access the folders' + #13#10 +
'in the applications directory, select "No"', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
s := sa; //AppDataDir + 'Mandelbulb3D.ini';
bCopyFile := True;
end;
end;
if FileExists(s) then
begin
AssignFile(f, s);
try
IniFileDate := GetFileModDate(s);
Reset(f);
for i := 0 to 5 do Readln(f, IniDirs[i]);
if not EOF(f) then Readln(f, IniDirs[6]);
while not EOF(f) do
begin
bFound := False;
Readln(f, s);
for i := 0 to IniMax do
if StrFirstWord(s) = IniItem[i] then
begin
IniVal[i] := StrLastWords(s);
bFound := True;
Break;
end;
if (not bFound) and (Length(Trim(s)) > 3) and (Length(IniHigherVersion) < 100) then
begin
SetLength(IniHigherVersion, Length(IniHigherVersion) + 1);
IniHigherVersion[Length(IniHigherVersion) - 1] := s;
end;
end;
finally
CloseFile(f);
end;
if bCopyFile then
begin
CopyFile(PChar(AppDataDir + 'Mandelbulb3D.ini') , PChar(AppFolder + 'Mandelbulb3D.ini'), True);
if FileExists(AppDataDir + IncludeTrailingPathDelimiter('Formulas') + 'FavouriteList.txt') then
CopyFile(PChar(AppDataDir + IncludeTrailingPathDelimiter('Formulas') + 'FavouriteList.txt') ,
PChar(IncludeTrailingPathDelimiter(IniDirs[3]) + 'FavouriteList.txt'), True);
end;
if IniVal[12] <> '' then IniDirs[7] := IniVal[12];
if IniVal[17] <> '' then IniDirs[8] := IniVal[17];
if IniVal[18] <> '' then IniDirs[9] := IniVal[18];
if IniVal[22] <> '' then IniDirs[10] := IniVal[22];
if IniVal[32] <> '' then IniDirs[11] := IniVal[32];
if IniVal[37] <> '' then IniDirs[12] := IniVal[37];
if not CheckAuthorValid(IniVal[33]) then IniVal[33] := '';
end;
end;
procedure SaveIni(ForceSave: LongBool);
var i: Integer;
f: TextFile;
s: String;
begin
s := AppFolder + 'Mandelbulb3D.ini';
if (not ForceSave) and (IniFileDate < GetFileModDate(s)) then Exit;
AssignFile(f, s);
try
IniVal[12] := IniDirs[7]; //m3l paras folder
IniVal[17] := IniDirs[8]; //BigRenders folder
IniVal[18] := IniDirs[9]; //LightMaps folder
IniVal[22] := IniDirs[10]; //voxel folder
IniVal[32] := IniDirs[11]; //m3c folder
IniVal[37] := IniDirs[12]; //meshes folder
IniVal[35] := TStyleManager.ActiveStyle.Name;
Rewrite(f);
for i := 0 to 6 do Writeln(f, IniDirs[i]);
for i := 0 to IniMax do
begin
if i = 0 then
s := IntToStr(FormsSticky[1] +
(Mand3DForm.bAniFormStick shl 2) +
(FormsSticky[0] shl 4) +
(FormsSticky[2]) shl 6)
else if i = 1 then s := Mand3DForm.Edit4.Text
else if i = 23 then
begin
if Mand3DForm.CheckBox13.Checked then s := 'Yes' else s := 'No';
end
else if i = 30 then
begin
if Mand3DForm.CheckBox14.Checked then s := 'Yes' else s := 'No';
end
else if i = 31 then
begin
if Mand3DForm.CheckBox16.Checked then s := 'Yes' else s := 'No';
end
else s := IniVal[i];
Writeln(f, IniItem[i] + ' ' + s);
end;
for i := 0 to Length(IniHigherVersion) - 1 do
Writeln(f, IniHigherVersion[i]);
finally
CloseFile(f);
end;
end;
procedure UpdatePresetFrom20(var p20: TLpreset20);
var i: Integer;
begin
with p20 do
begin
if Version < 4 then
begin
for i := 0 to 5 do if (Lights[i].Loption and 6) <> 2 then
Lights[i].Lamp := Word(SingleToShortFloat(1));
end;
if Version < 5 then Lights[3].FreeByte := 0;
Version := actPresetVersion;
end;
end;
function ConvertColPreset16To20(P16: TPLpreset16): TLpreset20;
var i: Integer;
begin
with Result do
begin
if P16.Version = 0 then
begin
AmbCol := InterpolateColorB(P16.Cols[3], P16.Cols[6], P16.Cols[9], 0.3333, 0.3333, 0.3333) or $FF000000;
AmbCol2 := AmbCol or $FF000000;
end else begin
AmbCol := P16.Cols[3] or $FF000000;
AmbCol2 := P16.Cols[6] or $FF000000;
end;
DepthCol := P16.DepthCol or $FF000000;
DepthCol2 := P16.DepthCol2;
for i := 0 to 2 do TB578pos[i] := P16.TB578pos[i];
Version := 3;
for i := 0 to 2 do
begin
LCols[i].Position := i * 10922;
LCols[i].ColorDif := P16.Cols[i * 3 + 1];
LCols[i].ColorSpe := P16.Cols[i * 3 + 2];
ICols[i].Position := i * 10922;
ICols[i].Color := P16.Cols[i * 3 + 1];
end;
for i := 3 to 9 do
begin
LCols[i] := LCols[0];
LCols[i].Position := 32200 + (i - 3) * 84;
end;
ICols[3] := ICols[0];
ICols[3].Position := 32700;
for i := 0 to 5 do Lights[i] := defaultLight8;
for i := 0 to 3 do
begin
Lights[i].Loption := P16.Lights[i].Loption;
Lights[i].LFunction := P16.Lights[i].LFunction;
Lights[i].Lcolor := CardinalToRGB(P16.Lights[i].Lcolor);
Lights[i].LightMapNr := 0;
Lights[i].LXpos := DoubleToD7B(P16.Lights[i].LXangle * Pid16384);
Lights[i].LYpos := DoubleToD7B(P16.Lights[i].LYangle * Pid16384);
end;
UpdatePresetFrom20(Result);
end;
end;
function ConvertColPreset164To20(var P164: TLpreset164): TLpreset20;
var i: Integer;
begin
with Result do
begin
AmbCol := P164.AmbTop or $FF000000;
AmbCol2 := P164.AmbBot or $FF000000;
DepthCol := P164.DepthCol or $FF000000;
DepthCol2 := P164.DepthCol2;
for i := 0 to 2 do TB578pos[i] := P164.TB578pos[i];
Version := 3;
for i := 0 to 3 do
begin
LCols[i].Position := i * 8191;
LCols[i].ColorDif := P164.ColDif[i];
LCols[i].ColorSpe := P164.ColSpec[i];
ICols[i].Position := i * 8191;
ICols[i].Color := P164.ColDif[i];
end;
for i := 4 to 9 do
begin
LCols[i] := LCols[0];
LCols[i].Position := 32200 + (i - 3) * 84;
end;
for i := 0 to 5 do Lights[i] := defaultLight8;
for i := 0 to 1 do
begin
Lights[i].Loption := P164.Lights[i].Loption;
Lights[i].LFunction := P164.Lights[i].LFunction;
Lights[i].Lcolor := CardinalToRGB(P164.Lights[i].Lcolor);
Lights[i].LightMapNr := 0;
Lights[i].LXpos := DoubleToD7B(P164.Lights[i].LXangle * Pid16384);
Lights[i].LYpos := DoubleToD7B(P164.Lights[i].LYangle * Pid16384);
end;
UpdatePresetFrom20(Result);
end;
end;
function LoadColPreset(nr: Integer): Boolean;
var s: String;
f: File;
i: Integer;
tP: TLpreset16;
begin
Result := False;
s := AppDataDir + 'color_preset_' + IntToStr(nr + 1) + '.bin';
if FileExists(s) then
begin
AssignFile(f, s);
try
Reset(f, 1);
if FileSize(f) < SizeOf(TLpreset20) then
begin
tP.Version := 0;
if FileSize(f) = SizeOf(TLpreset14) then
begin
BlockRead(f, tP, SizeOf(TLpreset14));
tP.DepthCol2 := tP.DepthCol;
end
else if FileSize(f) = SizeOf(TLpreset15) then
BlockRead(f, tP, SizeOf(TLpreset15))
else BlockRead(f, tP, SizeOf(TLpreset16));
if FileSize(f) < SizeOf(TLpreset16) then
for i := 0 to 3 do tP.Lights[i].LFunction :=
(tP.Lights[i].LFunction and 3) or
((tP.Lights[i].LFunction shl 2) and $30);
CustomPresets[nr + 6] := ConvertColPreset16To20(@tP);
end
else BlockRead(f, CustomPresets[nr + 6], SizeOf(TLpreset20));
UpdatePresetFrom20(CustomPresets[nr + 6]);
Result := True;
finally
CloseFile(f);
end;
end;
end;
procedure SaveColPreset(nr: Integer); //0..9 = CustomPresets[6..15]
var s: String;
f: File;
begin
s := AppDataDir + 'color_preset_' + IntToStr(nr + 1) + '.bin';
AssignFile(f, s);
try
Rewrite(f, 1);
CustomPresets[nr + 6].Version := actPresetVersion;
BlockWrite(f, CustomPresets[nr + 6], SizeOf(TLpreset20));
finally
CloseFile(f);
end;
end;
procedure LoadAccPreset(nr: Integer);
var s: String;
i: Integer;
f: TextFile;
P1: PInteger;
begin
s := AppDataDir + AccPresetFileNames[nr];
if FileExists(s) then
begin
AssignFile(f, s);
Reset(f);
try
P1 := PInteger(@AccPreset[nr].SmoothNormals);
for i := 0 to 5 do
begin
Readln(f, s);
Delete(s, 1, Length(AccPresetItemNames[i]) + 1);
if i = 1 then
begin
PDouble(P1)^ := StrToFloatK(s);
Inc(P1);
end
else P1^ := StrToIntTrim(s);
Inc(P1);
end;
if not EOF(f) then Readln(f, s) else s := '';
if Length(s) > 14 then
begin
Delete(s, 1, Length(AccPresetItemNames[6]) + 1);
AccPreset[nr].RayMultiplier := StrToFloatK(s);
end
else
begin
i := Max(2, Pinteger(@AccPreset[nr].RayMultiplier)^);
AccPreset[nr].RayMultiplier := 2.2 / (i + 0.25 * Sqr(i));
end;
if not EOF(f) then
begin
Readln(f, s);
Delete(s, 1, Length(AccPresetItemNames[7]) + 1);
AccPreset[nr].RayLimiter := StrToFloatK(s);
end;
ACCaddedStrings[nr] := 0;
while not EOF(f) do
begin
if ACCaddedStrings[nr] < 5 then
begin
Readln(f, ACCPresetFutureAddons[nr, ACCaddedStrings[nr]]);
Inc(ACCaddedStrings[nr]);
end;
end;
finally
CloseFile(f);
end;
end;
end;
procedure SaveAccPreset(nr: Integer);
var s: String;
i: Integer;
f: TextFile;
P1: PInteger;
begin
s := AppDataDir + AccPresetFileNames[nr];
AssignFile(f, s);
Rewrite(f);
try
P1 := PInteger(@AccPreset[nr].SmoothNormals);
for i := 0 to 5 do
begin
s := AccPresetItemNames[i] + ' ';
if i = 1 then
begin
s := s + FloatToStr(PDouble(P1)^);
Inc(P1);
end
else if i = 2 then s := s + IntToStr(Round(Sqrt(8.8 / AccPreset[nr].RayMultiplier + 4) - 2))
else s := s + IntToStr(P1^);
Inc(P1);
Writeln(f, s);
end;
Writeln(f, AccPresetItemNames[6] + ' ' + FloatToStrSingle(AccPreset[nr].RayMultiplier));
Writeln(f, AccPresetItemNames[7] + ' ' + FloatToStrSingle(AccPreset[nr].RayLimiter));
for i := 0 to ACCaddedStrings[nr] - 1 do Writeln(f, ACCPresetFutureAddons[nr, i]);
finally
CloseFile(f);
end;
end;
function MakeTextparas(para: TPMandHeader10; Titel: String): AnsiString;
var PB: PByte;
i, n: Integer;
p: TP6;
begin
SaveHeaderPointers(para, p);
InsertAuthorsToPara(para, Mand3DForm.Authors);
try
Result := 'Mandelbulb3Dv18{' + #13#10;
PB := @para.MandId;
for i := 1 to 280 do //280 * 3 = 840
begin
Result := Result + ThreeBytesTo4Chars(PB);
if (i mod 20) = 0 then Result := Result + #13#10;
Inc(PB, 3);
end;
finally
InsertHeaderPointers(para, p);
end;
PB := PByte(para.PCFAddon);
PTHeaderCustomAddon(PB).bHCAversion := 16; //HaddonVersion
if (PTHeaderCustomAddon(PB).bOptions1 and 3) = 1 then n := 1 else
begin
n := -1;
for i := 0 to 5 do
if PTHeaderCustomAddon(PB).Formulas[i].iItCount > 0 then n := i;
end;
PTHeaderCustomAddon(PB).iFCount := n + 1;
for i := 1 to ((n + 1) * SizeOf(THAformula) + 10) div 3 do
begin
Result := Result + ThreeBytesTo4Chars(PB);
if (i mod 20) = 0 then Result := Result + #13#10;
Inc(PB, 3);
end;
Result := Result + '}' + #13#10 + '{Titel: ' + Titel + '}' + #13#10;
end;
procedure CopyHeaderAsTextToClipBoard(para: TPMandHeader10; Titel: String);
begin
Clipboard.SetTextBuf(PWideChar(String(MakeTextparas(para, Titel))));
end;
function AnsiCopy(Text: AnsiString; Offset, Count: Integer): AnsiString;
var l: Integer;
begin
Result := '';
l := Length(Text);
if Offset < 1 then Offset := 1 else if Offset > l then Offset := l;
if Offset + Count > l + 1 then Count := l - Offset + 1;
if Count < 1 then Exit;
for l := 0 to Count - 1 do Result := Result + Text[Offset + l];
end;
function GetHeaderFromText(Text: AnsiString; var para: TMandHeader10; var Titel: String): LongBool;
var i, c, j, n, nup, l: Integer;
ver: Single;
PB: PByte;
s: AnsiString;
tmpHeader8: TMandHeader8;
tmpHeader10: TMandHeader10;
begin
Result := False;
Titel := '';
n := 1;
while n <= Length(Text) do //empty chars or 3-dot chars replacement
begin
if Text[n] = '}' then Break;
if Text[n] = ' ' then Delete(Text, n, 1) else