OSDN Git Service

r224をNetBeans用に設定変更.
[coroid/inqubus.git] / vhook / comment / shadow.c
1 #include <SDL/SDL.h>\r
2 #include "surf_util.h"\r
3 #include "shadow.h"\r
4 \r
5 /*\89e\82È\82µ*/\r
6 SDL_Surface* noShadow(SDL_Surface* surf,int is_black,int is_fix_size){\r
7         return surf;\r
8 }\r
9 /*\89E\89º*/\r
10 #define SHADOW_SIZE 3\r
11 \r
12 SDL_Surface* likeNicoNico(SDL_Surface* surf,int is_black,int is_fix_size){\r
13         /*\83X\83\89\83C\83h\95\9d\82Ì\8am\92è*/\r
14         int slide = SHADOW_SIZE;\r
15         if(is_fix_size){\r
16                 slide <<= 1;\r
17         }\r
18         int w = surf->w;\r
19         int h = surf->h;\r
20         SDL_Surface* shadow = SDL_CreateRGBSurface(             SDL_SRCALPHA | SDL_HWSURFACE | SDL_HWACCEL,\r
21                                                                                                 w+(slide<<1),\r
22                                                                                                 h+(slide<<1),\r
23                                                                                                 32,\r
24                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
25                                                                                                     0xff000000,\r
26                                                                                                     0x00ff0000,\r
27                                                                                                     0x0000ff00,\r
28                                                                                                     0x000000ff\r
29                                                                                                 #else\r
30                                                                                                     0x000000ff,\r
31                                                                                                     0x0000ff00,\r
32                                                                                                     0x00ff0000,\r
33                                                                                                     0xff000000\r
34                                                                                                 #endif\r
35                                                                                         );\r
36         SDL_Surface* shadow2 = SDL_CreateRGBSurface(    SDL_SRCALPHA | SDL_HWSURFACE | SDL_HWACCEL,\r
37                                                                                                 w+(slide<<1),\r
38                                                                                                 h+(slide<<1),\r
39                                                                                                 32,\r
40                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
41                                                                                                     0xff000000,\r
42                                                                                                     0x00ff0000,\r
43                                                                                                     0x0000ff00,\r
44                                                                                                     0x000000ff\r
45                                                                                                 #else\r
46                                                                                                     0x000000ff,\r
47                                                                                                     0x0000ff00,\r
48                                                                                                     0x00ff0000,\r
49                                                                                                     0xff000000\r
50                                                                                                 #endif\r
51                                                                                         );\r
52         SDL_Rect rect = {slide,slide};\r
53         SDL_SetAlpha(surf,0,0xff);\r
54         SDL_BlitSurface(surf,NULL,shadow,&rect);\r
55         SDL_SetAlpha(surf,SDL_SRCALPHA,0xff);\r
56         if(is_black){//\8d\95\82Å\82 \82ê\82Î\81A\8eü\82è\82ð\82µ\82ë\82Å\88Í\82Þ\r
57                 setRGB(shadow,0xffffffff);\r
58         }else{\r
59                 setRGB(shadow,0);\r
60         }\r
61         SDL_SetAlpha(shadow,0,0xff);\r
62         SDL_BlitSurface(shadow,NULL,shadow2,NULL);\r
63         SDL_SetAlpha(shadow,SDL_SRCALPHA,0xff);\r
64         int x,y,z;\r
65         int nw = shadow->w;\r
66         int nh = shadow->h;\r
67         int *pix;\r
68         int *pix2;\r
69         int pitch = shadow->pitch;\r
70         int bps = shadow->format->BytesPerPixel;\r
71         Uint32 Amask = shadow->format->Amask;\r
72         Uint32 Mask = (shadow->format->Rmask | shadow->format->Gmask | shadow->format->Bmask);\r
73         Uint32 Ashift = shadow->format->Ashift;\r
74         Uint32 Aloss = shadow->format->Aloss;\r
75         SDL_LockSurface(shadow);\r
76         SDL_LockSurface(shadow2);\r
77         //\82±\82±\82Í\8bô\90\94\82É\82·\82é\82±\82Æ\81B\r
78         int zmax = 10;\r
79         if(is_fix_size){\r
80                 zmax = 16;\r
81         }\r
82         SDL_Surface* tmp;\r
83         for(z=0;z<zmax;z++){\r
84                 char *pixels = (char*)shadow->pixels;\r
85                 char *pixels2 = (char*)shadow2->pixels;\r
86                 for(y=0;y<nh;y++){\r
87                         pix = (int*)(&pixels[pitch * y]);\r
88                         pix2 = (int*)(&pixels2[pitch * y]);\r
89                         for(x=0;x<nw;x++){\r
90                                 int right = (x==nw-1) ? 0 : *(int*)((((char*)pix)+bps));\r
91                                 int left = (x==0) ? 0 : *(int*)((((char*)pix)-bps));\r
92                                 int up = (y==0) ? 0 : *(int*)((((char*)pix)-pitch));\r
93                                 int down = (y==nh-1) ? 0 : *(int*)((((char*)pix)+pitch));\r
94                                 int my = *pix2;\r
95                                 int new_alpha = (((((my & Amask) >> Ashift) << Aloss) +(((right & Amask) >> Ashift) << Aloss)+(((left & Amask) >> Ashift) << Aloss)+(((up & Amask) >> Ashift) << Aloss)+(((down & Amask) >> Ashift) << Aloss)) / 5) & 0xff;\r
96                                 new_alpha = (new_alpha * 18) >> 4;\r
97                                 if(new_alpha > 0xff){\r
98                                         new_alpha = 0xff;\r
99                                 }\r
100                                 *pix2 &= Mask;\r
101                                 *pix2 |= ((new_alpha >> Aloss) << Ashift) & Amask;\r
102                                 pix = (int*)(((char*)pix)+bps);\r
103                                 pix2 = (int*)(((char*)pix2)+bps);\r
104                         }\r
105                 }\r
106                 tmp = shadow2;\r
107                 shadow2 = shadow;\r
108                 shadow = tmp;\r
109         }\r
110         SDL_UnlockSurface(shadow);\r
111         SDL_UnlockSurface(shadow2);\r
112         shadowBlitSurface(surf,NULL,shadow,&rect);\r
113         SDL_FreeSurface(surf);\r
114         SDL_FreeSurface(shadow2);\r
115         return shadow;\r
116 }\r
117 \r
118 /*\89E\89º*/\r
119 #define SHADOW_SLIDE 2\r
120 SDL_Surface* likeNovel(SDL_Surface* surf,int is_black,int is_fix_size){\r
121         /*\83X\83\89\83C\83h\95\9d\82Ì\8am\92è*/\r
122         int slide = SHADOW_SLIDE;\r
123         if(is_fix_size){\r
124                 slide <<= 1;\r
125         }\r
126         /*\8d\95\82Ì\97p\88Ó*/\r
127         SDL_Surface* black = SDL_CreateRGBSurface(      SDL_SRCALPHA | SDL_HWSURFACE | SDL_HWACCEL,\r
128                                                                                                 surf->w+slide,\r
129                                                                                                 surf->h+slide,\r
130                                                                                                 32,\r
131                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
132                                                                                                     0xff000000,\r
133                                                                                                     0x00ff0000,\r
134                                                                                                     0x0000ff00,\r
135                                                                                                     0x000000ff\r
136                                                                                                 #else\r
137                                                                                                     0x000000ff,\r
138                                                                                                     0x0000ff00,\r
139                                                                                                     0x00ff0000,\r
140                                                                                                     0xff000000\r
141                                                                                                 #endif\r
142                                                                                                 );\r
143         SDL_Rect rect = {slide,slide};\r
144         SDL_SetAlpha(surf,0,0xff);//\88ê\89ñalpha\8d\87\90¬\82ð\90Ø\82é\r
145         SDL_BlitSurface(surf,NULL,black,&rect);\r
146         SDL_SetAlpha(surf,SDL_SRCALPHA,0xff);\r
147         if(is_black){//\8d\95\82Å\82 \82ê\82Î\81A\8eü\82è\82ð\82µ\82ë\82Å\88Í\82Þ\r
148                 setRGB(black,0xffffffff);\r
149         }else{\r
150                 setRGB(black,0);\r
151         }\r
152         setAlpha(black,0.6f);\r
153         shadowBlitSurface(surf,NULL,black,NULL);\r
154         SDL_FreeSurface(surf);\r
155         return black;\r
156 }\r
157 \r
158 //\8eU\82ç\82·\82Ì\82Å\82Í\82È\82­\81A\88Í\82Á\82Ä\82µ\82Ü\82¤\81B\r
159 SDL_Surface* likeOld(SDL_Surface* surf,int is_black,int is_fix_size){\r
160         /*\83X\83\89\83C\83h\95\9d\82Ì\8am\92è*/\r
161         int slide = SHADOW_SIZE;\r
162         if(is_fix_size){\r
163                 slide <<= 1;\r
164         }\r
165         int w = surf->w;\r
166         int h = surf->h;\r
167         SDL_Surface* shadow = SDL_CreateRGBSurface(             SDL_SRCALPHA | SDL_HWSURFACE | SDL_HWACCEL,\r
168                                                                                                 w+(slide<<1),\r
169                                                                                                 h+(slide<<1),\r
170                                                                                                 32,\r
171                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
172                                                                                                     0xff000000,\r
173                                                                                                     0x00ff0000,\r
174                                                                                                     0x0000ff00,\r
175                                                                                                     0x000000ff\r
176                                                                                                 #else\r
177                                                                                                     0x000000ff,\r
178                                                                                                     0x0000ff00,\r
179                                                                                                     0x00ff0000,\r
180                                                                                                     0xff000000\r
181                                                                                                 #endif\r
182                                                                                         );\r
183         SDL_Surface* shadow2 = SDL_CreateRGBSurface(    SDL_SRCALPHA | SDL_HWSURFACE | SDL_HWACCEL,\r
184                                                                                                 w+(slide<<1),\r
185                                                                                                 h+(slide<<1),\r
186                                                                                                 32,\r
187                                                                                                 #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
188                                                                                                     0xff000000,\r
189                                                                                                     0x00ff0000,\r
190                                                                                                     0x0000ff00,\r
191                                                                                                     0x000000ff\r
192                                                                                                 #else\r
193                                                                                                     0x000000ff,\r
194                                                                                                     0x0000ff00,\r
195                                                                                                     0x00ff0000,\r
196                                                                                                     0xff000000\r
197                                                                                                 #endif\r
198                                                                                         );\r
199         SDL_Rect rect = {slide,slide};\r
200         SDL_SetAlpha(surf,0,0xff);\r
201         SDL_BlitSurface(surf,NULL,shadow,&rect);\r
202         SDL_SetAlpha(surf,SDL_SRCALPHA,0xff);\r
203         if(is_black){//\8d\95\82Å\82 \82ê\82Î\81A\8eü\82è\82ð\82µ\82ë\82Å\88Í\82Þ\r
204                 setRGB(shadow,0xffffffff);\r
205         }else{\r
206                 setRGB(shadow,0);\r
207         }\r
208         SDL_SetAlpha(shadow,0,0xff);\r
209         SDL_BlitSurface(shadow,NULL,shadow2,NULL);\r
210         SDL_SetAlpha(shadow,SDL_SRCALPHA,0xff);\r
211         int x,y,z;\r
212         int nw = shadow->w;\r
213         int nh = shadow->h;\r
214         int *pix;\r
215         int *pix2;\r
216         int pitch = shadow->pitch;\r
217         int bps = shadow->format->BytesPerPixel;\r
218         Uint32 Amask = shadow->format->Amask;\r
219         Uint32 Mask = (shadow->format->Rmask | shadow->format->Gmask | shadow->format->Bmask);\r
220         Uint32 Ashift = shadow->format->Ashift;\r
221         Uint32 Aloss = shadow->format->Aloss;\r
222         SDL_Surface* tmp;\r
223         SDL_LockSurface(shadow);\r
224         SDL_LockSurface(shadow2);\r
225         int zmax = 1;\r
226         if(is_fix_size){\r
227                 zmax = 2;\r
228         }\r
229         for(z=0;z<zmax;z++){\r
230                 char *pixels = (char*)shadow->pixels;\r
231                 char *pixels2 = (char*)shadow2->pixels;\r
232                 for(y=0;y<nh;y++){\r
233                         pix = (int*)(&pixels[pitch * y]);\r
234                         pix2 = (int*)(&pixels2[pitch * y]);\r
235                         for(x=0;x<nw;x++){\r
236                                 int right = (x==nw-1) ? 0 : *(int*)((((char*)pix)+bps));\r
237                                 int left = (x==0) ? 0 : *(int*)((((char*)pix)-bps));\r
238                                 int up = (y==0) ? 0 : *(int*)((((char*)pix)-pitch));\r
239                                 int down = (y==nh-1) ? 0 : *(int*)((((char*)pix)+pitch));\r
240                                 int my = *pix2;\r
241                                 //\8eü\82è\82ª\8bó\94\92\82Å\82È\82¢\r
242                                 if(((right | left | up | down | my) & Amask) != 0){\r
243                                         *pix2 &= Mask;\r
244                                         *pix2 |= (((0xff/(z+1)) >> Aloss) << Ashift) & Amask;\r
245                                 }\r
246                                 pix = (int*)(((char*)pix)+bps);\r
247                                 pix2 = (int*)(((char*)pix2)+bps);\r
248                         }\r
249                 }\r
250                 tmp = shadow2;\r
251                 shadow2 = shadow;\r
252                 shadow = tmp;\r
253         }\r
254         SDL_UnlockSurface(shadow);\r
255         SDL_UnlockSurface(shadow2);\r
256         shadowBlitSurface(surf,NULL,shadow,&rect);\r
257         SDL_FreeSurface(surf);\r
258         SDL_FreeSurface(shadow2);\r
259         return shadow;\r
260 }\r
261 \r
262 \r
263 //\92è\8b`\r
264 SDL_Surface* (*ShadowFunc[SHADOW_MAX])(SDL_Surface* surf,int is_black,int is_fix_size) = {\r
265         noShadow,\r
266         likeNicoNico,\r
267         likeNovel,\r
268         likeOld\r
269 };\r