I'm building a game and currently i'm working on GUI. Problem is that, when I use sf::RenderWindow.close() inside callback, which is called when I click on GUI button, then program crashes and Visual Studio gives me following errors. I'm using TGUI library.
First-chance exception at 0x00007FFC53F57CF3 (sfml-window-d-2.dll) in Poker.exe: 0xC0000005: Access violation reading location 0x0000009900000113.
Unhandled exception at 0x00007FFC53F57CF3 (sfml-window-d-2.dll) in Poker.exe: 0xC0000005: Access violation reading location 0x0000009900000113.
GUI_Menu.cpp, where I call window.close()
#include "GUI_Menu.h"
void GUI_Menu::InitalizeMenuWidgets()
{
gui.setWindow(this->window);
this->playButton = std::make_shared<tgui::Button>();
this->quitButton = std::make_shared<tgui::Button>();
auto _width = tgui::bindWidth(this->gui);
auto _height = tgui::bindHeight(this->gui);
auto _buttonHeight = _height / 9;
auto _buttonWidth = _width / 2;
this->playButton->setSize(_buttonWidth, _buttonHeight);
this->quitButton->setSize(_buttonWidth, _buttonHeight);
this->playButton->setPosition(_width/4, _height * 1.5/10);
this->quitButton->setPosition(_width / 4, _height * 3/10);
this->playButton->setText("Play");
this->quitButton->setText("Quit");
this->quitButton->connect("pressed", &GUI_Menu::function, this);
gui.add(playButton);
gui.add(quitButton);
}
void GUI_Menu::function() {
std::cout << "DEBUG";
this->window.close();
}
GUI_Menu.h
#ifndef GUI_MENU_H
#define GUI_MENU_H
#include "GUI.h"
#include <iostream>
class GUI_Menu
{
private:
void function();
sf::RenderWindow &window;
tgui::Gui &gui;
tgui::Button::Ptr playButton;
tgui::Button::Ptr quitButton;
public:
GUI_Menu(sf::RenderWindow &window, tgui::Gui &gui) : window(window), gui(gui){ InitalizeMenuWidgets(); }
void InitalizeMenuWidgets();
};
#endif
game.cpp, where my game loop runs.
#include "game.h"
Game::Game()
{
//Initalization
window.create(sf::VideoMode(700, 400), "Poker Game");
gui.setWindow(window);
GUI_Menu(this->window, this->gui);
this->isFullScreen = true;
}
void Game::GameLoop()
{
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyReleased:
switch (event.key.code)
case sf::Keyboard::Return:
this->ToggleFullScreen();
break;
case sf::Event::Closed:
window.close();
break;
}
gui.handleEvent(event);
}
window.clear();
gui.draw();
window.display();
}
}
void Game::ToggleFullScreen()
{
if (this->isFullScreen){
window.create(sf::VideoMode(700, 400), "Poker Game", sf::Style::Default);
this->isFullScreen = false;}
else{
window.create(sf::VideoMode(1920, 1080), "Poker Game", sf::Style::Fullscreen);
this->isFullScreen = true;}
}