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

Author Topic: [SOLVED ]Do calculations after window.clear(), is it bad?  (Read 1651 times)

0 Members and 1 Guest are viewing this topic.

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
[SOLVED ]Do calculations after window.clear(), is it bad?
« on: February 24, 2017, 01:29:40 pm »
Hi,

so I did a small code for do a sort of tilemap, that uses an std::vector of a class that inherits of sf::Sprite. So basically, a

std::vector<sf::Sprite> tilemap(i, sf::Sprite(sprite));

But I have to draw this tilemap in the constructor, where other operations are done. So I am basically doing this:

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

    std::vector<Tile> tilemap(i, Tile(tile));

    for(int a = 0; a < tilemap.size(); a++)
    {
        tilemap[a].setPosition(a * xpos, ypos);
    }

    for(int a = 0; a < tilemap.size(); a++)
    {
        window.draw(tilemap[a]);
    }

     window.draw(hero);

     window.display();

This is my till class, that inherits of sf::Sprite.

Tile::Tile(sf::Texture texture, int x, int y, int scalex, int scaley)
{
    t = texture;
    xpos = x; ypos = y; scx = scalex; scy = scaley;

    this->setTexture(t);
    this->setPosition(xpos, ypos);
    this->setScale(scx, scy);
}

Tile::Tile()
{

}

So my question is, is it bad, to do things beetween window.clear and window.display, or that doesn't matter? Will that impact the framerate of the program?

Thanks

Ah, I have of course done a class for do the vector and all, TileArray:

#include "TileArray.h"

TileArray::TileArray(int I, int x, int y, Tile &tile, sf::RenderWindow &window)
{
    i = I;
    xpos = x; ypos = y;

    std::vector<Tile> tilemap(i, Tile(tile));

    for(int a = 0; a < tilemap.size(); a++)
    {
        tilemap[a].setPosition(a * xpos, ypos);
    }

    for(int a = 0; a < tilemap.size(); a++)
    {
        window.draw(tilemap[a]);
    }
}
« Last Edit: February 24, 2017, 02:15:27 pm by Kanoha »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Do calculations after window.clear(), is it bad?
« Reply #1 on: February 24, 2017, 01:37:08 pm »
It's not wrong to compute stuff in the middle of drawing, but it's often a sign that you have design issues. To be more specific:
- why do you create your TileArray every time, and during drawing?
- why do you draw the TileArray directly in its constructor?

This stuff looks like it should be a function rather than a class. Or, if it's more persistent, you should create as much as possible during the initialization phase, and then only draw the existing instances of Tile / TileArray.
Laurent Gomila - SFML developer

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Do calculations after window.clear(), is it bad?
« Reply #2 on: February 24, 2017, 01:41:46 pm »
Thanks a lot, sir Laurent, I'll take care of what you said.

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Do calculations after window.clear(), is it bad?
« Reply #3 on: February 24, 2017, 02:09:04 pm »
For who that are interested, here's my final code:

main.cpp:

#include <SFML/Graphics.hpp>

#include "Tile.h"

void TileArray(std::vector<Tile>& tilemap, int x, int y)
{
    for(int a = 0; a < tilemap.size(); a++)
    {
        tilemap[a].setPosition(a * x, y);
    }
}

int main()
{
    int screenWidth = 1920; int screenHeight = 1080;

    sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "Colider");

    ///Textures loading

    sf::Texture ground_001;
    ground_001.loadFromFile("Game_Textures/ground_001.png"); ///Walls

    ///Tiles / Tilemaps

    Tile tile(ground_001, 0, 0, 7, 7);

    std::vector<Tile> tilemap(28, tile);

    TileArray(tilemap, 70, 870);

    ///

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

            ///Clear / Draw / Display

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

            for(int a = 0; a < tilemap.size(); a++)
            {
                   window.draw(tilemap[a]);
            }

            window.display();
        }

    }

    return 0;
}

Tile.h:

#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

class Tile : public sf::Sprite
{
    int xpos, ypos, scx, scy;
    sf::Texture t;

    public:
        Tile(sf::Texture, int, int, int, int);

        Tile();
};

#endif // TILE_H

Tile.cpp:

#include "Tile.h"

Tile::Tile(sf::Texture texture, int x, int y, int scalex, int scaley)
{
    t = texture;
    xpos = x; ypos = y; scx = scalex; scy = scaley;

    this->setTexture(t);
    this->setPosition(xpos, ypos);
    this->setScale(scx, scy);
}

Tile::Tile()
{

}

As you can see, I removed the TileArray class, and I replaced it by a function.
« Last Edit: February 24, 2017, 02:13:55 pm by Kanoha »

 

anything