-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.pas
3386 lines (3066 loc) · 90.8 KB
/
main.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
//------------------------------------------------------------------------------
//
// DD_TERRAIN: Terrain Generator
// Copyright (C) 2020-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Main Form
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/DD-TERRAIN/
//------------------------------------------------------------------------------
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xTGA, jpeg, zBitmap, ComCtrls, ExtCtrls, Buttons, Menus, FileCtrl,
StdCtrls, AppEvnts, ExtDlgs, clipbrd, ToolWin, dglOpenGL, ter_class, ter_undo,
ter_filemenuhistory, ter_slider, PngImage1, ter_pk3, ter_colorpickerbutton,
ter_wadexport, ter_voxelexport, xTIFF, ImgList;
type
drawlayeritem_t = packed record
pass: byte;
color: integer;
end;
drawlayer_t = packed array[0..MAXTEXTURESIZE - 1, 0..MAXTEXTURESIZE - 1] of drawlayeritem_t;
drawlayer_p = ^drawlayer_t;
type
heightlayeritem_t = packed record
pass: boolean;
end;
heightlayer_t = packed array[0..MAXHEIGHTMAPSIZE - 1, 0..MAXHEIGHTMAPSIZE - 1] of heightlayeritem_t;
heightlayer_p = ^heightlayer_t;
type
colorbuffer_t = array[0..MAXTEXTURESIZE - 1, 0..MAXTEXTURESIZE - 1] of LongWord;
colorbuffer_p = ^colorbuffer_t;
const
MAXPENSIZE = 128;
MAXHEIGHTSIZE = 128;
const
MINTEXTURESCALE = 10;
MAXTEXTURESCALE = 400;
type
TForm1 = class(TForm)
ColorDialog1: TColorDialog;
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
Open2: TMenuItem;
Save1: TMenuItem;
Savesa1: TMenuItem;
N2: TMenuItem;
Exit1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
ApplicationEvents1: TApplicationEvents;
OpenDialog1: TOpenDialog;
Undo1: TMenuItem;
Redo1: TMenuItem;
OpenPictureDialog1: TOpenPictureDialog;
Timer1: TTimer;
StatusBar1: TStatusBar;
Options1: TMenuItem;
SavePictureDialog1: TSavePictureDialog;
MNExport1: TMenuItem;
ExportObjModel1: TMenuItem;
SaveDialog1: TSaveDialog;
N5: TMenuItem;
N8: TMenuItem;
CopyTexture1: TMenuItem;
OpenPictureDialog2: TOpenPictureDialog;
ToolBar1: TToolBar;
PropertiesPanel: TPanel;
Splitter1: TSplitter;
SaveAsButton1: TSpeedButton;
SaveButton1: TSpeedButton;
OpenButton1: TSpeedButton;
NewButton1: TSpeedButton;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
UndoButton1: TSpeedButton;
RedoButton1: TSpeedButton;
ToolButton4: TToolButton;
AboutButton1: TSpeedButton;
N7: TMenuItem;
HistoryItem0: TMenuItem;
HistoryItem1: TMenuItem;
HistoryItem2: TMenuItem;
HistoryItem3: TMenuItem;
HistoryItem4: TMenuItem;
HistoryItem5: TMenuItem;
HistoryItem6: TMenuItem;
HistoryItem7: TMenuItem;
HistoryItem8: TMenuItem;
HistoryItem9: TMenuItem;
EditPageControl: TPageControl;
TabSheet1: TTabSheet;
ExportScreenshot1: TMenuItem;
Wireframe1: TMenuItem;
Renderenviroment1: TMenuItem;
SaveDialog2: TSaveDialog;
MainPageControl: TPageControl;
TabSheet4: TTabSheet;
PaintScrollBox: TScrollBox;
PaintBox1: TPaintBox;
TabSheet5: TTabSheet;
OpenGLScrollBox: TScrollBox;
OpenGLPanel: TPanel;
OpenWADDialog: TOpenDialog;
Panel1: TPanel;
Label3: TLabel;
PenSizePaintBox: TPaintBox;
PenSizeLabel: TLabel;
OpacityLabel: TLabel;
OpacityPaintBox: TPaintBox;
Label2: TLabel;
Label1: TLabel;
HeightPaintBox: TPaintBox;
HeightLabel: TLabel;
PenSpeedButton1: TSpeedButton;
PenSpeedButton2: TSpeedButton;
PenSpeedButton3: TSpeedButton;
PenSpeedButton5: TSpeedButton;
PenSpeedButton6: TSpeedButton;
Bevel1: TBevel;
Bevel2: TBevel;
Label4: TLabel;
SmoothPaintBox: TPaintBox;
SmoothLabel: TLabel;
N1: TMenuItem;
PasteTexture1: TMenuItem;
PasteHeightmap1: TMenuItem;
PenSpeedButton4: TSpeedButton;
MNTools1: TMenuItem;
Scaleheightmap1: TMenuItem;
ExportWADFile1: TMenuItem;
SaveWADDialog: TSaveDialog;
N3: TMenuItem;
N4: TMenuItem;
Copy3dview1: TMenuItem;
CopyHeightmap1: TMenuItem;
PalettePopupMenu1: TPopupMenu;
PaletteDoom1: TMenuItem;
PaletteHeretic1: TMenuItem;
PaletteHexen1: TMenuItem;
PaletteStrife1: TMenuItem;
PaletteRadix1: TMenuItem;
N18: TMenuItem;
PaletteGreyScale1: TMenuItem;
PaletteDefault1: TMenuItem;
N6: TMenuItem;
N9: TMenuItem;
MNResampleHeightmapX2: TMenuItem;
Panel2: TPanel;
TexturePageControl: TPageControl;
WADTabSheet1: TTabSheet;
OpenWADMainPanel: TPanel;
Panel3: TPanel;
Pk3TabSheet: TTabSheet;
Panel12: TPanel;
Panel13: TPanel;
OpenPK3Dialog: TOpenDialog;
DirTabSheet: TTabSheet;
Panel4: TPanel;
Panel6: TPanel;
Import1: TMenuItem;
MNImportTexture1: TMenuItem;
MNImportHeightmap1: TMenuItem;
TextureScaleResetLabel: TLabel;
TextureScalePaintBox: TPaintBox;
TextureScaleLabel: TLabel;
ImageList1: TImageList;
ColorTabSheet: TTabSheet;
SelectColorBackPanel: TPanel;
Panel29: TPanel;
ExportHeightmap1: TMenuItem;
SavePictureDialog2: TSavePictureDialog;
ToolButton5: TToolButton;
ExportWADButton1: TSpeedButton;
N10: TMenuItem;
MNExpoortTexture1: TMenuItem;
SavePictureDialog3: TSavePictureDialog;
MNExportVoxel1: TMenuItem;
Panel5: TPanel;
WADPageControl1: TPageControl;
WADFlatsTabSheet: TTabSheet;
Panel7: TPanel;
WADFlatListPanel: TPanel;
Panel28: TPanel;
WADFlatsListBox: TListBox;
WADPreviewFlatPanel: TPanel;
Panel8: TPanel;
Panel9: TPanel;
Panel10: TPanel;
WADFlatPreviewImage: TImage;
Panel11: TPanel;
FlatSizeLabel: TLabel;
WAADFlatNameLabel: TLabel;
Panel30: TPanel;
PaletteSpeedButton1: TSpeedButton;
SelectWADFileButton: TSpeedButton;
WADFileNameEdit: TEdit;
Label23: TLabel;
Panel31: TPanel;
Label5: TLabel;
PK3FileNameEdit: TEdit;
SelectPK3FileButton: TSpeedButton;
Panel32: TPanel;
Label6: TLabel;
DIRFileNameEdit: TEdit;
SelectDIRFileButton: TSpeedButton;
Panel33: TPanel;
ColorPanel1: TPanel;
PickColorRGBLabel: TLabel;
Panel15: TPanel;
PK3PageControl: TPageControl;
PK3TexturesTabSheet: TTabSheet;
Panel16: TPanel;
PK3TextureListPanel: TPanel;
Panel34: TPanel;
PK3TexListBox: TListBox;
PK3PreviewTexturePanel: TPanel;
Panel18: TPanel;
Panel19: TPanel;
Panel20: TPanel;
PK3TexPreviewImage: TImage;
Panel21: TPanel;
PK3TexSizeLabel: TLabel;
PK3TextureNameLabel: TLabel;
Panel14: TPanel;
DirPageControl: TPageControl;
DirTexturesTabSheet: TTabSheet;
Panel17: TPanel;
DIRTextureListPanel: TPanel;
Panel36: TPanel;
DIRTexListBox: TListBox;
DIRPreviewTexturePanel: TPanel;
Panel22: TPanel;
Panel23: TPanel;
Panel24: TPanel;
DIRTexPreviewImage: TImage;
Panel25: TPanel;
DIRTexSizeLabel: TLabel;
DIRTextureNameLabel: TLabel;
Panel26: TPanel;
ColorPalettePageControl: TPageControl;
ColorPaletteTabSheet: TTabSheet;
Panel27: TPanel;
PickColorPalettePanel: TPanel;
ColorPaletteImage: TImage;
WADPatchesTabSheet: TTabSheet;
Panel35: TPanel;
WADPatchListPanel: TPanel;
Panel37: TPanel;
WADPatchListBox: TListBox;
WADPreviewPatchPanel: TPanel;
Panel38: TPanel;
Panel39: TPanel;
Panel40: TPanel;
WADPatchPreviewImage: TImage;
Panel41: TPanel;
WADPatchSizeLabel: TLabel;
WADPatchNameLabel: TLabel;
Onlinedocumentation1: TMenuItem;
SetHeightmapValuestoZero1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure NewButton1Click(Sender: TObject);
procedure SaveButton1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure AboutButton1Click(Sender: TObject);
procedure SaveAsButton1Click(Sender: TObject);
procedure ExitButton1Click(Sender: TObject);
procedure OpenButton1Click(Sender: TObject);
procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
procedure OpenGLPanelResize(Sender: TObject);
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
procedure OpenGLPanelMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure OpenGLPanelMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure OpenGLPanelMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure OpenGLPanelDblClick(Sender: TObject);
procedure Edit1Click(Sender: TObject);
procedure Undo1Click(Sender: TObject);
procedure Redo1Click(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure File1Click(Sender: TObject);
procedure CopyTexture1Click(Sender: TObject);
procedure Options1Click(Sender: TObject);
procedure Wireframe1Click(Sender: TObject);
procedure TrunkImageDblClick(Sender: TObject);
procedure Renderenviroment1Click(Sender: TObject);
procedure ExportObjModel1Click(Sender: TObject);
procedure ExportScreenshot1Click(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure SelectWADFileButtonClick(Sender: TObject);
procedure WADFlatsListBoxClick(Sender: TObject);
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure PenSpeedButton1Click(Sender: TObject);
procedure PenSpeedButton2Click(Sender: TObject);
procedure PenSpeedButton3Click(Sender: TObject);
procedure PenSpeedButton4Click(Sender: TObject);
procedure PenSpeedButton5Click(Sender: TObject);
procedure PenSpeedButton6Click(Sender: TObject);
procedure PasteTexture1Click(Sender: TObject);
procedure PasteHeightmap1Click(Sender: TObject);
procedure Scaleheightmap1Click(Sender: TObject);
procedure ExportWADFile1Click(Sender: TObject);
procedure Copy3dview1Click(Sender: TObject);
procedure CopyHeightmap1Click(Sender: TObject);
procedure PaletteSpeedButton1Click(Sender: TObject);
procedure PaletteDefault1Click(Sender: TObject);
procedure PaletteDoom1Click(Sender: TObject);
procedure PaletteHeretic1Click(Sender: TObject);
procedure PaletteHexen1Click(Sender: TObject);
procedure PaletteStrife1Click(Sender: TObject);
procedure PaletteRadix1Click(Sender: TObject);
procedure PaletteGreyScale1Click(Sender: TObject);
procedure PalettePopupMenu1Popup(Sender: TObject);
procedure MNTools1Click(Sender: TObject);
procedure MNResampleHeightmapX2Click(Sender: TObject);
procedure SelectPK3FileButtonClick(Sender: TObject);
procedure PK3TexListBoxClick(Sender: TObject);
procedure MNImportTexture1Click(Sender: TObject);
procedure MNImportHeightmap1Click(Sender: TObject);
procedure DIRTexListBoxClick(Sender: TObject);
procedure SelectDIRFileButtonClick(Sender: TObject);
procedure Splitter1Moved(Sender: TObject);
procedure WADFileNameEditChange(Sender: TObject);
procedure PK3FileNameEditChange(Sender: TObject);
procedure DIRFileNameEditChange(Sender: TObject);
procedure TexturePageControlChange(Sender: TObject);
procedure PickColorPalettePanelCanResize(Sender: TObject; var NewWidth,
NewHeight: Integer; var Resize: Boolean);
procedure ColorPaletteImageMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ColorPaletteImageMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
procedure ColorPaletteImageMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure ExportHeightmap1Click(Sender: TObject);
procedure TextureScaleResetLabelDblClick(Sender: TObject);
procedure MNExpoortTexture1Click(Sender: TObject);
procedure MNExportVoxel1Click(Sender: TObject);
procedure WADPatchListBoxClick(Sender: TObject);
procedure WADPageControl1Change(Sender: TObject);
procedure Onlinedocumentation1Click(Sender: TObject);
procedure SetHeightmapValuestoZero1Click(Sender: TObject);
private
{ Private declarations }
ffilename: string;
fwadfilename: string;
fpalettename: string;
fpk3filename: string;
fpk3reader: TZipFile;
fdirdirectory: string;
fdirlist: TStringList;
fexportwadoptions: exportwadoptions_t;
fexportvoxeloptions: exportvoxeloptions_t;
fdrawcolor: TColor;
lpickcolormousedown: boolean;
drawlayer: drawlayer_p;
heightlayer: heightlayer_p;
colorbuffersize: integer;
colorbuffer: colorbuffer_p;
changed: Boolean;
terrain: TTerrain;
rc: HGLRC; // Rendering Context
dc: HDC; // Device Context
glpanx, glpany: integer;
glmousedown: integer;
undoManager: TUndoRedoManager;
filemenuhistory: TFileMenuHistory;
glneedsupdate: boolean;
glneedstexturerecalc: boolean;
fopacity: integer;
fpensize: integer;
ftexturescale: integer;
fheightsize: integer;
fsmoothfactor: integer;
foldopacity: integer;
foldpensize: integer;
OpacitySlider: TSliderHook;
TextureScaleSlider: TSliderHook;
PenSizeSlider: TSliderHook;
HeightSlider: TSliderHook;
SmoothSlider: TSliderHook;
closing: boolean;
lmousedown: boolean;
lmousedownx, lmousedowny: integer;
lmouseheightmapx, lmouseheightmapy: integer;
hmouseheightmapx, hmouseheightmapy: integer;
lasthmouseheightmapx, lasthmouseheightmapy: integer;
pen2mask: array[-MAXPENSIZE div 2..MAXPENSIZE div 2, -MAXPENSIZE div 2..MAXPENSIZE div 2] of integer;
pen3mask: array[-MAXPENSIZE div 2..MAXPENSIZE div 2, -MAXPENSIZE div 2..MAXPENSIZE div 2] of integer;
bitmapbuffer: TBitmap;
savebitmapundo: boolean;
ColorPickerButton1: TColorPickerButton;
procedure Idle(Sender: TObject; var Done: Boolean);
function CheckCanClose: boolean;
procedure DoNewTerrain(const tsize, hsize: integer);
procedure DoSaveTerrain(const fname: string);
function DoLoadTerrain(const fname: string): boolean;
procedure SetFileName(const fname: string);
procedure DoLoadTerrainBinaryUndo(s: TStream);
procedure DoSaveTerrainBinaryUndo(s: TStream);
procedure SaveUndo(const dosavebitmap: boolean);
procedure UpdateStausbar;
procedure UpdateEnable;
procedure OnLoadTerrainFileMenuHistory(Sender: TObject; const fname: string);
procedure DoRenderGL;
procedure Get3dPreviewBitmap(const b: TBitmap);
procedure SlidersToLabels;
procedure TerrainToControls;
procedure UpdateSliders;
procedure UpdateFromSliders(Sender: TObject);
procedure BitmapToColorBuffer(const abitmap: TBitmap);
procedure PopulateFlatsListBox(const wadname: string);
procedure NotifyFlatsListBox;
function GetWADFlatAsBitmap(const fwad: string; const flat: string): TBitmap;
procedure PopulateWADPatchListBox(const wadname: string);
procedure NotifyWADPatchListBox;
function GetWADPatchAsBitmap(const fwad: string; const patch: string): TBitmap;
procedure PopulatePK3ListBox(const pk3name: string);
procedure NotifyPK3ListBox;
function GetPK3TexAsBitmap(const tname: string): TBitmap;
procedure PopulateDirListBox;
procedure NotifyDIRListBox;
function DIRTexListBoxNameSize: integer;
procedure LLeftMousePaintAt(const X, Y: integer);
procedure LLeftMousePaintTo(const X, Y: integer);
procedure CalcPenMasks;
procedure DoRefreshPaintBox(const r: TRect);
procedure CheckPaletteName;
procedure GetHeighmapFromBitmap(const tempBitmap1: TBitmap);
procedure ChangeListHint(const lst: TListBox; const def: string);
procedure ColorPickerButton1Click(Sender: TObject);
procedure ColorPickerButton1Change(Sender: TObject);
procedure NotifyColor;
procedure RecreateColorPickPalette;
procedure PickColorPalette(const X, Y: integer);
protected
// Made protected to avoid "Private symbol never used" warning.
function DIRTexEditNameSize: integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
ter_gl,
ter_defs,
ter_utils,
frm_newterrain,
frm_zeroheightmapvalues,
ter_wadreader,
frm_editheightmapitem,
frm_scaleheightmap,
ter_palettes,
frm_loadimagehelper,
ter_colorpalettebmz,
ter_cursors,
frm_exportwadmap,
ter_voxels,
frm_exportvoxel,
ter_doomdata,
ter_doomutils,
ter_wad;
{$R *.dfm}
resourcestring
rsTitle = 'Terrain Generator';
// Helper function
procedure ClearList(const lst: TStringList);
var
i: integer;
begin
for i := 0 to lst.Count - 1 do
lst.Objects[i].Free;
lst.Clear;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
pfd: TPIXELFORMATDESCRIPTOR;
pf: Integer;
doCreate: boolean;
i: integer;
begin
Screen.Cursor := crHourglass;
Randomize;
CreateCustomCursors;
PaintBox1.Cursor := crPaint;
colorbuffer := nil;
DoubleBuffered := True;
for i := 0 to ComponentCount - 1 do
if Components[i].InheritsFrom(TWinControl) then
if not (Components[i] is TListBox) then
(Components[i] as TWinControl).DoubleBuffered := True;
fexportwadoptions.engine := ENGINE_RAD;
fexportwadoptions.game := GAME_RADIX;
fexportwadoptions.levelname := 'E1M1';
fexportwadoptions.palette := @RadixPaletteRaw;
fexportwadoptions.defsidetex := 'RDXW0012';
fexportwadoptions.defceilingtex := 'F_SKY1';
fexportwadoptions.lowerid := 1255;
fexportwadoptions.raiseid := 1254;
fexportwadoptions.flags := ETF_CALCDXDY or ETF_TRUECOLORFLAT or ETF_MERGEFLATSECTORS or ETF_ADDPLAYERSTART or ETF_EXPORTFLAT;
fexportwadoptions.elevationmethod := ELEVATIONMETHOD_SLOPES;
fexportwadoptions.defceilingheight := 512;
fexportwadoptions.deflightlevel := 192;
fexportwadoptions.layerstep := 24;
fexportvoxeloptions.size := 256;
fexportvoxeloptions.minz := 0;
fexportvoxeloptions.maxz := 255;
bitmapbuffer := TBitmap.Create;
bitmapbuffer.PixelFormat := pf32bit;
ColorPickerButton1 := TColorPickerButton.Create(nil);
ColorPickerButton1.Parent := ColorPanel1;
ColorPickerButton1.Align := alClient;
ColorPickerButton1.OnClick := ColorPickerButton1Click;
ColorPickerButton1.OnChange := ColorPickerButton1Change;
fdrawcolor := RGB(255, 255, 255);
lpickcolormousedown := False;
NotifyColor;
RecreateColorPickPalette;
ter_LoadSettingFromFile(ChangeFileExt(ParamStr(0), '.ini'));
closing := False;
EditPageControl.ActivePageIndex := 0;
TexturePageControl.ActivePageIndex := 0;
MainPageControl.ActivePageIndex := 0;
WADPageControl1.ActivePageIndex := 0;
GetMem(drawlayer, SizeOf(drawlayer_t));
GetMem(heightlayer, SizeOf(heightlayer_t));
GetMem(colorbuffer, SizeOf(colorbuffer_t));
FillChar(colorbuffer^, SizeOf(colorbuffer_t), 255);
colorbuffersize := 128;
// Set palette
fpalettename := bigstringtostring(@opt_defaultpalette);
CheckPaletteName;
// Open WAD resource
fwadfilename := bigstringtostring(@opt_lastwadfile);
if FileExists(fwadfilename) and (fwadfilename <> '') then
begin
WADFileNameEdit.Text := ExtractFileName(fwadfilename);
PopulateFlatsListBox(fwadfilename);
PopulateWADPatchListBox(fwadfilename);
end
else
fwadfilename := '';
// Open PK3 resource
fpk3filename := bigstringtostring(@opt_lastpk3file);
if FileExists(fpk3filename) and (fpk3filename <> '') then
begin
fpk3reader := TZipFile.Create(fpk3filename);
PK3FileNameEdit.Text := ExtractFileName(fpk3filename);
PopulatePK3ListBox(fpk3filename);
end
else
fpk3filename := '';
// Open Directory resource
fdirdirectory := bigstringtostring(@opt_lastdirectory);
fdirlist := TStringList.Create;
// DIRFileNameEdit.Text := MkShortName(fdirdirectory, DIRTexEditNameSize);
DIRFileNameEdit.Text := fdirdirectory;
PopulateDirListBox;
lmousedown := False;
lmousedownx := 0;
lmousedowny := 0;
lmouseheightmapx := 0;
lmouseheightmapy := 0;
hmouseheightmapx := 0;
hmouseheightmapy := 0;
lasthmouseheightmapx := -1;
lasthmouseheightmapy := -1;
fopacity := 100;
fpensize := 64;
ftexturescale := 100;
fheightsize := 64;
fsmoothfactor := 50;
foldopacity := -1;
foldpensize := -1;
savebitmapundo := true;
CalcPenMasks;
undoManager := TUndoRedoManager.Create;
undoManager.UndoLimit := 100;
undoManager.OnLoadFromStream := DoLoadTerrainBinaryUndo;
undoManager.OnSaveToStream := DoSaveTerrainBinaryUndo;
filemenuhistory := TFileMenuHistory.Create(self);
filemenuhistory.MenuItem0 := HistoryItem0;
filemenuhistory.MenuItem1 := HistoryItem1;
filemenuhistory.MenuItem2 := HistoryItem2;
filemenuhistory.MenuItem3 := HistoryItem3;
filemenuhistory.MenuItem4 := HistoryItem4;
filemenuhistory.MenuItem5 := HistoryItem5;
filemenuhistory.MenuItem6 := HistoryItem6;
filemenuhistory.MenuItem7 := HistoryItem7;
filemenuhistory.MenuItem8 := HistoryItem8;
filemenuhistory.MenuItem9 := HistoryItem9;
filemenuhistory.OnOpen := OnLoadTerrainFileMenuHistory;
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory9));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory8));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory7));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory6));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory5));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory4));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory3));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory2));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory1));
filemenuhistory.AddPath(bigstringtostring(@opt_filemenuhistory0));
terrain := TTerrain.Create;
Scaled := False;
OpenGLPanel.Width := 3 * Screen.Width div 4;
OpenGLPanel.Height := 3 * Screen.Height div 4;
OpenGLPanel.DoubleBuffered := True;
glpanx := 0;
glpany := 0;
glmousedown := 0;
InitOpenGL;
ReadExtensions;
ReadImplementationProperties;
// OpenGL initialisieren
dc := GetDC(OpenGLPanel.Handle);
// PixelFormat
pfd.nSize := SizeOf(pfd);
pfd.nVersion := 1;
pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
pfd.iPixelType := PFD_TYPE_RGBA; // PFD_TYPE_RGBA or PFD_TYPEINDEX
pfd.cColorBits := 32;
pf := ChoosePixelFormat(dc, @pfd); // Returns format that most closely matches above pixel format
SetPixelFormat(dc, pf, @pfd);
rc := wglCreateContext(dc); // Rendering Context = window-glCreateContext
wglMakeCurrent(dc, rc); // Make the DC (Form1) the rendering Context
// Initialize GL environment variables
glInit;
ResetCamera;
OpenGLPanelResize(sender); // sets up the perspective
terraintexture := gld_CreateTexture(terrain.Texture, False);
glneedsupdate := True;
glneedstexturerecalc := True;
TabSheet1.DoubleBuffered := True;
OpacitySlider := TSliderHook.Create(OpacityPaintBox);
OpacitySlider.Min := 1;
OpacitySlider.Max := 100;
PenSizeSlider := TSliderHook.Create(PenSizePaintBox);
PenSizeSlider.Min := 1;
PenSizeSlider.Max := MAXPENSIZE;
TextureScaleSlider := TSliderHook.Create(TextureScalePaintBox);
TextureScaleSlider.Min := MINTEXTURESCALE;
TextureScaleSlider.Max := MAXTEXTURESCALE;
HeightSlider := TSliderHook.Create(HeightPaintBox);
HeightSlider.Min := -MAXHEIGHTSIZE;
HeightSlider.Max := MAXHEIGHTSIZE;
SmoothSlider := TSliderHook.Create(SmoothPaintBox);
SmoothSlider.Min := 0;
SmoothSlider.Max := 100;
doCreate := True;
if ParamCount > 0 then
if DoLoadTerrain(ParamStr(1)) then
doCreate := False;
if DoCreate then
begin
SetFileName('');
DoNewTerrain(1024, 17);
glneedsupdate := True;
glneedstexturerecalc := True;
undoManager.Clear;
end;
NotifyWADPatchListBox;
NotifyFlatsListBox; // This must be placed here to use the flats for drawing at startup.
// when the app has spare time, render the GL scene
Application.OnIdle := Idle;
Screen.Cursor := crDefault;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := CheckCanClose;
end;
function TForm1.CheckCanClose: boolean;
var
ret: integer;
begin
if changed then
begin
ret := MessageBox(Handle, 'Do you want to save changes?', PChar(rsTitle), MB_YESNOCANCEL or MB_ICONQUESTION or MB_APPLMODAL);
if ret = IDCANCEL then
begin
Result := False;
exit;
end;
if ret = IDNO then
begin
Result := True;
exit;
end;
if ret = IDYES then
begin
SaveButton1Click(self);
Result := not changed;
exit;
end;
end;
Result := True;
end;
procedure TForm1.NewButton1Click(Sender: TObject);
var
tsize, hsize: integer;
begin
if not CheckCanClose then
Exit;
tsize := terrain.texturesize;
hsize := terrain.heightmapsize;
if GetNewTerrainSize(tsize, hsize) then
begin
DoNewTerrain(tsize, hsize);
ResetCamera;
end;
end;
procedure TForm1.DoNewTerrain(const tsize, hsize: integer);
begin
SetFileName('');
changed := False;
terrain.Clear(tsize, hsize);
PaintBox1.Width := tsize;
PaintBox1.Height := tsize;
bitmapbuffer.Width := tsize;
bitmapbuffer.Height := tsize;
TerrainToControls;
glneedsupdate := True;
glneedstexturerecalc := True;
undoManager.Clear;
end;
procedure TForm1.SetFileName(const fname: string);
begin
ffilename := fname;
Caption := rsTitle;
if ffilename <> '' then
Caption := Caption + ' - ' + MkShortName(ffilename);
end;
procedure TForm1.SaveButton1Click(Sender: TObject);
begin
if ffilename = '' then
begin
if SaveDialog1.Execute then
begin
ffilename := SaveDialog1.FileName;
filemenuhistory.AddPath(ffilename);
end
else
begin
Beep;
Exit;
end;
end;
BackupFile(ffilename);
DoSaveTerrain(ffilename);
end;
procedure TForm1.DoSaveTerrain(const fname: string);
begin
SetFileName(fname);
Screen.Cursor := crHourglass;
try
terrain.SaveToFile(fname);
finally
Screen.Cursor := crDefault;
end;
changed := False;
end;
function TForm1.DoLoadTerrain(const fname: string): boolean;
var
s: string;
begin
if not FileExists(fname) then
begin
s := Format('File %s does not exist!', [MkShortName(fname)]);
MessageBox(Handle, PChar(s), PChar(rsTitle), MB_OK or MB_ICONEXCLAMATION or MB_APPLMODAL);
Result := False;
exit;
end;
undoManager.Clear;
Screen.Cursor := crHourglass;
try
terrain.LoadFromFile(fname);
finally
Screen.Cursor := crDefault;
end;
PaintBox1.Width := terrain.texturesize;
PaintBox1.Height := terrain.texturesize;
bitmapbuffer.Width := terrain.texturesize;
bitmapbuffer.Height := terrain.texturesize;
TerrainToControls;
filemenuhistory.AddPath(fname);
SetFileName(fname);
glneedsupdate := True;
glneedstexturerecalc := True;
changed := False;
Result := True;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
closing := True;
Timer1.Enabled := False;
undoManager.Free;
wglMakeCurrent(0, 0);
wglDeleteContext(rc);
glDeleteTextures(1, @terraintexture);
stringtobigstring(filemenuhistory.PathStringIdx(0), @opt_filemenuhistory0);
stringtobigstring(filemenuhistory.PathStringIdx(1), @opt_filemenuhistory1);
stringtobigstring(filemenuhistory.PathStringIdx(2), @opt_filemenuhistory2);
stringtobigstring(filemenuhistory.PathStringIdx(3), @opt_filemenuhistory3);
stringtobigstring(filemenuhistory.PathStringIdx(4), @opt_filemenuhistory4);
stringtobigstring(filemenuhistory.PathStringIdx(5), @opt_filemenuhistory5);
stringtobigstring(filemenuhistory.PathStringIdx(6), @opt_filemenuhistory6);
stringtobigstring(filemenuhistory.PathStringIdx(7), @opt_filemenuhistory7);
stringtobigstring(filemenuhistory.PathStringIdx(8), @opt_filemenuhistory8);
stringtobigstring(filemenuhistory.PathStringIdx(9), @opt_filemenuhistory9);
stringtobigstring(fwadfilename, @opt_lastwadfile);
stringtobigstring(fpalettename, @opt_defaultpalette);
stringtobigstring(fpk3filename, @opt_lastpk3file);
stringtobigstring(fdirdirectory, @opt_lastdirectory);
ter_SaveSettingsToFile(ChangeFileExt(ParamStr(0), '.ini'));
filemenuhistory.Free;
OpacitySlider.Free;
TextureScaleSlider.Free;
PenSizeSlider.Free;
HeightSlider.Free;
SmoothSlider.Free;
terrain.Free;
Freemem(drawlayer, SizeOf(drawlayer_t));
Freemem(heightlayer, SizeOf(heightlayer_t));
Freemem(colorbuffer, SizeOf(colorbuffer_t));
bitmapbuffer.Free;
fpk3reader.Free;
ClearList(fdirlist);
fdirlist.Free;
ColorPickerButton1.Free;
PaintBox1.Cursor := crDefault;
DeleteCustomCursors;
end;
resourcestring
copyright = 'Copyright (c) 2020-2021, Jim Valavanis';
procedure TForm1.AboutButton1Click(Sender: TObject);
begin
MessageBox(
Handle,
PChar(Format('%s'#13#10 +
'Version ' + I_VersionBuilt + #13#10 +
'Copyright (c) 2020-2022, [email protected]'#13#10 +
#13#10'A tool to create Terrains.'#13#10#13#10,
[rsTitle])),
PChar(rsTitle),
MB_OK or MB_ICONINFORMATION or MB_APPLMODAL);
end;
procedure TForm1.SaveAsButton1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
filemenuhistory.AddPath(SaveDialog1.FileName);
BackupFile(SaveDialog1.FileName);
DoSaveTerrain(SaveDialog1.FileName);
end;
end;
procedure TForm1.ExitButton1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.OpenButton1Click(Sender: TObject);
begin
if not CheckCanClose then
Exit;
if OpenDialog1.Execute then