Oolite
Loading...
Searching...
No Matches
OOTextureVerifierStage.m
Go to the documentation of this file.
1/*
2
3OOTextureVerifierStage.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
27
28#if OO_OXP_VERIFIER_ENABLED
29
31#import "OOMaths.h"
32#import "OOTextureLoader.h"
33
34static NSString* const kStageName = @"Testing textures and images";
35
36@interface OOTextureVerifierStage (OOPrivate)
37
38- (void)checkTextureNamed:(NSString*)name inFolder:(NSString*)folder;
39
40@end
41
42@implementation OOTextureVerifierStage
43
44- (id)init
45{
46 self = [super init];
47 if (self != nil) {
48 _usedTextures = [[NSMutableSet alloc] init];
49 }
50 return self;
51}
52
53- (void)dealloc
54{
55 [_usedTextures release];
56
57 [super dealloc];
58}
59
60+ (NSString*)nameForReverseDependencyForVerifier:(OOOXPVerifier*)verifier
61{
62 return kStageName;
63}
64
65- (NSString*)name
66{
67 return kStageName;
68}
69
70- (BOOL)shouldRun
71{
72 return [_usedTextures count] != 0 || [[[self verifier] fileScannerStage] filesInFolder:@"Images"] != nil;
73}
74
75- (void)run
76{
77 NSArray* nameEnum = nil;
78 NSString* name = nil;
79 NSAutoreleasePool* pool = nil;
80
81 foreach (name, _usedTextures) {
82 pool = [[NSAutoreleasePool alloc] init];
83 [self checkTextureNamed:name inFolder:@"Textures"];
84 [pool release];
85 }
86 [_usedTextures release];
88
89 // All "images" are considered used, since we don't have a reasonable way to look for images referenced in JavaScript scripts.
90 nameEnum = [[[self verifier] fileScannerStage] filesInFolder:@"Images"];
91 foreach (name, nameEnum) {
92 [self checkTextureNamed:name inFolder:@"Images"];
93 }
94}
95
96- (void)textureNamed:(NSString*)name usedInContext:(NSString*)context
97{
98 OOFileScannerVerifierStage* fileScanner = nil;
99
100 if (name == nil)
101 return;
102 if ([_usedTextures containsObject:name])
103 return;
104 [_usedTextures addObject:name];
105
106 fileScanner = [[self verifier] fileScannerStage];
107 if (![fileScanner fileExists:name
108 inFolder:@"Textures"
109 referencedFrom:context
110 checkBuiltIn:YES]) {
111 OOLog(@"verifyOXP.texture.notFound", @"----- WARNING: texture \"%@\" referenced in %@ could not be found in %@ or in Oolite.", name, context, [[self verifier] oxpDisplayName]);
112 }
113}
114
115@end
116
117@implementation OOTextureVerifierStage (OOPrivate)
118
119- (void)checkTextureNamed:(NSString*)name inFolder:(NSString*)folder
120{
121 OOTextureLoader* loader = nil;
122 NSString* path = nil;
123 OOFileScannerVerifierStage* fileScanner = nil;
124 NSString* displayName = nil;
125 OOPixMapDimension rWidth, rHeight;
126 BOOL success;
127 OOPixMap pixmap;
128 OOTextureDataFormat format;
129
130 fileScanner = [[self verifier] fileScannerStage];
131 path = [fileScanner pathForFile:name
132 inFolder:folder
134 checkBuiltIn:NO];
135
136 if (path == nil)
137 return;
138
139 loader = [OOTextureLoader loaderWithPath:path
140 options:kOOTextureMinFilterNearest | kOOTextureMinFilterNearest | kOOTextureNoShrink | kOOTextureNoFNFMessage |
141 kOOTextureNeverScale];
142
143 displayName = [fileScanner displayNameForFile:name andFolder:folder];
144 if (loader == nil) {
145 OOLog(@"verifyOXP.texture.failed", @"***** ERROR: image %@ could not be read.", displayName);
146 } else {
147 success = [loader getResult:&pixmap format:&format originalWidth:NULL originalHeight:NULL];
148
149 if (success) {
150 rWidth = OORoundUpToPowerOf2_PixMap((2 * pixmap.width) / 3);
151 rHeight = OORoundUpToPowerOf2_PixMap((2 * pixmap.height) / 3);
152 if (pixmap.width != rWidth || pixmap.height != rHeight) {
153 OOLog(@"verifyOXP.texture.notPOT", @"----- WARNING: image %@ has non-power-of-two dimensions; it will have to be rescaled (from %ux%u pixels to %ux%u pixels) at runtime.", displayName, pixmap.width, pixmap.height, rWidth, rHeight);
154 } else {
155 OOLog(@"verifyOXP.verbose.texture.OK", @"- %@ (%ux%u px) OK.", displayName, pixmap.width, pixmap.height);
156 }
157
158 OOFreePixMap(&pixmap);
159 } else {
160 OOLog(@"verifyOXP.texture.failed", @"***** ERROR: texture loader failed to load %@.", displayName);
161 }
162 }
163}
164
165@end
166
167@implementation OOTextureHandlingStage
168
169- (NSSet*)dependents
170{
171 NSMutableSet* result = [[super dependents] mutableCopy];
173 return [result autorelease];
174}
175
176@end
177
178@implementation OOOXPVerifier (OOTextureVerifierStage)
179
181{
182 return [self stageWithName:kStageName];
183}
184
185@end
186
187#endif
static NSString *const kStageName
#define OOLog(class, format,...)
Definition OOLogging.h:84
void OOFreePixMap(OOPixMap *ioPixMap)
Definition OOPixMap.m:80
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
#define OORoundUpToPowerOf2_PixMap
Definition OOPixMap.h:34
return nil
OOPixMapFormat OOTextureDataFormat
Definition OOTexture.h:107
id displayNameForFile:andFolder:(NSString *file,[andFolder] NSString *folder)
NSString * pathForFile:inFolder:referencedFrom:checkBuiltIn:(NSString *file,[inFolder] NSString *folder,[referencedFrom] NSString *context,[checkBuiltIn] BOOL checkBuiltIn)
NSArray * filesInFolder:(NSString *folder)
OOOXPVerifier * verifier()
id stageWithName:(NSString *name)
OOFileScannerVerifierStage * fileScannerStage()
id loaderWithPath:options:(NSString *path,[options] uint32_t options)
BOOL getResult:format:originalWidth:originalHeight:(OOPixMap *result,[format] OOTextureDataFormat *outFormat,[originalWidth] uint32_t *outWidth,[originalHeight] uint32_t *outHeight)
void checkTextureNamed:inFolder:(NSString *name,[inFolder] NSString *folder)
NSString * nameForReverseDependencyForVerifier:(OOOXPVerifier *verifier)
OOPixMapDimension height
Definition OOPixMap.h:45
OOPixMapDimension width
Definition OOPixMap.h:45