Cleaned up exception handling code when creating new fonts, spritebatches, and shaders

This commit is contained in:
Alex Szpakowski
2013-02-14 21:49:11 -04:00
parent e1779897ab
commit 1945b7975d
3 changed files with 21 additions and 42 deletions
+16 -6
View File
@@ -50,10 +50,6 @@ Font::Font(love::font::Rasterizer *r, const Image::Filter &filter)
, filter(filter)
, mipmapsharpness(0.0f)
{
love::font::GlyphData *gd = r->getGlyphData(32);
type = (gd->getFormat() == love::font::GlyphData::FORMAT_LUMINANCE_ALPHA ? FONT_TRUETYPE : FONT_IMAGE);
delete gd;
// try to find the best texture size match for the font size
// default to the largest texture size if no rough match is found
texture_size_index = NUM_TEXTURE_SIZES - 1;
@@ -71,9 +67,23 @@ Font::Font(love::font::Rasterizer *r, const Image::Filter &filter)
texture_width = TEXTURE_WIDTHS[texture_size_index];
texture_height = TEXTURE_HEIGHTS[texture_size_index];
loadVolatile();
love::font::GlyphData *gd = 0;
r->retain();
try
{
gd = r->getGlyphData(32);
loadVolatile();
}
catch (love::Exception &)
{
delete gd;
throw;
}
type = (gd->getFormat() == love::font::GlyphData::FORMAT_LUMINANCE_ALPHA) ? FONT_TRUETYPE : FONT_IMAGE;
delete gd;
rasterizer->retain();
}
Font::~Font()
+3 -34
View File
@@ -375,31 +375,12 @@ Quad *Graphics::newQuad(float x, float y, float w, float h, float sw, float sh)
Font *Graphics::newFont(love::font::Rasterizer *r, const Image::Filter &filter)
{
Font *font = new Font(r, filter);
// Load it and check for errors.
if (!font)
{
delete font;
return 0;
}
return font;
return new Font(r, filter);
}
SpriteBatch *Graphics::newSpriteBatch(Image *image, int size, int usage)
{
SpriteBatch *t = NULL;
try
{
t = new SpriteBatch(image, size, usage);
}
catch(love::Exception &e)
{
if (t) delete t;
throw e;
}
return t;
return new SpriteBatch(image, size, usage);
}
ParticleSystem *Graphics::newParticleSystem(Image *image, int size)
@@ -462,19 +443,7 @@ Canvas *Graphics::newCanvas(int width, int height, Canvas::TextureType texture_t
Shader *Graphics::newShader(const Shader::ShaderSources &sources)
{
Shader *shader = NULL;
try
{
shader = new Shader(sources);
}
catch(love::Exception &)
{
if (shader)
delete shader;
throw;
}
return shader;
return new Shader(sources);
}
void Graphics::setColor(const Color &c)
@@ -63,6 +63,7 @@ ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer)
, pLast(0)
, pEnd(0)
, particleVerts(0)
, sprite(sprite)
, active(true)
, emissionRate(0)
, emitCounter(0)
@@ -91,11 +92,10 @@ ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer)
, offsetX(sprite->getWidth()*0.5f)
, offsetY(sprite->getHeight()*0.5f)
{
this->sprite = sprite;
sprite->retain();
sizes.push_back(1.0f);
colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
setBufferSize(buffer);
sprite->retain();
}
ParticleSystem::~ParticleSystem()