c++ - 第一个 SFML 2.1 GUI 项目崩溃

标签 c++ sfml ostringstream

今天我开始编写我的第一个 SFML 程序,它应该是一个 Snake 游戏,一切都很顺利,直到我开始制作二维类数组,它应该保存每个方 block 的信息。一些奇怪的事情开始发生:程序编译,并在我尝试将数字流式传输到字符串时崩溃,即使在编写数组代码之前该位有效......我也尝试一点一点地注释掉所有内容得到了更奇怪的结果......

#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;
}

我试图评论我认为导致问题的位,但无论如何,有人能告诉我为什么这个程序崩溃吗?

最佳答案

您的初始化循环从 1 到 30。这是错误的。如果您声明数组索引的大小为 30,则数组索引的范围从 0 到 29。您程序中发生的所有其他奇怪情况都可能是由于覆盖了与您无关的内存。先解决这个问题。

关于c++ - 第一个 SFML 2.1 GUI 项目崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20815317/

相关文章:

c++ - 如何为我的应用程序禁用 UAC

c++ - 如何增量编码 C/C++ 结构以通过套接字传输

c++ - 使用静态库进行 SFML 编译时出错。 C++

c++ - 为自定义流类设置 std::io_base 标志

c++ - 将字符串移出 std::ostringstream

c++ - double 和 oStringStream 的奇怪行为

c# - C++ 等效于 C# 嵌套私有(private)类

c++ - 我如何理解fdump-class-hierarchy输出

opengl - SDL2/SFML2 中的软件和硬件渲染

c++ - 未定义对 sf::Window::hasFocus 的引用?