|
我写了一个2D RPG小程序,里面用ID3DXSprite显示人物图片。用户输入上下左右移动人物。
可是,随着上下左右使用次数的增加,我的程序越来越卡。 按一下上,要过1-2秒之后,才有反应。
我自己觉得是消息堆积了。但是,又不知道如何处理。 下面附上我的主要代码(我这么写 哪里有问题啊):
- while(msg.message != WM_QUIT){
- if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)){
- TranslateMessage(&msg);
- DispatchMessage(&msg); }
- else if(active) { MainLoop(); }
- else { WaitMessage(); } }
复制代码
- MainLoop(){
- end_time = GetTickCount();
- if (end_time > start_time + 30) {
- start_time = end_time;
- lpKeyboard->GetDeviceState( 256, (LPVOID)KeyboardState);
- if (KeyboardState[DIK_UPARROW] & 0x80){
- b -= float(10); if (rect.top != 192) temp = 0; rect.top = 192; rect.bottom = 256; rect.left = temp%4 * 32;
- rect.right = rect.left + 32; temp++; }
- if (KeyboardState[DIK_DOWNARROW] & 0x80){ b +=float(10); if (rect.top != 0) temp = 0; rect.top = 0;
- rect.bottom = 64; rect.left = temp%4 * 32; rect.right = rect.left + 32; temp++; }
- if (KeyboardState[DIK_LEFTARROW] & 0x80){ a -= float(10); if (rect.top != 64) temp = 0; rect.top = 64;
- rect.bottom = 128; rect.left = temp%4 * 32; rect.right = rect.left + 32; temp++; }
- if (KeyboardState[DIK_RIGHTARROW] & 0x80){ a += float(10); if (rect.top != 128) temp = 0; rect.top = 128;
- rect.bottom = 192; rect.left = temp%4 * 32; rect.right = rect.left + 32; temp++; } }
- RenderScene();
- }
复制代码
- void RenderRole(){
- pSprite->Begin(D3DXSPRITE_ALPHABLEND);
- D3DXCreateTextureFromFile( g_D3DDevice, L"Tim.png", &tim);
- pSprite->Draw( tim, &rect, NULL, &D3DXVECTOR3( a, b, 0.0f), 0xffffffff);
- pSprite->End(); }
- void RenderScene(){ g_D3DDevice->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
- g_D3DDevice->BeginScene(); D3DXCreateTextureFromFile(g_D3DDevice, L"4.jpg", &tim);
- pSprite->Begin(D3DXSPRITE_ALPHABLEND); pSprite->Draw( tim, NULL, NULL, NULL, 0xffffffff);
- RenderRole();
- pSprite->End();
- g_D3DDevice->EndScene();
- g_D3DDevice->Present(NULL,NULL,NULL,NULL); }
复制代码
希望大家帮帮我,困惑好几天了。也不是没看别人的代码,他们好像没用ID3DXSprite,用的DDraw和API。其他真不知道哪里有问题了。
|
|