Browse Source

image: hide the constructor behind a factory so that we can later

return an already created object if necessary.
legacy
Sam Hocevar sam 12 years ago
parent
commit
e6b0bd07bf
4 changed files with 40 additions and 12 deletions
  1. +21
    -4
      src/image/image.cpp
  2. +5
    -2
      src/lol/image/image.h
  3. +9
    -3
      src/tileset.cpp
  4. +5
    -3
      test/unit/image.cpp

+ 21
- 4
src/image/image.cpp View File

@@ -53,15 +53,32 @@ static bool RegisterAllLoaders()
}

/*
* Public Image class
* Static image loader
*/

Image::Image(char const *path)
Image *Image::Create(char const *path)
{
/* Initialise loaders (see above) */
static bool init = RegisterAllLoaders();
UNUSED(init);

m_data = ImageLoader::Load(path);
Image *ret = new Image();
ret->m_data = ImageLoader::Load(path);
return ret;
}

void Image::Destroy(Image *img)
{
delete img;
}

/*
* Public Image class
*/

Image::Image()
: m_data(nullptr)
{
}

ivec2 Image::GetSize() const
@@ -74,7 +91,7 @@ PixelFormat Image::GetFormat() const
return m_data->m_format;
}

uint8_t * Image::GetData() const
uint8_t *Image::GetData() const
{
return m_data->GetData();
}


+ 5
- 2
src/lol/image/image.h View File

@@ -24,14 +24,17 @@ namespace lol
class Image
{
public:
Image(char const *path);
~Image();
static Image *Create(char const *path);
static void Destroy(Image *img);

ivec2 GetSize() const;
PixelFormat GetFormat() const;
uint8_t *GetData() const;

private:
Image();
~Image();

class ImageData *m_data;
};



+ 9
- 3
src/tileset.cpp View File

@@ -70,7 +70,7 @@ TileSet::TileSet(char const *path, ivec2 size, ivec2 count)

data->tiles = nullptr;
data->m_texture = 0;
data->img = new Image(path);
data->img = Image::Create(path);
data->isize = data->img->GetSize();

if (count.x > 0 && count.y > 0)
@@ -108,9 +108,15 @@ void TileSet::TickDraw(float seconds)
if (IsDestroying())
{
if (data->img)
delete data->img;
{
Image::Destroy(data->img);
data->img = nullptr;
}
else
{
delete data->m_texture;
data->m_texture = nullptr;
}
}
else if (data->img)
{
@@ -149,7 +155,7 @@ void TileSet::TickDraw(float seconds)

if (pixels != data->img->GetData())
delete[] pixels;
delete data->img;
Image::Destroy(data->img);
data->img = nullptr;
}
}


+ 5
- 3
test/unit/image.cpp View File

@@ -24,13 +24,13 @@ LOLUNIT_FIXTURE(ImageTest)
{
LOLUNIT_TEST(OpenImage)
{
Image image("data/gradient.png");
Image *image = Image::Create("data/gradient.png");

ivec2 size = image.GetSize();
ivec2 size = image->GetSize();
LOLUNIT_ASSERT_EQUAL(size.x, 256);
LOLUNIT_ASSERT_EQUAL(size.y, 16);

uint8_t *data = image.GetData();
uint8_t *data = image->GetData();
LOLUNIT_ASSERT(data);

LOLUNIT_ASSERT_EQUAL((int)data[0], 0x00);
@@ -40,6 +40,8 @@ LOLUNIT_FIXTURE(ImageTest)
LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 0], 0xff);
LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 1], 0xff);
LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 2], 0xff);

Image::Destroy(image);
}
};



Loading…
Cancel
Save