0

我正在使用 lua 在 C++/SDL 中加载我的地​​图,并且我在 lua 中有一个数组来确定平铺图像的位置,但我不知道如何将它传输到 C++ 中的数组我已经寻找解释但它们都处理整数,而 lua 文件只有 1 个数组,由于某种原因代码不起作用,这可能是一个愚蠢的问题,但是是的

这是代码:

地图.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

主文件

#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);
}
4

1 回答 1

0

你不能一次得到整个数组。
使用循环来获取所有字符串。

string whatsTile[475];
int len_whatsTile = 0;

void loadMapLua(){
   lua_State* L = lua_open();
   luaL_openlibs(L);
   luaL_dofile(L, "../GameResources/LuaScripts/Map.lua");
   lua_getglobal(L, "whatsTile");
   int Index = 0;
   do {
      lua_pushinteger(L, Index++);
      lua_gettable(L, -2);
      if (lua_isnil(L, -1))
         Index = 0;
      else
         whatsTile[len_whatsTile++].assign(lua_tostring(L, -1));
      lua_pop(L, 1);
   } while (Index > 0);
   lua_close(L);
}
于 2013-02-19T21:36:25.680 回答