2016-03-01 49 views
1

所以我正在制作一个图形应用程序(游戏),我想要使用资产管理器。对于这个班级,我决定使用Singleton Design。所以在我的main.cpp中,我会加载类似...全局访问单例数据

ASSET_MANAGER.LoadImage("res/graphics/background.png", "background"); 

这里是上述行中使用的宏/方法的含义。这是一种代码混搭,我必须让事情变得更简单,而不是将几百行代码粘贴到此处。

assetmanager.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; 
} 

assetmanager.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工作初始化的资产管理类的工作。它编译得很好,当我尝试在不同的类中使用管理器并将其传递给allegro函数时,它只会崩溃,因为它返回null而不是正确的allegro数据类型。我对此有何了解?

+0

也许你可以解释它在别的地方不起作用吗?它不会编译?它会崩溃吗?它只加载小猫吗? – molbdnilo

+1

在附注中,除非您独占使用字符串文字,并且要小心,因为您映射的是指针而不是字符串,所以'const char *'作为映射关键字是无用的。首选'std :: string'。 – molbdnilo

+0

+ molbdnilo它编译得很好,但是在没有定义ASSET_MANAGER的类中,它没有获取数据。 GetImage()函数只是返回null而不是我加载的数据。这会导致allegro导致应用程序崩溃,因为它无法呈现null。 –

回答

0

地图中的Char *存储位置,而不是使程序认为它为空的数据。