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

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

0 Members and 1 Guest are viewing this topic.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Loading a map of a file
« Reply #15 on: July 14, 2012, 02:48:32 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?
If you take your time to write this post (twice), 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 [] (an array of char, which is equal to char *).
And don't use char[] that's old C code and perfect for stack overflows, crashes and other security risks.

Just use a std::string unless you have a reason not to.
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 #16 on: July 14, 2012, 03:07:20 am »
eXpl0it3r, I heard that std :: string is equal to an array of chars. I believe in you, but can you explain it?

Furthermore, there are functions (fstream library) using const char * std. Can I use std::string as parameter?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Loading a map of a file
« Reply #17 on: July 14, 2012, 03:24:44 am »
What you've probably read, is that a C string is equal to char */char []. But a C string is not a std::string. A C string is just an array of chars with null delimiter \0 at the end, thus it has no function or anything, it's just data. The std::string is a class defined in the STL and has diffrent functions. One of which is .str() which returns a C string.

This is basic C/C++ knowledge you should defenetly read more about 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 #18 on: July 14, 2012, 08:13:05 am »
Thanks.

Can anyone give me an idea of ​​how to save / load a map with objects in different positions (not aligned to a grid)? I am spending much time on this.

mastodona7x

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a map of a file
« Reply #19 on: July 14, 2012, 02:35:54 pm »
game_maker I have very little knowledge of c++/sfml but one of the first games I made was a battelships game, which is the same sort of idea.

I made a vector<vector> for the map and that stored values for different things at each coordinate (0,4 might have had a value of "2" for example which translated to part of a ship, or value of 1 for open water, etc...then just loop through the vector to output whatever each value translates to to the screen, similar to how you've been doing.

for example:
Code: [Select]
//create empty map array
vector<vector<int>> player1map; // create 2d vector (matrix)

 // map array size xsize * ysize filled with 0's
        player1map.resize(xsize, vector<int>(ysize,0));

the above makes a vector of ints and xsize and ysize are the x and y size for the grid (might be 10*10 for example), first it fills them with 0's but you can obviously do what you like there,

Code: [Select]
for(int y=0; y<ysize; y++) // y-wise (number of rows)
    {

for(int x=0; x<xsize; x++) // fill in x-wise
        {
            switch (maparray[x][y])
{
case 0: // fill "square" with empty space / water
///display empty water graphic here
break;
                         }
         }
   }


Its very basic and not reading from files etc but maybe it'll give you something to work with I don't know.

mastodona7x

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a map of a file
« Reply #20 on: July 14, 2012, 02:39:07 pm »
I just realised my above post really doesn't answer your question but maybe instead of worrying about loading/saving just work out some more basic ways to do things if you are just begginning like I am? Don't try to walk before you can crawl sort of thing :) Make a basic grid based game to start with then build on what you have learned.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Loading a map of a file
« Reply #21 on: July 14, 2012, 03:54:26 pm »
@mastodona7x: Now you just need to write code=cpp instead of just code to get a code tag that highlights C++ code! ;)

Can anyone give me an idea of ​​how to save / load a map with objects in different positions (not aligned to a grid)? I am spending much time on this.
How does your game look like up to now? Because having objects with their own positions, probably requires something into the direction of an entity system.
For now I just assume that you have a st::map<std::string, sf::Sprite> that you will fill with sprites and use a key to access them more easily.
For a start you have to think about how your map format will look like. I could imagine something like this:
name:path/to/the/texture.png:10:20
This means each line starts with the name of the sprite/character/whatever that will end up as key, then we use a delimited ':' to seperate the diffrent properties. Next to the name you put the path to the texture that should be used for the sprite and the the position as x and y coordination.

Now you have to read that file line by line (code can be found earlier on this thread) and then split each line into it's individual parts. For that you can easily google "split string c++". And since you now have everything as string you'll have to convert the numbers into real numbers (int, float...), this can also be found easily with google. But keep in mind google is only a help to find a solution fast, to really understand it you'll have to figure it out on your own, or at least try to understand every step of the code you'd find.
Since you now have everything you need, you can just go ahead and create a new texture, load it from the provided path, then hand the texture over to the new sprite and set its position.

In the following pastie you can find a solution that I've used in one of my application. I may have missed some variables when copying. This solution is only one way of doing things, for example I don't use a std::map to hold my sprites as suggested. Also the use of std::move isn't always appropriate, so don't use it if you don't know what you're doing.
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 #22 on: July 14, 2012, 05:28:52 pm »
eXpl0it3r, thank you so much.

I can read the file line by line, dividing lines (split).

The problem now is to convert the "20" to 20. I think to use atoi, but I'll need the function c_str (), will not I?

Edit:

Now I saw your code at pastebin.
It was very helpful and I will try to take only the concepts.
Since I'm redoing this from scratch, I'm reviewing some things.

Thanks in advance!
« Last Edit: July 14, 2012, 05:32:51 pm by game_maker »