-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 に答える