Whenever I exit my application I get the following error:
Exception thrown at 0x009F8E44 in Shooter.exe: 0xC0000005: Access violation reading location 0x00000000.
The error is thrown by the file "glcontext.cpp".
I don't know what causes this, but I assume it is related to the way the project is set up.
Main file:
int main()
{
Game::start();
//This is printed to the console as expected
std::cout << "test main" << std::endl;
return 0;
}
Game header:
#pragma once
#include "pch.h"
class Game
{
public:
static void start();
private:
static bool isExiting();
static void gameLoop(sf::RenderWindow& window);
enum GameState {Uninitialized, ShowingMenu, Paused, Playing, Exiting};
static GameState gameState;
};
Game cpp:
#include "pch.h"
#include "Game.h"
#include "Menu.h"
#include <iostream>
void Game::start()
{
if(gameState != Uninitialized)
return;
sf::RenderWindow window(sf::VideoMode(1024, 768, 32), "Shooter");
gameState = ShowingMenu;
while(!isExiting())
gameLoop(window);
window.close();
std::cout << "test" << std::endl;
}
bool Game::isExiting()
{
return gameState == Exiting;
}
void Game::gameLoop(sf::RenderWindow& window)
{
sf::Event event;
while(window.pollEvent(event))
{
if(gameState == ShowingMenu)
{
window.clear(sf::Color(0, 0, 0));
Menu menu;
Menu::MenuResponse response = menu.showMenu(window);
if(response == Menu::MenuResponse::Exit)
gameState = Exiting;
}
}
}
Game::GameState Game::gameState = Uninitialized;
The "test" print is the last line of code that is executed by the program, as you can see in the main file. Note that I do not have the source of the glcontext.cpp file, since it is a library. I assume the issue is not caused by my code in itself since I get no compilation errors, and the error is from a library.
EDIT: The problem seems to come from the navigateMenu
function from my Menu class. The function looks like this:
Menu::MenuResponse Menu::navigateMenu(sf::RenderWindow& window)
{
sf::Event event;
while(true)
{
while(window.pollEvent(event))
{
if(event.type == sf::Event::KeyPressed)
{
int previousIndex = currentItem.index;
std::list<MenuItem>::iterator iterator;
int keyCode = event.key.code;
if(keyCode == sf::Keyboard::W || keyCode == sf::Keyboard::Up || keyCode == sf::Keyboard::Down || keyCode == sf::Keyboard::S)
{
int checkIndex = (keyCode == sf::Keyboard::W || keyCode == sf::Keyboard::Up) ? previousIndex - 1 : previousIndex + 1;
for(iterator = menuItems.begin(); iterator != menuItems.end(); ++iterator)
{
if(iterator->index == checkIndex)
{
currentItem = *iterator;
iterator->text.setFillColor(sf::Color::Yellow);
}
else
{
iterator->text.setFillColor(sf::Color::White);
}
}
}
if(keyCode == sf::Keyboard::Enter)
return currentItem.response;
if(keyCode == sf::Keyboard::Escape)
return Exit;
}
if(event.type == sf::Event::Closed)
return Exit;
}
std::list<MenuItem>::iterator iterator;
for(iterator = menuItems.begin(); iterator != menuItems.end(); iterator++)
{
window.draw(iterator->text);
}
window.display();
}
return Nothing;
}
currentItem
is an instance of MenuItem
, a struct that looks like this:
struct MenuItem
{
int index;
bool active;
MenuResponse response;
std::string name;
sf::Text text;
void create(int index, bool startsActive, MenuResponse response, std::string name, sf::Vector2f position)
{
this->index = index;
this->active = startsActive;
this->response = response;
this->name = name;
text.setFont(menuFont.font);
text.setString(name);
text.setFillColor(startsActive ? sf::Color::White : sf::Color::Yellow);
text.setPosition(position);
}
};
menuFont is a static instance of MenuFont, that looks like this:
class MenuFont
{
public:
sf::Font font;
MenuFont()
{
font.loadFromFile("digit.ttf");
}
};
User contributions licensed under CC BY-SA 3.0