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

Author Topic: Tilemap engine causing textures to move when view is moved.  (Read 5266 times)

0 Members and 1 Guest are viewing this topic.

gingerBill

  • Newbie
  • *
  • Posts: 6
    • View Profile
Tilemap engine causing textures to move when view is moved.
« on: August 19, 2013, 01:48:25 pm »
Hi I've been make this game about a fortnight now and I've been having this problem with the tiles/blocks being rendered when the view is moved.

I've been fixing the view to the character but when the character/view is moved in the y-axis (and sometimes the x-axis but very rare) the tiles seem have these lines:



I've figured out that these lines are cause by the texturerect of the tile (which is a sprite) is being moved cause some of the rest of the texture to be show in it. The Blue lines are where texture is transparent and the brown lines on some of the grass tiles, the texture is showing another tile.


I would show some minimal code if I could but I cannot seem to replicate this on a small level so here are snippets of my code:

Place tiles into sprites
m_tiles => std::Vector<sf::Sprite>
terrain2.png is a 256x256 png containing 256 tiles (akin to older version of minecraft)

for (uint8_t y = 0; y < 16; y++)
{
        for (uint8_t x = 0; x < 16; x++)
        {
                sf::Sprite stemp;
                stemp.setTexture(textureManager.getTexture("res/image/terrain2.png"));
                stemp.setTextureRect( sf::IntRect(16*x, 16*y, 16, 16) );
                stemp.setOrigin(8, 8);
                m_tiles.push_back(stemp);
        }
}
 

Display Tiles
mapLevel => std::vector< std::vector<uint8_t> > this way I call a tile like mapLevel[x][y]

// Only display what is on screen
        int drawRangeX = window.getSize().x/64;
        int drawRangeY = window.getSize().y/64;

// Add one to draw range
        drawRangeX++;
        drawRangeY++;

        for (unsigned int y = std::max((m_character.getPosition().y/16.0f)-drawRangeY, 0.0f);
             y < std::min((m_character.getPosition().y/16.0f)+drawRangeY, (float)(gridSize.y-1)); y++)
        {
                for (unsigned int x = std::max((m_character.getPosition().x/16.0f)-drawRangeX, 0.0f);
                     x < std::min((m_character.getPosition().x/16.0f)+drawRangeX, (float)(gridSize.x-1)); x++)
                {
                        uint8_t tileId = mapLevel[x][y];
                        if (tileId != 0x00)
                        {
                                switch (tileId) // used to "randomize" tiles or get specific tile
                                {
                                case 0x05: // Grass
                                        tileId += (x+y)%2;
                                        break;
                                case 0x30: // Ore
                                        tileId += (3*x + 2*y)%2;
                                        break;
                                case 0x0C: // Leaves
                                        if (mapLevel[x-1][y] != 0x0C && mapLevel[x][y-1] != 0x0C)
                                                tileId += 1;
                                        else if (mapLevel[x+1][y] != 0x0C && mapLevel[x][y-1] != 0x0C)
                                                tileId += 2;
                                        break;
                                default:
                                        break;
                                }

                                sf::Sprite temp = m_tiles[tileId];
                                temp.setPosition(x*16, y*16);
                                if (layerBrightness != 0xFF)
                                        temp.setColor(sf::Color(layerBrightness, layerBrightness, layerBrightness));

                                window.draw(temp);
                        }
                }
        }
 

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Re: Tilemap engine causing textures to move when view is moved.
« Reply #1 on: August 19, 2013, 02:24:58 pm »
Did you try aligning your view to integer coordinates?
Listen to my band: pencilcase.bandcamp.com

gingerBill

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Tilemap engine causing textures to move when view is moved.
« Reply #2 on: August 19, 2013, 02:31:16 pm »
Did you try aligning your view to integer coordinates?

I'm updating my view by moving it my how much my character moves each update cycle. Sorry but I don't understand what you mean my aligning the view to integer coordinates.

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Tilemap engine causing textures to move when view is moved.
« Reply #3 on: August 19, 2013, 02:36:09 pm »
Did you try aligning your view to integer coordinates?

I'm updating my view by moving it my how much my character moves each update cycle. Sorry but I don't understand what you mean my aligning the view to integer coordinates.

If your caracter position is [27.6 : 125.14] you may want to set your view center at [28 : 125].
You may also want to do the same for sprites position if not already the case.

gingerBill

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Tilemap engine causing textures to move when view is moved.
« Reply #4 on: August 19, 2013, 02:46:34 pm »
Did you try aligning your view to integer coordinates?

I'm updating my view by moving it my how much my character moves each update cycle. Sorry but I don't understand what you mean my aligning the view to integer coordinates.

If your caracter position is [27.6 : 125.14] you may want to set your view center at [28 : 125].
You may also want to do the same for sprites position if not already the case.

Thank you so much! I think this has fixed the problem; I've been trying to fix this for days! I wonder why a non integer view centre would cause the textures of the sprites to shift though.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Tilemap engine causing textures to move when view is moved.
« Reply #5 on: August 19, 2013, 02:50:39 pm »
Quote
I wonder why a non integer view centre would cause the textures of the sprites to shift though.
Because of OpenGL rasterization rules, which become more complicated in this case and may lead to such artifacts because texture pixels no longer match the window's pixels 1:1.
Laurent Gomila - SFML developer

gingerBill

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Tilemap engine causing textures to move when view is moved.
« Reply #6 on: August 19, 2013, 03:07:55 pm »
Quote
I wonder why a non integer view centre would cause the textures of the sprites to shift though.
Because of OpenGL rasterization rules, which become more complicated in this case and may lead to such artifacts because texture pixels no longer match the window's pixels 1:1.

Looking at the OpenGL specs, I think I can see why this may happen. Thank you for all the help.

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Tilemap engine causing textures to move when view is moved.
« Reply #7 on: August 19, 2013, 07:22:01 pm »
Dude, your game looks good especially only for 14 days work. Guess you have some prior experience?

gingerBill

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Tilemap engine causing textures to move when view is moved.
« Reply #8 on: August 19, 2013, 08:56:39 pm »
Dude, your game looks good especially only for 14 days work. Guess you have some prior experience?
I have had prior experience making games but not with SFML (first game with SFML). I was thinking to just using OpenGL but I thought I'd use SFML as it was easier to use/learn and I would not have to setup too much. Luckily the documentation for SFML is absolutely superb; compared to many that I have read, it you don't even need tutorials even though there are many!

JohannesMerkt

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Tilemap engine causing textures to move when view is moved.
« Reply #9 on: September 19, 2013, 04:29:17 pm »
Is there no way to get arround this ? aligning to integer coordinates is not a good option for me

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Tilemap engine causing textures to move when view is moved.
« Reply #10 on: September 19, 2013, 04:41:17 pm »
Is there no way to get arround this ? aligning to integer coordinates is not a good option for me
It's the only solution I've heard of, not sure if there are other ways.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything