Hello. I am working on a 2D game and now I'm dealing with the menu.
everything compiles and i see result i wanted where I can move up and down with up and down keys but I also get some unhandled exceptions, first time dealing with such thing and I really dont understand the issue, been trying to repair it myself for over 2 hours now, also its my first post (I read the rules).
I believe error is connected to fonts so no need to look in main if i gave not enough information just write and i ll try to provide more
#pragma once
#include "SFML/Graphics.hpp"
#define ITEMS 3
class Menu
{
public:
Menu(float width, float height);
~Menu();
void draw(sf::RenderWindow& window);
void MoveUp();
void MoveDown();
int MainMenuPressed()
{
return MainMenuSelected;
}
private:
int MainMenuSelected;
sf::Font font;
sf::Text menu[ITEMS];
};
#include "Menu.h"
#include <iostream>
Menu::Menu(float width, float height)
{
//font.loadFromFile("fonts/arial.ttf");
if (!font.loadFromFile("fonts/arial.ttf"))
{
std::cout << "error loading font" << std::endl;
}
menu[0].setFont(font);
menu[0].setFillColor(sf::Color::White);
menu[0].setString("Play");
menu[0].setCharacterSize(70);
menu[0].setPosition(sf::Vector2f(width / 2, height / (ITEMS+1) * 1));
menu[1].setFont(font);
menu[1].setFillColor(sf::Color::White);
menu[1].setString("Options");
menu[1].setCharacterSize(70);
menu[1].setPosition(sf::Vector2f(width / 2, height / (ITEMS + 1) * 2));
menu[2].setFont(font);
menu[2].setFillColor(sf::Color::White);
menu[2].setString("Exit");
menu[2].setCharacterSize(70);
menu[2].setPosition(sf::Vector2f(width / 2, height / (ITEMS + 1) * 3));
MainMenuSelected = -1;
}
void Menu::draw(sf::RenderWindow &window)
{
for (int i = 0; i < ITEMS; ++i)
{
window.draw(menu[i]);
}
}
void Menu::MoveUp()
{
if (MainMenuSelected - 1 >= 0)
{
menu[MainMenuSelected].setFillColor(sf::Color::White);
MainMenuSelected--;
if (MainMenuSelected == -1)
{
MainMenuSelected = 2;
}
menu[MainMenuSelected].setFillColor(sf::Color::Blue);
}
}
void Menu::MoveDown()
{
if (MainMenuSelected + 1 <= ITEMS)
{
menu[MainMenuSelected].setFillColor(sf::Color::White);
MainMenuSelected++;
if (MainMenuSelected == 4)
{
MainMenuSelected = 0;
}
menu[MainMenuSelected].setFillColor(sf::Color::Blue);
}
}
Menu::~Menu()
{
}
#include "SFML\Graphics.hpp">
#include <iostream>
#include "Menu.h"
int main()
{
sf::RenderWindow MENU(sf::VideoMode(960, 720), "main menu");
Menu menu(MENU.getSize().x, MENU.getSize().y);
while (MENU.isOpen())
{
sf::Event ev;
while (MENU.pollEvent(ev))
{
if (ev.type == sf::Event::Closed) MENU.close();
if (ev.type == sf::Event::KeyPressed)
{
if (ev.key.code == sf::Keyboard::Up)
{
menu.MoveUp();
break;
}
if (ev.key.code == sf::Keyboard::Down)
{
menu.MoveDown();
break;
}
if (ev.key.code == sf::Keyboard::Return)
{
sf::RenderWindow Play(sf::VideoMode(960, 720), "GameName");
sf::RenderWindow Options(sf::VideoMode(960, 720), "Options");
sf::RenderWindow About(sf::VideoMode(960, 720), "about");
int x = menu.MainMenuPressed();
if (x == 0)
{
while (Play.isOpen())
{
sf::Event ev1;
while (Play.pollEvent(ev1))
{
if (ev1.type == sf::Event::Closed) Play.close();
if (ev1.type == sf::Event::KeyPressed)
{
if (ev1.key.code == sf::Keyboard::Escape) Play.close();
}
}
Options.close();
About.close();
Play.clear();
Play.display();
}
}
if (x == 1)
{
while (Options.isOpen())
{
sf::Event ev1;
while (Options.pollEvent(ev1))
{
if (ev1.type == sf::Event::Closed) Options.close();
if (ev1.type == sf::Event::KeyPressed)
{
if (ev1.key.code == sf::Keyboard::Escape) Options.close();
}
}
Play.close();
About.close();
Options.clear();
Options.display();
}
}
if (x == 2)
{
while (About.isOpen())
{
sf::Event ev1;
while (About.pollEvent(ev1))
{
if (ev1.type == sf::Event::Closed) About.close();
if (ev1.type == sf::Event::KeyPressed)
{
if (ev1.key.code == sf::Keyboard::Escape) About.close();
}
}
Options.close();
Play.close();
About.clear();
About.display();
}
}
if (x == 3)
MENU.close();
break;
}
}
}
MENU.clear();
menu.draw(MENU);
MENU.display();
}
return 0;
}
An exception was thrown at location 0x00007FFD3A407196 (sfml-graphics-d-2.dll) in SFML.exe: 0xC0000005: Access violation while reading at location 0x0000018FFFFFFFFFF.
Unhandled exception at location 0x00007FFD3A407196 (sfml-graphics-d-2.dll) in SFML.exe: 0xC0000005: Access violation while reading at location 0x0000018FFFFFFFFF.
I am 90% sure its not because off my fonts file location and ttf file