Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: unhandled exception at location 0x00007FFD246F7196 (sfml-graphics-d-2.dll)  (Read 301 times)

0 Members and 1 Guest are viewing this topic.

adamzar

  • Newbie
  • *
  • Posts: 1
    • View Profile
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
« Last Edit: December 30, 2023, 12:44:21 pm by eXpl0it3r »

kojack

  • Sr. Member
  • ****
  • Posts: 315
  • C++/C# game dev teacher.
    • View Profile
Re: unhandled exception at location 0x00007FFD246F7196 (sfml-graphics-d-2.dll)
« Reply #1 on: December 27, 2023, 09:54:52 am »
In your up/down code, there's a spot that will cause an error.
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);
   }
}
 
Let's say you have selected the third text item (so MainMenuSelected is 2). It passes the first if (since 2+1 is <= 3). Then it is incremented to 3. But the next if checks if it's 4. It's not, so no wrap around. Then it tries to set the colour of text 3, but that doesn't exist.
Change the 4 to 3 and moving down shouldn't crash.

Moving up has a similar but less serious issue (doesn't crash, just no wrap around). The first if only works if you aren't at the top. It should be:
if (MainMenuSelected >= 0)

Another thing that may be an issue (I can't remember what SFML does in this situation) is you are closing windows repeatedly in the while loops down lower.

For example here (there's two more like it):
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();
                  }
While the play window is open, it will keep trying to close the options and about windows every frame. I don't know what closing a closed window does (might be fine), just mentioning it.

 

anything