//Lua主函数 int Lua_Main(lua_State *pL_main) { int result=0; //注册lua函数 luaL_register(pL_main,"lib", jylib); luaL_register(pL_main, "Byte", bytelib); //加载lua文件 result=luaL_loadfile(pL_main,JYMain_Lua); switch(result){ case LUA_ERRSYNTAX: JY_Error("load lua file %s error: syntax error!\n",JYMain_Lua); break; case LUA_ERRMEM: JY_Error("load lua file %s error: memory allocation error!\n",JYMain_Lua); break; case LUA_ERRFILE: JY_Error("load lua file %s error: can not open file!\n",JYMain_Lua); break; } result=lua_pcall(pL_main, 0, LUA_MULTRET, 0); //调用lua的主函数JY_Main lua_getglobal(pL_main,"JY_Main"); result=lua_pcall(pL_main,0,0,0); return 0; }
//Lua读取配置信息 int Lua_Config(lua_State *pL,const char *filename) { int result=0; //加载lua配置文件 result=luaL_loadfile(pL,filename); switch(result){ case LUA_ERRSYNTAX: JY_Error("load lua file %s error: syntax error!\n",filename); break; case LUA_ERRMEM: JY_Error("load lua file %s error: memory allocation error!\n",filename); break; case LUA_ERRFILE: JY_Error("load lua file %s error: can not open file!\n",filename); break; } result=lua_pcall(pL, 0, LUA_MULTRET, 0); lua_getglobal(pL,"CONFIG"); //读取config定义的值 g_Rotate=getfield(pL,"Rotate"); g_ScreenBpp= getfield(pL, "bpp"); g_charset = getfield(pL, "charset"); g_FullScreen= getfield(pL, "FullScreen"); g_XScale= getfield(pL, "XScale"); g_YScale= getfield(pL, "YScale"); g_EnableSound =getfield(pL, "EnableSound"); IsDebug =getfield(pL, "Debug"); g_MMapAddX =getfield(pL, "MMapAddX"); g_MMapAddY =getfield(pL, "MMapAddY"); g_SMapAddX =getfield(pL, "SMapAddX"); g_SMapAddY =getfield(pL, "SMapAddY"); g_WMapAddX =getfield(pL, "WMapAddX"); g_WMapAddY =getfield(pL, "WMapAddY"); g_SoundVolume =getfield(pL, "SoundVolume"); g_MusicVolume =getfield(pL, "MusicVolume"); g_MAXCacheNum =getfield(pL, "MAXCacheNum"); g_LoadFullS =getfield(pL, "LoadFullS"); g_LoadMMapType =getfield(pL, "LoadMMapType"); g_LoadMMapScope =getfield(pL, "LoadMMapScope"); g_PreLoadPicGrp =getfield(pL, "PreLoadPicGrp"); getfieldstr(pL,"JYMain_Lua",JYMain_Lua); #ifndef WIN32 getScreenSize(&g_ScreenW, &g_ScreenH); setfield(pL, "Width", g_ScreenW); setfield(pL, "Height", g_ScreenH); #else g_ScreenW=getfield(pL,"Width"); g_ScreenH= getfield(pL, "Height"); #endif return 0; }
int Byte_create(lua_State *pL) { int x=(int)lua_tonumber(pL,1); char *p=(char *)lua_newuserdata(pL,x); //创建userdata,不需要释放了。 int i; if(p==NULL){ JY_Error("Byte_create:cannot malloc memory\n"); return 1; } for(i=0;i<x;i++) p[i]=0; return 1; }
//加载码表文件 //码表文件按照GBK顺序排序,每个GBK字符对应三个字符:gbkf,big5,unicode int LoadMB(const char* mbfile) { FILE *fp; int i,j; Uint16 gbk,gbkf,big5,unicode; fp=fopen(_(mbfile),"rb"); if(fp==NULL){ JY_Error("cannot open mbfile: %s", mbfile); return 1; } for(i=0;i<128;i++){ for(j=0;j<256;j++){ gbkj_f[i][j]=0; gbkf_j[i][j]=0; gbk_unicode[i][j]=0; gbk_big5[i][j]=0; big5_gbk[i][j]=0; } } for(i=0x81;i<=0xfe;i++) for(j=0x40;j<=0xfe;j++){ if( j != 0x7f){ gbk=i+j*256; fread(&gbkf,2,1,fp); fread(&big5,2,1,fp); fread(&unicode,2,1,fp); // gbkf = swap16(gbkf); // big5 = swap16(big5); // unicode = swap16(unicode); if(gbk!=gbkf){ gbkj_f[i-128][j]=gbkf; gbkf_j[(gbkf & 0xff)-128][(gbkf & 0xff00)>>8]=gbk; } gbk_unicode[i-128][j]=unicode; if(gbkj_f[i-128][j]==0){ //没有简体 gbk_big5[i-128][j]=big5; big5_gbk[(big5 & 0xff) & 0x7F][( big5 & 0xff00)>>8]=gbk; }
int Byte_savefile(lua_State *pL) { char *p=(char *)lua_touserdata(pL,1); const char *filename=lua_tostring(pL,2); int start=(int)lua_tonumber(pL,3); int length=(int)lua_tonumber(pL,4); FILE *fp; if((fp=fopen(filename,"r+b"))==NULL){ JY_Error("file not open ---%s",filename); return 1; } fseek(fp,start,SEEK_SET); fwrite(p,1,length,fp); fclose(fp); return 0; }
// 根据字体文件名和字号打开字体 // size 为按像素大小的字号 static TTF_Font *GetFont(const char *filename,int size) { int i; TTF_Font *myfont=NULL; for(i=0;i<FONTNUM;i++){ // 判断字体是否已打开 if((Font[i].size ==size) && (Font[i].name) && (strcmp(filename,Font[i].name)==0) ){ myfont=Font[i].font ; break; } } if(myfont==NULL){ //没有打开 myfont =TTF_OpenFont(filename,size); //打开新字体 if(myfont==NULL){ JY_Error("GetFont error: can not open font file %s\n",filename); return NULL; } Font[currentFont].size =size; if(Font[currentFont].font) //直接关闭当前字体。 TTF_CloseFont(Font[currentFont].font); Font[currentFont].font=myfont; SafeFree(Font[currentFont].name); Font[currentFont].name =(char*)malloc(strlen(filename)+1); strcpy(Font[currentFont].name,filename); currentFont++; // 增加队列入口计数 if(currentFont==FONTNUM) currentFont=0; } return myfont; }