clean up LoadImage and UserPathz/AltPath/etc related redundancy by introducing 3 new functions in uStore.pas
--- a/hedgewars/uLand.pas Fri Aug 31 17:52:53 2012 +0200
+++ b/hedgewars/uLand.pas Fri Aug 31 22:00:31 2012 +0200
@@ -62,8 +62,7 @@
r, rr: TSDL_Rect;
x, yd, yu: LongInt;
begin
- tmpsurf:= LoadImage(UserPathz[ptCurrTheme] + '/LandTex', ifIgnoreCaps);
- if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/LandTex', ifCritical or ifIgnoreCaps);
+ tmpsurf:= LoadDataImage(ptCurrTheme, 'LandTex', ifCritical or ifIgnoreCaps);
r.y:= 0;
while r.y < LAND_HEIGHT do
begin
@@ -78,12 +77,10 @@
SDL_FreeSurface(tmpsurf);
// freed in freeModule() below
- LandBackSurface:= LoadImage(UserPathz[ptCurrTheme] + '/LandBackTex', ifIgnoreCaps or ifTransparent);
- if LandBackSurface = nil then LandBackSurface:= LoadImage(Pathz[ptCurrTheme] + '/LandBackTex', ifIgnoreCaps or ifTransparent);
+ LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
if (LandBackSurface <> nil) and GrayScale then Surface2GrayScale(LandBackSurface);
- tmpsurf:= LoadImage(UserPathz[ptCurrTheme] + '/Border', ifIgnoreCaps or ifTransparent);
- if tmpsurf = nil then tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Border', ifCritical or ifIgnoreCaps or ifTransparent);
+ tmpsurf:= LoadDataImage(ptCurrTheme, 'Border', ifCritical or ifIgnoreCaps or ifTransparent);
for x:= 0 to LAND_WIDTH - 1 do
begin
yd:= LAND_HEIGHT - 1;
@@ -418,15 +415,11 @@
WriteLnToConsole('Generating forts land...');
-tmpsurf:= LoadImage(UserPathz[ptForts] + '/' + ClansArray[0]^.Teams[0]^.FortName + 'L', ifAlpha or ifTransparent or ifIgnoreCaps);
-if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptForts] + '/' + ClansArray[0]^.Teams[0]^.FortName + 'L', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
+tmpsurf:= LoadDataImage(ptForts, ClansArray[0]^.Teams[0]^.FortName + 'L', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
BlitImageAndGenerateCollisionInfo(leftX+150, LAND_HEIGHT - tmpsurf^.h, tmpsurf^.w, tmpsurf);
SDL_FreeSurface(tmpsurf);
-tmpsurf:= LoadImage(UserPathz[ptForts] + '/' + ClansArray[1]^.Teams[0]^.FortName + 'R', ifAlpha or ifTransparent or ifIgnoreCaps);
-if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptForts] + '/' + ClansArray[1]^.Teams[0]^.FortName + 'R', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
+tmpsurf:= LoadDataImage(ptForts, ClansArray[1]^.Teams[0]^.FortName + 'R', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
BlitImageAndGenerateCollisionInfo(rightX - 150 - tmpsurf^.w, LAND_HEIGHT - tmpsurf^.h, tmpsurf^.w, tmpsurf);
SDL_FreeSurface(tmpsurf);
end;
@@ -437,15 +430,11 @@
p: PLongwordArray;
x, y, cpX, cpY: Longword;
begin
-tmpsurf:= LoadImage(UserPathz[ptMapCurrent] + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
-if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
+tmpsurf:= LoadDataImage(ptMapCurrent, 'mask', ifAlpha or ifTransparent or ifIgnoreCaps);
if tmpsurf = nil then
begin
mapName:= ExtractFileName(Pathz[ptMapCurrent]);
- tmpsurf:= LoadImage(UserPathz[ptMissionMaps] + '/' + mapName + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptMissionMaps] + '/' + mapName + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
+ tmpsurf:= LoadDataImage(ptMissionMaps, mapName + '/mask', ifAlpha or ifTransparent or ifIgnoreCaps);
end;
@@ -488,9 +477,7 @@
if not disableLandBack then
begin
// freed in freeModule() below
- LandBackSurface:= LoadImage(UserPathz[ptCurrTheme] + '/LandBackTex', ifIgnoreCaps or ifTransparent);
- if LandBackSurface = nil then
- LandBackSurface:= LoadImage(Pathz[ptCurrTheme] + '/LandBackTex', ifIgnoreCaps or ifTransparent);
+ LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
if (LandBackSurface <> nil) and GrayScale then
Surface2GrayScale(LandBackSurface)
end;
@@ -508,15 +495,11 @@
begin
WriteLnToConsole('Loading land from file...');
AddProgress;
-tmpsurf:= LoadImage(UserPathz[ptMapCurrent] + '/map', ifAlpha or ifTransparent or ifIgnoreCaps);
-if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/map', ifAlpha or ifTransparent or ifIgnoreCaps);
+tmpsurf:= LoadDataImage(ptMapCurrent, 'map', ifAlpha or ifTransparent or ifIgnoreCaps);
if tmpsurf = nil then
begin
mapName:= ExtractFileName(Pathz[ptMapCurrent]);
- tmpsurf:= LoadImage(UserPathz[ptMissionMaps] + '/' + mapName + '/map', ifAlpha or ifTransparent or ifIgnoreCaps);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptMissionMaps] + '/' + mapName + '/map', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
+ tmpsurf:= LoadDataImage(ptMissionMaps, mapName + '/map', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
end;
// (bare) Sanity check. Considering possible LongInt comparisons as well as just how much system memoery it would take
TryDo((tmpsurf^.w < $40000000) and (tmpsurf^.h < $40000000) and (tmpsurf^.w * tmpsurf^.h < 6*1024*1024*1024), 'Map dimensions too big!', true);
--- a/hedgewars/uLandObjects.pas Fri Aug 31 17:52:53 2012 +0200
+++ b/hedgewars/uLandObjects.pas Fri Aug 31 22:00:31 2012 +0200
@@ -210,13 +210,7 @@
if x1 > 0 then
begin
bRes:= true;
- tmpsurf:= LoadImage(UserPathz[ptCurrTheme] + '/Girder', ifTransparent or ifIgnoreCaps);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptCurrTheme] + '/Girder', ifTransparent or ifIgnoreCaps);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(UserPathz[ptGraphics] + '/Girder', ifTransparent or ifIgnoreCaps);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptGraphics] + '/Girder', ifCritical or ifTransparent or ifIgnoreCaps);
+ tmpsurf:= LoadDataImageAltPath(ptCurrTheme, ptGraphics, 'Girder', ifCritical or ifTransparent or ifIgnoreCaps);
rr.x:= x1;
while rr.x < x2 do
@@ -563,9 +557,7 @@
with ThemeObjects.objs[Pred(ThemeObjects.Count)] do
begin
i:= Pos(',', s);
- Surf:= LoadImage(UserPathz[ptCurrTheme] + '/' + Trim(Copy(s, 1, Pred(i))), ifTransparent or ifIgnoreCaps);
- if Surf = nil then
- Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + Trim(Copy(s, 1, Pred(i))), ifCritical or ifTransparent or ifIgnoreCaps);
+ Surf:= LoadDataImage(ptCurrTheme, Trim(Copy(s, 1, Pred(i))), ifTransparent or ifIgnoreCaps);
Width:= Surf^.w;
Height:= Surf^.h;
Delete(s, 1, i);
@@ -623,9 +615,7 @@
with SprayObjects.objs[Pred(SprayObjects.Count)] do
begin
i:= Pos(',', s);
- Surf:= LoadImage(UserPathz[ptCurrTheme] + '/' + Trim(Copy(s, 1, Pred(i))), ifTransparent or ifIgnoreCaps);
- if Surf = nil then
- Surf:= LoadImage(Pathz[ptCurrTheme] + '/' + Trim(Copy(s, 1, Pred(i))), ifCritical or ifTransparent or ifIgnoreCaps);
+ Surf:= LoadDataImage(ptCurrTheme, Trim(Copy(s, 1, Pred(i))), ifTransparent or ifIgnoreCaps);
Width:= Surf^.w;
Height:= Surf^.h;
Delete(s, 1, i);
--- a/hedgewars/uStore.pas Fri Aug 31 17:52:53 2012 +0200
+++ b/hedgewars/uStore.pas Fri Aug 31 22:00:31 2012 +0200
@@ -32,6 +32,14 @@
procedure AddProgress;
procedure FinishProgress;
function LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
+
+// loads an image from the game's data files
+function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
+// like LoadDataImage but uses altPath as fallback-path if file not found/loadable in path
+function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
+// like LoadDataImage but uses altFile as fallback-filename if file cannot be loaded
+function LoadDataImageAltFile(const path: TPathType; const filename, altFile: shortstring; imageFlags: LongInt): PSDL_Surface;
+
procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
procedure SetupOpenGL;
procedure SetScale(f: GLfloat);
@@ -95,9 +103,7 @@
Color, i: Longword;
s: shortstring;
begin
-s:= UserPathz[ptGraphics] + '/' + cCHFileName;
-if not FileExists(s+'.png') then s:= Pathz[ptGraphics] + '/' + cCHFileName;
-tmpsurf:= LoadImage(s, ifAlpha or ifCritical);
+tmpsurf:= LoadDataImage(ptGraphics, cCHFileName, ifAlpha or ifCritical);
for t:= 0 to Pred(TeamsCount) do
with TeamsArray[t]^ do
@@ -200,13 +206,7 @@
else if (Flag = 'cpu') or (Flag = 'cpu_plain') then
Flag:= 'hedgewars';
- flagsurf:= LoadImage(UserPathz[ptFlags] + '/' + Flag, ifNone);
- if flagsurf = nil then
- flagsurf:= LoadImage(Pathz[ptFlags] + '/' + Flag, ifNone);
- if flagsurf = nil then
- flagsurf:= LoadImage(UserPathz[ptFlags] + '/hedgewars', ifNone);
- if flagsurf = nil then
- flagsurf:= LoadImage(Pathz[ptFlags] + '/hedgewars', ifNone);
+ flagsurf:= LoadDataImageAltFile(ptFlags, Flag, 'hedgewars', ifNone);
TryDo(flagsurf <> nil, 'Failed to load flag "' + Flag + '" as well as the default flag', true);
case maxLevel of
@@ -251,9 +251,7 @@
end
end;
end;
- MissionIcons:= LoadImage(UserPathz[ptGraphics] + '/missions', ifNone);
- if MissionIcons = nil then
- MissionIcons:= LoadImage(Pathz[ptGraphics] + '/missions', ifCritical);
+ MissionIcons:= LoadDataImage(ptGraphics, 'missions', ifCritical);
iconsurf:= SDL_CreateRGBSurface(SDL_SWSURFACE, 28, 28, 32, RMask, GMask, BMask, AMask);
if iconsurf <> nil then
begin
@@ -291,13 +289,7 @@
begin
if GraveName = '' then
GraveName:= 'Statue';
- texsurf:= LoadImage(UserPathz[ptGraves] + '/' + GraveName, ifTransparent);
- if texsurf = nil then
- texsurf:= LoadImage(Pathz[ptGraves] + '/' + GraveName, ifTransparent);
- if texsurf = nil then
- texsurf:= LoadImage(UserPathz[ptGraves] + '/Statue', ifTransparent);
- if texsurf = nil then
- texsurf:= LoadImage(Pathz[ptGraves] + '/Statue', ifCritical or ifTransparent);
+ texsurf:= LoadDataImageAltFile(ptGraves, GraveName, 'Statue', ifCritical or ifTransparent);
GraveTex:= Surface2Tex(texsurf, false);
SDL_FreeSurface(texsurf)
end
@@ -309,7 +301,7 @@
fi: THWFont;
ai: TAmmoType;
tmpsurf: PSDL_Surface;
- i: LongInt;
+ i, imflags: LongInt;
begin
AddFileLog('StoreLoad()');
@@ -342,43 +334,18 @@
((cCloudsNumber > 0) or (ii <> sprCloud)) and
((vobCount > 0) or (ii <> sprFlake)) then
begin
- if AltPath = ptNone then
- if ii in [sprHorizont, sprHorizontL, sprHorizontR, sprSky, sprSkyL, sprSkyR, sprChunk] then // FIXME: hack
- begin
- if not reload then
- begin
- tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent)
- end
- else
- tmpsurf:= Surface
- end
- else
- begin
- if not reload then
- begin
- tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent or ifCritical)
- end
- else
- tmpsurf:= Surface
- end
+ if reload then
+ tmpsurf:= Surface
else
begin
- if not reload then
- begin
- tmpsurf:= LoadImage(UserPathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[Path] + '/' + FileName, ifAlpha or ifTransparent);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(UserPathz[AltPath] + '/' + FileName, ifAlpha or ifTransparent);
- if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[AltPath] + '/' + FileName, ifAlpha or ifCritical or ifTransparent)
- end
- else
- tmpsurf:= Surface
+ imflags := (ifAlpha or ifTransparent);
+
+ // these sprites are optional
+ if not (ii in [sprHorizont, sprHorizontL, sprHorizontR, sprSky, sprSkyL, sprSkyR, sprChunk]) then // FIXME: hack
+ imflags := (imflags or ifCritical);
+
+ // load the image
+ tmpsurf := LoadDataImageAltPath(Path, AltPath, FileName, imflags)
end;
if tmpsurf <> nil then
@@ -428,10 +395,8 @@
if not reload then
AddProgress;
- tmpsurf:= LoadImage(UserPathz[ptGraphics] + '/' + cHHFileName, ifAlpha or ifTransparent);
-if tmpsurf = nil then
- tmpsurf:= LoadImage(Pathz[ptGraphics] + '/' + cHHFileName, ifAlpha or ifCritical or ifTransparent);
-
+tmpsurf:= LoadDataImage(ptGraphics, cHHFileName, ifAlpha or ifCritical or ifTransparent);
+
HHTexture:= Surface2Tex(tmpsurf, false);
SDL_FreeSurface(tmpsurf);
@@ -627,12 +592,59 @@
LoadImage:= tmpsurf //Result
end;
+
+function LoadDataImage(const path: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
+var tmpsurf: PSDL_Surface;
+begin
+ // check for file in user dir (never critical)
+ tmpsurf:= LoadImage(UserPathz[path] + '/' + filename, imageFlags and (not ifCritical));
+
+ // if unsuccessful check data dir
+ if (tmpsurf = nil) then
+ tmpsurf:= LoadImage(Pathz[path] + '/' + filename, imageFlags);
+
+ LoadDataImage:= tmpsurf;
+end;
+
+
+function LoadDataImageAltPath(const path, altPath: TPathType; const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
+var tmpsurf: PSDL_Surface;
+begin
+ // if there is no alternative path, just forward and return result
+ if (altPath = ptNone) then
+ exit(LoadDataImage(path, filename, imageFlags));
+
+ // since we have a fallback path this search isn't critical yet
+ tmpsurf:= LoadDataImage(path, filename, imageFlags and (not ifCritical));
+
+ // if image still not found try alternative path
+ if (tmpsurf = nil) then
+ tmpsurf:= LoadDataImage(altPath, filename, imageFlags);
+
+ LoadDataImageAltPath:= tmpsurf;
+end;
+
+function LoadDataImageAltFile(const path: TPathType; const filename, altFile: shortstring; imageFlags: LongInt): PSDL_Surface;
+var tmpsurf: PSDL_Surface;
+begin
+ // if there is no alternative filename, just forward and return result
+ if (altFile = '') then
+ exit(LoadDataImage(path, filename, imageFlags));
+
+ // since we have a fallback filename this search isn't critical yet
+ tmpsurf:= LoadDataImage(path, filename, imageFlags and (not ifCritical));
+
+ // if image still not found try alternative filename
+ if (tmpsurf = nil) then
+ tmpsurf:= LoadDataImage(path, altFile, imageFlags);
+
+ LoadDataImageAltFile:= tmpsurf;
+end;
+
procedure LoadHedgehogHat(HHGear: PGear; newHat: shortstring);
var texsurf: PSDL_Surface;
begin
-texsurf:= LoadImage(UserPathz[ptHats] + '/' + newHat, ifNone);
- if texsurf = nil then
- texsurf:= LoadImage(Pathz[ptHats] + '/' + newHat, ifNone);
+ texsurf:= LoadDataImage(ptHats, newHat, ifNone);
// only do something if the hat could be loaded
if texsurf <> nil then
@@ -864,9 +876,7 @@
if Step = 0 then
begin
WriteToConsole(msgLoading + 'progress sprite: ');
- texsurf:= LoadImage(UserPathz[ptGraphics] + '/Progress', ifTransparent);
- if texsurf = nil then
- texsurf:= LoadImage(Pathz[ptGraphics] + '/Progress', ifCritical or ifTransparent);
+ texsurf:= LoadDataImage(ptGraphics, 'Progress', ifCritical or ifTransparent);
ProgrTex:= Surface2Tex(texsurf, false);
@@ -1135,9 +1145,7 @@
WriteLnToConsole(msgOK);
// load engine icon
{$IFNDEF DARWIN}
- ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
- if ico = nil then
- ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
+ ico:= LoadDataImage(ptGraphics, 'hwengine', ifIgnoreCaps);
if ico <> nil then
begin
SDL_WM_SetIcon(ico, 0);