能将图形水平翻转的函数


做2D游戏时应该用得到,将图形水平翻转,尤其是人物素材,可将本来朝向右边的翻转成朝向左边的。

[cpp]
  1. #include <string.h>   
  2. int flip_horizontal(  
  3. int img_width,  
  4. int img_hight,  
  5. unsigned char *in_red,//传入的红色   
  6. unsigned char *in_green,//传入的绿色   
  7. unsigned char *in_blue,//传入的蓝色   
  8. unsigned char *in_alpha//传入的透明度   
  9. )  
  10. //水平翻转图像   
  11. {  
  12.     int x,y,pos,temp;  
  13.     unsigned char *out_red;//传出的红色   
  14.     unsigned char *out_green;//传出的绿色   
  15.     unsigned char *out_blue;//传出的蓝色   
  16.     unsigned char *out_alpha;//传出的透明度   
  17.   
  18.     out_red = (unsigned char*)malloc(img_width*img_hight);//申请内存   
  19.     out_green = (unsigned char*)malloc(img_width*img_hight);  
  20.     out_blue = (unsigned char*)malloc(img_width*img_hight);  
  21.     out_alpha = (unsigned char*)malloc(img_width*img_hight);  
  22.       
  23.     temp = 0;  
  24.       
  25.     for (y=0;y<img_hight;y++) {//y的初始值为0,小于图片的高度,y自增   
  26.     //y表示的是图片第几行   
  27.         pos = y*img_width + img_width-1;  
  28.         for (x=0;x<img_width;x++) {  
  29.             out_red[pos] = in_red[temp];  
  30.             out_green[pos] = in_green[temp];  
  31.             out_blue[pos] = in_blue[temp];  
  32.             out_alpha[pos] = in_alpha[temp];  
  33.             ++temp;  
  34.             --pos;  
  35.             //假设img_width = 10,当y=0时,那么,out_red[9] = in_red[0],以此类推   
  36.             //当y=1时,out_red[19] = in_red[10],以此类推   
  37.             //把图片的每一行水平翻转   
  38.         }  
  39.     }  
  40.     memcpy(in_red,out_red,img_width*img_hight);//拷贝   
  41.     memcpy(in_green,out_green,img_width*img_hight);  
  42.     memcpy(in_blue,out_blue,img_width*img_hight);  
  43.     memcpy(in_alpha,out_alpha,img_width*img_hight);  
  44.     free(out_red);  
  45.     free(out_blue);//释放内存   
  46.     free(out_green);  
  47.     free(out_alpha);  
  48.     return 0;  
  49. }  

上面的是最初写的代码,现在想了一下,没必要申请内存存储翻转后的图形数组,于是,改成了这个:

[cpp]

  1. #include "game_data.h"   
  2. int flip_horizontal(Action_Graph *src)  
  3. /* 将图像进行水平翻转 */  
  4. {  
  5.     int value = 0,x,y,pos,temp,count;  
  6.     int width = src->width,hight = src->height;  
  7.     unsigned char buff;  
  8.     if(src->malloc != IS_TRUE) {  
  9.         value = -1;  
  10.     }  
  11.     else{  
  12.         temp = (int)width/2;  
  13.         for (y=0;y<hight;y++) {  
  14.             /* 水平翻转其实也就是交换两边的数据 */  
  15.             pos = y*img_width;  
  16.             for (x=0;x<temp;x++) {  
  17.                 count = img_width - x - 1;  
  18.                 buff = src->rgba[0][pos+x];  
  19.                 src->rgba[0][pos+x] = src->rgba[0][count];  
  20.                 src->rgba[0][count] = buff;  
  21.                   
  22.                 buff = src->rgba[1][pos+x];  
  23.                 src->rgba[1][pos+x] = src->rgba[1][count];  
  24.                 src->rgba[1][count] = buff;  
  25.                   
  26.                 buff = src->rgba[2][pos+x];  
  27.                 src->rgba[2][pos+x] = src->rgba[2][count];  
  28.                 src->rgba[2][count] = buff;  
  29.                   
  30.                 buff = src->rgba[3][pos+x];  
  31.                 src->rgba[3][pos+x] = src->rgba[3][count];  
  32.                 src->rgba[3][count] = buff;  
  33.                   
  34.                 /* “命中”点阵图的翻转 */  
  35.                 if(src->hit_flag == IS_TRUE){  
  36.                     buff = src->hit[pos+x];  
  37.                     src->hit[pos+x] = src->hit[count];  
  38.                     src->hit[count] = buff;  
  39.                 }  
  40.                 /* “攻击”点阵图的翻转 */  
  41.                 if(src->atk_flag == IS_TRUE){  
  42.                     buff = src->atk[pos+x];  
  43.                     src->atk[pos+x] = src->atk[count];  
  44.                     src->atk[count] = buff;  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49.     return value;  
  50. }  
由于现在写的游戏,需要处理攻击和被攻击,我就为每一帧的图形添加了攻击和被攻击的点阵图,方便处理。

相关内容