Page 1 of 1

C++ reading array of strings from lua

Posted: Tue Feb 19, 2013 8:08 am
by werdo659
Hey guys I've made a few SDL games now and want to release one to the community, but I need to have the maps load from files so i don't have to recompile every time I change the map it's really annoying another problem is it used to be in all C++/SDL but I want it to have lua implementation now and, allow the community to have tons of mods and maps the players can make and share but I haven't done much in lua yet and i ran into a problem :oops: I'm doing the file reading for my map in lua and C++ isn't reading the table that the location of the tile image is in. here's the code

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
			--Note it must be a bmp or sdl will not support the image. Also note each tile is 32x32 this is not changeable
			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();

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