Преглед изворни кода

Fixed Thread-safe image loading

undefined
Benjamin ‘Touky’ Huet Sam Hocevar <sam@hocevar.net> пре 10 година
родитељ
комит
809600ea47
2 измењених фајлова са 51 додато и 8 уклоњено
  1. +42
    -6
      src/image/image.cpp
  2. +9
    -2
      src/lol/image/image.h

+ 42
- 6
src/image/image.cpp Прегледај датотеку

@@ -63,6 +63,8 @@ static class ImageBank
public:
void Init();
Image *Create(char const *path);
bool Store(Image *img);
Image *Load(char const *path);

void Unref(Image *img);

@@ -80,8 +82,6 @@ void ImageBank::Init()

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

/* Is our image already in the bank? If so, no need to create it. */
Image *img;

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

++img->m_data->m_refcount;
return img;
}

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

Image *img = new Image(path);
img->m_data = ImageCodec::Load(img->GetPath().C());
return img;
}

bool ImageBank::Store(Image *img)
{
/* Is our image already in the bank? If so, no need to create it. */
if (m_images.HasKey(img->GetPath().C()))
return false;

m_images[img->GetPath().C()] = img;

++img->m_data->m_refcount;
return true;
}

void ImageBank::Unref(Image *img)
{
ASSERT(img->m_data->m_refcount > 0);
@@ -125,13 +145,24 @@ Image *Image::Create(char const *path)
return g_image_bank.Create(path);
}

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

bool Image::Store(Image *img)
{
return g_image_bank.Store(img);
}

/*
* Public Image class
*/

Image::Image()
Image::Image(char const* path)
: m_data(nullptr)
{
m_path = path;
}

void Image::Destroy()
@@ -159,6 +190,11 @@ uint8_t *Image::GetData() const
return m_data->GetData();
}

String Image::GetPath() const
{
return m_path;
}

bool Image::RetrieveTiles(Array<ivec2, ivec2>& tiles) const
{
return m_data->RetrieveTiles(tiles);


+ 9
- 2
src/lol/image/image.h Прегледај датотеку

@@ -26,7 +26,12 @@ class Image
friend class ImageBank;

public:
static Image *Create(char const *path);
//Create/Load/Store image into bank. THREAD: NOT SAFE
static Image* Create(char const *path);
//Create/Load image into bank. THREAD: SAFE
static Image* Load(char const *path);
//Store image into bank. THREAD: NOT SAFE
static bool Store(Image *img);

bool Save(char const *path);
void Destroy();
@@ -34,13 +39,15 @@ public:
ivec2 GetSize() const;
PixelFormat GetFormat() const;
uint8_t *GetData() const;
String GetPath() const;
bool RetrieveTiles(Array<ivec2, ivec2>& tiles) const;

private:
Image();
Image(char const* path);
~Image();

class ImageData *m_data;
String m_path;
};

} /* namespace lol */


Loading…
Откажи
Сачувај