SFML community forums
Help => Graphics => Topic started by: CowNation on May 01, 2018, 01:30:41 am
-
I was able to figure out how to draw a tilemap but I have a few questions.
How would one draw a tilemap off-screen or generate it procedurally?
I'm using https://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map
for my tilemap class.
Or how would you draw anything off-screen for that matter?
My View is centered on my local sprite
View view;
view.setCenter(Game::Entity::g_eLocal.sprite.getPosition());
view.setSize(((float)Engine::windowSize.x), ((float)Engine::windowSize.y));
window->setView(view);
Maybe i'm just not understanding how Views work.
Also, how would you draw an infinite tilemap?
-
Why do you want to draw anything off-screen? This just consumes unnecessary resources. Imagine drawing an infinite map, it would take you an infinite amount of time to do that.
Try to draw only those tiles (and other objects), which are visible to your view. For small games this is not so important, because your computer can probably handle it, but for your scale this doesn't apply anymore.
For that you want to calculate which tiles are visible and recreate the vertex array with them every frame.
You also don't want to store every tile, just the ones nearby. (take a look at chunks)
As for how to generate your map: it depends on what type of map you want to make. There are many examples for various types of maps online. Perlin Noise is often used for that. Simplex Noise, Voronoi Map, etc...
To draw your infinite map: you load (or generate) only nearby tiles (chunks) and draw just the visible ones from those on screen.
-
Why do you want to draw anything off-screen? This just consumes unnecessary resources. Imagine drawing an infinite map, it would take you an infinite amount of time to do that.
Try to draw only those tiles (and other objects), which are visible to your view. For small games this is not so important, because your computer can probably handle it, but for your scale this doesn't apply anymore.
For that you want to calculate which tiles are visible and recreate the vertex array with them every frame.
You also don't want to store every tile, just the ones nearby. (take a look at chunks)
As for how to generate your map: it depends on what type of map you want to make. There are many examples for various types of maps online. Perlin Noise is often used for that. Simplex Noise, Voronoi Map, etc...
To draw your infinite map: you load (or generate) only nearby tiles (chunks) and draw just the visible ones from those on screen.
So something like this?
(sorry for really bad psuedo-code, am away from my PC)
create backdrop sprite that is the size of the window and is always set to origin of window.
draw backdrop sprite
create grass tilemap
while backdrop is visible{
draw grass tilemap
}
-
The basic idea of tile maps is to store the data for the tiles yourself and then draw only those tiles that would appear in the view.
A quick illustration:
(https://i.imgur.com/20RY0xT.png)
To be able to dynamically change which tiles are displayed, you will need to create your own custom tile map graphic, adapt the tutorial one or use a pre-made displayer such as Selba Ward (https://github.com/Hapaxia/SelbaWard/wiki)'s Tile Map (that's why it exists!)