/*************************************************************************** * Copyright (C) 2004 by Christoph Freundl * * Christoph.Freundl@informatik.uni-erlangen.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "backgroundwidget.h" #include #include "graphics.h" #include namespace SDLwidgets { BackgroundWidget::BackgroundWidget( Widget* widget, const char* filename ) : WidgetDecorator( widget ), _alpha( SDL_ALPHA_OPAQUE ) { if ( widget ) { widget->setPosition( 0, 0 ); _min_width = _widget->getMinWidth(); _min_height = _widget->getMinHeight(); _max_width = _widget->getMaxWidth(); _max_height = _widget->getMaxHeight(); _width = _widget->getWidth(); _height = _widget->getHeight(); } _color.r = 0; _color.g = 0; _color.b = 0; _image = IMG_Load( filename ); } BackgroundWidget::BackgroundWidget( int width, int height ) : WidgetDecorator( 0 ), _alpha( SDL_ALPHA_OPAQUE ), _image( 0 ) { _min_width = width; _max_width = width; _width = width; _min_height = height; _max_height = height; _height = height; } void BackgroundWidget::draw( SDL_Surface* surface ) { if ( _alpha != SDL_ALPHA_TRANSPARENT ) { if ( !_image ) { if ( _alpha == SDL_ALPHA_OPAQUE ) { Uint32 pixelColor = SDL_MapRGBA( surface->format, _color.r, _color.g, _color.b, _alpha ); SDL_Rect rect; getOffset( rect.x, rect.y ); rect.w = _width; rect.h = _height; SDL_FillRect( surface, &rect, pixelColor ); } else { SDL_Rect rect; getOffset( rect.x, rect.y ); rect.w = _width; rect.h = _height; Graphics::fillRectAlpha( surface, &rect, _color, _alpha ); } } else { Sint16 offset_x, offset_y; getOffset( offset_x, offset_y ); SDL_Rect src_rect, dst_rect; src_rect.x = 0; src_rect.y = 0; dst_rect.y = offset_y; while ( dst_rect.y - offset_y < _height ) { if ( _image->h > _height - dst_rect.y + offset_y ) src_rect.h = _height - dst_rect.y + offset_y; else src_rect.h = _image->h; dst_rect.x = offset_x; while ( dst_rect.x - offset_x < _width ) { if ( _image->w > _width - dst_rect.x + offset_x ) src_rect.w = _width - dst_rect.x + offset_x; else src_rect.w = _image->w; SDL_BlitSurface( _image, &src_rect, surface, &dst_rect ); dst_rect.x += _image->w; } dst_rect.y += _image->h; } } } if ( _widget ) _widget->draw( surface ); } void BackgroundWidget::setColor( const SDL_Color& color ) { Widget::setColor( color ); } void BackgroundWidget::setAlpha( int alpha ) { _alpha = alpha; } void BackgroundWidget::setWidth( const Uint16 width ) { if ( width < _min_width ) Widget::setWidth( _min_width ); else Widget::setWidth( width ); if ( _widget ) { _widget->setWidth( _width ); _widget->setPosition( ( _width - _widget->getWidth() ) / 2, ( _height - _widget->getHeight() ) / 2 ); } } void BackgroundWidget::setMinWidth( const Uint16 min_width ) { _min_width = min_width; if ( _width < _min_width ) { setWidth( _min_width ); } } void BackgroundWidget::setHeight( const Uint16 height ) { _height = height; if ( _widget ) { _widget->setHeight( _height ); _widget->setPosition( ( _width - _widget->getWidth() ) / 2, ( _height - _widget->getHeight() ) / 2 ); } } void BackgroundWidget::doLayout() { if ( _widget ) { if ( _widget->getWidth() > _min_width ) _width = _widget->getWidth(); if ( _widget->getHeight() > _min_height ) _height = _widget->getHeight(); } Widget::doLayout(); } }