Browse Source

image: some refactoring of the Image class. Not final.

undefined
Sam Hocevar 10 years ago
parent
commit
d16595a0bc
5 changed files with 23 additions and 63 deletions
  1. +2
    -2
      src/image/image-private.h
  2. +15
    -54
      src/image/image.cpp
  3. +3
    -4
      src/lol/image/image.h
  4. +2
    -2
      src/tileset.cpp
  5. +1
    -1
      test/unit/image.cpp

+ 2
- 2
src/image/image-private.h View File

@@ -21,7 +21,7 @@ namespace lol


class ImageCodec class ImageCodec
{ {
friend class ImageLoader;
friend class ImageBank;
friend class ImageData; friend class ImageData;


public: public:
@@ -69,7 +69,7 @@ private:
class ImageData class ImageData
{ {
friend class Image; friend class Image;
friend class ImageLoader;
friend class ImageBank;


public: public:
inline ImageData() inline ImageData()


+ 15
- 54
src/image/image.cpp View File

@@ -58,29 +58,27 @@ static bool RegisterAllLoaders()
* Our static image loader * Our static image loader
*/ */


static class ImageLoader
static class ImageBank
{ {
public: public:
void Init(); void Init();
Image *Create(char const *path); Image *Create(char const *path);
Image *Get(char const *path);
Image *Load(char const *path);
Image *Store(char const *path, Image *image);
void Destroy(Image *img);

void Unref(Image *img);


private: private:
Map<String, Image *> m_images; Map<String, Image *> m_images;
} }
image_loader;
g_image_bank;


void ImageLoader::Init()
void ImageBank::Init()
{ {
/* Initialise loaders (see above) */ /* Initialise loaders (see above) */
static bool init = RegisterAllLoaders(); static bool init = RegisterAllLoaders();
UNUSED(init); UNUSED(init);
} }


Image *ImageLoader::Create(char const *path)
Image *ImageBank::Create(char const *path)
{ {
Init(); Init();


@@ -93,7 +91,8 @@ Image *ImageLoader::Create(char const *path)
} }
else else
{ {
m_images[path] = Load(path);
m_images[path] = new Image();
m_images[path]->m_data = ImageCodec::Load(path);
img = m_images[path]; img = m_images[path];
} }


@@ -101,35 +100,7 @@ Image *ImageLoader::Create(char const *path)
return img; return img;
} }


Image *ImageLoader::Get(char const *path)
{
/* Is our image already in the bank? If so, no need to create it. */
Image *img;
if (m_images.HasKey(path))
{
img = m_images[path];
++img->m_data->m_refcount;
return img;
}

return nullptr;
}

Image *ImageLoader::Load(char const *path)
{
Image* img = new Image();
img->m_data = ImageCodec::Load(path);
return img;
}

Image *ImageLoader::Store(char const *path, Image *image)
{
m_images[path] = image;
++image->m_data->m_refcount;
return image;
}

void ImageLoader::Destroy(Image *img)
void ImageBank::Unref(Image *img)
{ {
ASSERT(img->m_data->m_refcount > 0); ASSERT(img->m_data->m_refcount > 0);
if (--img->m_data->m_refcount == 0) if (--img->m_data->m_refcount == 0)
@@ -151,22 +122,7 @@ void ImageLoader::Destroy(Image *img)


Image *Image::Create(char const *path) Image *Image::Create(char const *path)
{ {
return image_loader.Create(path);
}

Image *Image::Store(char const *path, Image *img)
{
return image_loader.Store(path, img);
}

Image *Image::Load(char const *path)
{
return image_loader.Load(path);
}

void Image::Destroy(Image *img)
{
return image_loader.Destroy(img);
return g_image_bank.Create(path);
} }


/* /*
@@ -178,6 +134,11 @@ Image::Image()
{ {
} }


void Image::Destroy()
{
g_image_bank.Unref(this);
}

bool Image::Save(char const *path) bool Image::Save(char const *path)
{ {
return m_data->Save(path); return m_data->Save(path);


+ 3
- 4
src/lol/image/image.h View File

@@ -23,15 +23,14 @@ namespace lol


class Image class Image
{ {
friend class ImageLoader;
friend class ImageBank;


public: public:
static Image *Create(char const *path); static Image *Create(char const *path);
static Image *Store(char const *path, Image *img);
static Image *Load(char const *path);
static void Destroy(Image *img);


bool Save(char const *path); bool Save(char const *path);
void Destroy();

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


+ 2
- 2
src/tileset.cpp View File

@@ -190,7 +190,7 @@ void TileSet::TickDraw(float seconds)
{ {
if (m_data->m_image) if (m_data->m_image)
{ {
Image::Destroy(m_data->m_image);
m_data->m_image->Destroy();
m_data->m_image = nullptr; m_data->m_image = nullptr;
} }
else else
@@ -223,7 +223,7 @@ void TileSet::TickDraw(float seconds)


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


+ 1
- 1
test/unit/image.cpp View File

@@ -41,7 +41,7 @@ LOLUNIT_FIXTURE(ImageTest)
LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 1], 0xff); LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 1], 0xff);
LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 2], 0xff); LOLUNIT_ASSERT_EQUAL((int)data[255 * 4 + 2], 0xff);


Image::Destroy(image);
image->Destroy();
} }
}; };




Loading…
Cancel
Save