Can you use fstream to read values into a 2D array? I have one tile array and one object array, and want to have my map values saved in text(or binary) files external to the application, then read them into the tile and object array when you reach a point on the current map.
On a different train of thought, could I use a scripting language instead of just using fstream to declare and set the values of a 2D array in a script?
reading in maps
Moderator: Coders of Rage
- Donutslayer7
- Chaos Rift Newbie
- Posts: 16
- Joined: Tue Aug 18, 2009 5:16 pm
- Current Project: Map-Editor
- Favorite Gaming Platforms: N64, SNES, anything Nintendo
- Programming Language of Choice: C++
- Location: U.S.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: reading in maps
Absolutely, you could do either one, although using a scripting language would be a lot more involved. How you do it is really up to you and depends heavily on how your program is designed. A basic way of doing it however would be setting up your file so that each line is a tag (text string) followed by a value.
IE:
levelName The Alabaster Citadel
musicTrack Vengence.mp3
background marbleCollumns.png
Then create a function to read in the file like:
Youre also going to want to add extensive error checking as well for this type of thing.
hope this helps :D
IE:
levelName The Alabaster Citadel
musicTrack Vengence.mp3
background marbleCollumns.png
Then create a function to read in the file like:
Code: Select all
while( !fin.eof() )
// my phone doesnt seem to do curly braces :(
{ //ha! found it
string tag;
fin >> tag;
switch( tag )
case "background":
string bgName;
fin >> bgName;
// code to set the background image to the file named
break;
.
.
.
}
hope this helps :D
My love is like a Haddoken, it's downright fierce!
Re: reading in maps
You might check out this thread:http://elysianshadows.com/phpBB3/viewto ... 38&start=0
Re: reading in maps
Code: Select all
#include <fstream>
#include <stdio.h>
using namespace std;
class map
{
public:
map();
void printData();
private:
int data[10][10];
};
map::map()
{
for(int y = 0;y < 10;y++)
for(int x = 0;x < 10;x++)
this->data[y][x] = 10*y + x;
}
void map::printData()
{
for(int y = 0;y < 10;y++)
for(int x = 0;x < 10;x++)
printf("%d,%s", this->data[y][x], x == 9 ? "\n" : " ");
}
void saveMap(map *data, char *fileName)
{
ofstream ofs(fileName, ios::binary);
ofs.write((char*)data, sizeof(map));
ofs.close();
}
map *loadMap(char *fileName)
{
map *temp = (map*)malloc(sizeof(map));
ifstream ifs(fileName, ios::binary);
ifs.read((char*)temp, sizeof(map));
ifs.close();
return temp;
}
int main(void)
{
map *saveME = new map();
saveME->printData();
saveMap(saveME, "test.map");
map *test = loadMap("test.map");
test->printData();
getchar();
return 0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- Donutslayer7
- Chaos Rift Newbie
- Posts: 16
- Joined: Tue Aug 18, 2009 5:16 pm
- Current Project: Map-Editor
- Favorite Gaming Platforms: N64, SNES, anything Nintendo
- Programming Language of Choice: C++
- Location: U.S.
Re: reading in maps
Ah, I've seen the tag type structure used in the Source engine, all the tags were inside of scripts, and I kind of understand the code, but I'm not very familiar with the eof function(it looks very useful though). I guess I'll have to look more into fstream or some scripting language and look back at these examples.