Advanced Strategic Command
overviewmapimage.cpp
Go to the documentation of this file.
1 
2 #include "overviewmapimage.h"
3 #include "iconrepository.h"
4 #include "graphics/drawing.h"
5 
6 
7 OverviewMapImage::OverviewMapImage() : initialized(false )
8 {
9 }
10 
12 {
13  create ( image );
14 }
15 
17 {
18  return sizeof(*this);
19 }
20 
21 
22 
24 {
25  return SPoint( pos.x * 6 + (pos.y&1) * 3 , pos.y * 2 );
26 }
27 
29 {
30  int lx = pos.x % 6;
31  int ly = pos.y % 4;
32  switch ( ly ) {
33  case 0: {
34  if ( lx == 1 || lx == 2 )
35  return MapCoordinate( pos.x / 6, pos.y / 2 );
36  if ( lx == 0 )
37  return MapCoordinate( pos.x / 6 - 1, pos.y / 2 - 1 );
38  if ( lx >= 3 )
39  return MapCoordinate( pos.x / 6 , pos.y / 2 - 1 );
40  };
41  break;
42  case 1:
43  case 2: {
44  if ( lx < 4 )
45  return MapCoordinate( pos.x / 6, pos.y / 2 );
46  if ( lx >= 4 ) {
47  if ( ly == 1 )
48  return MapCoordinate( pos.x / 6, pos.y / 2 - 1 );
49  else
50  return MapCoordinate( pos.x / 6, pos.y / 2 + 1 );
51  }
52  };
53  case 3: {
54  if ( lx == 1 || lx == 2 )
55  return MapCoordinate( pos.x / 6, pos.y / 2 );
56  if ( lx == 0 )
57  return MapCoordinate( pos.x / 6 - 1, pos.y / 2 );
58  if ( lx >= 3 )
59  return MapCoordinate( pos.x / 6 , pos.y / 2 + 1 );
60  };
61  break;
62  };
63  return MapCoordinate(-1,-1);
64 }
65 
66 
67 void OverviewMapImage::create( const Surface& image )
68 {
69  if ( !image.valid() ) {
70  for ( int y = 0; y < height; ++y )
71  for ( int x = 0; x < width; ++x )
72  segment[x][y] = SDLmm::ColorRGBA( 0, 0, 0, Surface::transparent );
73  return;
74  }
75 
76  Surface fieldshape = IconRepository::getIcon("hexinvis.raw");
77 
78  for ( int y = 0; y < height; ++y )
79  for ( int x = 0; x < width; ++x )
80  if ( (y == 0 || y == height-1) && (x == 0 || x == width-1)) {
81  segment[x][y] = SDLmm::ColorRGBA( 0, 0, 0, Surface::transparent );
82  } else {
83  int r = 0;
84  int b = 0;
85  int g = 0;
86  int a = 0;
87  int count = 0;
88 
89  for ( int iy = image.h() * y / height; iy < image.h() * (y+1) / height; ++iy)
90  for ( int ix = image.w() * x / width; ix < image.w() * (x+1) / width; ++ix)
91  if ( fieldshape.GetPixel(ix,iy) != 255 ) {
92  if ( image.GetPixelFormat().BitsPerPixel() == 8 ) {
93  int col = image.GetPixel(ix,iy);
94  if ( col != 255 ) {
95  r += pal[col][0] * 4;
96  g += pal[col][1] * 4;
97  b += pal[col][2] * 4;
98  a += Surface::opaque;
99  } else
101  } else {
102  SDLmm::Color rawColor = image.GetPixel(ix,iy);
103  if ( !image.isTransparent( rawColor )) {
104  SDLmm::ColorRGBA col = image.GetPixelFormat().GetRGBA( rawColor );
105 
106  /*
107  r += col.r * (255-col.a) / 255;
108  g += col.g * (255-col.a) / 255;
109  b += col.b * (255-col.a) / 255;
110  */
111 
112  r += col.r * (col.a) / 255;
113  g += col.g * (col.a) / 255;
114  b += col.b * (col.a) / 255;
115 
116  /*
117  r += col.r ;
118  g += col.g ;
119  b += col.b ;
120  a += col.a;
121  */
122  a += Surface::opaque;
123  } else
125  }
126  ++count;
127  }
128 
129  if ( count )
130  segment[x][y] = SDLmm::ColorRGBA( r / count, g / count, b / count, a / count );
131  else
132  segment[x][y] = SDLmm::ColorRGBA( 0, 0, 0, Surface::transparent );
133  }
134 }
135 
136 void OverviewMapImage::blit( Surface& s, const SPoint& pos, int layer ) const
137 {
138  assert( s.GetPixelFormat().BytesPerPixel() == 4 );
140 
141  for ( int ly = 0; ly < height; ++ly )
142  for ( int lx = 0; lx < width; ++lx )
143  if ( ! ((ly == 0 || ly == height-1) && (lx == 0 || lx == width-1))) {
144 // *((Uint32 *)pixels() + y * pitch()/4 + x) = color
145 
146  putpix.set( SPoint( pos.x + lx, pos.y + ly), s.GetPixelFormat().MapRGBA( segment[lx][ly] ) );
147  // s.SetPixel( x + lx, y + ly, s.GetPixelFormat().MapRGBA( segment[lx][ly] ));
148  }
149 
150 }
151 
152 
153 void OverviewMapImage::fill( Surface& s, const SPoint& pos, SDL_Color color )
154 {
155  fill( s, pos, s.GetPixelFormat().MapRGB(color ));
156 }
157 
158 void OverviewMapImage::fill( Surface& s, const SPoint& pos, SDLmm::Color color )
159 {
160  for ( int ly = 0; ly < height; ++ly )
161  for ( int lx = 0; lx < width; ++lx )
162  if ( ! ((ly == 0 || ly == height-1) && (lx == 0 || lx == width-1)))
163  s.SetPixel4( pos.x + lx, pos.y + ly, color );
164 
165 }
166 
167 void OverviewMapImage::fillCenter( Surface& s, const SPoint& pos, SDL_Color color )
168 {
169  fillCenter( s, pos, s.GetPixelFormat().MapRGB(color ));
170 }
171 
173 {
174  for ( int ly = 1; ly < 3; ++ly )
175  for ( int lx = 1; lx < 3; ++lx )
176  s.SetPixel4( pos.x + lx, pos.y + ly, color );
177 
178 }
179 
180 void OverviewMapImage::lighten( Surface& s, const SPoint& pos, float value )
181 {
182  int value16 = int( value * 16 );
183  for ( int ly = 0; ly < height; ++ly )
184  for ( int lx = 0; lx < width; ++lx )
185  if ( ! ((ly == 0 || ly == height-1) && (lx == 0 || lx == width-1)))
186  s.SetPixel4( pos.x + lx, pos.y + ly, lighten_Color( s.GetPixel(pos.x + lx, pos.y + ly), value16 ));
187 }
188 
static const int height
static void lighten(Surface &s, const SPoint &pos, float value)
static const Uint32 transparent
Definition: surface.h:65
void blit(Surface &s, const SPoint &pos, int layer=0) const
int getMemoryFootprint() const
static const int width
static SPoint map2surface(const MapCoordinate &pos)
static void fill(Surface &s, const SPoint &pos, SDLmm::Color color)
void create(const Surface &image)
Coordinate on the twodimensional map.
Definition: typen.h:202
static const Uint32 opaque
Definition: surface.h:66
SDLmm::Color Color
Definition: blitter.h:35
SDLmm::SPoint SPoint
Definition: surface.h:27
bool isTransparent(SDLmm::Color col) const
Definition: surface.cpp:515
dacpalette256 pal
Definition: palette.cpp:33
static Surface & getIcon(const ASCString &name)
static MapCoordinate surface2map(const SPoint &pos)
SDLmm::ColorRGBA segment[width][height]
static void fillCenter(Surface &s, const SPoint &pos, SDLmm::Color color)
SDLmm::Color lighten_Color(SDLmm::Color color, int factor16)
Definition: lighten.h:37