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

Author Topic: [Solved]sf::Mouse, clicking on a graphical button on window screen x & y  (Read 7994 times)

0 Members and 1 Guest are viewing this topic.

Yemeni Cpluspluser

  • Newbie
  • *
  • Posts: 25
  • C/C++, Soon Java and SQL ^_^
    • View Profile
    • Email
Hello, I am still creating the main game window, I have done creating the click system for new game button, but It does not feels that its a nice method, stick to this method? or there is a better method?

The .exe file with the resource :
http://www.mediafire.com/?bqaskzv951c2bbx 2.6MB

this is the target :
    if( sf::Mouse::isButtonPressed(sf::Mouse::Left)
       && sf::Mouse::getPosition(mainWindow).x >= 12
       && sf::Mouse::getPosition(mainWindow).y >=150
       && sf::Mouse::getPosition(mainWindow).x <= 270
       && sf::Mouse::getPosition(mainWindow).y <= 190
       )

        std::cout << "Clicking on new game";

This is the whole code so far :
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <random>
#include <cstdlib>

int  main()
{
    srand( time(0));
    size_t R, G, B;
    std::cout << "This is the brain of the game, don't think of closing it\nPlease wait the game is loading . . . " << std::endl;
    // declare mainWindow
    sf::RenderWindow mainWindow( sf::VideoMode( 800, 600, 32) , "Hello title" , sf::Style::Close ); // Declare of mainWindow
    mainWindow.setFramerateLimit( 60 );
    std::cout << "Game resolution is : " << mainWindow.getSize().x  << " X " << mainWindow.getSize().y
              << " bits : " << mainWindow.getSettings().depthBits + mainWindow.getSettings().stencilBits << std::endl
              << "Frame per second : 60" << std::endl;
    // declared mainWidow

    // load images into ram
    sf::Image img_SpaceShip;
    if( !img_SpaceShip.loadFromFile( "Resource/img_ship.png" ) )
       std::cout << "Error LFF : Failed to load Resource/img_ship.png" << std::endl;
    else std::cout << "Resource/img_ship.png successfully loaded" << std::endl;

    sf::Texture img_button;
    if ( !img_button.loadFromFile( "Resource/img_button.png" ) )
        std::cout << "Error LFF : Failed to load  Resource/img_button.png" << std::endl;
    else std::cout << "Resource/img_button.png successfully loaded" << std::endl;

    // load sound and music into ram
    sf::Music msnd_intro;
    if( !msnd_intro.openFromFile( "Resource/msnd_intro.ogg") )
        std::cout << "Error OFF : Failed to load Resource/msnd_intro.ogg" << std::endl;
    else std::cout << "Resource/msnd_intro.ogg successfully loaded" << std::endl;
    msnd_intro.setLoop( true );
    msnd_intro.play();

    // loads fonts
    sf::Font font_English;
    if (!font_English.loadFromFile( "Resource/font_English.ttf" ) )
        std::cout << "Error LFF : Failed to load Resource/font_English.ttf" << std::endl;
    else std::cout << "Resource/font_English.ttf successfully loaded" << std::endl;

    // every thing should be loaded

    // declarations

    sf::Text text_MainWindowText ( "SpaceShips 0.01 Alfa", font_English);
    text_MainWindowText.setCharacterSize( 40 );
    text_MainWindowText.setColor( sf::Color::Blue );
    text_MainWindowText.setPosition( 200 , 30 );

    sf::Text text_MainWindowNewGame ( "New Game", font_English);
    text_MainWindowNewGame.setCharacterSize( 30 );
    text_MainWindowNewGame.setColor( sf::Color::Green );
    text_MainWindowNewGame.setPosition( 60 , 150 );

    sf::Text text_MainWindowExit ( "Exit Game", font_English );
    text_MainWindowExit.setCharacterSize( 30 );
    text_MainWindowExit.setColor( sf::Color::Red );
    text_MainWindowExit.setPosition( 60, 200 );

    sf::Sprite spr_ButtonNewGame;
    spr_ButtonNewGame.setTexture( img_button );
    spr_ButtonNewGame.setPosition( 0 , 150 );
    spr_ButtonNewGame.setScale( 1.8 , 1 );

    sf::Sprite spr_ButtonExitGame;
    spr_ButtonExitGame.setTexture( img_button );
    spr_ButtonExitGame.setPosition( 0 , 200 );
    spr_ButtonExitGame.setScale( 1.8 , 1 );

    sf::Sprite spr_SpaceShip;



    // the main game loop
    while ( mainWindow.isOpen() ) // main game loop
    {
        sf::Event mainEvents; // declare of main events checker
        while ( mainWindow.pollEvent( mainEvents ) ); // checking
        {
            switch ( mainEvents.type ) // what event type
            {
            case sf::Event::EventType::Closed: // users clicks X or press Alt-F4
                mainWindow.close(); // close the window
                break;
            }
        }
    // main loop :
    mainWindow.clear( sf::Color( 11,11,11) );
    mainWindow.draw( spr_ButtonNewGame );
    mainWindow.draw( spr_ButtonExitGame );
    mainWindow.draw( text_MainWindowText );

    if( sf::Mouse::isButtonPressed(sf::Mouse::Left)
       && sf::Mouse::getPosition(mainWindow).x >= 12
       && sf::Mouse::getPosition(mainWindow).y >=150
       && sf::Mouse::getPosition(mainWindow).x <= 270
       && sf::Mouse::getPosition(mainWindow).y <= 190
       )

        std::cout << "Clicking on new game";

    R = rand() % 255;
    G = rand() % 255;
    B = rand() % 255;

    text_MainWindowText.setColor( sf::Color ( R, G, B ) );
    mainWindow.draw( text_MainWindowNewGame );
    mainWindow.draw( text_MainWindowExit );
    mainWindow.display();
    //std::cout << sf::Mouse::getPosition(mainWindow).x << " " << sf::Mouse::getPosition(mainWindow).y << std::endl;

    }
    return EXIT_SUCCESS; // returns 0;
}
 
« Last Edit: August 01, 2013, 07:54:19 pm by Yemeni Cpluspluser »
sf::signature mySignature;
mySignature.setPosition(forum.x,forum.y);
window.draw(mySignature);

Yemeni Cpluspluser

  • Newbie
  • *
  • Posts: 25
  • C/C++, Soon Java and SQL ^_^
    • View Profile
    • Email
Re: sf::Mouse, clicking on a graphical button on window screen x & y
« Reply #1 on: August 01, 2013, 06:40:14 pm »
I got an idea, what if I use a by-pixel checking instead of x and y coordinates .
sf::signature mySignature;
mySignature.setPosition(forum.x,forum.y);
window.draw(mySignature);

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: sf::Mouse, clicking on a graphical button on window screen x & y
« Reply #2 on: August 01, 2013, 06:50:52 pm »
if (sprite.contains(sf::Mouse::getPosition(mainWindow).x, sf::Mouse::getPosition(mainWindow).y))
{
     std::cout << "Hovered over button!" << std::endl;

     if (sf::Mouse::isButtonPressed(sf::Mouse::Left)
     {
            std::cout << "Button pressed" << std::endl;
     }
}
« Last Edit: August 01, 2013, 06:52:26 pm by The Terminator »
Current Projects:
Technoport

Yemeni Cpluspluser

  • Newbie
  • *
  • Posts: 25
  • C/C++, Soon Java and SQL ^_^
    • View Profile
    • Email
Re: sf::Mouse, clicking on a graphical button on window screen x & y
« Reply #3 on: August 01, 2013, 07:53:57 pm »
if (sprite.contains(sf::Mouse::getPosition(mainWindow).x, sf::Mouse::getPosition(mainWindow).y))
{
     std::cout << "Hovered over button!" << std::endl;

     if (sf::Mouse::isButtonPressed(sf::Mouse::Left)
     {
            std::cout << "Button pressed" << std::endl;
     }
}

 :) Thank you for finding it.
sf::signature mySignature;
mySignature.setPosition(forum.x,forum.y);
window.draw(mySignature);