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

Author Topic: Updating Tile Map?  (Read 1262 times)

0 Members and 1 Guest are viewing this topic.

xy1qw1

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Updating Tile Map?
« on: January 12, 2024, 01:36:22 am »
Hi. I have a problem with tilemap. I used that example in my project.

https://www.sfml-dev.org/tutorials/2.6/graphics-vertex-array.php

Because i would like to create a tilemap like 100 X 100 or bigger. Previously i was trying without vertex array.
Like this code:

        int map[my][mx] = { 0,1,1,1,0,0...

}
/////////////////////////////////////////////////
        for (int h = 0; h < mx; h++)
        {
                for (int w = 0; w < my; w++)
                {
                        map[h + x][w + y] = 0;

                }
        }
 
////////////////////////////////////////////

my question is how can i update only 1 tile color, shape or another values in first example as on the top link. Please if you explain with a minimal code or share similar example links i will appreciate it. Thanks for all responses.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Updating Tile Map?
« Reply #1 on: January 13, 2024, 10:23:35 pm »
I would guess that x and y are always zero otherwise those loops are going to go past the array's limit.

To change a single element in a 2-dimensional array (as you have), you simply do it once; the loop is changing a single element multiple times (different elements).
So, to change the "map" array at (5, 2) to "1", you could do:
map[2][5] = 1;

To change a single tile in the example tile map, you'd need to add a method that modified that exact tile's vertices.
Basically, just do the code inside the double-loop (without the loops) and replace i and j with the x and y (respectively) of the tile's location.

Something like (in the TileMap class):
void changeTile(const unsigned int i, const unsigned int j, const int tileNumber)
{
    // code from between those for loops goes here (but you should remove the "tileNumber = ..." line)
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

xy1qw1

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Updating Tile Map?
« Reply #2 on: January 14, 2024, 12:03:50 pm »
Hapax thanks a lot for you response, yesterday i created a tilemap class. I researched some tutorials and i did something. Currently its working well. As you say i can modify tiles. That is the best tilemap system i have ever used :)

300 x 200 map (100 px per tile) is rendering with normal speed. Thanks for advices i ll keep it in my mind and apply in my project.
I m trying to make a little rpg project. If i finish the prototype of my game, i will share with forum.

Best regards.

https://ibb.co/fCbDV1k
« Last Edit: January 14, 2024, 12:07:29 pm by xy1qw1 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Updating Tile Map?
« Reply #3 on: January 14, 2024, 01:59:19 pm »
You're very welcome. Glad you sorted it.

For your information, however, there are existing solutions for tilemaps. Here are a couple:
Selba Ward's Tile Map (basic 2D grid with automatic separation support)
Cheese Map (flexible layered maps and free tiles with automatic culling)
Note: I made both of these to help people create maps more simply.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*