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

Author Topic: My window crashes the instant i compile the code and execute it.  (Read 1023 times)

0 Members and 1 Guest are viewing this topic.

beastlyhexquest

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
As soon as i debug and run the code it crashes on instant and i dont know why. Could someone help me!

Main
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Audio/Music.hpp>
#include <iostream>
#include "mainMenu.h"
int main()
{

        sf::RenderWindow window( sf::VideoMode( 600, 600 ), "RPG" );

    mainMenu menu(window.getSize().x, window.getSize().y);

    sf::Music menuMus;
    if (!menuMus.openFromFile("battletheme.wav"))
            std::cout<<"Sound file not found"<<std::endl;

//menuMus.setVolume(25);
//menuMus.setLoop(true);
 // menuMus.play()

        while ( window.isOpen( ) )
        {
                sf::Event event;

                while ( window.pollEvent( event ) )
                {
                        switch ( event.type )
                        {
                        case sf::Event::Closed:
                                window.close( );

                                break;
                        }
                }

                window.clear( );
        menu.draw(window);
                window.display( );
        }
}
 

mainMenu.cpp
#include "mainMenu.h"

mainMenu::mainMenu(float width, float height)
{
    if(!font.loadFromFile("arial.ttf"))
    {
        //handle error
    }

    menu[0].setFont(font);
    menu[0].setColor(sf::Color::Red);
    menu[0].setString("Play");
    menu[0].setPosition(sf::Vector2f(width/2, height/(MAX_NUMBER_OF_ITEMS + 1) *1));

    menu[1].setFont(font);
    menu[1].setColor(sf::Color::White);
    menu[1].setString("Options");
    menu[1].setPosition(sf::Vector2f(width/2, height/(MAX_NUMBER_OF_ITEMS + 1) *1));

    menu[2].setFont(font);
    menu[2].setColor(sf::Color::White);
    menu[2].setString("Exit");
    menu[2].setPosition(sf::Vector2f(width/2, height/(MAX_NUMBER_OF_ITEMS + 1) *3));
}
   ;

   void mainMenu::draw(sf::RenderWindow &window){
    for(int i=0; 1< MAX_NUMBER_OF_ITEMS; i++)
    {
        window.draw(menu[i]);
    }
   }
 

mainMenu.h
#ifndef MAINMENU_H
#define MAINMENU_H
#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
#define MAX_NUMBER_OF_ITEMS 3
#pragma once
class mainMenu
{
    public:
        mainMenu(float width, float height);
        mainMenu();

        void draw(sf::RenderWindow &window);
        void MoveUp();
        void MoveDown();

    private:
        int selectedItemIndex;
        sf::Font font;
        sf::Text menu[MAX_NUMBER_OF_ITEMS];
};

#endif // MAINMENU_H
 

Can someone help me solve this problem? ive been at it for 2/3 hours now and i think it might just be something really simple...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: My window crashes the instant i compile the code and execute it.
« Reply #1 on: October 17, 2015, 10:16:16 pm »
Probably because of this typo:
for(int i=0; 1< MAX_NUMBER_OF_ITEMS; i++)

Guess that should be i not 1.

Also next time just use a debugger and it will tell you where the application crashes and the call stack will allow you to find its origin.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/