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

Author Topic: SFML Tile Map Tearing  (Read 10146 times)

0 Members and 2 Guests are viewing this topic.

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
SFML Tile Map Tearing
« on: May 24, 2014, 12:39:12 pm »
Greetings,

As many others here I've recently started developing a small game in my spare time.
I have a decent amount of experience with c++ and feel comfortable enough to take this next step,
including school courses I have around 20 weeks of experience using raw OpenGL (1.1 & 3.2+) as well.

I started by making a small tile map, according to the tutorial found here:
http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php

This tutorial is extremely self explanatory and I didn't have any issues getting it up and running.
After some messing with view (I want to be able to scale my map up and down) I have been getting issues;
Tile tearing occurs and I'm left with something like this:

Image: Tile map 'tearing' occuring.
(click to show/hide)

I did some research (Yes I really did!) and I found a really promising solution here:
http://en.sfml-dev.org/forums/index.php?topic=6665.0

But the offsetting by 0.0075 just doesn't seem to work. (It does feel like it's getting closer)
I tried flooring some of the values while building the Vertex Array but to no avail.
The linked post doesn't seem to exist anymore, which is a shame since it might give me some clarity on the subject.

The important parts of the code are the following:

Building the Vertex Array:
(click to show/hide)

Drawing the Tile Map:
(click to show/hide)

Drawing the 'Game':

(click to show/hide)

I'm really hoping you guys can help me out!

Thanks in advance for any pointers in the right direction.

Yours sincerely,
Oxy

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: SFML Tile Map Tearing
« Reply #1 on: May 24, 2014, 01:13:55 pm »
Wasn't the number 0.375? You may want to try that.

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML Tile Map Tearing
« Reply #2 on: May 24, 2014, 02:26:40 pm »
I think I've fixed it. The rounding error seemed to be in the view.setCenter().

The 'fixed' code is as follows:

Main.cpp
(click to show/hide)

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: SFML Tile Map Tearing
« Reply #3 on: May 26, 2014, 05:01:26 am »
From my personal experience that occurred when position was not rounded value.
Seeing you fixed it but you can still use std::floor(value) to get rounded down decimal value.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML Tile Map Tearing
« Reply #4 on: May 26, 2014, 10:17:05 pm »
Seems like it isn't fully fixed; it just 'looks' like it works at specific resolutions/view sizes.

I've set the render window to 1920*1080 resolution. When I resize it to fullscreen (2560*1440) tearing occurs.
The difference is extremely small (it seems to be just 1 pixel) so this indeed seems like a rounding error.

From my personal experience that occurred when position was not rounded value.
Seeing you fixed it but you can still use std::floor(value) to get rounded down decimal value.

What do you mean exactly? I already round the position.x and position.y in the previous code snippet:

playerView.setCenter(floor(player.getPosition().x), floor(player.getPosition().y));

So, this question is open again! Any pointers?

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML Tile Map Tearing
« Reply #5 on: May 28, 2014, 11:47:28 pm »
Sorry guys, still haven't fixed it. Can anyone give me some guidance as to why this is happening?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Tile Map Tearing
« Reply #6 on: May 29, 2014, 12:12:25 am »
Have you read this?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML Tile Map Tearing
« Reply #7 on: May 29, 2014, 03:41:06 pm »
I had not read that no, thanks for the resource!
However, I don't feel like I'm breaking any of those guidelines:

The object's position, origin and scale have no fractional part.

The TileMap is a VertexArray; I have tried flooring all of it's components but it's built from integers (like in the official tutorial).

The object's and the view's rotation are a multiple of 90 degrees.
No rotation so I think I'm pretty much fine here.

The view's center and size have no fractional part.
The views center is the players position, as you can see I have floored it (I have tried rounding it as well). The size is based on the size of the window (divided by 2) but I also floor/round this value just in case.

I'm obviously doing something wrong but I cant figure out what it is exactly.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Tile Map Tearing
« Reply #8 on: May 29, 2014, 04:02:13 pm »
Can you get it to work with sprites instead of vertex arrays?

You should really try to reduce your code to a minimal example that can be written completely in a main() function. Just the simplest possible tile map and view, no game logic whatsoever. Use a single tile type and a fixed view. Make sure that while reducing, you can still reproduce the problem.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML Tile Map Tearing
« Reply #9 on: May 30, 2014, 03:57:18 pm »
I might be able to get it working with sprites but I would rather not; the maps can get rather big.
Would it be possible to post the entire solution? I've already tried starting a clean version but I can't really reproduce the error in a very minimal environment.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Tile Map Tearing
« Reply #10 on: May 30, 2014, 04:42:57 pm »
I might be able to get it working with sprites but I would rather not; the maps can get rather big.
I didn't mean you should do this in your own code, I was talking about finding the problem.

Would it be possible to post the entire solution?
No.

I've already tried starting a clean version but I can't really reproduce the error in a very minimal environment.
That's why I said "Make sure that while reducing, you can still reproduce the problem". If necessary, start with your original code, and omit more and more parts until you end up with a minimal complete example. It's possible that you already find the mistake in the process of reduction.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OxySocks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML Tile Map Tearing
« Reply #11 on: May 30, 2014, 07:01:54 pm »
Thanks Nexus,

While minifying code I've tracked 99% of the tearing down to anti-aliasing. I only get very small vertical tears now while jumping, probably due to another significant rounding mistake. There is no tearing now while not moving. Do you have any explanation as to why this has such a drastic effect? I understand it's not absolutely necessary in my case due to the pixelart style of the application; but I didn't see any harm in using it anyway.

I have indeed made a small code sample: It's mostly based on the example given in the tutorial:

Main.cpp:
(click to show/hide)

Tilemap.hpp:
(click to show/hide)

I've attached a small tileset for demonstration purposes. Commenting/uncommenting line 9 in Main.cpp shows some tearing (at least on my systems).

So again, thanks for all your help! :)


MaxxNiNo

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: SFML Tile Map Tearing
« Reply #12 on: April 21, 2020, 05:55:04 am »
The problem not in the Tile Map itself. It's in the Camera view. when Change the Camera position, just round x,y position, be for apply the change. My example:
https://github.com/UxiBeo/Game_SFML/blob/OOP/GameSFML/Graphics.cpp

 

anything