我目前正在尝试创建一个如下所示的按钮类。
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*。