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

Author Topic: Framerate drops to 11fps when I draw a tilemap  (Read 2445 times)

0 Members and 1 Guest are viewing this topic.

fundo

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Framerate drops to 11fps when I draw a tilemap
« on: November 11, 2013, 02:35:46 pm »
Hi

I went to the tutorials section of SFML 2.1 and in the "Designing your own entities with vertex arrays" tutorial they demonstrate how to create and draw a tilemap. So I implemented it exactly as it is. Changed nothing. But when I draw the framerate drops to 11fps. Has anyone else ecountered a problem where the framerate droped? I set the screen to 800x600 and the bigger the screen the worse the framerate drop. If you experienced this, how did you resolve it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11028
    • View Profile
    • development blog
    • Email
Re: Framerate drops to 11fps when I draw a tilemap
« Reply #1 on: November 11, 2013, 02:53:48 pm »
Quote from: General Rules
Give details

Provide as much relevant information as possible to people who will help you -- it saves time and posts.
  • What's your OS? Graphics card?
  • Which version of SFML are you using? Static or dynamic? If you're using a development snapshot, which revision is it?
  • ...

Also: Is your graphics driver uptodate? Is it a constant frame drop or random? :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

fundo

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Framerate drops to 11fps when I draw a tilemap
« Reply #2 on: November 11, 2013, 03:01:17 pm »
I'm using Widows 7
My graphics card is Mobile Intel (R) Express Chipset Family
I'm using SFML 2.1 the latest stable version
I'm using the dynamic library
My RAM is 2 gig

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Framerate drops to 11fps when I draw a tilemap
« Reply #3 on: November 11, 2013, 03:02:03 pm »
How are you measuring your frame rate? And could you show your exact code please  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

fundo

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Framerate drops to 11fps when I draw a tilemap
« Reply #4 on: November 11, 2013, 03:41:29 pm »
here is the complete code


/* includes ommitted */

class TileMap : public sf::Drawable, public sf::Transformable
{
        public:
                bool Load(const std::string&, sf::Vector2u, const int* tiles, unsigned int, unsigned int);

        private:
                virtual void draw(sf::RenderTarget&, sf::RenderStates) const;

        private:
                sf::VertexArray m_vertices;
                sf::Texture m_tileset;
};

bool TileMap::Load(const std::string& filename, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
        // Load the tileset texture

        if (!m_tileset.loadFromFile(filename))
                return false;

        // resize vertex array to fit the level size

        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);

        // populate the vertex array with one quad per tile

        for(unsigned int i = 0; i < width; ++i)
        {
                for (unsigned int j = 0; j < height; ++j)
                {
                        // get the current tile number

                        int tileNumber = tiles[i + j * width];

                        // find it's position in the tileset texture

                        int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                        int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                        // get a pointer to the current tile's quad

                        sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                        // define the 4 corners

                        quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                        quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                        quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                        quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                        // define the texture coordinates

                        quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                        quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                        quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                        quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
                }
        }

        return true;
}

void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
        // apply the tranform

        states.transform *= getTransform();

        // apply the tileset texture

        states.texture = &m_tileset;

        // draw the vertex array;

        target.draw(m_vertices, states);
}

template <typename T>
std::string ToString(T val)
{
        std::stringstream str;
        str << val;

        return str.str();
}

int main()
{
        sf::Font font;
        if (!font.loadFromFile("Showcard_Gothic.ttf"))
                return -1;

        sf::Text text;
        text.setFont(font);

        sf::Clock clock;

        // create the window

        sf::RenderWindow window(sf::VideoMode(800, 600), "TileMap");

        // define the level with an array of indexes

        const int level[] =
        {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 3, 2, 1, 2, 3, 3, 2, 1, 1, 2, 3, 2, 1, 2, 2, 3, 2, 1, 1, 1, 3, 2, 1, 2, 3, 3, 2, 1, 1, 2, 3, 2, 1, 2, 2, 3, 2, 1, 1,
        };

        // create the tilemap from the level definition

        TileMap map;

        if (!map.Load("tileset.png", sf::Vector2u(40, 40), level, 40, 30))
                return -1;

        //run the main loop

        while (window.isOpen())
        {
                // handle events

                sf::Event event;

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

                float elapsedTime = clock.restart().asSeconds();
                sf::String str(ToString(elapsedTime));
                text.setString(str);

                // draw the map

                window.clear();
                window.draw(map);
                window.draw(text);
                window.display();
        }

        return 0;
}

 

I'm drawing the map bigger than the screen because i wanted to include scrolling later but even after i shorten the map i get the same prblem

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Framerate drops to 11fps when I draw a tilemap
« Reply #5 on: November 11, 2013, 03:54:48 pm »
Are you sure the result your getting is not ".011xxx"? Because at the moment you are not really calculating the FPS. To get the FPS try something like this.

sf::String str(ToString(1f / elapsedTime));
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

fundo

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Framerate drops to 11fps when I draw a tilemap
« Reply #6 on: November 11, 2013, 04:58:42 pm »
That is exactly what I'm getting and when I  do 1/elapsedTime I'm getting 11fps like I said earlier... Why is the framerate dropping so much?