annotate window/render.cc @ 67:419761c8d9b9

Add configure option to build scn2kdump.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 23 Feb 2011 01:48:51 +0100
parents 4416cfac86ae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
28 #include "font/font.h"
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
29 #include "font/text.h"
56
c7bcc0ec2267 * replaced Grp and Text classes by the TextImpl and GrpImpl ones
thib
parents: 52
diff changeset
30 #include "SDL.h"
c7bcc0ec2267 * replaced Grp and Text classes by the TextImpl and GrpImpl ones
thib
parents: 52
diff changeset
31 #include "render.h"
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
32 #include "rect.h"
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
33 #include "surface.h"
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
34 #include <stdio.h>
0
223b71206888 Initial import
thib
parents:
diff changeset
35
223b71206888 Initial import
thib
parents:
diff changeset
36 #ifndef ALPHA_MAX
223b71206888 Initial import
thib
parents:
diff changeset
37 #define ALPHA_MAX 255
223b71206888 Initial import
thib
parents:
diff changeset
38 #endif
223b71206888 Initial import
thib
parents:
diff changeset
39
223b71206888 Initial import
thib
parents:
diff changeset
40 Rect::Rect(const TextGlyph& letter) {
223b71206888 Initial import
thib
parents:
diff changeset
41 lx = letter.x + letter.glyph->bitmap_left;
223b71206888 Initial import
thib
parents:
diff changeset
42 rx = lx + letter.glyph->bitmap.width;
223b71206888 Initial import
thib
parents:
diff changeset
43
223b71206888 Initial import
thib
parents:
diff changeset
44 ty = letter.y - letter.glyph->bitmap_top;
223b71206888 Initial import
thib
parents:
diff changeset
45 by = ty + letter.glyph->bitmap.rows;
223b71206888 Initial import
thib
parents:
diff changeset
46 }
223b71206888 Initial import
thib
parents:
diff changeset
47
223b71206888 Initial import
thib
parents:
diff changeset
48 Rect::Rect(const class Surface& so) {
223b71206888 Initial import
thib
parents:
diff changeset
49 SDL_Surface* s = (SDL_Surface*)(&so);
223b71206888 Initial import
thib
parents:
diff changeset
50 lx = 0; ty = 0;
223b71206888 Initial import
thib
parents:
diff changeset
51 rx = s->w; by = s->h;
223b71206888 Initial import
thib
parents:
diff changeset
52 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
53
0
223b71206888 Initial import
thib
parents:
diff changeset
54 inline Rect _Rect(const SDL_Rect& r) {
223b71206888 Initial import
thib
parents:
diff changeset
55 return Rect(r.x, r.y, r.x+r.w, r.y+r.h);
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
56 }
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
57
0
223b71206888 Initial import
thib
parents:
diff changeset
58 inline SDL_Rect SDLed(const Rect& rect) {
223b71206888 Initial import
thib
parents:
diff changeset
59 SDL_Rect r;
223b71206888 Initial import
thib
parents:
diff changeset
60 r.x = rect.lx;
223b71206888 Initial import
thib
parents:
diff changeset
61 r.y = rect.ty;
223b71206888 Initial import
thib
parents:
diff changeset
62 r.w = rect.rx-rect.lx;
223b71206888 Initial import
thib
parents:
diff changeset
63 r.h = rect.by-rect.ty;
223b71206888 Initial import
thib
parents:
diff changeset
64 return r;
223b71206888 Initial import
thib
parents:
diff changeset
65 }
223b71206888 Initial import
thib
parents:
diff changeset
66
223b71206888 Initial import
thib
parents:
diff changeset
67 Rect DSurfaceRenderText(TextGlyphStream::iterator start, TextGlyphStream::iterator end, const Rect& srcrect,
223b71206888 Initial import
thib
parents:
diff changeset
68 Surface* dst_o, const Rect& dstrect) {
223b71206888 Initial import
thib
parents:
diff changeset
69
223b71206888 Initial import
thib
parents:
diff changeset
70 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
71 Rect confine_to(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
72 Rect dst_clip(_Rect(dst->clip_rect));
223b71206888 Initial import
thib
parents:
diff changeset
73 Rect drawn_rect(0,0,0,0);
223b71206888 Initial import
thib
parents:
diff changeset
74
223b71206888 Initial import
thib
parents:
diff changeset
75 int dif_x = dstrect.lx - srcrect.lx;
223b71206888 Initial import
thib
parents:
diff changeset
76 int dif_y = dstrect.ty - srcrect.ty;
223b71206888 Initial import
thib
parents:
diff changeset
77
223b71206888 Initial import
thib
parents:
diff changeset
78 dst_clip.rmove(-dif_x, -dif_y);
223b71206888 Initial import
thib
parents:
diff changeset
79 confine_to.intersect(dst_clip);
223b71206888 Initial import
thib
parents:
diff changeset
80
223b71206888 Initial import
thib
parents:
diff changeset
81 TextGlyphStream::iterator it;
223b71206888 Initial import
thib
parents:
diff changeset
82 SDL_PixelFormat& fmt = *dst->format;
223b71206888 Initial import
thib
parents:
diff changeset
83 int ashift = fmt.Ashift - fmt.Aloss; int amask = fmt.Amask;
223b71206888 Initial import
thib
parents:
diff changeset
84 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
85 int dbpp = fmt.BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
86
223b71206888 Initial import
thib
parents:
diff changeset
87 SDL_LockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
88 for (it=start; it!=end; it++) {
223b71206888 Initial import
thib
parents:
diff changeset
89 Rect letter_r(*it);
223b71206888 Initial import
thib
parents:
diff changeset
90 letter_r.intersect(confine_to);
223b71206888 Initial import
thib
parents:
diff changeset
91 if (letter_r.empty()) continue;
223b71206888 Initial import
thib
parents:
diff changeset
92 drawn_rect.join(letter_r);
223b71206888 Initial import
thib
parents:
diff changeset
93
223b71206888 Initial import
thib
parents:
diff changeset
94 int w = letter_r.width();
223b71206888 Initial import
thib
parents:
diff changeset
95 int h = letter_r.height();
223b71206888 Initial import
thib
parents:
diff changeset
96 int sbpl = it->glyph->bitmap.width;
223b71206888 Initial import
thib
parents:
diff changeset
97
223b71206888 Initial import
thib
parents:
diff changeset
98 unsigned char* smem = it->glyph->bitmap.buffer +
223b71206888 Initial import
thib
parents:
diff changeset
99 (letter_r.ty - (it->y-it->glyph->bitmap_top)) * sbpl +
223b71206888 Initial import
thib
parents:
diff changeset
100 (letter_r.lx - (it->x+it->glyph->bitmap_left));
223b71206888 Initial import
thib
parents:
diff changeset
101 char* dmem = (char*)dst->pixels +
223b71206888 Initial import
thib
parents:
diff changeset
102 (letter_r.ty + dif_y) * dbpl +
223b71206888 Initial import
thib
parents:
diff changeset
103 (letter_r.lx + dif_x) * dbpp;
223b71206888 Initial import
thib
parents:
diff changeset
104 Uint32 pixel = ((int(it->r)<<fmt.Rshift)&fmt.Rmask) |((int(it->g)<<fmt.Gshift)&fmt.Gmask) |((int(it->b)<<fmt.Bshift)&fmt.Bmask);
223b71206888 Initial import
thib
parents:
diff changeset
105 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
106 if (!it->is_rev) {
223b71206888 Initial import
thib
parents:
diff changeset
107 if (dbpp == 4) {
223b71206888 Initial import
thib
parents:
diff changeset
108 for (i=0; i<h; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
109 for (j=0; j<w; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
110 int alpha = smem[j];
223b71206888 Initial import
thib
parents:
diff changeset
111 ((Uint32*)dmem)[j] = pixel | ((alpha << ashift)&amask);
223b71206888 Initial import
thib
parents:
diff changeset
112 }
223b71206888 Initial import
thib
parents:
diff changeset
113 smem += sbpl; dmem += dbpl;
223b71206888 Initial import
thib
parents:
diff changeset
114 }
223b71206888 Initial import
thib
parents:
diff changeset
115 } else if (dbpp == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
116 for (i=0; i<h; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
117 for (j=0; j<w; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
118 int alpha = smem[j];
223b71206888 Initial import
thib
parents:
diff changeset
119 ((Uint16*)dmem)[j] = pixel | ((alpha << ashift)&amask);
223b71206888 Initial import
thib
parents:
diff changeset
120 }
223b71206888 Initial import
thib
parents:
diff changeset
121 smem += sbpl; dmem += dbpl;
223b71206888 Initial import
thib
parents:
diff changeset
122 }
223b71206888 Initial import
thib
parents:
diff changeset
123 } else abort();
223b71206888 Initial import
thib
parents:
diff changeset
124 } else { /* reversed */
223b71206888 Initial import
thib
parents:
diff changeset
125 if (dbpp == 4) {
223b71206888 Initial import
thib
parents:
diff changeset
126 for (i=0; i<h; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
127 for (j=0; j<w; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
128 int alpha = 255 - smem[j];
223b71206888 Initial import
thib
parents:
diff changeset
129 ((Uint32*)dmem)[j] = pixel | ((alpha << ashift)&amask);
223b71206888 Initial import
thib
parents:
diff changeset
130 }
223b71206888 Initial import
thib
parents:
diff changeset
131 smem += sbpl; dmem += dbpl;
223b71206888 Initial import
thib
parents:
diff changeset
132 }
223b71206888 Initial import
thib
parents:
diff changeset
133 } else if (dbpp == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
134 for (i=0; i<h; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
135 for (j=0; j<w; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
136 int alpha = 255 - smem[j];
223b71206888 Initial import
thib
parents:
diff changeset
137 ((Uint16*)dmem)[j] = pixel | ((alpha << ashift)&amask);
223b71206888 Initial import
thib
parents:
diff changeset
138 }
223b71206888 Initial import
thib
parents:
diff changeset
139 smem += sbpl; dmem += dbpl;
223b71206888 Initial import
thib
parents:
diff changeset
140 }
223b71206888 Initial import
thib
parents:
diff changeset
141 } else abort();
223b71206888 Initial import
thib
parents:
diff changeset
142 }
223b71206888 Initial import
thib
parents:
diff changeset
143 }
223b71206888 Initial import
thib
parents:
diff changeset
144 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
145 drawn_rect.rmove(dif_x, dif_y);
223b71206888 Initial import
thib
parents:
diff changeset
146 return drawn_rect;
223b71206888 Initial import
thib
parents:
diff changeset
147 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
148
0
223b71206888 Initial import
thib
parents:
diff changeset
149 void DSurfaceFill(Surface* src_o, const Rect& rect, int r, int g, int b, int a) {
223b71206888 Initial import
thib
parents:
diff changeset
150 SDL_Rect sr = SDLed(rect);
223b71206888 Initial import
thib
parents:
diff changeset
151 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
152 SDL_FillRect( src, &sr, SDL_MapRGBA(src->format, r, g, b, a));
223b71206888 Initial import
thib
parents:
diff changeset
153 }
223b71206888 Initial import
thib
parents:
diff changeset
154
223b71206888 Initial import
thib
parents:
diff changeset
155 static void clip_rect(Rect& srcrect, Rect& dstrect, SDL_Surface* dstsurf) {
223b71206888 Initial import
thib
parents:
diff changeset
156 Rect confine_to(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
157 Rect dst_clip(_Rect(dstsurf->clip_rect));
223b71206888 Initial import
thib
parents:
diff changeset
158
223b71206888 Initial import
thib
parents:
diff changeset
159 int dif_x = dstrect.lx - srcrect.lx;
223b71206888 Initial import
thib
parents:
diff changeset
160 int dif_y = dstrect.ty - srcrect.ty;
223b71206888 Initial import
thib
parents:
diff changeset
161
223b71206888 Initial import
thib
parents:
diff changeset
162 dst_clip.rmove(-dif_x, -dif_y);
223b71206888 Initial import
thib
parents:
diff changeset
163 confine_to.intersect(dst_clip);
223b71206888 Initial import
thib
parents:
diff changeset
164
223b71206888 Initial import
thib
parents:
diff changeset
165 srcrect = confine_to;
223b71206888 Initial import
thib
parents:
diff changeset
166 dstrect = confine_to;
223b71206888 Initial import
thib
parents:
diff changeset
167 dstrect.rmove(dif_x, dif_y);
223b71206888 Initial import
thib
parents:
diff changeset
168 }
223b71206888 Initial import
thib
parents:
diff changeset
169
223b71206888 Initial import
thib
parents:
diff changeset
170 void DSurfaceMove(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o) {
223b71206888 Initial import
thib
parents:
diff changeset
171
223b71206888 Initial import
thib
parents:
diff changeset
172 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
173 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
174
65
4416cfac86ae Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
175 if (dst->format->BytesPerPixel != src->format->BytesPerPixel) return; // RGB変換はできない
0
223b71206888 Initial import
thib
parents:
diff changeset
176
223b71206888 Initial import
thib
parents:
diff changeset
177 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
178 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
179
223b71206888 Initial import
thib
parents:
diff changeset
180 SDL_LockSurface(dst); SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
181 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
182 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
183 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
184 int width = bpp * srcrect.width();
223b71206888 Initial import
thib
parents:
diff changeset
185 int height = srcrect.height();
223b71206888 Initial import
thib
parents:
diff changeset
186 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
187 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
188 char* smem_end = src_o->mem_end(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
189 char* dmem_end = dst_o->mem_end(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
190
65
4416cfac86ae Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
191 // メモリに重なりがあり、src が上位側の場合、コピー方向を逆転する
0
223b71206888 Initial import
thib
parents:
diff changeset
192 if (smem < dmem && dmem < smem_end) {
223b71206888 Initial import
thib
parents:
diff changeset
193 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
194 for (i=0; i<height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
195 char* s = smem_end; char* d = dmem_end;
223b71206888 Initial import
thib
parents:
diff changeset
196 for (j=0; j<width; j++)
223b71206888 Initial import
thib
parents:
diff changeset
197 *--d = *--s;
223b71206888 Initial import
thib
parents:
diff changeset
198 dmem_end -= dbpl; smem_end -= sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
199 }
223b71206888 Initial import
thib
parents:
diff changeset
200 } else {
223b71206888 Initial import
thib
parents:
diff changeset
201 int i;
223b71206888 Initial import
thib
parents:
diff changeset
202 for (i=0; i<height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
203 memcpy(dmem, smem, width);
223b71206888 Initial import
thib
parents:
diff changeset
204 dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
205 }
223b71206888 Initial import
thib
parents:
diff changeset
206 }
223b71206888 Initial import
thib
parents:
diff changeset
207 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
208 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
209 }
223b71206888 Initial import
thib
parents:
diff changeset
210
223b71206888 Initial import
thib
parents:
diff changeset
211 void DSurfaceFillA(Surface* dsto, const Rect& rect, int r, int g, int b, int a) {
223b71206888 Initial import
thib
parents:
diff changeset
212 SDL_Surface* dst = (SDL_Surface*)dsto;
223b71206888 Initial import
thib
parents:
diff changeset
213 SDL_LockSurface( dst);
223b71206888 Initial import
thib
parents:
diff changeset
214 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
215 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
216 int width = rect.width();
223b71206888 Initial import
thib
parents:
diff changeset
217 int height = rect.height();
223b71206888 Initial import
thib
parents:
diff changeset
218 int amask = dst->format->Amask;
223b71206888 Initial import
thib
parents:
diff changeset
219 int ashift = dst->format->Ashift - dst->format->Aloss;
223b71206888 Initial import
thib
parents:
diff changeset
220
223b71206888 Initial import
thib
parents:
diff changeset
221 char* dmem = (char*)(dst->pixels) + rect.ty*dbpl + rect.lx*bpp;
223b71206888 Initial import
thib
parents:
diff changeset
222 unsigned int pixel = SDL_MapRGBA(dst->format, r, g, b, 0);
223b71206888 Initial import
thib
parents:
diff changeset
223 unsigned int pixela = SDL_MapRGBA(dst->format, r, g, b, a);
65
4416cfac86ae Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
224 a += a>>7; /* 0-256 にする */
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
225 int i, j;
0
223b71206888 Initial import
thib
parents:
diff changeset
226 for (i=0; i<height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
227 char* d = dmem;
223b71206888 Initial import
thib
parents:
diff changeset
228 if (bpp == 4) {
223b71206888 Initial import
thib
parents:
diff changeset
229 for (j=0; j<width; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
230 int alpha = (*(Uint32*)d)>>ashift;
223b71206888 Initial import
thib
parents:
diff changeset
231 if (alpha == 0) (*(Uint32*)d) = pixel;
223b71206888 Initial import
thib
parents:
diff changeset
232 else if (alpha == 255) *((Uint32*)d) = pixela;
223b71206888 Initial import
thib
parents:
diff changeset
233 else { *((Uint32*)d) = pixel | ((((alpha*a)>>8)<<ashift)&amask); }
223b71206888 Initial import
thib
parents:
diff changeset
234 d += bpp;
223b71206888 Initial import
thib
parents:
diff changeset
235 }
223b71206888 Initial import
thib
parents:
diff changeset
236 } else if (bpp == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
237 for (j=0; j<width; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
238 int alpha = (*(Uint16*)d)>>ashift;
223b71206888 Initial import
thib
parents:
diff changeset
239 if (alpha == 0) *((Uint16*)d) = pixel;
223b71206888 Initial import
thib
parents:
diff changeset
240 else if (alpha == 255) *((Uint16*)d) = pixela;
223b71206888 Initial import
thib
parents:
diff changeset
241 else { *((Uint16*)d) = pixel | ((((alpha*a)>>8)<<ashift)&amask); }
223b71206888 Initial import
thib
parents:
diff changeset
242 d += bpp;
223b71206888 Initial import
thib
parents:
diff changeset
243 }
223b71206888 Initial import
thib
parents:
diff changeset
244 }
223b71206888 Initial import
thib
parents:
diff changeset
245 dmem += dbpl;
223b71206888 Initial import
thib
parents:
diff changeset
246 }
223b71206888 Initial import
thib
parents:
diff changeset
247 SDL_UnlockSurface( dst);
223b71206888 Initial import
thib
parents:
diff changeset
248 }
223b71206888 Initial import
thib
parents:
diff changeset
249
223b71206888 Initial import
thib
parents:
diff changeset
250 #define ASHIFT 24
223b71206888 Initial import
thib
parents:
diff changeset
251 #define CMASK1 0xff00ff
223b71206888 Initial import
thib
parents:
diff changeset
252 #define CMASK2 0x00ff00
223b71206888 Initial import
thib
parents:
diff changeset
253
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
254 inline void blit_pixel(Uint32* dest, Uint32* src, const unsigned char* alpha, bool use_srcalpha) {
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
255 Uint32 dest_value = *dest;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
256 Uint32 src_value = *src;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
257 Uint32 as = src_value >> ASHIFT;
0
223b71206888 Initial import
thib
parents:
diff changeset
258 if (as == 255 || (!use_srcalpha) ) {
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
259 as = *alpha;
0
223b71206888 Initial import
thib
parents:
diff changeset
260 } else {
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
261 as += (as >> 7); /* 0-0xff -> 0-0x100 */
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
262 as *= *alpha;
0
223b71206888 Initial import
thib
parents:
diff changeset
263 as >>= 8;
223b71206888 Initial import
thib
parents:
diff changeset
264 }
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
265 as += as >> 7;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
266 // Isolate Red and Blue components
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
267 Uint32 src_c1 = src_value & CMASK1;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
268 Uint32 dest_c1 = dest_value & CMASK1;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
269 // Blend Red and Blue components
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
270 dest_c1 = (dest_c1 + (((src_c1-dest_c1) * as) >> 8)) & CMASK1;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
271 // Isolate Green component
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
272 src_value &= CMASK2;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
273 dest_value &= CMASK2;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
274 // Blend Green component
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
275 dest_value = (dest_value + (((src_value-dest_value) * as) >> 8)) & CMASK2;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
276 // Put it alltogether
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
277 *dest = dest_c1 | dest_value | 0xff000000;
0
223b71206888 Initial import
thib
parents:
diff changeset
278 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
279
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
280 static void blit_line(Uint32* dest, Uint32* src, const unsigned char* alpha,int ax0, int ax1, int awidth, int aj0, int aj1, bool use_srcalpha) {
0
223b71206888 Initial import
thib
parents:
diff changeset
281 int j;
223b71206888 Initial import
thib
parents:
diff changeset
282 int ax = ax0;
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
283 const unsigned char* a = alpha + ax0;
65
4416cfac86ae Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
284 if (awidth == 1) { // わりとよくあるので最適化
0
223b71206888 Initial import
thib
parents:
diff changeset
285 for (j=aj0; j < aj1; j++) {
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
286 blit_pixel(dest++, src++, alpha, use_srcalpha);
0
223b71206888 Initial import
thib
parents:
diff changeset
287 }
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
288 }
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
289 else
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
290 {
0
223b71206888 Initial import
thib
parents:
diff changeset
291 for (j=aj0; j < aj1; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
292 for (; ax<awidth; ax++)
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
293 blit_pixel(dest++, src++, a++, use_srcalpha);
0
223b71206888 Initial import
thib
parents:
diff changeset
294 ax = 0;
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
295 a = alpha;
0
223b71206888 Initial import
thib
parents:
diff changeset
296 }
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
297 for (; ax < ax1; ax++)
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
298 blit_pixel(dest++, src++, a++, use_srcalpha);
0
223b71206888 Initial import
thib
parents:
diff changeset
299 }
223b71206888 Initial import
thib
parents:
diff changeset
300 }
223b71206888 Initial import
thib
parents:
diff changeset
301
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
302 void DSurfaceBlitAlpha(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, const unsigned char* alpha, const Rect& alpharect)
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
303 {
0
223b71206888 Initial import
thib
parents:
diff changeset
304 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
305 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
306 SDL_PixelFormat& fmt = *dst->format;
223b71206888 Initial import
thib
parents:
diff changeset
307
223b71206888 Initial import
thib
parents:
diff changeset
308 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
309 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
310
223b71206888 Initial import
thib
parents:
diff changeset
311 int awidth = alpharect.width();
223b71206888 Initial import
thib
parents:
diff changeset
312 int aheight = alpharect.height();
223b71206888 Initial import
thib
parents:
diff changeset
313 if (awidth == 0 || aheight == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
314 int ax0 = srcrect.lx % awidth;
223b71206888 Initial import
thib
parents:
diff changeset
315 int ay0 = srcrect.ty % aheight;
223b71206888 Initial import
thib
parents:
diff changeset
316 int aj0 = srcrect.lx / awidth;
223b71206888 Initial import
thib
parents:
diff changeset
317 int ai0 = srcrect.ty / aheight;
223b71206888 Initial import
thib
parents:
diff changeset
318 int ax1 = srcrect.rx % awidth;
223b71206888 Initial import
thib
parents:
diff changeset
319 int ay1 = srcrect.by % aheight;
223b71206888 Initial import
thib
parents:
diff changeset
320 int aj1 = srcrect.rx / awidth;
223b71206888 Initial import
thib
parents:
diff changeset
321 int ai1 = srcrect.by / aheight;
223b71206888 Initial import
thib
parents:
diff changeset
322
223b71206888 Initial import
thib
parents:
diff changeset
323 SDL_LockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
324 SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
325 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
326 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
327 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
328
223b71206888 Initial import
thib
parents:
diff changeset
329 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
330 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
331
223b71206888 Initial import
thib
parents:
diff changeset
332 const unsigned char* amem = alpha + ay0 * awidth;
223b71206888 Initial import
thib
parents:
diff changeset
333 int i;
223b71206888 Initial import
thib
parents:
diff changeset
334 int ay = ay0;
223b71206888 Initial import
thib
parents:
diff changeset
335 for (i = ai0; i < ai1; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
336 for (; ay < aheight; ay++) {
223b71206888 Initial import
thib
parents:
diff changeset
337 if (src->format->Amask == 0)
223b71206888 Initial import
thib
parents:
diff changeset
338 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, false);
223b71206888 Initial import
thib
parents:
diff changeset
339 else
223b71206888 Initial import
thib
parents:
diff changeset
340 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, true);
223b71206888 Initial import
thib
parents:
diff changeset
341 amem += awidth; dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
342 }
223b71206888 Initial import
thib
parents:
diff changeset
343 ay = 0; amem = alpha;
223b71206888 Initial import
thib
parents:
diff changeset
344 }
223b71206888 Initial import
thib
parents:
diff changeset
345 for (; ay < ay1; ay++) {
223b71206888 Initial import
thib
parents:
diff changeset
346 if (src->format->Amask == 0)
223b71206888 Initial import
thib
parents:
diff changeset
347 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, false);
223b71206888 Initial import
thib
parents:
diff changeset
348 else
223b71206888 Initial import
thib
parents:
diff changeset
349 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, true);
223b71206888 Initial import
thib
parents:
diff changeset
350 amem += awidth; dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
351 }
223b71206888 Initial import
thib
parents:
diff changeset
352 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
353 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
354 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
355
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
356 void DSurfaceBlitAdd(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, unsigned char alpha) {
0
223b71206888 Initial import
thib
parents:
diff changeset
357 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
358 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
359
65
4416cfac86ae Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
360 if (dst->format->BytesPerPixel != src->format->BytesPerPixel) return; // RGB変換はできない
0
223b71206888 Initial import
thib
parents:
diff changeset
361
223b71206888 Initial import
thib
parents:
diff changeset
362 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
363 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
364
223b71206888 Initial import
thib
parents:
diff changeset
365 SDL_LockSurface(dst); SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
366 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
367 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
368 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
369 int width = srcrect.width();
223b71206888 Initial import
thib
parents:
diff changeset
370 int height = srcrect.height();
223b71206888 Initial import
thib
parents:
diff changeset
371 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
372 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
373 char* smem_end = src_o->mem_end(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
374 char* dmem_end = dst_o->mem_end(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
375
223b71206888 Initial import
thib
parents:
diff changeset
376 SDL_PixelFormat& fmt = *dst->format;
223b71206888 Initial import
thib
parents:
diff changeset
377 int rshift = fmt.Rshift - fmt.Rloss; int rmask = fmt.Rmask;
223b71206888 Initial import
thib
parents:
diff changeset
378 int gshift = fmt.Gshift - fmt.Gloss; int gmask = fmt.Gmask;
223b71206888 Initial import
thib
parents:
diff changeset
379 int bshift = fmt.Bshift - fmt.Bloss; int bmask = fmt.Bmask;
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
380 int ashift = src->format->Ashift - src->format->Aloss; int amask = src->format->Amask;
0
223b71206888 Initial import
thib
parents:
diff changeset
381 int allmask = rmask | gmask | bmask;
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
382 int i, j;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
383 for (i=0; i < height; i++) {
0
223b71206888 Initial import
thib
parents:
diff changeset
384 char* d = dmem; char* s = smem;
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
385 for (j=0; j < width; j++) {
0
223b71206888 Initial import
thib
parents:
diff changeset
386 Uint32 sd = *(Uint32*)s;
223b71206888 Initial import
thib
parents:
diff changeset
387 Uint32 dd = *(Uint32*)d;
223b71206888 Initial import
thib
parents:
diff changeset
388 if (sd&allmask) {
223b71206888 Initial import
thib
parents:
diff changeset
389 Uint32 sr = (sd&rmask)>>rshift;
223b71206888 Initial import
thib
parents:
diff changeset
390 Uint32 sg = (sd&gmask)>>gshift;
223b71206888 Initial import
thib
parents:
diff changeset
391 Uint32 sb = (sd&bmask)>>bshift;
60
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
392 Uint32 alpha2 = alpha;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
393 if (amask)
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
394 {
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
395 alpha2 = ((sd&amask)>>ashift);
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
396 alpha2 += alpha2 >> 7;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
397 alpha2 *= alpha;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
398 alpha2 >>= 8;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
399 }
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
400
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
401 if (alpha2 != ALPHA_MAX) {
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
402 sr = (sr*alpha2)>>8;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
403 sg = (sg*alpha2)>>8;
e16e13d8cd68 Replaced SATURATE -> ADD, implemented objComposite, corrected minor things
Thibaut GIRKA <thib@sitedethib.com>
parents: 56
diff changeset
404 sb = (sb*alpha2)>>8;
0
223b71206888 Initial import
thib
parents:
diff changeset
405 }
223b71206888 Initial import
thib
parents:
diff changeset
406 Uint32 dr = sr + ((dd&rmask)>>rshift);
223b71206888 Initial import
thib
parents:
diff changeset
407 Uint32 dg = sg + ((dd&gmask)>>gshift);
223b71206888 Initial import
thib
parents:
diff changeset
408 Uint32 db = sb + ((dd&bmask)>>bshift);
223b71206888 Initial import
thib
parents:
diff changeset
409 if (dr > 255) dr = 255;
223b71206888 Initial import
thib
parents:
diff changeset
410 if (dg > 255) dg = 255;
223b71206888 Initial import
thib
parents:
diff changeset
411 if (db > 255) db = 255;
223b71206888 Initial import
thib
parents:
diff changeset
412 *(Uint32*)d = ((dr<<rshift)&rmask)| ((dg<<gshift)&gmask)| ((db<<bshift)&bmask);
223b71206888 Initial import
thib
parents:
diff changeset
413 }
223b71206888 Initial import
thib
parents:
diff changeset
414 s += bpp; d += bpp;
223b71206888 Initial import
thib
parents:
diff changeset
415 }
223b71206888 Initial import
thib
parents:
diff changeset
416 dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
417 }
223b71206888 Initial import
thib
parents:
diff changeset
418 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
419 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
420 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
421
0
223b71206888 Initial import
thib
parents:
diff changeset
422 void DSurfaceBlitMultiply(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o) {
223b71206888 Initial import
thib
parents:
diff changeset
423 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
424 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
425
223b71206888 Initial import
thib
parents:
diff changeset
426 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
427 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
428
223b71206888 Initial import
thib
parents:
diff changeset
429 SDL_LockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
430 SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
431 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
432 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
433 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
434 int width = srcrect.width();
223b71206888 Initial import
thib
parents:
diff changeset
435 int height = srcrect.height();
223b71206888 Initial import
thib
parents:
diff changeset
436 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
437 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
438
223b71206888 Initial import
thib
parents:
diff changeset
439 SDL_PixelFormat& fmt = *dst->format;
65
4416cfac86ae Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
440 /* dst の 0-255 を 0-pixel に変換する(積算) */
0
223b71206888 Initial import
thib
parents:
diff changeset
441 int i;
223b71206888 Initial import
thib
parents:
diff changeset
442 int table1[256], table2[256], table3[256];
223b71206888 Initial import
thib
parents:
diff changeset
443 Uint32 src_pixel = *(Uint32*)smem;
223b71206888 Initial import
thib
parents:
diff changeset
444 src_pixel |= 0xff000000;
223b71206888 Initial import
thib
parents:
diff changeset
445 int c1=src_pixel&255, c2=(src_pixel>>8)&255, c3=(src_pixel>>16)&255;
223b71206888 Initial import
thib
parents:
diff changeset
446 for (i=0; i<256; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
447 int n = i + (i>>7);
223b71206888 Initial import
thib
parents:
diff changeset
448 table1[i] = (c1*n)>>8;
223b71206888 Initial import
thib
parents:
diff changeset
449 table2[i] = (c2*n)&0xff00;
223b71206888 Initial import
thib
parents:
diff changeset
450 table3[i] = ((c3*n)<<8) & 0xff0000;
223b71206888 Initial import
thib
parents:
diff changeset
451 }
223b71206888 Initial import
thib
parents:
diff changeset
452 int err_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
453 for (i=0; i<height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
454 Uint32* d = (Uint32*)dmem;
223b71206888 Initial import
thib
parents:
diff changeset
455 Uint32* s = (Uint32*)smem;
223b71206888 Initial import
thib
parents:
diff changeset
456 int j; for (j=0; j<width; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
457 Uint32 dd = *d;
223b71206888 Initial import
thib
parents:
diff changeset
458 Uint32 ss = *s;
223b71206888 Initial import
thib
parents:
diff changeset
459 if (ss == src_pixel) {
223b71206888 Initial import
thib
parents:
diff changeset
460 *(Uint32*)d = table1[ dd & 0xff] | table2[ (dd>>8) & 0xff] | table3[ (dd>>16) & 0xff];
223b71206888 Initial import
thib
parents:
diff changeset
461 } else if ( ((ss^src_pixel) & 0xffffff) == 0) { // r,g,b of ss == src_pixel
223b71206888 Initial import
thib
parents:
diff changeset
462 Uint32 as = ss>>ASHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
463 as += as>>7;
223b71206888 Initial import
thib
parents:
diff changeset
464 ss = table1[ dd & 0xff] | table2[ (dd>>8) & 0xff] | table3[ (dd>>16) & 0xff];
223b71206888 Initial import
thib
parents:
diff changeset
465 Uint32 s1 = ss&CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
466 Uint32 d1 = dd&CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
467 d1 = (d1 + (((s1-d1) * as) >> 8)) & CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
468 ss &= CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
469 dd &= CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
470 dd = (dd + (((ss-dd) * as) >> 8)) & CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
471 *(Uint32*)d = d1 | dd | 0xff000000;
223b71206888 Initial import
thib
parents:
diff changeset
472 } else err_count++;
223b71206888 Initial import
thib
parents:
diff changeset
473 d++; s++;
223b71206888 Initial import
thib
parents:
diff changeset
474 }
223b71206888 Initial import
thib
parents:
diff changeset
475 dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
476 }
223b71206888 Initial import
thib
parents:
diff changeset
477 if (err_count) {
223b71206888 Initial import
thib
parents:
diff changeset
478 fprintf(stderr,"multipy_blit : surface does not have unique color(%08x); %d pixels in width %d, height %d\n",
223b71206888 Initial import
thib
parents:
diff changeset
479 src_pixel, err_count, width, height);
223b71206888 Initial import
thib
parents:
diff changeset
480 }
223b71206888 Initial import
thib
parents:
diff changeset
481 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
482 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
483 }