我最近做个2D的射击类小游戏,
采用的是观察者的设计模式.
1个绘图线程,
1个计算线程,
当我操纵射击或者来会跑动,
瞄准的时候,这个单位出现残像
有的时候却没有.怎么回事啊,那位大侠做过,给个
明示吧.想的头发都只剩下3根了...
采用的是观察者的设计模式.
1个绘图线程,
1个计算线程,
当我操纵射击或者来会跑动,
瞄准的时候,这个单位出现残像
有的时候却没有.怎么回事啊,那位大侠做过,给个
明示吧.想的头发都只剩下3根了...
delphiX 还是 Asphype啊
就是GDI+多线程
这一步走好了在弄dx或者openGL什么的..
结构体
..
posPre:Tpoint;//原位置
posNow:Tpoint;//当前位置
..计算线程每隔10MS计算下单元的位置,然后把这个位置
写到posNow,将原当前位置移动到pospre;绘图线程
在当前位置bitblt上单元图象,在posPre上贴上背景图.现象如下:
开始游戏后不管,那么游戏一直正常运行,
使用键盘控制单元射击,一段时间后出现残像,
使用鼠标点击From,也会出现.我用的就是TFrom,override了wndproc,提升了进程优先级为HIGH_PRIORITY_CLASS
写到posNow,将原当前位置移动到pospre; 完成这个过程之后,必须马上重绘,才能进行下一次计算。画面不花才怪。线程同步没有做好。
正在+声音,
delphi的DxSound资料不多,
混音还没有过去....
你的问题只有看过Demo才能继续讨论下去。
{
LPDIRECTSOUND pDS;
HRESULT result;
int ErrorCode=0;
//create Sound OBject
result=DirectSoundCreate(NULL,&pDS,NULL);
if (result!=DS_OK)
{
ErrorCode= -1;
goto GameOver;
};
//set RativeLevel
result=pDS->SetCooperativeLevel(hd,DSSCL_PRIORITY);
if (result!=DS_OK)
{
ErrorCode= -2;
goto GameOver;
};
//set First buffer
LPDIRECTSOUNDBUFFER pPBuf;
DSBUFFERDESC desc;
memset(&desc,0,sizeof(desc));
desc.dwSize=sizeof(desc);
desc.dwFlags=DSBCAPS_PRIMARYBUFFER;
desc.dwBufferBytes=0;
desc.lpwfxFormat=NULL;
result=pDS->CreateSoundBuffer(&desc,&pPBuf,NULL);
if (result!=DS_OK)
{
ErrorCode= -3;
goto GameOver;
}
//set file Format=*.wav
WAVEFORMATEX pwfmt;
memset(&pwfmt,0,sizeof(pwfmt));
pwfmt.wFormatTag =WAVE_FORMAT_PCM;
pwfmt.nChannels=2;
pwfmt.nSamplesPerSec=44100;
pwfmt.wBitsPerSample=16;
pwfmt.nBlockAlign=pwfmt.wBitsPerSample/8*pwfmt.nChannels;
pwfmt.nAvgBytesPerSec=pwfmt.nSamplesPerSec*pwfmt.nBlockAlign;
result=pPBuf->SetFormat(&pwfmt);
if (result!=DS_OK)
{
ErrorCode= -4;
goto GameOver;
}
//load file
WAVEFORMATEX swfmt;
MMCKINFO ckRiff;
MMCKINFO ckInfo;
MMRESULT mmresult;
HMMIO hmmio;
DWORD size;
// file is not find
hmmio=mmioOpen((char*)Filename,NULL,MMIO_ALLOCBUF|MMIO_READ);
if (hmmio==NULL)
{
ErrorCode= -11;
goto GameOver;
}
//file Format is Wrong
ckRiff.fccType=mmioFOURCC('W','A','V','E');
mmresult=mmioDescend(hmmio,&ckRiff,NULL,MMIO_FINDRIFF);
if (mmresult!=MMSYSERR_NOERROR)
{
ErrorCode= -6;
goto GameOver;
}
// Format Error
ckInfo.ckid=mmioFOURCC('f','m','t',' ');
mmresult=mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
if (mmresult!=MMSYSERR_NOERROR)
{
ErrorCode= -7;
goto GameOver;
}
//load Foramt lost
mmresult=mmioRead(hmmio,(HPSTR)&swfmt,sizeof(swfmt));
if (mmresult==-1)
{
ErrorCode= -8;
goto GameOver;
}
//load Format lost
mmresult=mmioAscend(hmmio,&ckInfo,0);
ckInfo.ckid=mmioFOURCC('d','a','t','a');
mmresult=mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
if (mmresult!=MMSYSERR_NOERROR)
{
ErrorCode= -9;
goto GameOver;
}
size=ckInfo.cksize; //create second Buffer
//LPDIRECTSOUNDBUFFER pSBuf;
memset(&desc,0,sizeof(desc));
desc.dwSize=sizeof(desc);
desc.dwBufferBytes=size;
desc.lpwfxFormat=&swfmt;
desc.dwFlags=DSBCAPS_STATIC | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME |DSBCAPS_GLOBALFOCUS;
result=pDS->CreateSoundBuffer(&desc,(LPDIRECTSOUNDBUFFER *)(&pSBuf),NULL);
if (result!=DS_OK)
{
ErrorCode= -10;
goto GameOver;
}
//load wav and lock it
LPVOID pAudio;
DWORD bytesAudio;
result=LPDIRECTSOUNDBUFFER(pSBuf)->Lock(0,size,&pAudio,&bytesAudio,NULL,NULL,NULL);
if (result!=DS_OK)
{
ErrorCode= -10;
goto GameOver;
}
//this read sound
mmresult=mmioRead(hmmio,(HPSTR)pAudio,bytesAudio);
if (mmresult==-1)
{
ErrorCode= -12;
goto GameOver;
}
//unlock it
result=LPDIRECTSOUNDBUFFER(pSBuf)->Unlock(pAudio,bytesAudio,NULL,NULL);
if (mmresult==-1)
{
ErrorCode= -13;
goto GameOver;
}
GameOver:
if (hmmio!=NULL) {mmioClose(hmmio,0);}
if (pPBuf!=NULL) {pPBuf->Release;}
if (pDS!=NULL) {pDS->Release;}
return ErrorCode;
}
//=================================
extern "C" __declspec(dllexport) int _stdcall PlayWave(void *p,DWORD playMark)
{
LPDIRECTSOUNDBUFFER(p)->Play(0,0,playMark);
return 0;
}
//=================================extern "C" __declspec(dllexport) int _stdcall releaseLPDIRECTSOUNDBUFFER(void *p)
{
LPDIRECTSOUNDBUFFER(p)->Release;
return 0;
}
//================================
type Typ=function(hd:Thandle;Filename:LPCSTR;var pSBuf:pointer):integer;stdcall;
type Tpl=function(p:pointer;playMark:DWORD):integer;stdcall;
//例子:
var
p:pointer;
ru:integer;
fn:LPCSTR;
begin
hwnd:=LoadLibrary('soundDll5.dll');
if hwnd>0 then
begin
fn:='102.wav';
a1:=GetProcAddress(Hwnd,'_WaveOutBuffer@12');//这个函数名称是编译后的
yp:=Typ(a1);
ru:=yp(self.Handle,fn,p);//无类型指针P 被赋值为DxSound建立的声音缓冲
a2:=getprocAddress(hwnd,'_PlayWave@8');
pl:=Tpl(a2);
ru:=pl(p,1);//播放背景音乐
end;
//freeLibrary(hwnd);//这里不要立即释放,子弹声音还需要用
end;
你如果不是商业用的软件,可以去使用FMOD库,它对Delphi支持的很好。
也可以使用OpenAL,不过需要自己改写头文件,但是比使用DSound方便多了。