Lua reading array of strings issue C++/SDL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
werdo659
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri Aug 19, 2011 11:37 am
Current Project: A top-down 2D RPG game using C++, and lua
Favorite Gaming Platforms: N64, and PC
Programming Language of Choice: C++, Lua

Lua reading array of strings issue C++/SDL

Post 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);
}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Lua reading array of strings issue C++/SDL

Post by dandymcgee »

Out of curiosity, why are you trying to load the map in Lua rather than passing the filename of the map from Lua (or a config file even) to C++ and doing the loading as part of the engine?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Light-Dark
Dreamcast Developer
Dreamcast Developer
Posts: 307
Joined: Sun Mar 13, 2011 7:57 pm
Current Project: 2D RPG & NES Platformer
Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
Programming Language of Choice: C/++
Location: Canada

Re: Lua reading array of strings issue C++/SDL

Post by Light-Dark »

Just a question and I don't mean to offend you sir, but why the hell are you loading a new image for every tile :shock:? You know you can just load a single sheet and clip it up right...?
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
Image
werdo659
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri Aug 19, 2011 11:37 am
Current Project: A top-down 2D RPG game using C++, and lua
Favorite Gaming Platforms: N64, and PC
Programming Language of Choice: C++, Lua

Re: Lua reading array of strings issue C++/SDL

Post by werdo659 »

Light-Dark wrote:Just a question and I don't mean to offend you sir, but why the hell are you loading a new image for every tile :shock:? You know you can just load a single sheet and clip it up right...?
yes but i didnt feel like it i was lazy and tired
werdo659
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri Aug 19, 2011 11:37 am
Current Project: A top-down 2D RPG game using C++, and lua
Favorite Gaming Platforms: N64, and PC
Programming Language of Choice: C++, Lua

Re: Lua reading array of strings issue C++/SDL

Post by werdo659 »

dandymcgee wrote:Out of curiosity, why are you trying to load the map in Lua rather than passing the filename of the map from Lua (or a config file even) to C++ and doing the loading as part of the engine?
That way players can add new tiles to the game
Post Reply