📄 winmain.cpp
字号:
// DEMO7_13.CPP 8-bit blitting demo
// INCLUDES ///////////////////////////////////////////////
#define INITGUID
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <direct.h>
#include <wchar.h>
#include <fcntl.h>
#include <ddraw.h>
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dmusicf.h>
#include "ddutil.h"
#include "resource.h"
// DEFINES ////////////////////////////////////////////////
#define WINDOW_CLASS_NAME "WINCLASS1"
#define PLANEE_NUM 14
#define FIREE_NUM 19
#define FIRE_NUM 27
#define SCREEN_WIDTH 800 // size of screen
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 8 // bits per pixel
#define PFS 33
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define INIT_DDSD(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
#define MULTI_TO_WIDE( x,y ) MultiByteToWideChar( CP_ACP,MB_PRECOMPOSED, y,-1,x,_MAX_PATH);
#define STAR_NUM 1000//最大的星星数量------------
//-----------------------------------关于DirectSound 的状态----------
#define SOUND_NULL 0
#define SOUND_LOADED 1
#define SOUND_PLAYING 2
#define SOUND_STOPPED 3
//------------------------------------关于DirectMusic 的状态---------
#define MUSIC_NUM 6
#define MIDI_NULL 0 // this midi object is not loaded
#define MIDI_LOADED 1 // this midi object is loaded
#define MIDI_PLAYING 2 // this midi object is loaded and playing
#define MIDI_STOPPED 3 // this midi object is loaded, but stopped
RECT rect;
int MMWIDTH=SCREEN_WIDTH-52; //我机最大到达的X轴坐标-------
int MMHEIGHT=SCREEN_HEIGHT-64; //我机最大到达的Y轴坐标-------
int fire_x_max=SCREEN_WIDTH-15;//我机子弹最大到达的X轴坐标-------
int fire_y_max=SCREEN_HEIGHT-28;//我机子弹最大到达的Y轴坐标-------
int firee_x_max=SCREEN_WIDTH-15;//敌机子弹最大到达的X轴坐标-------
int firee_y_max=SCREEN_HEIGHT-17;//敌机子弹最大到达的Y轴坐标-------
int planee_x_max=SCREEN_WIDTH-58;//敌机最大到达的X轴坐标-------
int planee_y_max=SCREEN_HEIGHT-52;//敌机最大到达的Y轴坐标-------
WORD STAR_TIME;//用来记录最初时间--------------------
int p_e_n,f_e_n,u,n,p,end,xx,yy,dx,dy,t=0,sco,bb=0,fire_stat=0,star_num=200,star_stat=0;
int planee_num,firee_num,fire_num,grade=0,plane_v,fire_v,planee_v,firee_v,over_sco=100;
int my_left=65,my_right=68,my_up=87,my_down=83,my_fire=71;//对方向及发弹键的定义-----
HWND my_hwnd = NULL; // 窗口句柄
HINSTANCE my_hinstance = NULL; // 本程序句柄
LPDIRECTDRAW4 lpdd = NULL; // dd4 object
LPDIRECTDRAWSURFACE4 lpddsprimary = NULL; // dd primary surface
LPDIRECTDRAWSURFACE4 lpddsback = NULL; // dd back surface
LPDIRECTDRAWSURFACE4 lpddsplane = NULL;
LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette
PALETTEENTRY palette[256]; // color palette
PALETTEENTRY save_palette[256]; // used to save palettes
DDSURFACEDESC2 ddsd; // a direct draw surface description struct
DDBLTFX ddbltfx; // used to fill
DDSCAPS2 ddscaps; // a direct draw surface capabilities struct
HRESULT ddrval; // result back from dd calls
DWORD start_clock_count = 0; // used for timing
HDC my_dc=NULL;
IDirectMusicPerformance *dm_perf = NULL; // the directmusic performance manager
IDirectMusicLoader *dm_loader = NULL; // the directmusic loader
char buffer[80]; // general printing buffer
LPDIRECTSOUND lpds; // directsound interface pointer
DSBUFFERDESC dsbd; // directsound description
DSCAPS dscaps; // directsound caps
HRESULT dsresult; // general directsound result
DSBCAPS dsbcaps; // directsound buffer caps
LPDIRECTSOUNDBUFFER lpdsbprimary, // you won't need this normally
lpdsbsecondary; // the sound buffers
WAVEFORMATEX pcmwf; // generic waveformat structure
// FUNCTIONS ////////////////////////////////////////////////
//用来记录WAV文件的结构
typedef struct pcm_sound_typ
{
LPDIRECTSOUNDBUFFER dsbuffer; // the ds buffer containing the sound
int state; // state of the sound
int rate; // playback rate
int size; // size of sound
int id; // id number of the sound
} pcm_sound, *pcm_sound_ptr;
// 用来记录MIDI文件的结构
typedef struct DMUSIC_MIDI_TYP
{
IDirectMusicSegment *dm_segment; // the directmusic segment
IDirectMusicSegmentState *dm_segstate; // the state of the segment
int id; // the id of this segment
int state; // state of midi song
} DMUSIC_MIDI, *DMUSIC_MIDI_PTR;
//----------------对背景星星的定义 --------------
typedef struct strat_type
{int x;//X轴坐标
int y;//Y轴坐标
int v; //星星的速度----
COLORREF c;//顔色
}STAR,* LPSTAR;
//----------------对我机的定义--------
typedef struct plane_type
{int x;
int y;
int num;//我机的子弹的数量------
int stat;//0为中间,1为向左,2为向右----------
}PLANE,* LPPLANE;
//-----------------对我机子弹的定义--------------
typedef struct fire_type
{int x;
int y;
int xv;//速度---------
int stat;//1为子弹存在,0为子弹不存在
}FIRE,* LPFIRE;
//_-----------------对敌机的定义----------------
typedef struct planeE_tyep
{int x;
int y;
int stat;//0为活着--1为被打中正爆炸-__-1为死掉
}PLANEE,* LPPLANEE;
//-------------------对敌机子弹的定义--------------
typedef struct firee_type
{int x;
int y;
int xv;//X轴方向的速度
int yv;//Y轴方向的速度
int stat;//1为子弹存在,0为子弹不存在
}FIREE,* LPFIREE;
PLANE plane;//定义我机
FIRE fire[FIRE_NUM];//定义我机子弹
STAR star[STAR_NUM];//定义背景星星
PLANEE planee[PLANEE_NUM];//定义敌机
FIREE firee[FIREE_NUM];//定义敌机子弹
pcm_sound fire_s[FIRE_NUM]; //我机子弹的声音-----
//pcm_sound firee_s[FIREE_NUM]; //敌机子弹的声音-----
pcm_sound planee_s[PLANEE_NUM]; //敌机爆炸的声音-----没有找到合适的声音,所以本游戏中不用
pcm_sound plane_s; //我机爆炸的声音-----没有找到合适的声音,所以本游戏中不用
pcm_sound gameover_s; //游戏结束的声音-----
pcm_sound gamepast_s;//过关的声音------
pcm_sound gamepause_s;//游戏暂停的声音---
pcm_sound bb_s[10];//菜单的声音-----
DMUSIC_MIDI dmusic[MUSIC_NUM];//背景音乐----MIDI音乐-----
//------------------------------------------------------------此函数用来把一个pcm_sound结构的Dsbuffer指向一个声音-----------------
//------------------------------------------------------------具体原理我也不太懂,是别人的一个库函数(经过我的一点加工)--------
int DSound_Replicate_Sound(pcm_sound * sound_fx,int id)
{
if (sound_fx[id].state==SOUND_NULL)
{
// first make an identical copy
sound_fx[id] = sound_fx[0];
// now actually replicate the directsound buffer
if (FAILED(lpds->DuplicateSoundBuffer(sound_fx[0].dsbuffer,&sound_fx[id].dsbuffer)))
{
// reset sound to NULL
sound_fx[id].dsbuffer = NULL;
sound_fx[id].state = SOUND_NULL;
return(-1);
} // end if
sound_fx[id].id = id;
return(0);
} // end if found
return(-1);
}
//---------------------------------------------------- end DSound_Replicate_Sound--------------------
//--------------------------------------------------此函数用来把加载一个WAV声音------------------
//--------------------------------------------------经过加工-------------------------------------
int DSound_Load_WAV(pcm_sound * sound_fx,char *filename)
{
int control_flags=DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME;
HMMIO hwav; // handle to wave file
MMCKINFO parent, // parent chunk
child; // child chunk
WAVEFORMATEX wfmtx; // wave format structure
UCHAR *snd_buffer, // temporary sound buffer to hold voc data
*audio_ptr_1=NULL, // data ptr to first write buffer
*audio_ptr_2=NULL; // data ptr to second write buffer
DWORD audio_length_1=0, // length of first write buffer
audio_length_2=0; // length of second write buffer
parent.ckid = (FOURCC)0;
parent.cksize = 0;
parent.fccType = (FOURCC)0;
parent.dwDataOffset = 0;
parent.dwFlags = 0;
// copy data
child = parent;
// open the WAV file
if ((hwav = mmioOpen(filename, NULL, MMIO_READ | MMIO_ALLOCBUF))==NULL)
return(0);
// descend into the RIFF
parent.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioDescend(hwav, &parent, NULL, MMIO_FINDRIFF))
{
// close the file
mmioClose(hwav, 0);
// return error, no wave section
return(0);
} // end if
// descend to the WAVEfmt
child.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hwav, &child, &parent, 0))
{
// close the file
mmioClose(hwav, 0);
// return error, no format section
return(0 );
} // end if
// now read the wave format information from file
if (mmioRead(hwav, (char *)&wfmtx, sizeof(wfmtx)) != sizeof(wfmtx))
{
// close file
mmioClose(hwav, 0);
// return error, no wave format data
return(0);
} // end if
// make sure that the data format is PCM
if (wfmtx.wFormatTag != WAVE_FORMAT_PCM)
{
// close the file
mmioClose(hwav, 0);
// return error, not the right data format
return(0);
} // end if
// now ascend up one level, so we can access data chunk
if (mmioAscend(hwav, &child, 0))
{
// close file
mmioClose(hwav, 0);
// return error, couldn't ascend
return(0);
} // end if
// descend to the data chunk
child.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioDescend(hwav, &child, &parent, MMIO_FINDCHUNK))
{
// close file
mmioClose(hwav, 0);
// return error, no data
return(0);
} // end if
// finally!!!! now all we have to do is read the data in and
// set up the directsound buffer
// allocate the memory to load sound data
snd_buffer = (UCHAR *)malloc(child.cksize);
// read the wave data
mmioRead(hwav, (char *)snd_buffer, child.cksize);
// close the file
mmioClose(hwav, 0);
// set rate and size in data structure
(*sound_fx).rate = wfmtx.nSamplesPerSec;
(*sound_fx).size = child.cksize;
(*sound_fx).state = SOUND_LOADED;
// set up the format data structure
memset(&pcmwf, 0, sizeof(WAVEFORMATEX));
pcmwf.wFormatTag = WAVE_FORMAT_PCM; // pulse code modulation
pcmwf.nChannels = 1; // mono
pcmwf.nSamplesPerSec = 11025; // always this rate
pcmwf.nBlockAlign = 1;
pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
pcmwf.wBitsPerSample = 8;
pcmwf.cbSize = 0;
// prepare to create sounds buffer
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = control_flags | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE;
dsbd.dwBufferBytes = child.cksize;
dsbd.lpwfxFormat = &pcmwf;
// create the sound buffer
if (FAILED(lpds->CreateSoundBuffer(&dsbd,&(*sound_fx).dsbuffer,NULL)))
{
// release memory
free(snd_buffer);
// return error
return(0);
} // end if
// copy data into sound buffer
if (FAILED((*sound_fx).dsbuffer->Lock(0,
child.cksize,
(void **) &audio_ptr_1,
&audio_length_1,
(void **)&audio_ptr_2,
&audio_length_2,
DSBLOCK_FROMWRITECURSOR)))
return(0);
// copy first section of circular buffer
memcpy(audio_ptr_1, snd_buffer, audio_length_1);
// copy last section of circular buffer
memcpy(audio_ptr_2, (snd_buffer+audio_length_1),audio_length_2);
// unlock the buffer
if (FAILED((*sound_fx).dsbuffer->Unlock(audio_ptr_1,
audio_length_1,
audio_ptr_2,
audio_length_2)))
return(0);
// release the temp buffer
free(snd_buffer);
// return id
return(1);
}
//-------------------------------------------------- end DSound_Load_WAV-------------------------
//--------------------------------------------------此函数用来初始化DirectMusic--------------------
//---------------------------------------------------经过加工------------------------------------------
int DMusic_Init(void)
{
int index;
if (FAILED(CoInitialize(NULL)))
{
return(0);
} // end if
if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicPerformance,
(void**)&dm_perf)))
{
// return null
return(0);
} // end if
if (FAILED(dm_perf->Init(NULL, lpds, my_hwnd)))
{
return(0);// Failure -- performance not initialized
} // end if
// add the port to the performance
if (FAILED(dm_perf->AddPort(NULL)))
{
return(0);// Failure -- port not initialized
} // end if
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -