annotate window/render.cc @ 56:c7bcc0ec2267

* replaced Grp and Text classes by the TextImpl and GrpImpl ones * splitted scn2k.h into smaller header files * moved some definitions from scn2k_*.cc to the header files * moved opcode implementation to scn2k_*impl.cc
author thib
date Thu, 30 Apr 2009 19:05:09 +0000
parents 15a18fbe6f21
children e16e13d8cd68
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
223b71206888 Initial import
thib
parents:
diff changeset
175 if (dst->format->BytesPerPixel != src->format->BytesPerPixel) return; // RGB変換はできない
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
223b71206888 Initial import
thib
parents:
diff changeset
191 // メモリに重なりがあり、src が上位側の場合、コピー方向を逆転する
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);
223b71206888 Initial import
thib
parents:
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
223b71206888 Initial import
thib
parents:
diff changeset
254 inline void blit_pixel(Uint32* dmem, Uint32* smem, const unsigned char* amem, bool use_srcalpha) {
223b71206888 Initial import
thib
parents:
diff changeset
255 Uint32 d = *dmem;
223b71206888 Initial import
thib
parents:
diff changeset
256 Uint32 s = *smem;
223b71206888 Initial import
thib
parents:
diff changeset
257 Uint32 as = s>>ASHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
258 if (as == 255 || (!use_srcalpha) ) {
223b71206888 Initial import
thib
parents:
diff changeset
259 as = *amem;
223b71206888 Initial import
thib
parents:
diff changeset
260 } else {
223b71206888 Initial import
thib
parents:
diff changeset
261 as += as>>7; /* 0-0xff -> 0-0x100 */
223b71206888 Initial import
thib
parents:
diff changeset
262 as *= *amem;
223b71206888 Initial import
thib
parents:
diff changeset
263 as >>= 8;
223b71206888 Initial import
thib
parents:
diff changeset
264 }
223b71206888 Initial import
thib
parents:
diff changeset
265 as += as>>7;
223b71206888 Initial import
thib
parents:
diff changeset
266 Uint32 s1 = s & CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
267 Uint32 d1 = d & CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
268 d1 = (d1 + (((s1-d1) * as) >> 8)) & CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
269 s &= CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
270 d &= CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
271 d = (d + (((s-d) * as) >> 8)) & CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
272 *dmem = d1 | d | 0xff000000;
223b71206888 Initial import
thib
parents:
diff changeset
273 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
274
0
223b71206888 Initial import
thib
parents:
diff changeset
275 static void blit_line(Uint32* dmem, Uint32* smem, const unsigned char* amem,int ax0, int ax1, int awidth, int aj0, int aj1, bool use_srcalpha) {
223b71206888 Initial import
thib
parents:
diff changeset
276 int j;
223b71206888 Initial import
thib
parents:
diff changeset
277 int ax = ax0;
223b71206888 Initial import
thib
parents:
diff changeset
278 const unsigned char* a = amem + ax0;
223b71206888 Initial import
thib
parents:
diff changeset
279 Uint32* d = dmem;
223b71206888 Initial import
thib
parents:
diff changeset
280 Uint32* s = smem;
223b71206888 Initial import
thib
parents:
diff changeset
281 if (awidth == 1) { // わりとよくあるので最適化
223b71206888 Initial import
thib
parents:
diff changeset
282 for (j=aj0; j < aj1; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
283 blit_pixel(d++, s++, amem, use_srcalpha);
223b71206888 Initial import
thib
parents:
diff changeset
284 }
223b71206888 Initial import
thib
parents:
diff changeset
285 } else {
223b71206888 Initial import
thib
parents:
diff changeset
286 for (j=aj0; j < aj1; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
287 for (; ax<awidth; ax++)
223b71206888 Initial import
thib
parents:
diff changeset
288 blit_pixel(d++, s++, a++, use_srcalpha);
223b71206888 Initial import
thib
parents:
diff changeset
289 ax = 0;
223b71206888 Initial import
thib
parents:
diff changeset
290 a = amem;
223b71206888 Initial import
thib
parents:
diff changeset
291 }
223b71206888 Initial import
thib
parents:
diff changeset
292 for (; ax < ax1; ax++) blit_pixel(d++, s++, a++, use_srcalpha);
223b71206888 Initial import
thib
parents:
diff changeset
293 }
223b71206888 Initial import
thib
parents:
diff changeset
294 }
223b71206888 Initial import
thib
parents:
diff changeset
295
223b71206888 Initial import
thib
parents:
diff changeset
296 void DSurfaceBlitAlpha(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, const unsigned char* alpha, const Rect& alpharect) {
223b71206888 Initial import
thib
parents:
diff changeset
297 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
298 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
299 SDL_PixelFormat& fmt = *dst->format;
223b71206888 Initial import
thib
parents:
diff changeset
300
223b71206888 Initial import
thib
parents:
diff changeset
301 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
302 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
303
223b71206888 Initial import
thib
parents:
diff changeset
304 int awidth = alpharect.width();
223b71206888 Initial import
thib
parents:
diff changeset
305 int aheight = alpharect.height();
223b71206888 Initial import
thib
parents:
diff changeset
306 if (awidth == 0 || aheight == 0) return;
223b71206888 Initial import
thib
parents:
diff changeset
307 int ax0 = srcrect.lx % awidth;
223b71206888 Initial import
thib
parents:
diff changeset
308 int ay0 = srcrect.ty % aheight;
223b71206888 Initial import
thib
parents:
diff changeset
309 int aj0 = srcrect.lx / awidth;
223b71206888 Initial import
thib
parents:
diff changeset
310 int ai0 = srcrect.ty / aheight;
223b71206888 Initial import
thib
parents:
diff changeset
311 int ax1 = srcrect.rx % awidth;
223b71206888 Initial import
thib
parents:
diff changeset
312 int ay1 = srcrect.by % aheight;
223b71206888 Initial import
thib
parents:
diff changeset
313 int aj1 = srcrect.rx / awidth;
223b71206888 Initial import
thib
parents:
diff changeset
314 int ai1 = srcrect.by / aheight;
223b71206888 Initial import
thib
parents:
diff changeset
315
223b71206888 Initial import
thib
parents:
diff changeset
316 SDL_LockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
317 SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
318 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
319 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
320 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
321
223b71206888 Initial import
thib
parents:
diff changeset
322 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
323 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
324
223b71206888 Initial import
thib
parents:
diff changeset
325 const unsigned char* amem = alpha + ay0 * awidth;
223b71206888 Initial import
thib
parents:
diff changeset
326 int i;
223b71206888 Initial import
thib
parents:
diff changeset
327 int ay = ay0;
223b71206888 Initial import
thib
parents:
diff changeset
328 for (i = ai0; i < ai1; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
329 for (; ay < aheight; ay++) {
223b71206888 Initial import
thib
parents:
diff changeset
330 if (src->format->Amask == 0)
223b71206888 Initial import
thib
parents:
diff changeset
331 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, false);
223b71206888 Initial import
thib
parents:
diff changeset
332 else
223b71206888 Initial import
thib
parents:
diff changeset
333 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, true);
223b71206888 Initial import
thib
parents:
diff changeset
334 amem += awidth; dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
335 }
223b71206888 Initial import
thib
parents:
diff changeset
336 ay = 0; amem = alpha;
223b71206888 Initial import
thib
parents:
diff changeset
337 }
223b71206888 Initial import
thib
parents:
diff changeset
338 for (; ay < ay1; ay++) {
223b71206888 Initial import
thib
parents:
diff changeset
339 if (src->format->Amask == 0)
223b71206888 Initial import
thib
parents:
diff changeset
340 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, false);
223b71206888 Initial import
thib
parents:
diff changeset
341 else
223b71206888 Initial import
thib
parents:
diff changeset
342 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, true);
223b71206888 Initial import
thib
parents:
diff changeset
343 amem += awidth; dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
344 }
223b71206888 Initial import
thib
parents:
diff changeset
345 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
346 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
347 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
348
0
223b71206888 Initial import
thib
parents:
diff changeset
349 void DSurfaceBlitSaturate(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, unsigned char alpha) {
223b71206888 Initial import
thib
parents:
diff changeset
350 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
351 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
352
223b71206888 Initial import
thib
parents:
diff changeset
353 if (dst->format->BytesPerPixel != src->format->BytesPerPixel) return; // RGB変換はできない
223b71206888 Initial import
thib
parents:
diff changeset
354
223b71206888 Initial import
thib
parents:
diff changeset
355 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
356 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
357
223b71206888 Initial import
thib
parents:
diff changeset
358 SDL_LockSurface(dst); SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
359 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
360 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
361 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
362 int width = srcrect.width();
223b71206888 Initial import
thib
parents:
diff changeset
363 int height = srcrect.height();
223b71206888 Initial import
thib
parents:
diff changeset
364 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
365 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
366 char* smem_end = src_o->mem_end(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
367 char* dmem_end = dst_o->mem_end(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
368
223b71206888 Initial import
thib
parents:
diff changeset
369 SDL_PixelFormat& fmt = *dst->format;
223b71206888 Initial import
thib
parents:
diff changeset
370 int rshift = fmt.Rshift - fmt.Rloss; int rmask = fmt.Rmask;
223b71206888 Initial import
thib
parents:
diff changeset
371 int gshift = fmt.Gshift - fmt.Gloss; int gmask = fmt.Gmask;
223b71206888 Initial import
thib
parents:
diff changeset
372 int bshift = fmt.Bshift - fmt.Bloss; int bmask = fmt.Bmask;
223b71206888 Initial import
thib
parents:
diff changeset
373 int allmask = rmask | gmask | bmask;
223b71206888 Initial import
thib
parents:
diff changeset
374 int i;
223b71206888 Initial import
thib
parents:
diff changeset
375 for (i=0; i<height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
376 char* d = dmem; char* s = smem;
223b71206888 Initial import
thib
parents:
diff changeset
377 int j; for (j=0; j<width; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
378 Uint32 sd = *(Uint32*)s;
223b71206888 Initial import
thib
parents:
diff changeset
379 Uint32 dd = *(Uint32*)d;
223b71206888 Initial import
thib
parents:
diff changeset
380 if (sd&allmask) {
223b71206888 Initial import
thib
parents:
diff changeset
381 Uint32 sr = (sd&rmask)>>rshift;
223b71206888 Initial import
thib
parents:
diff changeset
382 Uint32 sg = (sd&gmask)>>gshift;
223b71206888 Initial import
thib
parents:
diff changeset
383 Uint32 sb = (sd&bmask)>>bshift;
223b71206888 Initial import
thib
parents:
diff changeset
384 if (alpha != ALPHA_MAX) {
223b71206888 Initial import
thib
parents:
diff changeset
385 sr = (sr*alpha)>>8;
223b71206888 Initial import
thib
parents:
diff changeset
386 sg = (sg*alpha)>>8;
223b71206888 Initial import
thib
parents:
diff changeset
387 sb = (sb*alpha)>>8;
223b71206888 Initial import
thib
parents:
diff changeset
388 }
223b71206888 Initial import
thib
parents:
diff changeset
389 Uint32 dr = sr + ((dd&rmask)>>rshift);
223b71206888 Initial import
thib
parents:
diff changeset
390 Uint32 dg = sg + ((dd&gmask)>>gshift);
223b71206888 Initial import
thib
parents:
diff changeset
391 Uint32 db = sb + ((dd&bmask)>>bshift);
223b71206888 Initial import
thib
parents:
diff changeset
392 if (dr > 255) dr = 255;
223b71206888 Initial import
thib
parents:
diff changeset
393 if (dg > 255) dg = 255;
223b71206888 Initial import
thib
parents:
diff changeset
394 if (db > 255) db = 255;
223b71206888 Initial import
thib
parents:
diff changeset
395 *(Uint32*)d = ((dr<<rshift)&rmask)| ((dg<<gshift)&gmask)| ((db<<bshift)&bmask);
223b71206888 Initial import
thib
parents:
diff changeset
396 }
223b71206888 Initial import
thib
parents:
diff changeset
397 s += bpp; d += bpp;
223b71206888 Initial import
thib
parents:
diff changeset
398 }
223b71206888 Initial import
thib
parents:
diff changeset
399 dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
400 }
223b71206888 Initial import
thib
parents:
diff changeset
401 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
402 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
403 }
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
404
0
223b71206888 Initial import
thib
parents:
diff changeset
405 void DSurfaceBlitMultiply(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o) {
223b71206888 Initial import
thib
parents:
diff changeset
406 SDL_Surface* dst = (SDL_Surface*)dst_o;
223b71206888 Initial import
thib
parents:
diff changeset
407 SDL_Surface* src = (SDL_Surface*)src_o;
223b71206888 Initial import
thib
parents:
diff changeset
408
223b71206888 Initial import
thib
parents:
diff changeset
409 Rect srcrect(srcrect_o), dstrect(dstrect_o);
223b71206888 Initial import
thib
parents:
diff changeset
410 clip_rect(srcrect, dstrect, dst);
223b71206888 Initial import
thib
parents:
diff changeset
411
223b71206888 Initial import
thib
parents:
diff changeset
412 SDL_LockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
413 SDL_LockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
414 int dbpl = dst->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
415 int sbpl = src->pitch;
223b71206888 Initial import
thib
parents:
diff changeset
416 int bpp = dst->format->BytesPerPixel;
223b71206888 Initial import
thib
parents:
diff changeset
417 int width = srcrect.width();
223b71206888 Initial import
thib
parents:
diff changeset
418 int height = srcrect.height();
223b71206888 Initial import
thib
parents:
diff changeset
419 char* dmem = dst_o->mem(dstrect);
223b71206888 Initial import
thib
parents:
diff changeset
420 char* smem = src_o->mem(srcrect);
223b71206888 Initial import
thib
parents:
diff changeset
421
223b71206888 Initial import
thib
parents:
diff changeset
422 SDL_PixelFormat& fmt = *dst->format;
223b71206888 Initial import
thib
parents:
diff changeset
423 /* dst の 0-255 を 0-pixel に変換する(積算) */
223b71206888 Initial import
thib
parents:
diff changeset
424 int i;
223b71206888 Initial import
thib
parents:
diff changeset
425 int table1[256], table2[256], table3[256];
223b71206888 Initial import
thib
parents:
diff changeset
426 Uint32 src_pixel = *(Uint32*)smem;
223b71206888 Initial import
thib
parents:
diff changeset
427 src_pixel |= 0xff000000;
223b71206888 Initial import
thib
parents:
diff changeset
428 int c1=src_pixel&255, c2=(src_pixel>>8)&255, c3=(src_pixel>>16)&255;
223b71206888 Initial import
thib
parents:
diff changeset
429 for (i=0; i<256; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
430 int n = i + (i>>7);
223b71206888 Initial import
thib
parents:
diff changeset
431 table1[i] = (c1*n)>>8;
223b71206888 Initial import
thib
parents:
diff changeset
432 table2[i] = (c2*n)&0xff00;
223b71206888 Initial import
thib
parents:
diff changeset
433 table3[i] = ((c3*n)<<8) & 0xff0000;
223b71206888 Initial import
thib
parents:
diff changeset
434 }
223b71206888 Initial import
thib
parents:
diff changeset
435 int err_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
436 for (i=0; i<height; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
437 Uint32* d = (Uint32*)dmem;
223b71206888 Initial import
thib
parents:
diff changeset
438 Uint32* s = (Uint32*)smem;
223b71206888 Initial import
thib
parents:
diff changeset
439 int j; for (j=0; j<width; j++) {
223b71206888 Initial import
thib
parents:
diff changeset
440 Uint32 dd = *d;
223b71206888 Initial import
thib
parents:
diff changeset
441 Uint32 ss = *s;
223b71206888 Initial import
thib
parents:
diff changeset
442 if (ss == src_pixel) {
223b71206888 Initial import
thib
parents:
diff changeset
443 *(Uint32*)d = table1[ dd & 0xff] | table2[ (dd>>8) & 0xff] | table3[ (dd>>16) & 0xff];
223b71206888 Initial import
thib
parents:
diff changeset
444 } else if ( ((ss^src_pixel) & 0xffffff) == 0) { // r,g,b of ss == src_pixel
223b71206888 Initial import
thib
parents:
diff changeset
445 Uint32 as = ss>>ASHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
446 as += as>>7;
223b71206888 Initial import
thib
parents:
diff changeset
447 ss = table1[ dd & 0xff] | table2[ (dd>>8) & 0xff] | table3[ (dd>>16) & 0xff];
223b71206888 Initial import
thib
parents:
diff changeset
448 Uint32 s1 = ss&CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
449 Uint32 d1 = dd&CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
450 d1 = (d1 + (((s1-d1) * as) >> 8)) & CMASK1;
223b71206888 Initial import
thib
parents:
diff changeset
451 ss &= CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
452 dd &= CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
453 dd = (dd + (((ss-dd) * as) >> 8)) & CMASK2;
223b71206888 Initial import
thib
parents:
diff changeset
454 *(Uint32*)d = d1 | dd | 0xff000000;
223b71206888 Initial import
thib
parents:
diff changeset
455 } else err_count++;
223b71206888 Initial import
thib
parents:
diff changeset
456 d++; s++;
223b71206888 Initial import
thib
parents:
diff changeset
457 }
223b71206888 Initial import
thib
parents:
diff changeset
458 dmem += dbpl; smem += sbpl;
223b71206888 Initial import
thib
parents:
diff changeset
459 }
223b71206888 Initial import
thib
parents:
diff changeset
460 if (err_count) {
223b71206888 Initial import
thib
parents:
diff changeset
461 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
462 src_pixel, err_count, width, height);
223b71206888 Initial import
thib
parents:
diff changeset
463 }
223b71206888 Initial import
thib
parents:
diff changeset
464 SDL_UnlockSurface(dst);
223b71206888 Initial import
thib
parents:
diff changeset
465 SDL_UnlockSurface(src);
223b71206888 Initial import
thib
parents:
diff changeset
466 }