SFML Game Help

Jun 20, 2015 at 9:03pm
I would like to make a program that generates a random number between 1-10, gets the player to guess if the next number will be higher or lower.

My idea so far:
Fullscreen, start menu with a play button.
3 lives, displayed in the top left corner.
It will have 3 words down the bottom with Higher, Lower and Exit. The color of the word will change to red when selected.
The information and number will be displayed in the middle/top.
Gain 5 point for a right answer and -3 for wrong.
Highscore saved in a file and displayed in top right corner.

I have decided to code this in c++ and SFML, here is my code so far (question in the comments):

Stdafx.h
1
2
3
4
5
// Graphics
#include <SFML\Graphics.hpp>

#include <iostream>
using namespace std;


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Stdafx.h"
#include "Menu.h"

int main()
{
	sf::RenderWindow window(sf::VideoMode(200, 200), "Window", sf::Style::Fullscreen);
	window.setFramerateLimit(60);

	Menu menu(window.getSize().x, window.getSize().y);

	while (window.isOpen())
	{
		sf::Event event;

		while (window.pollEvent(event)) // Event handler
		{
			switch (event.type)
			{
			case sf::Event::KeyPressed:
				switch (event.key.code)
				{
				case sf::Keyboard::Left:
					menu.MoveLeft();
					break;

				case sf::Keyboard::Right:
					menu.MoveRight();
					break;

				case sf::Keyboard::Return:
					switch (menu.GetPressedItem())
					{
					case 0: // Play button
						// What do i put here to reset my screen so i can put my actual game on the screen?
						break;
					case 1: // Exit
						window.close();
						break;
					}
					break;
				}

				break;

			case sf::Event::Closed:
				window.close();

				break;
			}
		}

		window.clear(sf::Color(135, 206, 250)); // Light blue
		menu.draw(window); // Draw the menu
		window.display(); // Display
	}

	return 0;
}


Menu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

#define MAX_NUMBER_OF_ITEMS 2

class Menu
{
public:
	Menu(float width, float height);
	~Menu();

	void draw(sf::RenderWindow &window);
	void MoveLeft();
	void MoveRight();
	int GetPressedItem();

private:
	int selectedItemIndex;
	sf::Font font;
	sf::Text menu[MAX_NUMBER_OF_ITEMS];
};


Menu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "Stdafx.h"
#include "Menu.h"


Menu::Menu(float width, float height)
{
	if (!font.loadFromFile("arial.ttf"))
	{
		cout << "Error can not load Font!" << endl;
	}

	menu[0].setFont(font);
	menu[0].setColor(sf::Color::Red);
	menu[0].setString("Play");
	menu[0].setPosition(sf::Vector2f(width / (MAX_NUMBER_OF_ITEMS + 1) * 1, height / 1.2));

	menu[1].setFont(font);
	menu[1].setColor(sf::Color::White);
	menu[1].setString("Exit");
	menu[1].setPosition(sf::Vector2f(width / (MAX_NUMBER_OF_ITEMS + 1) * 2, height / 1.2));

	selectedItemIndex = 0;
}

Menu::~Menu()
{

}

void Menu::draw(sf::RenderWindow &window)
{
	for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++)
	{
		window.draw(menu[i]);
	}
}

void Menu::MoveLeft()
{
	if (selectedItemIndex - 1 >= 0)
	{
		menu[selectedItemIndex].setColor(sf::Color::White);
		selectedItemIndex--;
		menu[selectedItemIndex].setColor(sf::Color::Red);
	}
}

void Menu::MoveRight()
{
	if (selectedItemIndex + 1 < MAX_NUMBER_OF_ITEMS)
	{
		menu[selectedItemIndex].setColor(sf::Color::White);
		selectedItemIndex++;
		menu[selectedItemIndex].setColor(sf::Color::Red);
	}
}

int Menu::GetPressedItem()
{
	return selectedItemIndex;
}


This is my first time using graphics properly so please explain as much as possible. Thanks in advance.

Edit: This is my game plan http://imgur.com/YXN1a6A
Last edited on Jun 20, 2015 at 9:34pm
Jun 21, 2015 at 6:28am
So you want to know how to switch between different screens?

read this
http://gamedevgeek.com/tutorials/managing-game-states-in-c/
Jun 21, 2015 at 7:13am
you can implement a function in your menu class to clear the window, or use window.clear() to clear the window
then you can draw everything you want
Last edited on Jun 21, 2015 at 3:12pm
Jun 21, 2015 at 8:00am
So is that the way i would want to go, with those functions in my game loop?

Edit: I have installed SDL and been reading the link Yanson gave me, but now for no reason i get an exception thrown, i have debugged to limit it down to the loading of the font that is causing it:

First-chance exception at 0x57A88B51 (msvcr120d.dll) in Higher Or Lower.exe: 0xC0000005: Access violation reading location 0x74742E6C.

If there is a handler for this exception, the program may be safely continued.

Edit: My code compiles in Release mode but not Debug mode.
Last edited on Jun 21, 2015 at 11:00am
Jun 21, 2015 at 3:14pm
which line do you get that exception?
this exception is that you want to access a part of address of memory that windows doesnt allow to write in it
Jun 21, 2015 at 3:24pm
I get it on the line:

1
2
3
4
If(!font.loadFromFile("arial.ttf"))
{
//Error
}


Edit: The If statement is where it crashes as i do not receive my error message on the console
Last edited on Jun 21, 2015 at 5:36pm
Jun 21, 2015 at 7:40pm
I made a new project and fixed my error! Yay, i can code again
Topic archived. No new replies allowed.