Class constructor causes segfault, using SFML in C++

Viewed 35

I'm testing out creating a main menu window with SFML (just the graphics, no functionality yet,) and I keep getting segmentation fault 11 for the class constructor. These individual functions work fine on their own as functions, but not when I put them in a mainMenu class and call them. I'm not sure what is wrong with my code ); Any help is super appreciated!

Note: the state constructor sets the screenWidth to 1000 and screenHeight to 800

mainMenu.cpp

#include "mainMenu.hpp"

mainMenu::mainMenu() 
: State() 
{
    Font font;
    if (!font.loadFromFile("./assets/pixelletters.ttf")) {
        cout << "Couldn't load font" << endl;
    }
        
    // Exit
    options[0].setFont(font);
    options[0].setString("Exit");
    options[0].setPosition(35, 35);
    options[0].setFillColor(Color::White);
    options[0].setCharacterSize(30);

    // Sorts
    options[1].setFont(font);
    options[1].setString("Sorts");
    options[1].setPosition(screenWidth*0.05, screenHeight*0.4);
    options[1].setFillColor(Color::White);
    options[1].setCharacterSize(60);

    // Graphs
    options[2].setFont(font);
    options[2].setString("Graphs");
    options[2].setPosition(screenWidth*0.05, screenHeight*0.6);
    options[2].setFillColor(Color::White);
    options[2].setCharacterSize(60);

    // Title
    options[3].setFont(font);
    options[3].setString("Algorithm Visualizer");
    options[3].setPosition(screenWidth/4, screenHeight*0.2);
    options[3].setFillColor(Color::White);
    options[3].setCharacterSize(70);
}

void mainMenu::draw(RenderWindow &window)
{
    for(int i=0; i<MENU_OPTIONS; i++)
    {
        window.draw(options[i]);
    }
    window.display();
}

mainMenu.hpp

#ifndef MAINMENU_HPP
#define MAINMENU_HPP

#include <SFML/Graphics.hpp>
using namespace sf;
#include <iostream>
using namespace std;

#define MENU_OPTIONS 4

class mainMenu : public State {
protected:
    Text options[MENU_OPTIONS];

public:
    mainMenu();
    void draw(RenderWindow &window);
};

#endif

main.cpp

#include "mainMenu.hpp"

int main()
{
    RenderWindow window(VideoMode(1000, 800), "Visualizer");
    mainMenu menu = mainMenu();
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
        menu.draw(window);
    }
    return 0;
}
0 Answers
Related