这里以给lib.LoadPic这个函数添加功能为例。
首先看该函数在C部分的原型:
- int HAPI_LoadPic(lua_State* pL)
- {
- int fileid = (int)lua_tonumber(pL, 1);
- int picid = (int)lua_tonumber(pL, 2);
- int x = (int)lua_tonumber(pL, 3);
- int y = (int)lua_tonumber(pL, 4);
- int nooffset = 0;
- int bright = 0;
- if (lua_isnoneornil(pL, 5) == 0)
- {
- nooffset = (int)lua_tonumber(pL, 5);
- }
- if (lua_isnoneornil(pL, 6) == 0)
- {
- bright = (int)lua_tonumber(pL, 6);
- }
- JY_LoadPic(fileid, picid, x, y, nooffset, bright);
- return 0;
- }
复制代码
可知该函数至少需要4个参数,第5和第6个可选,至于怎么看出来的…………
那么再去看其中调用的JY_LoadPic的原型:
- int JY_LoadPic(int fileid, int picid, int x, int y, int flag, int value)
- {
- return JY_LoadPicColor(fileid, picid, x, y, flag, value, 0);
- }
复制代码
这里可以知道,前面和后面同一位置的变量居然名字不同。名字不同就算了,意义也大相径庭。这说明代码非常混乱………………
先不管这个。我们发现后面又调用了一个别的,而且……名字基本也是瞎起…………
因此可以清理一下………………不行了容我蹲一会
好了清理了一下。因为黑山这里用的是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值,
看起来两边的定义还不一样,被占用了
不行了实在编不下去了,直接放结果吧
注意里面的注释大多是瞎编的。
- int HAPI_LoadPic(lua_State* pL)
- {
- int fileid = (int)lua_tonumber(pL, 1);
- int picid = (int)lua_tonumber(pL, 2);
- int x = (int)lua_tonumber(pL, 3);
- int y = (int)lua_tonumber(pL, 4);
- int flag = 0;
- int value = 0;
- int width = -1;
- int height = -1;
- if (lua_isnoneornil(pL, 5) == 0)
- {
- flag = (int)lua_tonumber(pL, 5);
- }
- if (lua_isnoneornil(pL, 6) == 0)
- {
- value = (int)lua_tonumber(pL, 6);
- }
- if (lua_isnoneornil(pL, 7) == 0)
- {
- width = (int)lua_tonumber(pL, 7);
- }
- if (lua_isnoneornil(pL, 8) == 0)
- {
- height = (int)lua_tonumber(pL, 8);
- }
- JY_LoadPic(fileid, picid, x, y, flag, value,width,height);
- return 0;
- }
复制代码
- // 加载并显示贴图
- // fileid 贴图文件id
- // picid 贴图编号
- // x,y 显示位置
- // flag 不同bit代表不同含义,缺省均为0
- // B0 0 考虑偏移xoff,yoff。=1 不考虑偏移量
- // B1 0 , 1 与背景alpla 混合显示, value 为alpha值(0-256), 0表示透明
- // B2 1 全黑
- // B3 1 全白
- // value 按照flag定义,为alpha值,
- int JY_LoadPic(int fileid, int picid, int x, int y, int flag, int value, int color, int width, int height)
- {
- struct CacheNode* newcache, *tmpcache;
- int xnew, ynew;
- SDL_Surface* tmpsur;
- picid = picid / 2;
- if (fileid < 0 || fileid >= PIC_FILE_NUM || picid < 0 || picid >= pic_file[fileid].num) // 参数错误
- {
- return 1;
- }
- if (pic_file[fileid].pcache[picid] == NULL) //当前贴图没有加载
- {
- //生成cache数据
- newcache = new CacheNode();
- if (newcache == NULL)
- {
- JY_Error("JY_LoadPic: cannot malloc newcache memory!\n");
- return 1;
- }
- newcache->id = picid;
- newcache->fileid = fileid;
- LoadPic(fileid, picid, newcache);
- ////指定宽度和高度
- //if (newcache->s != NULL && pic_file[fileid].width > 0 && pic_file[fileid].height > 0
- // && pic_file[fileid].width != newcache->s->w && pic_file[fileid].height != newcache->s->h)
- //{
- // double zoomx = (double)pic_file[fileid].width / newcache->s->w;
- // double zoomy = (double)pic_file[fileid].height / newcache->s->h;
- // if (zoomx < zoomy)
- // {
- // zoomy = zoomx;
- // }
- // else
- // {
- // zoomx = zoomy;
- // }
- // tmpsur = newcache->s;
- // newcache->s = zoomSurface(tmpsur, zoomx, zoomy, SMOOTHING_OFF);
- // newcache->xoff = (int)(zoomx * newcache->xoff);
- // newcache->yoff = (int)(zoomy * newcache->yoff);
- // //SDL_SetColorKey(newcache->s, SDL_TRUE, ConvertColor(g_MaskColor32)); //透明色
- // SDL_FreeSurface(tmpsur);
- //}
- pic_file[fileid].pcache[picid] = newcache;
- }
- else
- {
- newcache = pic_file[fileid].pcache[picid];
- }
- if (newcache->t == NULL) //贴图为空,直接退出
- {
- return 1;
- }
- if (flag & 0x00000001)
- {
- xnew = x;
- ynew = y;
- }
- else
- {
- xnew = x - newcache->xoff;
- ynew = y - newcache->yoff;
- }
- RenderTexture(newcache->t, xnew, ynew, flag, value, color, width, height);
- return 0;
- }
复制代码
- // 把表面blit到背景或者前景表面
- // x,y 要加载到表面的左上角坐标
- int RenderTexture(SDL_Texture* lps, int x, int y, int flag, int value, int color, int width, int height)
- {
- SDL_Surface* tmps;
- SDL_Rect rect, rect0;
- int i, j;
- //color = ConvertColor(g_MaskColor32);
- if (value > 255)
- {
- value = 255;
- }
- rect.x = x;
- rect.y = y;
- SDL_QueryTexture(lps, NULL, NULL, &rect.w, &rect.h);
- if (width > 0 && height > 0)
- {
- rect.w = width;
- rect.h = height;
- }
- else if (width > 0 && height <= 0)
- {
- rect.w = width;
- rect.h = width * rect.h / rect.w;
- }
- rect0 = rect;
- rect0.x = 0;
- rect0.y = 0;
- if (!lps)
- {
- JY_Error("BlitSurface: lps is null!");
- return 1;
- }
- if ((flag & 0x2) == 0) // 没有alpha
- {
- SDL_SetTextureColorMod(lps, 255, 255, 255);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
- SDL_SetTextureAlphaMod(lps, 255);
- RenderToTexture(lps, NULL, g_Texture, &rect);
- //SDL_BlitSurface(lps, NULL, g_Surface, &rect);
- }
- else // 有alpha
- {
- if ((flag & 0x4) || (flag & 0x8) || (flag & 0x10)) // 4-黑, 8-白, 16-颜色
- {
- // 4-黑, 8-白, 16-颜色
- if (flag & 0x4)
- {
- SDL_SetTextureColorMod(lps, 32, 32, 32);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
- SDL_SetTextureAlphaMod(lps, (Uint8)value);
- RenderToTexture(lps, NULL, g_Texture, &rect);
- }
- else if (flag & 0x8)
- {
- SDL_SetTextureColorMod(lps, 255, 255, 255);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_NONE);
- SDL_SetTextureAlphaMod(lps, 255);
- RenderToTexture(lps, NULL, g_TextureTmp, &rect);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_ADD);
- SDL_SetRenderDrawColor(g_Renderer, 255, 255, 255, 255);
- SDL_SetRenderDrawBlendMode(g_Renderer, SDL_BLENDMODE_ADD);
- SDL_RenderFillRect(g_Renderer, &rect);
- SDL_SetTextureColorMod(g_TextureTmp, 255, 255, 255);
- SDL_SetTextureBlendMode(g_TextureTmp, SDL_BLENDMODE_BLEND);
- SDL_SetTextureAlphaMod(g_TextureTmp, (Uint8)value);
- RenderToTexture(g_TextureTmp, &rect, g_Texture, &rect);
- SDL_SetTextureAlphaMod(g_TextureTmp, 255);
- }
- else
- {
- Uint8 r = (Uint8)((color & RMASK) >> 16);
- Uint8 g = (Uint8)((color & GMASK) >> 8);
- Uint8 b = (Uint8)((color & BMASK));
- Uint8 a = 255;
- SDL_SetTextureColorMod(lps, 255, 255, 255);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_NONE);
- SDL_SetTextureAlphaMod(lps, 255);
- RenderToTexture(lps, NULL, g_TextureTmp, &rect);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_ADD);
- SDL_SetRenderDrawColor(g_Renderer, r, g, b, a);
- SDL_SetRenderDrawBlendMode(g_Renderer, SDL_BLENDMODE_ADD);
- SDL_RenderFillRect(g_Renderer, &rect);
- SDL_SetTextureColorMod(g_TextureTmp, 255, 255, 255);
- SDL_SetTextureBlendMode(g_TextureTmp, SDL_BLENDMODE_BLEND);
- SDL_SetTextureAlphaMod(g_TextureTmp, (Uint8)value);
- RenderToTexture(g_TextureTmp, &rect, g_Texture, &rect);
- SDL_SetTextureAlphaMod(g_TextureTmp, 255);
- //SDL_SetTextureColorMod(lps, r, g, b);
- //SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
- //SDL_SetTextureAlphaMod(lps, (Uint8)value);
- //RenderToTexture(lps, NULL, g_Texture, &rect);
- }
- }
- else
- {
- SDL_SetTextureColorMod(lps, 255, 255, 255);
- SDL_SetTextureBlendMode(lps, SDL_BLENDMODE_BLEND);
- SDL_SetTextureAlphaMod(lps, (Uint8)value);
- RenderToTexture(lps, NULL, g_Texture, &rect);
- //SDL_BlitSurface(lps, NULL, g_Surface, &rect);
- }
- }
- return 0;
- }
复制代码
|