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