Advanced Strategic Command
drawing.h
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #ifndef drawingH
22  #define drawingH
23 
24 #include <cmath>
25  #include <map>
26  #include "loki/static_check.h"
27  #include "../libs/sdlmm/src/sdlmm.h"
28  #include "surface.h"
29  #include "blitter.h"
30 
31 #include "../misc.h"
32  #include "../palette.h"
33  // #include "../basegfx.h"
34 #include "lighten.h"
35 
36 
37 
38 template<int BytePerPixel, class ColorMergerUL, class ColorMergerLR>
39 void rectangle( Surface& surface, const SPoint& pos, int w, int h, const ColorMergerUL& ul, const ColorMergerLR& lr )
40 {
41  SurfaceLock lock( surface );
42  typedef typename PixelSize2Type<BytePerPixel>::PixelType TargetPixelType;
43 
44  TargetPixelType* pix = (TargetPixelType*)( surface.pixels() );
45  pix += pos.y * surface.pitch()/BytePerPixel + pos.x;
46 
47  int pitch = surface.pitch()/BytePerPixel;
48 
49  h -= 1;
50  w -= 1;
51 
52  for ( int x = 0; x <= w; ++x )
53  ul.assign ( 1, (pix+x) );
54 
55  for ( int y = 0; y <= h; ++y )
56  ul.assign ( 1, (pix+y*pitch) );
57 
58  for ( int x = 0; x <= w; ++x )
59  lr.assign ( 1, (pix+x+h*pitch) );
60 
61  for ( int y = 0; y <= h; ++y )
62  lr.assign ( 1, (pix+y*pitch+w) );
63 
64 }
65 
66 template<int BytePerPixel, class ColorMergerUL, class ColorMergerLR >
67 void rectangle( Surface& surface, const SPoint& pos, int w, int h, const ColorMergerUL& ul, const ColorMergerLR& lr, const SDLmm::SRect& clip )
68 {
69  SurfaceLock lock( surface );
70  typedef typename PixelSize2Type<BytePerPixel>::PixelType TargetPixelType;
71 
72  TargetPixelType* pix = (TargetPixelType*)( surface.pixels() );
73  pix += pos.y * surface.pitch()/BytePerPixel + pos.x;
74 
75  int pitch = surface.pitch()/BytePerPixel;
76 
77  h -= 1;
78  w -= 1;
79 
80  for ( int x = 0; x <= w; ++x )
81  if ( clip.Contains( SPoint( pos.x+x, pos.y )))
82  ul.assign ( 1, (pix+x) );
83 
84  for ( int y = 0; y <= h; ++y )
85  if ( clip.Contains( SPoint( pos.x, pos.y + y)))
86  ul.assign ( 1, (pix+y*pitch) );
87 
88  for ( int x = 0; x <= w; ++x )
89  if ( clip.Contains( SPoint( pos.x+x, pos.y + h )))
90  lr.assign ( 1, (pix+x+h*pitch) );
91 
92  for ( int y = 0; y <= h; ++y )
93  if ( clip.Contains( SPoint( pos.x+w, pos.y +y )))
94  lr.assign ( 1, (pix+y*pitch+w) );
95 
96 }
97 
98 
99 
100 
101 
102 template<int BytePerPixel, class ColorMerger>
103 void paintFilledRectangle( Surface& surface, const SPoint& pos, int w, int h, const ColorMerger& ul )
104 {
105  if ( w <= 0 || h <= 0 )
106  return;
107 
108  SurfaceLock lock( surface );
109 
110  typedef typename PixelSize2Type<BytePerPixel>::PixelType TargetPixelType;
111  ul.init( surface );
112 
113  TargetPixelType* pix = (TargetPixelType*)( surface.pixels() );
114  pix += pos.y * surface.pitch()/BytePerPixel + pos.x;
115 
116  int pitch = surface.pitch()/BytePerPixel - w;
117 
118  h -= 1;
119  w -= 1;
120 
121  for ( int y = 0; y <= h; ++y ) {
122  for ( int x = 0; x <= w; ++x ) {
123  ul.assign ( pix );
124  ++pix;
125  }
126  pix += pitch;
127  }
128 }
129 
130 extern Uint8 saturationTranslationTable[256][256];
131 
132 template< int pixelsize,
133 template<int> class ColorMerger >
134 class PutPixel: public ColorMerger<pixelsize>
135 {
136  typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
137  Surface& surf;
138  public:
139  PutPixel( Surface& surface ) : surf ( surface )
140  {
141  ColorMerger<pixelsize>::init( surface );
142  };
143 
144  void set( SPoint pos, PixelType src )
145  {
146  PixelType* pix = (PixelType*)( surf.pixels() );
147  pix += pos.y * surf.pitch()/pixelsize + pos.x;
148 
149  this->assign ( src, pix );
150  };
151 };
152 
153 template< int pixelsize, class ColorMerger >
155 {
156  typedef typename PixelSize2Type<pixelsize>::PixelType PixelType;
157  Surface& surf;
158  const ColorMerger& colormerger;
159  public:
160  PutPixel2( Surface& surface, const ColorMerger& cm ) : surf ( surface ), colormerger(cm)
161  {
162  };
163 
164  void set( SPoint pos, PixelType src )
165  {
166  PixelType* pix = (PixelType*)( surf.pixels() );
167  pix += pos.y * surf.pitch()/pixelsize + pos.x;
168 
169  colormerger.assign ( src, pix );
170  };
171 };
172 
173 
174 template< int pixelsize, class ColorMerger >
175 void drawLine ( Surface& surface, const ColorMerger& cm, const SPoint& pos, const SPoint& pos2 )
176 {
177  PutPixel2<pixelsize, ColorMerger> pp ( surface, cm );
178  int i, deltax, deltay, numpixels, d, dinc1, dinc2, x, xinc1, xinc2, y, yinc1, yinc2;
179 
180  int x1 = pos.x;
181  int y1 = pos.y;
182  int x2 = pos2.x;
183  int y2 = pos2.y;
184 
185  /* calculate deltax and deltay for initialisation */
186 
187  deltax = x2 - x1;
188  if ( deltax < 0 )
189  deltax = -deltax;
190 
191  deltay = y2 - y1;
192  if ( deltay < 0 )
193  deltay = -deltay;
194 
195  /* initialize all vars based on which is the independent variable */
196  if (deltax >= deltay)
197  {
198 
199  /* x is independent variable */
200  numpixels = deltax + 1;
201  d = (2 * deltay) - deltax;
202  dinc1 = deltay << 1;
203  dinc2 = (deltay - deltax) << 1;
204  xinc1 = 1;
205  xinc2 = 1;
206  yinc1 = 0;
207  yinc2 = 1;
208  }
209  else
210  {
211 
212  /* y is independent variable */
213  numpixels = deltay + 1;
214  d = (2 * deltax) - deltay;
215  dinc1 = deltax << 1;
216  dinc2 = (deltax - deltay) << 1;
217  xinc1 = 0;
218  xinc2 = 1;
219  yinc1 = 1;
220  yinc2 = 1;
221  }
222 
223  /* make sure x and y move in the right directions */
224  if (x1 > x2)
225  {
226  xinc1 = -xinc1;
227  xinc2 = -xinc2;
228  }
229  if (y1 > y2)
230  {
231  yinc1 = -yinc1;
232  yinc2 = -yinc2;
233  }
234 
235  /* start drawing at <x1, y1> */
236  x = x1;
237  y = y1;
238 
239  /* draw the pixels */
240  for (i = 1; i <= numpixels; i++)
241  {
242  pp.set( SPoint(x, y), 1 );
243 
244 
245  if (d < 0)
246  {
247  d = d + dinc1;
248  x = x + xinc1;
249  y = y + yinc1;
250  }
251  else
252  {
253  d = d + dinc2;
254  x = x + xinc2;
255  y = y + yinc2;
256  }
257  }
258 }
259 
260 
261 
262 template<int pixelsize>
264 {}
265 ;
266 
267 template<>
269 {
270  typedef PixelSize2Type<1>::PixelType PixelType;
271  protected:
272 
273  void assign ( PixelType src, PixelType* dest )
274  {
275  // not defined
276  };
277  public:
279  ColorMerger_AlphaLighter ( float factor ) {} ;
280 };
281 
282 
283 template<>
285 {
286  typedef PixelSize2Type<4>::PixelType PixelType;
287  int factor16;
288  protected:
289 
290  void init( const Surface& srf )
291  {
293  };
294 
295 
296  void assign ( PixelType src, PixelType* dest )
297  {
298  if ( isOpaque(src ) )
299  *dest = lighten_Color( *dest, factor16 );
300 
301  };
302  public:
304  {}
305  ;
306  ColorMerger_AlphaLighter ( float factor ) : factor16 ( int ( factor * 16 ))
307  {}
308  ;
309 };
310 
311 
312 #endif
313 
void assign(PixelType src, PixelType *dest)
Definition: drawing.h:296
void drawLine(Surface &surface, const ColorMerger &cm, const SPoint &pos, const SPoint &pos2)
Definition: drawing.h:175
void paintFilledRectangle(Surface &surface, const SPoint &pos, int w, int h, const ColorMerger &ul)
Definition: drawing.h:103
NullParamType nullParam
Definition: blitter.cpp:30
void init(const Surface &srf)
Definition: blitter.h:658
ColorMerger_AlphaLighter(float factor)
Definition: drawing.h:279
PutPixel(Surface &surface)
Definition: drawing.h:139
PutPixel2(Surface &surface, const ColorMerger &cm)
Definition: drawing.h:160
Uint8 saturationTranslationTable[256][256]
Definition: drawing.cpp:35
ColorMerger_AlphaLighter(float factor)
Definition: drawing.h:306
void assign(PixelType src, PixelType *dest)
Definition: drawing.h:273
SDLmm::SPoint SPoint
Definition: surface.h:27
void rectangle(Surface &surface, const SPoint &pos, int w, int h, const ColorMergerUL &ul, const ColorMergerLR &lr)
Definition: drawing.h:39
void init(const Surface &srf)
Definition: drawing.h:290
SDLmm::Color lighten_Color(SDLmm::Color color, int factor16)
Definition: lighten.h:37
ColorMerger_AlphaLighter(NullParamType npt=nullParam)
Definition: drawing.h:303
void set(SPoint pos, PixelType src)
Definition: drawing.h:164
void set(SPoint pos, PixelType src)
Definition: drawing.h:144
ColorMerger_AlphaLighter(NullParamType npt=nullParam)
Definition: drawing.h:278