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

Author Topic: [Solved]Loading tile maps  (Read 1954 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
[Solved]Loading tile maps
« on: August 28, 2013, 01:32:24 pm »
Hello

I downloaded a mario clone source from here : http://en.sfml-dev.org/forums/index.php?topic=12232.msg84944#msg84944

But I didn't understand the tile map loading part. Can anyone please explain it to me?

Thanks
« Last Edit: August 30, 2013, 03:37:31 pm by The illusionist mirage »

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Loading tile maps
« Reply #1 on: August 28, 2013, 04:19:39 pm »
The tilemap is hardcoded into an array of sf::String. He doesn't really "load" the tilemap, he directly reads the array and draws it.

To draw the tilemap, he loops on every character in every string of his tilemap: loop from 0 to tilemap height (H) and loop from 0 to tilemap width (W). The character represents the type of the tile, P is ground, C is ?block, etc. He has only one sprite, and change its textureRect (with hardcoded values) according to the character read in TileMap[j], and its position too, then draw it.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Loading tile maps
« Reply #2 on: August 30, 2013, 03:24:48 pm »
Thanks! Now I am clear about how stuff works using SFML.

BTW what do you suggesst is better - keeping tile maps in separate text files and using them through the fstream class objects or by declaring them in a sf::String?

Like:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
.
.
.
int main()
{
     std::fstream tilemap("Tilemap.txt", ios::in);
     .
     .
     .
}
 

OR

#include <SFML/Graphics.hpp>

sf::String tilemap[10][10] =
{ "1 1 1 1 1 1 1 1 1 1",
  "1                 1",
  "1                 1",
  "1                 1",
  "1                 1",
  "1     #####       1",
  "1                 1",
  "1                 1",
  "1                 1",
  "1 1 1 1 1 1 1 1 1 1",
};
.
.
.
int main()
{
     .
     .
     .
}


 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Loading tile maps
« Reply #3 on: August 30, 2013, 03:28:15 pm »
External, so you can just edit the map and restart/reload instead of having to recompile every single time. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Loading tile maps
« Reply #4 on: August 30, 2013, 03:37:03 pm »
External, so you can just edit the map and restart/reload instead of having to recompile every single time. ;)

Thanks! :D

 

anything