Reduced the number of explicit retain/release method calls on love objects. Less chance of bugs!

This commit is contained in:
Alex Szpakowski
2014-08-07 14:53:17 -03:00
parent a0fff798aa
commit d59c76a55f
38 changed files with 194 additions and 247 deletions
-3
View File
@@ -87,13 +87,10 @@ Font::Font(love::font::Rasterizer *r, const Texture::Filter &filter)
}
delete gd;
rasterizer->retain();
}
Font::~Font()
{
rasterizer->release();
unloadVolatile();
}
+1 -1
View File
@@ -183,7 +183,7 @@ private:
Glyph *addGlyph(uint32 glyph);
Glyph *findGlyph(uint32 glyph);
love::font::Rasterizer *rasterizer;
Object::StrongRef<love::font::Rasterizer> rasterizer;
int height;
float lineHeight;
+39 -80
View File
@@ -95,8 +95,8 @@ void Graphics::restoreState(const DisplayState &s)
else
setScissor();
setFont(s.font);
setShader(s.shader);
setFont(s.font.get());
setShader(s.shader.get());
setCanvas(s.canvases);
setColorMask(s.colorMask);
@@ -135,12 +135,12 @@ void Graphics::restoreStateChecked(const DisplayState &s)
setScissor();
}
setFont(s.font);
setShader(s.shader);
setFont(s.font.get());
setShader(s.shader.get());
for (size_t i = 0; i < s.canvases.size() && i < cur.canvases.size(); i++)
{
if (s.canvases[i] != cur.canvases[i])
if (s.canvases[i].get() != cur.canvases[i].get())
{
setCanvas(s.canvases);
break;
@@ -606,19 +606,12 @@ Color Graphics::getBackgroundColor() const
void Graphics::setFont(Font *font)
{
DisplayState &state = states.back();
if (font != nullptr)
font->retain();
if (state.font != nullptr)
state.font->release();
state.font = font;
state.font.set(font);
}
Font *Graphics::getFont() const
{
return states.back().font;
return states.back().font.get();
}
void Graphics::setShader(Shader *shader)
@@ -630,13 +623,7 @@ void Graphics::setShader(Shader *shader)
shader->attach();
if (shader)
shader->retain();
if (state.shader)
state.shader->release();
state.shader = shader;
state.shader.set(shader);
}
void Graphics::setShader()
@@ -645,15 +632,12 @@ void Graphics::setShader()
Shader::detach();
if (state.shader)
state.shader->release();
state.shader = nullptr;
state.shader.set(nullptr);
}
Shader *Graphics::getShader() const
{
return states.back().shader;
return states.back().shader.get();
}
void Graphics::setCanvas(Canvas *canvas)
@@ -665,13 +649,10 @@ void Graphics::setCanvas(Canvas *canvas)
canvas->startGrab();
canvas->retain();
std::vector<Object::StrongRef<Canvas>> canvasref;
canvasref.push_back(canvas);
for (Canvas *c : state.canvases)
c->release();
state.canvases.clear();
state.canvases.push_back(canvas);
std::swap(state.canvases, canvasref);
}
void Graphics::setCanvas(const std::vector<Canvas *> &canvases)
@@ -686,13 +667,24 @@ void Graphics::setCanvas(const std::vector<Canvas *> &canvases)
auto attachments = std::vector<Canvas *>(canvases.begin() + 1, canvases.end());
canvases[0]->startGrab(attachments);
std::vector<Object::StrongRef<Canvas>> canvasrefs;
canvasrefs.reserve(canvases.size());
for (Canvas *c : canvases)
c->retain();
canvasrefs.push_back(c);
for (Canvas *c : state.canvases)
c->release();
std::swap(state.canvases, canvasrefs);
}
state.canvases = canvases;
void Graphics::setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases)
{
std::vector<Canvas *> canvaslist;
canvaslist.reserve(canvases.size());
for (const Object::StrongRef<Canvas> &c : canvases)
canvaslist.push_back(c.get());
return setCanvas(canvaslist);
}
void Graphics::setCanvas()
@@ -702,15 +694,18 @@ void Graphics::setCanvas()
if (Canvas::current != nullptr)
Canvas::current->stopGrab();
for (Canvas *c : state.canvases)
c->release();
state.canvases.clear();
}
std::vector<Canvas *> Graphics::getCanvas() const
{
return states.back().canvases;
std::vector<Canvas *> canvases;
canvases.reserve(states.back().canvases.size());
for (const Object::StrongRef<Canvas> &c : states.back().canvases)
canvases.push_back(c.get());
return canvases;
}
void Graphics::setColorMask(const bool mask[4])
@@ -876,7 +871,7 @@ void Graphics::print(const std::string &str, float x, float y , float angle, flo
{
DisplayState &state = states.back();
if (state.font != nullptr)
if (state.font.get() != nullptr)
state.font->print(str, x, y, 0.0, angle, sx, sy, ox, oy, kx, ky);
}
@@ -884,7 +879,7 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Alig
{
DisplayState &state = states.back();
if (state.font == nullptr)
if (state.font.get() == nullptr)
return;
if (wrap < 0.0f)
@@ -1266,12 +1261,8 @@ void Graphics::pop()
// Hack: the Lua-facing love.graphics.print function will set the current
// font if needed, but only on its first call... we always want a font.
if (newstate.font == nullptr)
{
newstate.font = states.back().font;
if (newstate.font != nullptr)
newstate.font->retain();
}
if (newstate.font.get() == nullptr)
newstate.font.set(states.back().font.get());
restoreStateChecked(newstate);
@@ -1347,27 +1338,10 @@ Graphics::DisplayState::DisplayState(const DisplayState &other)
{
for (int i = 0; i < 4; i++)
colorMask[i] = other.colorMask[i];
if (font)
font->retain();
if (shader)
shader->retain();
for (Canvas *c : canvases)
c->retain();
}
Graphics::DisplayState::~DisplayState()
{
for (Canvas *c : canvases)
c->release();
if (shader)
shader->release();
if (font)
font->release();
}
Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState &other)
@@ -1383,23 +1357,8 @@ Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState &
scissor = other.scissor;
scissorBox = other.scissorBox;
Object::AutoRelease fontrelease(font);
font = other.font;
if (font)
font->retain();
Object::AutoRelease shaderrelease(shader);
shader = other.shader;
if (shader)
shader->retain();
for (Canvas *c : other.canvases)
c->retain();
for (Canvas *c : canvases)
c->release();
canvases = other.canvases;
for (int i = 0; i < 4; i++)
+5 -3
View File
@@ -208,6 +208,7 @@ public:
void setCanvas(Canvas *canvas);
void setCanvas(const std::vector<Canvas *> &canvases);
void setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases);
void setCanvas();
std::vector<Canvas *> getCanvas() const;
@@ -459,9 +460,10 @@ private:
bool scissor;
OpenGL::Viewport scissorBox;
Font *font;
Shader *shader;
std::vector<Canvas *> canvases;
Object::StrongRef<Font> font;
Object::StrongRef<Shader> shader;
std::vector<Object::StrongRef<Canvas>> canvases;
// Color mask.
bool colorMask[4];
+13 -21
View File
@@ -50,8 +50,6 @@ Image::Image(love::image::ImageData *data, Format format)
{
width = data->getWidth();
height = data->getHeight();
data->retain();
preload();
}
@@ -69,28 +67,22 @@ Image::Image(love::image::CompressedData *cdata, Format format)
{
width = cdata->getWidth(0);
height = cdata->getHeight(0);
cdata->retain();
preload();
}
Image::~Image()
{
if (data != nullptr)
data->release();
if (cdata != nullptr)
cdata->release();
unload();
}
love::image::ImageData *Image::getImageData() const
{
return data;
return data.get();
}
love::image::CompressedData *Image::getCompressedData() const
{
return cdata;
return cdata.get();
}
void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
@@ -140,7 +132,7 @@ GLuint Image::getGLTexture() const
void Image::uploadCompressedMipmaps()
{
if (!isCompressed() || !cdata || !hasCompressedTextureSupport(cdata->getFormat()))
if (!isCompressed() || !cdata.get() || !hasCompressedTextureSupport(cdata->getFormat()))
return;
bind();
@@ -175,7 +167,7 @@ void Image::uploadCompressedMipmaps()
void Image::createMipmaps()
{
// Only valid for Images created with ImageData.
if (!data || isCompressed())
if (!data.get() || isCompressed())
return;
if (!hasMipmapSupport())
@@ -231,9 +223,9 @@ void Image::checkMipmapsCreated()
if (mipmapsCreated || filter.mipmap == FILTER_NONE || usingDefaultTexture)
return;
if (isCompressed() && cdata && hasCompressedTextureSupport(cdata->getFormat()))
if (isCompressed() && cdata.get() && hasCompressedTextureSupport(cdata->getFormat()))
uploadCompressedMipmaps();
else if (data)
else if (data.get())
createMipmaps();
else
return;
@@ -334,7 +326,7 @@ bool Image::loadVolatile()
if (format == FORMAT_SRGB && !hasSRGBSupport())
throw love::Exception("sRGB images are not supported on this system.");
if (isCompressed() && cdata && !hasCompressedTextureSupport(cdata->getFormat()))
if (isCompressed() && cdata.get() && !hasCompressedTextureSupport(cdata->getFormat()))
{
const char *str;
if (image::CompressedData::getConstant(cdata->getFormat(), str))
@@ -375,7 +367,7 @@ bool Image::loadVolatile()
// Mutex lock will potentially cover texture loading and mipmap creation.
love::thread::EmptyLock lock;
if (data)
if (data.get())
lock.setLock(data->getMutex());
while (glGetError() != GL_NO_ERROR); // Clear errors.
@@ -398,13 +390,13 @@ bool Image::loadVolatile()
void Image::uploadTexturePadded()
{
if (isCompressed() && cdata)
if (isCompressed() && cdata.get())
{
// Padded textures don't really work if they're compressed...
throw love::Exception("Cannot create image: "
"compressed NPOT images are not supported on this system.");
}
else if (data)
else if (data.get())
{
GLenum iformat = (format == FORMAT_SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
glTexImage2D(GL_TEXTURE_2D,
@@ -430,7 +422,7 @@ void Image::uploadTexturePadded()
void Image::uploadTexture()
{
if (isCompressed() && cdata)
if (isCompressed() && cdata.get())
{
GLenum format = getCompressedFormat(cdata->getFormat());
glCompressedTexImage2DARB(GL_TEXTURE_2D,
@@ -442,7 +434,7 @@ void Image::uploadTexture()
GLsizei(cdata->getSize(0)),
cdata->getData(0));
}
else if (data)
else if (data.get())
{
GLenum iformat = (format == FORMAT_SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
glTexImage2D(GL_TEXTURE_2D,
@@ -484,7 +476,7 @@ bool Image::refresh()
bind();
if (data && !isCompressed())
if (data.get() && !isCompressed())
lock.setLock(data->getMutex());
while (glGetError() != GL_NO_ERROR); // Clear errors.
+2 -2
View File
@@ -156,11 +156,11 @@ private:
// The ImageData from which the texture is created. May be null if
// Compressed image data was used to create the texture.
love::image::ImageData *data;
Object::StrongRef<love::image::ImageData> data;
// Or the Compressed Image Data from which the texture is created. May be
// null if raw ImageData was used to create the texture.
love::image::CompressedData *cdata;
Object::StrongRef<love::image::CompressedData> cdata;
// Real dimensions of the texture, if it was auto-padded to POT size.
int paddedWidth, paddedHeight;
+5 -16
View File
@@ -79,9 +79,6 @@ Mesh::Mesh(int vertexcount, Mesh::DrawMode mode)
Mesh::~Mesh()
{
if (texture)
texture->release();
delete vbo;
delete ibo;
}
@@ -264,25 +261,17 @@ size_t Mesh::getVertexMapCount() const
void Mesh::setTexture(Texture *tex)
{
tex->retain();
if (texture)
texture->release();
texture = tex;
texture.set(tex);
}
void Mesh::setTexture()
{
if (texture)
texture->release();
texture = nullptr;
texture.set(nullptr);
}
Texture *Mesh::getTexture() const
{
return texture;
return texture.get();
}
void Mesh::setDrawMode(Mesh::DrawMode mode)
@@ -334,7 +323,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo
if (vertex_count == 0)
return;
if (texture)
if (texture.get())
texture->predraw();
else
gl.bindTexture(0);
@@ -412,7 +401,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo
gl.setColor(gl.getColor());
}
if (texture)
if (texture.get())
texture->postdraw();
}
+1 -1
View File
@@ -178,7 +178,7 @@ private:
int range_min;
int range_max;
Texture *texture;
Object::StrongRef<Texture> texture;
// Whether the per-vertex colors are used when drawing.
bool colors_enabled;
+17 -32
View File
@@ -102,7 +102,6 @@ ParticleSystem::ParticleSystem(Texture *texture, uint32 size)
sizes.push_back(1.0f);
colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
setBufferSize(size);
texture->retain();
}
ParticleSystem::ParticleSystem(const ParticleSystem &p)
@@ -150,22 +149,10 @@ ParticleSystem::ParticleSystem(const ParticleSystem &p)
, relativeRotation(p.relativeRotation)
{
setBufferSize(maxParticles);
if (texture != nullptr)
texture->retain();
for (Quad *quad : quads)
quad->retain();
}
ParticleSystem::~ParticleSystem()
{
if (texture != nullptr)
texture->release();
for (Quad *quad : quads)
quad->release();
deleteBuffers();
}
@@ -422,19 +409,14 @@ ParticleSystem::Particle *ParticleSystem::removeParticle(Particle *p)
return pNext;
}
void ParticleSystem::setTexture(Texture *texture)
void ParticleSystem::setTexture(Texture *tex)
{
Object::AutoRelease imagerelease(this->texture);
this->texture = texture;
if (texture)
texture->retain();
texture.set(tex);
}
Texture *ParticleSystem::getTexture() const
{
return texture;
return texture.get();
}
void ParticleSystem::setInsertMode(InsertMode mode)
@@ -732,26 +714,29 @@ std::vector<Color> ParticleSystem::getColor() const
void ParticleSystem::setQuads(const std::vector<Quad *> &newQuads)
{
for (Quad *quad : newQuads)
quad->retain();
std::vector<StrongRef<Quad>> quadlist;
quadlist.reserve(newQuads.size());
for (Quad *quad : quads)
quad->release();
for (Quad *q : newQuads)
quadlist.push_back(q);
quads = newQuads;
quads = quadlist;
}
void ParticleSystem::setQuads()
{
for (Quad *quad : quads)
quad->release();
quads.clear();
}
const std::vector<Quad *> &ParticleSystem::getQuads() const
std::vector<Quad *> ParticleSystem::getQuads() const
{
return quads;
std::vector<Quad *> quadlist;
quadlist.reserve(quads.size());
for (const Object::StrongRef<Quad> &q : quads)
quadlist.push_back(q.get());
return quadlist;
}
void ParticleSystem::setRelativeRotation(bool enable)
@@ -838,7 +823,7 @@ bool ParticleSystem::isFull() const
void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{
uint32 pCount = getCount();
if (pCount == 0 || texture == nullptr || pMem == nullptr || particleVerts == nullptr)
if (pCount == 0 || texture.get() == nullptr || pMem == nullptr || particleVerts == nullptr)
return;
Color curcolor = gl.getColor();
+3 -3
View File
@@ -431,7 +431,7 @@ public:
/**
* Gets the Quads used when drawing the particles.
**/
const std::vector<Quad *> &getQuads() const;
std::vector<Quad *> getQuads() const;
/**
* sets whether particle angles & rotations are relative to their velocities.
@@ -563,7 +563,7 @@ protected:
Vertex *particleVerts;
// The texture to be drawn.
Texture *texture;
Object::StrongRef<Texture> texture;
// Whether the particle emitter is active.
bool active;
@@ -640,7 +640,7 @@ protected:
std::vector<Colorf> colors;
// Quads.
std::vector<Quad *> quads;
std::vector<Object::StrongRef<Quad>> quads;
bool relativeRotation;
+2 -9
View File
@@ -88,14 +88,10 @@ SpriteBatch::SpriteBatch(Texture *texture, int size, int usage)
delete element_buf;
throw love::Exception("Out of memory.");
}
texture->retain();
}
SpriteBatch::~SpriteBatch()
{
texture->release();
delete color;
delete array_buf;
delete element_buf;
@@ -172,15 +168,12 @@ void SpriteBatch::flush()
void SpriteBatch::setTexture(Texture *newtexture)
{
Object::AutoRelease imagerelease(texture);
newtexture->retain();
texture = newtexture;
texture.set(newtexture);
}
Texture *SpriteBatch::getTexture()
{
return texture;
return texture.get();
}
void SpriteBatch::setColor(const Color &color)
+1 -1
View File
@@ -126,7 +126,7 @@ private:
*/
void setColorv(Vertex *v, const Color &color);
Texture *texture;
Object::StrongRef<Texture> texture;
// Max number of sprites in the batch.
int size;