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

Author Topic: Any sugestions to optimize this code?  (Read 2402 times)

0 Members and 1 Guest are viewing this topic.

reethok

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Any sugestions to optimize this code?
« on: March 27, 2013, 05:20:48 am »
Well... it uses ~70-80% of my CPU (I'm using a netbook) when the music start to go "hard"

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>

int main()
{
    // Ventana en la que se muestra todo el contenido del juego.
    sf::RenderWindow gameWindow(sf::VideoMode(1024, 600, 32), "Project Genesis",
                                sf::Style::Fullscreen);

    // Se establece el límite de frames por segundo a 60
    gameWindow.setFramerateLimit(60);

    enum Boton{NUEVO_JUEGO, CONTINUAR, OPCIONES, SALIR};
    Boton boton = NUEVO_JUEGO;

    // Música
    sf::SoundBuffer buffer;
    buffer.loadFromFile("main.wav");

    sf::SoundBuffer button;
    button.loadFromFile("button.wav");

    sf::SoundBuffer selection;
    selection.loadFromFile("selection.wav");

    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.setLoop(true);

    sf::Sound buttons;
    buttons.setBuffer(button);

    sf::Sound selections;
    selections.setBuffer(selection);

    sf::Texture menu_i;
    if(!menu_i.loadFromFile("menu.png"))
        return EXIT_FAILURE;
    sf::Sprite menu_s;
    menu_s.setTexture(menu_i);

    sf::Texture nuevo_juego_i;
    if(!nuevo_juego_i.loadFromFile("nuevo_juego.png"))
        return EXIT_FAILURE;
    sf::Sprite nuevo_juego_s;
    nuevo_juego_s.setTexture(nuevo_juego_i);
    nuevo_juego_s.setPosition(700, 225);

    sf::Texture nuevo_juego2_i;
    if(!nuevo_juego2_i.loadFromFile("nuevo_juego2.png"))
        return EXIT_FAILURE;
    sf::Sprite nuevo_juego2_s;
    nuevo_juego2_s.setTexture(nuevo_juego2_i);
    nuevo_juego2_s.setPosition(700, 225);

    sf::Texture continuar_i;
    if(!continuar_i.loadFromFile("continuar.png"))
        return EXIT_FAILURE;
    sf::Sprite continuar_s;
    continuar_s.setTexture(continuar_i);
    continuar_s.setPosition(700, 275);

    sf::Texture continuar2_i;
    if(!continuar2_i.loadFromFile("continuar2.png"))
        return EXIT_FAILURE;
    sf::Sprite continuar2_s;
    continuar2_s.setTexture(continuar2_i);
    continuar2_s.setPosition(700, 275);

    sf::Texture opciones_i;
    if(!opciones_i.loadFromFile("opciones.png"))
        return EXIT_FAILURE;
    sf::Sprite opciones_s;
    opciones_s.setTexture(opciones_i);
    opciones_s.setPosition(700, 325);

    sf::Texture opciones2_i;
    if(!opciones2_i.loadFromFile("opciones2.png"))
        return EXIT_FAILURE;
    sf::Sprite opciones2_s;
    opciones2_s.setTexture(opciones2_i);
    opciones2_s.setPosition(700, 325);

    sf::Texture salir_i;
    if(!salir_i.loadFromFile("salir.png"))
        return EXIT_FAILURE;
    sf::Sprite salir_s;
    salir_s.setTexture(salir_i);
    salir_s.setPosition(700, 375);

    sf::Texture salir2_i;
    if(!salir2_i.loadFromFile("salir2.png"))
        return EXIT_FAILURE;
    sf::Sprite salir2_s;
    salir2_s.setTexture(salir2_i);
    salir2_s.setPosition(700, 375);

    gameWindow.draw(menu_s);
    gameWindow.draw(nuevo_juego2_s);
    gameWindow.draw(continuar_s);
    gameWindow.draw(opciones_s);
    gameWindow.draw(salir_s);
    gameWindow.display();

    sf::Image captura;
    captura = gameWindow.capture();
    captura.saveToFile("captura.png");
   
    sound.play();

    // Mientras la ventana esté abierta
    while(gameWindow.isOpen())
    {
        // Ciclo para procesar eventos

        // Variable para almacenar los eventos
        sf::Event Event;

        while(gameWindow.pollEvent(Event))
        {
            switch (Event.type)
            {
                case sf::Event::Closed:
                    gameWindow.close();
                    break;
                case sf::Event::KeyPressed:
                    if(Event.key.code == sf::Keyboard::Escape)
                        gameWindow.close();
                    if(Event.key.code == sf::Keyboard::Up)
                    {
                        buttons.play();
                        switch(boton)
                        {
                            case NUEVO_JUEGO:
                                boton = SALIR;
                                break;
                            case CONTINUAR:
                                boton = NUEVO_JUEGO;
                                break;
                            case OPCIONES:
                                boton = CONTINUAR;
                                break;
                            case SALIR:
                                boton = OPCIONES;
                                break;
                        }
                    }
                    if(Event.key.code == sf::Keyboard::Down)
                    {
                        buttons.play();
                        switch(boton)
                        {
                            case NUEVO_JUEGO:
                                boton = CONTINUAR;
                                break;
                            case CONTINUAR:
                                boton = OPCIONES;
                                break;
                            case OPCIONES:
                                boton = SALIR;
                                break;
                            case SALIR:
                                boton = NUEVO_JUEGO;
                                break;
                        }
                    }
                    if(Event.key.code == sf::Keyboard::Return)
                    {
                        switch(boton)
                        {
                            case SALIR:
                                selections.play();
                                while(selections.getStatus() == sf::SoundSource::Status::Playing)
                                    sf::sleep(sf::milliseconds(1));
                                gameWindow.close();
                                break;
                        }
                    }
                default:
                    break;
            }

            // Limpia la pantalla
            gameWindow.clear();

            switch(boton)
            {
                case NUEVO_JUEGO:
                    gameWindow.draw(menu_s);
                    gameWindow.draw(nuevo_juego2_s);
                    gameWindow.draw(continuar_s);
                    gameWindow.draw(opciones_s);
                    gameWindow.draw(salir_s);
                    break;
                case CONTINUAR:
                    gameWindow.draw(menu_s);
                    gameWindow.draw(nuevo_juego_s);
                    gameWindow.draw(continuar2_s);
                    gameWindow.draw(opciones_s);
                    gameWindow.draw(salir_s);
                    break;
                case OPCIONES:
                    gameWindow.draw(menu_s);
                    gameWindow.draw(nuevo_juego_s);
                    gameWindow.draw(continuar_s);
                    gameWindow.draw(opciones2_s);
                    gameWindow.draw(salir_s);
                    break;
                case SALIR:
                    gameWindow.draw(menu_s);
                    gameWindow.draw(nuevo_juego_s);
                    gameWindow.draw(continuar_s);
                    gameWindow.draw(opciones_s);
                    gameWindow.draw(salir2_s);
                    break;
            }

            // Redibuja la oantalla
            gameWindow.display();
        }
    }
}
 

I'll thank you if you can help me with ideas to optimize.

Greetings, and sorry for my bad english.
« Last Edit: March 27, 2013, 06:53:12 am by reethok »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Any sugestions to optimize this code?
« Reply #1 on: March 27, 2013, 08:28:16 am »
Quote
Starts to go "hard"
What does that mean? ???

There's not much to optimize, your netbook just might not be the best. What's your PC spec?

The only thing I see, is that you can replace the if's in the event loop with else if, but that won't really make any difference.

Is the CPU also at 70% if you don't move your mouse?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

reethok

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Any sugestions to optimize this code?
« Reply #2 on: March 27, 2013, 10:25:05 am »
First, music goes "slow" and the CPU consumption es 3-4%, and when the music start to go faster, it rises to 70-80%.

By the way, I've used the -O3 optimization flag and it seems to be usefull, now it maintains 3-4% of CPU consumption.

I tryed with -O2 and -O1, but it dont seems to have any effect, I wonder why (I know -O3 is better, but -O1 and -O2 are the same if i dont use any optimization flag).

Thanks for answering.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Any sugestions to optimize this code?
« Reply #3 on: March 27, 2013, 11:11:11 am »
First, music goes "slow" and the CPU consumption es 3-4%, and when the music start to go faster, it rises to 70-80%.

By the way, I've used the -O3 optimization flag and it seems to be usefull, now it maintains 3-4% of CPU consumption.
Sounds quite strange...

Well I guess it could be a combination of hardware, driver and bad optimization by the compiler. Are your drivers uptodate?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

reethok

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Any sugestions to optimize this code?
« Reply #4 on: March 27, 2013, 12:37:06 pm »
Well, I'm using an Intel Atom netbook with Linux, haha, they're updated but i seriously doubt they work properly... I'll make some test in a Windows machine just to look how the program behaves.

santiaboy

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Any sugestions to optimize this code?
« Reply #5 on: March 29, 2013, 01:19:47 am »
Do that and post results!

Also, try and use some functions, and even some classes. That was really hard to read.
Another tip: don't use names that are too similar, you had for example nuevo_juego2_i, nuevo_juego_i, nuevo_juego_s, nuevo_juego2_s; it was hard to keep track of it.

I know this isn't crucial, but one part of programming is making readable code. You (or anyone else for that matter) have to be able to pick up a certain piece of code and be able to read it, in the same way you read an article.

Moreover, look into state machines as it will also help in that regard. I read an article from lazy foo's tutorials in order to understand what a state machine does, why it is important, and he even provides an implementation (http://lazyfoo.net/articles/article06/index.php)