Skip to content

Commit

Permalink
Refactor|Resources: ResourceSystem has ownership of raw textures
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Dec 9, 2013
1 parent b76d265 commit d6a1636
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 158 deletions.
1 change: 0 additions & 1 deletion doomsday/client/client.pro
Expand Up @@ -715,7 +715,6 @@ SOURCES += \
src/resource/patch.cpp \
src/resource/patchname.cpp \
src/resource/pcx.cpp \
src/resource/rawtexture.cpp \
src/resource/resourcesystem.cpp \
src/resource/sprite.cpp \
src/resource/texture.cpp \
Expand Down
26 changes: 3 additions & 23 deletions doomsday/client/include/resource/rawtexture.h
Expand Up @@ -18,8 +18,8 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCE_RAWTEXTURE
#define LIBDENG_RESOURCE_RAWTEXTURE
#ifndef DENG_RESOURCE_RAWTEXTURE
#define DENG_RESOURCE_RAWTEXTURE

#include "dd_share.h" // For lumpnum_t
#include <de/str.h>
Expand All @@ -37,24 +37,4 @@ struct rawtex_t
rawtex_t *next;
};

void R_InitRawTexs();
void R_UpdateRawTexs();

/**
* Returns a rawtex_t for the given lump if one already exists; otherwise @c 0.
*/
rawtex_t *R_FindRawTex(lumpnum_t lumpNum);

/**
* Get a rawtex_t data structure for a raw texture specified with a WAD lump
* number. Allocates a new rawtex_t if it hasn't been loaded yet.
*/
rawtex_t *R_GetRawTex(lumpnum_t lumpNum);

/**
* Returns a NULL-terminated array of pointers to all the rawtexs.
* The array must be freed with Z_Free.
*/
rawtex_t **R_CollectRawTexs(int *count = 0);

#endif // LIBDENG_RESOURCE_RAWTEXTURE
#endif // DENG_RESOURCE_RAWTEXTURE
18 changes: 18 additions & 0 deletions doomsday/client/include/resource/resourcesystem.h
Expand Up @@ -463,6 +463,23 @@ class ResourceSystem : public de::System

patchid_t declarePatch(de::String encodedName);

/**
* Returns a rawtex_t for the given lump if one already exists; otherwise @c 0.
*/
rawtex_t *rawTexture(lumpnum_t lumpNum);

/**
* Get a rawtex_t data structure for a raw texture specified with a WAD lump
* number. Allocates a new rawtex_t if it hasn't been loaded yet.
*/
rawtex_t *declareRawTexture(lumpnum_t lumpNum);

/**
* Returns a NULL-terminated array of pointers to all the rawtexs.
* The array must be freed with Z_Free.
*/
rawtex_t **collectRawTextures(int *count = 0);

#ifdef __CLIENT__
/**
* Determines if a manifest exists for a resource on @a path.
Expand Down Expand Up @@ -843,6 +860,7 @@ class ResourceSystem : public de::System
public: /// @todo Should be private:
void initCompositeTextures();
void initFlatTextures();
void initRawTextures();
void initSpriteTextures();
void initSystemTextures();

Expand Down
3 changes: 1 addition & 2 deletions doomsday/client/src/dd_main.cpp
Expand Up @@ -2014,7 +2014,6 @@ static int DD_StartupWorker(void * /*context*/)
UI_LoadFonts();
#endif
R_InitTranslationTables();
R_InitRawTexs();
R_InitSvgs();
#ifdef __CLIENT__
R_InitViewWindow();
Expand Down Expand Up @@ -2104,7 +2103,7 @@ static int DD_UpdateEngineStateWorker(void *context)
// Re-read definitions.
Def_Read();

R_UpdateRawTexs();
App_ResourceSystem().initRawTextures();
App_ResourceSystem().initSprites(); // Fully reinitialize sprites.
#ifdef __CLIENT__
App_ResourceSystem().initModels(); // Defs might've changed.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/gl/gl_main.cpp
Expand Up @@ -999,7 +999,7 @@ void GL_SetPSprite(Material *mat, int tClass, int tMap)

void GL_SetRawImage(lumpnum_t lumpNum, gl::Wrapping wrapS, gl::Wrapping wrapT)
{
if(rawtex_t *rawTex = R_GetRawTex(lumpNum))
if(rawtex_t *rawTex = App_ResourceSystem().declareRawTexture(lumpNum))
{
GL_BindTextureUnmanaged(GL_PrepareRawTexture(*rawTex), wrapS, wrapT,
(filterUI ? gl::Linear : gl::Nearest));
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/gl/gl_texmanager.cpp
Expand Up @@ -369,7 +369,7 @@ GLuint GL_PrepareRawTexture(rawtex_t &raw)

void GL_SetRawTexturesMinFilter(int newMinFilter)
{
rawtex_t **rawTexs = R_CollectRawTexs();
rawtex_t **rawTexs = App_ResourceSystem().collectRawTextures();
for(rawtex_t **ptr = rawTexs; *ptr; ptr++)
{
rawtex_t *r = *ptr;
Expand All @@ -387,7 +387,7 @@ void GL_SetRawTexturesMinFilter(int newMinFilter)

void GL_ReleaseTexturesForRawImages()
{
rawtex_t **rawTexs = R_CollectRawTexs();
rawtex_t **rawTexs = App_ResourceSystem().collectRawTextures();
for(rawtex_t **ptr = rawTexs; *ptr; ptr++)
{
rawtex_t *r = (*ptr);
Expand Down
125 changes: 0 additions & 125 deletions doomsday/client/src/resource/rawtexture.cpp

This file was deleted.

97 changes: 97 additions & 0 deletions doomsday/client/src/resource/resourcesystem.cpp
Expand Up @@ -37,6 +37,7 @@
# include "MaterialSnapshot"
#endif

#include "api_filesys.h"
#include "filesys/fs_main.h"
#include "filesys/lumpindex.h"

Expand Down Expand Up @@ -195,6 +196,16 @@ DENG2_PIMPL(ResourceSystem)
/// All texture instances in the system (from all schemes).
AllTextures textures;

static int const RAWTEX_HASH_SIZE = 128;
struct RawTexHash {
rawtex_t *first;
};
RawTexHash rawtexhash[RAWTEX_HASH_SIZE];

inline RawTexHash &rawTextureHash(lumpnum_t lumpNum) {
return rawtexhash[((unsigned) lumpNum) & (RAWTEX_HASH_SIZE - 1)];
}

/// System subspace schemes containing the manifests/resources.
MaterialSchemes materialSchemes;
QList<MaterialScheme *> materialSchemeCreationOrder;
Expand Down Expand Up @@ -327,6 +338,8 @@ DENG2_PIMPL(ResourceSystem)
, modelRepository(0)
#endif
{
zap(rawtexhash);

LOG_AS("ResourceSystem");
resClasses.append(new ResourceClass("RC_PACKAGE", "Packages"));
resClasses.append(new ResourceClass("RC_DEFINITION", "Defs"));
Expand Down Expand Up @@ -2445,6 +2458,90 @@ patchid_t ResourceSystem::declarePatch(String encodedName)
return 0;
}

rawtex_t *ResourceSystem::rawTexture(lumpnum_t lumpNum)
{
LOG_AS("ResourceSystem::rawTexture");
if(-1 == lumpNum || lumpNum >= F_LumpCount())
{
LOG_DEBUG("LumpNum #%i out of bounds (%i), returning 0.") << lumpNum << F_LumpCount();
return 0;
}

for(rawtex_t *i = d->rawTextureHash(lumpNum).first; i; i = i->next)
{
if(i->lumpNum == lumpNum)
return i;
}
return 0;
}

rawtex_t *ResourceSystem::declareRawTexture(lumpnum_t lumpNum)
{
LOG_AS("ResourceSystem::rawTexture");
if(-1 == lumpNum || lumpNum >= F_LumpCount())
{
LOG_DEBUG("LumpNum #%i out of bounds (%i), returning 0.") << lumpNum << F_LumpCount();
return 0;
}

// Check if this lumpNum has already been loaded as a rawtex.
rawtex_t *r = rawTexture(lumpNum);
if(r) return r;

// Hmm, this is an entirely new rawtex.
r = (rawtex_t *) Z_Calloc(sizeof(*r), PU_REFRESHRAW, 0);
F_FileName(Str_Init(&r->name), Str_Text(F_LumpName(lumpNum)));
r->lumpNum = lumpNum;

// Link to the hash.
Instance::RawTexHash &hash = d->rawTextureHash(lumpNum);
r->next = hash.first;
hash.first = r;

return r;
}

rawtex_t **ResourceSystem::collectRawTextures(int *count)
{
// First count the number of patchtexs.
int num = 0;
for(int i = 0; i < Instance::RAWTEX_HASH_SIZE; ++i)
for(rawtex_t *r = d->rawtexhash[i].first; r; r = r->next)
{
num++;
}

// Tell this to the caller.
if(count) *count = num;

// Allocate the array, plus one for the terminator.
rawtex_t **list = (rawtex_t **) Z_Malloc(sizeof(**list) * (num + 1), PU_APPSTATIC, NULL);

// Collect the pointers.
num = 0;
for(int i = 0; i < Instance::RAWTEX_HASH_SIZE; ++i)
for(rawtex_t *r = d->rawtexhash[i].first; r; r = r->next)
{
list[num++] = r;
}

// Terminate.
list[num] = NULL;

return list;
}

void ResourceSystem::initRawTextures()
{
for(int i = 0; i < Instance::RAWTEX_HASH_SIZE; ++i)
for(rawtex_t *rawTex = d->rawtexhash[i].first; rawTex; rawTex = rawTex->next)
{
Str_Free(&rawTex->name);
}
Z_FreeTags(PU_REFRESHRAW, PU_REFRESHRAW);
zap(d->rawtexhash);
}

MaterialScheme &ResourceSystem::materialScheme(String name) const
{
LOG_AS("ResourceSystem::materialScheme");
Expand Down
6 changes: 3 additions & 3 deletions doomsday/client/src/ui/finaleinterpreter.cpp
Expand Up @@ -1563,7 +1563,7 @@ DEFFC(Image)

FIData_PicClearAnimation(obj);

rawTex = R_GetRawTex(lumpNum);
rawTex = App_ResourceSystem().declareRawTexture(lumpNum);
if(NULL != rawTex)
{
FIData_PicAppendFrame(obj, PFT_RAW, -1, &rawTex->lumpNum, 0, false);
Expand All @@ -1584,7 +1584,7 @@ DEFFC(ImageAt)
AnimatorVector3_Init(obj->pos, x, y, 0);
FIData_PicClearAnimation(obj);

rawTex = R_GetRawTex(lumpNum);
rawTex = App_ResourceSystem().declareRawTexture(lumpNum);
if(NULL != rawTex)
{
FIData_PicAppendFrame(obj, PFT_RAW, -1, &rawTex->lumpNum, 0, false);
Expand Down Expand Up @@ -1721,7 +1721,7 @@ DEFFC(AnimImage)
char const *encodedName = OP_CSTRING(1);
int tics = FRACSECS_TO_TICKS(OP_FLOAT(2));
lumpnum_t lumpNum = F_LumpNumForName(encodedName);
rawtex_t *rawTex = R_GetRawTex(lumpNum);
rawtex_t *rawTex = App_ResourceSystem().declareRawTexture(lumpNum);
if(rawTex)
{
FIData_PicAppendFrame(obj, PFT_RAW, tics, &rawTex->lumpNum, 0, false);
Expand Down

0 comments on commit d6a1636

Please sign in to comment.