作者:龙飞
有了按钮类,我们制作对话框就很轻松了。边写程序边说明吧。
bool hand_dialog(const ScreenSurface& screen, const std::string& dialog_text, int size)
{
const int CENTRE_X = (screen.point()->w) / 2;
const int CENTRE_Y = (screen.point()->h) / 2;
const int HALF_SUB_BUTTON_W = 64 / 2;
先找出显示屏的中心点,然后是对话框按钮宽度的一半。这些是为了在屏幕中心的对称位置显示YES和NO
PictureSurface quitBG("./images/dialog_bg.png", screen);
quitBG.blit();
载入对话框的背景
TextSurface quitDlg(dialog_text, screen, 215, 195, 122, size);
int quitDlg_x = CENTRE_X - ((quitDlg.point()->w)/2);
int quitDlg_y = CENTRE_Y - 50;
quitDlg.blit(quitDlg_x, quitDlg_y);
载入对话框的提示文字
const int YES_X = CENTRE_X - 40 - HALF_SUB_BUTTON_W;
const int YES_Y = CENTRE_Y + 30;
Button yesButtonEffect("./images/h3_yes_off.png", "./images/h3_yes_over.png", screen);
yesButtonEffect.setup(YES_X, YES_Y, 5);
yesButtonEffect.blitOut();
构建YES按钮
const int NO_X = CENTRE_X + 40 - HALF_SUB_BUTTON_W;
const int NO_Y = CENTRE_Y + 30;
Button noButtonEffect("./images/h3_no_off.png", "./images/h3_no_over.png", screen);
noButtonEffect.setup(NO_X, NO_Y, 5);
noButtonEffect.blitOut();
构建NO按钮
screen.flip();
显示屏幕
SDL_Event gameEvent;
while( true ){
while ( SDL_PollEvent(&gameEvent) != 0 ){
if ( gameEvent.type == SDL_KEYDOWN ){
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
return false;
}
}
quitBG.blit();
quitDlg.blit(quitDlg_x, quitDlg_y);
if ( yesButtonEffect.effectiveClick(gameEvent) == true )
return true;
if ( noButtonEffect.effectiveClick(gameEvent) == true )
return false;
screen.flip();
}
}
}
主循环了。可以通过按下ESC或者按下NO按钮取消对话框,按下YES按钮则表示做出了选择。我们使用effectiveClick()方法,一次性的将接收事件,判断事件,显示按钮的不同状态集成性的完成了。
posted on 2008-04-19 12:32
lf426 阅读(3766)
评论(2) 编辑 收藏 引用 所属分类:
SDL入门教程