c++ SFML应用程序中的窗口将自动关闭,代码为0

3htmauhk  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(125)

我试着做回合制游戏。现在我试着做网格。我在屏幕上输出关于网格的信息的文本。但是过了一会儿窗口一直关闭。可能我做错了什么?我的代码在下面。
tile. h//网格块

#pragma once

#include "SFML/Graphics.hpp"
#include "AssetManager.h"

class Tile
{
public:
    Tile();

    Tile(float size, sf::Texture texture);

    void setPosition(sf::Vector2f position);    
    sf::Vector2f getPosition() const;

    void setTexture(std::string& filename);

    
    float getTileSize();

    void setShape(); // set the rectangle shape parametrs
    sf::RectangleShape& getShape();

private:

    float tileSize = 0;
    sf::Vector2f tilePosition;
    sf::Texture tileTexture;
    sf::RectangleShape tileShape;
};

字符串
tile.cpp

#include "Tile.h"

Tile::Tile()
{
    setShape();
}

Tile::Tile(float size, sf::Texture texture) : tileSize(size), tileTexture(texture)
{
    tileShape.setTexture(&tileTexture);
    tileShape.setOutlineThickness(1.f);
    tileShape.setOutlineColor(sf::Color::Black);
}

void Tile::setShape()
{
    if(tileSize == 0) tileSize = 64.f;  
    tileShape.setSize(sf::Vector2f(tileSize,tileSize));
    tileShape.setFillColor(sf::Color::White);
    tileShape.setOutlineThickness(1.f);
    tileShape.setOutlineColor(sf::Color::Black);

}

void Tile::setPosition(sf::Vector2f position)
{
    tilePosition = position;
    tileShape.setPosition(tilePosition.x, tilePosition.y);
}

sf::Vector2f Tile::getPosition() const
{
    return tilePosition;
}

void Tile::setTexture(std::string& filename)
{
    tileShape.setTexture(&AssetManager::GetTexture(filename));  
}

float Tile::getTileSize()
{
    return tileSize;
}

sf::RectangleShape& Tile::getShape()
{
    return tileShape;
}


MapManager. h//网格生成器类

#pragma once

#include "SFML/Graphics.hpp"
#include "Tile.h"
#include <string>
#include <vector> 
#include <sstream>

class AssetManager;

class MapManager
{
public:

    MapManager();

    Tile getTile();

    void generateGrid();

    void InitializeInformationText();//Initialize sf::Text gridInformationText

    std::string gridInformation(sf::RenderWindow& window); // Make information obaut screen

    sf::Text& getInformationText();

    void drawGrid(sf::RenderWindow& window);
    
    std::vector<std::vector<Tile>>& getGrid();

private:
    size_t Rows = 10;  
    size_t Colloms = 10;
    float gridSizeF = 0;  // size of one cell of grid 
    unsigned gridSizeU = 0;
    Tile tile; 
    sf::Text gridInformationText;
    sf::Font textFont;
    sf::Vector2i mousePosScreen; // position of mause on the screen
    sf::Vector2i mousePosWindow; // mouse position on window
    sf::Vector2f mousePosView;   // mouse position on view
    sf::Vector2u mousePosGrid;   // moues position on grid
    std::vector<std::vector<Tile>> grid;

};


MapManager.cpp

#include "MapManager.h"
#include <iostream>


MapManager::MapManager()
{   
    std::cout << "Map manager initialized\n";
    gridSizeF = tile.getTileSize();
    gridSizeU = static_cast<unsigned>(gridSizeF);
    grid.resize(Rows, std::vector<Tile>(Colloms));
    InitializeInformationText();
}

Tile MapManager::getTile()
{
    return tile;
}

void MapManager::generateGrid()
{
    for (size_t x = 0; x < Rows; x++)
    {
        for (size_t y = 0; y < Colloms; y++)
        {
            grid[x][y].setPosition(sf::Vector2f(x * gridSizeF, y * gridSizeF));
        }
    }
}

void MapManager::InitializeInformationText()
{
    textFont.loadFromFile("font/CaviarDreams.ttf");
    gridInformationText.setCharacterSize(40);
    gridInformationText.setFillColor(sf::Color::Red);
    gridInformationText.setFont(textFont);
    gridInformationText.setPosition(20.f, 20.f);

}

std::string MapManager::gridInformation(sf::RenderWindow& window)
{
    mousePosScreen = sf::Mouse::getPosition();
    mousePosWindow = sf::Mouse::getPosition(window);
    mousePosView = window.mapPixelToCoords(mousePosWindow);
    if (mousePosView.x >= 0.f)
        mousePosGrid.x = mousePosView.x / gridSizeU;
    if (mousePosView.y >= 0.f)
        mousePosGrid.y = mousePosView.y / gridSizeU;
    window.setView(window.getDefaultView());

    std::stringstream ss;
    ss << "Screan: " << mousePosScreen.x << " " << mousePosScreen.y << std::endl <<
        "Window: " << mousePosWindow.x << " " << mousePosWindow.y << std::endl <<
        "View: " << mousePosView.x << " " << mousePosView.y << std::endl <<
        "Grid: " << mousePosGrid.x << " " << mousePosGrid.y << std::endl;
    return ss.str();
}

sf::Text& MapManager::getInformationText() 
{
    return gridInformationText;
}

void MapManager::drawGrid(sf::RenderWindow& window)
{
    for (size_t x = 0; x < Rows; x++)
    {
        for (size_t y = 0; y < Colloms; y++)
        {
            window.draw(getGrid()[x][y].getShape());
        }
    }
}

std::vector<std::vector<Tile>>& MapManager::getGrid()
{
    return grid;
}


Engine.h

#pragma once

#include<SFML/Graphics.hpp>
#include "AssetManager.h"
#include "MapManager.h"
#include<memory>

class Engine
{

public:
    Engine();
    void run();

private:
    AssetManager manager;
    std::unique_ptr<sf::RenderWindow> window = std::make_unique<sf::RenderWindow>(sf::VideoMode(1280, 720),
        L"Dark Heresy Tactics", sf::Style::Default);
    MapManager mapManager;
    sf::RectangleShape background = sf::RectangleShape(sf::Vector2f(1280, 720));
    sf::Texture background_texture;

    void input();
    void update(sf::Time const& deltaTime);
    void draw();

};


Engine.cpp

#include "Engine.h"
#include <iostream>

void Engine::input()
{
    
    sf::Event event_play;

    while (window->pollEvent(event_play)) 
    {
        if (event_play.key.code == sf::Keyboard::Escape) { window->close(); }
    }
}

void Engine::update(sf::Time const& deltaTime)
{
    mapManager.getInformationText().setString(mapManager.gridInformation(*window));
}

void Engine::draw()
{
    // Очистка графического окна
    window->clear();
    mapManager.drawGrid(*window);
    window->draw(mapManager.getInformationText());
    window->display();
}

Engine::Engine() 
{
    mapManager.generateGrid();
}

void Engine::run()
{
    
    // Объявление переменной часы
    sf::Clock clock;
    // Цикл работает пока окно открыто
    while (window->isOpen())
    {
        // Текущее время присваиваем переменной времени dt
        sf::Time dt = clock.restart();

        input();
        update(dt);
        draw();
    }
}


main.cpp

#include "Engine.h"
#include "SFML/Graphics.hpp"
#include <iostream>

int main()
{
    Engine engine;
    try
    {
        engine.run();
    }
    catch (std::exception e)
    {
        std::cout << e.what() << std::endl;
    }

        return 0;
}


我不知道如何解决这个问题。

7z5jn7bk

7z5jn7bk1#

我知道问题出在哪。我有:

while (window->pollEvent(event_play)) 
{
    if (event_play.key.code == sf::Keyboard::Escape) { window->close(); } }
}

字符串
当我将这个字符串改为下面的字符串时,一切都很好:

while (window->pollEvent(event_play)) 
{
    if (event_play.type == sf::Event::Closed) { window->close(); }
}

相关问题