refactored code to add surface_restorer object

This commit is contained in:
Dave White 2003-11-23 05:41:47 +00:00
parent 1c42351ca7
commit 9e4d3db7b3
3 changed files with 41 additions and 39 deletions

View file

@ -16,6 +16,7 @@
#include "game.hpp"
#include "sdl_utils.hpp"
#include "util.hpp"
#include "video.hpp"
SDL_Surface* scale_surface(SDL_Surface* surface, int w, int h)
{
@ -103,3 +104,25 @@ SDL_Surface* get_surface_portion(SDL_Surface* src, SDL_Rect& area)
return dst;
}
surface_restorer::surface_restorer(SDL_Surface* surface, SDL_Rect& rect)
: target_(surface), rect_(rect), surface_(NULL)
{
update();
}
surface_restorer::~surface_restorer()
{
restore();
}
void surface_restorer::restore()
{
::SDL_BlitSurface(surface_,NULL,target_,&rect_);
update_rect(rect_);
}
void surface_restorer::update()
{
surface_.assign(::get_surface_portion(target_,rect_));
}

View file

@ -122,4 +122,18 @@ private:
bool locked_;
};
struct surface_restorer
{
surface_restorer(SDL_Surface* surface, SDL_Rect& rect);
~surface_restorer();
void restore();
void update();
private:
SDL_Surface* target_;
SDL_Rect rect_;
scoped_sdl_surface surface_;
};
#endif

View file

@ -414,12 +414,10 @@ int show_dialog(display& disp, SDL_Surface* image,
button_offset += buttons[button_num].width();
}
SDL_Rect dlgr;
dlgr.x=xloc-10;
dlgr.y=yloc-10;
dlgr.w=total_width+20;
dlgr.h=total_height+20;
SDL_Surface *original = get_surface_portion(disp.video().getSurface(),dlgr);
SDL_Rect dlgr = {xloc-10,yloc-10,total_width+20,total_height+20};
const surface_restorer restorer(disp.video().getSurface(),dlgr);
draw_dialog_frame(xloc,yloc,total_width,total_height,disp);
if(menu_.height() > 0)
@ -524,22 +522,13 @@ int show_dialog(display& disp, SDL_Surface* image,
*text_widget_text = text_widget.text();
if(menu_.height() == 0) {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return 0;
} else {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return menu_.selection();
}
}
if(!key_down && key[SDLK_ESCAPE] && type == MESSAGE) {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return -1;
}
@ -547,14 +536,8 @@ int show_dialog(display& disp, SDL_Surface* image,
(type == YES_NO || type == OK_CANCEL)) {
if(menu_.height() == 0) {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return 1;
} else {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return -1;
}
}
@ -590,9 +573,6 @@ int show_dialog(display& disp, SDL_Surface* image,
select_item);
if(res != -1)
{
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return res;
}
}
@ -628,20 +608,11 @@ int show_dialog(display& disp, SDL_Surface* image,
//item selected if the last button is not pressed, and
//cancel (-1) otherwise
if(menu_.height() == 0) {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return button_it - buttons.begin();
} else if(buttons.size() <= 1 ||
size_t(button_it-buttons.begin()) != buttons.size()-1) {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return menu_.selection();
} else {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return -1;
}
}
@ -656,9 +627,6 @@ int show_dialog(display& disp, SDL_Surface* image,
if(action != NULL) {
const int act = action->do_action();
if(act != dialog_action::CONTINUE_DIALOG) {
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return act;
}
}
@ -667,9 +635,6 @@ int show_dialog(display& disp, SDL_Surface* image,
SDL_Delay(20);
}
SDL_BlitSurface(original,NULL,disp.video().getSurface(),&dlgr);
SDL_FreeSurface(original);
update_rect(dlgr);
return -1;
}