Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

c++ - I can not open and image using the SFML library

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.

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

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

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
projectsMandelbrotProjectMandelbrotProjectest15.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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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)

More info about VS macros


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...