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

Author Topic: Problem with the draw & display methods  (Read 5816 times)

0 Members and 1 Guest are viewing this topic.

gmestanley

  • Newbie
  • *
  • Posts: 7
    • View Profile
Problem with the draw & display methods
« on: February 04, 2017, 07:04:54 pm »
I'm making a game on C++ using SFML 2.4.1 for GCC SJLJ (CodeBlocks with MinGW). When it starts, I want my game to draw the SFML logo (like a splash screen) and, after 3 seconds, draw the title screen's background. Sounds simple, but after displaying the background, after a time it displays the SFML logo again, thus making a loop rather than making the image static. Could you guys help me? This is my code:
#include <Windows.h>
#include <SFML/Graphics.hpp>

int main()
{
    // Setting up resources and the window
    sf::RenderWindow screen(sf::VideoMode(816, 624), "Elkiria");
    sf::Texture tx_sfml;
    tx_sfml.loadFromFile("sfml.png");
    sf::Sprite sp_sfml;
    sp_sfml.setTexture(tx_sfml);
    sf::Texture tx_titlebg;
    tx_titlebg.loadFromFile("skybg.png");
    sf::Sprite sp_titlebg;
    sp_titlebg.setTexture(tx_titlebg);

    // While the window is opened, do stuff.
    while (screen.isOpen())
    {
        // Create game's event and event's types
        sf::Event main_ev;
        while (screen.pollEvent(main_ev))
        {
            if (main_ev.type == sf::Event::Closed) {
                screen.close();
            }
        }

        // Operate with the window
        screen.clear();
        screen.draw(sp_sfml);
        screen.display();
        Sleep(3000);
        screen.draw(sp_titlebg);
        screen.display();
    }

    return 0;
}
 

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Problem with the draw & display methods
« Reply #1 on: February 04, 2017, 10:55:36 pm »
Don't know if this is any help. Or if it even matters. But you haven't declared anything for your sprite? Position, size, etc.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Problem with the draw & display methods
« Reply #2 on: February 05, 2017, 12:57:52 am »
you only need call clear() and display() once with all your drawing in between (from back to front).

screen.clear();
screen.draw(sp_titlebg);
screen.draw(sp_sfml);
screen.display();
 

gmestanley

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Problem with the draw & display methods
« Reply #3 on: February 05, 2017, 01:47:20 am »
you only need call clear() and display() once with all your drawing in between (from back to front).

screen.clear();
screen.draw(sp_titlebg);
screen.draw(sp_sfml);
screen.display();
 
Actually, what I want to do is display only the sfml logo and after 3 seconds, display the background, not one behind another.
Don't know if this is any help. Or if it even matters. But you haven't declared anything for your sprite? Position, size, etc.
Well you can declare a sprite without properties, but it'll be on the top of the screen.

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: Problem with the draw & display methods
« Reply #4 on: February 05, 2017, 04:52:42 am »
just use an
if
and a boolean

Code: [Select]

 bool firstTime = false;
 
 while (screen.isOpen())
 {
      blah blah blah

      // Operate with the window
      screen.clear();
      if ( firstTime )
     {
         screen.draw(sp_sfml);
         firstTime = false;
         screen.display();
         sleep(3000);
     }
     screen.draw(sp_titlebg);
     screen.display();
 }


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with the draw & display methods
« Reply #5 on: February 05, 2017, 12:38:02 pm »
I would say that it would be better to use a clock rather than blocking the window for three seconds. Something like this, maybe:
   
    sf::Clock clock;
    while (screen.isOpen())
    {
        // Create game's event and event's types
        sf::Event main_ev;
        while (screen.pollEvent(main_ev))
        {
            if (main_ev.type == sf::Event::Closed) {
                screen.close();
            }
        }

        // Operate with the window
        screen.clear();
        if (clock.getElapsedTime() < sf::seconds(3))
            screen.draw(sp_sfml);
        else
            screen.draw(sp_titlebg);
        screen.display();
    }

Remember that the whole loop should be constantly and regularly looping with every attempt at processing events as soon as they arrive/happen.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*