This repository was archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerrainManager.pas
286 lines (249 loc) · 6.98 KB
/
TerrainManager.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
unit TerrainManager;
{$mode objfpc}{$H+}
{$INTERFACES CORBA}
{$DEFINE LOG_WARN_ON_NO_TEXTURE}
interface
uses
Classes,
SysUtils,
IniFiles,
zgl_textures,
NiceExceptions,
StringFeatures,
LogEntity,
LogEntityFace,
EngineManagerFace,
ZenGLFCLGraphics,
Common,
TerrainManagerFace,
TerrainManagerFaceE,
MapDataCells;
type
{ TTerrainManager }
TTerrainManager = class(TComponent, ITerrainManager, ITerrainManagerE)
public
constructor Create(const aOwner: TComponent); reintroduce;
private
fLog: ILog;
fTerrains: TTerrains;
fMasks: TMultiTexture;
fEngine: IEngineManager;
procedure Initialize;
procedure LoadTerrains(const aFile: TIniFile);
procedure LoadTerrainsFromList(const aFile: TIniFile; const aList: TStrings);
procedure LoadTerrain(var aTerrain: TTerrain; const aFile: TIniFile);
procedure LoadTerrainTexture(var aTerrain: TTerrain; const aFile: TIniFile);
function GetTerrain(const aIndex: integer): PTerrain;
function GetTerrainsCount: integer;
procedure ReleaseTerrains;
procedure LoadMasks(const aFile: TIniFile);
function GetMasks: TMultiTexture;
procedure Finalize;
public const
ColorIdent = 'replacingColor';
VehicleableIdent = 'Vehicleable';
TextureFilePathIdent = 'textureFile';
CommonSection = 'common';
public
property Log: ILog read fLog;
property Terrains[const aIndex: integer]: PTerrain read GetTerrain;
property TerrainsCount: integer read GetTerrainsCount;
property Masks: TMultiTexture read fMasks;
property Engine: IEngineManager read fEngine;
procedure LoadTerrains(const aFileName: string);
procedure LoadMasks(const aFileName: string);
function GetTerrainsInfoAsText: string;
function GetTypeColor(const aType: TTerrainType): LongWord;
function Reverse: TObject;
destructor Destroy; override;
end;
implementation
{ TTerrainManager }
constructor TTerrainManager.Create(const aOwner: TComponent);
begin
inherited Create(aOwner);
Initialize;
end;
procedure TTerrainManager.Initialize;
begin
fLog := TLog.Create(GlobalLogManager, 'TerrainManager');
fEngine := GlobalGameManager.Engine;
fMasks := TMultiTexture.Create;
Masks.Engine := fEngine;
end;
procedure TTerrainManager.LoadTerrainsFromList(const aFile: TIniFile;
const aList: TStrings);
var
i: integer;
begin
SetLength(fTerrains, aList.Count);
for i := 0 to aList.Count - 1 do
begin
Terrains[i]^.Init;
Terrains[i]^.Name := aList[i];
Terrains[i]^.id := i;
LoadTerrain(fTerrains[i], aFile);
end;
end;
procedure TTerrainManager.LoadMasks(const aFile: TIniFile);
const
DEBUG = true;
function LoadMask(const aSection: string): zglPTexture;
var
MaskFilePath: string;
begin
MaskFilePath := aFile.ReadString(aSection, TextureFilePathIdent, '');
if DEBUG then
Log.Write('Now loading mask "' + aSection + '"'
+ LineEnding + '"' + MaskFilePath + '"');
MaskFilePath := GlobalApplicationPath + MaskFilePath;
if not FileExists(MaskFilePath) then
raise EFileNotFound.Create(MaskFilePath);
result := Engine.LoadTexture(MaskFilePath);
end;
var
sections: TStrings;
s: string;
texture: zglPTexture;
begin
sections := TStringList.Create;
aFile.ReadSections(sections);
Masks.Count := sections.Count;
if DEBUG then
Log.Write('Now loading masks: ' + IntToStr(Masks.Count) + ' items');
for s in sections do
begin
texture := LoadMask(s);
Masks.Add(texture);
if DEBUG then
Log.Write('Now disposing texture...');
Engine.DisposeTexture(texture);
end;
Masks.FinishArea;
sections.Free;
end;
function TTerrainManager.GetMasks: TMultiTexture;
begin
result := fMasks;
end;
procedure TTerrainManager.ReleaseTerrains;
var
i: integer;
begin
for i := 0 to TerrainsCount - 1 do
fTerrains[i].Done;
SetLength(fTerrains, 0);
end;
procedure TTerrainManager.Finalize;
begin
ReleaseTerrains;
FreeAndNil(fMasks);
FreeLog(fLog);
end;
procedure TTerrainManager.LoadTerrains(const aFileName: string);
var
ini: TIniFile;
begin
AssertFileExists(aFileName);
ini := TIniFile.Create(aFileName);
LoadTerrains(ini);
ini.Free;
end;
procedure TTerrainManager.LoadMasks(const aFileName: string);
var
ini: TIniFile;
begin
AssertFileExists(aFileName);
ini := TIniFile.Create(aFileName);
LoadMasks(ini);
ini.Free;
end;
procedure TTerrainManager.LoadTerrains(const aFile: TIniFile);
procedure LoadTheTerrains;
var
terrainList: TStrings;
commonSectionIndex: integer;
begin
terrainList := TStringList.Create;
aFile.ReadSections(terrainList);
commonSectionIndex := terrainList.IndexOf(CommonSection);
if commonSectionIndex <> -1 then
terrainList.Delete(commonSectionIndex); // "common" section should be excluded
LoadTerrainsFromList(aFile, terrainList);
terrainList.Free;
end;
begin
LoadTheTerrains;
end;
procedure TTerrainManager.LoadTerrain(var aTerrain: TTerrain;
const aFile: TIniFile);
begin
aTerrain.Color := StrHexToLongWord(aFile.ReadString(aTerrain.Name, ColorIdent, '000000'));
aTerrain.Vehicleable := aFile.ReadBool(aTerrain.Name, VehicleableIdent, false);
LoadTerrainTexture(aTerrain, aFile);
end;
procedure TTerrainManager.LoadTerrainTexture(var aTerrain: TTerrain;
const aFile: TIniFile);
{$DEFINE DEBUG_THIS}
procedure DebugWrite(const aText: string);
begin
{$IFDEF DEBUG_THIS}
Log.Write(aText);
{$ENDIF}
end;
var
textureFilePath: string;
begin
textureFilePath := aFile.ReadString(aTerrain.Name, TextureFilePathIdent, '');
if textureFilePath = '' then
begin
{$IFDEF LOG_WARN_ON_NO_TEXTURE}
Log.Write('No texture for "' + aTerrain.Name + '" specified');
{$ENDIF}
aTerrain.Texture := nil;
end
else
begin
DebugWrite('Now loading texture for "' + aTerrain.Name + '" from:'
+ LineEnding + '"' + textureFilePath + '"');
textureFilePath := GlobalApplicationPath + textureFilePath;
if FileExists(textureFilePath) then
aTerrain.Texture := Engine.LoadTexture(textureFilePath)
else
Log.Write(logTagError, 'File does not exists'
+ LineEnding + '"' + textureFilePath + '"');
end;
end;
function TTerrainManager.GetTerrain(const aIndex: integer): PTerrain;
begin
result := nil;
AssertIndexInBounds(0, aIndex, TerrainsCount - 1, 'Terrain type out of bounds');
result := @ ( fTerrains[aIndex] );
end;
function TTerrainManager.GetTerrainsCount: integer;
begin
result := Length(fTerrains);
end;
function TTerrainManager.GetTerrainsInfoAsText: string;
var
i: integer;
begin
result := 'Terrain types: ' + IntToStr(Length(fTerrains)) + ' items total';
for i := 0 to TerrainsCount - 1 do
result += LineEnding + Terrains[i]^.ToText;
end;
function TTerrainManager.GetTypeColor(const aType: TTerrainType): LongWord;
begin
AssertIndexInBounds(0, aType, TerrainsCount - 1, 'No such terrain type');
result := Terrains[aType]^.Color;
end;
function TTerrainManager.Reverse: TObject;
begin
result := self;
end;
destructor TTerrainManager.Destroy;
begin
Finalize;
inherited Destroy;
end;
end.