Sign in to follow this  
superman3275

[SFML] Question About Main Menus?

Recommended Posts

superman3275    2061
About two hours ago, I wrote a MainMenu class. It essentially Made a sf::FloatRect that held the Play button and ran an Update loop that tested if the player clicked inside the sf::FloatRect. There's a boolean varible called IsPlaying. Originally I had a while loop that ran MainMenu.Update(), Cleared the screen, and Ran MainMenu.Draw() while IsPlaying was false. If the player clicked on the play button IsPlaying was supposed to be set to false. But the screen was freezing and not working whenever I ran my main loop. All helps appreciated, and if anyone has any tutorials for making a main menu, or can tell me how they do it (<- This would be Awesome!) I'd be extremely happy.
MainMenu.h
[CODE]
#pragma once
#include <SFML/Graphics.hpp>
class MainMenu
{
public:
MainMenu(sf::Image &MainImage, sf::RenderWindow &Window, float x1, float y1, float x2, float y2);
void Update(sf::RenderWindow &Window);
void Draw(sf::RenderWindow &Window);
bool GetIsPlaying();
private:
bool IsPlaying;
sf::Sprite MainSprite;
sf::Image MainImage;
sf::FloatRect PlayGame;
sf::Event MainEvent;
};
[/CODE]
MainMenu.cpp
[CODE]
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "MainMenu.h"
MainMenu::MainMenu(sf::Image &MainImage, sf::RenderWindow &Window, float x1, float y1, float x2, float y2)
{
this->MainImage = MainImage;
MainSprite.SetImage(this->MainImage);
PlayGame.Left = x1;
PlayGame.Top = y1;
PlayGame.Right = x2;
PlayGame.Bottom = y2;
IsPlaying = false;
}
void MainMenu::Update(sf::RenderWindow &Window)
{
if ((MainEvent.Type == sf::Event::KeyPressed) && (MainEvent.Key.Code == sf::Key::Up))
IsPlaying = true;
}
void MainMenu::Draw(sf::RenderWindow &Window)
{
Window.Draw(MainSprite);
}
bool MainMenu::GetIsPlaying()
{
return IsPlaying;
}
[/CODE]
Can someone tell me how to make a mainmenu. I made it so that the up button played it. I tried to program it myself but I'm kind of lost, and even with looking at the sfml documentation I'm not sure how to do this. (The Up Button didn't work either :( Edited by superman3275

Share this post


Link to post
Share on other sites
Dragonsoulj    3212
[code]Window.GetEvent(MainEvent)[/code]
Is missing from your class definitions. I don't see you call that anywhere so you aren't passing events to MainEvent.



An update loop, without a timer/clock to limit the number of updates you do, would look like this:
[code]sf::RenderWindow myApp(......);
sf::Event myEvent;
while(myApp.IsOpened())
{
while(myApp.GetEvent(myEvent))
{
// Handle Events
}
}[/code]


From their tutorials page ([url="http://www.sfml-dev.org/tutorials/1.6/window-events.php"]link[/url]) where App is a sf::RenderWindow
[code]while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Window closed
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key pressed
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
}[/code]

By the way, what do you mean by "playing" a main menu?

[b]EDIT:[/b]
Why don't you try to match this loop?
[code]
sf::RenderWindow myRenderWindow(.....);
sf::Event myEventHolder;
bool isMainMenu = false;
while(myRenderWindow.IsOpened()
{
while(myRenderWindow.GetEvent(myEventHolder))
{
if(myEventHolder.Type == sf::Event::KeyPressed && myEventHolder.Key.Code == sf::Key::Up)
{
// Toggles isMainMenu. If it is false, it becomes true, if it is true it becomes false
isMainMenu = !isMainMenu;
}
}

myRenderWindow.Clear();

if(isMainMenu)
{
// Draw Main Menu
// myRenderWindow.Draw(....);
}
else
{
// Draw Something Else
// myRenderWindow.Draw(....);
}

myRenderWindow.Display();
}[/code] Edited by Dragonsoulj

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this