📄 lua_root.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 "lua_aleg.h"
#include "lua_root.h"
//------------------------------------------------------------------------------------------
//---------------- Pocz箃ek dzia硊 g丑wnego ------------------------------------------------
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//--- Garbage collection routine for Allegro userdata --------------------------------------
//------------------------------------------------------------------------------------------
static int allegrolib__gc (lua_State *L)
{
AUD* ad = (AUD*) lua_touserdata(L, 1);
if (ad)
switch(ad->DataType)
{
case AL_BITMAP:
if (ad->DataPtr!=NULL)
{ destroy_bitmap( (BITMAP*) ad->DataPtr);
ad->DataPtr=NULL; }
break;
case AL_ZBUFFER:
if (ad->DataPtr!=NULL)
{ destroy_zbuffer( (ZBUFFER*) ad->DataPtr);
ad->DataPtr=NULL; }
break;
case AL_FONT:
if (ad->DataPtr!=NULL)
{ destroy_font( (FONT*) ad->DataPtr);
ad->DataPtr=NULL; }
break;
case AL_RLE_SPRITE:
if (ad->DataPtr!=NULL)
{ destroy_rle_sprite( (RLE_SPRITE*) ad->DataPtr);
ad->DataPtr=NULL; }
break;
case AL_COMPILED_SPRITE:
if (ad->DataPtr!=NULL)
{ destroy_compiled_sprite( (COMPILED_SPRITE*) ad->DataPtr);
ad->DataPtr=NULL; }
break;
}
return 0;
}
// - int install_allegro(int system_id, int *errno_ptr, int (*atexit_ptr)());
//------------------------------------------------------------------------------------------
//bool allegro_init();
static int l_allegro_init(lua_State* L)
{
set_uformat(U_ASCII); //this will disappear once unicode are provided
lua_pushboolean( L , !(allegro_init()) );
return 1;
}
//------------------------------------------------------------------------------------------
//void allegro_exit();
static int l_allegro_exit(lua_State* L)
{
allegro_exit();
return 0;
}
//------------------------------------------------------------------------------------------
//void allegro_message(const char *text_format, ...);
#define MAX_PARAMS 9
static int l_allegro_message(lua_State* L)
{
int params[MAX_PARAMS+1];
int x;
if (lua_type(L,1)!=LUA_TSTRING)
return 0;
for (x=1;x<=MAX_PARAMS;x++)
switch (lua_type(L,x))
{
case LUA_TNONE:
case LUA_TNIL:
params[x]=0;
break;
case LUA_TBOOLEAN:
params[x]=lua_toboolean(L,x);
break;
case LUA_TNUMBER:
params[x]=lua_tonumber(L,x);
break;
case LUA_TSTRING:
params[x]=(int) lua_tostring(L,x);
break;
default:
params[x]=0;
}
allegro_message( (char*) params[1],params[2],params[3],params[4],
params[5],params[6],params[7],params[8],params[9]);
return 0;
}
//------------------------------------------------------------------------------------------
//extern char allegro_id[];
static int l_allegro_id(lua_State* L)
{
lua_pushstring( L , allegro_id );
return 1;
}
//------------------------------------------------------------------------------------------
//extern char allegro_error[ALLEGRO_ERROR_SIZE];
static int l_allegro_error(lua_State* L)
{
lua_pushstring( L , allegro_error );
return 1;
}
//------------------------------------------------------------------------------------------
//I'm not sure whether it makes sense to return os_type as string value
//probably I should provide function returning integer constant, just in case ?
//extern int os_type;
#define case_os(a,b) case a: lua_pushstring(L,b); break;
//extern int os_type;
static int l_os_type(lua_State* L)
{
switch(os_type)
{
case_os( OSTYPE_UNKNOWN , "UNKNOWN" )
case_os( OSTYPE_WIN3 , "WIN3" )
case_os( OSTYPE_WIN95 , "WIN95" )
case_os( OSTYPE_WIN98 , "WIN98" )
case_os( OSTYPE_WINME , "WINME" )
case_os( OSTYPE_WINNT , "WINNT" )
case_os( OSTYPE_WIN2000 , "WIN2000" )
case_os( OSTYPE_WINXP , "WINXP" )
case_os( OSTYPE_OS2 , "OS2" )
case_os( OSTYPE_WARP , "WARP" )
case_os( OSTYPE_DOSEMU , "DOSEMU" )
case_os( OSTYPE_OPENDOS , "OPENDOS" )
case_os( OSTYPE_LINUX , "LINUX" )
case_os( OSTYPE_SUNOS , "SUNOS" )
case_os( OSTYPE_FREEBSD , "FREEBSD" )
case_os( OSTYPE_NETBSD , "NETBSD" )
case_os( OSTYPE_IRIX , "IRIX" )
case_os( OSTYPE_QNX , "QNX" )
case_os( OSTYPE_UNIX , "UNIX" )
case_os( OSTYPE_BEOS , "BEOS" )
case_os( OSTYPE_MACOS , "MACOS" )
default: lua_pushstring(L,"UNKNOWN");
}
return 1;
}
//------------------------------------------------------------------------------------------
static int l_os_version(lua_State* L)
{
lua_pushnumber(L,os_version);
return 1;
}
//------------------------------------------------------------------------------------------
static int l_os_revision(lua_State* L)
{
lua_pushnumber(L,os_revision);
return 1;
}
//------------------------------------------------------------------------------------------
void setfield (lua_State *L, const char *key, int value)
{
lua_pushstring(L, key);
lua_pushnumber(L, value);
lua_rawset(L, -3);
}
//------------------------------------------------------------------------------------------
void setfield_d (lua_State *L, const char *key, double value) {
lua_pushstring(L, key);
lua_pushnumber(L, value);
lua_rawset(L, -3);
}
//------------------------------------------------------------------------------------------
//extern int os_multitasking;
static int l_os_multitasking(lua_State* L)
{
lua_pushboolean(L,os_multitasking);
return 1;
}
//------------------------------------------------------------------------------------------
//void set_window_title(const char *name);
int l_set_window_title(lua_State* L)
{
const char* new_title= lua_tostring(L,1);
if (new_title==NULL) new_title="";
set_window_title( new_title );
return 0;
}
//------------------------------------------------------------------------------------------
//int set_close_button_callback(void (*proc)(void));
//implementation of this function is probably difficult. Is it in a separate thread or something?
//If yes, I would prefer setting some kind of a volatile flag.
//There would be a function allowing lua user to poll and clear such a flag
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
//int desktop_color_depth();
static int l_desktop_color_depth(lua_State* L)
{
lua_pushnumber(L,desktop_color_depth());
return 1;
}
//------------------------------------------------------------------------------------------
//int get_desktop_resolution(int *width, int *height);
static int l_get_desktop_resolution(lua_State* L)
{
int w,h;
if (!get_desktop_resolution(&w,&h)) //returns 0 on success, ! is not a mistake
{
lua_pushnumber(L,w);
lua_pushnumber(L,h);
return 2;
}
else
return 0;
}
//------------------------------------------------------------------------------------------
//cpu_vendor, cpu_family, cpu_model, cpu_capabilities = check_cpu();
//It would be better to provide text in all four parameters. Improve it in the future
static int l_check_cpu(lua_State* L)
{
check_cpu();
lua_pushstring(L,cpu_vendor);
lua_pushnumber(L,cpu_family);
lua_pushnumber(L,cpu_model);
lua_pushnumber(L,cpu_capabilities);
return 4;
}
//---------------------------------------------------------------------------------------------
volatile int close_button_flag = FALSE;
//---------------------------------------------------------------------------------------------
void close_button_handler(void)
{
close_button_flag = TRUE;
}
END_OF_FUNCTION(close_button_handler)
//---------------------------------------------------------------------------------------------
int l_close_button_pressed(lua_State* L)
{
lua_pushboolean(L,close_button_flag);
close_button_flag=FALSE;
return 1;
}
//---------------------------------------------------------------------------------------------
int l_set_close_button_callback(lua_State* L)
{
LOCK_FUNCTION(close_button_handler);
lua_pushboolean(L, !set_close_button_callback(close_button_handler));
return 1;
}
//-----------------------------------------------------------------------------------------------
int l_algif_init(lua_State* L)
{
algif_init();
return 0;
}
//---------------------------------------------------------------------------------------------
static const struct luaL_reg allegro_root_lib [] = {
{"allegro_init", l_allegro_init},
{"allegro_exit", l_allegro_exit},
{"algif_init", l_algif_init},
{"allegro_id", l_allegro_id},
{"os_type", l_os_type},
{"os_version", l_os_version},
{"os_revision", l_os_revision},
{"os_multitasking", l_os_multitasking},
{"allegro_error", l_allegro_error},
{"message", l_allegro_message},
{"allegro_message", l_allegro_message},
{"set_window_title", l_set_window_title},
{"desktop_color_depth", l_desktop_color_depth},
{"get_desktop_resolution", l_get_desktop_resolution},
{"check_cpu", l_check_cpu},
{"close_button_pressed", l_close_button_pressed},
{"set_close_button_callback", l_set_close_button_callback},
{NULL, NULL} /* sentinel */
};
//-----------------------------------------------------------------------------------------------
static struct
{
char* key;
int value;
}
cpu_cap_const[]={
{ "CPU_ID", CPU_ID },
{ "CPU_FPU", CPU_FPU },
{ "CPU_IA64", CPU_IA64 },
{ "CPU_AMD64", CPU_AMD64 },
{ "CPU_MMX", CPU_MMX },
{ "CPU_MMXPLUS", CPU_MMXPLUS },
{ "CPU_SSE", CPU_SSE },
{ "CPU_SSE2", CPU_SSE2 },
{ "CPU_SSE3", CPU_SSE3 },
{ "CPU_3DNOW", CPU_3DNOW },
{ "CPU_ENH3DNOW", CPU_ENH3DNOW },
{ "CPU_CMOV", CPU_CMOV },
{ NULL, 0 }
};
//-----------------------------------------------------------------------------------------------
int luaopen_allegro_root(lua_State *L)
{
int i=0;
luaL_newmetatable(L, "allegro_meta");
lua_pushstring(L, "__gc");
lua_pushcfunction(L, allegrolib__gc);
lua_settable(L, -3);
luaL_openlib(L, "allegro", allegro_root_lib, 0);
//zarejestruj sta彻 informuj筩
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -