⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lua_pal.c

📁 lua脚本语言调用allegro游戏程序库的接口-跨平台
💻 C
字号:
/****************************************************************************
**                                                                         **
**                  LuAllegro library - Lua port of Allegro                **
**                       Copyright (C) 2005 Peter Jamroz                   **
**                 e-mail: peterjam at poczta dot onet dot pl              **
**                                                                         **
**        Permission to use, copy, modify, publish this software           **
**    is hereby granted to any person obtaining a copy of this software,   **
**                              FREE OF CHARGE,                            **
**                     under the following conditions:                     **
**                                                                         **
** 1:  The above copyright notice and this permission notice shall be      **
**     included in all copies or substantial portions of the Software.     **
**                                                                         **
** 2:  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,     **
**   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    **
**   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.**
**   IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY    **
**   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  **
**   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     **
**   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                **
**                                                                         **
****************************************************************************/
#include <allegro.h>
#include <string.h>

#include "lua_aleg.h"
#include "lua_pal.h"

#define max(a,b)  (((a) > (b)) ? (a) : (b))
#define min(a,b)  (((a) < (b)) ? (a) : (b))


//------------------------------------------------------------------------------------------
//----- Allegro chapter "Palette routines" -------------------------------------------------
//------------------------------------------------------------------------------------------
int is_rgb_table_format(lua_State* L,int st_pos,int index)
{
   int result=0;
   lua_pushnumber (L, index);
   lua_gettable(L, st_pos);
   result = lua_istable(L, -1);
   lua_pop(L, 1);
   return result;
}

//------------------------------------------------------------------------------------------
// helper function. Converts lua table to PALETTE structure. returns 0 on failure
int ConvertLuaToPalette(lua_State* L,int st_pos,void* pal,int start,int end)
{
int r,g,b;
int i;
int j=1;
RGB* palette=(RGB*) pal;
   
   if (!palette)
      return 0;
   
   if (!lua_istable(L, st_pos))
      return 0;

   if (is_rgb_table_format(L,st_pos,start+1))
   {
      for (i=start; i<=end; i++)
      {
         lua_pushnumber (L, i+1);
         lua_gettable(L, st_pos);
         if (lua_istable(L, -1))
         {
            lua_pushlstring (L, "r",1);
            lua_gettable(L, -2);
            r=(int)lua_tonumber(L,-1);
            lua_pop(L, 1); 

            lua_pushlstring (L, "g",1);
            lua_gettable(L, -2);
            g=(int)lua_tonumber(L,-1);
            lua_pop(L, 1); 

            lua_pushlstring (L, "b",1);
            lua_gettable(L, -2);
            b=(int)lua_tonumber(L,-1);
            lua_pop(L, 1); 

         }
         else
         {
            r=0;g=0;b=0;
         }

         palette[i].r=r;
         palette[i].g=g;
         palette[i].b=b;
         lua_pop(L, 1); 
      }
   }
   else
   {
      j= (start*3) + 1;
      for (i=start; i<=end; i++)
      {
         lua_pushnumber (L, j++);
         lua_gettable(L, st_pos);
         r=(int)lua_tonumber(L,-1);
         lua_pop(L, 1); 

         lua_pushnumber (L, j++);
         lua_gettable(L, st_pos);
         g=(int)lua_tonumber(L,-1);
         lua_pop(L, 1); 
      
         lua_pushnumber (L, j++);
         lua_gettable(L, st_pos);
         b=(int)lua_tonumber(L,-1);
         lua_pop(L, 1); 

         palette[i].r=r;
         palette[i].g=g;
         palette[i].b=b;
      }
   }
return 1;
}

//---------------------------------------------------------------------------------------------
int ConvertPaletteToLua(lua_State* L,void* pal,int start,int end,int pformat)

{
   int x,j=1;
   RGB* palette=(RGB*) pal;

   lua_newtable (L); 

   if (pformat)
   {
      for (x=start; x<=end; x++)
      {
         lua_pushnumber(L,x+1);
         lua_newtable (L);
         
         lua_pushlstring(L,"r",1);
         lua_pushnumber(L,palette[x].r);
         lua_settable(L,-3);

         lua_pushlstring(L,"g",1);
         lua_pushnumber(L,palette[x].g);
         lua_settable(L,-3);

         lua_pushlstring(L,"b",1);
         lua_pushnumber(L,palette[x].b);
         lua_settable(L,-3);

         lua_settable(L,-3);
      }
   }
   else
   {
      j=(start*3) + 1;
      for (x=start; x<=end; x++)
      {
         lua_pushnumber(L,j++);
         lua_pushnumber(L,palette[x].r);
         lua_settable(L,-3);

         lua_pushnumber(L,j++);
         lua_pushnumber(L,palette[x].g);
         lua_settable(L,-3);

         lua_pushnumber(L,j++);
         lua_pushnumber(L,palette[x].b);
         lua_settable(L,-3);
      }
   }

return 1;
}

//---------------------------------------------------------------------------------------------
//void set_color(index,r,g,b);
int l_set_color(lua_State* L)
{
   int index= (int) lua_tonumber(L,1);
   int r=     (int) lua_tonumber(L,2);
   int g=     (int) lua_tonumber(L,3);
   int b=     (int) lua_tonumber(L,4);
   RGB rgb;

   if (index>255 || index<0) return 0;
   rgb.r=max(0,min(255,r));
   rgb.g=max(0,min(255,g));
   rgb.b=max(0,min(255,b));

   set_color(index,&rgb);
   return 0;
}

//---------------------------------------------------------------------------------------------
int l_get_color(lua_State* L)
{
   int index= (int) lua_tonumber(L,1);
   RGB rgb;
   
   if (index>255 || index<0) return 0;

   get_color(index,&rgb);

   lua_pushnumber(L,rgb.r);
   lua_pushnumber(L,rgb.g);
   lua_pushnumber(L,rgb.b);
   return 3;
}

//##########################################################################################
//---------------------------------------------------------------------------------------------
int l_set_palette(lua_State* L)
{
PALETTE palette;

if (ConvertLuaToPalette(L,1,palette,0,255))
   set_palette(palette);
   
return 0;
}

//---------------------------------------------------------------------------------------------
//void set_palette_range(const PALETTE p, int from, int to, int vsync);
int l_set_palette_range(lua_State* L)
{
   PALETTE palette;

   int     from=    (int)     lua_tonumber(L,2);
   int     to=      (int)     lua_tonumber(L,3);
   int     vsync=   (int)     lua_toboolean(L,4);
   
   from=   max(0,min(255,from));
   to=     max(from,min(255,to));
   
   if (ConvertLuaToPalette(L,1,palette,from,to))
      set_palette_range(palette,from,to,vsync);
   
return 0;
}



//---------------------------------------------------------------------------------------------
//void get_palette(PALETTE p);
//if format = "rgb" then palette is returned as table of tables: 
//{{r=r1,g=g1,b=b1},{r=r2,g=g2,b=b2}, ... ,{r=rn,g=gn,b=bn}}
//else palette is returned in format {r1,g1,b1,r2,g2,b2, ... ,rn,gn,bn}
//table = get_palette(string format)
int l_get_palette(lua_State* L)
{
   const char* format= lua_tostring(L,1);
   int   pformat = !(format==NULL) && !strcmpi(format,"rgb");
   PALETTE pal;
   
   get_palette(pal);

   return ConvertPaletteToLua(L,pal,0,255,pformat);
}

//---------------------------------------------------------------------------------------------
//extern PALETTE default_palette;
int l_get_default_palette(lua_State* L)
{
   const char* format= lua_tostring(L,1);
   int   pformat = !(format==NULL) && !strcmpi(format,"rgb");
   
   return ConvertPaletteToLua(L,default_palette,0,255,pformat);
}

//---------------------------------------------------------------------------------------------
//void get_palette_range(PALETTE p, int from, int to);
int l_get_palette_range(lua_State* L)
{
   int     from=    (int)     lua_tonumber(L,1);
   int     to=      (int)     lua_tonumber(L,2);
   const   char* format=      lua_tostring(L,3);
   int     pformat =          !(format==NULL) && !strcmpi(format,"rgb");
   PALETTE pal;
   
   get_palette_range(pal,from,to);

   return ConvertPaletteToLua(L,pal,from,to,pformat);
}

//---------------------------------------------------------------------------------------------
//void select_palette(const PALETTE p);
int l_select_palette(lua_State* L)
{
PALETTE palette;

if (ConvertLuaToPalette(L,1,palette,0,255))
   select_palette(palette);
  
return 0;
}

//---------------------------------------------------------------------------------------------
//pal= generate_332_palette();
int l_generate_332_palette(lua_State* L)
{
   const   char* format=      lua_tostring(L,3);
   int     pformat =          !(format==NULL) && !strcmpi(format,"rgb");
   PALETTE pal;
   
   generate_332_palette(pal);  
   
   return ConvertPaletteToLua(L,pal,0,255,pformat);
}

//---------------------------------------------------------------------------------------------
int l_unselect_palette(lua_State* L)
{
   unselect_palette();
   return 0;
}

//---------------------------------------------------------------------------------------------
int l_fade_in  (lua_State* L)
{
   PALETTE palette;
   int speed;
   if (ConvertLuaToPalette(L,1,palette,0,255))
   {
      speed = (int) lua_tonumber(L,2);
      speed = max(1,min(64,speed));
      fade_in(palette,speed);
   }
   return 0;
}

//---------------------------------------------------------------------------------------------
int l_fade_out (lua_State *L) 
{
   int speed= (int) lua_tonumber(L,1);
   speed=  max(1,min(64,speed));
   fade_out(speed);
   return 0;  
}

//---------------------------------------------------------------------------------------------
//void fade_out_range(int speed, int from, int to);
int l_fade_out_range(lua_State* L)
{
   int speed= (int) lua_tonumber(L,1);
   int from=  (int) lua_tonumber(L,2);
   int to=    (int) lua_tonumber(L,3);

   speed=  max(1,min(64,speed));
   from=   max(0,min(255,from));
   to=     max(from,min(255,to));
   
   fade_out_range(speed,from,to);
   return 0;
}

//---------------------------------------------------------------------------------------------
//fade_from(const PALETTE source, const PALETTE dest, int speed);
int l_fade_from(lua_State* L)
{
   PALETTE source;
   PALETTE destin;
   int     speed;
   if (ConvertLuaToPalette(L,1,source,0,255) &&
       ConvertLuaToPalette(L,2,destin,0,255) )
   {
      speed=   (int) lua_tonumber(L,3);
      speed=   max(1,min(64,speed));  
      
      fade_from(source,destin,speed);
   }
   return 0;
}

//---------------------------------------------------------------------------------------------
//void fade_from_range(const PALETTE source, const PALETTE dest, int speed, int from, int to);
int l_fade_from_range(lua_State* L)
{
   int from=  (int) lua_tonumber(L,4);
   int to=    (int) lua_tonumber(L,5);
   PALETTE source;
   PALETTE destin;

   if (ConvertLuaToPalette(L,1,source,from,to) &&
       ConvertLuaToPalette(L,2,destin,from,to) )
   {
      int speed= (int) lua_tonumber(L,3);

      speed=  max(1,min(64,speed));
      from=   max(0,min(255,from));
      to=     max(from,min(255,to));

      fade_from_range(source,destin,speed,from,to);
   }
   return 0;
}

//---------------------------------------------------------------------------------------------
//output = fade_interpolate(const PALETTE source, const PALETTE dest, int pos, int from, int to,char* format);
int l_fade_interpolate(lua_State* L)
{
   int pos=   (int) lua_tonumber(L,3);
   int from=  (int) lua_tonumber(L,4);
   int to=    (int) lua_tonumber(L,5);
   const char* format= lua_tostring(L,6);
   int   pformat = !(format==NULL) && !strcmpi(format,"rgb");
   
   PALETTE source;
   PALETTE destin;
   PALETTE output;

   from=   max(0,min(255,from));
   to=     max(from,min(255,to));

   if (ConvertLuaToPalette(L,1,source,from,to) &&
       ConvertLuaToPalette(L,2,destin,from,to) )
   {
      to=     max(from,min(255,to));
      fade_interpolate(source,destin,output,pos,from,to);
      return ConvertPaletteToLua(L,output,from,to,pformat);
   }
   return 0;
}

//---------------------------------------------------------------------------------------------
//void fade_in_range(const PALETTE p, int speed, int from, int to);
int l_fade_in_range(lua_State* L)
{
   int from=  (int) lua_tonumber(L,3);
   int to=    (int) lua_tonumber(L,4);
   PALETTE source;

   if (ConvertLuaToPalette(L,1,source,from,to))
   {
      int speed= (int) lua_tonumber(L,2);

      speed=  max(1,min(64,speed));
      from=   max(0,min(255,from));
      to=     max(from,min(255,to));

      fade_in_range(source,speed,from,to);
   }
   return 0;
}



//---------------------------------------------------------------------------------------------
static const struct luaL_reg allegro_pal_lib [] = {
      {"set_color",            l_set_color},
      {"get_color",            l_get_color},

      {"set_palette",          l_set_palette},
      {"set_palette_range",    l_set_palette_range},

      {"get_palette",          l_get_palette},
      {"get_default_palette",  l_get_default_palette},

      {"get_palette_range",    l_get_palette_range},

      {"generate_332_palette", l_generate_332_palette},
      {"fade_interpolate",     l_fade_interpolate},

      {"select_palette",       l_select_palette},
      {"unselect_palette",     l_unselect_palette},
      
      {"fade_in",              l_fade_in},
      {"fade_in_range",        l_fade_in_range},
      
      {"fade_out",             l_fade_out},
      {"fade_out_range",       l_fade_out_range},
      
      {"fade_from",            l_fade_from},
      {"fade_from_range",      l_fade_from_range},

      {NULL,                   NULL}  /* sentinel */
    };


//-----------------------------------------------------------------------------------------------
int luaopen_allegro_pal (lua_State *L) 
{
   luaL_openlib(L, "allegro", allegro_pal_lib, 0);
   return 1;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -