2014-10-07 109 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); 
} 

}