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

Author Topic: Is this Efficient (sf::RectangleShape vs tilemaps) ??  (Read 1141 times)

0 Members and 1 Guest are viewing this topic.

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Is this Efficient (sf::RectangleShape vs tilemaps) ??
« 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;
}

 

G.

  • Hero Member
  • *****
  • Posts: 1554
    • View Profile
Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
« Reply #1 on: July 30, 2019, 09:44:25 pm »
What you did is pretty much what a tilemap is, so... not sure what you mean... ?

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
« Reply #2 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??

Paul

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
« Reply #3 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10191
    • View Profile
    • development blog
    • Email
Re: Is this Efficient (sf::RectangleShape vs tilemaps) ??
« Reply #4 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/