Navigation Menu

Skip to content

Commit

Permalink
Refactor: Textures_ComposePath() now returns an AutoStr
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Aug 25, 2012
1 parent 5947c4b commit 31af6e4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 61 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/resource/textures.h
Expand Up @@ -118,7 +118,7 @@ texturenamespaceid_t Textures_Namespace(textureid_t textureId);

/// @return Symbolic, percent-encoded name/path-to this texture as a string.
/// Must be destroyed with Str_Delete().
Str* Textures_ComposePath(textureid_t textureId);
AutoStr* Textures_ComposePath(textureid_t textureId);

/// @return URI to this texture, percent-encoded. Must be destroyed with Uri_Delete().
Uri* Textures_ComposeUri(textureid_t textureId);
Expand Down
71 changes: 30 additions & 41 deletions doomsday/engine/portable/src/gl_texmanager.c
Expand Up @@ -705,21 +705,17 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
// Attempt to load an external replacement for this flat?
if(!noHighResTex && (loadExtAlways || highResWithPWAD || !Texture_IsCustom(tex)))
{
const ddstring_t suffix = { "-ck" };
ddstring_t* path = Textures_ComposePath(Textures_Id(tex));
ddstring_t searchPath;

const Str suffix = { "-ck" };
AutoStr* path = Textures_ComposePath(Textures_Id(tex));
// First try the flats namespace then the old-fashioned "flat-name"
// in the textures namespace.
Str_Init(&searchPath);
Str_Appendf(&searchPath,
FLATS_RESOURCE_NAMESPACE_NAME":%s;"
TEXTURES_RESOURCE_NAMESPACE_NAME":flat-%s;", Str_Text(path), Str_Text(path));
AutoStr* searchPath = Str_Appendf(AutoStr_NewStd(),
FLATS_RESOURCE_NAMESPACE_NAME":%s;"
TEXTURES_RESOURCE_NAMESPACE_NAME":flat-%s;", Str_Text(path), Str_Text(path));

source = GL_LoadExtTextureEX(image, Str_Text(&searchPath), Str_Text(&suffix), true/*quiet please*/);
Str_Free(&searchPath);
Str_Delete(path);
source = GL_LoadExtTextureEX(image, Str_Text(searchPath), Str_Text(&suffix), true/*quiet please*/);
}

if(source == TEXS_NONE)
{
const Uri* resourcePath = Textures_ResourcePath(Textures_Id(tex));
Expand Down Expand Up @@ -754,17 +750,12 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
// Attempt to load an external replacement for this patch?
if(!noHighResTex && (loadExtAlways || highResWithPWAD || !Texture_IsCustom(tex)))
{
const ddstring_t suffix = { "-ck" };
ddstring_t* path = Textures_ComposePath(Textures_Id(tex));
ddstring_t searchPath;

Str_Init(&searchPath);
Str_Appendf(&searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s;", Str_Text(path));

source = GL_LoadExtTextureEX(image, Str_Text(&searchPath), Str_Text(&suffix), true/*quiet please*/);
Str_Free(&searchPath);
Str_Delete(path);
const Str suffix = { "-ck" };
AutoStr* path = Textures_ComposePath(Textures_Id(tex));
AutoStr* searchPath = Str_Appendf(AutoStr_NewStd(), PATCHES_RESOURCE_NAMESPACE_NAME":%s;", Str_Text(path));
source = GL_LoadExtTextureEX(image, Str_Text(searchPath), Str_Text(&suffix), true/*quiet please*/);
}

if(source == TEXS_NONE)
{
const Uri* resourcePath = Textures_ResourcePath(Textures_Id(tex));
Expand All @@ -779,8 +770,8 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
}
}
}
break;
}
break; }

case TN_SPRITES: {
int tclass = 0, tmap = 0;
if(spec->flags & TSF_HAS_COLORPALETTE_XLAT)
Expand All @@ -793,26 +784,25 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
// Attempt to load an external replacement for this sprite?
if(!noHighResPatches)
{
ddstring_t searchPath, suffix = { "-ck" };
ddstring_t* path = Textures_ComposePath(Textures_Id(tex));
const Str suffix = { "-ck" };
AutoStr* path = Textures_ComposePath(Textures_Id(tex));

// Prefer psprite or translated versions if available.
Str_Init(&searchPath);
AutoStr* searchPath = AutoStr_NewStd();
if(TC_PSPRITE_DIFFUSE == spec->context)
{
Str_Appendf(&searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s-hud;", Str_Text(path));
Str_Appendf(searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s-hud;", Str_Text(path));
}
else if(tclass || tmap)
{
Str_Appendf(&searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s-table%i%i;",
Str_Text(path), tclass, tmap);
Str_Appendf(searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s-table%i%i;",
Str_Text(path), tclass, tmap);
}
Str_Appendf(&searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s", Str_Text(path));
Str_Appendf(searchPath, PATCHES_RESOURCE_NAMESPACE_NAME":%s", Str_Text(path));

source = GL_LoadExtTextureEX(image, Str_Text(&searchPath), Str_Text(&suffix), true/*quiet please*/);
Str_Free(&searchPath);
Str_Delete(path);
source = GL_LoadExtTextureEX(image, Str_Text(searchPath), Str_Text(&suffix), true/*quiet please*/);
}

if(source == TEXS_NONE)
{
const Uri* resourcePath = Textures_ResourcePath(Textures_Id(tex));
Expand All @@ -827,8 +817,8 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
}
}
}
break;
}
break; }

case TN_DETAILS: {
const Uri* resourcePath = Textures_ResourcePath(Textures_Id(tex));
if(Str_CompareIgnoreCase(Uri_Scheme(resourcePath), "Lumps"))
Expand All @@ -846,8 +836,8 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
F_Delete(file);
}
}
break;
}
break; }

case TN_SYSTEM:
case TN_REFLECTIONS:
case TN_MASKS:
Expand All @@ -858,8 +848,8 @@ static TexSource loadSourceImage(Texture* tex, const texturevariantspecification
const Uri* resourcePath = Textures_ResourcePath(Textures_Id(tex));
AutoStr* path = Uri_Compose(resourcePath);
source = GL_LoadExtTextureEX(image, Str_Text(path), NULL, true/*quiet please*/);
break;
}
break; }

default:
Con_Error("Textures::loadSourceImage: Unknown texture namespace %i.", (int) Textures_Namespace(Textures_Id(tex)));
exit(1); // Unreachable.
Expand Down Expand Up @@ -3643,10 +3633,9 @@ DGLuint GL_NewTextureWithParams2(dgltexformat_t format, int width, int height,
AutoStr* GL_ComposeCacheNameForTexture(Texture* tex)
{
textureid_t texId = Textures_Id(tex);
Str* path = Textures_ComposePath(texId);
AutoStr* path = Textures_ComposePath(texId);
AutoStr* cacheName = Str_Appendf(AutoStr_NewStd(), "texcache/%s/%s.png",
Str_Text(Textures_NamespaceName(Textures_Namespace(texId))), Str_Text(path));
Str_Delete(path);
return cacheName;
}

Expand Down
10 changes: 6 additions & 4 deletions doomsday/engine/portable/src/r_things.c
Expand Up @@ -325,14 +325,17 @@ static spriterecord_t* findSpriteRecordForName(const ddstring_t* name)
return rec;
}

static int buildSpriteRotationsWorker(textureid_t texId, void* paramaters)
static int buildSpriteRotationsWorker(textureid_t texId, void* parameters)
{
ddstring_t* path, decodedPath;
AutoStr* path;
Str decodedPath;
spriterecord_frame_t* frame;
spriterecord_t* rec;
boolean link;
Uri* uri;

DENG_UNUSED(parameters);

// Have we already encountered this name?
path = Textures_ComposePath(texId);
rec = findSpriteRecordForName(path);
Expand All @@ -351,7 +354,7 @@ static int buildSpriteRotationsWorker(textureid_t texId, void* paramaters)
}

// Add the frame(s).
Str_Init(&decodedPath);
Str_InitStd(&decodedPath);
Str_PercentDecode(Str_Set(&decodedPath, Str_Text(path)));

link = false;
Expand Down Expand Up @@ -395,7 +398,6 @@ static int buildSpriteRotationsWorker(textureid_t texId, void* paramaters)
}

Str_Free(&decodedPath);
Str_Delete(path);
return 0; // Continue iteration.
}

Expand Down
25 changes: 10 additions & 15 deletions doomsday/engine/portable/src/resource/textures.cpp
Expand Up @@ -148,20 +148,19 @@ static inline texturenamespaceid_t namespaceIdForDirectoryNode(const TextureDire
return namespaceIdForDirectory(&node->directory());
}

/// @return Newly composed path for @a node. Must be released with Str_Delete()
static inline Str* composePathForDirectoryNode(const TextureDirectoryNode* node, char delimiter)
/// @return Newly composed path for @a node.
static inline AutoStr* composePathForDirectoryNode(const TextureDirectoryNode* node, char delimiter)
{
return node->composePath(Str_New(), NULL, delimiter);
return node->composePath(AutoStr_NewStd(), NULL, delimiter);
}

/// @return Newly composed Uri for @a node. Must be released with Uri_Delete()
static Uri* composeUriForDirectoryNode(const TextureDirectoryNode* node)
{
const Str* namespaceName = Textures_NamespaceName(namespaceIdForDirectoryNode(node));
Str* path = composePathForDirectoryNode(node, TEXTURES_PATH_DELIMITER);
AutoStr* path = composePathForDirectoryNode(node, TEXTURES_PATH_DELIMITER);
Uri* uri = Uri_NewWithPath2(Str_Text(path), RC_NULL);
Uri_SetScheme(uri, Str_Text(namespaceName));
Str_Delete(path);
return uri;
}

Expand Down Expand Up @@ -1043,15 +1042,15 @@ texturenamespaceid_t Textures_Namespace(textureid_t id)
return TN_ANY;
}

Str* Textures_ComposePath(textureid_t id)
AutoStr* Textures_ComposePath(textureid_t id)
{
TextureDirectoryNode* node = directoryNodeForBindId(id);
if(node)
{
return composePathForDirectoryNode(node, TEXTURES_PATH_DELIMITER);
}
DEBUG_Message(("Warning:Textures::ComposePath: Attempted with unbound textureId #%u, returning null-object.\n", id));
return Str_New();
return AutoStr_NewStd();
}

Uri* Textures_ComposeUri(textureid_t id)
Expand Down Expand Up @@ -1285,9 +1284,8 @@ static TextureDirectoryNode** collectDirectoryNodes(texturenamespaceid_t namespa
{
if(like && like[0])
{
Str* path = composePathForDirectoryNode(*nodeIt, delimiter);
AutoStr* path = composePathForDirectoryNode(*nodeIt, delimiter);
int delta = qstrnicmp(Str_Text(path), like, strlen(like));
Str_Delete(path);
if(delta) continue; // Continue iteration.
}

Expand Down Expand Up @@ -1331,12 +1329,9 @@ static TextureDirectoryNode** collectDirectoryNodes(texturenamespaceid_t namespa
static int composeAndCompareDirectoryNodePaths(const void* nodeA, const void* nodeB)
{
// Decode paths before determining a lexicographical delta.
Str* a = Str_PercentDecode(composePathForDirectoryNode(*(const TextureDirectoryNode**)nodeA, TEXTURES_PATH_DELIMITER));
Str* b = Str_PercentDecode(composePathForDirectoryNode(*(const TextureDirectoryNode**)nodeB, TEXTURES_PATH_DELIMITER));
int delta = stricmp(Str_Text(a), Str_Text(b));
Str_Delete(b);
Str_Delete(a);
return delta;
AutoStr* a = Str_PercentDecode(composePathForDirectoryNode(*(const TextureDirectoryNode**)nodeA, TEXTURES_PATH_DELIMITER));
AutoStr* b = Str_PercentDecode(composePathForDirectoryNode(*(const TextureDirectoryNode**)nodeB, TEXTURES_PATH_DELIMITER));
return stricmp(Str_Text(a), Str_Text(b));
}

/**
Expand Down

0 comments on commit 31af6e4

Please sign in to comment.