歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 能將圖形水平翻轉的函數

能將圖形水平翻轉的函數

日期:2017/3/1 10:38:10   编辑:Linux編程

做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. out_red = (unsigned char*)malloc(img_width*img_hight);//申請內存
  18. out_green = (unsigned char*)malloc(img_width*img_hight);
  19. out_blue = (unsigned char*)malloc(img_width*img_hight);
  20. out_alpha = (unsigned char*)malloc(img_width*img_hight);
  21. temp = 0;
  22. for (y=0;y<img_hight;y++) {//y的初始值為0,小於圖片的高度,y自增
  23. //y表示的是圖片第幾行
  24. pos = y*img_width + img_width-1;
  25. for (x=0;x<img_width;x++) {
  26. out_red[pos] = in_red[temp];
  27. out_green[pos] = in_green[temp];
  28. out_blue[pos] = in_blue[temp];
  29. out_alpha[pos] = in_alpha[temp];
  30. ++temp;
  31. --pos;
  32. //假設img_width = 10,當y=0時,那麼,out_red[9] = in_red[0],以此類推
  33. //當y=1時,out_red[19] = in_red[10],以此類推
  34. //把圖片的每一行水平翻轉
  35. }
  36. }
  37. memcpy(in_red,out_red,img_width*img_hight);//拷貝
  38. memcpy(in_green,out_green,img_width*img_hight);
  39. memcpy(in_blue,out_blue,img_width*img_hight);
  40. memcpy(in_alpha,out_alpha,img_width*img_hight);
  41. free(out_red);
  42. free(out_blue);//釋放內存
  43. free(out_green);
  44. free(out_alpha);
  45. return 0;
  46. }

上面的是最初寫的代碼,現在想了一下,沒必要申請內存存儲翻轉後的圖形數組,於是,改成了這個:

[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. buff = src->rgba[1][pos+x];
  22. src->rgba[1][pos+x] = src->rgba[1][count];
  23. src->rgba[1][count] = buff;
  24. buff = src->rgba[2][pos+x];
  25. src->rgba[2][pos+x] = src->rgba[2][count];
  26. src->rgba[2][count] = buff;
  27. buff = src->rgba[3][pos+x];
  28. src->rgba[3][pos+x] = src->rgba[3][count];
  29. src->rgba[3][count] = buff;
  30. /* “命中”點陣圖的翻轉 */
  31. if(src->hit_flag == IS_TRUE){
  32. buff = src->hit[pos+x];
  33. src->hit[pos+x] = src->hit[count];
  34. src->hit[count] = buff;
  35. }
  36. /* “攻擊”點陣圖的翻轉 */
  37. if(src->atk_flag == IS_TRUE){
  38. buff = src->atk[pos+x];
  39. src->atk[pos+x] = src->atk[count];
  40. src->atk[count] = buff;
  41. }
  42. }
  43. }
  44. }
  45. return value;
  46. }
由於現在寫的游戲,需要處理攻擊和被攻擊,我就為每一幀的圖形添加了攻擊和被攻擊的點陣圖,方便處理。
Copyright © Linux教程網 All Rights Reserved