DEADSOFTWARE

gui: implement new settings dialog
[d2df-editor.git] / src / editor / Editor.lpr
1 program Editor;
3 {$INCLUDE ../shared/a_modes.inc}
5 uses
6 {$IFDEF DARWIN}
7 MacOSAll, CocoaAll,
8 {$ENDIF}
9 Forms, Interfaces, Dialogs,
10 GL, GLExt, SysUtils,
11 e_graphics in '../engine/e_graphics.pas',
12 e_log in '../engine/e_log.pas',
13 e_textures in '../engine/e_textures.pas',
14 MAPSTRUCT in '../shared/MAPSTRUCT.pas',
15 MAPREADER in '../shared/MAPREADER.pas',
16 MAPWRITER in '../shared/MAPWRITER.pas',
17 MAPDEF in '../shared/MAPDEF.pas',
18 WADEDITOR in '../shared/WADEDITOR.pas',
19 WADSTRUCT in '../shared/WADSTRUCT.pas',
20 CONFIG in '../shared/CONFIG.pas',
21 xstreams in '../shared/xstreams.pas',
22 dfzip in '../shared/dfzip.pas',
23 sfs in '../sfs/sfs.pas',
24 sfsPlainFS in '../sfs/sfsPlainFS.pas',
25 sfsZipFS in '../sfs/sfsZipFS.pas',
27 f_about in 'f_about.pas' {AboutForm},
28 f_options in 'f_options.pas' {OptionsForm},
29 f_main in 'f_main.pas' {MainForm},
30 g_map in 'g_map.pas',
31 f_mapoptions in 'f_mapoptions.pas' {MapOptionsForm},
32 f_activationtype in 'f_activationtype.pas' {ActivationTypeForm},
33 f_addresource in 'f_addresource.pas' {AddResourceForm},
34 f_keys in 'f_keys.pas' {KeysForm},
35 f_mapcheck in 'f_mapcheck.pas' {MapCheckForm},
36 f_mapoptimization in 'f_mapoptimization.pas' {MapOptimizationForm},
37 g_basic in 'g_basic.pas',
38 g_textures in 'g_textures.pas',
39 f_addresource_texture in 'f_addresource_texture.pas' {AddTextureForm},
40 f_savemap in 'f_savemap.pas' {SaveMapForm},
41 f_selectmap in 'f_selectmap.pas' {SelectMapForm},
42 f_addresource_sky in 'f_addresource_sky.pas' {AddSkyForm},
43 f_addresource_sound in 'f_addresource_sound.pas' {AddSoundForm},
44 spectrum in 'spectrum.pas',
45 f_saveminimap in 'f_saveminimap.pas' {SaveMiniMapForm},
46 f_packmap in 'f_packmap.pas' {PackMapForm},
47 f_choosetype in 'f_choosetype.pas' {ChooseTypeForm},
48 {$IFNDEF NOSOUND}
49 fmod,
50 fmoderrors,
51 fmodpresets,
52 fmodtypes,
53 {$ENDIF}
54 ImagingTypes,
55 Imaging,
56 ImagingUtility,
57 g_options in 'g_options.pas',
58 g_language in 'g_language.pas',
59 f_selectlang in 'f_selectlang.pas' {SelectLanguageForm};
61 {$IFDEF WINDOWS}
62 {$R *.res}
63 {$ENDIF}
65 type
66 THandlerObject = class (TObject)
67 procedure ExceptionHandler (Sender: TObject; e: Exception);
68 end;
70 var
71 LogFileName: AnsiString = '';
72 ParamFileIndex: Integer = 1;
74 {$IFDEF DARWIN}
75 function NSStringToAnsiString (s: NSString): AnsiString;
76 var i: Integer;
77 begin
78 result := '';
79 for i := 0 to s.length - 1 do
80 result := result + AnsiChar(s.characterAtIndex(i));
81 end;
83 function GetBundlePath (): AnsiString;
84 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
85 begin
86 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
87 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
88 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
89 CFRelease(pathRef);
90 CFRelease(pathCFStr);
91 Result := pathStr;
92 end;
93 {$ENDIF}
95 procedure THandlerObject.ExceptionHandler (Sender: TObject; e: Exception);
96 begin
97 e_WriteStackTrace(e.message);
98 MessageDlg('Unhandled exception: ' + e.message + ' (see Editor.log for more information)', mtError, [mbOK], 0);
99 end;
101 procedure CheckParamOptions;
102 var i: Integer; p: AnsiString;
103 begin
104 i := 1;
105 while (i <= ParamCount) and (Length(ParamStr(i)) > 0) and (ParamStr(i)[1] = '-') do
106 begin
107 p := ParamStr(i);
108 if p = '--log-file' then
109 begin
110 if i + 1 <= ParamCount then
111 begin
112 Inc(i);
113 LogFileName := ParamStr(i);
114 end;
115 end
116 else if p = '--config' then
117 begin
118 if i + 1 <= ParamCount then
119 begin
120 Inc(i);
121 CfgFileName := ParamStr(i);
122 end;
123 end
124 else if p = '--game-wad' then
125 begin
126 if i + 1 <= ParamCount then
127 begin
128 Inc(i);
129 GameWad := ParamStr(i);
130 end;
131 end
132 else if p = '--editor-wad' then
133 begin
134 if i + 1 <= ParamCount then
135 begin
136 Inc(i);
137 EditorWad := ParamStr(i);
138 end;
139 end
140 else if p = '--wads-dir' then
141 begin
142 if i + 1 <= ParamCount then
143 begin
144 Inc(i);
145 WadsDir := ParamStr(i);
146 end;
147 end;
148 Inc(i);
149 end;
150 ParamFileIndex := i;
151 end;
153 procedure CheckParamFiles;
154 var i: Integer;
155 begin
156 i := ParamFileIndex;
157 if i <= ParamCount then
158 StartMap := ParamStr(i);
159 end;
161 procedure InitPathes;
162 {$IFDEF DARWIN}
163 var BundlePath, DFPath, DocPath: AnsiString; ns: NSString;
164 var ApplicationSupportDirs, DocumentDirs: NSArray;
165 var count: Integer;
166 {$ELSE}
167 var EditorDir: AnsiString;
168 {$ENDIF}
169 begin
170 {$IFDEF DARWIN}
171 BundlePath := GetBundlePath();
172 ApplicationSupportDirs := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
173 count := ApplicationSupportDirs.count;
174 ns := ApplicationSupportDirs.objectAtIndex(count - 1);
175 DFPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
176 DocumentDirs := NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
177 count := DocumentDirs.count;
178 ns := DocumentDirs.objectAtIndex(count - 1);
179 DocPath := NSStringToAnsiString(ns) + DirectorySeparator + 'Doom 2D Forever';
180 GameExeFile := 'Doom 2D Forever.app';
181 CfgFileName := DFPath + DirectorySeparator + 'Editor.cfg';
182 LogFileName := DFPath + DirectorySeparator + 'Editor.log';
183 MapsDir := DocPath + DirectorySeparator + 'Maps';
184 WadsDir := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'wads';
185 GameWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
186 EditorWad := BundlePath + DirectorySeparator + 'Contents' + DirectorySeparator + 'Resources' + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
187 {$ELSE}
188 EditorDir := ExtractFilePath(Application.ExeName);
189 {$IFDEF WINDOWS}
190 GameExeFile := 'Doom2DF.exe';
191 {$ELSE}
192 GameExeFile := 'Doom2DF';
193 {$ENDIF}
194 CfgFileName := EditorDir + DirectorySeparator + 'Editor.cfg';
195 LogFileName := EditorDir + DirectorySeparator + 'Editor.log';
196 MapsDir := EditorDir + DirectorySeparator + 'maps';
197 WadsDir := EditorDir + DirectorySeparator + 'wads';
198 GameWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'game.wad';
199 EditorWad := EditorDir + DirectorySeparator + 'data' + DirectorySeparator + 'editor.wad';
200 {$ENDIF}
201 ForceDirectories(MapsDir);
202 ForceDirectories(WadsDir);
203 end;
205 procedure InitLogs;
206 begin
207 e_InitLog(LogFileName, WM_NEWFILE);
209 {$IF DECLARED(UseHeapTrace)}
210 (* http://wiki.freepascal.org/heaptrc *)
211 GlobalSkipIfNoLeaks := True;
212 //SetHeapTraceOutput('EditorLeaks.log');
213 //HaltOnError := False;
214 {$ENDIF}
216 e_WriteLog('Used file pathes:', MSG_NOTIFY);
217 e_WriteLog(' GameExeFile = ' + GameExeFile, MSG_NOTIFY);
218 e_WriteLog(' CfgFileName = ' + CfgFileName, MSG_NOTIFY);
219 e_WriteLog(' LogFileName = ' + LogFileName, MSG_NOTIFY);
220 e_WriteLog(' MapsDir = ' + MapsDir, MSG_NOTIFY);
221 e_WriteLog(' WadsDir = ' + WadsDir, MSG_NOTIFY);
222 e_WriteLog(' GameWad = ' + GameWad, MSG_NOTIFY);
223 e_WriteLog(' EditorWad = ' + EditorWad, MSG_NOTIFY);
224 end;
226 begin
227 Application.ExceptionDialog := aedOkMessageBox;
228 Application.AddOnExceptionHandler(THandlerObject.ExceptionHandler, True);
229 Application.Initialize();
230 {$IFDEF DARWIN}
231 // Disable icons in menu on OSX by default
232 Application.ShowMenuGlyphs := sbgNever;
233 {$ENDIF}
235 InitPathes;
236 CheckParamOptions;
237 InitLogs;
239 Application.CreateForm(TMainForm, MainForm);
240 Application.CreateForm(TOptionsForm, OptionsForm);
241 Application.CreateForm(TAboutForm, AboutForm);
242 Application.CreateForm(TMapOptionsForm, MapOptionsForm);
243 Application.CreateForm(TActivationTypeForm, ActivationTypeForm);
244 Application.CreateForm(TAddResourceForm, AddResourceForm);
245 Application.CreateForm(TKeysForm, KeysForm);
246 Application.CreateForm(TMapCheckForm, MapCheckForm);
247 Application.CreateForm(TMapOptimizationForm, MapOptimizationForm);
248 Application.CreateForm(TAddTextureForm, AddTextureForm);
249 Application.CreateForm(TSaveMapForm, SaveMapForm);
250 Application.CreateForm(TSelectMapForm, SelectMapForm);
251 Application.CreateForm(TAddSkyForm, AddSkyForm);
252 Application.CreateForm(TAddSoundForm, AddSoundForm);
253 Application.CreateForm(TSaveMiniMapForm, SaveMiniMapForm);
254 Application.CreateForm(TPackMapForm, PackMapForm);
255 Application.CreateForm(TChooseTypeForm, ChooseTypeForm);
256 Application.CreateForm(TSelectLanguageForm, SelectLanguageForm);
258 CheckParamFiles;
260 Application.Run();
261 end.