sfml 相关问题

SFML(简单快速多媒体库)是一种用C ++编写的便携且易于使用的多媒体API。您可以将其视为SDL的现代,面向对象的替代方案。 SFML由几个软件包组成,完全满足您的需求。您可以将SFML用作与OpenGL交互的最小窗口系统,或者作为用于构建游戏或交互式程序的全功能多媒体库。

collect2.exe:错误:ld 返回 1 退出状态

有一个带有 SFML 的简单 C++ 脚本。当我尝试编译它时,编译器出现一些错误。 请注意,代码编辑器中不会显示与脚本相关的错误。 脚本: #包括<...

回答 1 投票 0

显示一个白框而不是精灵(C++、SFML)

我最初将精灵纹理代码放在主cpp中并且它正在工作,我在将其迁移到玩家类后遇到了这个问题。 目前只有方向仍然是精灵,...

回答 1 投票 0

如何在SFML中查找矩形形状的坐标?

我想使用矩形在屏幕上绘制一条连续的线。我修改了矩形的厚度,所以它看起来像一条线,而不是矩形。现在,既然我...

回答 2 投票 0

如何将 SFML 库与 CMake 链接?

我正在尝试在 Ubuntu 上使用 SFML 库创建一个 C++ 项目(使用 VSCode,如果重要的话)。 过去,我曾经使用以下命令编译 SFML: g++ main.o -o sfml-app -lsfml-graphics -ls...

回答 2 投票 0

如何将 SFML 库链接到 cmake?

我正在尝试在 Ubuntu 上使用 sfml 库创建一个 C++ 项目(使用 vscode,如果有的话)。 过去,我曾经使用以下方法编译 sfml: g++ main.o -o sfml-app -lsfml-graphics -lsfml-win...

回答 1 投票 0

MouseMoved 事件后将 sf::Sprite 移动到新位置的问题

我正在使用 C++ 中的 SFML 开发国际象棋游戏,并且遇到了拖放精灵的问题。我实现了一个功能,允许玩家通过点击和拖动来移动棋子......

回答 1 投票 0

包含来自不同项目的标头时无法打开源文件 SFML/Graphics.hpp

这个项目就是一个游戏引擎。然而,对于这方面,我尝试添加一个使用 SFML 来显示图形的开发工具。我已成功链接 SFML 以使其正常工作...

回答 1 投票 0

从(整数到字符串)显示为点的文本的 SFML 转换

#包括 #包括 #包括 使用命名空间 std; 结构帕特拉{ 长双问候语; //大量的 sf::RectangleShape 形状; 长...

回答 1 投票 0

有人能找出 CHESS 的 sfml 代码有什么问题吗

抱歉写了糟糕的代码,我是 cpp 新手 所以问题是无论我做什么,精灵位置都不会改变。我无法弄清楚。 #包括\ #包括\ 抱歉写了糟糕的代码,我是 cpp 新手 所以问题是无论我做什么,精灵位置都没有改变。我无法弄清楚。 #include \<SFML/Graphics.hpp\> #include \<vector\> #include \<string\> #include\<iostream\> #include \<unistd.h\> int main() { // Create a window sf::RenderWindow window(sf::VideoMode(800, 800), "Chess by devtony"); sf::RectangleShape board\[8\]\[8\]; sf::Color blck(128,128,128); sf::Color wth(255,204,204); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { board[i][j].setPosition(i*100,j*100); board[i][j].setSize(sf::Vector2f(100, 100)); board[i][j].setFillColor((i + j) % 2 == 0 ? wth : blck); } } sf::Texture pieces\[4\]\[8\]; for (int i = 0; i \< 8; i++) { if (!pieces\[1\]\[i\].loadFromFile("assets/w_pawn.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } } //rooks for (int i = 0; i \< 8; i++) { if (!pieces\[2\]\[i\].loadFromFile("assets/b_pawn.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } } if (!pieces\[0\]\[0\].loadFromFile("assets/w_rook.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[0\]\[7\].loadFromFile("assets/w_rook.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[0\].loadFromFile("assets/b_rook.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[7\].loadFromFile("assets/b_rook.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } //bishop if (!pieces\[0\]\[2\].loadFromFile("assets/w_bishop.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[0\]\[5\].loadFromFile("assets/w_bishop.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[2\].loadFromFile("assets/b_bishop.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[5\].loadFromFile("assets/b_bishop.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } //horse if (!pieces\[0\]\[1\].loadFromFile("assets/w_knight.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[0\]\[6\].loadFromFile("assets/w_knight.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[1\].loadFromFile("assets/b_knight.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[6\].loadFromFile("assets/b_knight.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } //queen if (!pieces\[0\]\[4\].loadFromFile("assets/w_queen.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[0\]\[3\].loadFromFile("assets/w_king.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[3\].loadFromFile("assets/b_queen.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } if (!pieces\[3\]\[4\].loadFromFile("assets/b_king.png")) { std::cout \<\< "Error loading image!" \<\< std::endl; return EXIT_FAILURE; } //king // Create a sprite sf::Sprite sprites\[4\]\[8\]; for (int i = 0; i \< 4; i++) { for (int j = 0; j \< 8; j++) { sprites\[i\]\[j\].setTexture(pieces\[i\]\[j\]); sprites\[i\]\[j\].setScale(0.5,0.5); sprites\[i\]\[j\].setOrigin(50,50); } } // Flag to check if a piece is selected bool isPieceSelected = false; sf::Sprite\* selectedPiece = nullptr; sf::Vector2f clickOffset; // Main loop while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { if (sprites[i][j].getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) { isPieceSelected = true; selectedPiece = &sprites[i][j]; clickOffset = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - selectedPiece->getPosition(); break; } } if (isPieceSelected) break; } } else if (event.type == sf::Event::MouseMoved && isPieceSelected) { sf::Vector2f newPosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - clickOffset; std::cout \<\< "New Position: " \<\< newPosition.x \<\< ", " \<\< newPosition.y \<\< std::endl; selectedPiece-\>setPosition(newPosition.x,newPosition.y); } else if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) { isPieceSelected = false; } } // Clear window window.clear(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { window.draw(board[i][j]); } } for (int i = 0; i < 8; i++) { for (int j = 0; j < 4; j++) { if (j<2) { sprites[j][i].setPosition(sf::Vector2f(100*(i+1)-50,100*(j+1)-50)); }else{ sprites[j][i].setPosition(sf::Vector2f(100*(i+1)-50,100*(j+5)-50)); } } } for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { window.draw(sprites[i][j]); } } // Draw the sprite // Display window contents window.display(); } return 0; } 我正在使用 C++ 中的 SFML 开发国际象棋游戏,并且遇到了拖放精灵的问题。我实现了一项功能,允许玩家通过在棋盘上单击并拖动棋子来移动棋子。然而,精灵并没有像预期那样随着鼠标拖动而移动。 单击时会正确检测到精灵,并且 isPieceSelected 标志设置为 true。计算 clickOffset 是为了保持相对于鼠标光标的正确位置。但是,当我尝试拖动精灵时,它不跟随鼠标。窗口已聚焦,并且没有设置可能影响响应能力的帧速率限制。 我尝试过使用 setPosition 和 move 方法来更新精灵的位置,但都没有解决问题。尽管没有错误,但精灵根本没有移动,并且程序编译成功。 您的问题是您在窗口循环的每次迭代中将棋子的位置设置在其起始位置。这发生在循环中: for (int i = 0; i < 8; i++) { for (int j = 0; j < 4; j++) { if (j < 2) { sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 1) - 50)); } else { sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 5) - 50)); } } } 这可以通过引入一个初始化函数来轻松解决,该函数接受精灵矩阵并仅设置其原始位置一次。 创建一个这样的函数: void setPiecesInStartingPosition(sf::Sprite(&sprites)[4][8]) 并将初始化代码移至函数中,即: for (int i = 0; i < 8; i++) { for (int j = 0; j < 4; j++) { if (j < 2) { sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 1) - 50)); } else { sprites[j][i].setPosition(sf::Vector2f(100 * (i + 1) - 50, 100 * (j + 5) - 50)); } } } 最后,确保只在开始 while 循环之前的某个地方调用它一次。也许在创建精灵矩阵之后立即是一个好主意。 另外,我建议删除break语句并将布尔检查移到for循环上方 for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { if (sprites[i][j].getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) { isPieceSelected = true; selectedPiece = &sprites[i][j]; clickOffset = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - selectedPiece->getPosition(); break; } } if (isPieceSelected) break; 所以像这样: if (!isPieceSelected) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 8; j++) { if (sprites[i][j].getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) { isPieceSelected = true; selectedPiece = &sprites[i][j]; clickOffset = window.mapPixelToCoords(sf::Mouse::getPosition(window)) - selectedPiece->getPosition(); break; } } } }

回答 1 投票 0

SFML 麻烦 if(font.loadFromFile(FONT_PATH)) -> 预期声明

sf::Font 字体 const std::string FONT_PATH = "$(SolutionDir) esources\Fonts\Oswald-Regular.ttf"; if (!font.loadFromFile(FONT_PATH)) 返回-1; ... if 语句有错误......

回答 1 投票 0

有没有办法配置 VS 代码以直接使用“运行代码”按钮使用 SFML 库?

我一直在尝试将 SFML 库与 Visual Studio Code 和 C/C++ v1.19.9 Microsoft 扩展一起使用。我的目标是配置 Visual Studio Code 以直接从

回答 1 投票 0

使用 SFML 绘制函数

我是 SFML 新手。我在 Google 上搜索,找到了一种从方程在 SFML 中绘制多个点的方法。例如,我想绘制 200 个点 (x,y),使得 y = 2x,范围为 (-10 < x < 10). ...

回答 2 投票 0

SFML/C++ 为什么我的将敌人移向玩家的方法在玩家移动时停止工作?

在其他地方使用的唯一其他代码是 Player::getPosition(),它只是返回playerSprite.getPosition() 和 Game::update 将 Player::getPosition() 传递给此函数...

回答 1 投票 0

如何使用 SFML 和 C++ 渲染贪吃蛇游戏的精灵

游戏运行良好,但它使用矩形形状,我想使用精灵渲染蛇部分。这是代码(是的,它不是那么干净,我只是想让它工作): #包括 游戏运行良好,但它使用矩形形状,我想使用精灵渲染蛇部分。这是代码(是的,它不是那么干净,我只是想让它工作): #include <SFML/Graphics.hpp> // Graphics module #include <SFML/Audio.hpp> // Sound module #include <iostream> // Basic input/output (for errors) #include <vector> // Dynamic arrays (for snake body) #include <random> // For random food positions class Game { private: sf::Vector2u windowSize; // Size of the game window int a, b; // Length and width of a block sf::RenderWindow window; // Window object for drawing sf::Font font; // Text font sf::Clock clock; // For time measurement std::vector<sf::Vector2i> body; // Snake body (segments as coordinates) sf::Vector2i food; // Food position sf::Vector2i direction; // Snake direction int score; // Player's score bool gameOver; // Game state: finished or not int n, m; // Number of rows and columns float delay, timer; // Update delay and elapsed time sf::Music eat; // Sound effect for eating public: Game(unsigned short x, unsigned short y); // Constructor void start(); // Start the game private: void loop(); // Main game loop void events(); // Process events (keyboard, etc.) void update(); // Update game logic void render(); // Render elements on screen void gameOverScreen(); // Show "Game Over" screen sf::Vector2i getFoodPosition(); // Generate new food position }; int WinMain() { Game game(800, 600); game.start(); } int main() { Game game(800, 600); // Create object with window size 800x600 game.start(); // Call the start function } Game::Game(uint16_t x, uint16_t y) { windowSize = { x, y }; // Save window width and height window.create(sf::VideoMode(windowSize.x, windowSize.y, 1), "Snake"); // Create the window a = 50; // Set block width b = 50; // Set block height n = windowSize.x / a; // Calculate number of horizontal blocks m = windowSize.y / b; // Calculate number of vertical blocks font.loadFromFile("resources/Fonts/sfpro_bold.OTF"); // Load font for text eat.openFromFile("resources/Audio/eating_apple.mp3"); } void Game::start() { body.clear(); // Clear the snake body body.push_back({ 5,3 }); // Head body.push_back({ 4,3 }); // Body segment body.push_back({ 3,3 }); // Tail direction = { 0, 0 }; // Direction food = { getFoodPosition() }; // Initial food position gameOver = false; // Game not over (false) score = 0; // Start score from 0 loop(); // Main loop } void Game::loop() { timer = 0.f; // Accumulated time delay = 0.125f; // Game update delay while (window.isOpen()) { events(); // Handle user inputs (keyboard and mouse) timer += clock.getElapsedTime().asSeconds(); // Add elapsed time in seconds to the timer if (timer > delay) { update(); // Update the game (move snake, etc.) render(); // Render the screen (blocks, snake, food, text, etc.) timer = 0; // Reset timer } clock.restart(); // Restart the clock for the next cycle } } void Game::events() { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Escape) window.close(); else if (event.key.code == sf::Keyboard::Up && (direction.y != 1)) direction = { 0, -1 }; else if (event.key.code == sf::Keyboard::Down && (direction.y != -1)) direction = { 0, 1 }; else if (event.key.code == sf::Keyboard::Left && (direction.x != 1)) direction = { -1, 0 }; else if (event.key.code == sf::Keyboard::Right && (direction.x != -1)) direction = { 1, 0 }; else if (event.key.code == sf::Keyboard::R) /*if (gameOver)*/ start(); } } } void Game::update() { if (gameOver || direction == sf::Vector2i{ 0,0 }) return; sf::Vector2i newHead = body[0] + direction; for (size_t i = 1; i < body.size(); i++) { if (newHead == body[i]) { gameOver = true; return; } } if (newHead.x > n - 1 || newHead.x < 0 || newHead.y > m - 1 || newHead.y < 0) { gameOver = true; return; } if (newHead == food) { body.insert(body.begin(), newHead); food = getFoodPosition(); score++; eat.play(); } else { body.insert(body.begin(), newHead); body.pop_back(); } } void Game::render() { window.clear(); sf::RectangleShape block(sf::Vector2f(a, b)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { block.setPosition(i * a, j * b); int p = (i + j) % 2; block.setFillColor(sf::Color(172 - p * 7, 214 - p * 7, 67 - p * 7, 255)); window.draw(block); } } block.setPosition(food.x * a, food.y * b); block.setFillColor(sf::Color::Red); window.draw(block); for (int i = 0; i < body.size(); i++) { if (i == 0) block.setFillColor(sf::Color::Magenta); else block.setFillColor(sf::Color(0, 71, 181)); block.setPosition(body[i].x * a, body[i].y * b); window.draw(block); } sf::Text scr("Score: " + std::to_string(score), font, 32); scr.setFillColor(sf::Color::White); scr.setPosition(4, 0); window.draw(scr); if (gameOver) gameOverScreen(); window.display(); } void Game::gameOverScreen() { eat.stop(); sf::RectangleShape screen({ float(windowSize.x), float(windowSize.y) }); screen.setPosition({ 0,0 }); screen.setFillColor(sf::Color(0, 0, 0, 100)); sf::Text gameOverText(" Game over!\nPress R to restart", font, 38); gameOverText.setFillColor(sf::Color::White); gameOverText.setPosition((windowSize.x / 2) - 150, (windowSize.y / 2) - 20); window.draw(screen); window.draw(gameOverText); } sf::Vector2i Game::getFoodPosition() { std::random_device randomDevice; std::mt19937 randomNumberGenerator(randomDevice()); std::uniform_int_distribution<int> distX(0, n - 1); std::uniform_int_distribution<int> distY(0, m - 1); sf::Vector2i position; do { position.x = distX(randomNumberGenerator); position.y = distY(randomNumberGenerator); if (std::find(body.begin(), body.end(), position) == body.end()) { return position; } } while (true); } 上面的代码目前没有精灵功能,但这是我尝试过的一种方法(适用于渲染精灵):创建纹理和精灵的地图,从文件加载纹理,将它们映射到精灵,然后我准备好了一张精灵地图。这部分并不难,我的渲染逻辑很可能存在缺陷。代码是从我拥有精灵逻辑时获取的,我基本上使用字符串来确定我应该加载哪个精灵。 sf::Vector2i prev = body[i + 1]; // sf::Vector2i next = body[i - 1]; //because i pop the tail and insert new part at the beginning if (next.x == prev.x) { spriteName = "body_vertical"; //vertical sprite | } else if (next.y == prev.y) { spriteName = "body_horizontal"; // horizontal -- } else { if (prev.x < next.x) { if (prev.y > next.y) spriteName = "body_topright"; // start top curve right downwards else spriteName = "body_bottomleft"; // bottom -> left } else if (prev.y < next.y) { spriteName = "body_topleft"; // top -> left else spriteName = "body_bottomright"; // bottom -> right } } else{ if (prev.y > next.y) { spriteName = "body_topleft"; // top -> left else spriteName = "body_bottomright"; // bottom -> right } else if (prev.y < next.y) { spriteName = "body_topright"; // top -> right else spriteName = "body_bottomleft"; // bottom -> left } } } 精灵链接:opengameart.org/content/snake-game-assets。基本上,我想要的是一些关于如何选择要加载的正确精灵以及在哪种情况下加载的反馈(头部和尾部也将受到赞赏🥹)。谢谢! 这是一个稍微干净一点的代码, // choosing is based on the assumption // that the head is the last element in the body array // if not, you can simply reverse the conditions if (next == /*last in body*/) { spriteName == "head_sprite"; } else if (next == /*first in body*/) { spriteName = "tail_sprite"; } else if (next.x == prev.x || next.y == prev.y) { spriteName == "body_sprite"; } else { spriteName = "curved_sprite"; } // now rotate and reverse if (prev.x == next.x) { // rotate 90deg } else if (prev.x > next.x) { // reverse horizontally } if (prev.y > next.y) { // reverse vertically } 旋转/反转精灵比根据每种情况选择单独的精灵要方便得多。 我从您附加的那些精灵中选择了snake_graphics.zip [“head_down”,“tail_down”,“body_vertical”,“body_bottomright”] 并将它们重命名为 [“head_sprite”、“tail_sprite”、“body_sprite”、“curved_sprite”] 分别。 我可能错误地旋转/反转了精灵 因为我不确定上一个还是下一个是当前的(“假设上一个是”),但这就是基本思想

回答 1 投票 0

如何检查片段是否位于纹理的边缘

我目前正在尝试用glsl(使用SFML)编写一个简单的轮廓着色器。我是着色器编程的新手,所以如果问题很明显,请原谅我。 无论如何,着色器可以很好地检测边缘

回答 1 投票 0

SFML draw() 未绘制第一个对象形状

我正在用 C++ 和 SFML 创建物理模拟引擎。我遇到了问题,我想绘制对象向量,但是绘制不会从向量中绘制第一个对象。如果我想画下一个

回答 1 投票 0

VS2022 C++ 音频文件的空目录。 CMake; SFML

我完全按照说明做了所有事情,结果和往常一样,我阅读了文档,没有指定这个错误,在控制台中引号中应该是au的目录...

回答 1 投票 0

target_include_directories 语法?

节目3 ├── CMakeLists.txt ├── CMakePresets.json ├── CPP ├── Program3.cpp // 这是我正在尝试运行的示例代码 ├── Program3.txt // 这是预制的.cpp 文件...

回答 1 投票 0

sf::Event 类未定义,但 SFML 应该已定义它

最近我一直在尝试 C++ 和 SFML,因为它似乎非常适合我想要做的项目,但是,我什至编译用于检查我的示例代码都遇到了困难

回答 2 投票 0

在 SFML 项目中加载字体时出现 Valgrind 错误

这段代码: // g++ sfml.cpp -o sfml_test -lsfml-graphics -lsfml-window -lsfml-system #包括 #包括 int main() { sf::字体字体; 如果(!字体。

回答 1 投票 0

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