OSDN Git Service

ソースの文字コードと改行コードを修正
[coroid/inqubus.git] / vhook / comment / surf_util.c
index f8da9b7..a948b00 100644 (file)
-#include <SDL/SDL.h>\r
-#include "surf_util.h"\r
-#include "../mydef.h"\r
-\r
-SDL_Surface* connectSurface(SDL_Surface* top,SDL_Surface* bottom){\r
-       SDL_Surface* ret = SDL_CreateRGBSurface( SDL_SRCALPHA,\r
-                                                                                       MAX(top->w,bottom->w),\r
-                                                                                       top->h+bottom->h,\r
-                                                                                       32,\r
-                                                                                       #if SDL_BYTEORDER == SDL_BIG_ENDIAN\r
-                                                                                           0xff000000,\r
-                                                                                           0x00ff0000,\r
-                                                                                           0x0000ff00,\r
-                                                                                           0x000000ff\r
-                                                                                       #else\r
-                                                                                           0x000000ff,\r
-                                                                                           0x0000ff00,\r
-                                                                                           0x00ff0000,\r
-                                                                                               0xff000000\r
-                                                                                       #endif\r
-                                                                                       );\r
-       SDL_SetAlpha(top,SDL_SRCALPHA | SDL_RLEACCEL,0xff);\r
-       SDL_SetAlpha(bottom,SDL_SRCALPHA | SDL_RLEACCEL,0xff);\r
-\r
-       SDL_BlitSurface(top,NULL,ret,NULL);\r
-\r
-       SDL_Rect rect2 = {0,top->h};\r
-       SDL_BlitSurface(bottom,NULL,ret,&rect2);\r
-       SDL_FreeSurface(top);\r
-       SDL_FreeSurface(bottom);\r
-       return ret;\r
-}\r
-\r
-void setAlpha(SDL_Surface* surf,double alpha_t){\r
-       int x,y;\r
-       int h = surf->h;\r
-       int w = surf->w;\r
-       Uint32 mask,shift,bytesp,pitch,loss;\r
-       Uint8 alpha;\r
-       Uint8* pixels;\r
-       Uint32* pix;\r
-       SDL_PixelFormat* format = surf->format;\r
-       /*\95Ï\90\94\82Ì\90Ý\92è*/\r
-       mask = format->Amask;\r
-       shift = format->Ashift;\r
-       loss = format->Aloss;\r
-       bytesp = format->BytesPerPixel;\r
-       pitch = surf->pitch;\r
-       pixels = surf->pixels;\r
-       SDL_LockSurface(surf);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
-       for(y=0;y<h;y++){\r
-               for(x=0;x<w;x++){\r
-                       pix = (Uint32*)(&pixels[y*pitch + x*bytesp]);\r
-                       alpha = (Uint8)((((*pix) & mask) >> shift) << loss);\r
-                       alpha *= alpha_t;\r
-                       (*pix) &= ~((0xff >> loss) << shift);\r
-                       (*pix) |= (alpha >> loss) << shift;\r
-               }\r
-       }\r
-       SDL_UnlockSurface(surf);//\83A\83\93\83\8d\83b\83N\r
-}\r
-\r
-/**\r
- * src\82Ì\95s\93§\96¾\93x\82ð\8fã\8f\91\82«\82µ\82Ä\82µ\82Ü\82¤\81B\r
- * src\82Ì\95û\82ª\95s\93§\96¾\82È\82ç\81A\82»\82ê\82ð\8fã\8f\91\82«\81B\r
- */\r
-\r
-void overrideAlpha(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect){\r
-       SDL_LockSurface(src);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
-       SDL_LockSurface(dst);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
-       //\94Í\88Í\82Ì\8am\92è\r
-       int sw = src->w;\r
-       int sh = src->h;\r
-       int sx = 0;\r
-       int sy = 0;\r
-       if(srcrect != NULL){\r
-               sx = srcrect->x;\r
-               sy = srcrect->y;\r
-               if(sx >= sw || sy > sh){\r
-                       return;\r
-               }\r
-               sw = MIN(sw-sx,srcrect->w);\r
-               sh = MIN(sh-sy,srcrect->h);\r
-       }\r
-       int dw = dst->w;\r
-       int dh = dst->h;\r
-       int dx = 0;\r
-       int dy = 0;\r
-       if(dstrect != NULL){\r
-               dx = dstrect->x;\r
-               dy = dstrect->y;\r
-               if(dx >= dw || dy > dh){\r
-                       return;\r
-               }\r
-               dw = MIN(dw-dx,dstrect->w);\r
-               dh = MIN(dh-dy,dstrect->h);\r
-       }\r
-       //\8f¬\82³\82¢\82Ù\82¤\82É\82 \82í\82¹\82é\r
-       if(dw > sw){\r
-               dw = sw;\r
-       }else{\r
-               sw = dw;\r
-       }\r
-       if(dh > sh){\r
-               dh = sh;\r
-       }else{\r
-               sh = dh;\r
-       }\r
-       //\82â\82Á\82Æ\82±\82³\95`\89æ\81B\81B\r
-       int sbytesp = src->format->BytesPerPixel;\r
-       int dbytesp = dst->format->BytesPerPixel;\r
-       int spitch = src->pitch;\r
-       int dpitch = dst->pitch;\r
-       int sAmask = src->format->Amask;\r
-       int dAmask = dst->format->Amask;\r
-       int sAshift = src->format->Ashift;\r
-       int dAshift = dst->format->Ashift;\r
-       int sAloss = src->format->Aloss;\r
-       int dAloss = dst->format->Aloss;\r
-       Uint8* spix = (Uint8*)src->pixels;\r
-       Uint8* dpix = (Uint8*)dst->pixels;\r
-       Uint32* spt;\r
-       Uint32* dpt;\r
-       Uint32 salpha;\r
-       Uint32 dalpha;\r
-       int x,y;\r
-       for(y=0;y<sh;y++){\r
-               for(x=0;x<sw;x++){\r
-                       spt = (Uint32*)(&spix[(sy+y)*spitch+(sx+x)*sbytesp]);\r
-                       dpt = (Uint32*)(&dpix[(dy+y)*dpitch+(dx+x)*dbytesp]);\r
-                       salpha = ((*spt & sAmask)>>sAshift)<<sAloss;\r
-                       dalpha = ((*dpt & dAmask)>>dAshift)<<dAloss;\r
-                       if(salpha > dalpha){\r
-                               *dpt &= ~dAmask;\r
-                               *dpt |= (salpha>>dAloss)<<dAshift;\r
-                       }\r
-               }\r
-       }\r
-       SDL_UnlockSurface(dst);//\83A\83\93\83\8d\83b\83N\r
-       SDL_UnlockSurface(src);//\83A\83\93\83\8d\83b\83N\r
-}\r
-\r
-void inline shadowBlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect){\r
-       SDL_BlitSurface(src,srcrect,dst,dstrect);\r
-       overrideAlpha(src,srcrect,dst,dstrect);\r
-}\r
-\r
-void setRGB(SDL_Surface* surf,Uint32 color){\r
-       int x,y;\r
-       int h = surf->h;\r
-       int w = surf->w;\r
-       int bytesp = surf->format->BytesPerPixel;\r
-       int pitch = surf->pitch;\r
-       Uint32 Amask = surf->format->Amask;\r
-       color &= surf->format->Rmask | surf->format->Gmask | surf->format->Bmask;\r
-       Uint8* pix = (Uint8*)surf->pixels;\r
-       Uint32* pt;\r
-       SDL_LockSurface(surf);//\83T\81[\83t\83F\83C\83X\82ð\83\8d\83b\83N\r
-       for(y=0;y<h;y++){\r
-               for(x=0;x<w;x++){\r
-                       pt = (Uint32*)(&pix[y*pitch + x*bytesp]);\r
-                       *pt &= Amask;\r
-                       *pt |= color;\r
-               }\r
-       }\r
-       SDL_UnlockSurface(surf);//\83T\81[\83t\83F\83C\83X\82ð\83A\83\93\83\8d\83b\83N\r
-}\r
-\r
-void getRGBA(SDL_Surface* surf,int x,int y,char* r,char* g,char* b,char* a){\r
-       int pix_index = y * surf->pitch + x * surf->format->BytesPerPixel;\r
-       char* pix = (char*)surf->pixels;\r
-       SDL_GetRGBA(*(Uint32*)(&pix[pix_index]),surf->format,(Uint8*)r,(Uint8*)g,(Uint8*)b,(Uint8*)a);\r
-}\r
-\r
+#include <SDL/SDL.h>
+#include "surf_util.h"
+#include "../mydef.h"
+
+SDL_Surface* connectSurface(SDL_Surface* top,SDL_Surface* bottom){
+       SDL_Surface* ret = SDL_CreateRGBSurface( SDL_SRCALPHA,
+                                                                                       MAX(top->w,bottom->w),
+                                                                                       top->h+bottom->h,
+                                                                                       32,
+                                                                                       #if SDL_BYTEORDER == SDL_BIG_ENDIAN
+                                                                                           0xff000000,
+                                                                                           0x00ff0000,
+                                                                                           0x0000ff00,
+                                                                                           0x000000ff
+                                                                                       #else
+                                                                                           0x000000ff,
+                                                                                           0x0000ff00,
+                                                                                           0x00ff0000,
+                                                                                               0xff000000
+                                                                                       #endif
+                                                                                       );
+       SDL_SetAlpha(top,SDL_SRCALPHA | SDL_RLEACCEL,0xff);
+       SDL_SetAlpha(bottom,SDL_SRCALPHA | SDL_RLEACCEL,0xff);
+
+       SDL_BlitSurface(top,NULL,ret,NULL);
+
+       SDL_Rect rect2 = {0,top->h};
+       SDL_BlitSurface(bottom,NULL,ret,&rect2);
+       SDL_FreeSurface(top);
+       SDL_FreeSurface(bottom);
+       return ret;
+}
+
+void setAlpha(SDL_Surface* surf,double alpha_t){
+       int x,y;
+       int h = surf->h;
+       int w = surf->w;
+       Uint32 mask,shift,bytesp,pitch,loss;
+       Uint8 alpha;
+       Uint8* pixels;
+       Uint32* pix;
+       SDL_PixelFormat* format = surf->format;
+       /*変数の設定*/
+       mask = format->Amask;
+       shift = format->Ashift;
+       loss = format->Aloss;
+       bytesp = format->BytesPerPixel;
+       pitch = surf->pitch;
+       pixels = surf->pixels;
+       SDL_LockSurface(surf);//サーフェイスをロック
+       for(y=0;y<h;y++){
+               for(x=0;x<w;x++){
+                       pix = (Uint32*)(&pixels[y*pitch + x*bytesp]);
+                       alpha = (Uint8)((((*pix) & mask) >> shift) << loss);
+                       alpha *= alpha_t;
+                       (*pix) &= ~((0xff >> loss) << shift);
+                       (*pix) |= (alpha >> loss) << shift;
+               }
+       }
+       SDL_UnlockSurface(surf);//アンロック
+}
+
+/**
+ * srcの不透明度を上書きしてしまう。
+ * srcの方が不透明なら、それを上書き。
+ */
+
+void overrideAlpha(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect){
+       SDL_LockSurface(src);//サーフェイスをロック
+       SDL_LockSurface(dst);//サーフェイスをロック
+       //範囲の確定
+       int sw = src->w;
+       int sh = src->h;
+       int sx = 0;
+       int sy = 0;
+       if(srcrect != NULL){
+               sx = srcrect->x;
+               sy = srcrect->y;
+               if(sx >= sw || sy > sh){
+                       return;
+               }
+               sw = MIN(sw-sx,srcrect->w);
+               sh = MIN(sh-sy,srcrect->h);
+       }
+       int dw = dst->w;
+       int dh = dst->h;
+       int dx = 0;
+       int dy = 0;
+       if(dstrect != NULL){
+               dx = dstrect->x;
+               dy = dstrect->y;
+               if(dx >= dw || dy > dh){
+                       return;
+               }
+               dw = MIN(dw-dx,dstrect->w);
+               dh = MIN(dh-dy,dstrect->h);
+       }
+       //小さいほうにあわせる
+       if(dw > sw){
+               dw = sw;
+       }else{
+               sw = dw;
+       }
+       if(dh > sh){
+               dh = sh;
+       }else{
+               sh = dh;
+       }
+       //やっとこさ描画。。
+       int sbytesp = src->format->BytesPerPixel;
+       int dbytesp = dst->format->BytesPerPixel;
+       int spitch = src->pitch;
+       int dpitch = dst->pitch;
+       int sAmask = src->format->Amask;
+       int dAmask = dst->format->Amask;
+       int sAshift = src->format->Ashift;
+       int dAshift = dst->format->Ashift;
+       int sAloss = src->format->Aloss;
+       int dAloss = dst->format->Aloss;
+       Uint8* spix = (Uint8*)src->pixels;
+       Uint8* dpix = (Uint8*)dst->pixels;
+       Uint32* spt;
+       Uint32* dpt;
+       Uint32 salpha;
+       Uint32 dalpha;
+       int x,y;
+       for(y=0;y<sh;y++){
+               for(x=0;x<sw;x++){
+                       spt = (Uint32*)(&spix[(sy+y)*spitch+(sx+x)*sbytesp]);
+                       dpt = (Uint32*)(&dpix[(dy+y)*dpitch+(dx+x)*dbytesp]);
+                       salpha = ((*spt & sAmask)>>sAshift)<<sAloss;
+                       dalpha = ((*dpt & dAmask)>>dAshift)<<dAloss;
+                       if(salpha > dalpha){
+                               *dpt &= ~dAmask;
+                               *dpt |= (salpha>>dAloss)<<dAshift;
+                       }
+               }
+       }
+       SDL_UnlockSurface(dst);//アンロック
+       SDL_UnlockSurface(src);//アンロック
+}
+
+void inline shadowBlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect){
+       SDL_BlitSurface(src,srcrect,dst,dstrect);
+       overrideAlpha(src,srcrect,dst,dstrect);
+}
+
+void setRGB(SDL_Surface* surf,Uint32 color){
+       int x,y;
+       int h = surf->h;
+       int w = surf->w;
+       int bytesp = surf->format->BytesPerPixel;
+       int pitch = surf->pitch;
+       Uint32 Amask = surf->format->Amask;
+       color &= surf->format->Rmask | surf->format->Gmask | surf->format->Bmask;
+       Uint8* pix = (Uint8*)surf->pixels;
+       Uint32* pt;
+       SDL_LockSurface(surf);//サーフェイスをロック
+       for(y=0;y<h;y++){
+               for(x=0;x<w;x++){
+                       pt = (Uint32*)(&pix[y*pitch + x*bytesp]);
+                       *pt &= Amask;
+                       *pt |= color;
+               }
+       }
+       SDL_UnlockSurface(surf);//サーフェイスをアンロック
+}
+
+void getRGBA(SDL_Surface* surf,int x,int y,char* r,char* g,char* b,char* a){
+       int pix_index = y * surf->pitch + x * surf->format->BytesPerPixel;
+       char* pix = (char*)surf->pixels;
+       SDL_GetRGBA(*(Uint32*)(&pix[pix_index]),surf->format,(Uint8*)r,(Uint8*)g,(Uint8*)b,(Uint8*)a);
+}
+