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

3htmauhk  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(153)

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

  1. #pragma once
  2. #include "SFML/Graphics.hpp"
  3. #include "AssetManager.h"
  4. class Tile
  5. {
  6. public:
  7. Tile();
  8. Tile(float size, sf::Texture texture);
  9. void setPosition(sf::Vector2f position);
  10. sf::Vector2f getPosition() const;
  11. void setTexture(std::string& filename);
  12. float getTileSize();
  13. void setShape(); // set the rectangle shape parametrs
  14. sf::RectangleShape& getShape();
  15. private:
  16. float tileSize = 0;
  17. sf::Vector2f tilePosition;
  18. sf::Texture tileTexture;
  19. sf::RectangleShape tileShape;
  20. };

字符串
tile.cpp

  1. #include "Tile.h"
  2. Tile::Tile()
  3. {
  4. setShape();
  5. }
  6. Tile::Tile(float size, sf::Texture texture) : tileSize(size), tileTexture(texture)
  7. {
  8. tileShape.setTexture(&tileTexture);
  9. tileShape.setOutlineThickness(1.f);
  10. tileShape.setOutlineColor(sf::Color::Black);
  11. }
  12. void Tile::setShape()
  13. {
  14. if(tileSize == 0) tileSize = 64.f;
  15. tileShape.setSize(sf::Vector2f(tileSize,tileSize));
  16. tileShape.setFillColor(sf::Color::White);
  17. tileShape.setOutlineThickness(1.f);
  18. tileShape.setOutlineColor(sf::Color::Black);
  19. }
  20. void Tile::setPosition(sf::Vector2f position)
  21. {
  22. tilePosition = position;
  23. tileShape.setPosition(tilePosition.x, tilePosition.y);
  24. }
  25. sf::Vector2f Tile::getPosition() const
  26. {
  27. return tilePosition;
  28. }
  29. void Tile::setTexture(std::string& filename)
  30. {
  31. tileShape.setTexture(&AssetManager::GetTexture(filename));
  32. }
  33. float Tile::getTileSize()
  34. {
  35. return tileSize;
  36. }
  37. sf::RectangleShape& Tile::getShape()
  38. {
  39. return tileShape;
  40. }


MapManager. h//网格生成器类

  1. #pragma once
  2. #include "SFML/Graphics.hpp"
  3. #include "Tile.h"
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. class AssetManager;
  8. class MapManager
  9. {
  10. public:
  11. MapManager();
  12. Tile getTile();
  13. void generateGrid();
  14. void InitializeInformationText();//Initialize sf::Text gridInformationText
  15. std::string gridInformation(sf::RenderWindow& window); // Make information obaut screen
  16. sf::Text& getInformationText();
  17. void drawGrid(sf::RenderWindow& window);
  18. std::vector<std::vector<Tile>>& getGrid();
  19. private:
  20. size_t Rows = 10;
  21. size_t Colloms = 10;
  22. float gridSizeF = 0; // size of one cell of grid
  23. unsigned gridSizeU = 0;
  24. Tile tile;
  25. sf::Text gridInformationText;
  26. sf::Font textFont;
  27. sf::Vector2i mousePosScreen; // position of mause on the screen
  28. sf::Vector2i mousePosWindow; // mouse position on window
  29. sf::Vector2f mousePosView; // mouse position on view
  30. sf::Vector2u mousePosGrid; // moues position on grid
  31. std::vector<std::vector<Tile>> grid;
  32. };


MapManager.cpp

  1. #include "MapManager.h"
  2. #include <iostream>
  3. MapManager::MapManager()
  4. {
  5. std::cout << "Map manager initialized\n";
  6. gridSizeF = tile.getTileSize();
  7. gridSizeU = static_cast<unsigned>(gridSizeF);
  8. grid.resize(Rows, std::vector<Tile>(Colloms));
  9. InitializeInformationText();
  10. }
  11. Tile MapManager::getTile()
  12. {
  13. return tile;
  14. }
  15. void MapManager::generateGrid()
  16. {
  17. for (size_t x = 0; x < Rows; x++)
  18. {
  19. for (size_t y = 0; y < Colloms; y++)
  20. {
  21. grid[x][y].setPosition(sf::Vector2f(x * gridSizeF, y * gridSizeF));
  22. }
  23. }
  24. }
  25. void MapManager::InitializeInformationText()
  26. {
  27. textFont.loadFromFile("font/CaviarDreams.ttf");
  28. gridInformationText.setCharacterSize(40);
  29. gridInformationText.setFillColor(sf::Color::Red);
  30. gridInformationText.setFont(textFont);
  31. gridInformationText.setPosition(20.f, 20.f);
  32. }
  33. std::string MapManager::gridInformation(sf::RenderWindow& window)
  34. {
  35. mousePosScreen = sf::Mouse::getPosition();
  36. mousePosWindow = sf::Mouse::getPosition(window);
  37. mousePosView = window.mapPixelToCoords(mousePosWindow);
  38. if (mousePosView.x >= 0.f)
  39. mousePosGrid.x = mousePosView.x / gridSizeU;
  40. if (mousePosView.y >= 0.f)
  41. mousePosGrid.y = mousePosView.y / gridSizeU;
  42. window.setView(window.getDefaultView());
  43. std::stringstream ss;
  44. ss << "Screan: " << mousePosScreen.x << " " << mousePosScreen.y << std::endl <<
  45. "Window: " << mousePosWindow.x << " " << mousePosWindow.y << std::endl <<
  46. "View: " << mousePosView.x << " " << mousePosView.y << std::endl <<
  47. "Grid: " << mousePosGrid.x << " " << mousePosGrid.y << std::endl;
  48. return ss.str();
  49. }
  50. sf::Text& MapManager::getInformationText()
  51. {
  52. return gridInformationText;
  53. }
  54. void MapManager::drawGrid(sf::RenderWindow& window)
  55. {
  56. for (size_t x = 0; x < Rows; x++)
  57. {
  58. for (size_t y = 0; y < Colloms; y++)
  59. {
  60. window.draw(getGrid()[x][y].getShape());
  61. }
  62. }
  63. }
  64. std::vector<std::vector<Tile>>& MapManager::getGrid()
  65. {
  66. return grid;
  67. }


Engine.h

  1. #pragma once
  2. #include<SFML/Graphics.hpp>
  3. #include "AssetManager.h"
  4. #include "MapManager.h"
  5. #include<memory>
  6. class Engine
  7. {
  8. public:
  9. Engine();
  10. void run();
  11. private:
  12. AssetManager manager;
  13. std::unique_ptr<sf::RenderWindow> window = std::make_unique<sf::RenderWindow>(sf::VideoMode(1280, 720),
  14. L"Dark Heresy Tactics", sf::Style::Default);
  15. MapManager mapManager;
  16. sf::RectangleShape background = sf::RectangleShape(sf::Vector2f(1280, 720));
  17. sf::Texture background_texture;
  18. void input();
  19. void update(sf::Time const& deltaTime);
  20. void draw();
  21. };


Engine.cpp

  1. #include "Engine.h"
  2. #include <iostream>
  3. void Engine::input()
  4. {
  5. sf::Event event_play;
  6. while (window->pollEvent(event_play))
  7. {
  8. if (event_play.key.code == sf::Keyboard::Escape) { window->close(); }
  9. }
  10. }
  11. void Engine::update(sf::Time const& deltaTime)
  12. {
  13. mapManager.getInformationText().setString(mapManager.gridInformation(*window));
  14. }
  15. void Engine::draw()
  16. {
  17. // Очистка графического окна
  18. window->clear();
  19. mapManager.drawGrid(*window);
  20. window->draw(mapManager.getInformationText());
  21. window->display();
  22. }
  23. Engine::Engine()
  24. {
  25. mapManager.generateGrid();
  26. }
  27. void Engine::run()
  28. {
  29. // Объявление переменной часы
  30. sf::Clock clock;
  31. // Цикл работает пока окно открыто
  32. while (window->isOpen())
  33. {
  34. // Текущее время присваиваем переменной времени dt
  35. sf::Time dt = clock.restart();
  36. input();
  37. update(dt);
  38. draw();
  39. }
  40. }


main.cpp

  1. #include "Engine.h"
  2. #include "SFML/Graphics.hpp"
  3. #include <iostream>
  4. int main()
  5. {
  6. Engine engine;
  7. try
  8. {
  9. engine.run();
  10. }
  11. catch (std::exception e)
  12. {
  13. std::cout << e.what() << std::endl;
  14. }
  15. return 0;
  16. }


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

7z5jn7bk

7z5jn7bk1#

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

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

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

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

相关问题