我想從一個Lua腳本加載紋理到我的C++遊戲引擎。從Lua傳遞枚舉類型到C++的最簡單方法?
引擎使用一個名爲「ResourceHolder」的類,枚舉類型來自一個名爲「ResourceIdenifiers」的類。
我的遊戲場景爲紋理創建了自己的ResourceHolder &字體(以及我需要的任何其他東西)。所以我有用於Textures :: ID(枚舉類型)和字體:: ID的名稱空間。
所以我只需創建一個TextureHolder對象mTextures「
TextureHolder mTextures;
然後,我只是加載很容易的紋理有燒灼線像這樣:
mTextures.load(Textures::Airplane2, "../GFX/Airplane2.png");
的問題是,我不能在Lua中使用這些枚舉類型,儘管我的計劃是在我的lua.script文件中有這樣的東西:
allTextures
{
--Airplanes
["Airplane1"] = "../GFX/Airplane1.png",
["Airplane2"] = "../GFX/Airplane2.png",
--Or something like this instead
["Textures::Airplane3"] = "../GFX/Airplane3.png"
}
允許Lua腳本處理這些枚舉類型的最簡單方法是什麼?
這裏是我的ResourceIdentifier和ResourceHolder的類。
ResourceIdentifier.h
#ifndef RESOURCEIDENTIFIERS_H
#define RESOURCEIDENTIFIERS_H
// Forward declaration of SFML classes
namespace sf
{
class Texture;
class Font;
}
namespace Textures
{
enum ID
{
//Airplanes
Airplane1,
Airplane2,
Airplane3,
Background1,
Background2,
};
}
namespace Fonts
{
enum ID
{
Main,
};
}
// Forward declaration and a few type definitions
template <typename Resource, typename Identifier>
class ResourceHolder;
typedef ResourceHolder<sf::Texture, Textures::ID> TextureHolder;
typedef ResourceHolder<sf::Font, Fonts::ID> FontHolder;
#endif // RESOURCEIDENTIFIERS_H
ResourceHolder.h(不太相關)
#ifndef RESOURCEHOLDER_H
#define RESOURCEHOLDER_H
#include <map>
#include <string>
#include <memory>
#include <stdexcept>
#include <cassert>
#include <SFML/Graphics/Image.hpp>
template <typename Resource, typename Identifier>
//This class stores Identifier so they can be accessed.
class ResourceHolder
{
public:
//This creates loads the texture from the filename, gives it an ID, and stores it in the std::map container mTextureMap.
void load(Identifier id, const std::string& filename);
void loadImage(Identifier id, const sf::Image& image);
template <typename Parameter>
void load(Identifier id, const std::string& filename, const Parameter& secondParam);
//This gets the texture from the std::map container, so it can be used. It gets the Resource based on the texture's ID (name).
Resource& get(Identifier id);
const Resource& get(Identifier id) const;
//^SFML book - Chapter 2 - "Accessing the Identifier" ??? For when you dont want to allow editing of the Texture???
private:
//A map stores all of the Identifier. The std::map< (1 parameter) 'Name of Resource', (2 parameter) a unique pointer of the Resource).
std::map<Identifier, std::unique_ptr<Resource> > mResourceMap;
};
#include "ResourceHolder.inl"
#endif // RESOURCEHOLDER_H