本文介绍了我无法使用SFML库打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法打开test15.bmp.当我运行该程序时,它尝试打开一个窗口,但崩溃了.据我所知,问题线是image.loadFromFile(...);

I am having trouble opening the test15.bmp. When I run the program it tries to open a window but crashes. The problem line as much as I could understand is image.loadFromFile(...);

Exception thrown at 0x50E82CC4 (vcruntime140.dll) in
MandelbrotProject.exe: 0xC0000005: Access violation reading location
0x00741000.

这是我使用image.loadFromFile("test15.bmp")而不是绝对路径时遇到的异常.

This is the exception I get when I use image.loadFromFile("test15.bmp") instead of the absolute path.

我已经链接并包含了SFML教程中描述的所有内容. https://www.sfml-dev.org/tutorials/2.5/start-vc.php

I have linked and included everything as it was described in the SFML tutorials. https://www.sfml-dev.org/tutorials/2.5/start-vc.php

我正在使用Visual C ++(2017)-32位SFML和Microsoft Visual Studio 2017.

I am using Visual C++(2017)-32 bit SFML and Microsoft Visual Studio 2017.

#include "pch.h"
#include<iostream>
#include "Fractal.h"
#include "RGB.h"
#include "Zoom.h"
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf::Glsl;
using namespace project;

int main() {

    Fractal fractal(800, 600);

    fractal.addRange(0.0, RGB(0, 0, 0));
    fractal.addRange(0.3, RGB(255, 0, 0));
    fractal.addRange(0.5, RGB(255, 255, 0));
    fractal.addRange(1.0, RGB(255, 255, 255));
    fractal.addZoom(Zoom(295, 202, 0.1));
    fractal.addZoom(Zoom(312, 304, 0.1));//specified zooms
    fractal.run("test15.bmp");

    cout << "Zooming!" << endl;
    double zoom = 0.1;
    double offsetX = 400.0;
    double offsetY = 300.0;

    sf::RenderWindow window(sf::VideoMode(800, 600), "Mandelbrot");
    window.setFramerateLimit(0);
    sf::Image image;
    image.loadFromFile("D:\Visual Studio
projects\MandelbrotProject\MandelbrotProject\test15.bmp");

    if (!image.loadFromFile("test15.bmp")) { return -1; }
    sf::Texture texture;
    sf::Sprite sprite;

    bool stateChanged = true;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::KeyPressed:
                stateChanged = true; 
                switch (event.key.code) {
                case sf::Keyboard::Escape:
                    stateChanged = false;
                    window.close();
                    break;
                case sf::Keyboard::Equal:
                    zoom *= 0.9;
                    break;
                case sf::Keyboard::Dash:
                    zoom /= 0.9;
                    break;
                case sf::Keyboard::W:
                    offsetY -= 40 * zoom;
                    break;
                case sf::Keyboard::S:
                    offsetY += 40 * zoom;
                    break;
                case sf::Keyboard::A:
                    offsetX -= 40 * zoom;
                    break;
                case sf::Keyboard::D:
                    offsetX += 40 * zoom;
                    break;
                default:stateChanged = false;
                    break;
                }
            default:
                break;
            }
        }
        if (stateChanged) { 
            fractal.addZoom(Zoom(offsetX, offsetY, zoom));
            fractal.run("test15.bmp");
            texture.loadFromFile("test15.bmp");
            sprite.setTexture(texture);
        }
        window.clear();
        window.draw(sprite);
        window.display();
        stateChanged = false;
    }
    cout << "Finished!" << endl;
    cin.get();
    return 0;
}

推荐答案

默认情况下,Visual Studio将项目目录视为工作目录.

By default, Visual Studio consider project directory the working directory.

如果我没记错的话,请按您的情况D:\Visual Studio projects\MandelbrotProject\MandelbrotProject\.您应该确保您的test15.bmp在该目录中.

If I'm not wrong, D:\Visual Studio projects\MandelbrotProject\MandelbrotProject\ in your case. You should ensure your test15.bmp is inside that directory.

以防万一,您可以通过打开 Project Properties-> Configuration Properties-> Output Directory (可以使用任何字段)来检查哪个是您的$(ProjectDir).点击下拉箭头,然后点击编辑.打开并输入$(ProjectDir)

Just in case, you can check which one is your $(ProjectDir) by opening Project Properties->Configuration Properties->Output Directory (any field can be used). Click on the drop-down arrow and Edit. Open Macros and write $(ProjectDir)

有关 VS宏

这篇关于我无法使用SFML库打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:19