0
我在C++中遇到了SDL_BlitSurface的分段錯誤。SDL_BlitSurface分段錯誤
這裏的代碼
mainScreen->draw(0, 0, background, NULL);
(其中mainScreen是Screen類的一個實例)和Screen類的繪製函數被實現爲
void Screen::draw(float x, float y, Texture* source, SDL_Rect* clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
if (source->myimage == NULL) throw 5;
if (this->screen == NULL) throw 6;
SDL_BlitSurface(source->myimage, NULL, this->screen, &offset);
}
和紋理被定義爲如下:
class Texture
{
public:
Texture(std::string imagepath);
~Texture();
private:
SDL_Surface* myimage;
float width;
float height;
friend class Screen;
//friend void Screen::draw(float x, float y, Texture source, SDL_Rect* clip);
};
並實施
#include "Texture.h"
Texture::Texture(std::string filepath)
{
// Initialize the image to NULL to avoid any pointer issues
myimage = NULL;
// OPTIMIZED FOR PNGs
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filepath.c_str());
if (loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormatAlpha(loadedImage); // Notice no colour keying (use transparent PNGs)
}
myimage = optimizedImage;
SDL_FreeSurface(loadedImage);
SDL_FreeSurface(optimizedImage);
if (myimage == NULL)
{
// Some kind of error
throw 4;
}
}
Texture::~Texture()
{
SDL_FreeSurface(myimage);
}
我已經穿過圖像加載到Texture類的區域,似乎工作正常。 此外,我知道this->screen
正確加載,因爲我在一個單獨的函數中使用SDL_FillRect。
發佈一個完整的,最小的示例來演示問題。 – genpfault 2011-02-08 16:23:07