0
我使用lua加載我在C++/SDL中的地圖,並且我在lua中有一個數組來確定平鋪圖像的位置,但我不知道如何傳遞它以C++數組我已經尋找一個解釋,但他們都處理整數和lua文件只有1陣列和由於某種原因代碼不起作用這可能是一個愚蠢的問題,但是是的從lua加載字符串的數組C++/SDL
這是的代碼:
Map.lua
Tile = {}
--opens file and reads it
local file = io.open("../Maps/Map.txt")
local n = 0
while(n <= 494) do
Tile[n] = file:read(1)
n = n+1
end
file:close()
--end of reading file
n = 0
local x = 0
-- creates a new array for the tiles
whatsTile = {}
isSolid = {}
--sets the tiles to their specific image
while(n <= 494) do
if(Tile[n] ~= "\n" and Tile[n] ~= "\r") then
if(Tile[n] == '0') then
--This is the array in lua that holds the file location
whatsTile[x] = "../GameResources/Pictures/Tile1.bmp"
--sets this tile to non solid
isSolid[x] = 0
end
--You can add new tiles just set their number and, add a directory for the Bitmap image.
--Make sure you set the directory based on where the executable file is, not this script.
x = x+1
end
n = n+1
end
的main.cpp
#include <SDL/SDL.h>
#include <string>
#include <iostream>
#include <lua.hpp>
#include <fstream>
using namespace std;
extern "C"{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <luaconf.h>
}
void loadMapLua();
//This is the array i want to transfer the values into
string whatsTile[475];
int main(int argc, char** argv){
ofstream myfile;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Event event;
loadMapLua();
SDL_Surface* Screen = SDL_SetVideoMode(800, 608, 32, SDL_SWSURFACE);
SDL_Surface* BackGround = SDL_LoadBMP("../GameResources/Pictures/BackGround.bmp");
SDL_Surface* Tiles[475];
const char* c;
SDL_Rect Tile[475];
int n = 0;
while(n < 475){
c = whatsTile[n].c_str();
Tiles[n] = SDL_LoadBMP(c);
Tile[n].w = 32;
Tile[n].h = 32;
Tile[n].x = n*32;
Tile[n].y = (n/25)*32;
n ++;
}
n = 0;
myfile.open("trolls.txt");
myfile << whatsTile[0] << endl;
myfile.close();
bool gameRunning = true;
while(gameRunning){
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
gameRunning = false;
}
}
SDL_FillRect(Screen, 0, SDL_MapRGB(Screen->format, 255, 255, 255));
SDL_BlitSurface(BackGround, NULL, Screen, NULL);
while(n < 475){
SDL_BlitSurface(Tiles[n], NULL, Screen, &Tile[n]);
n ++;
}
SDL_Flip(Screen);
}
SDL_Quit();
return 0;
}
void loadMapLua(){
lua_State* Map = lua_open();
luaL_openlibs(Map);
luaL_dofile(Map, "../GameResources/LuaScripts/Map.lua");
lua_pushnil(Map);
lua_close(Map);
}
驚慌:在調用Lua的API(嘗試指標零值)未受保護的錯誤 – SilentBaws 2013-02-20 01:44:07
@SilentBaws - 也許,你的Lua腳本沒有成功執行,所以'whatsTile'未初始化的全局變量。請檢查由'luaL_dofile'返回的值,如果此值不爲零,則在執行'lua_close(L)'之前,顯示由'char * errmes = lua_tostring(L,-1)'指向的錯誤消息。 – 2013-02-20 08:43:53