无法使用SFML库加载图像

发布于 2025-01-20 17:51:44 字数 1985 浏览 3 评论 0原文

我刚刚成功将 SFML 库链接到我的 Visual Studio 2019。我正在尝试运行一个简单的 C++ 游戏扫雷。它编译后,没有给出错误消息,但在运行时窗口中给出了奇怪的意外结果。说加载图片失败。请帮忙。这是错误消息的图片,请参阅下面的游戏代码。

无法加载图片图片

#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

int main()
{
srand(time(0));

RenderWindow app(VideoMode(400, 400), "Minesweeper!");

int w = 32;
int grid[12][12];
int sgrid[12][12]; //for showing

Texture t;
t.loadFromFile("images/tiles.jpg");
Sprite s(t);

for (int i = 1; i <= 10; i++)
    for (int j = 1; j <= 10; j++)
    {
        sgrid[i][j] = 10;
        if (rand() % 5 == 0)  grid[i][j] = 9;
        else grid[i][j] = 0;
    }

for (int i = 1; i <= 10; i++)
    for (int j = 1; j <= 10; j++)
    {
        int n = 0;
        if (grid[i][j] == 9) continue;
        if (grid[i + 1][j] == 9) n++;
        if (grid[i][j + 1] == 9) n++;
        if (grid[i - 1][j] == 9) n++;
        if (grid[i][j - 1] == 9) n++;
        if (grid[i + 1][j + 1] == 9) n++;
        if (grid[i - 1][j - 1] == 9) n++;
        if (grid[i - 1][j + 1] == 9) n++;
        if (grid[i + 1][j - 1] == 9) n++;
        grid[i][j] = n;
    }

while (app.isOpen())
{
    Vector2i pos = Mouse::getPosition(app);
    int x = pos.x / w;
    int y = pos.y / w;

    Event e;
    while (app.pollEvent(e))
    {
        if (e.type == Event::Closed)
            app.close();

        if (e.type == Event::MouseButtonPressed)
            if (e.key.code == Mouse::Left) sgrid[x][y] = grid[x][y];
            else if (e.key.code == Mouse::Right) sgrid[x][y] = 11;
    }

    app.clear(Color::White);
    for (int i = 1; i <= 10; i++)
        for (int j = 1; j <= 10; j++)
        {
            if (sgrid[x][y] == 9) sgrid[i][j] = grid[i][j];
            s.setTextureRect(IntRect(sgrid[i][j] * w, 0, w, w));
            s.setPosition(i * w, j * w);
            app.draw(s);
        }

    app.display();
}

return 0;

}

I have just successfully linked the SFML library to my Visual Studio 2019. I am trying to run a simple C++ game Minesweeper. It compiles, gives no error message, but gives weird unexpected results in the runtime window. It says failed to load images. Please help. This is a pic of the error message, and see code for game below.

Failed to load images pic

#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

int main()
{
srand(time(0));

RenderWindow app(VideoMode(400, 400), "Minesweeper!");

int w = 32;
int grid[12][12];
int sgrid[12][12]; //for showing

Texture t;
t.loadFromFile("images/tiles.jpg");
Sprite s(t);

for (int i = 1; i <= 10; i++)
    for (int j = 1; j <= 10; j++)
    {
        sgrid[i][j] = 10;
        if (rand() % 5 == 0)  grid[i][j] = 9;
        else grid[i][j] = 0;
    }

for (int i = 1; i <= 10; i++)
    for (int j = 1; j <= 10; j++)
    {
        int n = 0;
        if (grid[i][j] == 9) continue;
        if (grid[i + 1][j] == 9) n++;
        if (grid[i][j + 1] == 9) n++;
        if (grid[i - 1][j] == 9) n++;
        if (grid[i][j - 1] == 9) n++;
        if (grid[i + 1][j + 1] == 9) n++;
        if (grid[i - 1][j - 1] == 9) n++;
        if (grid[i - 1][j + 1] == 9) n++;
        if (grid[i + 1][j - 1] == 9) n++;
        grid[i][j] = n;
    }

while (app.isOpen())
{
    Vector2i pos = Mouse::getPosition(app);
    int x = pos.x / w;
    int y = pos.y / w;

    Event e;
    while (app.pollEvent(e))
    {
        if (e.type == Event::Closed)
            app.close();

        if (e.type == Event::MouseButtonPressed)
            if (e.key.code == Mouse::Left) sgrid[x][y] = grid[x][y];
            else if (e.key.code == Mouse::Right) sgrid[x][y] = 11;
    }

    app.clear(Color::White);
    for (int i = 1; i <= 10; i++)
        for (int j = 1; j <= 10; j++)
        {
            if (sgrid[x][y] == 9) sgrid[i][j] = grid[i][j];
            s.setTextureRect(IntRect(sgrid[i][j] * w, 0, w, w));
            s.setPosition(i * w, j * w);
            app.draw(s);
        }

    app.display();
}

return 0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文