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

Author Topic: How to draw a sprite on top of another?  (Read 1605 times)

0 Members and 1 Guest are viewing this topic.

Khaos

  • Newbie
  • *
  • Posts: 7
    • View Profile
How to draw a sprite on top of another?
« on: May 23, 2012, 12:13:00 am »
    while (window.isOpen())
    {
        //Processes events
        sf::Event event;

        //Fills Event with data
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;
                default:
                break;
            }
        }
        window.clear();
        window.draw(bgs);
        window.display();
        xTurn();
    }

void xTurn()
{
    bool xturn = true;

    while(xturn)
    {
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::Vector2i mousePosition = sf::Mouse::getPosition(window);

        if((mousePosition.x < 200 && mousePosition.y < 200)
               && sf::Mouse::isButtonPressed(sf::Mouse::Left))
           {
        X.setPosition(25, 20);
        window.draw(X);
        std::cout << "hi";
        xturn = false;
           }

    }
    }

There's part of my main method and my whole xTurn code. Essentially when you left click, I want the sprite X to be printed. Also, that buttonispressed seems to activate multiple times (hi prints out 30+ times as opposed to just once).

How do I go about doing this?

Thanks.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: How to draw a sprite on top of another?
« Reply #1 on: May 23, 2012, 12:24:48 am »
You have to put the Xturn() call between your window.clear() and your window.display() call. Otherwise the draw call inside your xturn() function will never be shown in the window. Sf::mouse::isbuttonpressed is true as long as the button is pressed, so you are likely to have 30+ executions even if you only press the mouse button for a short time.

Khaos

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to draw a sprite on top of another?
« Reply #2 on: May 23, 2012, 12:29:19 am »
The thing is, its going to be tic tac toe game. I wanted to load the background (bgs) and afterwards every time someone clicks a certain area, an x or o appears. Wouldn't the bgs not display until after xturn ends?

Khaos

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to draw a sprite on top of another?
« Reply #3 on: May 23, 2012, 04:47:35 am »
Nevermind, I figured everything out.

Here's the Tic Tac Toe app: http://d.pr/f/gh1L

Credit goes to EnigmaticFellow (http://en.sfml-dev.org/forums/index.php?topic=6953.0) for supplying the Grid, X, and O images.

I used none of the code from that project, but the images supplied in that pack were too perfect to pass up.

Source Code: http://pastebin.com/Q6gAbHwV

Enjoy!
« Last Edit: May 23, 2012, 04:49:19 am by Khaos »