From 1945b7975d26750e91170e882eb16df922b5dc03 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 14 Feb 2013 21:49:11 -0400 Subject: [PATCH] Cleaned up exception handling code when creating new fonts, spritebatches, and shaders --- src/modules/graphics/opengl/Font.cpp | 22 ++++++++--- src/modules/graphics/opengl/Graphics.cpp | 37 ++----------------- .../graphics/opengl/ParticleSystem.cpp | 4 +- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index 5ddfb4a69..28dc10a98 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -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() diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index ed6b64f17..0670f9d66 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -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) diff --git a/src/modules/graphics/opengl/ParticleSystem.cpp b/src/modules/graphics/opengl/ParticleSystem.cpp index 468016b89..7f10ee5ce 100644 --- a/src/modules/graphics/opengl/ParticleSystem.cpp +++ b/src/modules/graphics/opengl/ParticleSystem.cpp @@ -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()