📄 filesample.c
字号:
/*===========================================================================
FILE: FileSample.c
===========================================================================*/
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEFile.h" // File interface definitions
#include "filesample.bid"
/*-------------------------------------------------------------------
Function Prototypes
-------------------------------------------------------------------*/
static boolean FileSample_HandleEvent(IApplet * pi, AEEEvent eCode,
uint16 wParam, uint32 dwParam);
/*===============================================================================
FUNCTION DEFINITIONS
=============================================================================== */
static boolean FileSample_TestFun(IShell *pIShell, uint32 score);
int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * po,void ** ppObj)
{
*ppObj = NULL;
if(ClsId == AEECLSID_FILESAMPLE){
if(AEEApplet_New(sizeof(AEEApplet), ClsId, pIShell,po,(IApplet**)ppObj,
(AEEHANDLER)FileSample_HandleEvent,NULL)
== TRUE)
{
// Add your code here .....
return (AEE_SUCCESS);
}
}
return (EFAILED);
}
static boolean FileSample_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
AECHAR hints1[18] = {'P','r','e','s','s',' ','S','E','L',' ','t','o',' ','t','e','s','t','!'};
AECHAR hints2[13] = {'T','e','s','t',' ','S','u','c','c','e','s','s','!'};
AECHAR hints3[12] = {'T','e','s','t',' ','F','a','i','l','e','d','!'};
AEEApplet *pAEEApplet = (AEEApplet *)pi;
IDisplay *pIDisplay = pAEEApplet->m_pIDisplay;
IShell *pIShell = pAEEApplet->m_pIShell;
switch (eCode)
{
case EVT_APP_START:
IDISPLAY_ClearScreen(pIDisplay);
IDISPLAY_DrawText(pIDisplay, AEE_FONT_NORMAL, hints1,
18, 0, 50, NULL, IDF_ALIGN_CENTER);
IDISPLAY_Update(pIDisplay);
// Add your code here .....
return(TRUE);
case EVT_APP_STOP:
// Add your code here .....
return TRUE;
case EVT_KEY:
switch(wParam)
{
case AVK_SELECT:
if(FileSample_TestFun(pIShell, 10) == SUCCESS){
IDISPLAY_ClearScreen(pIDisplay);
IDISPLAY_DrawText(pIDisplay, AEE_FONT_NORMAL, hints2,
13, 0, 50, NULL, IDF_ALIGN_CENTER);
IDISPLAY_Update(pIDisplay);
}
else{
IDISPLAY_ClearScreen(pIDisplay);
IDISPLAY_DrawText(pIDisplay, AEE_FONT_NORMAL, hints3,
12, 0, 50, NULL, IDF_ALIGN_CENTER);
IDISPLAY_Update(pIDisplay);
}
return TRUE;
default:
break;
}
default:
break;
}
return FALSE;
}
/************************************************************************
/* 函数功能:记录游戏的高分信息
/*
/* 参数:
/* pIShell - IShell接口指针
/* score - 本次游戏的得分
/*
/* 返回值:
/* SUCCESS - 执行成功
/* EFAILED - 执行失败
************************************************************************/
static boolean FileSample_TestFun(IShell *pIShell, uint32 score)
{
//龙虎榜文件的文件名
const char *pszfile = "record.dat";
//存储龙虎榜文件中记录的最高分
uint32 oldscore = 0;
//IFileMgr接口指针,用于生成IFileMgr接口实例
IFileMgr *pIFileMgr = NULL;
//IFile接口指针,用于生成IFile接口实例
IFile *pIFile = NULL;
//创建IFileMgr接口实例
if(ISHELL_CreateInstance(pIShell, AEECLSID_FILEMGR, (void **)&pIFileMgr) != SUCCESS){
return EFAILED;
}
//判断龙虎榜文件是否存在
if(IFILEMGR_Test(pIFileMgr, pszfile) == SUCCESS){ //龙虎榜文件存在
//以读写方式打开龙虎榜文件,返回IFile接口实例
if((pIFile = IFILEMGR_OpenFile(pIFileMgr, pszfile, _OFM_READWRITE)) == NULL){
return EFAILED;
}
//读取龙虎榜文件中记录的最高分信息
IFILE_Read(pIFile, &oldscore, sizeof(uint32));
//判断本次游戏的得分是否超过龙虎榜记录
if(score > oldscore){ //超过
//重新定位文件指针到文件头
IFILE_Seek(pIFile, _SEEK_START, 0);
//覆盖原有记录
IFILE_Write(pIFile, &score, sizeof(uint32));
}
}
else{ //龙虎榜文件不存在
//创建龙虎榜文件,返回IFile接口实例
if((pIFile = IFILEMGR_OpenFile(pIFileMgr, pszfile, _OFM_CREATE)) == NULL){
return EFAILED;
}
//写入高分记录
IFILE_Write(pIFile, &score, sizeof(uint32));
}
//关闭文件
IFILE_Release(pIFile);
//释放IFileMgr接口实例
IFILEMGR_Release(pIFileMgr);
return SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -