SFML community forums

Help => General => Topic started by: C_Worm on July 30, 2019, 09:40:20 pm

Title: Is this Efficient (sf::RectangleShape vs tilemaps) ??
Post by: C_Worm on July 30, 2019, 09:40:20 pm
hey im wondering if this is efficient for say, writing a game like "breakOut"?

Or should one strive for using tilemaps instead of allocating many sf::RectangleShapes?

Say i would want to add some texture to the rectShapes, would this be really inefficient in the long run?

#define SFML_STATIC
#define WIN32_LEAN_AND_MEAN

#include "SFML\Graphics.hpp"

float vel = 0.02f;

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        window.setFramerateLimit(60);  

        sf::RectangleShape tiles[10][10];
        for(int i = 0; i < 10; i++)
        {
                        for(int j = 0; j < 10; j++)
                        {
                                tiles[i][j].setPosition(sf::Vector2f((i * 10), ((j * 10) + 20)));
                                tiles[i][j].setSize(sf::Vector2f(5, 5));
                                tiles[i][j].setFillColor(sf::Color(20*i, 40, 140, 255));
                        }
        }

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


        window.clear();


                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(1.0f, 0.0f));
                                        }
                                }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(-1.0f, 0.0f));
                                        }
                                }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(0.0f, -1.0f));
                                        }
                                }
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                                for(int i = 0; i < 10; i++)
                                {
                                        for(int j = 0; j < 10; j++)
                                        {
                                                        tiles[i][j].move(sf::Vector2f(0.0f, 1.0f));
                                        }
                                }
                }

                for(int i = 0; i < 10; i++)
                {
                        for(int j = 0; j < 10; j++)
                        {

                                window.draw(tiles[i][j]);
                        }
                }


        window.display();
    }

    return 0;
}

 
Title: Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
Post by: G. on July 30, 2019, 09:44:25 pm
What you did is pretty much what a tilemap is, so... not sure what you mean... ?
Title: Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
Post by: C_Worm on July 30, 2019, 10:01:45 pm
oh, okay!

but this example here:
https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php

seems much more complicated than what i did, are there any differencies??
Title: Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
Post by: Paul on July 31, 2019, 09:51:38 am
Rectangleshape = 1x vertexarray = 1x draw call. If you draw whole 10x10 tilemap then you have 100 vertexarrays = 100 draw calls.

If you use one vertexarray for whole tilemap it's only 1 draw call => better performance.
Title: Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
Post by: eXpl0it3r on August 02, 2019, 09:49:20 am
While vertex arrays or buffers are certainly the way to go for tilemaps, at the scale of a breakout clone, it doesn't really matter. ;)