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

Author Topic: Suggestions to prevent lag? (Beginner)  (Read 1381 times)

0 Members and 1 Guest are viewing this topic.

bobtran12

  • Guest
Suggestions to prevent lag? (Beginner)
« on: December 19, 2014, 11:10:06 am »
I'm a beginner at this.

I was trying out a function where it would draw a certain section of an image depending on the parameters, but if I use the function, the entire program becomes very laggy. The code is probably poorly "designed".

This is the relevant code:
void drawSolidObject(sf::Texture& buildingsTexture, int objectType, int objectNumber, float objectX, float objectY)
{
        if(objectType == Building)
        {
                if(objectNumber == 0)
                {
                        if(!buildingsTexture.loadFromFile("images/map/buildings.png"))
                                std::cout << "Error: Game failed to load 'buildings' image." << std::endl;
                        buildings.setTexture(buildingsTexture);
                        buildings.setTextureRect(sf::IntRect(0, 0, 140, 122));
                        buildings.setPosition(objectX, objectY);
                        float left = buildings.getGlobalBounds().left - 16;
                        float right = left + buildings.getGlobalBounds().width + 8;
                        float top = buildings.getGlobalBounds().top - 24;
                        float bottom = top + buildings.getGlobalBounds().height - 8;
                        sf::Vector2f newSolidObjectLF(left, right);
                        sf::Vector2f newSolidObjectTB(top, bottom);
                        if(player.getPosition().x > newSolidObjectLF.x && player.getPosition().x < newSolidObjectLF.y && player.getPosition().y > newSolidObjectTB.x && player.getPosition().y < newSolidObjectTB.y)
                        {
                                player.setPosition(prevPlayerPos.x, prevPlayerPos.y);
                        }
                }
        }
}
 

while(Window.isOpen())
{
        drawSolidObject(House3, Building, 0, 800, 800);
}
 

Any suggestions to prevent the lag and use the same concept?

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Suggestions to prevent lag? (Beginner)
« Reply #1 on: December 19, 2014, 11:11:58 am »
Don't uselessly load the same texture over and over. Load it once.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Suggestions to prevent lag? (Beginner)
« Reply #2 on: December 19, 2014, 02:14:10 pm »
To clarify a tiny little bit more, every time you are trying to prepare this object for drawing, you are accessing and loading the texture image file. Texture loading is often one of the most time-consuming parts of any game. Best to - as G already said - load it just once, at the beginning, before the loop.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

bobtran12

  • Guest
Re: Suggestions to prevent lag? (Beginner)
« Reply #3 on: December 19, 2014, 09:07:30 pm »
Thanks. Doing that removed the lag.

However, if I do this:
while(Window.isOpen())
{
    drawSolidObject(House3, Building, 0, 800, 800);
    drawSolidObject(House4, Building, 0, 1100, 800);
}

How do I make it so that it would show two images (House3 and House4) instead of just moving the first image (House3) to another position (1100,800)?

Because with my current code, it just moves the position of the first drawSolidObject to (1100, 800).

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Suggestions to prevent lag? (Beginner)
« Reply #4 on: December 19, 2014, 09:57:30 pm »
You have two options:
  • window.draw() before drawing the second,
  • create two sprites and modify them separately, then draw them together (much better)
It looks like you create a sprite called buildings. I'd suggest considering creating a vector of sprites called buildings, thusly:
std::vector<sf::Sprite> buildings;
For more information on vectors, see here.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*