-3

试图只做一个简单的程序来显示加载到系统中的图片。如果我输入一个在目录中找不到的图像名称,它会返回并且错误说它还没有找到,但是在此之后当我返回 0 时出于某种原因;在主要内部,我崩溃了。

我找不到解决它的方法。对我做错了什么有帮助吗?

#include "SDL_Wrapper.h"
#include <iostream>
#include <string>

using namespace std;

class Image {
public:
    Image();
    ~Image();
    int loadImage();
    int saveImage();
    int outputImage();
protected:
    int height, width;
    string imageName;
    SDL_Surface* displayWindow;
    SDL_Surface* imageSurface;
    SDL_Surface* tempSurface;
};

Image::Image() {
    displayWindow = NULL;
    imageSurface = NULL;
    tempSurface = NULL;
}

Image::~Image() {
}

int Image::saveImage() {
    return 0;
}

int Image::outputImage() {
    return 0;
}

int Image::loadImage() {
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
        cout << "Error: Did not initialise correctly" << endl;
    }
    cout << "Enter the image name (and extension) you wish to use - ";
    cin >> imageName;
    imageSurface = LoadImage(imageName.c_str());

    // Check if the image has been loaded correctly
    if (imageSurface == NULL) {
        imageSurface = NULL;
        SDL_Quit();
        cout << "Error: Picture not loaded/found" << endl;
        return 1;
    }
    displayWindow = SDL_SetVideoMode(imageSurface->w, imageSurface->h, 32, SDL_SWSURFACE);
    if (displayWindow == NULL) {
        cout << "Error: displayWindow is blank, halting";
        return 1;
    }
    SDL_WM_SetCaption("Image Preview", NULL);
    SDL_BlitSurface(imageSurface, NULL, displayWindow, NULL);
    if (SDL_Flip(displayWindow) == -1) {
        cout << "Error: displayWindow did not flip correctly";
        return 1;
    }
    SDL_Event event;
    bool quit = false;
    while (quit == false) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
    }
    SDL_FreeSurface(imageSurface);
    SDL_Quit();
    return 0;
}

void test()
{
    Image img;
    img.loadImage();
}

int main(int argc, char * argv[]) {
    test();
    return 0;
}

编辑:编译器告诉我这个 -

Unhandled exception at 0x776015de in SDL_Display.exe: 0xC0000005: Access violation reading location 0x61d47bce.

在“调用堆栈”中,它在线

- > SDL_Display.exe!std::_DebugHeapDelete<std::locale::facet>(std::locale::facet * _Ptr)  Line 62 + 0xc bytes   C++

编辑 2:LoadImage() 函数 -

// Load Image
SDL_Surface* LoadImage(const char* ImageLocation)
{
    // Initialize Variables
    SDL_Surface *SDL_S_Return = NULL;

    // Load Image
    SDL_S_Return = IMG_Load(ImageLocation);

    // If it hasn't been loaded...
    if(SDL_S_Return == NULL)
    {
        // Send out an Error Message
        fprintf(stderr, "Error: %s\n", IMG_GetError());
    }

    // Return the Surface (Will be NULL if file isn't loaded)
    return SDL_S_Return;
}
4

1 回答 1

0

你的代码没问题。它运行良好,我能看到的唯一问题是:您没有将加载的图像转换为显示格式。试试这个功能:

 SDL_Surface *loadImage(char *imageToLoad)
{
    SDL_Surface *rawImage = NULL;
    SDL_Surface *fixedImage = NULL;

    //load image from file
    rawImage = IMG_Load(imageToLoad);
    //fix image's depth
    fixedImage = SDL_DisplayFormat(rawImage);
    //transparenty
    Uint32 colorkey = SDL_MapRGB(fixedImage->format,0xff,0,0);
    SDL_SetColorKey(fixedImage,SDL_SRCCOLORKEY,colorkey);
    //free memory
    SDL_FreeSurface(rawImage);
    return fixedImage;
}
于 2013-02-22T13:03:44.000 回答