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

Author Topic: CPU Performance, 2D Tile Based game  (Read 2596 times)

0 Members and 1 Guest are viewing this topic.

Carlo Meroni

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
CPU Performance, 2D Tile Based game
« on: October 05, 2013, 01:44:24 pm »
Hi! I've recently started using SFML and I really like it a lot. I am translating my game from XNA to SFML and I noticed that the performance of SFML are lower in image rendering. I created a simple class for spritebatching (with vertex array) and I compared the performance with XNA.
10000 drawing sprites with the same texture:
- XNA: 60 FPS, about 4% CPU usage
- SFML: 60 FPS, about 11% CPU usage

is a problem of my code? can be fixed?
sorry for my bad english!

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: CPU Performance, 2D Tile Based game
« Reply #1 on: October 05, 2013, 03:34:55 pm »
well, i really don't know. can you show the code?
i have no idea how much a single image affect the CPU, since SFML is mainly based on OpenGL...
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Carlo Meroni

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: CPU Performance, 2D Tile Based game
« Reply #2 on: October 05, 2013, 04:44:44 pm »
this is the code:
//SpriteBatch.h
#include<SFML\Graphics.hpp>

class SpriteBatch
{
public:
        SpriteBatch(void);
        ~SpriteBatch(void);

        void Begin(int Maxvertex);
        void Draw(sf::RenderWindow &window,sf::Texture &Texture,sf::Rect<int> &transform,sf::Rect<int> &Source,sf::Color &colora,sf::Color &colorb,sf::Color &colorc,sf::Color &colord,int deep);
        virtual void End(sf::RenderWindow &window,sf::Texture &Texture);

        sf::VertexArray vertexs;
        sf::Texture texture;
        int maxvertex;
        int cvertex;
};

//SpriteBatch.cpp
void SpriteBatch::Begin(int Max)
{
        //Vertex Array
        cvertex=0;
        maxvertex=Max;
        vertexs.setPrimitiveType(sf::Quads);
        vertexs.resize(maxvertex);
}

void SpriteBatch::Draw(sf::RenderWindow &window,sf::Texture &tx, sf::Rect<int> &transform,sf::Rect<int> &Source,sf::Color &colora,sf::Color &colorb,sf::Color &colorc,sf::Color &colord,int deep)
{

        //Position - Dimension
        vertexs[cvertex].position = sf::Vector2f(transform.left, transform.top);
        vertexs[cvertex+1].position = sf::Vector2f(transform.left+transform.width, transform.top);
        vertexs[cvertex+2].position = sf::Vector2f(transform.left+transform.width, transform.top+transform.height);
        vertexs[cvertex+3].position = sf::Vector2f(transform.left,transform.top+transform.height);

        //Texture
        vertexs[cvertex].texCoords = sf::Vector2f(Source.left,Source.top);
        vertexs[cvertex+1].texCoords = sf::Vector2f(Source.left+Source.width, Source.top);
        vertexs[cvertex+2].texCoords = sf::Vector2f(Source.left+Source.width,Source.top+Source.height);
        vertexs[cvertex+3].texCoords = sf::Vector2f(Source.left, Source.top+Source.height);

        //Color
        vertexs[cvertex].color = colora;
        vertexs[cvertex+1].color = colorb;
        vertexs[cvertex+2].color = colorc;
        vertexs[cvertex+3].color = colord;

        cvertex+=4;
        if (cvertex==maxvertex)
        {
                End(window,tx);
                cvertex=0;
        }

}

void SpriteBatch::End(sf::RenderWindow &window,sf::Texture &tx)
{
        window.draw(vertexs,&tx);
}
 
I set Maxvertex = 1024
« Last Edit: October 05, 2013, 04:49:40 pm by Carlo Meroni »

Carlo Meroni

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: CPU Performance, 2D Tile Based game
« Reply #3 on: October 06, 2013, 02:32:32 pm »
Now it works fine. My code was wrong. :)