Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411489 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 04:33:17 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Loading text file into 2-dim. array?
Pages: [1]
Print
Author Topic: Loading text file into 2-dim. array?  (Read 3109 times)
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« on: August 30, 2008, 01:09:51 AM »

Let's say I want to use text files to load my maps in a simple tile-based game, (C++ and Allegro) how would I do this? I'm completely lost on that one.
Logged

muku
Level 10
*****


View Profile
« Reply #1 on: August 30, 2008, 03:48:17 AM »

What exactly are you having trouble with? Basically, you open a file with

Code:
ifstream file(filename);

Then you can read lines from it one at a time with

Code:
string line;
getline(file, line);

and then you simply go through every line with a loop

Code:
for (int x = 0; x < line.size(); ++x) {
    if (line[x] == '#')
        map[y][x] = Tile.Floor;
    else if (...)
        ....
}

or however you store your map data. So, what do you need help on?

(Disclaimer: Completely untested code. Might be wrong.)
Logged
OryBand
Level 0
***


Screamadelica.


View Profile WWW
« Reply #2 on: August 30, 2008, 05:05:45 AM »

Heh, I did just that for my high-school project some years back.

There are two solutions - depending if you know the size of the map before runtime or not.

Knowing the size from start:
Well, all you have to do is just iterate over the text file and set the tiles accordingly. You should use characters to represent different tile types - like grass-planes, mountains, water, etc.

A 3x3 map will look something like this:

SSS
MMM
GGG

Those are for a row of Sea, a row of Mountains, and a row of Grass.

Then all you have to do is just read the characters one by one, and set the tiles accordingly.

Here is a psuedo-code example:
Code:
x = 0
y = 0
for tileChar in readChar(textFromFile):
    map[x][y] = SetTileByChar(tileChar)
    x++
    y++

that's it.

Not knowing the size of the map before runtime - determining the size in runtime:
Now - if you don't know the size of your map before runtime, you'll have to determine its size by reading it from some sort of configuration file, or by just counting the rows and columns of the text map. Then, all you have to do is just dynamically allocate the map size, and execute the above code.

If your using C++ / Java or the like, that'll have to be done usine the Allocate functions (alloc, malloc, etc). If your using scripting languages like Python and such, you'll just have to declare the map size - map[50][70]

Hope that helped,
Cheers.
Logged

Never go to bed mad, stay up and fight. - Phyllis Diller
Gnarf
Guest
« Reply #3 on: August 30, 2008, 07:06:32 AM »

The allocation functions are generally used in C. Java and C++ have 'new' operators. In C++ there is a heap/stack distinction that may or may not make a difference, depending on how you program the thing. If so, using vectors instead of arrays might make things easier.

Code:
x = 0
y = 0
for tileChar in readChar(textFromFile):
    map[x][y] = SetTileByChar(tileChar)
    x++
    y++

That doesn't work. You're increasing both x and y in every iteration, so you're only assigning to positions along a diagonal line in the map (like, [1][1] and [2][2], but not [1][2]). if you do it like that. It needs and inner loop or some if-stuff in there stuff in there. Eg. if we're going with the 3x3 map:

Code:
w = 3
h = 3
x = 0
y = 0
for tileChar in readChar(textFromFile):
    map[x][y] = SetTileByChar(tileChar)
    x++
    if x == w:
        x = 0
        y++
        if y == h:
            break
        // maybe deal with newline or something here

I usually go for (y,x) instead of (x,y) too. So that in memory, the order is the same as in the file, and probably more like the order I'm drawing things to the screen. It's my impression that that's pretty common. Like, muku totally did that in his example.
Logged
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #4 on: August 30, 2008, 07:19:11 AM »

I'm not having problems with one specific thing; I just haven't ever read external files (except sounds and bitmaps) before, and I have no idea how it would be done.
Logged

Gnarf
Guest
« Reply #5 on: August 30, 2008, 07:31:27 AM »

muku's post deals a bit with that. Using ifstreams seems the way to go for reading from files. They are streams, like cin, which I suppose you might have experimented with some?

Also, if you have a specific format for the map-file in mind, it's possibly easier to help you out. (like, if all you need to do is to read one character at a time, I'd rather not explain how to do something more complex than that that's also completely different and so on :)
Logged
Hideous
That's cool.
Level 10
*****


3D models are the best


View Profile WWW
« Reply #6 on: August 30, 2008, 07:34:53 AM »

Yeah, muku's post seems to be exactly what I'm looking for.
Logged

OryBand
Level 0
***


Screamadelica.


View Profile WWW
« Reply #7 on: August 30, 2008, 09:37:56 AM »

Thanks for the fix in the code, your right. Wrote that kinda fast, so...

Anyways, I suggest using scanf's instead of ifstreams. A lot more clearer.
Logged

Never go to bed mad, stay up and fight. - Phyllis Diller
muku
Level 10
*****


View Profile
« Reply #8 on: August 30, 2008, 09:58:45 AM »

I personally prefer ifstreams. I find them easier to use, you get automatic resource managament via RAII, they are typesafe etc. The STL is on the whole a very good thing and a great advantage over C.
Logged
Gnarf
Guest
« Reply #9 on: August 30, 2008, 10:15:12 AM »

ifstream is used for opening the file and reading from it, so scanf won't make a very good replacement. Can of course use FILE, fopen and fscanf. I'm more familiar with those myself since I've written a lot more C than C++, but I'd suggest the C++ style of doing it rather than the C style when someone asks how to do something in C++. Less likely to clash with whatever else he might be doing and whatever.
Logged
OryBand
Level 0
***


Screamadelica.


View Profile WWW
« Reply #10 on: August 30, 2008, 02:54:35 PM »

yeah, meant fscanf...

Anyways, doing something C-style when actually coding with C++ won't collide in any way I'm familiar with, btw.

If you do know of any example, I'll be happy to know.
Logged

Never go to bed mad, stay up and fight. - Phyllis Diller
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #11 on: August 30, 2008, 03:04:39 PM »

I think if you're doing C++, you should use the modern C++ standards (i.e. ifstream)
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Gnarf
Guest
« Reply #12 on: August 30, 2008, 06:33:24 PM »

yeah, meant fscanf...

Anyways, doing something C-style when actually coding with C++ won't collide in any way I'm familiar with, btw.

If you do know of any example, I'll be happy to know.
I'm sure it won't make anything absolutely impossible to achieve or any such. In general C++-style things are likely to be more easily compatible with other C++-style things. For example, C++-style things might throw exceptions all on their own. So like if you're generally handling errors by catching exceptions, then it's probably preferable to use something that throws exception when things go wrong rather than something that makes you have to check for errors C-style (possibly just to throw an exception when something goes wrong). Not converting between char arrays and strings all the time is nice too. Little things like that. And there's like neat things you can do because it's all fancy and object oriented and stuff.

Nothing very serious or anything, and certainly nothing that can't be worked around. Just, if there is a totally C++-way of doing something I'd suggest that one when someone asks how to do something in C++ :)
Logged
OryBand
Level 0
***


Screamadelica.


View Profile WWW
« Reply #13 on: August 31, 2008, 09:08:43 AM »

Thanks.
Logged

Never go to bed mad, stay up and fight. - Phyllis Diller
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic