Stars

Started by
14 comments, last by qadir 20 years, 9 months ago
HDC = GetDC(hWnd);
HDC hOffscreenDC = CreateCompatibleDC(hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC,640,480);
HGDIOBJ OldBmp = SelectObject(hOffscreenDC,hBmp);

// draw your stars
SetPixelV(hOffscreenDC,0,0,RGB(255,255,255));
BitBlt(hDC,0,0,640,480,hOffscreenDC,0,0,SRCCOPY)

// clean up
SelectObject(hOffscreenDC,OldBmp);
DeleteObject(hBmp);
DeleteDC(hOffscreenDC);
ReleaseDC(hDC);

this is just out of my head, but i think it should work.

My Site
Advertisement
I tried your code, it didn''t work exactly how I planned it, but it has given me an idea as to be able to get the effect without the use of blitting. Thx

Also I have updated the code in DirectX, it uses double buffering instead of the crude blank screen and replot pixels that I had used earlier. So the effect is a lot smoother
I''m still working on using page flipping which should be able to make the movement very smooth.

The link to the old code should now have the improved code

All Sprites are created equal
All Sprites are created equal
Well , as suggested by Invader X, I used InvalidateRect and managed to come up with working code written in GDI. However the screen seems to be flickering since the code is similar to my old direct X code. i.e Blank the screen using InvalidateRect() and replot the pixels.

The code is in GDI and is much shorter than the one in DirectX.

If someone could please go over the code and suggest optimizations
I have uploaded the code at

www.geocities.com/abdul_stud/starfield.txt

All Sprites are created equal
All Sprites are created equal
If you don''t want flickering, you have to draw to a backbuffer (ie offscreen dc), and blit that backbuffer to the screen.

This time I have checked the code, and it works fine, with no flickering.

HDC hDC = GetDC(hWnd);
HDC hOffscreenDC = CreateCompatibleDC(hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC,640,480);
HGDIOBJ OldBmp = SelectObject(hOffscreenDC,hBmp);

// draw your stars
RECT rect;
SetRect(&rect,0,0,640,480);
FillRect(hOffscreenDC,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));

SetPixelV(hOffscreenDC,50,80,RGB(255,255,255));

BitBlt(hDC,0,0,640,480,hOffscreenDC,0,0,SRCCOPY);

// clean up
SelectObject(hOffscreenDC,OldBmp);
DeleteObject(hBmp);
DeleteDC(hOffscreenDC);
ReleaseDC(hWnd,hDC);

I won''t use invalidate rect, since it clears the screen. If you blit the offscreen dc over the existing image, you don''t need to clear the screen, because every pixel will be redrawn anyway.

My Site
I meant InvalidateRect() to be used in conjunction with WM_PAINT (which, by the example code posted here, is not being used). InvalidateRect() does not clean the screen at all (unless the second parameter is TRUE) but instead only updates the area that needs to be redrawn. It sends a WM_PAINT message so that you can erase and draw.


Qui fut tout, et qui ne fut rien
Invader''s Realm
Thanks a lot quasar3d, the code works perfectly.

InvaderX, I guess I misunderstood what you meant to say, thanks anyway.

I have put up the corrected code, in case anyone is interested.

Thanks to everyone who helped

All Sprites are created equal
All Sprites are created equal

This topic is closed to new replies.

Advertisement