1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 09:44:02 +00:00

Refactor texture loading code; should fix a crash that started appearing recently.

This commit is contained in:
fgenesis 2015-03-24 00:06:51 +01:00
parent 66cf20ffa9
commit f0d580d873
22 changed files with 255 additions and 387 deletions

View file

@ -847,16 +847,17 @@ void AquariaMenuItem::useSound(const std::string &tex)
useSfx = tex; useSfx = tex;
} }
void AquariaMenuItem::useQuad(const std::string &tex) bool AquariaMenuItem::useQuad(const std::string &tex)
{ {
if (quad) if (quad)
{ {
debugLog("trying to call useQuad twice on the same object"); debugLog("trying to call useQuad twice on the same object");
return; return true;
} }
quad = new Quad; quad = new Quad;
quad->setTexture(tex); bool good = quad->setTexture(tex);
addChild(quad, PM_POINTER); addChild(quad, PM_POINTER);
return good;
} }
void AquariaMenuItem::useGlow(const std::string &tex, int w, int h) void AquariaMenuItem::useGlow(const std::string &tex, int w, int h)

View file

@ -81,7 +81,7 @@ public:
XMLElement *ability, *xmlItem; XMLElement *ability, *xmlItem;
int choice; int choice;
Quad *glow, *quad; Quad *glow, *quad;
void useQuad(const std::string &tex); bool useQuad(const std::string &tex);
void useGlow(const std::string &tex, int w, int h); void useGlow(const std::string &tex, int w, int h);
void useSound(const std::string &tex); void useSound(const std::string &tex);

View file

@ -4115,8 +4115,7 @@ void Avatar::refreshNormalForm()
hair->alphaMod = 1.0; hair->alphaMod = 1.0;
if (!c.empty() && c!="Naija") if (!c.empty() && c!="Naija")
{ {
hair->setTexture("naija/cape-"+c); if(!hair->setTexture("naija/cape-"+c))
if (Texture::textureError != TEXERR_OK)
hair->alphaMod = 0; hair->alphaMod = 0;
} }
else else

View file

@ -2222,7 +2222,7 @@ void DSQ::refreshResourcesForPatch(const std::string& name)
{ {
for(int i = 0; i < dsq->resources.size(); ++i) for(int i = 0; i < dsq->resources.size(); ++i)
{ {
Resource *r = dsq->resources[i]; Texture *r = dsq->resources[i];
if(files.find(r->name) != files.end()) if(files.find(r->name) != files.end())
r->reload(); r->reload();
} }
@ -2313,7 +2313,7 @@ void DSQ::shutdown()
SkeletalSprite::clearCache(); SkeletalSprite::clearCache();
cursor->setTexturePointer(0, RenderObject::NO_ADD_REF); cursor->setTexturePointer(0);
UNREFTEX(texCursor); UNREFTEX(texCursor);
UNREFTEX(texCursorSwim); UNREFTEX(texCursorSwim);
@ -2410,7 +2410,7 @@ void DSQ::setTexturePointers()
texCursorSing = core->addTexture("cursor-sing"); texCursorSing = core->addTexture("cursor-sing");
if (cursor) if (cursor)
cursor->setTexturePointer(texCursor, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursor);
} }
void DSQ::setCursor(CursorType type) void DSQ::setCursor(CursorType type)
@ -2418,22 +2418,22 @@ void DSQ::setCursor(CursorType type)
switch(type) switch(type)
{ {
case CURSOR_NORMAL: case CURSOR_NORMAL:
cursor->setTexturePointer(texCursor, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursor);
break; break;
case CURSOR_LOOK: case CURSOR_LOOK:
cursor->setTexturePointer(texCursorLook, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursorLook);
break; break;
case CURSOR_BURST: case CURSOR_BURST:
cursor->setTexturePointer(texCursorBurst, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursorBurst);
break; break;
case CURSOR_SWIM: case CURSOR_SWIM:
cursor->setTexturePointer(texCursorSwim, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursorSwim);
break; break;
case CURSOR_SING: case CURSOR_SING:
cursor->setTexturePointer(texCursorSing, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursorSing);
break; break;
default: default:
cursor->setTexturePointer(texCursor, RenderObject::NO_ADD_REF); cursor->setTexturePointer(texCursor);
break; break;
} }
} }
@ -4550,7 +4550,7 @@ void DSQ::onUpdate(float dt)
if (isDeveloperKeys() && fpsText && cmDebug && cmDebug->alpha == 1) if (isDeveloperKeys() && fpsText && cmDebug && cmDebug->alpha == 1)
{ {
std::ostringstream os; std::ostringstream os;
os << "FPS: " << core->fps << " | ROC: " << core->renderObjectCount << " | RC: " << Core::dbg_numRenderCalls; os << "FPS: " << core->fps << " | ROC: " << core->renderObjectCount << " | RC: " << Core::dbg_numRenderCalls << " | RES: " << core->resources.size();
os << " | p: " << core->processedRenderObjectCount << " | t: " << core->totalRenderObjectCount; os << " | p: " << core->processedRenderObjectCount << " | t: " << core->totalRenderObjectCount;
os << " | s: " << dsq->continuity.seconds; os << " | s: " << dsq->continuity.seconds;
os << " | evQ: " << core->eventQueue.getSize(); os << " | evQ: " << core->eventQueue.getSize();

View file

@ -1262,7 +1262,7 @@ public:
Quad *bar_left, *bar_right, *bar_up, *bar_down; Quad *bar_left, *bar_right, *bar_up, *bar_down;
Quad *barFade_left, *barFade_right; Quad *barFade_left, *barFade_right;
Texture *texCursor, *texCursorSwim, *texCursorBurst, *texCursorSing, *texCursorLook; CountedPtr<Texture> texCursor, texCursorSwim, texCursorBurst, texCursorSing, texCursorLook;
void setBlackBarsColor(Vector color); void setBlackBarsColor(Vector color);

View file

@ -66,14 +66,14 @@ namespace MiniMapRenderSpace
const int healthMarkerSize = 20; const int healthMarkerSize = 20;
Texture *texCook = 0; CountedPtr<Texture> texCook = 0;
Texture *texWaterBit = 0; CountedPtr<Texture> texWaterBit = 0;
Texture *texMinimapBtm = 0; CountedPtr<Texture> texMinimapBtm = 0;
Texture *texMinimapTop = 0; CountedPtr<Texture> texMinimapTop = 0;
Texture *texRipple = 0; CountedPtr<Texture> texRipple = 0;
Texture *texNaija = 0; CountedPtr<Texture> texNaija = 0;
Texture *texHealthBar = 0; CountedPtr<Texture> texHealthBar = 0;
Texture *texMarker = 0; CountedPtr<Texture> texMarker = 0;
float waterSin = 0; float waterSin = 0;

View file

@ -640,8 +640,7 @@ bool ModIconOnline::fixIcon()
quad->setDecayRate(2); quad->setDecayRate(2);
quad = 0; quad = 0;
} }
useQuad(iconfile); result = useQuad(iconfile);
result = Texture::textureError == TEXERR_OK;
} }
if(!quad) if(!quad)
{ {

View file

@ -144,6 +144,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "ttvfs_stdio.h" #include "ttvfs_stdio.h"
#include "tinyxml2.h" #include "tinyxml2.h"
#include "Refcounted.h"
#ifdef BBGE_BUILD_LINUX #ifdef BBGE_BUILD_LINUX
# include <sys/types.h> # include <sys/types.h>

View file

@ -45,11 +45,8 @@ void BmpFont::destroy()
font.Destroy(); font.Destroy();
loaded = false; loaded = false;
} }
if (overrideTexture)
{ overrideTexture = NULL;
overrideTexture->removeRef();
overrideTexture = 0;
}
} }
void BmpFont::load(const std::string &file, float scale, bool loadTexture) void BmpFont::load(const std::string &file, float scale, bool loadTexture)

View file

@ -48,7 +48,7 @@ struct BmpFont
Vector fontTopColor; Vector fontTopColor;
Vector fontBtmColor; Vector fontBtmColor;
Texture *overrideTexture; CountedPtr<Texture> overrideTexture;
}; };
class BitmapText : public BaseText class BitmapText : public BaseText

View file

@ -4240,26 +4240,13 @@ void Core::showBuffer()
// when destroy is called on them // when destroy is called on them
void Core::clearResources() void Core::clearResources()
{ {
std::vector<Resource*> deletedResources; if(resources.size())
int i;
for (i = 0; i < resources.size(); i++)
{ {
int j = 0; debugLog("Warning: The following resources were not cleared:");
for (j = 0; j < deletedResources.size(); j++) for(size_t i = 0; i < resources.size(); ++i)
{ debugLog(resources[i]->name);
if (deletedResources[j] == resources[i]) resources.clear(); // nothing we can do; refcounting is messed up
break;
}
if (j == deletedResources.size())
{
deletedResources.push_back (resources[i]);
Resource *r = resources[i];
r->destroy();
delete r;
}
} }
resourceNames.clear();
resources.clear();
} }
void Core::shutdownInputLibrary() void Core::shutdownInputLibrary()
@ -4408,20 +4395,7 @@ bool Core::exists(const std::string &filename)
return ::exists(filename, false); // defined in Base.cpp return ::exists(filename, false); // defined in Base.cpp
} }
Resource* Core::findResource(const std::string &name) CountedPtr<Texture> Core::findTexture(const std::string &name)
{
for (int i = 0; i < resources.size(); i++)
{
if (resources[i]->name == name)
{
return resources[i];
}
}
return 0;
}
Texture* Core::findTexture(const std::string &name)
{ {
//stringToUpper(name); //stringToUpper(name);
//std::ofstream out("texturefind.log"); //std::ofstream out("texturefind.log");
@ -4432,19 +4406,12 @@ Texture* Core::findTexture(const std::string &name)
//NOTE: ensure all names are lowercase before this point //NOTE: ensure all names are lowercase before this point
if (resources[i]->name == name) if (resources[i]->name == name)
{ {
return (Texture*)resources[i]; return resources[i];
} }
} }
return 0; return 0;
} }
std::string Core::getInternalTextureName(const std::string &name)
{
std::string n = name;
stringToUpper(n);
return n;
}
// This handles unix/win32 relative paths: ./rel/path // This handles unix/win32 relative paths: ./rel/path
// Unix abs paths: /home/user/... // Unix abs paths: /home/user/...
// Win32 abs paths: C:/Stuff/.. and also C:\Stuff\... // Win32 abs paths: C:/Stuff/.. and also C:\Stuff\...
@ -4462,7 +4429,7 @@ std::string Core::getTextureLoadName(const std::string &texture)
return loadName; return loadName;
} }
Texture *Core::doTextureAdd(const std::string &texture, const std::string &loadName, std::string internalTextureName) std::pair<CountedPtr<Texture>, TextureLoadResult> Core::doTextureAdd(const std::string &texture, const std::string &loadName, std::string internalTextureName)
{ {
if (texture.empty() || !ISPATHROOT(texture)) if (texture.empty() || !ISPATHROOT(texture))
{ {
@ -4479,47 +4446,32 @@ Texture *Core::doTextureAdd(const std::string &texture, const std::string &loadN
} }
stringToLowerUserData(internalTextureName); stringToLowerUserData(internalTextureName);
Texture *t = core->findTexture(internalTextureName); CountedPtr<Texture> t = core->findTexture(internalTextureName);
if (t) if (t)
{ return std::make_pair(t, TEX_SUCCESS);
t->addRef();
Texture::textureError = t->failed ? TEXERR_FILENOTFOUND : TEXERR_OK;
/*
std::ostringstream os;
os << "reference texture: " << internalTextureName << " ref: " << t->getRef();
debugLog(os.str());
*/
//msg ("found texture " + internalTextureName);
return t;
}
t = new Texture; t = new Texture;
t->name = internalTextureName; t->name = internalTextureName;
t->load(loadName); unsigned res = TEX_FAILED;
t->addRef();
//resources.push_back (t);
addResource(t);
if (debugLogTextures) if(t->load(loadName))
res |= (TEX_LOADED | TEX_SUCCESS);
else
{ {
std::ostringstream os; t->width = 64;
os << "LOADED TEXTURE FROM DISK: [" << internalTextureName << "] ref: " << t->getRef() << " idx: " << resources.size()-1; t->height = 64;
debugLog(os.str());
} }
return t; return std::make_pair(t, (TextureLoadResult)res);
} }
Texture* Core::addTexture(const std::string &textureName) CountedPtr<Texture> Core::addTexture(const std::string &textureName)
{ {
if (textureName.empty()) return 0; if (textureName.empty()) return NULL;
BBGE_PROF(Core_addTexture); BBGE_PROF(Core_addTexture);
Texture *texPointer = 0; std::pair<CountedPtr<Texture>, TextureLoadResult> texResult;
std::string texture = textureName; std::string texture = textureName;
stringToLowerUserData(texture); stringToLowerUserData(texture);
@ -4537,30 +4489,34 @@ Texture* Core::addTexture(const std::string &textureName)
std::string ln = loadName; std::string ln = loadName;
texture = secondaryTexturePath + texture; texture = secondaryTexturePath + texture;
loadName = texture; loadName = texture;
texPointer = doTextureAdd(texture, loadName, internalTextureName); texResult = doTextureAdd(texture, loadName, internalTextureName);
if (Texture::textureError != TEXERR_OK) if (!texResult.second)
{ texResult = doTextureAdd(t, ln, internalTextureName);
if (texPointer)
{
texPointer->destroy();
texPointer = 0;
}
texPointer = doTextureAdd(t, ln, internalTextureName);
}
} }
else else
texPointer = doTextureAdd(texture, loadName, internalTextureName); texResult = doTextureAdd(texture, loadName, internalTextureName);
return texPointer; addTexture(texResult.first.content());
if(debugLogTextures)
{
if (texResult.second & TEX_LOADED)
{
std::ostringstream os;
os << "LOADED TEXTURE FROM DISK: [" << internalTextureName << "] idx: " << resources.size()-1;
debugLog(os.str());
}
else if(!(texResult.second & TEX_SUCCESS))
{
std::ostringstream os;
os << "FAILED TO LOAD TEXTURE: [" << internalTextureName << "] idx: " << resources.size()-1;
debugLog(os.str());
}
}
return texResult.first;
} }
void Core::removeTexture(std::string texture)
{
//std::string internalName = baseTextureDirectory + texture;
removeResource(texture, DESTROY);
}
void Core::addRenderObject(RenderObject *o, int layer) void Core::addRenderObject(RenderObject *o, int layer)
{ {
if (!o) return; if (!o) return;
@ -4603,59 +4559,36 @@ void Core::reloadResources()
onReloadResources(); onReloadResources();
} }
void Core::addResource(Resource *r) void Core::addTexture(Texture *r)
{ {
for(size_t i = 0; i < resources.size(); ++i)
if(resources[i] == r)
return;
resources.push_back(r); resources.push_back(r);
resourceNames.push_back(r->name);
if (r->name.empty()) if (r->name.empty())
{ {
debugLog("Empty name resource added"); debugLog("Empty name resource added");
} }
} }
void Core::removeResource(std::string name, RemoveResource removeFlag) void Core::removeTexture(Texture *res)
{ {
//Resource *r = findResource(name); std::vector<Texture*> copy;
//int idx = 0; copy.swap(resources);
int i = 0;
std::vector<Resource*>copy;
copy = resources;
resources.clear();
for (size_t i = 0; i < copy.size(); ++i)
std::vector <std::string> copyNames;
copyNames = resourceNames;
resourceNames.clear();
bool isDestroyed = false;
for (i = 0; i < copy.size(); i++)
{ {
#ifdef _DEBUG if (copy[i] == res)
std::string s = copy[i]->name;
#endif
if (!isDestroyed && copy[i]->name == name)
{ {
if (removeFlag == DESTROY) copy[i]->destroy();
{ copy[i] = copy.back();
copy[i]->destroy(); copy.pop_back();
delete copy[i]; break;
isDestroyed = true;
}
continue;
}
// also remove other entries of the same resource
else if (isDestroyed && copyNames[i] == name)
{
continue;
}
else
{
resources.push_back(copy[i]);
resourceNames.push_back(copy[i]->name);
} }
} }
resources.swap(copy);
} }
void Core::deleteRenderObjectMemory(RenderObject *r) void Core::deleteRenderObjectMemory(RenderObject *r)
@ -4709,25 +4642,6 @@ void Core::clearGarbage()
} }
garbage.clear(); garbage.clear();
// to clear resources
for (std::vector<Resource*>::iterator i = resources.begin(); i != resources.end(); )
{
if ((*i)->getRef() == 0)
{
clearedGarbageFlag = true;
delete (*i);
i = resources.erase(i);
continue;
}
if ((*i)->getRef() < 0)
{
errorLog("Texture ref < 0");
}
i++;
}
} }
bool Core::canChangeState() bool Core::canChangeState()

View file

@ -964,11 +964,6 @@ class Core : public ActionMapper, public StateManager
{ {
public: public:
enum RemoveResource
{
DESTROY = 0,
NO_DESTROY
};
// init // init
Core(const std::string &filesystem, const std::string& extraDataDir, int numRenderLayers, const std::string &appName="BBGE", int particleSize=1024, std::string userDataSubFolder=""); Core(const std::string &filesystem, const std::string& extraDataDir, int numRenderLayers, const std::string &appName="BBGE", int particleSize=1024, std::string userDataSubFolder="");
void initPlatform(const std::string &filesystem); void initPlatform(const std::string &filesystem);
@ -1023,13 +1018,12 @@ public:
void enable2D(int pixelScaleX=0, int pixelScaleY=0, bool forcePixelScale=false); void enable2D(int pixelScaleX=0, int pixelScaleY=0, bool forcePixelScale=false);
void addRenderObject(RenderObject *o, int layer=0); void addRenderObject(RenderObject *o, int layer=0);
void switchRenderObjectLayer(RenderObject *o, int toLayer); void switchRenderObjectLayer(RenderObject *o, int toLayer);
void addResource(Resource *r); void addTexture(Texture *r);
Resource *findResource(const std::string &name); CountedPtr<Texture> findTexture(const std::string &name);
Texture *findTexture(const std::string &name); void removeTexture(Texture *res);
void removeResource(std::string name, RemoveResource removeFlag); void clearResources();
Texture *addTexture(const std::string &texture); CountedPtr<Texture> addTexture(const std::string &texture);
void removeTexture(std::string texture);
PostProcessingFX postProcessingFx; PostProcessingFX postProcessingFx;
@ -1047,7 +1041,6 @@ public:
void enqueueRenderObjectDeletion(RenderObject *object); void enqueueRenderObjectDeletion(RenderObject *object);
void clearGarbage(); void clearGarbage();
void clearResources();
bool isNested() { return nestedMains > 1; } bool isNested() { return nestedMains > 1; }
@ -1103,8 +1096,7 @@ public:
HINSTANCE hInstance; // Holds The Instance Of The Application HINSTANCE hInstance; // Holds The Instance Of The Application
#endif #endif
std::vector<Resource*>resources; std::vector<Texture*> resources;
std::vector<std::string> resourceNames;
RenderObjectLayer *getRenderObjectLayer(int i); RenderObjectLayer *getRenderObjectLayer(int i);
std::vector <int> renderObjectLayerOrder; std::vector <int> renderObjectLayerOrder;
@ -1247,7 +1239,6 @@ public:
Joystick joystick; Joystick joystick;
std::string getInternalTextureName(const std::string &name);
void setClearColor(const Vector &c); void setClearColor(const Vector &c);
Vector getClearColor(); Vector getClearColor();
int flipMouseButtons; int flipMouseButtons;
@ -1337,7 +1328,7 @@ protected:
virtual void onReloadResources(); virtual void onReloadResources();
Texture* doTextureAdd(const std::string &texture, const std::string &name, std::string internalTextureName); std::pair<CountedPtr<Texture>, TextureLoadResult> doTextureAdd(const std::string &texture, const std::string &name, std::string internalTextureName);
void deleteRenderObjectMemory(RenderObject *r); void deleteRenderObjectMemory(RenderObject *r);
bool _hasFocus; bool _hasFocus;
@ -1398,6 +1389,8 @@ protected:
int numSavedScreenshots; int numSavedScreenshots;
CountedPtr<Texture> texError;
//unsigned int windowWidth, windowHeight; //unsigned int windowWidth, windowHeight;

View file

@ -798,6 +798,11 @@ void Quad::onSetTexture()
width = this->texture->width; width = this->texture->width;
height = this->texture->height; height = this->texture->height;
} }
else
{
width = 64;
height = 64;
}
} }
PauseQuad::PauseQuad() : Quad(), pauseLevel(0) PauseQuad::PauseQuad() : Quad(), pauseLevel(0)

95
BBGE/Refcounted.h Normal file
View file

@ -0,0 +1,95 @@
#ifndef CG_CORE_REFCOUNTED_H
#define CG_CORE_REFCOUNTED_H
#include <stddef.h>
#include <assert.h>
#include <algorithm>
class Refcounted
{
protected:
Refcounted() : _refcount(0)
{
}
virtual ~Refcounted()
{
assert(_refcount == 0 && "Object was deleted with refcount != 0");
}
public:
inline void incref()
{
++_refcount;
}
inline void decref()
{
if (!--_refcount)
delete this;
}
private:
unsigned _refcount;
};
template<typename T> class CountedPtr
{
public:
inline ~CountedPtr()
{
if(_p)
_p->decref();
}
inline CountedPtr() : _p(NULL)
{}
inline CountedPtr(T* p) : _p(p)
{
if(p)
p->incref();
}
inline CountedPtr(const CountedPtr& ref) : _p(ref._p)
{
if (_p)
_p->incref();
}
// intentionally not a reference -- see http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
CountedPtr& operator=(CountedPtr ref)
{
CountedPtr::swap(*this, ref);
return *this;
}
const T* operator->() const { return _p; }
T* operator->() { return _p; }
bool operator!() const { return !_p; }
// Safe for use in if statements
operator bool() const { return !!_p; }
T* content () { return _p; }
const T* content () const { return _p; }
bool operator<(const CountedPtr& ref) const { return _p < ref._p; }
bool operator<=(const CountedPtr& ref) const { return _p <= ref._p; }
bool operator==(const CountedPtr& ref) const { return _p == ref._p; }
bool operator!=(const CountedPtr& ref) const { return _p != ref._p; }
bool operator>=(const CountedPtr& ref) const { return _p >= ref._p; }
bool operator>(const CountedPtr& ref) const { return _p > ref._p; }
inline static void swap(CountedPtr& a, CountedPtr& b)
{
std::swap(a._p, b._p);
}
private:
T *_p;
};
#endif

View file

@ -416,11 +416,8 @@ void RenderObject::destroy()
parent->removeChild(this); parent->removeChild(this);
parent = 0; parent = 0;
} }
if (texture)
{ texture = NULL;
texture->removeRef();
texture = 0;
}
} }
void RenderObject::copyProperties(RenderObject *target) void RenderObject::copyProperties(RenderObject *target)
@ -1320,26 +1317,20 @@ void RenderObject::reloadDevice()
} }
} }
void RenderObject::setTexture(const std::string &n) bool RenderObject::setTexture(const std::string &n)
{ {
std::string name = n; std::string name = n;
stringToLowerUserData(name); stringToLowerUserData(name);
if (name.empty()) if (name.empty())
return; return false;
if(texture && !texture->failed && texture->name == core->getInternalTextureName(name)) if(texture && name == texture->name)
{ return true; // no texture change
Texture::textureError = TEXERR_OK;
return; // no texture change
}
Texture *oldtex = texture; CountedPtr<Texture> tex = core->addTexture(name);
Texture *t = core->addTexture(name); setTexturePointer(tex);
setTexturePointer(t, NO_ADD_REF); return !!tex;
if (oldtex)
oldtex->removeRef();
} }
float RenderObject::getSortDepth() float RenderObject::getSortDepth()

View file

@ -83,18 +83,14 @@ public:
static RenderObjectLayer *rlayer; static RenderObjectLayer *rlayer;
enum AddRefChoice { NO_ADD_REF = 0, ADD_REF = 1}; void setTexturePointer(CountedPtr<Texture> t)
void setTexturePointer(Texture *t, AddRefChoice addRefChoice)
{ {
this->texture = t; this->texture = t;
if (addRefChoice == ADD_REF)
texture->addRef();
onSetTexture(); onSetTexture();
} }
void setStateDataObject(StateData *state); void setStateDataObject(StateData *state);
void setTexture(const std::string &name); bool setTexture(const std::string &name);
void toggleAlpha(float t = 0.2); void toggleAlpha(float t = 0.2);
void matrixChain(); void matrixChain();
@ -239,7 +235,7 @@ public:
InterpolatedVector offset, rotationOffset, internalOffset, beforeScaleOffset; InterpolatedVector offset, rotationOffset, internalOffset, beforeScaleOffset;
InterpolatedVector velocity, gravity; InterpolatedVector velocity, gravity;
Texture *texture; CountedPtr<Texture> texture;
//int mode; //int mode;

View file

@ -1,28 +0,0 @@
/*
Copyright (C) 2007, 2010 - Bit-Blot
This file is part of Aquaria.
Aquaria is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Resource.h"
#include "Core.h"
void Resource::destroy()
{
core->removeResource(this->name, Core::NO_DESTROY);
}

View file

@ -1,57 +0,0 @@
/*
Copyright (C) 2007, 2010 - Bit-Blot
This file is part of Aquaria.
Aquaria is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __resource__
#define __resource__
#include "Base.h"
class Resource
{
public:
Resource()
{
ref = 0;
}
~Resource()
{
destroy();
}
virtual void destroy();
void addRef()
{
ref++;
}
void removeRef()
{
ref--;
if (ref == 0)
destroy();
}
int getRef() { return ref; }
std::string name;
virtual void reload() {}
virtual void unload() {}
protected:
int ref;
};
#endif

View file

@ -54,9 +54,8 @@ bool Texture::useMipMaps = true;
#endif #endif
*/ */
TexErr Texture::textureError = TEXERR_OK;
Texture::Texture() : Resource() Texture::Texture()
{ {
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
textures[0] = 0; textures[0] = 0;
@ -68,9 +67,7 @@ Texture::Texture() : Resource()
repeat = false; repeat = false;
repeating = false; repeating = false;
failed = false;
pngSetStandardOrientation(0); pngSetStandardOrientation(0);
ow = oh = -1; ow = oh = -1;
} }
@ -152,7 +149,6 @@ void Texture::write(int tx, int ty, int w, int h, const unsigned char *pixels)
void Texture::unload() void Texture::unload()
{ {
Resource::unload();
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
if (textures[0]) if (textures[0])
{ {
@ -167,8 +163,6 @@ void Texture::unload()
glDeleteTextures(1, &textures[0]); glDeleteTextures(1, &textures[0]);
textures[0] = 0; textures[0] = 0;
//removeRef();
} }
#endif #endif
} }
@ -186,10 +180,7 @@ void Texture::destroy()
} }
#endif #endif
if (!core->isShuttingDown()) core->removeTexture(this);
core->removeTexture(this->name);
// Resource::destroy();
} }
int Texture::getPixelWidth() int Texture::getPixelWidth()
@ -256,8 +247,6 @@ int Texture::getPixelHeight()
void Texture::reload() void Texture::reload()
{ {
Resource::reload();
debugLog("RELOADING TEXTURE: " + name + " with loadName " + loadName + "..."); debugLog("RELOADING TEXTURE: " + name + " with loadName " + loadName + "...");
unload(); unload();
@ -271,17 +260,12 @@ void Texture::reload()
debugLog("DONE"); debugLog("DONE");
} }
void Texture::load(std::string file) bool Texture::load(std::string file)
{ {
Texture::textureError = TEXERR_OK;
failed = false;
if (file.size()<4) if (file.size()<4)
{ {
errorLog("Texture Name is Empty or Too Short"); errorLog("Texture Name is Empty or Too Short");
Texture::textureError = TEXERR_FILENOTFOUND; return false;
failed = true;
return;
} }
stringToLowerUserData(file); stringToLowerUserData(file);
@ -335,7 +319,7 @@ void Texture::load(std::string file)
{ {
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
loadPNG(file); return loadPNG(file);
#endif #endif
#ifdef BBGE_BUILD_DIRECTX #ifdef BBGE_BUILD_DIRECTX
@ -356,19 +340,15 @@ void Texture::load(std::string file)
} }
else if (post == "zga") else if (post == "zga")
{ {
loadZGA(file); return loadZGA(file);
} }
else if (post == "tga") else if (post == "tga")
{ {
loadTGA(file); return loadTGA(file);
} }
else else
{ {
debugLog("unknown image file type: " + file); debugLog("unknown image file type: " + file);
Texture::textureError = TEXERR_FILENOTFOUND;
width = 64;
height = 64;
failed = true;
} }
} }
else else
@ -376,11 +356,8 @@ void Texture::load(std::string file)
// load default image / leave white // load default image / leave white
if (core->debugLogTextures) if (core->debugLogTextures)
debugLog("***Could not find texture: " + file); debugLog("***Could not find texture: " + file);
Texture::textureError = TEXERR_FILENOTFOUND;
width = 64;
height = 64;
failed = true;
} }
return false;
} }
void Texture::apply(bool repeatOverride) void Texture::apply(bool repeatOverride)
@ -416,18 +393,10 @@ void Texture::unbind()
{ {
} }
#ifdef BBGE_BUILD_OPENGL bool Texture::loadPNG(const std::string &file)
void Texture::setID(int id)
{ {
textures[0] = id; if (file.empty()) return false;
} bool good = false;
#endif
void Texture::loadPNG(const std::string &file)
{
if (file.empty()) return;
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
@ -435,7 +404,7 @@ void Texture::loadPNG(const std::string &file)
pngInfo info; pngInfo info;
int pngType = PNG_ALPHA; int pngType = PNG_ALPHA;
if (format != 0) if (format != 0)
{ {
if (format == GL_LUMINANCE_ALPHA) if (format == GL_LUMINANCE_ALPHA)
@ -460,31 +429,29 @@ void Texture::loadPNG(const std::string &file)
{ {
width = info.Width; width = info.Width;
height = info.Height; height = info.Height;
good = true;
} }
else else
{ {
fail: fail:
debugLog("Can't load PNG file: " + file); debugLog("Can't load PNG file: " + file);
width = 64;
height = 64;
Texture::textureError = TEXERR_FILENOTFOUND;
failed = true;
} }
if(memptr) if(memptr)
delete [] memptr; delete [] memptr;
#endif #endif
return good;
} }
// internal load functions // internal load functions
void Texture::loadTGA(const std::string &file) bool Texture::loadTGA(const std::string &file)
{ {
loadTGA(TGAload(file.c_str())); return loadTGA(TGAload(file.c_str()));
} }
void Texture::loadZGA(const std::string &file) bool Texture::loadZGA(const std::string &file)
{ {
unsigned long size = 0; unsigned long size = 0;
char *buf = readCompressedFile(file, &size); char *buf = readCompressedFile(file, &size);
@ -492,15 +459,15 @@ void Texture::loadZGA(const std::string &file)
if (!tga) if (!tga)
{ {
debugLog("Can't load ZGA File: " + file); debugLog("Can't load ZGA File: " + file);
return; return false;
} }
loadTGA(tga); return loadTGA(tga);
} }
void Texture::loadTGA(ImageTGA *imageTGA) bool Texture::loadTGA(ImageTGA *imageTGA)
{ {
if (!imageTGA) if (!imageTGA)
return; return false;
glGenTextures(1, &textures[0]); glGenTextures(1, &textures[0]);
glBindTexture(GL_TEXTURE_2D, textures[0]); glBindTexture(GL_TEXTURE_2D, textures[0]);
@ -518,6 +485,8 @@ void Texture::loadTGA(ImageTGA *imageTGA)
if (imageTGA->data) if (imageTGA->data)
delete[] (imageTGA->data); delete[] (imageTGA->data);
free (imageTGA); free (imageTGA);
return true;
} }

View file

@ -21,7 +21,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __texture__ #ifndef __texture__
#define __texture__ #define __texture__
#include "Resource.h" #include "Base.h"
enum TextureLoadResult
{
TEX_FAILED = 0x00,
TEX_SUCCESS = 0x01,
TEX_LOADED = 0x02,
};
struct ImageTGA struct ImageTGA
{ {
@ -31,24 +38,16 @@ struct ImageTGA
unsigned char *data; // The image pixel data unsigned char *data; // The image pixel data
}; };
enum TexErr class Texture : public Refcounted
{
TEXERR_OK = 0,
TEXERR_FILENOTFOUND = 1,
TEXERR_MAX
};
class Texture : public Resource
{ {
public: public:
Texture(); Texture();
~Texture(); ~Texture();
void load(std::string file); bool load(std::string file);
void apply(bool repeatOverride=false); void apply(bool repeatOverride=false);
void unbind(); void unbind();
void unload(); void unload();
void setLayer(int layer);
int getPixelWidth(); int getPixelWidth();
int getPixelHeight(); int getPixelHeight();
@ -62,12 +61,11 @@ public:
static ImageTGA *TGAloadMem(void *mem, int size); static ImageTGA *TGAloadMem(void *mem, int size);
static bool useMipMaps; static bool useMipMaps;
bool repeat, repeating, failed; bool repeat, repeating;
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
static GLint filter; static GLint filter;
static GLint format; static GLint format;
void setID (int id);
GLuint textures[1]; GLuint textures[1];
#endif #endif
#ifdef BBGE_BUILD_DIRECTX #ifdef BBGE_BUILD_DIRECTX
@ -76,26 +74,26 @@ public:
void reload(); void reload();
static TexErr textureError;
void write(int tx, int ty, int w, int h, const unsigned char *pixels); void write(int tx, int ty, int w, int h, const unsigned char *pixels);
void read(int tx, int ty, int w, int h, unsigned char *pixels); void read(int tx, int ty, int w, int h, unsigned char *pixels);
unsigned char *getBufferAndSize(int *w, int *h, unsigned int *size); // returned memory must be free()'d unsigned char *getBufferAndSize(int *w, int *h, unsigned int *size); // returned memory must be free()'d
std::string name;
protected: protected:
std::string loadName; std::string loadName;
// internal load functions // internal load functions
void loadPNG(const std::string &file); bool loadPNG(const std::string &file);
void loadTGA(const std::string &file); bool loadTGA(const std::string &file);
void loadZGA(const std::string &file); bool loadZGA(const std::string &file);
void loadTGA(ImageTGA *tga); bool loadTGA(ImageTGA *tga);
int ow, oh; int ow, oh;
}; };
#define UNREFTEX(x) if (x) {x->removeRef(); x=0;} #define UNREFTEX(x) if (x) {x = NULL;}
#endif #endif

View file

@ -482,7 +482,6 @@ SET(BBGE_SRCS
${BBGEDIR}/RenderObject.cpp ${BBGEDIR}/RenderObject.cpp
${BBGEDIR}/RenderObjectLayer.cpp ${BBGEDIR}/RenderObjectLayer.cpp
${BBGEDIR}/RenderRect.cpp ${BBGEDIR}/RenderRect.cpp
${BBGEDIR}/Resource.cpp
${BBGEDIR}/RoundedRect.cpp ${BBGEDIR}/RoundedRect.cpp
${BBGEDIR}/ScreenTransition.cpp ${BBGEDIR}/ScreenTransition.cpp
${BBGEDIR}/ScriptObject.cpp ${BBGEDIR}/ScriptObject.cpp

View file

@ -396,6 +396,10 @@
RelativePath="..\..\BBGE\Rect.h" RelativePath="..\..\BBGE\Rect.h"
> >
</File> </File>
<File
RelativePath="..\..\BBGE\Refcounted.h"
>
</File>
<File <File
RelativePath="..\..\BBGE\RenderObject.cpp" RelativePath="..\..\BBGE\RenderObject.cpp"
> >
@ -416,14 +420,6 @@
RelativePath="..\..\BBGE\RenderRect.cpp" RelativePath="..\..\BBGE\RenderRect.cpp"
> >
</File> </File>
<File
RelativePath="..\..\BBGE\Resource.cpp"
>
</File>
<File
RelativePath="..\..\BBGE\Resource.h"
>
</File>
<File <File
RelativePath="..\..\BBGE\RoundedRect.cpp" RelativePath="..\..\BBGE\RoundedRect.cpp"
> >