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

Author Topic: Main Menu selection  (Read 3971 times)

0 Members and 1 Guest are viewing this topic.

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Main Menu selection
« on: December 28, 2011, 09:36:00 pm »
Hey again, would anyone know how to show a sprite when the mouse is over a certain piece of the screen, like text for example. After that I'll then do a click function to then launch different parts of the game.

Here is my code at the moment, and I have been trying to get it so it automatically comes up with the selection sprite one the first option and then it will be moved by pressing up and down.

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(1024, 600), "Before the rain");

    // Load a font from a file
    sf::Font MyFont;
    if (!MyFont.LoadFromFile("cinnamon_cake/cinnamon cake.ttf", 50))
        return EXIT_FAILURE;

    // Load the sprite image from a file
    sf::Image selection;
    if (!selection.LoadFromFile("selection.tga"))
        return EXIT_FAILURE;

    // Create the sprite
    sf::Sprite Sprite(selection);

    // Change its properties
    // Sprite.SetColor(sf::Color(0, 255, 255, 128));
    // Sprite.SetPosition(200.f, 100.f);
    Sprite.SetScale(2.f, 2.f);

    // Create a graphical string
    sf::String start;
    start.SetText("Start Game");
    start.SetFont(MyFont);
    start.SetColor(sf::Color(0, 128, 128));
    start.SetPosition(50.f, 300.f);
    start.SetSize(28.f);

    sf::String options;
    options.SetText("Options");
    options.SetFont(MyFont);
    options.SetColor(sf::Color(0, 128, 128));
    options.SetPosition(50.f, 350.f);
    options.SetSize(28.f);

    sf::String credits;
    credits.SetText("Credits");
    credits.SetFont(MyFont);
    credits.SetColor(sf::Color(0, 128, 128));
    credits.SetPosition(50.f, 400.f);
    credits.SetSize(28.f);

    sf::String exit;
    exit.SetText("Exit");
    exit.SetFont(MyFont);
    exit.SetColor(sf::Color(0, 128, 128));
    exit.SetPosition(50.f, 450.f);
    exit.SetSize(28.f);

    sf::String title;
    title.SetText("Before The Rain");
    title.SetFont(MyFont);
    title.SetColor(sf::Color(0, 128, 128));
    title.SetPosition(75.f, 100.f);
    title.SetRotation(5.f);
    title.SetSize(75.f);



    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Make the second string rotate
        // Bonjour.Rotate(App.GetFrameTime() * 100.f);

        // Clear screen
        App.Clear();

        // Draw our strings
        App.Draw(title);
        App.Draw(start);
        App.Draw(options);
        App.Draw(credits);
        App.Draw(exit);

    }

    return EXIT_SUCCESS;
}


Either way would REALLY help me out, if someone could supply any source code to do this. Sorry about all the questions, but I'm new and there aren't many tutorials out there for what I need :(

Many thanks, i'll be back on tomorrow!

aBallofWin :)[/code]

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Main Menu selection
« Reply #1 on: December 29, 2011, 01:30:18 am »
Check the mouse position against each sprite if it is inside its boundaries. X must be bigger or equals to the left boundary and smaller or equals to the right boundary, and so on. Same to Y.

You could also make some Menu main class with some common methods, and let the specific function of each button for the derivated classes to implement. Then you make an array of objects and call the common methods for all of them.

aBallofWin

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Main Menu selection
« Reply #2 on: December 29, 2011, 03:16:06 pm »
I figured it would have something to do with mouse position, but I don't really know how to code that :s

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Main Menu selection
« Reply #3 on: December 31, 2011, 10:22:28 pm »
I am trying to do a similar thing with my project.  I think it has something to do with the sf::Rect class and the Contains function, but the documentation I have been able to find on it isnt very helpful.

EDIT: Perhaps the below code will help people in trying to figure out what we are trying to do. There are no problems with compiling it (using SFML 1.6), but when the .exe is run, the window closes as soon as the cursor enters it.

EDIT #2:  Lol, nevermind. I figured it out. Just had my code in the wrong place. Below is the code for a simple button click if anyone wants it.

Code: [Select]
#include <SFML/Graphics.hpp>

 int main()
 {
     sf::RenderWindow App(sf::VideoMode(400, 300), "Button Test");

     sf::Image Button1Img;
     if (!Button1Img.LoadFromFile("button1.png"))
         return EXIT_FAILURE;
     sf::Sprite Button1Obj(Button1Img);

     sf::String Text1;
     Text1.SetText("FAILURE");
     Text1.SetPosition(0, 100);

     float XPos = Button1Obj.GetPosition().x;//the x position of the button
     float YPos = Button1Obj.GetPosition().y;//the y position of the button
     float XSize = Button1Obj.GetSize().x;//the width of the button
     float YSize = Button1Obj.GetSize().y;//the height of the button
     float MouseX = App.GetInput().GetMouseX();//the x coord of the mouse when clicked
     float MouseY = App.GetInput().GetMouseY();//the y coord of the mouse when clicked

     while (App.IsOpened())
     {
         sf::Event Event;
         while (App.GetEvent(Event))
         {
             if (Event.Type == sf::Event::Closed)
                 App.Close();

                 if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
                    MouseX = App.GetInput().GetMouseX();
                    MouseY = App.GetInput().GetMouseY();


                if((MouseX >= XPos && MouseX <= XPos+XSize && MouseY >= YPos && MouseY <= YPos+YSize) == true)  {
                Text1.SetText("SUCCESS");
                }}
         }
         App.Clear();
         App.Draw(Button1Obj);
         App.Draw(Text1);
         App.Display();
     }
     return EXIT_SUCCESS;
 }

 

anything