| 1 |
|
| 2 |
#ifdef HAVE_CONFIG_H |
| 3 |
#include <config.h> |
| 4 |
#endif |
| 5 |
|
| 6 |
#include "BgTilesetsManager.h" |
| 7 |
#include "paths.h" |
| 8 |
#include "log.h" |
| 9 |
#include "gfx.h" |
| 10 |
#include "ImageLoader.h" |
| 11 |
#include "IOGfxDisplay.h" |
| 12 |
#include "SDL_image.h" |
| 13 |
|
| 14 |
BgTilesetsManager::BgTilesetsManager() { |
| 15 |
memset(&slots, 0, sizeof(slots)); |
| 16 |
} |
| 17 |
|
| 18 |
BgTilesetsManager::~BgTilesetsManager() { |
| 19 |
unloadAll(); |
| 20 |
} |
| 21 |
|
| 22 |
// Load the tiles from the BMPs |
| 23 |
void BgTilesetsManager::loadDefault() { |
| 24 |
char crap[30]; |
| 25 |
char crap1[10]; |
| 26 |
int h; |
| 27 |
|
| 28 |
log_info("loading tilescreens..."); |
| 29 |
for (h = 1; h <= GFX_TILES_NB_SETS; h++) |
| 30 |
{ |
| 31 |
if (h < 10) |
| 32 |
strcpy(crap1,"0"); |
| 33 |
else |
| 34 |
strcpy(crap1, ""); |
| 35 |
sprintf(crap, "tiles/Ts%s%d.BMP", crap1, h); |
| 36 |
|
| 37 |
loadSlot(h, crap); |
| 38 |
|
| 39 |
if (slots[h] == NULL) |
| 40 |
exit(0); |
| 41 |
} |
| 42 |
|
| 43 |
log_info("Done with tilescreens..."); |
| 44 |
} |
| 45 |
|
| 46 |
void BgTilesetsManager::loadSlot(int slot, char* relpath) { |
| 47 |
FILE* in = paths_dmodfile_fopen(relpath, "rb"); |
| 48 |
if (in == NULL) |
| 49 |
in = paths_fallbackfile_fopen(relpath, "rb"); |
| 50 |
|
| 51 |
if (slots[slot] != NULL) { |
| 52 |
delete slots[slot]; |
| 53 |
slots[slot] = NULL; |
| 54 |
} |
| 55 |
|
| 56 |
SDL_Surface* image = ImageLoader::loadToBlitFormat(in); |
| 57 |
slots[slot] = g_display->upload(image); |
| 58 |
|
| 59 |
/* Note: attempting SDL_RLEACCEL showed no improvement for the |
| 60 |
memory usage, including when using a transparent color and |
| 61 |
blitting the surface once. It did show a decrease of 400kB (out |
| 62 |
of 6000kB) when using transparent color 255, but in this case the |
| 63 |
color is not supposed to be transparent. */ |
| 64 |
|
| 65 |
if (slots[slot] == NULL) |
| 66 |
log_error("Couldn't find tilescreen %s: %s\n", relpath, SDL_GetError()); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Free memory used by tiles |
| 71 |
*/ |
| 72 |
void BgTilesetsManager::unloadAll() { |
| 73 |
int h = 0; |
| 74 |
for (h=1; h <= GFX_TILES_NB_SETS; h++) { |
| 75 |
if (slots[h] != NULL) |
| 76 |
delete slots[h]; |
| 77 |
slots[h] = NULL; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
int BgTilesetsManager::getMemUsage() { |
| 82 |
int sum = 0; |
| 83 |
int i = 0; |
| 84 |
IOGfxSurface* s = NULL; |
| 85 |
for (; i < GFX_TILES_NB_SETS+1; i++) { |
| 86 |
s = slots[i]; |
| 87 |
if (s != NULL) |
| 88 |
sum += s->getMemUsage(); |
| 89 |
} |
| 90 |
return sum; |
| 91 |
} |