[英]Window closes when adding a sprite
當我嘗試使用自己創建的函數時,我的窗口崩潰了,我用它在屏幕上創建了一個精靈,但是由於某種原因它崩潰了。
我收到錯誤:
分段故障(核心已轉儲)
這是我的代碼:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include <unistd.h>
#include <iostream>
#include <vector>
using namespace std;
vector<sf::Sprite*> Tiles;
void createTile (string TextureIN, int x, int y) {
sf::Vector2f Pos(x, y);
sf::Texture Texture;
Texture.loadFromFile(TextureIN);
sf::Sprite *Tile;
Tile->setTexture(Texture);
Tile->setPosition(Pos);
Tiles.push_back(Tile);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
createTile("Recources/grass.png", 50, 50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Blue);
for (int i; i < Tiles.size(); i++) {
window.draw(*Tiles[i]);
}
window.display();
}
return 0;
}
我之前有一個可用的版本,但是我的計算機崩潰了,我忘記了對其進行備份>。<
無論如何,希望您能為我解決這個問題。
您創建的指針沒有適當的內存分配。
所以代替
sf::Sprite *Tile;
采用
sf::Sprite *Tile = new sf::Sprite;
一定要看一看如何使用指針向量在C ++中動態分配對象時如何避免內存泄漏? 也一樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.