铁血丹心

 找回密码
 我要成为铁血侠客
搜索
查看: 2161|回复: 3

[lua复刻] 如何在lua底层添加函数功能

[复制链接]
发表于 2017-10-28 00:14 | 显示全部楼层 |阅读模式

马上注册,结交更多侠友!

您需要 登录 才可以下载或查看,没有账号?我要成为铁血侠客

x
这里以给lib.LoadPic这个函数添加功能为例。

首先看该函数在C部分的原型:

  1. int HAPI_LoadPic(lua_State* pL)
  2. {

  3.     int fileid = (int)lua_tonumber(pL, 1);
  4.     int picid = (int)lua_tonumber(pL, 2);
  5.     int x = (int)lua_tonumber(pL, 3);
  6.     int y = (int)lua_tonumber(pL, 4);
  7.     int nooffset = 0;
  8.     int bright = 0;

  9.     if (lua_isnoneornil(pL, 5) == 0)
  10.     {
  11.         nooffset = (int)lua_tonumber(pL, 5);
  12.     }

  13.     if (lua_isnoneornil(pL, 6) == 0)
  14.     {
  15.         bright = (int)lua_tonumber(pL, 6);
  16.     }

  17.     JY_LoadPic(fileid, picid, x, y, nooffset, bright);

  18.     return 0;
  19. }
复制代码


可知该函数至少需要4个参数,第5和第6个可选,至于怎么看出来的…………

那么再去看其中调用的JY_LoadPic的原型:

  1. int JY_LoadPic(int fileid, int picid, int x, int y, int flag, int value)
  2. {
  3.     return JY_LoadPicColor(fileid, picid, x, y, flag, value, 0);
  4. }
复制代码



这里可以知道,前面和后面同一位置的变量居然名字不同。名字不同就算了,意义也大相径庭。这说明代码非常混乱………………
先不管这个。我们发现后面又调用了一个别的,而且……名字基本也是瞎起…………

因此可以清理一下………………不行了容我蹲一会

好了清理了一下。因为黑山这里用的是c++,所以瞎搞了一下。那么参考jy027的设计:

flag
0x01 关闭居中显示
0x02 开启透明度
0x04 开启着色
0x08 Blend mode set to ADD
0x10 Blend mode set to MOD

虽然这么多东西都用flag设定有点诡异,不过这也是减少参数的一个办法

但是看回这边

//  flag 不同bit代表不同含义,缺省均为0
//  B0    0 考虑偏移xoff,yoff。=1 不考虑偏移量
//  B1    0     , 1 与背景alpla 混合显示, value 为alpha值(0-256), 0表示透明
//  B2            1 全黑
//  B3            1 全白
//  value 按照flag定义,为alpha值,

看起来两边的定义还不一样,被占用了

不行了实在编不下去了,直接放结果吧

注意里面的注释大多是瞎编的。

  1. int HAPI_LoadPic(lua_State* pL)
  2. {

  3.     int fileid = (int)lua_tonumber(pL, 1);
  4.     int picid = (int)lua_tonumber(pL, 2);
  5.     int x = (int)lua_tonumber(pL, 3);
  6.     int y = (int)lua_tonumber(pL, 4);
  7.     int flag = 0;
  8.     int value = 0;
  9.     int width = -1;
  10.     int height = -1;

  11.     if (lua_isnoneornil(pL, 5) == 0)
  12.     {
  13.         flag = (int)lua_tonumber(pL, 5);
  14.     }

  15.     if (lua_isnoneornil(pL, 6) == 0)
  16.     {
  17.         value = (int)lua_tonumber(pL, 6);
  18.     }

  19.     if (lua_isnoneornil(pL, 7) == 0)
  20.     {
  21.         width = (int)lua_tonumber(pL, 7);
  22.     }

  23.     if (lua_isnoneornil(pL, 8) == 0)
  24.     {
  25.         height = (int)lua_tonumber(pL, 8);
  26.     }

  27.     JY_LoadPic(fileid, picid, x, y, flag, value,width,height);

  28.     return 0;
  29. }
复制代码

  1. // 加载并显示贴图
  2. // fileid        贴图文件id
  3. // picid     贴图编号
  4. // x,y       显示位置
  5. //  flag 不同bit代表不同含义,缺省均为0
  6. //  B0    0 考虑偏移xoff,yoff。=1 不考虑偏移量
  7. //  B1    0     , 1 与背景alpla 混合显示, value 为alpha值(0-256), 0表示透明
  8. //  B2            1 全黑
  9. //  B3            1 全白
  10. //  value 按照flag定义,为alpha值,

  11. int JY_LoadPic(int fileid, int picid, int x, int y, int flag, int value, int color, int width, int height)
  12. {
  13.     struct CacheNode* newcache, *tmpcache;
  14.     int xnew, ynew;
  15.     SDL_Surface* tmpsur;

  16.     picid = picid / 2;

  17.     if (fileid < 0 || fileid >= PIC_FILE_NUM || picid < 0 || picid >= pic_file[fileid].num)    // 参数错误
  18.     {
  19.         return 1;
  20.     }

  21.     if (pic_file[fileid].pcache[picid] == NULL)     //当前贴图没有加载
  22.     {
  23.         //生成cache数据
  24.         newcache = new CacheNode();
  25.         if (newcache == NULL)
  26.         {
  27.             JY_Error("JY_LoadPic: cannot malloc newcache memory!\n");
  28.             return 1;
  29.         }

  30.         newcache->id = picid;
  31.         newcache->fileid = fileid;
  32.         LoadPic(fileid, picid, newcache);
  33.         ////指定宽度和高度
  34.         //if (newcache->s != NULL && pic_file[fileid].width > 0 && pic_file[fileid].height > 0
  35.         //    && pic_file[fileid].width != newcache->s->w && pic_file[fileid].height != newcache->s->h)
  36.         //{
  37.         //    double zoomx = (double)pic_file[fileid].width / newcache->s->w;
  38.         //    double zoomy = (double)pic_file[fileid].height / newcache->s->h;

  39.         //    if (zoomx < zoomy)
  40.         //    {
  41.         //        zoomy = zoomx;
  42.         //    }
  43.         //    else
  44.         //    {
  45.         //        zoomx = zoomy;
  46.         //    }

  47.         //    tmpsur = newcache->s;

  48.         //    newcache->s = zoomSurface(tmpsur, zoomx, zoomy, SMOOTHING_OFF);

  49.         //    newcache->xoff = (int)(zoomx * newcache->xoff);
  50.         //    newcache->yoff = (int)(zoomy * newcache->yoff);
  51.         //    //SDL_SetColorKey(newcache->s, SDL_TRUE, ConvertColor(g_MaskColor32));  //透明色
  52.         //    SDL_FreeSurface(tmpsur);
  53.         //}
  54.         pic_file[fileid].pcache[picid] = newcache;
  55.     }
  56.     else
  57.     {
  58.         newcache = pic_file[fileid].pcache[picid];
  59.     }

  60.     if (newcache->t == NULL)     //贴图为空,直接退出
  61.     {
  62.         return 1;
  63.     }

  64.     if (flag & 0x00000001)
  65.     {
  66.         xnew = x;
  67.         ynew = y;
  68.     }
  69.     else
  70.     {
  71.         xnew = x - newcache->xoff;
  72.         ynew = y - newcache->yoff;
  73.     }
  74.     RenderTexture(newcache->t, xnew, ynew, flag, value, color, width, height);
  75.     return 0;
  76. }
复制代码


  1. // 把表面blit到背景或者前景表面
  2. // x,y 要加载到表面的左上角坐标
  3. int RenderTexture(SDL_Texture* lps, int x, int y, int flag, int value, int color, int width, int height)
  4. {
  5.     SDL_Surface* tmps;
  6.     SDL_Rect rect, rect0;
  7.     int i, j;
  8.     //color = ConvertColor(g_MaskColor32);
  9.     if (value > 255)
  10.     {
  11.         value = 255;
  12.     }
  13.     rect.x = x;
  14.     rect.y = y;
  15.     SDL_QueryTexture(lps, NULL, NULL, &rect.w, &rect.h);

  16.     if (width > 0 && height > 0)
  17.     {
  18.         rect.w = width;
  19.         rect.h = height;
  20.     }
  21.     else if (width > 0 && height <= 0)
  22.     {
  23.         rect.w = width;
  24.         rect.h = width * rect.h / rect.w;
  25.     }
  26.     rect0 = rect;
  27.     rect0.x = 0;
  28.     rect0.y = 0;
  29.     if (!lps)
  30.     {
  31.         JY_Error("BlitSurface: lps is null!");
  32.         return 1;
  33.     }

  34.     if ((flag & 0x2) == 0)          // 没有alpha
  35.     {
  36.         SDL_SetTextureColorMod(lps, 255, 255, 255);
  37.         SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
  38.         SDL_SetTextureAlphaMod(lps, 255);
  39.         RenderToTexture(lps, NULL, g_Texture, &rect);
  40.         //SDL_BlitSurface(lps, NULL, g_Surface, &rect);
  41.     }
  42.     else    // 有alpha
  43.     {
  44.         if ((flag & 0x4) || (flag & 0x8) || (flag & 0x10))     // 4-黑, 8-白, 16-颜色
  45.         {
  46.             // 4-黑, 8-白, 16-颜色
  47.             if (flag & 0x4)
  48.             {
  49.                 SDL_SetTextureColorMod(lps, 32, 32, 32);
  50.                 SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
  51.                 SDL_SetTextureAlphaMod(lps, (Uint8)value);
  52.                 RenderToTexture(lps, NULL, g_Texture, &rect);
  53.             }
  54.             else if (flag & 0x8)
  55.             {
  56.                 SDL_SetTextureColorMod(lps, 255, 255, 255);
  57.                 SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_NONE);
  58.                 SDL_SetTextureAlphaMod(lps, 255);
  59.                 RenderToTexture(lps, NULL, g_TextureTmp, &rect);
  60.                 SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_ADD);
  61.                 SDL_SetRenderDrawColor(g_Renderer, 255, 255, 255, 255);
  62.                 SDL_SetRenderDrawBlendMode(g_Renderer, SDL_BLENDMODE_ADD);
  63.                 SDL_RenderFillRect(g_Renderer, &rect);
  64.                 SDL_SetTextureColorMod(g_TextureTmp, 255, 255, 255);
  65.                 SDL_SetTextureBlendMode(g_TextureTmp, SDL_BLENDMODE_BLEND);
  66.                 SDL_SetTextureAlphaMod(g_TextureTmp, (Uint8)value);
  67.                 RenderToTexture(g_TextureTmp, &rect, g_Texture, &rect);
  68.                 SDL_SetTextureAlphaMod(g_TextureTmp, 255);
  69.             }
  70.             else
  71.             {
  72.                 Uint8 r = (Uint8)((color & RMASK) >> 16);
  73.                 Uint8 g = (Uint8)((color & GMASK) >> 8);
  74.                 Uint8 b = (Uint8)((color & BMASK));
  75.                 Uint8 a = 255;
  76.                 SDL_SetTextureColorMod(lps, 255, 255, 255);
  77.                 SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_NONE);
  78.                 SDL_SetTextureAlphaMod(lps, 255);
  79.                 RenderToTexture(lps, NULL, g_TextureTmp, &rect);
  80.                 SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_ADD);
  81.                 SDL_SetRenderDrawColor(g_Renderer, r, g, b, a);
  82.                 SDL_SetRenderDrawBlendMode(g_Renderer, SDL_BLENDMODE_ADD);
  83.                 SDL_RenderFillRect(g_Renderer, &rect);
  84.                 SDL_SetTextureColorMod(g_TextureTmp, 255, 255, 255);
  85.                 SDL_SetTextureBlendMode(g_TextureTmp, SDL_BLENDMODE_BLEND);
  86.                 SDL_SetTextureAlphaMod(g_TextureTmp, (Uint8)value);
  87.                 RenderToTexture(g_TextureTmp, &rect, g_Texture, &rect);
  88.                 SDL_SetTextureAlphaMod(g_TextureTmp, 255);
  89.                 //SDL_SetTextureColorMod(lps, r, g, b);
  90.                 //SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
  91.                 //SDL_SetTextureAlphaMod(lps, (Uint8)value);
  92.                 //RenderToTexture(lps, NULL, g_Texture, &rect);
  93.             }
  94.         }
  95.         else
  96.         {
  97.             SDL_SetTextureColorMod(lps, 255, 255, 255);
  98.             SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
  99.             SDL_SetTextureAlphaMod(lps, (Uint8)value);
  100.             RenderToTexture(lps, NULL, g_Texture, &rect);
  101.             //SDL_BlitSurface(lps, NULL, g_Surface, &rect);
  102.         }
  103.     }
  104.     return 0;
  105. }
复制代码

【武侠.中国】铁血丹心论坛(大武侠):致力于推广和发展武侠文化,让我们一起努力,做全球最大的武侠社区。
可能是目前为止最好的金庸群侠传MOD游戏交流论坛,各种经典武侠游戏等你来玩,各种开源制作工具等你来实现你的游戏开发之梦。
发表于 2017-10-28 06:31 来自手机 | 显示全部楼层
感谢bt老师无私奉献
【武侠.中国】铁血丹心论坛(大武侠):致力于推广和发展武侠文化,让我们一起努力,做全球最大的武侠社区。
可能是目前为止最好的金庸群侠传MOD游戏交流论坛,各种经典武侠游戏等你来玩,各种开源制作工具等你来实现你的游戏开发之梦。
发表于 2017-10-28 09:29 | 显示全部楼层
貌似nb   
【武侠.中国】铁血丹心论坛(大武侠):致力于推广和发展武侠文化,让我们一起努力,做全球最大的武侠社区。
可能是目前为止最好的金庸群侠传MOD游戏交流论坛,各种经典武侠游戏等你来玩,各种开源制作工具等你来实现你的游戏开发之梦。
发表于 2017-10-28 12:36 来自手机 | 显示全部楼层
唔。。。。。。。。虽然看不懂,但是我还是要表达一下崇敬
【武侠.中国】铁血丹心论坛(大武侠):致力于推广和发展武侠文化,让我们一起努力,做全球最大的武侠社区。
可能是目前为止最好的金庸群侠传MOD游戏交流论坛,各种经典武侠游戏等你来玩,各种开源制作工具等你来实现你的游戏开发之梦。

本版积分规则

小黑屋|手机版|铁血丹心

GMT+8, 2024-5-2 18:05

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表