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

Author Topic: Drawing tilemap is lowering my fps to a crawl  (Read 3510 times)

0 Members and 1 Guest are viewing this topic.

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Drawing tilemap is lowering my fps to a crawl
« on: April 03, 2011, 04:41:27 pm »
Okay, I'm trying to get my fps to 60, but right now it's at around 20. What can I do to this code to speed it up? Note: this is c++ using sfml.

Code: [Select]
       App.Clear();
        for(int x = 0; x < lv.width; x++){
            for(int y = 0; y < lv.height; y++){
 
                int tileXCoord = 0;
                int tileYCoord = 0;
                int tileSheetWidth = tilemapImage.GetWidth() / lv.tileSize;
 
                if (lv.tile[x][y] != 0)
                {
                    tileXCoord = lv.tile[x][y] % tileSheetWidth;
                    tileYCoord = lv.tile[x][y] / tileSheetWidth;
                }
 
                tilemap.SetSubRect(sf::IntRect(tileXCoord * lv.tileSize, tileYCoord * lv.tileSize, (tileXCoord * lv.tileSize) + lv.tileSize, (tileYCoord * lv.tileSize) + lv.tileSize));
                tilemap.SetPosition(x * lv.tileSize, y * lv.tileSize);
                App.Draw(tilemap);
            }
        }
 
        playerSprite.SetSubRect(sf::IntRect(player.width * player.frame, player.height * player.state,
                                       (player.width * player.frame) + player.width, (player.height * player.state) + player.height));
        playerSprite.SetPosition(player.x, player.y);
 
        App.Draw(playerSprite);
 
        if(player.walking){
            if(player.frameDelay >= 0)
                player.frameDelay--;
 
            if(player.frameDelay <= 0){
 
                player.frame++;
                player.frameDelay = 10;
 
                if(player.frame >= 4)
                    player.frame = 0;
            }
        }
 
 
        for(int x = 0; x < lv.width; x++){
            for(int y = 0; y < lv.height; y++){
 
                int tileXCoord = 0;
                int tileYCoord = 0;
                int tileSheetWidth = tilemapImage.GetWidth() / lv.tileSize;
 
                if (lv.ftile[x][y] != 0)
                {
                    tileXCoord = lv.ftile[x][y] % tileSheetWidth;
                    tileYCoord = lv.ftile[x][y] / tileSheetWidth;
                }
 
                tilemap.SetSubRect(sf::IntRect(tileXCoord * lv.tileSize, tileYCoord * lv.tileSize, (tileXCoord * lv.tileSize) + lv.tileSize, (tileYCoord * lv.tileSize) + lv.tileSize));
                tilemap.SetPosition(x * lv.tileSize, y * lv.tileSize);
                App.Draw(tilemap);
            }
        }
 
 
        App.Display();

Elffus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Drawing tilemap is lowering my fps to a crawl
« Reply #1 on: April 03, 2011, 05:09:00 pm »
If tiles aren't being constantly changed wouldn't it be easier to store them in an sf::Image and only perform changes when necessary? Showing one big image is much faster than loads of smaller ones I believe.

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Drawing tilemap is lowering my fps to a crawl
« Reply #2 on: April 03, 2011, 05:10:54 pm »
Quote from: "Elffus"
If tiles aren't being constantly changed wouldn't it be easier to store them in an sf::Image and only perform changes when necessary? Showing one big image is much faster than loads of smaller ones I believe.
How would I go about doing this?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Drawing tilemap is lowering my fps to a crawl
« Reply #4 on: April 03, 2011, 05:53:18 pm »
That worked! Thank you very much!!!!

Gibgezr

  • Newbie
  • *
  • Posts: 33
    • View Profile
Drawing tilemap is lowering my fps to a crawl
« Reply #5 on: April 05, 2011, 08:52:34 pm »
The thread below has example code that demonstrates one way of doing it:
http://www.sfml-dev.org/forum/viewtopic.php?t=4376

drakelord

  • Newbie
  • *
  • Posts: 22
    • View Profile
Drawing tilemap is lowering my fps to a crawl
« Reply #6 on: April 14, 2011, 07:46:13 pm »
Just out of curiosity, what makes the engine run faster displaying one image versus a bunch of smaller ones?  

One reason I can think of in my head is only having to perform all of the extra manipulations such as scaling and what not once, but other than that, I don't know.  I don't have much OpenGL experience to know what goes on behind the scenes.

 

anything