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

Author Topic: "infinite" tilemap - please rate & help improve  (Read 5133 times)

0 Members and 4 Guests are viewing this topic.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
"infinite" tilemap - please rate & help improve
« on: November 10, 2016, 03:07:33 am »
hello everybody.
i created this little snippet of a seamless "infinite" tilemap. it works, but i feel like it could be better. so i'm taking suggestions for everything, even variable names.

how it works:
1- check if player has moved from one tile to another
2- if yes, then search in a binary file for the current player tile
3- load player tile and some tiles around it, just enough to fill a little more than the screen
4- draw the tiles

the code is below (<100 lines). tileset is attached.
(click to show/hide)

and this is the code that generates the binary file that stores the tiles:
(click to show/hide)

I noticed that if you start at some position *relatively close* to negative values (ie 10, 10), it doesn't work as expected. i don't exactly know how to fix it.

let me know if i forgot to explain something. besides that, i take any suggestions. i really want to improve this.
thanks in advance :)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: "infinite" tilemap - please rate & help improve
« Reply #1 on: November 10, 2016, 06:16:06 am »
Hello! :)

Haven't had time to properly have a look but I did notice at a glance something small in the binary generator...

It seems that all of this:
        size_t m = n;
        while (m>=256){
            m-=256;
        }
        vec[n] = m;
could be replaced with just this:
        vec[n] = n % 256;
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: "infinite" tilemap - please rate & help improve
« Reply #2 on: November 10, 2016, 07:24:42 am »
I want to note that by putting tiles right after one another, you might get filter-artefacts (aka texture bleeding) in case you will not render pixel-perfect from texture to screen. That means if you're slightly off-pixel with your offset or if you're rotating or scaling the sprites, the edges of them might show wrong colours due to the OpenGL-filter-algorithms also using neighbour-pixels to determine the source-colour. It often helps to either duplicate border-pixels as a save-guard or to offset your sprites a little bit (to create empty space between them) in combination with using premultiplied alpha-channels. The latter might cause darker borders instead of weirdly colours ones though.
If you're only using them pixel-perfect, all is fine. I just wanted to note it, because it's a common issue.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: "infinite" tilemap - please rate & help improve
« Reply #3 on: November 10, 2016, 11:55:41 pm »
I see you're constantly loading from a file. Is this really necessary? Is this what you mean by "infinite"?

I would think that it would be a better idea to load "chunks" of the map as they are needed (as they approach the player/view) if you are unable to load the entire map at once.

Also, we are unable to actually run the code as-is as you don't provide the map file :P

There are some other things in the "un-map" part of the code too...

Why are you sleeping each frame? If you need a target/maximum framerate, try using:
window.setFramerateLimit(100);

Your movement (or "step") amount is the same each frame/cycle. This can lead to inconsistent motion; a single frame is not guaranteed to be any particular amount of time. The simple option is to simply multiply the speed you wish to move by the time passed in that particular frame - maybe something like this:
sf::Clock clock;
float movementSpeed = 100.f;
if (window.isOpen())
{
    // input

    sf::Time frameDuration = clock.restart();
    sf::Vector2f step_size(sf::Vector2f(tile_size) * movementSpeed * frameDuration);

    // logic
    // render
}
and the speed would be pretty much consistent on all computers.
The more complicated (but often more solid and reliable) approach is to Fix Your Timestep. I've personally discussed this approach many times on the SFML forums.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: "infinite" tilemap - please rate & help improve
« Reply #4 on: November 11, 2016, 03:19:27 pm »
hello! thanks for the answers.

It seems that all of this:
        size_t m = n;
        while (m>=256){
            m-=256;
        }
        vec[n] = m;
could be replaced with just this:
        vec[n] = n % 256;
that's something new to me. thanks for the hint :)


I want to note that by putting tiles right after one another, you might get filter-artefacts (aka texture bleeding) in case you will not render pixel-perfect from texture to screen. [...]
If you're only using them pixel-perfect, all is fine. I just wanted to note it, because it's a common issue.
"pixel-perfect" would be rendering an image that has the exact same size as the final sprite, in pixels? because each of my tiles have 32x32 pixels, but the sprites are 80x60. i do not notice these artifacts...


I see you're constantly loading from a file. Is this really necessary? Is this what you mean by "infinite"?

I would think that it would be a better idea to load "chunks" of the map as they are needed (as they approach the player/view) if you are unable to load the entire map at once.
yes, my first approach was using "chunks". i don't remeber why it didn't work well for me, but i'll try that again.

Also, we are unable to actually run the code as-is as you don't provide the map file :P
the map file is created with the second piece of code :P

Why are you sleeping each frame?
the sleep functon is just to relieve the cpu work. without it, the cpu usage goes to the maximum :o
Visit my game site (and hopefully help funding it? )
Website | IndieDB

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: "infinite" tilemap - please rate & help improve
« Reply #5 on: November 11, 2016, 10:18:11 pm »
Coincidentally I've been working on something similar myself, and have been blogging my musings, if that's any help.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: "infinite" tilemap - please rate & help improve
« Reply #6 on: November 17, 2016, 01:02:44 am »
great, i'm reading it now! thanks for the hint :D
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything