在我加载 SDL_Surface 后,我自己定义的类 Actor 中的 SDL_Surface 将返回 NULL。经过大量控制台输出并使用 GDB 查找可能出现错误的位置。Actor 类包含一个指向 SDL_Surface 的指针,并且 Actor::getSurface 返回指向 SDL_Surface 的指针。
当然,您相信显示的更多代码将有助于解决问题,我很乐意分享。
我的程序输出是:
--Surface was loaded.
Surface is NULL after loading.
我删掉了代码中不重要的部分。
主.cpp:
SDL_Init( SDL_INIT_EVERYTHING );
Actor sprite;
sprite.loadSurface("images/bg.bmp");
if( sprite.getSurface() == NULL )
std::clog << "Surface is NULL after loading." << std::endl;
演员.cpp
bool Actor::loadSurface(std::string t_filename)
{
std::clog << "-ACTOR " << m_id << " LOADSURFACE" << std::endl;
SDL_Surface * loadedImage = NULL;
m_surface = NULL;
loadedImage = IMG_Load( t_filename.c_str() );
if( loadedImage != NULL )
{
std::clog << "--Surface was loaded." << std::endl;
m_surface = SDL_DisplayFormatAlpha( loadedImage );
SDL_FreeSurface( loadedImage );
return true;
}
std::clog << "--Surface was not loaded." << std::endl;
return false;
}