📄 lua_load.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"
#include "lua_load.h"
//------------------------------------------------------------------------------------------
#define set_field_i(L,key,intvalue){\
lua_pushstring(L,key);\
lua_pushnumber(L,intvalue);\
lua_settable(L,-3);}
//------------------------------------------------------------------------------------------
#define set_field_n(L,indx,intvalue){\
lua_pushnumber(L,indx);\
lua_pushnumber(L,intvalue);\
lua_settable(L,-3);}
//------------------------------------------------------------------------------------------
//another helper function
int StoreBitmapAndPalette(lua_State* L,BITMAP* bmp,RGB* pal)
{
AUD* ad;
if (bmp==NULL)
return 0;
else
{
ad= (AUD*) lua_newuserdata (L, sizeof(AUD));
if (!ad)
{ destroy_bitmap(bmp); return 0; }
luaL_getmetatable(L, "allegro_meta");
lua_setmetatable(L, -2);
ad->DataType = AL_BITMAP;
ad->DataPtr = bmp;
if (pal)
{
ConvertPaletteToLua(L,pal,0,255,0);
return 2;
}
else
return 1;
}
}
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//--- Bitmap functions ---------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//C:
//BITMAP *load_bitmap(const char *filename, RGB *pal);
//Lua:
//bitmap,palette = load_bitmap(filename,bool save_palette)
static int l_load_bitmap(lua_State* L)
{
BITMAP* bmp;
PALETTE palette;
RGB* pal=NULL;
char* filename =(char*) lua_tostring(L,1); if (!filename) return 0;
if (lua_toboolean(L,2)) pal= palette;
bmp= load_bitmap(filename,pal);
return StoreBitmapAndPalette(L,bmp,pal);
}
//------------------------------------------------------------------------------------------
//C:
//BITMAP *load_bmp(const char *filename, RGB *pal);
//Lua:
//bitmap,palette = load_bmp(filename)
static int l_load_bmp(lua_State* L)
{
BITMAP* bmp;
PALETTE palette;
RGB* pal=NULL;
char* filename =(char*) lua_tostring(L,1); if (!filename) return 0;
if (lua_toboolean(L,2)) pal= palette;
bmp= load_bmp(filename,pal);
return StoreBitmapAndPalette(L,bmp,pal);
}
//------------------------------------------------------------------------------------------
//C:
//BITMAP *load_lbm(const char *filename, RGB *pal);
//Lua:
//bitmap,palette = load_lbm(filename)
static int l_load_lbm(lua_State* L)
{
BITMAP* bmp;
PALETTE palette;
RGB* pal=NULL;
char* filename =(char*) lua_tostring(L,1); if (!filename) return 0;
if (lua_toboolean(L,2)) pal= palette;
bmp= load_lbm(filename,pal);
return StoreBitmapAndPalette(L,bmp,pal);
}
//------------------------------------------------------------------------------------------
//C:
//BITMAP *load_pcx(const char *filename, RGB *pal);
//Lua:
//bitmap,palette = load_pcx(filename)
static int l_load_pcx(lua_State* L)
{
BITMAP* bmp;
PALETTE palette;
RGB* pal=NULL;
char* filename =(char*) lua_tostring(L,1); if (!filename) return 0;
if (lua_toboolean(L,2)) pal= palette;
bmp= load_pcx(filename,pal);
return StoreBitmapAndPalette(L,bmp,pal);
}
//------------------------------------------------------------------------------------------
//C:
//BITMAP *load_tga(const char *filename, RGB *pal);
//Lua:
//bitmap,palette = load_tga(filename)
static int l_load_tga(lua_State* L)
{
BITMAP* bmp;
PALETTE palette;
RGB* pal=NULL;
char* filename =(char*) lua_tostring(L,1); if (!filename) return 0;
if (lua_toboolean(L,2)) pal= palette;
bmp= load_tga(filename,pal);
return StoreBitmapAndPalette(L,bmp,pal);
}
//---------------------------------------------------------------------------------------------
//C:
//int save_bitmap(const char *filename, BITMAP *bmp, const RGB *pal);
//Lua:
//bool save_bitmap(filename,bitmap,palette)//memfill
static int l_save_bitmap(lua_State* L)
{
PALETTE palette;
AUD* ad;
int result;
char* filename =(char*) lua_tostring(L,1);
BITMAP* bmp;
if (!filename)
{
lua_pushboolean(L,0);
return 1;
}
ad= (AUD*) lua_touserdata(L,2);
if (!ad || ad->DataType!=AL_BITMAP)
bmp=screen; //use screen if bitmap not provided
else
bmp=(BITMAP*) ad->DataPtr;
if (!ConvertLuaToPalette(L,3,palette,0,255))
{
get_palette(palette); //use current palette if palette not provided
}
result=save_bitmap(filename,bmp,palette);
lua_pushboolean(L,!result);
return 1;
}
//---------------------------------------------------------------------------------------------
//C:
//int save_bmp(const char *filename, BITMAP *bmp, const RGB *pal);
//Lua:
//bool save_bmp(filename,bitmap,palette)
static int l_save_bmp(lua_State* L)
{
PALETTE palette;
AUD* ad;
int result;
char* filename =(char*) lua_tostring(L,1);
BITMAP* bmp;
if (!filename)
{
lua_pushboolean(L,0);
return 1;
}
ad= (AUD*) lua_touserdata(L,2);
if (!ad || ad->DataType!=AL_BITMAP)
bmp=screen; //use screen if bitmap not provided
else
bmp=(BITMAP*) ad->DataPtr;
if (!ConvertLuaToPalette(L,3,palette,0,255))
{
get_palette(palette); //use current palette if palette not provided
}
result=save_bmp(filename,bmp,palette);
lua_pushboolean(L,!result);
return 1;
}
//---------------------------------------------------------------------------------------------
//C:
//int save_pcx(const char *filename, BITMAP *bmp, const RGB *pal);
//Lua:
//bool save_pcx(filename,bitmap,palette)
static int l_save_pcx(lua_State* L)
{
PALETTE palette;
AUD* ad;
int result;
char* filename =(char*) lua_tostring(L,1);
BITMAP* bmp;
if (!filename)
{
lua_pushboolean(L,0);
return 1;
}
ad= (AUD*) lua_touserdata(L,2);
if (!ad || ad->DataType!=AL_BITMAP)
bmp=screen; //use screen if bitmap not provided
else
bmp=(BITMAP*) ad->DataPtr;
if (!ConvertLuaToPalette(L,3,palette,0,255))
{
get_palette(palette); //use current palette if palette not provided
}
result=save_pcx(filename,bmp,palette);
lua_pushboolean(L,!result);
return 1;
}
//---------------------------------------------------------------------------------------------
//C:
//int save_tga(const char *filename, BITMAP *bmp, const RGB *pal);
//Lua:
//bool save_tga(filename,bitmap,palette)
static int l_save_tga(lua_State* L)
{
PALETTE palette;
AUD* ad;
int result;
char* filename =(char*) lua_tostring(L,1);
BITMAP* bmp;
if (!filename)
{
lua_pushboolean(L,0);
return 1;
}
ad= (AUD*) lua_touserdata(L,2);
if (!ad || ad->DataType!=AL_BITMAP)
bmp=screen; //use screen if bitmap not provided
else
bmp=(BITMAP*) ad->DataPtr;
if (!ConvertLuaToPalette(L,3,palette,0,255))
{
get_palette(palette); //use current palette if palette not provided
}
result=save_tga(filename,bmp,palette);
lua_pushboolean(L,!result);
return 1;
}
//---------------------------------------------------------------------------------------------
//C:
//void set_color_conversion(int mode);
//Lua:
//allegro.set_color_conversion(mode)
static int l_set_color_conversion(lua_State* L)
{
int cc;
if (lua_type(L,1)!=LUA_TNUMBER)
return 0;
cc= (int) lua_tonumber(L,1);
set_color_conversion(cc);
return 0;
}
//---------------------------------------------------------------------------------------------
//C:
//int get_color_conversion();
//Lua:
//cc= allegro.get_color_conversion()
static int l_get_color_conversion(lua_State* L)
{
int cc= get_color_conversion();
lua_pushnumber(L,cc);
return 1;
}
//---------------------------------------------------------------------------------------------
struct
{
int value;
char* key;
} cc_const[]={
{ COLORCONV_NONE, "COLORCONV_NONE" },
{ COLORCONV_8_TO_15, "COLORCONV_8_TO_15" },
{ COLORCONV_8_TO_16, "COLORCONV_8_TO_16" },
{ COLORCONV_8_TO_24, "COLORCONV_8_TO_24" },
{ COLORCONV_8_TO_32, "COLORCONV_8_TO_32" },
{ COLORCONV_15_TO_8, "COLORCONV_15_TO_8" },
{ COLORCONV_15_TO_16, "COLORCONV_15_TO_16" },
{ COLORCONV_15_TO_24, "COLORCONV_15_TO_24" },
{ COLORCONV_15_TO_32, "COLORCONV_15_TO_32" },
{ COLORCONV_16_TO_8, "COLORCONV_16_TO_8" },
{ COLORCONV_16_TO_15, "COLORCONV_16_TO_15" },
{ COLORCONV_16_TO_24, "COLORCONV_16_TO_24" },
{ COLORCONV_16_TO_32, "COLORCONV_16_TO_32" },
{ COLORCONV_24_TO_8, "COLORCONV_24_TO_8" },
{ COLORCONV_24_TO_15, "COLORCONV_24_TO_15" },
{ COLORCONV_24_TO_16, "COLORCONV_24_TO_16" },
{ COLORCONV_24_TO_32, "COLORCONV_24_TO_32" },
{ COLORCONV_32_TO_8, "COLORCONV_32_TO_8" },
{ COLORCONV_32_TO_15, "COLORCONV_32_TO_15" },
{ COLORCONV_32_TO_16, "COLORCONV_32_TO_16" },
{ COLORCONV_32_TO_24, "COLORCONV_32_TO_24" },
{ COLORCONV_32A_TO_8, "COLORCONV_32A_TO_8" },
{ COLORCONV_32A_TO_15, "COLORCONV_32A_TO_15" },
{ COLORCONV_32A_TO_16, "COLORCONV_32A_TO_16" },
{ COLORCONV_32A_TO_24, "COLORCONV_32A_TO_24" },
{ COLORCONV_DITHER_PAL, "COLORCONV_DITHER_PAL" },
{ COLORCONV_DITHER_HI, "COLORCONV_DITHER_HI" },
{ COLORCONV_KEEP_TRANS, "COLORCONV_KEEP_TRANS" },
{ COLORCONV_EXPAND_256, "COLORCONV_EXPAND_256" },
{ COLORCONV_REDUCE_TO_256, "COLORCONV_REDUCE_TO_256" },
{ COLORCONV_EXPAND_15_TO_16, "COLORCONV_EXPAND_15_TO_16" },
{ COLORCONV_REDUCE_16_TO_15, "COLORCONV_REDUCE_16_TO_15" },
{ COLORCONV_EXPAND_HI_TO_TRUE, "COLORCONV_EXPAND_HI_TO_TRUE" },
{ COLORCONV_REDUCE_TRUE_TO_HI, "COLORCONV_REDUCE_TRUE_TO_HI" },
{ COLORCONV_24_EQUALS_32, "COLORCONV_24_EQUALS_32" },
{ COLORCONV_TOTAL, "COLORCONV_TOTAL" },
{ COLORCONV_PARTIAL, "COLORCONV_PARTIAL" },
{ COLORCONV_MOST, "COLORCONV_MOST" },
{ COLORCONV_DITHER, "COLORCONV_DITHER" },
{ COLORCONV_KEEP_ALPHA, "COLORCONV_KEEP_ALPHA" },
{ 0, NULL} /* sentinel */ };
//---------------------------------------------------------------------------------------------
static const struct luaL_reg allegro_load_lib [] = {
{"load_bitmap", l_load_bitmap},
{"load_bmp", l_load_bmp},
{"load_lbm", l_load_lbm},
{"load_pcx", l_load_pcx},
{"load_tga", l_load_tga},
{"save_bitmap", l_save_bitmap},
{"save_bmp", l_save_bmp},
{"save_pcx", l_save_pcx},
{"save_tga", l_save_tga},
{"set_color_conversion", l_set_color_conversion},
{"get_color_conversion", l_get_color_conversion},
{NULL, NULL} /* sentinel */
};
//-----------------------------------------------------------------------------------------------
int luaopen_allegro_load(lua_State *L)
{
int i=0;
luaL_openlib(L, "allegro", allegro_load_lib, 0);
//zarejestruj sta砮 allegro
lua_getglobal(L, "allegro");
while(cc_const[i].key!=NULL)
{setfield(L,cc_const[i].key,cc_const[i].value);i++;}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -