
[ad_1]
I’m engaged on my first huge venture and I’ve determined to combine Lua with my C++ framework. Everything’s labored out thus far, however the one approach I’ve managed to move the SDL_GetKeyboardState(NULL)
array to Lua is by constructing a Lua desk manually.
void passKeyboard(const Uint8* keyboard){
std::map<std::string,std::string> kmap;
kmap.clear();
kmap["UP"]=toString(keyboard[SDL_GetScancodeFromKey(SDLK_UP)]);
kmap["DOWN"]=toString(keyboard[SDL_GetScancodeFromKey(SDLK_DOWN)]);
kmap["LEFT"]=toString(keyboard[SDL_GetScancodeFromKey(SDLK_LEFT)]);
kmap["RIGHT"]=toString(keyboard[SDL_GetScancodeFromKey(SDLK_RIGHT)]);
...
vbind_SetTable("keyboard",kmap);
}
Now, the code works and in all fairness quick for my functions, nevertheless it’s undeniably ugly. I even really feel soiled it. Is there any approach I might to this extra elegantly?
For reference, vbind.h
:
void vbind_SetTable(const std::string &identify,std::map<std::string,std::string> &t){
lua_newtable(L);
for(std::map<std::string,std::string>::iterator it=t.start();it!=t.finish();it++){
lua_pushstring(L,it->first.c_str());
std::string worth=it->second;
const char* p=worth.c_str();
char* finish;
lengthy t=strtol(worth.c_str(),&finish,10);
if(p!=finish&&errno!=ERANGE){
lua_pushnumber(L,t);
}
else{
lua_pushstring(L,worth.c_str());
}
lua_settable(L,-3);
}
lua_setglobal(L,identify.c_str());
}
[ad_2]