First SFML 2.1 GUI project crash

307 views Asked by At

Today i started writing my firs SFML program, it is supposed to be a Snake game, and it was all going pretty well, until I started making the two dimension class array, which was supposed to hold the information of each tile. The some wierd stuff started happening: The program compiled, and crashed where i tried to stream a number into a string, even though that bit worked before the array code was written... I also tried commenting out everything bit by bit and with that got even stranger results...

#include <SFML/Graphics.hpp>
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <windows.h>
int main(){
char KeyPressed;
int angle = 0;
float FPS;
sf::Clock Clock;
std::ostringstream oss;
sf::String sFPS;
sf::RenderWindow window(sf::VideoMode(510,510),"Snake Prototype");
window.display();
class Tile {
public:
unsigned int x;
unsigned int y;
unsigned char state;
unsigned char lenght;
sf::RectangleShape make(){
    if(state == 1){
sf::RectangleShape rect(sf::Vector2f(15, 15));
rect.setPosition(x, y);
rect.setFillColor(sf::Color(220, 40, 140));
std::cout<< "Rendering.. State: "<< (int)state<<std::endl; return rect;
}
}
};
    Tile tileArray[30][30];
    int i = 1;
    int t = 1;
while(t <= 30){
while(i <= 30){
        tileArray[i][t].x = i;
        tileArray[i][t].y = t;
        std::cout<<"x:  " << tileArray[i][t].x << "   y:  " << tileArray[i][t].y <<std::endl;
        i++;
}
i = 1;
t++;
}
t = 0;
i = 0;
std::cout<<"LoopOver"<<std::endl;
while(window.isOpen()){
        window.clear(); // If this is commented out the array doesn't initialise correctly, No Idea why
        sf::Event event;
    while(window.pollEvent(event)){
        if(event.type == sf::Event::Closed){window.close();}
        if((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Escape)){window.close();}
        if((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Left)){KeyPressed = 4;}
        if((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Up)){KeyPressed = 1;}
        if((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Right)){KeyPressed = 2;}
        if((event.type == sf::Event::KeyPressed)&&(event.key.code == sf::Keyboard::Down)){KeyPressed = 3;}
    }
std::cout<<"Polling loop ended"<<std::endl;


//      FPS COUNT
     window.display();
    FPS = 1/Clock.restart().asSeconds();
     std::cout<<"Time gotten"<<std::endl; // random cout's to know, up to which point the program actually runs
     oss<< "Snake Prototype "<<"FPS: "<<(int)FPS;
     std::cout<<"TitleSet"<<std::endl;
    sFPS = oss.str(); // this seems to be the problem, even though it seems like legal c++ code
    std::cout<<"String written"<<std::endl;
    oss.str("");
window.setTitle(sFPS);
std::cout<<"MainLoopover"<<std::endl;
}
return 1;
}

I tried to comment the bits that ,I think are causing problems, but anyway, can someone tell me why this program crashes?

1

There are 1 answers

0
nvoigt On BEST ANSWER

Your initialization loops go from 1 to 30. That's wrong. Array indexes range from 0 to 29 if you declare them to be of size 30. Every other strange happening in your program is probably from overwriting memory that is none of your business. Fix this first.