OSDN Git Service

r224をNetBeans用に設定変更.
[coroid/inqubus.git] / vhook / comment / surf_util.c
1 #include <SDL/SDL.h>\r
2 #include "surf_util.h"\r
3 #include "../mydef.h"\r
4 \r
5 SDL_Surface* connectSurface(SDL_Surface* top,SDL_Surface* bottom){\r
6         SDL_Surface* ret = SDL_CreateRGBSurface( SDL_SRCALPHA,\r
7                                                                                         MAX(top->w,bottom->w),\r
8                                                                                         top->h+bottom->h,\r
9                                                                                         32,\r
10                                                                                         #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
11                                                                                             0xff000000,\r
12                                                                                             0x00ff0000,\r
13                                                                                             0x0000ff00,\r
14                                                                                             0x000000ff\r
15                                                                                         #else\r
16                                                                                             0x000000ff,\r
17                                                                                             0x0000ff00,\r
18                                                                                             0x00ff0000,\r
19                                                                                                 0xff000000\r
20                                                                                         #endif\r
21                                                                                         );\r
22         SDL_SetAlpha(top,SDL_SRCALPHA | SDL_RLEACCEL,0xff);\r
23         SDL_SetAlpha(bottom,SDL_SRCALPHA | SDL_RLEACCEL,0xff);\r
24 \r
25         SDL_BlitSurface(top,NULL,ret,NULL);\r
26 \r
27         SDL_Rect rect2 = {0,top->h};\r
28         SDL_BlitSurface(bottom,NULL,ret,&rect2);\r
29         SDL_FreeSurface(top);\r
30         SDL_FreeSurface(bottom);\r
31         return ret;\r
32 }\r
33 \r
34 void setAlpha(SDL_Surface* surf,double alpha_t){\r
35         int x,y;\r
36         int h = surf->h;\r
37         int w = surf->w;\r
38         Uint32 mask,shift,bytesp,pitch,loss;\r
39         Uint8 alpha;\r
40         Uint8* pixels;\r
41         Uint32* pix;\r
42         SDL_PixelFormat* format = surf->format;\r
43         /*\95Ï\90\94\82Ì\90Ý\92è*/\r
44         mask = format->Amask;\r
45         shift = format->Ashift;\r
46         loss = format->Aloss;\r
47         bytesp = format->BytesPerPixel;\r
48         pitch = surf->pitch;\r
49         pixels = surf->pixels;\r
50         SDL_LockSurface(surf);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
51         for(y=0;y<h;y++){\r
52                 for(x=0;x<w;x++){\r
53                         pix = (Uint32*)(&pixels[y*pitch + x*bytesp]);\r
54                         alpha = (Uint8)((((*pix) & mask) >> shift) << loss);\r
55                         alpha *= alpha_t;\r
56                         (*pix) &= ~((0xff >> loss) << shift);\r
57                         (*pix) |= (alpha >> loss) << shift;\r
58                 }\r
59         }\r
60         SDL_UnlockSurface(surf);//\83A\83\93\83\8d\83b\83N\r
61 }\r
62 \r
63 /**\r
64  * src\82Ì\95s\93§\96¾\93x\82ð\8fã\8f\91\82«\82µ\82Ä\82µ\82Ü\82¤\81B\r
65  * src\82Ì\95û\82ª\95s\93§\96¾\82È\82ç\81A\82»\82ê\82ð\8fã\8f\91\82«\81B\r
66  */\r
67 \r
68 void overrideAlpha(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect){\r
69         SDL_LockSurface(src);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
70         SDL_LockSurface(dst);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
71         //\94Í\88Í\82Ì\8am\92è\r
72         int sw = src->w;\r
73         int sh = src->h;\r
74         int sx = 0;\r
75         int sy = 0;\r
76         if(srcrect != NULL){\r
77                 sx = srcrect->x;\r
78                 sy = srcrect->y;\r
79                 if(sx >= sw || sy > sh){\r
80                         return;\r
81                 }\r
82                 sw = MIN(sw-sx,srcrect->w);\r
83                 sh = MIN(sh-sy,srcrect->h);\r
84         }\r
85         int dw = dst->w;\r
86         int dh = dst->h;\r
87         int dx = 0;\r
88         int dy = 0;\r
89         if(dstrect != NULL){\r
90                 dx = dstrect->x;\r
91                 dy = dstrect->y;\r
92                 if(dx >= dw || dy > dh){\r
93                         return;\r
94                 }\r
95                 dw = MIN(dw-dx,dstrect->w);\r
96                 dh = MIN(dh-dy,dstrect->h);\r
97         }\r
98         //\8f¬\82³\82¢\82Ù\82¤\82É\82 \82í\82¹\82é\r
99         if(dw > sw){\r
100                 dw = sw;\r
101         }else{\r
102                 sw = dw;\r
103         }\r
104         if(dh > sh){\r
105                 dh = sh;\r
106         }else{\r
107                 sh = dh;\r
108         }\r
109         //\82â\82Á\82Æ\82±\82³\95`\89æ\81B\81B\r
110         int sbytesp = src->format->BytesPerPixel;\r
111         int dbytesp = dst->format->BytesPerPixel;\r
112         int spitch = src->pitch;\r
113         int dpitch = dst->pitch;\r
114         int sAmask = src->format->Amask;\r
115         int dAmask = dst->format->Amask;\r
116         int sAshift = src->format->Ashift;\r
117         int dAshift = dst->format->Ashift;\r
118         int sAloss = src->format->Aloss;\r
119         int dAloss = dst->format->Aloss;\r
120         Uint8* spix = (Uint8*)src->pixels;\r
121         Uint8* dpix = (Uint8*)dst->pixels;\r
122         Uint32* spt;\r
123         Uint32* dpt;\r
124         Uint32 salpha;\r
125         Uint32 dalpha;\r
126         int x,y;\r
127         for(y=0;y<sh;y++){\r
128                 for(x=0;x<sw;x++){\r
129                         spt = (Uint32*)(&spix[(sy+y)*spitch+(sx+x)*sbytesp]);\r
130                         dpt = (Uint32*)(&dpix[(dy+y)*dpitch+(dx+x)*dbytesp]);\r
131                         salpha = ((*spt & sAmask)>>sAshift)<<sAloss;\r
132                         dalpha = ((*dpt & dAmask)>>dAshift)<<dAloss;\r
133                         if(salpha > dalpha){\r
134                                 *dpt &= ~dAmask;\r
135                                 *dpt |= (salpha>>dAloss)<<dAshift;\r
136                         }\r
137                 }\r
138         }\r
139         SDL_UnlockSurface(dst);//\83A\83\93\83\8d\83b\83N\r
140         SDL_UnlockSurface(src);//\83A\83\93\83\8d\83b\83N\r
141 }\r
142 \r
143 void inline shadowBlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect){\r
144         SDL_BlitSurface(src,srcrect,dst,dstrect);\r
145         overrideAlpha(src,srcrect,dst,dstrect);\r
146 }\r
147 \r
148 void setRGB(SDL_Surface* surf,Uint32 color){\r
149         int x,y;\r
150         int h = surf->h;\r
151         int w = surf->w;\r
152         int bytesp = surf->format->BytesPerPixel;\r
153         int pitch = surf->pitch;\r
154         Uint32 Amask = surf->format->Amask;\r
155         color &= surf->format->Rmask | surf->format->Gmask | surf->format->Bmask;\r
156         Uint8* pix = (Uint8*)surf->pixels;\r
157         Uint32* pt;\r
158         SDL_LockSurface(surf);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
159         for(y=0;y<h;y++){\r
160                 for(x=0;x<w;x++){\r
161                         pt = (Uint32*)(&pix[y*pitch + x*bytesp]);\r
162                         *pt &= Amask;\r
163                         *pt |= color;\r
164                 }\r
165         }\r
166         SDL_UnlockSurface(surf);//\83T\81[\83t\83F\83C\83X\82ð\83A\83\93\83\8d\83b\83N\r
167 }\r
168 \r
169 void getRGBA(SDL_Surface* surf,int x,int y,char* r,char* g,char* b,char* a){\r
170         int pix_index = y * surf->pitch + x * surf->format->BytesPerPixel;\r
171         char* pix = (char*)surf->pixels;\r
172         SDL_GetRGBA(*(Uint32*)(&pix[pix_index]),surf->format,(Uint8*)r,(Uint8*)g,(Uint8*)b,(Uint8*)a);\r
173 }\r
174 \r