SFML community forums

Help => Graphics => Topic started by: Raiid on February 07, 2011, 11:48:45 pm

Title: With an Image, open new Form
Post by: Raiid on February 07, 2011, 11:48:45 pm
Hello,

i am a noob in SFML :(

My Game starts with the menu.cpp. In this Menu.cpp are 2 Images. The Background and a Img "Start game". My Problem is, i didnt understand how i click this image.

This is My Code:

Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <cmath>

int main()
{
// Create Window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Race of Life");

    // Create a sprite for the background
    sf::Image HintergrundImage;
    if (!HintergrundImage.LoadFromFile("can.jpg"))
        return EXIT_FAILURE;
    sf::Sprite Hintergrund(HintergrundImage);


    sf::Image StartImage;
    if (!StartImage.LoadFromFile("start.png"))
        return EXIT_FAILURE;
    sf::Sprite Start(StartImage);
sf::Image


//Load Musik
sf::SoundBuffer  buffer;
buffer.LoadFromFile( "IM.ogg" );

sf::Sound  sound( buffer );
sound.Play();

   // 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();
        }
        float ElapsedTime = App.GetFrameTime();
App.Draw(Hintergrund);
App.Draw(Start);
       App.Display();
sf::Sleep(0.0015f);

}
return 0;
}



When the "Start Game" image clicked, they must open me the race.cpp. but How?

Anybody help me pls:(
Title: With an Image, open new Form
Post by: excrulon on February 08, 2011, 04:07:44 am
If I understand what you're asking, you'll need to implement some collision detection with the mouse and the "Start" image. You could use a basic point in rect test.

Code: [Select]

if(mouseX > startImgX &&
   mouseX < startImgX + startImgWidth &&
   mouseY > startImgY &&
   mouseY < startImgY + startImgHeight)
{
     race.run();
}


Think that logic is right. Might have screwed it up, but it should give you the general idea. :)
Title: With an Image, open new Form
Post by: XDest on February 08, 2011, 11:48:56 pm
Code: [Select]
sf::Vector2f mouse (App.ConvertCoords
         (App.GetInput().GetMouseX(),
          App.GetInput().GetMouseY()));


To get the mouse.

Get the Rect of your button, and then look up the sf::Rect "Contains()" function in the documentation.