[SFML] Question About Main Menus?

Started by
0 comments, last by Dragonsoulj 11 years, 6 months ago
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

#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;
};

MainMenu.cpp

#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;
}

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 :(

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Advertisement
Window.GetEvent(MainEvent)
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:
sf::RenderWindow myApp(......);
sf::Event myEvent;
while(myApp.IsOpened())
{
while(myApp.GetEvent(myEvent))
{
// Handle Events
}
}



From their tutorials page (link) where App is a sf::RenderWindow
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();
}
}


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

EDIT:
Why don't you try to match this loop?

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();
}

This topic is closed to new replies.

Advertisement