comparison window/render.cc @ 0:223b71206888

Initial import
author thib
date Fri, 01 Aug 2008 16:32:45 +0000
parents
children 15a18fbe6f21
comparison
equal deleted inserted replaced
-1:000000000000 0:223b71206888
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
28 #include"font/font.h"
29 #include"font/text.h"
30 #include"rect.h"
31 #include"SDL.h"
32 #include"surface.h"
33 #include<stdio.h>
34
35 Rect DSurfaceRenderText(TextGlyphStream::iterator start, TextGlyphStream::iterator end, const Rect& srcrect,
36 Surface* dst, const Rect& dstrect); // 文字描画
37 void DSurfaceFill(Surface* src, const Rect& rect, int r, int g, int b, int a); // クリア
38 void DSurfaceFillA(Surface* src, const Rect& rect, int r, int g, int b, int a); // テキストウィンドウ背景の設定
39 void DSurfaceMove(Surface* src_o, const Rect& srcrect, Surface* dst_o, const Rect& dstrect); // コピー
40 void DSurfaceBlitAlpha(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, const unsigned char* alpha, const Rect& alpharect);
41 void DSurfaceBlitSaturate(Surface* src_o, const Rect& srcrect, Surface* dst_o, const Rect& dstrect, unsigned char alpha);
42 void DSurfaceBlitMultiply(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o);
43
44 #ifndef ALPHA_MAX
45 #define ALPHA_MAX 255
46 #endif
47
48 Rect::Rect(const TextGlyph& letter) {
49 lx = letter.x + letter.glyph->bitmap_left;
50 rx = lx + letter.glyph->bitmap.width;
51
52 ty = letter.y - letter.glyph->bitmap_top;
53 by = ty + letter.glyph->bitmap.rows;
54 }
55
56 Rect::Rect(const class Surface& so) {
57 SDL_Surface* s = (SDL_Surface*)(&so);
58 lx = 0; ty = 0;
59 rx = s->w; by = s->h;
60 }
61 inline Rect _Rect(const SDL_Rect& r) {
62 return Rect(r.x, r.y, r.x+r.w, r.y+r.h);
63 };
64 inline SDL_Rect SDLed(const Rect& rect) {
65 SDL_Rect r;
66 r.x = rect.lx;
67 r.y = rect.ty;
68 r.w = rect.rx-rect.lx;
69 r.h = rect.by-rect.ty;
70 return r;
71 }
72
73 Rect DSurfaceRenderText(TextGlyphStream::iterator start, TextGlyphStream::iterator end, const Rect& srcrect,
74 Surface* dst_o, const Rect& dstrect) {
75
76 SDL_Surface* dst = (SDL_Surface*)dst_o;
77 Rect confine_to(srcrect);
78 Rect dst_clip(_Rect(dst->clip_rect));
79 Rect drawn_rect(0,0,0,0);
80
81 int dif_x = dstrect.lx - srcrect.lx;
82 int dif_y = dstrect.ty - srcrect.ty;
83
84 dst_clip.rmove(-dif_x, -dif_y);
85 confine_to.intersect(dst_clip);
86
87 TextGlyphStream::iterator it;
88 SDL_PixelFormat& fmt = *dst->format;
89 int ashift = fmt.Ashift - fmt.Aloss; int amask = fmt.Amask;
90 int dbpl = dst->pitch;
91 int dbpp = fmt.BytesPerPixel;
92
93 SDL_LockSurface(dst);
94 for (it=start; it!=end; it++) {
95 Rect letter_r(*it);
96 letter_r.intersect(confine_to);
97 if (letter_r.empty()) continue;
98 drawn_rect.join(letter_r);
99
100 int w = letter_r.width();
101 int h = letter_r.height();
102 int sbpl = it->glyph->bitmap.width;
103
104 unsigned char* smem = it->glyph->bitmap.buffer +
105 (letter_r.ty - (it->y-it->glyph->bitmap_top)) * sbpl +
106 (letter_r.lx - (it->x+it->glyph->bitmap_left));
107 char* dmem = (char*)dst->pixels +
108 (letter_r.ty + dif_y) * dbpl +
109 (letter_r.lx + dif_x) * dbpp;
110 Uint32 pixel = ((int(it->r)<<fmt.Rshift)&fmt.Rmask) |((int(it->g)<<fmt.Gshift)&fmt.Gmask) |((int(it->b)<<fmt.Bshift)&fmt.Bmask);
111 int i,j;
112 if (!it->is_rev) {
113 if (dbpp == 4) {
114 for (i=0; i<h; i++) {
115 for (j=0; j<w; j++) {
116 int alpha = smem[j];
117 ((Uint32*)dmem)[j] = pixel | ((alpha << ashift)&amask);
118 }
119 smem += sbpl; dmem += dbpl;
120 }
121 } else if (dbpp == 2) {
122 for (i=0; i<h; i++) {
123 for (j=0; j<w; j++) {
124 int alpha = smem[j];
125 ((Uint16*)dmem)[j] = pixel | ((alpha << ashift)&amask);
126 }
127 smem += sbpl; dmem += dbpl;
128 }
129 } else abort();
130 } else { /* reversed */
131 if (dbpp == 4) {
132 for (i=0; i<h; i++) {
133 for (j=0; j<w; j++) {
134 int alpha = 255 - smem[j];
135 ((Uint32*)dmem)[j] = pixel | ((alpha << ashift)&amask);
136 }
137 smem += sbpl; dmem += dbpl;
138 }
139 } else if (dbpp == 2) {
140 for (i=0; i<h; i++) {
141 for (j=0; j<w; j++) {
142 int alpha = 255 - smem[j];
143 ((Uint16*)dmem)[j] = pixel | ((alpha << ashift)&amask);
144 }
145 smem += sbpl; dmem += dbpl;
146 }
147 } else abort();
148 }
149 }
150 SDL_UnlockSurface(dst);
151 drawn_rect.rmove(dif_x, dif_y);
152 return drawn_rect;
153 }
154 void DSurfaceFill(Surface* src_o, const Rect& rect, int r, int g, int b, int a) {
155 SDL_Rect sr = SDLed(rect);
156 SDL_Surface* src = (SDL_Surface*)src_o;
157 SDL_FillRect( src, &sr, SDL_MapRGBA(src->format, r, g, b, a));
158 return;
159 }
160
161 static void clip_rect(Rect& srcrect, Rect& dstrect, SDL_Surface* dstsurf) {
162 Rect confine_to(srcrect);
163 Rect dst_clip(_Rect(dstsurf->clip_rect));
164
165 int dif_x = dstrect.lx - srcrect.lx;
166 int dif_y = dstrect.ty - srcrect.ty;
167
168 dst_clip.rmove(-dif_x, -dif_y);
169 confine_to.intersect(dst_clip);
170
171 srcrect = confine_to;
172 dstrect = confine_to;
173 dstrect.rmove(dif_x, dif_y);
174 }
175
176 void DSurfaceMove(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o) {
177
178 SDL_Surface* dst = (SDL_Surface*)dst_o;
179 SDL_Surface* src = (SDL_Surface*)src_o;
180
181 if (dst->format->BytesPerPixel != src->format->BytesPerPixel) return; // RGB変換はできない
182
183 Rect srcrect(srcrect_o), dstrect(dstrect_o);
184 clip_rect(srcrect, dstrect, dst);
185
186 SDL_LockSurface(dst); SDL_LockSurface(src);
187 int sbpl = src->pitch;
188 int dbpl = dst->pitch;
189 int bpp = dst->format->BytesPerPixel;
190 int width = bpp * srcrect.width();
191 int height = srcrect.height();
192 char* smem = src_o->mem(srcrect);
193 char* dmem = dst_o->mem(dstrect);
194 char* smem_end = src_o->mem_end(srcrect);
195 char* dmem_end = dst_o->mem_end(dstrect);
196
197 // メモリに重なりがあり、src が上位側の場合、コピー方向を逆転する
198 if (smem < dmem && dmem < smem_end) {
199 int i,j;
200 for (i=0; i<height; i++) {
201 char* s = smem_end; char* d = dmem_end;
202 for (j=0; j<width; j++)
203 *--d = *--s;
204 dmem_end -= dbpl; smem_end -= sbpl;
205 }
206 } else {
207 int i;
208 for (i=0; i<height; i++) {
209 memcpy(dmem, smem, width);
210 dmem += dbpl; smem += sbpl;
211 }
212 }
213 SDL_UnlockSurface(dst);
214 SDL_UnlockSurface(src);
215 return;
216 }
217
218 void DSurfaceFillA(Surface* dsto, const Rect& rect, int r, int g, int b, int a) {
219 SDL_Surface* dst = (SDL_Surface*)dsto;
220 SDL_LockSurface( dst);
221 int dbpl = dst->pitch;
222 int bpp = dst->format->BytesPerPixel;
223 int width = rect.width();
224 int height = rect.height();
225 int amask = dst->format->Amask;
226 int ashift = dst->format->Ashift - dst->format->Aloss;
227
228 char* dmem = (char*)(dst->pixels) + rect.ty*dbpl + rect.lx*bpp;
229 unsigned int pixel = SDL_MapRGBA(dst->format, r, g, b, 0);
230 unsigned int pixela = SDL_MapRGBA(dst->format, r, g, b, a);
231 a += a>>7; /* 0-256 にする */
232 int i,j;
233 for (i=0; i<height; i++) {
234 char* d = dmem;
235 if (bpp == 4) {
236 for (j=0; j<width; j++) {
237 int alpha = (*(Uint32*)d)>>ashift;
238 if (alpha == 0) (*(Uint32*)d) = pixel;
239 else if (alpha == 255) *((Uint32*)d) = pixela;
240 else { *((Uint32*)d) = pixel | ((((alpha*a)>>8)<<ashift)&amask); }
241 d += bpp;
242 }
243 } else if (bpp == 2) {
244 for (j=0; j<width; j++) {
245 int alpha = (*(Uint16*)d)>>ashift;
246 if (alpha == 0) *((Uint16*)d) = pixel;
247 else if (alpha == 255) *((Uint16*)d) = pixela;
248 else { *((Uint16*)d) = pixel | ((((alpha*a)>>8)<<ashift)&amask); }
249 d += bpp;
250 }
251 }
252 dmem += dbpl;
253 }
254 SDL_UnlockSurface( dst);
255 return;
256 }
257
258 #define ASHIFT 24
259 #define CMASK1 0xff00ff
260 #define CMASK2 0x00ff00
261
262 inline void blit_pixel(Uint32* dmem, Uint32* smem, const unsigned char* amem, bool use_srcalpha) {
263 Uint32 d = *dmem;
264 Uint32 s = *smem;
265 Uint32 as = s>>ASHIFT;
266 if (as == 255 || (!use_srcalpha) ) {
267 as = *amem;
268 } else {
269 as += as>>7; /* 0-0xff -> 0-0x100 */
270 as *= *amem;
271 as >>= 8;
272 }
273 as += as>>7;
274 Uint32 s1 = s & CMASK1;
275 Uint32 d1 = d & CMASK1;
276 d1 = (d1 + (((s1-d1) * as) >> 8)) & CMASK1;
277 s &= CMASK2;
278 d &= CMASK2;
279 d = (d + (((s-d) * as) >> 8)) & CMASK2;
280 *dmem = d1 | d | 0xff000000;
281 }
282 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) {
283 int j;
284 int ax = ax0;
285 const unsigned char* a = amem + ax0;
286 Uint32* d = dmem;
287 Uint32* s = smem;
288 if (awidth == 1) { // わりとよくあるので最適化
289 for (j=aj0; j < aj1; j++) {
290 blit_pixel(d++, s++, amem, use_srcalpha);
291 }
292 } else {
293 for (j=aj0; j < aj1; j++) {
294 for (; ax<awidth; ax++)
295 blit_pixel(d++, s++, a++, use_srcalpha);
296 ax = 0;
297 a = amem;
298 }
299 for (; ax < ax1; ax++) blit_pixel(d++, s++, a++, use_srcalpha);
300 }
301 }
302
303 void DSurfaceBlitAlpha(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, const unsigned char* alpha, const Rect& alpharect) {
304 SDL_Surface* dst = (SDL_Surface*)dst_o;
305 SDL_Surface* src = (SDL_Surface*)src_o;
306 SDL_PixelFormat& fmt = *dst->format;
307
308 Rect srcrect(srcrect_o), dstrect(dstrect_o);
309 clip_rect(srcrect, dstrect, dst);
310
311 int awidth = alpharect.width();
312 int aheight = alpharect.height();
313 if (awidth == 0 || aheight == 0) return;
314 int ax0 = srcrect.lx % awidth;
315 int ay0 = srcrect.ty % aheight;
316 int aj0 = srcrect.lx / awidth;
317 int ai0 = srcrect.ty / aheight;
318 int ax1 = srcrect.rx % awidth;
319 int ay1 = srcrect.by % aheight;
320 int aj1 = srcrect.rx / awidth;
321 int ai1 = srcrect.by / aheight;
322
323 SDL_LockSurface(dst);
324 SDL_LockSurface(src);
325 int dbpl = dst->pitch;
326 int sbpl = src->pitch;
327 int bpp = dst->format->BytesPerPixel;
328
329 char* dmem = dst_o->mem(dstrect);
330 char* smem = src_o->mem(srcrect);
331
332 const unsigned char* amem = alpha + ay0 * awidth;
333 int i;
334 int ay = ay0;
335 for (i = ai0; i < ai1; i++) {
336 for (; ay < aheight; ay++) {
337 if (src->format->Amask == 0)
338 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, false);
339 else
340 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, true);
341 amem += awidth; dmem += dbpl; smem += sbpl;
342 }
343 ay = 0; amem = alpha;
344 }
345 for (; ay < ay1; ay++) {
346 if (src->format->Amask == 0)
347 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, false);
348 else
349 blit_line( (Uint32*)dmem, (Uint32*)smem, amem,ax0, ax1, awidth, aj0, aj1, true);
350 amem += awidth; dmem += dbpl; smem += sbpl;
351 }
352 SDL_UnlockSurface(src);
353 SDL_UnlockSurface(dst);
354 return;
355 }
356 void DSurfaceBlitSaturate(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o, unsigned char alpha) {
357 SDL_Surface* dst = (SDL_Surface*)dst_o;
358 SDL_Surface* src = (SDL_Surface*)src_o;
359
360 if (dst->format->BytesPerPixel != src->format->BytesPerPixel) return; // RGB変換はできない
361
362 Rect srcrect(srcrect_o), dstrect(dstrect_o);
363 clip_rect(srcrect, dstrect, dst);
364
365 SDL_LockSurface(dst); SDL_LockSurface(src);
366 int sbpl = src->pitch;
367 int dbpl = dst->pitch;
368 int bpp = dst->format->BytesPerPixel;
369 int width = srcrect.width();
370 int height = srcrect.height();
371 char* smem = src_o->mem(srcrect);
372 char* dmem = dst_o->mem(dstrect);
373 char* smem_end = src_o->mem_end(srcrect);
374 char* dmem_end = dst_o->mem_end(dstrect);
375
376 SDL_PixelFormat& fmt = *dst->format;
377 int rshift = fmt.Rshift - fmt.Rloss; int rmask = fmt.Rmask;
378 int gshift = fmt.Gshift - fmt.Gloss; int gmask = fmt.Gmask;
379 int bshift = fmt.Bshift - fmt.Bloss; int bmask = fmt.Bmask;
380 int allmask = rmask | gmask | bmask;
381 int i;
382 for (i=0; i<height; i++) {
383 char* d = dmem; char* s = smem;
384 int j; for (j=0; j<width; j++) {
385 Uint32 sd = *(Uint32*)s;
386 Uint32 dd = *(Uint32*)d;
387 if (sd&allmask) {
388 Uint32 sr = (sd&rmask)>>rshift;
389 Uint32 sg = (sd&gmask)>>gshift;
390 Uint32 sb = (sd&bmask)>>bshift;
391 if (alpha != ALPHA_MAX) {
392 sr = (sr*alpha)>>8;
393 sg = (sg*alpha)>>8;
394 sb = (sb*alpha)>>8;
395 }
396 Uint32 dr = sr + ((dd&rmask)>>rshift);
397 Uint32 dg = sg + ((dd&gmask)>>gshift);
398 Uint32 db = sb + ((dd&bmask)>>bshift);
399 if (dr > 255) dr = 255;
400 if (dg > 255) dg = 255;
401 if (db > 255) db = 255;
402 *(Uint32*)d = ((dr<<rshift)&rmask)| ((dg<<gshift)&gmask)| ((db<<bshift)&bmask);
403 }
404 s += bpp; d += bpp;
405 }
406 dmem += dbpl; smem += sbpl;
407 }
408 SDL_UnlockSurface(src);
409 SDL_UnlockSurface(dst);
410 return;
411 }
412 void DSurfaceBlitMultiply(Surface* src_o, const Rect& srcrect_o, Surface* dst_o, const Rect& dstrect_o) {
413 SDL_Surface* dst = (SDL_Surface*)dst_o;
414 SDL_Surface* src = (SDL_Surface*)src_o;
415
416 Rect srcrect(srcrect_o), dstrect(dstrect_o);
417 clip_rect(srcrect, dstrect, dst);
418
419 SDL_LockSurface(dst);
420 SDL_LockSurface(src);
421 int dbpl = dst->pitch;
422 int sbpl = src->pitch;
423 int bpp = dst->format->BytesPerPixel;
424 int width = srcrect.width();
425 int height = srcrect.height();
426 char* dmem = dst_o->mem(dstrect);
427 char* smem = src_o->mem(srcrect);
428
429 SDL_PixelFormat& fmt = *dst->format;
430 /* dst の 0-255 を 0-pixel に変換する(積算) */
431 int i;
432 int table1[256], table2[256], table3[256];
433 Uint32 src_pixel = *(Uint32*)smem;
434 src_pixel |= 0xff000000;
435 int c1=src_pixel&255, c2=(src_pixel>>8)&255, c3=(src_pixel>>16)&255;
436 for (i=0; i<256; i++) {
437 int n = i + (i>>7);
438 table1[i] = (c1*n)>>8;
439 table2[i] = (c2*n)&0xff00;
440 table3[i] = ((c3*n)<<8) & 0xff0000;
441 }
442 int err_count = 0;
443 for (i=0; i<height; i++) {
444 Uint32* d = (Uint32*)dmem;
445 Uint32* s = (Uint32*)smem;
446 int j; for (j=0; j<width; j++) {
447 Uint32 dd = *d;
448 Uint32 ss = *s;
449 if (ss == src_pixel) {
450 *(Uint32*)d = table1[ dd & 0xff] | table2[ (dd>>8) & 0xff] | table3[ (dd>>16) & 0xff];
451 } else if ( ((ss^src_pixel) & 0xffffff) == 0) { // r,g,b of ss == src_pixel
452 Uint32 as = ss>>ASHIFT;
453 as += as>>7;
454 ss = table1[ dd & 0xff] | table2[ (dd>>8) & 0xff] | table3[ (dd>>16) & 0xff];
455 Uint32 s1 = ss&CMASK1;
456 Uint32 d1 = dd&CMASK1;
457 d1 = (d1 + (((s1-d1) * as) >> 8)) & CMASK1;
458 ss &= CMASK2;
459 dd &= CMASK2;
460 dd = (dd + (((ss-dd) * as) >> 8)) & CMASK2;
461 *(Uint32*)d = d1 | dd | 0xff000000;
462 } else err_count++;
463 d++; s++;
464 }
465 dmem += dbpl; smem += sbpl;
466 }
467 if (err_count) {
468 fprintf(stderr,"multipy_blit : surface does not have unique color(%08x); %d pixels in width %d, height %d\n",
469 src_pixel, err_count, width, height);
470 }
471 SDL_UnlockSurface(dst);
472 SDL_UnlockSurface(src);
473 return;
474 }