Selaa lähdekoodia

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

undefined
Sam Hocevar 10 vuotta sitten
vanhempi
commit
d16595a0bc
5 muutettua tiedostoa jossa 23 lisäystä ja 63 poistoa
  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 Näytä tiedosto

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

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

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

public:
inline ImageData()


+ 15
- 54
src/image/image.cpp Näytä tiedosto

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

static class ImageLoader
static class ImageBank
{
public:
void Init();
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:
Map<String, Image *> m_images;
}
image_loader;
g_image_bank;

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

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

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

@@ -101,35 +100,7 @@ Image *ImageLoader::Create(char const *path)
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);
if (--img->m_data->m_refcount == 0)
@@ -151,22 +122,7 @@ void ImageLoader::Destroy(Image *img)

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)
{
return m_data->Save(path);


+ 3
- 4
src/lol/image/image.h Näytä tiedosto

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

class Image
{
friend class ImageLoader;
friend class ImageBank;

public:
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);
void Destroy();

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


+ 2
- 2
src/tileset.cpp Näytä tiedosto

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

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


+ 1
- 1
test/unit/image.cpp Näytä tiedosto

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

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



Ladataan…
Peruuta
Tallenna