00001
00002 #include "overviewmapimage.h"
00003 #include "iconrepository.h"
00004 #include "graphics/drawing.h"
00005
00006
00007 OverviewMapImage::OverviewMapImage() : initialized(false )
00008 {
00009 }
00010
00011 OverviewMapImage::OverviewMapImage( const Surface& image )
00012 {
00013 create ( image );
00014 }
00015
00016 int OverviewMapImage::getMemoryFootprint() const
00017 {
00018 return sizeof(*this);
00019 }
00020
00021
00022
00023 SPoint OverviewMapImage::map2surface( const MapCoordinate& pos )
00024 {
00025 return SPoint( pos.x * 6 + (pos.y&1) * 3 , pos.y * 2 );
00026 }
00027
00028 MapCoordinate OverviewMapImage::surface2map( const SPoint& pos )
00029 {
00030 int lx = pos.x % 6;
00031 int ly = pos.y % 4;
00032 switch ( ly ) {
00033 case 0: {
00034 if ( lx == 1 || lx == 2 )
00035 return MapCoordinate( pos.x / 6, pos.y / 2 );
00036 if ( lx == 0 )
00037 return MapCoordinate( pos.x / 6 - 1, pos.y / 2 - 1 );
00038 if ( lx >= 3 )
00039 return MapCoordinate( pos.x / 6 , pos.y / 2 - 1 );
00040 };
00041 break;
00042 case 1:
00043 case 2: {
00044 if ( lx < 4 )
00045 return MapCoordinate( pos.x / 6, pos.y / 2 );
00046 if ( lx >= 4 ) {
00047 if ( ly == 1 )
00048 return MapCoordinate( pos.x / 6, pos.y / 2 - 1 );
00049 else
00050 return MapCoordinate( pos.x / 6, pos.y / 2 + 1 );
00051 }
00052 };
00053 case 3: {
00054 if ( lx == 1 || lx == 2 )
00055 return MapCoordinate( pos.x / 6, pos.y / 2 );
00056 if ( lx == 0 )
00057 return MapCoordinate( pos.x / 6 - 1, pos.y / 2 );
00058 if ( lx >= 3 )
00059 return MapCoordinate( pos.x / 6 , pos.y / 2 + 1 );
00060 };
00061 break;
00062 };
00063 return MapCoordinate(-1,-1);
00064 }
00065
00066
00067 void OverviewMapImage::create( const Surface& image )
00068 {
00069 if ( !image.valid() ) {
00070 for ( int y = 0; y < height; ++y )
00071 for ( int x = 0; x < width; ++x )
00072 segment[x][y] = SDLmm::ColorRGBA( 0, 0, 0, Surface::transparent );
00073 return;
00074 }
00075
00076 Surface fieldshape = IconRepository::getIcon("hexinvis.raw");
00077
00078 for ( int y = 0; y < height; ++y )
00079 for ( int x = 0; x < width; ++x )
00080 if ( (y == 0 || y == height-1) && (x == 0 || x == width-1)) {
00081 segment[x][y] = SDLmm::ColorRGBA( 0, 0, 0, Surface::transparent );
00082 } else {
00083 int r = 0;
00084 int b = 0;
00085 int g = 0;
00086 int a = 0;
00087 int count = 0;
00088
00089 for ( int iy = image.h() * y / height; iy < image.h() * (y+1) / height; ++iy)
00090 for ( int ix = image.w() * x / width; ix < image.w() * (x+1) / width; ++ix)
00091 if ( fieldshape.GetPixel(ix,iy) != 255 ) {
00092 if ( image.GetPixelFormat().BitsPerPixel() == 8 ) {
00093 int col = image.GetPixel(ix,iy);
00094 if ( col != 255 ) {
00095 r += pal[col][0] * 4;
00096 g += pal[col][1] * 4;
00097 b += pal[col][2] * 4;
00098 a += Surface::opaque;
00099 } else
00100 a += Surface::transparent;
00101 } else {
00102 SDLmm::Color rawColor = image.GetPixel(ix,iy);
00103 if ( !image.isTransparent( rawColor )) {
00104 SDLmm::ColorRGBA col = image.GetPixelFormat().GetRGBA( rawColor );
00105
00106
00107
00108
00109
00110
00111
00112 r += col.r * (col.a) / 255;
00113 g += col.g * (col.a) / 255;
00114 b += col.b * (col.a) / 255;
00115
00116
00117
00118
00119
00120
00121
00122 a += Surface::opaque;
00123 } else
00124 a += Surface::transparent;
00125 }
00126 ++count;
00127 }
00128
00129 if ( count )
00130 segment[x][y] = SDLmm::ColorRGBA( r / count, g / count, b / count, a / count );
00131 else
00132 segment[x][y] = SDLmm::ColorRGBA( 0, 0, 0, Surface::transparent );
00133 }
00134 }
00135
00136 void OverviewMapImage::blit( Surface& s, const SPoint& pos, int layer ) const
00137 {
00138 assert( s.GetPixelFormat().BytesPerPixel() == 4 );
00139 PutPixel<4,ColorMerger_AlphaMerge> putpix( s );
00140
00141 for ( int ly = 0; ly < height; ++ly )
00142 for ( int lx = 0; lx < width; ++lx )
00143 if ( ! ((ly == 0 || ly == height-1) && (lx == 0 || lx == width-1))) {
00144
00145
00146 putpix.set( SPoint( pos.x + lx, pos.y + ly), s.GetPixelFormat().MapRGBA( segment[lx][ly] ) );
00147
00148 }
00149
00150 }
00151
00152
00153 void OverviewMapImage::fill( Surface& s, const SPoint& pos, SDL_Color color )
00154 {
00155 fill( s, pos, s.GetPixelFormat().MapRGB(color ));
00156 }
00157
00158 void OverviewMapImage::fill( Surface& s, const SPoint& pos, SDLmm::Color color )
00159 {
00160 for ( int ly = 0; ly < height; ++ly )
00161 for ( int lx = 0; lx < width; ++lx )
00162 if ( ! ((ly == 0 || ly == height-1) && (lx == 0 || lx == width-1)))
00163 s.SetPixel4( pos.x + lx, pos.y + ly, color );
00164
00165 }
00166
00167 void OverviewMapImage::fillCenter( Surface& s, const SPoint& pos, SDL_Color color )
00168 {
00169 fillCenter( s, pos, s.GetPixelFormat().MapRGB(color ));
00170 }
00171
00172 void OverviewMapImage::fillCenter( Surface& s, const SPoint& pos, SDLmm::Color color )
00173 {
00174 for ( int ly = 1; ly < 3; ++ly )
00175 for ( int lx = 1; lx < 3; ++lx )
00176 s.SetPixel4( pos.x + lx, pos.y + ly, color );
00177
00178 }
00179
00180 void OverviewMapImage::lighten( Surface& s, const SPoint& pos, float value )
00181 {
00182 int value16 = int( value * 16 );
00183 for ( int ly = 0; ly < height; ++ly )
00184 for ( int lx = 0; lx < width; ++lx )
00185 if ( ! ((ly == 0 || ly == height-1) && (lx == 0 || lx == width-1)))
00186 s.SetPixel4( pos.x + lx, pos.y + ly, lighten_Color( s.GetPixel(pos.x + lx, pos.y + ly), value16 ));
00187 }
00188