Changed to user video hardware surfaces where possible

This commit is contained in:
Dave White 2003-09-21 07:26:39 +00:00
parent 76748f4445
commit 8c687712ac
9 changed files with 39 additions and 325 deletions

View File

@ -95,7 +95,6 @@ display::display(unit_map& units, CVideo& video, const gamemap& map,
short* const pixels = reinterpret_cast<short*>(disp->pixels);
std::fill(pixels,pixels+length,0);
update_whole_screen();
}
void clear_surfaces(std::map<std::string,SDL_Surface*>& surfaces)
@ -370,7 +369,6 @@ void display::draw(bool update,bool force)
if(dstrect.x + dstrect.w <= this->x() &&
dstrect.y + dstrect.h <= this->y()) {
SDL_BlitSurface(image,NULL,screen,&dstrect);
update_rect(dstrect);
} else {
std::cout << (dstrect.x+dstrect.w) << " > " << this->x() << " or " << (dstrect.y + dstrect.h) << " > " << this->y() << "\n";
}
@ -423,12 +421,7 @@ void display::update_display()
if(updatesLocked_ > 0)
return;
for(std::vector<SDL_Rect>::const_iterator i = updateRects_.begin();
i != updateRects_.end(); ++i) {
screen_.update(i->x,i->y,i->w,i->h);
}
updateRects_.clear();
screen_.flip();
}
void display::draw_sidebar()
@ -479,12 +472,6 @@ void display::draw_game_status(int x, int y)
if(tod_surface != NULL) {
//hardcoded values as to where the time of day image appears
blit_surface(mapx() + 21,int(196*sidebarScaling_),tod_surface);
SDL_Rect update_area;
update_area.x = mapx() + 21;
update_area.y = int(196*sidebarScaling_);
update_area.w = tod_surface->w;
update_area.h = tod_surface->h;
update_rect(update_area);
}
}
@ -498,7 +485,6 @@ void display::draw_game_status(int x, int y)
SDL_Rect srcrect = gameStatusRect_;
srcrect.x -= mapx();
SDL_BlitSurface(background,&srcrect,screen,&gameStatusRect_);
update_rect(gameStatusRect_);
}
int nunits = 0;
@ -558,8 +544,6 @@ void display::draw_game_status(int x, int y)
gameStatusRect_ = font::draw_text(this,clipRect,14,font::NORMAL_COLOUR,
details.str(),x,y);
update_rect(gameStatusRect_);
}
void display::draw_unit_details(int x, int y, const gamemap::location& loc,
@ -581,14 +565,12 @@ void display::draw_unit_details(int x, int y, const gamemap::location& loc,
SDL_Rect srcrect = description_rect;
srcrect.x -= mapx();
SDL_BlitSurface(background,&srcrect,screen,&description_rect);
update_rect(description_rect);
}
if(profile_rect.w > 0 && profile_rect.x >= mapx() && background != NULL) {
SDL_Rect srcrect = profile_rect;
srcrect.x -= this->mapx();
SDL_BlitSurface(background,&srcrect,screen,&profile_rect);
update_rect(profile_rect);
}
std::string status = string_table["healthy"];
@ -650,8 +632,6 @@ void display::draw_unit_details(int x, int y, const gamemap::location& loc,
font::draw_text(this,clipRect,14,font::NORMAL_COLOUR,
details.str(),x,y);
update_rect(description_rect);
y += description_rect.h;
SDL_Surface* const profile = getImage(u.type().image_profile(),UNSCALED);
@ -665,8 +645,6 @@ void display::draw_unit_details(int x, int y, const gamemap::location& loc,
profile_rect.y = y;
profile_rect.w = profile->w;
profile_rect.h = profile->h;
update_rect(profile_rect);
}
SDL_Rect display::get_minimap_location(int x, int y, int w, int h)
@ -782,8 +760,6 @@ void display::draw_minimap(int x, int y, int w, int h)
*side = boxcolour;
side[wbox] = boxcolour;
}
update_rect(minimap_location);
}
void display::draw_terrain_palette(int x, int y, gamemap::TERRAIN selected)
@ -826,7 +802,6 @@ void display::draw_terrain_palette(int x, int y, gamemap::TERRAIN selected)
}
invalid_rect.h = y - invalid_rect.y;
update_rect(invalid_rect);
}
gamemap::TERRAIN display::get_terrain_on(int palx, int paly, int x, int y)
@ -879,32 +854,6 @@ void display::draw_tile(int x, int y, SDL_Surface* unit_image,
const int se_xpos = (int)get_location_x(se_loc);
const int se_ypos = (int)get_location_y(se_loc);
//mark the rectangle for updating
{
SDL_Rect update_rect;
if(xpos >= 0)
update_rect.x = xpos;
else
update_rect.x = 0;
if(ypos >= 0)
update_rect.y = ypos;
else
update_rect.y = 0;
if(xend > mapx())
update_rect.w = mapx() - update_rect.x;
else
update_rect.w = xend - update_rect.x;
if(yend > this->y())
update_rect.h = this->y() - update_rect.y;
else
update_rect.h = yend - update_rect.y;
this->update_rect(update_rect);
}
gamemap::TERRAIN terrain = gamemap::VOID_TERRAIN;
if(x >= 0 && y >= 0 && x < map_.x() && y < map_.y())
@ -2172,7 +2121,6 @@ void display::invalidate_all()
{
invalidateAll_ = true;
invalidated_.clear();
update_map_area();
}
void display::invalidate_unit()
@ -2203,80 +2151,6 @@ void display::remove_overlay(const gamemap::location& loc)
overlays_.erase(loc);
}
void display::update_whole_screen()
{
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = x();
rect.h = y();
update_rect(rect);
}
void display::update_map_area()
{
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = mapx();
rect.h = y();
update_rect(rect);
}
void display::update_side_bar()
{
SDL_Rect rect;
rect.x = mapx();
rect.y = 0;
rect.w = x() - mapx();
rect.h = y();
update_rect(rect);
}
struct inside_rect {
inside_rect(const SDL_Rect& a) : a(a)
{}
bool operator()(const SDL_Rect& b) const
{
if(a.x <= b.x && a.y <= b.y && a.x+a.w >= b.x+b.w && a.y+a.h >= b.y+b.h)
return true;
else
return false;
}
private:
SDL_Rect a;
};
struct outside_rect {
outside_rect(const SDL_Rect& b) : b(b)
{}
bool operator()(const SDL_Rect& a) const
{
if(a.x <= b.x && a.y <= b.y && a.x+a.w >= b.x+b.w && a.y+a.h >= b.y+b.h)
return true;
else
return false;
}
private:
SDL_Rect b;
};
void display::update_rect(const SDL_Rect& rect)
{
std::remove_if(updateRects_.begin(),updateRects_.end(),inside_rect(rect));
if(std::find_if(updateRects_.begin(),updateRects_.end(),outside_rect(rect))
!= updateRects_.end())
return;
updateRects_.push_back(rect);
}
void display::set_team(int team)
{
currentTeam_ = team;

View File

@ -98,7 +98,6 @@ public:
const unit& u, SDL_Rect& description_rect, SDL_Rect& profile_rect);
void update_display();
void update_rect(const SDL_Rect& rect);
void draw_terrain_palette(int x, int y, gamemap::TERRAIN selected);
gamemap::TERRAIN get_terrain_on(int palx, int paly, int x, int y);
@ -198,11 +197,6 @@ private:
std::multimap<gamemap::location,std::string> overlays_;
void update_whole_screen();
void update_map_area();
void update_side_bar();
std::vector<SDL_Rect> updateRects_;
bool sideBarBgDrawn_;
int lastTimeOfDay_;

View File

@ -96,7 +96,7 @@ void show_intro(display& screen, config& data)
textx,texty);
next_button.draw();
skip_button.draw();
screen.video().update(0,0,screen.x(),screen.y());
screen.video().flip();
const std::string& delay = (*i)->values["delay"];
const int ndelay = atoi(delay.c_str());
@ -173,7 +173,7 @@ void show_map_scene(display& screen, config& data)
const std::string& scenario = scenario_name.empty() ? data.values["name"] :
scenario_name;
screen.video().update(0,0,screen.x(),screen.y());
screen.video().flip();
CKey key;
@ -210,12 +210,14 @@ void show_map_scene(display& screen, config& data)
SDL_BlitSurface(image,NULL,screen.video().getSurface(),&dstrect);
for(int i = 0; i != 10; ++i) {
for(int i = 0; i != 50; ++i) {
if(key[KEY_ESCAPE]) {
break;
}
SDL_Delay(50);
SDL_Delay(10);
pump_events();
int a, b;
const int mouse_flags = SDL_GetMouseState(&a,&b);
@ -228,8 +230,7 @@ void show_map_scene(display& screen, config& data)
break;
}
screen.video().update(0,0,screen.x(),screen.y());
pump_events();
screen.video().flip();
}
if(!key[KEY_ESCAPE]) {
@ -243,7 +244,7 @@ void show_map_scene(display& screen, config& data)
dstrect.x,dstrect.y - scenario_size.h - 4);
SDL_BlitSurface(image,NULL,screen.video().getSurface(),&dstrect);
screen.video().update(0,0,screen.x(),screen.y());
screen.video().flip();
bool last_state = true;
for(;;) {

View File

@ -40,10 +40,6 @@ void draw_dialog_frame(int x, int y, int w, int h, display& disp)
draw_rectangle(x-border_size,y-border_size,w+border_size,h+border_size,
border_colour,scr);
draw_rectangle(x,y,w+border_size,h+border_size,border_colour,scr);
SDL_Rect update = {x-border_size,y-border_size,
w+border_size*2+1,h+border_size*2+1};
disp.update_rect(update);
}
void draw_rectangle(int x, int y, int w, int h, short colour,
@ -190,7 +186,7 @@ class menu
for(size_t i = 0; i != items_.size(); ++i)
draw_item(i);
display_->video().update(x_,y_,width(),height());
display_->video().flip();
}
int hit(int x, int y) const {
@ -677,7 +673,7 @@ int show_dialog(display& disp, SDL_Surface* image,
xloc + left_padding,text_widget_y);
}
screen.update(0,0,scr->w,scr->h);
screen.flip();
CKey key;
@ -731,12 +727,10 @@ int show_dialog(display& disp, SDL_Surface* image,
SDL_Surface* const screen = disp.video().getSurface();
if(unit_details_rect.w > 0) {
SDL_FillRect(screen,&unit_details_rect,0);
disp.update_rect(unit_details_rect);
}
if(unit_profile_rect.w > 0) {
SDL_FillRect(screen,&unit_profile_rect,0);
disp.update_rect(unit_profile_rect);
}
disp.draw_unit_details(unitx+left_padding,
@ -861,7 +855,7 @@ TITLE_RESULT show_title(display& screen)
multi_button.draw();
quit_button.draw();
language_button.draw();
screen.video().update(0,0,screen.x(),screen.y());
screen.video().flip();
CKey key;

View File

@ -64,6 +64,7 @@ namespace {
unsigned int get_flags(unsigned int flags)
{
flags |= SDL_HWSURFACE | SDL_DOUBLEBUF;
if((flags&SDL_FULLSCREEN) == 0)
flags |= SDL_RESIZABLE;
@ -71,7 +72,7 @@ namespace {
}
}
CVideo::CVideo(const char* text) : frameBuffer(NULL), backBuffer(NULL)
CVideo::CVideo(const char* text) : frameBuffer(NULL)
{
const int res =
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE);
@ -87,7 +88,7 @@ CVideo::CVideo(const char* text) : frameBuffer(NULL), backBuffer(NULL)
}
CVideo::CVideo( int x, int y, int bits_per_pixel, int flags, const char* text )
: frameBuffer(NULL), backBuffer(NULL)
: frameBuffer(NULL)
{
const int res = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE);
if(res < 0) {
@ -103,7 +104,6 @@ CVideo::CVideo( int x, int y, int bits_per_pixel, int flags, const char* text )
CVideo::~CVideo()
{
SDL_FreeSurface( backBuffer );
SDL_Quit();
}
@ -124,215 +124,75 @@ int CVideo::setMode( int x, int y, int bits_per_pixel, int flags )
frameBuffer = SDL_SetVideoMode( x, y, bits_per_pixel, flags );
if( frameBuffer != NULL ) {
if(backBuffer != NULL)
SDL_FreeSurface(backBuffer);
backBuffer = SDL_ConvertSurface( frameBuffer,
frameBuffer->format, 0 );
if( backBuffer == NULL )
fprintf( stderr, "out of memory\n" );
return bits_per_pixel;
} else return 0;
}
int CVideo::getx() const
{
return backBuffer->w;
return frameBuffer->w;
}
int CVideo::gety() const
{
return backBuffer->h;
return frameBuffer->h;
}
int CVideo::getBitsPerPixel()
{
return backBuffer->format->BitsPerPixel;
return frameBuffer->format->BitsPerPixel;
}
int CVideo::getBytesPerPixel()
{
return backBuffer->format->BytesPerPixel;
return frameBuffer->format->BytesPerPixel;
}
int CVideo::getRedMask()
{
return backBuffer->format->Rmask;
return frameBuffer->format->Rmask;
}
int CVideo::getGreenMask()
{
return backBuffer->format->Gmask;
return frameBuffer->format->Gmask;
}
int CVideo::getBlueMask()
{
return backBuffer->format->Bmask;
return frameBuffer->format->Bmask;
}
void* CVideo::getAddress()
{
return backBuffer->pixels;
return frameBuffer->pixels;
}
void CVideo::flip()
{
::SDL_Flip(frameBuffer);
}
void CVideo::lock()
{
if( SDL_MUSTLOCK(backBuffer) )
SDL_LockSurface( backBuffer );
if( SDL_MUSTLOCK(frameBuffer) )
SDL_LockSurface( frameBuffer );
}
void CVideo::unlock()
{
if( SDL_MUSTLOCK(backBuffer) )
SDL_UnlockSurface( backBuffer );
if( SDL_MUSTLOCK(frameBuffer) )
SDL_UnlockSurface( frameBuffer );
}
int CVideo::mustLock()
{
return SDL_MUSTLOCK(backBuffer);
}
void CVideo::setPixel( int x, int y, int r, int g, int b )
{
int pixel = ((r<<(backBuffer->format->Rshift-backBuffer->format->Rloss))
& backBuffer->format->Rmask)+
((g<<(backBuffer->format->Gshift-backBuffer->format->Gloss))
& backBuffer->format->Gmask)+
((b>>(backBuffer->format->Bloss-backBuffer->format->Bshift))
& backBuffer->format->Bmask);
setPixel( x, y, pixel );
}
int CVideo::convertColour(int r, int g, int b)
{
return ((r<<(backBuffer->format->Rshift-backBuffer->format->Rloss))
& backBuffer->format->Rmask)+
((g<<(backBuffer->format->Gshift-backBuffer->format->Gloss))
& backBuffer->format->Gmask)+
((b>>(backBuffer->format->Bloss-backBuffer->format->Bshift))
& backBuffer->format->Bmask);
}
void CVideo::setPixel( int x, int y, int p )
{
static int pixel;
static char* p1 = ((char*)&pixel);
static char* p2 = ((char*)&pixel)+1;
static char* p3 = ((char*)&pixel)+2;
static short* sp = ((short*)&pixel);
pixel = p;
if( x < 0 || x >= backBuffer->w || y < 0 || y >= backBuffer->h )
return;
switch( backBuffer->format->BytesPerPixel ) {
case 1:
*((char*)backBuffer->pixels+y*backBuffer->w+x) = *p1;
break;
case 2:
*((short*)backBuffer->pixels+y*backBuffer->w+x) = *sp;
break;
case 3:
*((char*)backBuffer->pixels+y*backBuffer->w*3+x*3)
= *p1;
*((char*)backBuffer->pixels+y*backBuffer->w*3+x*3+1)
= *p2;
*((char*)backBuffer->pixels+y*backBuffer->w*3+x*3+2)
= *p3;
break;
case 4:
*((int*)backBuffer->pixels+y*backBuffer->w+x) = pixel;
break;
default:
fprintf( stderr, "Unknown colour depth\n" );
}
}
void CVideo::update( int x, int y, int w, int h )
{
if( w == 0 || h == 0 )
return;
if( x < 0 ) {
w += x;
x = 0;
}
if( y < 0 ) {
h += y;
y = 0;
}
if( x+w > frameBuffer->w )
w = frameBuffer->w - x;
if( y+h > frameBuffer->h )
h = frameBuffer->h - y;
SDL_Rect rect = {x,y,w,h};
SDL_BlitSurface( backBuffer, &rect, frameBuffer, &rect );
SDL_UpdateRect( frameBuffer, x, y, w, h );
}
void CVideo::update( SDL_Rect* rect )
{
SDL_BlitSurface( backBuffer, rect, frameBuffer, rect );
SDL_UpdateRect( frameBuffer, rect->x, rect->y, rect->w, rect->h );
return SDL_MUSTLOCK(frameBuffer);
}
SDL_Surface* CVideo::getSurface( void )
{
return backBuffer;
}
void CVideo::drawChar(int x, int y, int pixel, int bg, char c, int sz)
{
const char* const data = text_ + c*8;
for(int i = 0; i != 8*sz; ++i) {
if(y+i >= backBuffer->h)
return;
for(int j = 0; j != 8*sz; ++j) {
if(x+j >= backBuffer->w)
break;
if(data[i/sz] & (128 >> (j/sz))) {
setPixel(x+j,y+i,pixel);
} else {
if(bg != pixel)
setPixel(x+j,y+i,bg);
}
}
}
}
int CVideo::drawText(int x, int y, int pixel, int bg, const char* text, int sz)
{
const int good_colour = 0x0F00;
const int bad_colour = 0xF000;
int colour = pixel;
const int startx = x;
const int starty = y;
while(*text) {
if(*text == '@') {
colour = good_colour;
} else if(*text == '#') {
colour = bad_colour;
} else if(*text == '\n') {
colour = pixel;
y += 8*sz;
x = startx;
} else {
drawChar(x,y,colour,bg == pixel ? colour : bg,*text,sz);
x += 8*sz;
}
++text;
}
y += 8*sz;
return y - starty;
return frameBuffer;
}
bool CVideo::isFullScreen() const { return fullScreen; }

View File

@ -43,26 +43,19 @@ class CVideo {
void lock();
void unlock();
int mustLock();
void setPixel( int x, int y, int r, int g, int b );
void setPixel( int x, int y, int pixel );
int convertColour(int r, int g, int b);
void update( int x, int y, int w, int h );
void update( SDL_Rect* area );
void flip();
SDL_Surface* getSurface( void );
int drawText(int x, int y, int pixel, int bg, const char* text,int size=1);
bool isFullScreen() const;
struct error {};
private:
void drawChar(int x, int y, int pixel, int bg, char c, int size=1);
SDL_Surface* frameBuffer;
SDL_Surface* backBuffer;
// SDL_Surface* backBuffer;
char text_[256*8];
};

View File

@ -153,7 +153,7 @@ void button::draw()
font::draw_text(display_,clipArea,font_size,
font::NORMAL_COLOUR,label_,textx,texty);
display_->video().update(x_,y_,width(),height());
display_->video().flip();
}
bool button::hit(int x, int y) const

View File

@ -65,7 +65,6 @@ void slider::draw()
SDL_Rect slider = slider_area();
disp_.blit_surface(slider.x,slider.y,image);
disp_.update_rect(area_);
}
double slider::process(int mousex, int mousey, bool button)
@ -76,7 +75,6 @@ double slider::process(int mousex, int mousey, bool button)
bool should_draw = !drawn_;
const SDL_Rect& hit_area = slider_area();
//std::cerr << mousex << ", " << mousey << ": " << hit_area.x << "-" << (hit_area.x+hit_area.w) << "," << hit_area.y << "-" << (hit_area.y+hit_area.h) << "\n";
const bool on = mousex > hit_area.x && mousex <= hit_area.x+hit_area.w &&
mousey > hit_area.y && mousey <= hit_area.y+hit_area.h;

View File

@ -103,7 +103,7 @@ void textbox::draw() const
draw_cursor(pos-1);
}
disp_.video().update(x_,y_,width(),height());
disp_.video().flip();
}
void textbox::process()