2014-10-07 39 views
1

我想从一个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 

回答

0

下面是做到这一点是实际上只是使用的基本上是在Lua一个新手的枚举类型的最简单方法通过将枚举类型从C++复制/粘贴到Lua(并删除逗号),然后通过名称和路径将纹理加载到图像中。

我正在使用“LuaPlus”而不是LuaBridge。虽然LuaPlus是安装的PITA,但在正确添加到您的项目中时,它是优越的(易于使用和理解)。

这里是我的枚举类型:

namespace Textures 
{ 
    enum ID 
    { 
     //Airplanes 
     Airplane1 = 1, 
     Airplane2 = 2, 
     Airplane3 = 3, 
     //Backgrounds 
     Background1 = 100, 
     Background2 = 101, 
    }; 
} 

我的LUA脚本结束这样看:

--Copy/Paste the Enumerated Types here, deleting the "," commas. 
Airplane1 = 1 
Airplane2 = 2 
Airplane3 = 3 
Background1 = 100 
Background2 = 101 

--All Textures are registered here. 
allTextures = 
{ 

--Airplanes 
[Airplane1]   = "../GFX/Airplane1.png", 
[Airplane2]   = "../GFX/Airplane2.png", 
--Backgrounds 
[Background]  = "../GFX/Background.png", 

} 
在C++

然后,调用函数

//LOAD GAME TEXTURES HERE 
void Scene::loadTextures() 
{ 
LuaState* pLuaState = LuaState::Create(); 
pLuaState->DoFile("../GFX/test.lua"); 
LuaObject table = pLuaState->GetGlobals()["allTextures"]; 

for(LuaTableIterator it(table); it; it.Next()) 
{ 
    int key = it.GetKey().GetInteger(); 
    const char* value = it.GetValue().GetString(); 
    //std::cout<<"Key: "<<key<<", Value: "<<value<<std::endl; 
    mTextures.load(static_cast<Textures::ID>(key), value); 
} 

}