Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading a map from text file  (Read 15251 times)

0 Members and 1 Guest are viewing this topic.

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Loading a map from text file
« on: July 09, 2012, 05:32:09 pm »
I want to load a map from text tile, but the coordinates are not aligned to a grid. So, a map of tiles is not useful for me. I write, in a text file (manually), the positions of the sprites.

Something like this:

name:path:x:y
sprite1:resources\sprite1.png:25:24

So I load:

while(reading)
{
std::string line;
std::string p[4];

getline(..., line)

split(line, ':', &p); //in this case I'll create a method to do this

add p[0] and p[1] to a map (<string, string>)  and return the id of the created texture
create a sprite (sf::Sprite name(id_created_texture))
set the position of the sprite to (p[2], p[3])
}
 

ps: the above is a draft of what I do.

I want to receive tips for making it.

Thanks in advance!
« Last Edit: July 14, 2012, 07:11:07 pm by game_maker »

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #1 on: July 09, 2012, 10:19:57 pm »
If you are trying to do this for a game you are making, and you don't care if EVERYTHING is made by your fingers, I would suggest making your maps with Tiled Map Editor (http://www.mapeditor.org/).
It is extremely easy to use and their is a library that loads the map directly into your program. All you have to do is change that information into SFML Textures and Sprites.
TMXParser (http://code.google.com/p/tmx-parser/)

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #2 on: July 09, 2012, 10:29:14 pm »
I found a tutorial explaining how to load from XML.
I'm trying to adapt it to the SFML 2.0.
I got it, but I want to load from txt.

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #3 on: July 09, 2012, 10:42:25 pm »
Okay well then open the text file and using a for loop, cycle through each line. Save one line at a time inside of a std::string and then us another for loop to go character by character and, depending on the character, save a specific image into a vector.

for (int row = 0; file.good(); ++row) {
    std::string line;
    getline(file, line);
    for (int col = 0; col < line.length(); ++col) {
        if (line[col] == '1') {
            // save image for ID of 1
        }
        // .... so on and so forth
    }
}

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #4 on: July 09, 2012, 11:55:19 pm »
Yes, I made (did) like this.  :)
It is almost done.

Thank you!

mastodona7x

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a map of a file
« Reply #5 on: July 12, 2012, 12:20:14 pm »
I did this for a simple battleships type game and yeh just a for loop to go through the txt file, though I used a "switch" statement instead of lots of if statements, seemed neater somehow but same end result!

for (int row = 0; file.good(); ++row) {
    std::string line;
    getline(file, line);
    for (int col = 0; col < line.length(); ++col) {

switch(line[col])
{
case '1':
            // save image for ID of 1
break;
case '2':
            // save image for ID of 2
break;
}
        // .... so on and so forth
    }
}

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #6 on: July 12, 2012, 11:45:10 pm »
Can you help me?

My project: https://dl.dropbox.com/u/38888521/sfml.zip

I am getting errors, so I can't make it work.

At the beginning, it freezes.

Goodbye!

mastodona7x

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a map of a file
« Reply #7 on: July 13, 2012, 12:05:47 am »
I can't even get it to compile never mind freeze? Im so new to C++ I could be wrong here but why are you making line a string then using [] on it? Wouldn't it be better to make it a vector and push each item in to that?

ifstream In("data.dat");
    vector v;

    cout << endl << "Read data from file" << endl;
    while ( ! In.eof() )
    {
       getline (In, str);
       v.push_back(str);
    }


the compile error I get is:

imageManager.LoadImage(line[col], id);

1>Level.cpp(97): error C2664: 'ImageManager::LoadImage' : cannot convert parameter 1 from 'char' to 'const std::string &'
1>          Reason: cannot convert from 'char' to 'const std::string'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #8 on: July 13, 2012, 04:47:32 am »
Quote
why are you making line a string then using [] on it?
A string is simply an array of characters, so using [] on it will access a specific character inside of it.

string line = "abcde";
line[2] will return 'c'.
line[4] will return 'e'.
etc.

The format of his map is a string of numbers (each number represents one tile on a tilemap). If he accesses each number individually he will be able to then display each tile individually and thereby creating his map.
Make sense?

Quote
Can you help me?

My project: https://dl.dropbox.com/u/38888521/sfml.zip

I am getting errors, so I can't make it work.

At the beginning, it freezes.

Goodbye!
No body is going to want to download your entire project and go through your entire code. Make a reduced code that still presents the error. Then C/P that code into here.

mastodona7x

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a map of a file
« Reply #9 on: July 13, 2012, 11:34:42 am »
Well for some reason the function he passes the string[] in to sees the string[] as a char and not a string so it won't compile...
« Last Edit: July 13, 2012, 11:37:49 am by mastodona7x »

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #10 on: July 13, 2012, 09:52:24 pm »
Now I am making a level editor from scratch (based in others).

Thanks

Tresky

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #11 on: July 13, 2012, 10:07:53 pm »
Quote
Well for some reason the function he passes the string[] in to sees the string[] as a char and not a string so it won't compile...
I see what you're saying. Allow me to explain. A std::string is nothing more than a character array. Therefore using the brackets on a string is simply accessing a specific element in the character array.

std::string line = "Tresky";
line[0] will return 'T'
line[2] will return 'e'
etc.

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #12 on: July 14, 2012, 01:45:20 am »
If I create a method:

MyMethod (char *);

And I use the code:

std::string A;
MyMethod (A);


Will generate errors?



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Loading a map of a file
« Reply #13 on: July 14, 2012, 02:16:55 am »
If I create a method:
MyMethod (char *);
And I use the code:
std::string A;
MyMethod (A);
Will generate errors?
If you take your time to write this post, why didn't you just test it?
Also this is a basic C++, read a book about it.

Yes of course it will generate an error since: std::string (class) is not the same type as char * (pointer to a char).
And don't use char * that's old C code and perfect for stack overflows, crashes and other security risks.

@mastodona7x: You are aware that there are code=cpp tags to use? Please use it...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

game_maker

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
    • Email
Re: Loading a map of a file
« Reply #14 on: July 14, 2012, 02:45:10 am »
Sorry, I posted the wrong thing.

The correct:

If I create a method:
MyMethod (char []);
And I use the code:
std::string A;
MyMethod (A);
Will generate errors?