Mercurial > fife-parpg
comparison ext/guichan-0.8.1/include/guichan/sdl/sdlpixel.hpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 /* _______ __ __ __ ______ __ __ _______ __ __ | |
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ | |
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / | |
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / | |
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / | |
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / | |
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ | |
8 * | |
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson | |
10 * | |
11 * | |
12 * Per Larsson a.k.a finalman | |
13 * Olof Naessén a.k.a jansem/yakslem | |
14 * | |
15 * Visit: http://guichan.sourceforge.net | |
16 * | |
17 * License: (BSD) | |
18 * Redistribution and use in source and binary forms, with or without | |
19 * modification, are permitted provided that the following conditions | |
20 * are met: | |
21 * 1. Redistributions of source code must retain the above copyright | |
22 * notice, this list of conditions and the following disclaimer. | |
23 * 2. Redistributions in binary form must reproduce the above copyright | |
24 * notice, this list of conditions and the following disclaimer in | |
25 * the documentation and/or other materials provided with the | |
26 * distribution. | |
27 * 3. Neither the name of Guichan nor the names of its contributors may | |
28 * be used to endorse or promote products derived from this software | |
29 * without specific prior written permission. | |
30 * | |
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
42 */ | |
43 | |
44 #ifndef GCN_SDLPIXEL_HPP | |
45 #define GCN_SDLPIXEL_HPP | |
46 | |
47 #include "SDL.h" | |
48 #include "guichan/color.hpp" | |
49 | |
50 namespace gcn | |
51 { | |
52 | |
53 /** | |
54 * Checks a pixels color of an SDL_Surface. | |
55 * | |
56 * @param surface an SDL_Surface where to check for a pixel color. | |
57 * @param x the x coordinate on the surface. | |
58 * @param y the y coordinate on the surface. | |
59 * @return a color of a pixel. | |
60 */ | |
61 inline const Color SDLgetPixel(SDL_Surface* surface, int x, int y) | |
62 { | |
63 int bpp = surface->format->BytesPerPixel; | |
64 | |
65 SDL_LockSurface(surface); | |
66 | |
67 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | |
68 | |
69 unsigned int color = 0; | |
70 | |
71 switch(bpp) | |
72 { | |
73 case 1: | |
74 color = *p; | |
75 break; | |
76 | |
77 case 2: | |
78 color = *(Uint16 *)p; | |
79 break; | |
80 | |
81 case 3: | |
82 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
83 { | |
84 color = p[0] << 16 | p[1] << 8 | p[2]; | |
85 } | |
86 else | |
87 { | |
88 color = p[0] | p[1] << 8 | p[2] << 16; | |
89 } | |
90 break; | |
91 | |
92 case 4: | |
93 color = *(Uint32 *)p; | |
94 break; | |
95 | |
96 } | |
97 | |
98 unsigned char r,g,b,a; | |
99 | |
100 SDL_GetRGBA(color, surface->format, &r, &g, &b, &a); | |
101 SDL_UnlockSurface(surface); | |
102 | |
103 return Color(r,g,b,a); | |
104 } | |
105 | |
106 /** | |
107 * Puts a pixel on an SDL_Surface. | |
108 * | |
109 * @param x the x coordinate on the surface. | |
110 * @param y the y coordinate on the surface. | |
111 * @param color the color the pixel should be in. | |
112 */ | |
113 inline void SDLputPixel(SDL_Surface* surface, int x, int y, const Color& color) | |
114 { | |
115 int bpp = surface->format->BytesPerPixel; | |
116 | |
117 SDL_LockSurface(surface); | |
118 | |
119 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | |
120 | |
121 Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b); | |
122 | |
123 switch(bpp) | |
124 { | |
125 case 1: | |
126 *p = pixel; | |
127 break; | |
128 | |
129 case 2: | |
130 *(Uint16 *)p = pixel; | |
131 break; | |
132 | |
133 case 3: | |
134 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
135 { | |
136 p[0] = (pixel >> 16) & 0xff; | |
137 p[1] = (pixel >> 8) & 0xff; | |
138 p[2] = pixel & 0xff; | |
139 } | |
140 else | |
141 { | |
142 p[0] = pixel & 0xff; | |
143 p[1] = (pixel >> 8) & 0xff; | |
144 p[2] = (pixel >> 16) & 0xff; | |
145 } | |
146 break; | |
147 | |
148 case 4: | |
149 *(Uint32 *)p = pixel; | |
150 break; | |
151 } | |
152 | |
153 SDL_UnlockSurface(surface); | |
154 } | |
155 | |
156 /** | |
157 * Blends two 32 bit colors together. | |
158 * | |
159 * @param src the source color. | |
160 * @param dst the destination color. | |
161 * @param a alpha. | |
162 */ | |
163 inline unsigned int SDLAlpha32(unsigned int src, unsigned int dst, unsigned char a) | |
164 { | |
165 unsigned int b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8; | |
166 unsigned int g = ((src & 0xff00) * a + (dst & 0xff00) * (255 - a)) >> 8; | |
167 unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000) * (255 - a)) >> 8; | |
168 | |
169 return (b & 0xff) | (g & 0xff00) | (r & 0xff0000); | |
170 } | |
171 | |
172 /** | |
173 * Blends two 16 bit colors together. | |
174 * | |
175 * @param src the source color. | |
176 * @param dst the destination color. | |
177 * @param a alpha. | |
178 */ | |
179 inline unsigned short SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat *f) | |
180 { | |
181 unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask) * (255 - a)) >> 8; | |
182 unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask) * (255 - a)) >> 8; | |
183 unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask) * (255 - a)) >> 8; | |
184 | |
185 return (unsigned short)((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask)); | |
186 } | |
187 | |
188 /* | |
189 typedef struct{ | |
190 SDL_Palette *palette; | |
191 Uint8 BitsPerPixel; | |
192 Uint8 BytesPerPixel; | |
193 Uint32 Rmask, Gmask, Bmask, Amask; | |
194 Uint8 Rshift, Gshift, Bshift, Ashift; | |
195 Uint8 Rloss, Gloss, Bloss, Aloss; | |
196 Uint32 colorkey; | |
197 Uint8 alpha; | |
198 } SDL_PixelFormat; | |
199 */ | |
200 | |
201 /** | |
202 * Puts a pixel on an SDL_Surface with alpha | |
203 * | |
204 * @param x the x coordinate on the surface. | |
205 * @param y the y coordinate on the surface. | |
206 * @param color the color the pixel should be in. | |
207 */ | |
208 inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y, const Color& color) | |
209 { | |
210 int bpp = surface->format->BytesPerPixel; | |
211 | |
212 SDL_LockSurface(surface); | |
213 | |
214 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | |
215 | |
216 Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b); | |
217 | |
218 switch(bpp) | |
219 { | |
220 case 1: | |
221 *p = pixel; | |
222 break; | |
223 | |
224 case 2: | |
225 *(Uint16 *)p = SDLAlpha16(pixel, *(Uint32 *)p, color.a, surface->format); | |
226 break; | |
227 | |
228 case 3: | |
229 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
230 { | |
231 unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8; | |
232 unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8; | |
233 unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8; | |
234 | |
235 p[2] = b; | |
236 p[1] = g; | |
237 p[0] = r; | |
238 } | |
239 else | |
240 { | |
241 unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8; | |
242 unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8; | |
243 unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8; | |
244 | |
245 p[0] = b; | |
246 p[1] = g; | |
247 p[2] = r; | |
248 } | |
249 break; | |
250 | |
251 case 4: | |
252 *(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a); | |
253 break; | |
254 } | |
255 | |
256 SDL_UnlockSurface(surface); | |
257 } | |
258 } | |
259 | |
260 #endif // end GCN_SDLPIXEL_HPP |