_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

sdlpixel.h
Go to the documentation of this file.
1 /* _______ __ __ __ ______ __ __ _______ __ __
2  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8  *
9  * Copyright (c) 2004, 2005 darkbits Js_./
10  * Per Larsson a.k.a finalman _RqZ{a<^_aa
11  * Olof Naess�n a.k.a jansem/yakslem _asww7!uY`> )\a//
12  * _Qhm`] _f "'c 1!5m
13  * Visit: http://guichan.darkbits.org )Qk<P ` _: :+' .' "{[
14  * .)j(] .d_/ '-( P . S
15  * License: (BSD) <Td/Z <fP"5(\"??"\a. .L
16  * Redistribution and use in source and _dV>ws?a-?' ._/L #'
17  * binary forms, with or without )4d[#7r, . ' )d`)[
18  * modification, are permitted provided _Q-5'5W..j/?' -?!\)cam'
19  * that the following conditions are met: j<<WP+k/);. _W=j f
20  * 1. Redistributions of source code must .$%w\/]Q . ."' . mj$
21  * retain the above copyright notice, ]E.pYY(Q]>. a J@\
22  * this list of conditions and the j(]1u<sE"L,. . ./^ ]{a
23  * following disclaimer. 4'_uomm\. )L);-4 (3=
24  * 2. Redistributions in binary form must )_]X{Z('a_"a7'<a"a, ]"[
25  * reproduce the above copyright notice, #}<]m7`Za??4,P-"'7. ).m
26  * this list of conditions and the ]d2e)Q(<Q( ?94 b- LQ/
27  * following disclaimer in the <B!</]C)d_, '(<' .f. =C+m
28  * documentation and/or other materials .Z!=J ]e []('-4f _ ) -.)m]'
29  * provided with the distribution. .w[5]' _[ /.)_-"+? _/ <W"
30  * 3. Neither the name of Guichan nor the :$we` _! + _/ . j?
31  * names of its contributors may be used =3)= _f (_yQmWW$#( "
32  * to endorse or promote products derived - W, sQQQQmZQ#Wwa]..
33  * from this software without specific (js, \[QQW$QWW#?!V"".
34  * prior written permission. ]y:.<\.. .
35  * -]n w/ ' [.
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT )/ )/ !
37  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY < (; sac , '
38  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, ]^ .- %
39  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF c < r
40  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR aga< <La
41  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 5% )P'-3L
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR _bQf` y`..)a
43  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ,J?4P'.P"_(\?d'.,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES _Pa,)!f/<[]/ ?"
45  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT _2-..:. .r+_,.. .
46  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ?a.<%"' " -'.a_ _,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ^
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
49  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
52  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #ifndef GCN_SDLPIXEL_HPP
56 #define GCN_SDLPIXEL_HPP
57 
58 #include "SDL.h"
59 
60 #include "guichan/color.h"
61 
62 namespace gcn
63 {
64 
73  inline const Color SDLgetPixel(SDL_Surface* surface, int x, int y)
74  {
75  int bpp = surface->format->BytesPerPixel;
76 
77  SDL_LockSurface(surface);
78 
79  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
80 
81  unsigned int color = 0;
82 
83  switch(bpp)
84  {
85  case 1:
86  color = *p;
87  break;
88 
89  case 2:
90  color = *(Uint16 *)p;
91  break;
92 
93  case 3:
94  if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
95  {
96  color = p[0] << 16 | p[1] << 8 | p[2];
97  }
98  else
99  {
100  color = p[0] | p[1] << 8 | p[2] << 16;
101  }
102  break;
103 
104  case 4:
105  color = *(Uint32 *)p;
106  break;
107 
108  }
109 
110  Uint8 r,g,b,a;
111 
112  SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
113  SDL_UnlockSurface(surface);
114 
115  return Color(r,g,b,a);
116  }
117 
126  inline void SDLputPixel(SDL_Surface* surface, int x, int y, const Color& color)
127  {
128  int bpp = surface->format->BytesPerPixel;
129 
130  SDL_LockSurface(surface);
131 
132  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
133 
134  Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b);
135 
136  switch(bpp)
137  {
138  case 1:
139  *p = pixel;
140  break;
141 
142  case 2:
143  *(Uint16 *)p = pixel;
144  break;
145 
146  case 3:
147  if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
148  {
149  p[0] = (pixel >> 16) & 0xff;
150  p[1] = (pixel >> 8) & 0xff;
151  p[2] = pixel & 0xff;
152  }
153  else
154  {
155  p[0] = pixel & 0xff;
156  p[1] = (pixel >> 8) & 0xff;
157  p[2] = (pixel >> 16) & 0xff;
158  }
159  break;
160 
161  case 4:
162  *(Uint32 *)p = pixel;
163  break;
164  }
165 
166  SDL_UnlockSurface(surface);
167  }
168 
176  inline unsigned int SDLAlpha16(unsigned int src, unsigned int dst, unsigned char a)
177  {
178  // Loses precision for speed
179  a = (255 - a) >> 3;
180 
181  src = (((src << 16) | src) & 0x07E0F81F);
182  dst = ((dst << 16) | dst) & 0x07E0F81F;
183  dst = ((((dst - src) * a) >> 5) + src) & 0x07E0F81F;
184  return (dst >> 16) | dst;
185  }
186 
194  inline unsigned int SDLAlpha32(unsigned int src, unsigned int dst, unsigned char a)
195  {
196  a = 255 - a;
197 
198  unsigned int src2 = (src & 0xFF00FF00) >> 8;
199  src &= 0x00FF00FF;
200 
201  unsigned int dst2 = (dst & 0xFF00FF00) >> 8;
202  dst &= 0x00FF00FF;
203 
204  dst = ((((dst - src) * a) >> 8) + src) & 0x00FF00FF;
205  dst2 = ((((dst2 - src2) * a) >> 8) + src2) & 0x00FF00FF;
206  return dst | (dst2 << 8);
207  }
208 
217  inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y, const Color& color)
218  {
219  int bpp = surface->format->BytesPerPixel;
220 
221  SDL_LockSurface(surface);
222 
223  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
224 
225  Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b);
226 
227  switch(bpp)
228  {
229  case 1:
230  *p = pixel;
231  break;
232 
233  case 2:
234  *(Uint16 *)p = SDLAlpha16(pixel, *(Uint16 *)p, color.a);
235  break;
236 
237  case 3:
238  if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
239  {
240  p[0] = (pixel >> 16) & 0xff;
241  p[1] = (pixel >> 8) & 0xff;
242  p[2] = pixel & 0xff;
243  }
244  else
245  {
246  p[0] = pixel & 0xff;
247  p[1] = (pixel >> 8) & 0xff;
248  p[2] = (pixel >> 16) & 0xff;
249  }
250  break;
251 
252  case 4:
253  *(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a);
254  break;
255  }
256 
257  SDL_UnlockSurface(surface);
258  }
259 }
260 
261 #endif // end GCN_SDLPIXEL_HPP
gcn::SDLputPixelAlpha
void SDLputPixelAlpha(SDL_Surface *surface, int x, int y, const Color &color)
Definition: sdlpixel.h:217
color.h
gcn
Definition: cliprectangle.cpp:61
gcn::Color::a
int a
Definition: color.h:159
gcn::Color::b
int b
Definition: color.h:153
gcn::Color
Definition: color.h:65
gcn::SDLAlpha16
unsigned int SDLAlpha16(unsigned int src, unsigned int dst, unsigned char a)
Definition: sdlpixel.h:176
gcn::SDLAlpha32
unsigned int SDLAlpha32(unsigned int src, unsigned int dst, unsigned char a)
Definition: sdlpixel.h:194
gcn::Color::g
int g
Definition: color.h:148
gcn::SDLgetPixel
const Color SDLgetPixel(SDL_Surface *surface, int x, int y)
Definition: sdlpixel.h:73
gcn::Color::r
int r
Definition: color.h:143
gcn::SDLputPixel
void SDLputPixel(SDL_Surface *surface, int x, int y, const Color &color)
Definition: sdlpixel.h:126
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.