SFML 应用程序中的窗口自行关闭并显示代码 0

问题描述 投票:0回答:1

我尝试制作回合制游戏。现在我尝试制作网格。我在屏幕上输出带有有关网格信息的文本。但过了一段时间后,窗口继续关闭。也许我做错了什么?我的代码如下。

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

瓷砖.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;

};


地图管理器.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;
}

引擎.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();


};

引擎.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();
    }
}

主.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;
}

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

c++ sfml
1个回答
0
投票

我明白出了什么问题。我有

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

当我为此更改此字符串时:

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

一切都很好。

© www.soinside.com 2019 - 2024. All rights reserved.