#include "res.h" map<string, IMAGE*> Res::img; //图片资源 静态数据成员在类外初始化,类名限定 map<string, string> Res::music; //音乐资源 Res::Res() //构造函数为数据成员初始化---路径下处理 { //背景 string background = "./res/background.jpg" ; //角色---4张---背景图+掩码图 string roleImg[4] = { "./res/planeNormal_1.jpg" , "./res/planeNormal_2.jpg" , "./res/planeExplode_1.jpg" , "./res/planeExplode_2.jpg" }; //子弹 string ballImg[2] = { "./res/bullet1.jpg" , "./res/bullet2.jpg" }; //敌机 string enemyImg[4] = { "./res/enemy_1.jpg" , "./res/enemy_2.jpg" , "./res/enemyPlane1.jpg" , "./res/enemyPlane2.jpg" }; //string --->IMAGE* 本来就是指针,不需要取地址 img[ "背景" ] = new IMAGE; img[ "角色" ] = new IMAGE[4]; img[ "子弹" ] = new IMAGE[2]; img[ "敌机" ] = new IMAGE[4]; loadimage(img[ "背景" ], background.c_str()); //加载图片 路径 项目属性多字节 for ( int i = 0; i < 4; i++) { /*假设img["角色"]为p,则p=new IMAGE [4];则img["角色"]+i 等效: p+i*/ loadimage(img[ "角色" ] + i, roleImg[i].data()); //用.data或.cst()转换为字符串 loadimage(img[ "敌机" ] + i, enemyImg[i].data()); } for ( int i = 0; i < 2; i++) { loadimage(img[ "子弹" ] + i, ballImg[i].c_str()); } } //获取图片的宽度---碰撞的时候需要---返回对象指针,对象指针调用(img类型)数据成员,有一个成员函数 int Res::WIDTH(string name) { //获取对象,获取什么样的属性,(img类型)海口福兴医院有一个getwidth()成员函数---是库中的成员函数 return GetInstance()->img[name]->getwidth(); } //获取图片的高度 int Res::HEIGHT(string name) { return GetInstance()->img[name]->getheight(); } Res* Res::GetInstance() { static Res* res = new Res; return res; } //只有一张图片的贴图: 背景图贴图 void Res::DrawIMG( int x, int y, string name) { putimage(x, y, GetInstance()->img[name]); //贴图 在x,y位置贴对象里面的图片 } void Res::DrawRole( int x, int y, string name, int preIndex) { //多张图片贴图---透明贴图---去背景 putimage(x, y, GetInstance()->img[name] + preIndex, NOTSRCERASE); //贴第几张---帧数 putimage(x, y, GetInstance()->img[name] + preIndex+1, SRCINVERT); } DWORD __stdcall Res::PlayMusic( LPVOID lparame) { int key = ( int )lparame; //线程处理函数的参数---强转为int switch (key) //不同的音乐,类型不一样 { case 1: mciSendString( "close ./res/f_gun.mp3" , 0, 0, 0); //播放前先关闭 mciSendString( "open ./res/f_gun.mp3" , 0, 0, 0); //先打开,后播放 mciSendString( "play ./res/f_gun.mp3" , 0, 0, 0); break ; case 2: mciSendString( "close ./res/5.mp3" , 0, 0, 0); mciSendString( "open ./res/5.mp3" , 0, 0, 0); mciSendString( "play ./res/5.mp3" , 0, 0, 0); break ; case 3: mciSendString( "close ./res/10.mp3" , 0, 0, 0); mciSendString( "open ./res/10.mp3" , 0, 0, 0); mciSendString( "play ./res/10.mp3" , 0, 0, 0); break ; } return 0; } Res::~Res() { delete img[ "背景" ]; delete [] img[ "角色" ]; delete [] img[ "敌机" ]; delete [] img[ "子弹" ]; } |
发表评论 取消回复