SDL程序崩溃关闭 - c0000005?

时间:2013-02-21 14:46:41

标签: c++ sdl sdl-1.2

尝试制作一个简单的程序来显示加载到系统中的图片。如果我输入的图像名称在它返回的目录中找不到,并且错误说它没有被找到,但由于某种原因,在此之后我返回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;
}

1 个答案:

答案 0 :(得分: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;
}