Итак, я делаю графическое приложение (игру), в котором хочу использовать Asset Manager. Для этого класса я решил использовать Singleton Design. Итак, в моем main.cpp я бы загрузил что-то вроде...
ASSET_MANAGER.LoadImage("res/graphics/background.png", "background");
Вот значение макросов/методов, использованных в строке выше. Это своего рода мэшап кода, который мне нужно упростить, вместо того чтобы вставлять сюда несколько сотен строк кода.
активменеджер.h
#define ASSET_MANAGER AssetManager::GetAssetManager()
#define DEBUG
class AssetManager {
public:
static AssetManager &GetAssetManager();
//-----------------------------------------------------------------------------
// Purpose: Load a new image for the game to use. This function will store an
// instance of the asset in memory (in a hash map corresponding with
// the data type provided.
//
// param file: The location on disk of the asset
// param key: The string you use to receive this asset (defaults to the path str)
//-----------------------------------------------------------------------------
bool LoadImage(const char *file, const char *key = "");
//-----------------------------------------------------------------------------
// Purpose: Returns the image
//
// param key: The string used to store the asset in memory
//-----------------------------------------------------------------------------
ALLEGRO_BITMAP *GetImage(const char *key);
//-----------------------------------------------------------------------------
// Purpose: Destroys an asset that is presumably no longer needed by the game.
// This function is good for performance so that you don't use more
// RAM than you need to.
//
// param key: The string you use to receive this asset (defaults to the path str)
//-----------------------------------------------------------------------------
void DiscardImage(const char *key);
private:
AssetManager();
~AssetManager();
std::map<const char *, std::shared_ptr<ALLEGRO_BITMAP>> _ImageMap;
}
assetsmanager.cpp
AssetManager &AssetManager::GetAssetManager() {
static AssetManager instance;
return instance;
}
bool AssetManager::LoadImage(const char *file, const char *key) {
key = key == "" ? file : key;
std::shared_ptr<ALLEGRO_BITMAP> x(al_load_bitmap(file), al_destroy_bitmap);
if (!x) {
fprintf(stderr, "Failed to load %s\n", file);
return false;
}
#ifdef DEBUG
printf("DEBUG: Loaded %s\n", key); //debug
#endif // DEBUG
_ImageMap.insert(std::pair<const char *, std::shared_ptr<ALLEGRO_BITMAP>>(key, x));
return true;
}
ALLEGRO_BITMAP *AssetManager::GetImage(const char *key) {
return _ImageMap.find(key) != _ImageMap.end() ? _ImageMap.at(key).get() : nullptr;
}
void AssetManager::DiscardImage(const char *key) {
_ImageMap.erase(key);
#ifdef DEBUG
printf("DEBUG: Discarded %s\n", key); //debug
#endif // DEBUG
}
Этот класс работает только из класса, в котором я инициализировал менеджер активов, в то время как я ожидал, что он будет работать везде, где я назвал ASSET_MANAGER. Он компилируется нормально, он падает только тогда, когда я пытаюсь использовать менеджер в другом классе и передать его в функцию аллегро, потому что он возвращает что-то, что равно нулю, вместо правильных типов данных аллегро. Что я не понимаю в этом?
const char*
в качестве ключа карты в основном бесполезен, поскольку вы отображаете указатели, а не строки. Предпочитаюstd::string
. - person molbdnilo   schedule 01.03.2016LoadImage("foo"...)
, тоchar[] foo = "foo"; ... GetImage(foo, ...)
потерпит неудачу. - person molbdnilo   schedule 01.03.2016key
равно""
, что правда. Таким образом, вы сохраняете все, используя один и тот же ключ (то есть указатель на пустую строку), если вы не укажете его. - person molbdnilo   schedule 01.03.2016const char*
наstd::string
. - person Thomas Steinholz   schedule 01.03.2016