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

Author Topic: Every time I optimise my map rendering code, it gets slower.  (Read 1434 times)

0 Members and 1 Guest are viewing this topic.

CHKN

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #15 on: August 01, 2023, 09:50:48 pm »
oh, it really was just as simple as removing the size parameter. thanks again for the help! I'm embarrased I didn't catch that sooner.

CHKN

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #16 on: August 01, 2023, 10:05:08 pm »
thanks for this, that fixed that particular error. I do think I'm eventually going to use tilemap, but while getting tilemap to work, I found that I had placed my draw call that drew the whole map inside the loop that iterates over every block, thus I was drawing the map thousands of times. I feel incredibly stupid. moving that draw call outside the loop fixes all performance issues. I am still going to switch to tilemap however.

Hapax

  • Hero Member
  • *****
  • Posts: 3363
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #17 on: August 01, 2023, 10:33:30 pm »
You're welcome! Hope it helps :)

I found that I had placed my draw call that drew the whole map inside the loop that iterates over every block, thus I was drawing the map thousands of times.
Oops! We've all done that; I'm sure! 8)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

CHKN

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #18 on: August 01, 2023, 10:35:51 pm »
switching to tilemap, the game loads fine, the map is made fine, I get no errors, but the tilemap only renders as a transparent rectangle with the dimensions I specified. I have no clue if this is just due to a function I need to run, (I've run setSize, setTexture, setTextureTileSize, setOutOfBoundsTile, and setLevel, as well as update and redraw in the draw loop) or if I'm doing it wrong.

int main()
{
    tileMapImg.loadFromFile("Assets/Textures/tilemap.png");
    mapTex.create(32, 32);
    mapTex.update(tileMapImg);
    tileMapDisplay.setSize(sf::Vector2f(mapWidth * blockZoom, mapHeight * blockZoom));
    tileMapDisplay.setTexture(mapTex);
    tileMapDisplay.setTextureTileSize(sf::Vector2u(1, 1));
    tileMapDisplay.setOutOfBoundsTile(3);
    sf::RenderWindow window(sf::VideoMode(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height), "Teria: also try pixel game!", sf::Style::Fullscreen);
    window.setVerticalSyncEnabled(true);
    view.setSize(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height);
    view.setCenter(0, 0);
    window.setView(view);

    //map generation code here
    {
        ...
    }

    tileMapDisplay.setLevel(map, mapWidth);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::Resized)
            {
                sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
                window.setView(sf::View(visibleArea));
            }
        }

        window.clear(sf::Color::Blue);

        sf::Time elapsed = gameClock.restart();

        //game update logic (doesn't work in it's own function for unknown reasons)
        {
            //map drawing
            {
                tileMapDisplay.update();
                tileMapDisplay.redraw();
               
                window.draw(tileMapDisplay);
            }

            //player movement
            {
              ...
            }

        }



        window.display();
    }




    return 0;
}

EDIT:
fixed it, turns out I misunderstood a function and was only rendering the first 16x16 square of tiles.
« Last Edit: August 02, 2023, 12:26:53 am by CHKN »

Hapax

  • Hero Member
  • *****
  • Posts: 3363
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #19 on: August 02, 2023, 01:38:01 am »
Was it the view centre? If you set the centre of the view to 0, 0 then (0, 0) is in the centre of the view ;D
This means that the top and left parts of the screen are negative values.
You're also using a different system when resizing the window where (0, 0) is at the top-left corner so it may change depending on when you've resized or not.

It doesn't look like you've set the grid size. And it's working now? The grid size is basically the number of tiles visible and it defaults to 1x1...

You should probably set setNumberOfTextureTilesPerRow too. If your texture is one tile per pixel, number of texture tiles per row for 32x32 would be 32.

If you have any more confusion with Tile Map and I am unable to respond, you could try the Tile Map wiki on GitHub:
https://github.com/Hapaxia/SelbaWard/wiki/Tile-Map
It describes every method.

Also look at the simple example at the bottom. It's really short and is probably the minimum you should be setting up. It also has comments to help ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

CHKN

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #20 on: August 02, 2023, 05:52:07 pm »
sorry for responding so late. I've got it all fixed and working now. I'm going to try implementing your progress bar as well, as my own doesn't seem to be working. Thanks again for all the help you've given me!
EDIT:
though I forgot to mention testing full size worlds (2000x900), even on release mode, I get single digit framerates. I think the only thing possible at this point is maybe a vertex array, or going back to using an image but in a smarter way.

EDIT AGAIN:
after some thinking, I've reimplemented my image method, but I've made it so that it only iterates over the blocks visible, instead of the whole image, and then checking if it's visible. It's fast, even at larger world sizes.
« Last Edit: August 02, 2023, 08:37:43 pm by CHKN »

Hapax

  • Hero Member
  • *****
  • Posts: 3363
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #21 on: August 02, 2023, 09:56:46 pm »
You're welcome. Hope Selba Ward stuff is useful to you as well! :)

It's interesting that you had an issue with a 2000x900 level. I tested Tile Map with exactly that size earlier on in our discussion and it seemed fine. ??? :'( :(

How many tiles were visible on the screen at one time i.e. grid size?
Remember that the level is 2000x900 but the grid size of Tile Map should be approximately how many tiles you can see. Grid size, then, is the optimisation stage.

It's worth noting that Tile Map is, in fact, using a (version of a) vertex array and it draws them all with one call.



Oh, and if you're still not sure about Selba Ward, have a look at its SFML forum thread here:
https://en.sfml-dev.org/forums/index.php?topic=19496.0
There even a short video demo showing some of the drawables (some hadn't been created at that point).

Maybe also click on the Spoiler button to show some screenshots/gifs as well ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

CHKN

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #22 on: August 03, 2023, 02:59:48 pm »
ok, so to preface I've found success with using an image, texture and sprite, and instead of looping over every block, calculate exactly which blocks are on screen. this technique does not scale with map size, so it's perfect.

the amount of blocks on screen was 64x36, which seems reasonable to me. each block was 30x30 pixels on screen, and 1x1 in the texture.I think it was almost certainly either updating the tilemap every frame which caused the FPS issue, or not setting the grid size the the screen size, but to the map size.

Hapax

  • Hero Member
  • *****
  • Posts: 3363
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Every time I optimise my map rendering code, it gets slower.
« Reply #23 on: August 03, 2023, 10:14:45 pm »
It really should not have had an issue with that size. Like I said, I already tested with it. I should investigate this issue.

Could you please confirm the follow that you used:
The size of the tilemap (e.g. tilemap.setSize(?))
The size of the grid (e.g. tilemap.setGridSize(?))
The size of the texture tile size (e.g. tilemap.setTextureTileSize(?))
The width of the level (e.g. tilemap.setLevelWidth(?))

In addition, could you also clarify which storage you used for the level in case there's an error in the different code. (std::vector, c array...?)

If you'd like, you can also provide the texture you used and I'll test with it directly.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything