【发布时间】:2012-10-30 23:42:14
【问题描述】:
我目前正在尝试创建一个如下所示的按钮类。
void Button::apply_image(std::string path) {
SDL_Surface* loaded_image = NULL;
loaded_image = IMG_Load(path.c_str());
m_button_image = SDL_DisplayFormat(loaded_image);
SDL_FreeSurface(loaded_image);
}
void Button::show(SDL_Surface* screen) {
SDL_BlitSurface(m_button_image, NULL, screen, &m_box);
}
使用该类时,我会执行以下操作:
Button button1(0,0,50,50);
button1.apply_image("images/cards/1.png");
SDL_Surface* screen = NULL;
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
SDL_WM_SetCaption("House Of Cards", NULL);
button1.show(screen);
SDL_Flip(screen);
我的问题是,当我使用SDL_DisplayFormat(loaded_image); 时,图像没有显示,但是当我摆脱那条线和SDL_FreeSurface(loaded_image); 时。然后我将loaded_image = IMG_load(path.c_str()); 更改为m_button_image = IMG_Load(path.c_str());,图像就会出现。我是不是做错了什么,因为路径显然是正确的,因为当我不调用 DisplayFormat() 时图像会显示。通过使用gdb,m_button_image 为NULL,我不明白为什么,因为gdb 也显示loaded_image 指向一个SDL_Surface*。
【问题讨论】: