• Advertisement
Sign in to follow this  

Am i scrolling it correctly?

This topic is 1318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.
Im trying to make a Windows-app, intended for moving desktop background (based on following code: http://www.pouet.net/topic.php?which=7440&page=2#c340591). It makes ListBox draw icons into memory buffer and after combines result with own background image. To scroll it i redraw all that with defined frequency, but it takes alot of CPU time and i feel that im doing smth in a very inefficient way.
 
Please, help me optimize my code. Here it is: 
d3ddev->BeginScene();
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, 0xff00ff00, 0.0f, 0);

// background
d3ddev->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1);
d3ddev->SetTexture(0, pic);
d3ddev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
d3ddev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
d3ddev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
color = D3DCOLOR_ARGB(255, 255, 255, 255);
spr->Begin(D3DXSPRITE_ALPHABLEND);
spr->Draw(pic, NULL, NULL, &position, color);
spr->End();

// desktop icons
d3ddev->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1);
d3ddev->SetTexture(0, tex);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3ddev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, fgv, sizeof(FgVertex));

d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
ValidateRgn(hwnd, NULL);
It runs each time i need to move background on 1 pixel. 
 
- Maybe its better to move viewport instead of redrawing sprite?
- Can u advice what to do and what to read? Its my first attempt to use DirectX.
- Maybe some code examples?
 
Thanks. Edited by inflate

Share this post


Link to post
Share on other sites
Advertisement

Does your application go idle when not in motion? Or is it always moving and using up CPU. If the background goes idle and uses little to no resources most of the time I wouldn't worry about optimizing it. If it is always using up a good piece of the CPU then I would worry, in which case, I would be sure that when you aren't interacting with the background that the application goes idle.

Share this post


Link to post
Share on other sites

Does your application go idle when not in motion? Or is it always moving and using up CPU. If the background goes idle and uses little to no resources most of the time I wouldn't worry about optimizing it. If it is always using up a good piece of the CPU then I would worry, in which case, I would be sure that when you aren't interacting with the background that the application goes idle.

Yes, it does:
// scroller thread
DWORD WINAPI Scroller(LPVOID param) { 
      for (int i=0; i<3077; i++) {
         position.y -= 0.325;
         InvalidateRect(listView, NULL, TRUE); // cause desktop ListView component to repaint
         Sleep(300); // <---
      }
   return 0;
} 
After InvalidateRect() is called -- my own ListView's WndProc() takes control, then i call original WndProc() to get icons drawn and, at last, my code from post above is being executed. Don't bring alot of attention to hardcoded values and Sleep(), my plan is to replace that code with delta calculation and timer. That is just for testing. Edited by inflate

Share this post


Link to post
Share on other sites

You probably want your code to sleep until there is user input.

GetMessage

 

Of course, when you do that you wont be able to play any animations. For the times you need animation you could use SetTimer to have a regular frame rate. I'm no expert on windows programming, but for something always running in the background it is best to only respond to user input instead of constantly running.

Share this post


Link to post
Share on other sites

for something always running in the background it is best to only respond to user input instead of constantly running.

As i know, there should be no significant performance diff. between, for example, Sleep(300) and timer with 300ms interval. In both cases my code will receive control with ~300ms interval.
As for DirectX-related code, its nothing to improve too? I thought about drawing an entire sprite and moving viewport instead of redrawing it on each iteration. Is it possible? Edited by inflate

Share this post


Link to post
Share on other sites
I finally found a reason for high CPU load: unnecessary calls to original WndProc(), even when i need just to scroll, not to redraw icons.
If to redraw only when system WM_PAINT message received -- scroller gets 1-3% of CPU time, which is ok.

HappyCoder, thanks for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement