0

lua を使用して C++/SDL でマップをロードしています。lua にはタイル イメージの場所を決定する配列がありますが、それを C++ の配列に転送する方法がわかりません。説明を探しました。しかし、それらはすべてintを扱い、luaファイルには配列が1つしかなく、何らかの理由でコードが機能しません。これはおそらくばかげた質問ですが、ええ

コードは次のとおりです。

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);
}
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 に答える