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

Author Topic: 9557 segmentation fault (core dumped)  (Read 849 times)

0 Members and 1 Guest are viewing this topic.

That Martin Guy

  • Newbie
  • *
  • Posts: 27
    • View Profile
9557 segmentation fault (core dumped)
« on: May 21, 2017, 12:19:36 pm »
I have installed a virtual machine which runs manjaro. This virtual machine is the one I use to program on. While working on a small game I encountered an error when trying to launch ./a.out. The code compiles fine when I use
Quote
g++ *.cc *.h *.cpp -std=c++14 -lsfml-system -lsfml-graphics -lsfml-window -Wall -Wextra
, but it immediatly closes when I try and run it, and gives me that error. I got this error while reworking my main menu code.

Game.cpp
#include <iostream>
#include "Game.h"
#include "MainMenu.h"

void Game::start()
{
        if(gameState != Uninitialized)
                return;
       
        window.create(sf::VideoMode(1024, 768), "Window");
        gameState = Game::Menu;

        while(!isExiting())
        {
                gameLoop();
        }

        window.close();
}

bool Game::isExiting()
{
        return gameState == Exiting;
}

void Game::gameLoop()
{
        switch(gameState)
        {
                case Menu:
                        handleMenu();
                        break;
                default:
                        break;
        }      

        sf::Event event;
        while(window.pollEvent(event))
        {
                switch(event.type)
                {
                        case sf::Event::Closed:
                                gameState = Exiting;
                                break;
                       
                        case sf::Event::MouseEntered:
                                window.setMouseCursorVisible(false);
                                break;
                        case sf::Event::MouseLeft:
                                window.setMouseCursorVisible(true);
                                break;

                        case sf::Event::KeyPressed:
                                if(gameState == Menu)
                                {
                                        MainMenu menu;
                                        menu.navigate(event);
                                }

                        default:
                                break;
                }
        }
        window.display();
}

void Game::handleMenu()
{
        window.setKeyRepeatEnabled(false);
        MainMenu menu;
        menu.show(window);
}

Game::GameState Game::gameState = Uninitialized;
sf::RenderWindow Game::window;

MainMenu.cpp
#include "MainMenu.h"
#include <string>

class MenuItem
{
        public:
                MenuItem(std::string textIn, bool activeDefault, float verticalOffset, sf::Font font)
                {
                        sf::VideoMode resolution = sf::VideoMode::getDesktopMode();
                        setText(textIn, sf::Vector2f(resolution.width / 2, resolution.height / 2 + verticalOffset), font);
                        setActive(activeDefault);
                }
                bool isActive()
                {
                        return active;
                }
                sf::Text getText()
                {
                        return text;
                }
                void setActive(bool isActive)
                {
                        active = isActive;
                }
                void setText(std::string string, sf::Vector2f position, sf::Font font)
                {
                        text.setString(string);
                        text.setFont(font);
                        text.setCharacterSize(24);
                        text.setPosition(position);
                       
                        if(this->isActive())
                        {
                                text.setFillColor(sf::Color::Green);
                        }
                        else
                        {
                                text.setFillColor(sf::Color::Blue);
                        }
                }
        private:
                bool active;
                sf::Text text;
};

void MainMenu::show(sf::RenderWindow& window)
{
        sf::Font font;
        font.loadFromFile("resources/fonts/arial.ttf");
       
        MenuItem playButton("Play", true, -50.f, font);
        MenuItem optionsButton("Options", false, 0.f, font);
        MenuItem exitButton("Exit", false, 50.f, font);

        window.draw(playButton.getText());
        window.draw(optionsButton.getText());
        window.draw(exitButton.getText());
}

//Just a stub for now
void MainMenu::navigate(sf::Event event);

}

Note that I'm garbage at linux, and I have done nothing on it except for use the terminal to program and make some folders. I did not even install SFML myself, a friend did it for me while I was AFK.
« Last Edit: May 21, 2017, 12:21:55 pm by That Martin Guy »