00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "pgimage.h"
00030
00031
00032 PG_Rect PG_Image::CalcWidgetSize( const PG_Point& p, const SDL_Surface* image )
00033 {
00034 if ( image )
00035 return PG_Rect (p.x, p.y, image->w, image->h );
00036 else
00037 return PG_Rect (p.x, p.y, 1,1 );
00038 }
00039
00040
00041 PG_Image::PG_Image(PG_Widget* parent, const PG_Point& p, const std::string& filename, Uint32 colorkey, PG_Draw::BkMode drawmode, const std::string& style) : PG_ThemeWidget(parent, CalcWidgetSize(p, NULL), style), my_cachedSrf(NULL), my_DrawMode(drawmode) {
00042 LoadImage(filename, colorkey);
00043
00044 if(my_image != NULL && drawmode == PG_Draw::TILE) {
00045 SizeWidget(my_image->w, my_image->h);
00046 }
00047 }
00048
00049 PG_Image::PG_Image(PG_Widget* parent, const PG_Point& p, const std::string& filename, PG_Draw::BkMode drawmode, const std::string& style) : PG_ThemeWidget(parent, CalcWidgetSize(p, NULL), style), my_cachedSrf(NULL), my_DrawMode(drawmode) {
00050 LoadImage(filename);
00051
00052 if(my_image != NULL && drawmode == PG_Draw::TILE) {
00053 SizeWidget(my_image->w, my_image->h);
00054 }
00055 }
00056
00057
00058
00059 PG_Image::PG_Image(PG_Widget* parent, const PG_Point& p, SDL_Surface* image, bool freeimage, PG_Draw::BkMode drawmode, const std::string& style) : PG_ThemeWidget(parent, CalcWidgetSize(p, image), style), my_cachedSrf(NULL), my_DrawMode(drawmode) {
00060 SetImage(image, freeimage);
00061
00062 if(my_image != NULL && drawmode == PG_Draw::TILE) {
00063 SizeWidget(my_image->w, my_image->h);
00064 }
00065 }
00066
00067 PG_Image::~PG_Image() {
00068 DeleteThemedSurface(my_cachedSrf);
00069 }
00070
00071 void PG_Image::eventDraw(SDL_Surface* surface, const PG_Rect& rect) {}
00072
00073 void PG_Image::eventBlit(SDL_Surface* srf, const PG_Rect& src, const PG_Rect& dst) {
00074
00075 if(my_cachedSrf != NULL) {
00076 PG_Rect my_src;
00077 PG_Rect my_dst;
00078
00079 GetClipRects(my_src, my_dst, *this);
00080 PG_Widget::eventBlit(my_cachedSrf, my_src, my_dst);
00081 return;
00082 }
00083
00084 if(my_image == NULL) {
00085 return;
00086 }
00087
00088 if(my_image->w != 0 && my_image->h != 0) {
00089 PG_Rect my_src;
00090 PG_Rect my_dst;
00091
00092 GetClipRects(my_src, my_dst, *this);
00093 if(my_DrawMode != PG_Draw::STRETCH) {
00094 PG_Widget::eventBlit(my_image, my_src, my_dst);
00095 return;
00096 }
00097
00098 my_cachedSrf = CreateThemedSurface(PG_Rect(0, 0, my_width, my_height), 0, my_background, my_DrawMode, my_blendLevel);
00099 PG_Draw::DrawThemedSurface(my_cachedSrf, PG_Rect(0, 0, my_src.w, my_src.h), 0, my_image, my_DrawMode, my_blendLevel);
00100
00101
00102 eventBlit(srf, src, dst);
00103 }
00104 }
00105
00106 void PG_Image::SetColorKey(const PG_Color& key) {
00107 SDL_SetColorKey(my_image, SDL_SRCCOLORKEY, key);
00108 DeleteThemedSurface(my_cachedSrf);
00109 my_cachedSrf = NULL;
00110 }
00111
00112 void PG_Image::SetDrawMode(PG_Draw::BkMode mode) {
00113 if(mode != my_DrawMode) {
00114 my_DrawMode = mode;
00115 DeleteThemedSurface(my_cachedSrf);
00116 my_cachedSrf = NULL;
00117 }
00118 }
00119
00120 bool PG_Image::LoadImage(const std::string& filename) {
00121 DeleteThemedSurface(my_cachedSrf);
00122 my_cachedSrf = NULL;
00123
00124 if(PG_ThemeWidget::LoadImage(filename)) {
00125 return true;
00126 }
00127
00128 return false;
00129 }
00130
00131 bool PG_Image::LoadImage(const std::string& filename, const PG_Color& key) {
00132 DeleteThemedSurface(my_cachedSrf);
00133 my_cachedSrf = NULL;
00134
00135 if(PG_ThemeWidget::LoadImage(filename, key)) {
00136 return true;
00137 }
00138
00139 return false;
00140 }
00141
00142 bool PG_Image::SetImage(SDL_Surface* image, bool bFreeImage) {
00143 DeleteThemedSurface(my_cachedSrf);
00144 my_cachedSrf = NULL;
00145
00146 if(PG_ThemeWidget::SetImage(image, bFreeImage)) {
00147 return true;
00148 }
00149
00150 return false;
00151 }