📄 cxw.c
字号:
/*===========================================================================
FILE: cxw.c
===========================================================================*/
#include "AEEModGen.h" // Module interface definitions
#include "AEEAppGen.h" // Applet interface definitions
#include "AEEShell.h" // Shell interface definitions
#include "AEEStdLib.h"
#include "cxw.bid"
#include "cxw_res.h"
#include "AEEDisp.h"
#include "AEEGraphics.h"
//#include "AEESound.h"
#include "AEESoundPlayer.h"
#define NUM 3
#define SPEED 5
#define L 17
typedef struct point
{
int x;
int y;
int r;
} Point;
/*-------------------------------------------------------------------
Applet structure. All variables in here are reference via "pMe->" 全部pMe 相当于全局的指向
-------------------------------------------------------------------*/
typedef struct _cxw {
AEEApplet a ; // First element of this structure must be AEEApplet
AEEDeviceInfo DeviceInfo; // always have access to the hardware device information
IDisplay *pIDisplay; // give a standard way to access the Display interface
IShell *pIShell; // give a standard way to access the Shell interface
// add your own variables here...
ISoundPlayer * pISoundPlayer;
IImage * pIImage_color[NUM];
AEECallback cb_timer;
Point ballPoint[NUM];
Point ballSpeed[NUM];
AEESoundPlayerInfo PlayerInfo; //用于指定音频源 它可以通过 ISOUNDPLAYER_SetInfo() 设置 ISoundPlayer 音频源。第二种方法
} cxw;
/*-------------------------------------------------------------------
Function Prototypes 函数说明部分
-------------------------------------------------------------------*/
static boolean cxw_HandleEvent(cxw* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);//事件处理
boolean cxw_InitAppData(cxw* pMe); //初使化
void cxw_FreeAppData(cxw* pMe); //适放
void cxw_drawString(cxw* pMe); //画字符串
void cxw_drawImage(cxw* pMe,IImage * pIImage_color,int x,int y);//画图片
void cxw_drawround(cxw* pMe); //手动画圆
void myKeyEvent(cxw* pMe,uint16 wParam);//事件分解处理函数
static void CBMainTimer(cxw* pMe);//延时后要处理的事
void mainFram(cxw* pMe);// 表示层
void mainLogin(cxw* pMe);//逻辑层
//boolean InterRect(AEERect rect1, AEERect rect2);//碰撞处理
int Interround(Point pa, Point pb);//碰撞处理 勾股定理做的
/*-------------------------------------------------------------------
函数方法体部分
-------------------------------------------------------------------*/
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)//Aee调用的记启动Applet 接口
{
*ppObj = NULL;
if( ClsId == AEECLSID_CXW )
{
// Create the applet and make room for the applet structure
if( AEEApplet_New(sizeof(cxw),
ClsId,
pIShell,
po,
(IApplet**)ppObj,
(AEEHANDLER)cxw_HandleEvent,
(PFNFREEAPPDATA)cxw_FreeAppData) ) // the FreeAppData function is called after sending EVT_APP_STOP to the HandleEvent function
{
//Initialize applet data, this is called before sending EVT_APP_START
// to the HandleEvent function
if(cxw_InitAppData((cxw*)*ppObj))
{
//Data initialized successfully
return(AEE_SUCCESS);
}
else
{
//Release the applet. This will free the memory allocated for the applet when
// AEEApplet_New was called.
IAPPLET_Release((IApplet*)*ppObj);
return EFAILED;
}
} // end AEEApplet_New
}
return(EFAILED);
}
static boolean cxw_HandleEvent(cxw* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
switch (eCode)
{
// App is told it is starting up
case EVT_APP_START:
//cxw_drawString(pMe);
// cxw_drawImage(pMe);
// cxw_drawround(pMe);
ISHELL_SetTimerEx(pMe->pIShell,80,&pMe->cb_timer) ;
return(TRUE);
// App is told it is exiting
case EVT_APP_STOP:
// Add your code here...
return(TRUE);
// App is being suspended
case EVT_APP_SUSPEND:
// Add your code here...
return(TRUE);
// App is being resumed
case EVT_APP_RESUME:
// Add your code here...
return(TRUE);
case EVT_APP_MESSAGE:
return(TRUE);
case EVT_KEY:
// Add your code here...
myKeyEvent(pMe,wParam);
return(TRUE);
default:
break;
}
return FALSE;
}
// this function is called when your application is starting up
boolean cxw_InitAppData(cxw* pMe)
{
pMe->DeviceInfo.wStructSize = sizeof(pMe->DeviceInfo);
ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&pMe->DeviceInfo);
pMe->pIDisplay = pMe->a.m_pIDisplay;
pMe->pIShell = pMe->a.m_pIShell;
// Insert your code here for initializing or allocating resources...
ISHELL_CreateInstance (pMe->pIShell, AEECLSID_SOUNDPLAYER,(void**)&pMe->pISoundPlayer);//-----
pMe->PlayerInfo.dwSize=0;
pMe->PlayerInfo.eInput=SDT_FILE;
pMe->PlayerInfo.pData="aaa.mp3";
ISOUNDPLAYER_SetInfo(pMe->pISoundPlayer,&pMe->PlayerInfo) ;//第二种初使化方法________
// pMe->pISoundPlayer=ISHELL_LoadSound(pMe->pIShell,"aaa.mp3") ;
pMe->pIImage_color[0]=ISHELL_LoadResImage(pMe->pIShell,CXW_RES_FILE,IMAGE_CXW) ;
IIMAGE_SetParm(pMe->pIImage_color[0],IPARM_ROP,AEE_RO_TRANSPARENT,0) ;//光栅操作
pMe->pIImage_color[1]=ISHELL_LoadResImage(pMe->pIShell,CXW_RES_FILE,IMAGE_CXZ) ;
IIMAGE_SetParm(pMe->pIImage_color[1],IPARM_ROP,AEE_RO_TRANSPARENT,0) ;//光栅操作
pMe->pIImage_color[2]=ISHELL_LoadResImage(pMe->pIShell,CXW_RES_FILE,IMAGE_CXH) ;
IIMAGE_SetParm(pMe->pIImage_color[2],IPARM_ROP,AEE_RO_TRANSPARENT,0) ;//光栅操作
// pMe->pIImage_color[3]=ISHELL_LoadResImage(pMe->pIShell,CXW_RES_FILE,IMAGE_CXK) ;
// IIMAGE_SetParm(pMe->pIImage_color[3],IPARM_ROP,AEE_RO_TRANSPARENT,0) ;//光栅操作
// pMe->pIImage_color[4]=ISHELL_LoadResImage(pMe->pIShell,CXW_RES_FILE,IMAGE_CXX) ;
// IIMAGE_SetParm(pMe->pIImage_color[4],IPARM_ROP,AEE_RO_TRANSPARENT,0) ;//光栅操作
pMe->ballPoint[0].r=L;
pMe->ballPoint[0].x=16;
pMe->ballPoint[0].y=16;
pMe->ballSpeed[0].x=-2;
pMe->ballSpeed[0].y=0-SPEED+2;
pMe->ballPoint[1].r=L;
pMe->ballPoint[1].x=68;
pMe->ballPoint[1].y=68;
pMe->ballSpeed[1].x=-3;
pMe->ballSpeed[1].y=0-SPEED+3;
pMe->ballPoint[2].r=L;
pMe->ballPoint[2].x=100;
pMe->ballPoint[2].y=20;
pMe->ballSpeed[2].x=-4;
pMe->ballSpeed[2].y=0-SPEED+4;
// pMe->ballPoint[3].r=12;
// pMe->ballPoint[3].x=30;
// pMe->ballPoint[3].y=100;
// pMe->ballSpeed[3].x=-1;
// pMe->ballSpeed[3].y=0-SPEED+1;
// pMe->ballPoint[4].r=12;
// pMe->ballPoint[4].x=150;
// pMe->ballPoint[4].y=140;
// pMe->ballSpeed[4].x=-1;
// pMe->ballSpeed[4].y=0-SPEED+1;
CALLBACK_Init(&pMe->cb_timer,(PFNNOTIFY)CBMainTimer,(void*)pMe); //初使化cb_timer
return TRUE;
}
// this function is called when your application is exiting
void cxw_FreeAppData(cxw* pMe)//集中适放
{
int i;
for(i=0;i<NUM;i++)
if(pMe->pIImage_color[i]!=NULL)
{
IIMAGE_Release(pMe->pIImage_color[i]);//适放内存
pMe->pIImage_color[i] = NULL;
}
if(pMe->pISoundPlayer!=NULL)
{
ISOUNDPLAYER_Release(pMe->pISoundPlayer); //适放内存
pMe->pISoundPlayer = NULL;
}
}
void cxw_drawString(cxw* pMe,)//画一个字符串
{
AECHAR chText[10];
ISHELL_LoadResString(pMe->pIShell,CXW_RES_FILE,STR_PLAY,chText,10) ;
// IDISPLAY_ClearScreen(pMe->pIDisplay) ;
IDISPLAY_DrawText(pMe->pIDisplay,AEE_FONT_BOLD,chText,-1,0,0,NULL,IDF_ALIGN_CENTER|IDF_ALIGN_MIDDLE) ;
// IDISPLAY_Update(pMe->pIDisplay);
}
void cxw_drawImage(cxw* pMe,IImage * pIImages_color,int x,int y) //从资源文件画图
{
IIMAGE_Draw(pIImages_color,x,y) ;//画图
cxw_drawString(pMe);
}
//void cxw_drawround(cxw* pMe) //画小球
//{
// IGraphics * pIIGraphics;
// AEECircle circle;
//
// ISHELL_CreateInstance(pMe->pIShell,AEECLSID_GRAPHICS,(void**)&pIIGraphics);//此函数用于创建具有指定的 32 位 ClassID 的对象
// circle.cx=32;
// circle.cy=32;
// circle.r=16;
// IGRAPHICS_ClearViewport(pIIGraphics) ;
// IGRAPHICS_DrawCircle(pIIGraphics,&circle) ;
// IGRAPHICS_Update(pIIGraphics);
// if(pIIGraphics!=NULL)
// {
// IIMAGE_Release(pIIGraphics);//适放内存
// pIIGraphics=NULL;
// }
//}
void myKeyEvent(cxw* pMe,uint16 wParam)//事件分解处理
{
switch(wParam)
{
case AVK_SELECT: DBGPRINTF("AVK_SELECT");
ISOUNDPLAYER_Play(pMe->pISoundPlayer);//播 >
break;
case AVK_RIGHT: DBGPRINTF("AVK_RIGHT");
// ISOUNDPLAYER_Rewind(pMe->pISoundPlayer,5000) ;//倒回
break;
case AVK_LEFT: DBGPRINTF("AVK_LEFT");
ISOUNDPLAYER_Stop(pMe->pISoundPlayer);//停止播 []
break;
case AVK_UP: DBGPRINTF("AVK_UP");
ISOUNDPLAYER_Pause(pMe->pISoundPlayer) ;//暂停 ||
break;
case AVK_DOWN: DBGPRINTF("AVK_DOWN");
ISOUNDPLAYER_Resume(pMe->pISoundPlayer) ;//暂停恢复 >
break;
default:DBGPRINTF("default");
break;
}
}
//boolean InterRect(AEERect rect1, AEERect rect2)//碰撞处理
//{
// if(rect1.x > rect2.x+rect2.dx) return FALSE;
// if(rect2.x > rect1.x+rect1.dx) return FALSE;
// if(rect1.y > rect2.y+rect2.dy) return FALSE;
// if(rect2.y > rect1.y+rect1.dy) return FALSE;
// return TRUE;
//}
int Interround(Point pa, Point pb) //碰撞处理,我程希望自格想的
{
// int notxian =2*L*(2*L) ;
int notxian =(pa.r+pb.r)*(pa.r+pb.r);
int gou=pa.x-pb.x ;
int gu= pa.y-pb.y ;
if(gou<0)gou=0-gou ;
if(gu<0)gu=0-gu;
if( notxian >= gou*gou+gu*gu )return 1;
// DBGPRINTF("%d,%d%,%d\t,%d",gou,gu,notxian,gou*gou+gu*gu);
return 0;
}
static void CBMainTimer(cxw* pMe)//定时驱动
{
ISHELL_SetTimerEx(pMe->pIShell,80,&pMe->cb_timer) ;
mainFram(pMe);
mainLogin(pMe);
}
void mainFram(cxw* pMe)//表示层处理
{
int i;
IDISPLAY_ClearScreen(pMe->pIDisplay) ;//清屏
for(i=0;i<NUM;i++)
{
cxw_drawImage(pMe,pMe->pIImage_color[i],pMe->ballPoint[i].x,pMe->ballPoint[i].y); //从资源文件画图
}
IDISPLAY_Update(pMe->pIDisplay);//更新
}
void mainLogin(cxw* pMe) //逻辑层,小球运动
{
int i,j;
for(i=0;i<NUM;i++)
{
pMe->ballPoint[i].x+=pMe->ballSpeed[i].x;
pMe->ballPoint[i].y+=pMe->ballSpeed[i].y;
if(pMe->ballPoint[i].x<=0)
{
pMe->ballPoint[i].x=0;
pMe->ballSpeed[i].x=0-pMe->ballSpeed[i].x;
}
if(pMe->ballPoint[i].x>=pMe->DeviceInfo.cxScreen-35)
{
pMe->ballPoint[i].x=pMe->DeviceInfo.cxScreen-35;
pMe->ballSpeed[i].x=0-pMe->ballSpeed[i].x;
}
if(pMe->ballPoint[i].y<=0)
{
pMe->ballPoint[i].y=0;
pMe->ballSpeed[i].y=0-pMe->ballSpeed[i].y;
}
if(pMe->ballPoint[i].y>=pMe->DeviceInfo.cyScreen-35)
{
pMe->ballPoint[i].y=pMe->DeviceInfo.cyScreen-35;
pMe->ballSpeed[i].y=0-pMe->ballSpeed[i].y;
}
}
for(i=0;i<=NUM-2;i++)
for(j=1;j<=NUM-1;j++)
if(Interround(pMe->ballPoint[i], pMe->ballPoint[j]))
{
int tempx,tempy;
tempx=pMe->ballSpeed[i].x;
tempy=pMe->ballSpeed[i].y;
pMe->ballSpeed[i].x=pMe->ballSpeed[j].x;
pMe->ballSpeed[i].y=pMe->ballSpeed[j].y;
pMe->ballSpeed[j].x=tempx;
pMe->ballSpeed[j].y=tempy;
}
}
//流程图
//效果图 3张
//如何做出 API 关键算法
// if(pMe->ballSpeed[0].y==pMe->ballSpeed[1].y)
/*
L是长量
工程 word文档(里边要有这个文件是怎么做的)。
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -