SDL_BlitSurface 分段错误
我从 C++ 中的 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 );
}
,Texture 定义如下:
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 。
I'm getting a segmentation fault from SDL_BlitSurface in C++.
here's the code
mainScreen->draw( 0, 0, background, NULL );
(where mainScreen is an instance of the Screen class) and the draw function of the Screen class is implemented as
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 );
}
and Texture is defined as follows:
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);
};
and implemented
#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 );
}
I've stepped through the area where the image is loaded into the Texture class and that seems to work fine.
Also, I know the this->screen
is loaded correctly as I use SDL_FillRect with it in a separate function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要这样做:
您还没有完成表面(它被分配给 myimage)。
Don't do this:
You aren't done with the surface (it gets assigned to myimage).