47 PRAGMA48(GCC diagnostic ignored
"-Wshadow")
48 #ifndef SDL_BIG_ENDIAN
49 #include <SDL_endian.h>
53 #include "utils/checkutils.h"
65 STD_VECTOR<TextureAtlas*> atlases;
66 STD_VECTOR<Image*> images;
71 #if !defined(ANDROID) && !defined(__APPLE__)
80 FOR_EACH (STD_VECTOR<TextureAtlas*>::iterator, it, atlases)
90 resource->
atlases.push_back(atlas);
93 BLOCK_END(
"AtlasManager::loadTextureAtlas")
100 STD_VECTOR<TextureAtlas*> atlases;
101 STD_VECTOR<Image*> images;
109 FOR_EACH (STD_VECTOR<TextureAtlas*>::iterator, it, atlases)
112 if (atlas ==
nullptr)
121 resource->
atlases.push_back(atlas);
124 BLOCK_END(
"AtlasManager::loadTextureAtlas")
129 STD_VECTOR<Image*> &images)
135 const std::string str = *it;
146 std::string path = str;
147 const size_t p = path.find(
'|');
149 if (p != std::string::npos)
151 d =
new Dye(path.substr(p + 1));
152 path = path.substr(0, p);
158 Image *
const image = d !=
nullptr ?
162 if (image !=
nullptr)
164 image->mIdPath = str;
166 logger->
log(
"set name %p, %s",
static_cast<void*
>(image),
167 image->mIdPath.c_str());
170 images.push_back(image);
179 STD_VECTOR<Image*> &images)
185 const std::string str = *it;
199 image->mIdPath = str;
200 images.push_back(image);
202 BLOCK_END(
"AtlasManager::loadEmptyImages")
206 STD_VECTOR<TextureAtlas*> &
restrict atlases,
207 const STD_VECTOR<Image*> &
restrict images,
215 STD_VECTOR<Image*>::const_iterator it = images.begin();
216 const STD_VECTOR<Image*>::const_iterator it_end = images.end();
217 for (it = images.begin(); it != it_end; ++ it)
219 const Image *
const img = *it;
222 atlas->
name = std::string(
"atlas_").append(name).append(
223 "_").append(img->mIdPath);
228 for (it = images.begin(); it != it_end; ++ it)
230 Image *
const img = *it;
234 item->
name = img->mIdPath;
236 if (
x + img->mBounds.w >
size)
244 if (
y + img->mBounds.h >
size)
248 atlases.push_back(atlas);
250 atlas->
name = std::string(
"atlas_").append(name).append(
251 "_").append(img->mIdPath);
254 if (img->mBounds.h > tempHeight)
255 tempHeight = img->mBounds.h;
259 atlas->
items.push_back(item);
265 if (
y + img->mBounds.h > atlas->
height)
266 atlas->
height =
y + img->mBounds.h;
269 if (!atlas->
items.empty())
270 atlases.push_back(atlas);
277 STD_VECTOR<TextureAtlas*> &
restrict atlases,
278 const STD_VECTOR<Image*> &
restrict images)
282 STD_VECTOR<Image*>::const_iterator it = images.begin();
283 const STD_VECTOR<Image*>::const_iterator it_end = images.end();
284 for (it = images.begin(); it != it_end; ++ it)
286 const Image *
const img = *it;
289 atlas->
name = std::string(
"atlas_").append(name).append(
290 "_").append(img->mIdPath);
295 for (it = images.begin(); it != it_end; ++ it)
297 Image *
const img = *it;
301 item->
name = img->mIdPath;
304 atlas->
items.push_back(item);
307 if (!atlas->
items.empty())
308 atlases.push_back(atlas);
317 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
318 const uint32_t rmask = 0xff000000U;
319 const uint32_t gmask = 0x00ff0000U;
320 const uint32_t bmask = 0x0000ff00U;
321 const uint32_t amask = 0x000000ffU;
324 const uint32_t rmask = 0x000000ffU;
325 const uint32_t gmask = 0x0000ff00U;
326 const uint32_t bmask = 0x00ff0000U;
327 const uint32_t amask = 0xff000000U;
331 if (atlas->
items.size() == 1)
333 logger->
log(
"Skip atlas creation because only one image in atlas.");
334 BLOCK_END(
"AtlasManager::createSDLAtlas")
342 const int width = atlas->
width;
343 const int height = atlas->
height;
344 BLOCK_START(
"AtlasManager::createSDLAtlas create surface")
347 width, height, 32U, rmask, gmask, bmask, amask);
348 if (surface ==
nullptr)
350 reportAlways(
"Error creating surface for atlas. Size: %dx%d",
353 BLOCK_END(
"AtlasManager::createSDLAtlas")
356 BLOCK_END(
"AtlasManager::createSDLAtlas create surface")
359 logger->
log(
"OpenGL debug: creating atlase %dx%d", width, height);
365 if (image ==
nullptr)
367 reportAlways(
"Error converting surface to texture. Size: %dx%d",
374 FOR_EACH (STD_VECTOR<AtlasItem*>::iterator, it, atlas->
items)
378 item->
image->mSDLSurface);
380 logger->
log(
"OpenGL debug: put atlas image %s (size %dx%d),"
381 " into %d,%d to %d,%d",
383 item->
image->mSDLSurface->w,
384 item->
image->mSDLSurface->h,
387 item->
x + item->
image->mSDLSurface->w - 1,
388 item->
y + item->
image->mSDLSurface->h - 1);
389 if (item->
x >= width)
390 logger->
log(
"OpenGL error: start x position outside of atlas");
391 if (item->
y >= height)
392 logger->
log(
"OpenGL error: start y position outside of atlas");
393 if (item->
x + item->
image->mSDLSurface->w - 1 >= width)
394 logger->
log(
"OpenGL error: end x position outside of atlas");
395 if (item->
y + item->
image->mSDLSurface->h - 1 >= height)
396 logger->
log(
"OpenGL error: end y position outside of atlas");
400 BLOCK_END(
"AtlasManager::createSDLAtlas")
409 if (oldImage->mSDLSurface !=
nullptr)
417 if (image ==
nullptr)
420 image->mIdPath = atlas->
name;
422 logger->
log(
"set name %p, %s",
static_cast<void*
>(image),
423 image->mIdPath.c_str());
428 FOR_EACH (STD_VECTOR<AtlasItem*>::iterator, it, atlas->
items)
434 item->
image = image->getSubImage(item->
x, item->
y,
437 if (image2 !=
nullptr)
439 image2->mIdPath = item->
name;
441 logger->
log(
"set empty name %p, %s",
static_cast<void*
>(image2),
442 image2->mIdPath.c_str());
456 if (oldImage->mSDLSurface !=
nullptr)
464 if (image ==
nullptr)
467 image->mIdPath = atlas->
name;
469 logger->
log(
"set name %p, %s",
static_cast<void*
>(image),
470 image->mIdPath.c_str());
475 FOR_EACH (STD_VECTOR<AtlasItem*>::iterator, it, atlas->
items)
481 item->
image = image->getSubImage(item->
x, item->
y,
484 if (image2 !=
nullptr)
486 image2->mIdPath = item->
name;
488 logger->
log(
"set name %p, %s",
static_cast<void*
>(image2),
489 image2->mIdPath.c_str());
499 if (resource ==
nullptr)
501 FOR_EACH (STD_VECTOR<TextureAtlas*>::const_iterator,
506 if (atlas !=
nullptr)
509 if (image !=
nullptr)
511 FOR_EACH (STD_VECTOR<AtlasItem*>::iterator, it2, atlas->
items)
525 if (resource ==
nullptr)
531 if (atlas !=
nullptr)
534 if (image !=
nullptr)
539 FOR_EACH (STD_VECTOR<AtlasItem*>::iterator, it2, atlas->
items)
545 if (image2 !=
nullptr)
#define reportAlways(...)
static void injectToResources(const AtlasResource *const resource)
static AtlasResource * loadTextureAtlas(const std::string &name, const StringVect &files)
static void loadEmptyImages(const StringVect &files, std::vector< Image * > &images)
static AtlasResource * loadEmptyAtlas(const std::string &name, const StringVect &files)
static void loadImages(const StringVect &files, std::vector< Image * > &images)
static void emptySort(const std::string &name, std::vector< TextureAtlas * > &atlases, const std::vector< Image * > &images)
static void convertAtlas(TextureAtlas *const atlas)
static void convertEmptyAtlas(TextureAtlas *const atlas)
static void simpleSort(const std::string &name, std::vector< TextureAtlas * > &atlases, const std::vector< Image * > &images, int size)
static void moveToDeleted(AtlasResource *const resource)
static void createSDLAtlas(TextureAtlas *const atlas)
std::vector< TextureAtlas * > atlases
virtual void copySurfaceToImage(const Image *const image, const int x, const int y, SDL_Surface *const surface) const
virtual Image * loadSurface(SDL_Surface *const)
Image * load(SDL_RWops *const rw)
void log(const char *const log_text,...)
static int getTextureSize()
#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a)
#define MSDL_FreeSurface(surface)
#define FOR_EACH(type, iter, array)
ImageHelper * imageHelper
ImageHelper * surfaceImageHelper
int powerOfTwo(const unsigned int input)
void moveToDeleted(Resource *const res)
bool addResource(const std::string &idPath, Resource *const resource)
Resource * getTempResource(const std::string &idPath)
SDL_RWops * rwopsOpenRead(const std::string &fname)
#define BLOCK_START(name)
StringVect::const_iterator StringVectCIter
std::vector< std::string > StringVect
std::vector< AtlasItem * > items