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

Author Topic: Question about drawing tilemaps off-screen  (Read 2574 times)

0 Members and 1 Guest are viewing this topic.

CowNation

  • Newbie
  • *
  • Posts: 20
    • View Profile
Question about drawing tilemaps off-screen
« 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?

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Question about drawing tilemaps off-screen
« Reply #1 on: May 01, 2018, 08:23:01 am »
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.
Failing to succeed does not mean failing to progress!

CowNation

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Question about drawing tilemaps off-screen
« Reply #2 on: May 01, 2018, 03:53:33 pm »
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
}


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Question about drawing tilemaps off-screen
« Reply #3 on: May 02, 2018, 12:46:22 am »
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:


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's Tile Map (that's why it exists!)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything