Jump to content
  • Advertisement
Sign in to follow this  
n3Xus

[C++] GDI-How to draw a full screen of pixels very fast

This topic is 3665 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

what I'm trying to do is to draw a entire screen (or a window) with pixels with GDI. so far I used two for-loops (one for screen width and one for screen height) and than a SetPixel(hdc,i,j,COLOR) to draw the pixels. but this takes a long time to draw (and I also get a strange problem: it does not draw on the entire window; there is a rectangle in the middle that doesn't get its pixel set) and I want to know what is the fastest way to draw a pixel by pixel in GDI (so it gets updated at least 60 times per second)? this is the rect that doesn't get overwritten (the black parts are those which I draw using SetPixel) Shrani.si

Share this post


Link to post
Share on other sites
Advertisement
The best way to improve performance is to only draw what needs to be redrawn. If you are filling the entire window with a single color consider using FillRect() rather than SetPixel() as a single function call is far better than several hundred thousand.

Share this post


Link to post
Share on other sites
The best way is to draw to a memory bitmap then blit that to the target window (which should be fast). I remember getting frame rates above 400 fps using this technique (was doing basic software rendering). Implementing that is a bit tricky, but I have implemented it in a class that is very easy to use. I can post it here with a demo of how to use it if you want (though you'd have to wait until I get home).

Share this post


Link to post
Share on other sites
hikikomori-san I'm actually also learning this because I want to dive into software rendering :D so when you get home can you please post that demo?

Share this post


Link to post
Share on other sites
ok thx. I got it to work now.

now I got some code that generates noise but it is very slow. is this because I use the memset() every loop? since I'm making a simple software renderer, should I copy the entire buffer that i will get with my renderer to this bitmap with only one memset() call?

here is the code to generate noise (this gets called every frame):

for(int i=0;i<1024*768*3;i++)
{
icounter=rand()%255;
memset(&(pBuffer),icounter,1);
}

hdc=GetDC(hWnd);

HDC hdcmem=CreateCompatibleDC(hdc);
SelectObject(hdcmem,bitmap2);
BitBlt(hdc,0,0,1024,768,hdcmem,0,0,SRCCOPY);

ReleaseDC(hWnd,hdc);

DeleteDC(hdcmem);
DeleteObject(bitmap);

Share this post


Link to post
Share on other sites
You're creating a new DC every frame, that's going to be extremely slow. You're also leaking memory by not storing the return value of SelectObject() and applying it again.
Also, you shouldn't be using GetDC(). Instead, use UpdateWindow() to tell Windows to issue a WM_PAINT message to your window, and then use the HDC from BeginPaint() inside your WM_PAINT handler.

That makes your per-frame code:

for(int i=0;i<1024*768*3;i++)
{
pBuffer=rand()&0xff;
}
UpdateWindow(...);
hdc=GetDC(hWnd);

// Insize WM_PAINT:

HDC hdc=BeginPaint(...)
BitBlt(hdc,0,0,1024,768,hdcmem,0,0,SRCCOPY);
EndPaint(...);



Where "..." obviously means "fill in your params here".

EDIT: Also, there's no point in that memcpy(), and you may as well make the modulo into a bit mask (Although the compiler should do that for you anyway).

Share this post


Link to post
Share on other sites
MAJOR EDIT:

so, I can display the image but it doesn't get updated (it has the same value as it was given to it in the for-loop right after the CreateDIBSection() call

it goes totally white right after the 1st frame if i call InvalidateRect() (if I don't call InvalidateRect() nothing happens, the image doesn't changes at all). if I use UpdateWindow() the same image that was created after the CreateDIBSection() is there and it doesn't change

code:

// Setup render objects
void W32WindowWrapper::SetupRender()
{
// Setup buffer
bmi.bmiHeader.biBitCount=24;
bmi.bmiHeader.biClrImportant=0;
bmi.bmiHeader.biClrUsed=0;
bmi.bmiHeader.biCompression=BI_RGB;
bmi.bmiHeader.biHeight=768;
bmi.bmiHeader.biWidth=1024;
bmi.bmiHeader.biPlanes=1;
bmi.bmiHeader.biSize=sizeof(BITMAPINFO);
bmi.bmiHeader.biSizeImage=bmi.bmiHeader.biWidth*bmi.bmiHeader.biHeight*bmi.bmiHeader.biBitCount/8;
bmi.bmiHeader.biXPelsPerMeter=0;
bmi.bmiHeader.biYPelsPerMeter=0;

hdcmem=CreateCompatibleDC(m_HDC);
gdiobject=SelectObject(hdcmem,buffer);

// DIB
buffer=CreateDIBSection(hdcmem,&bmi,DIB_RGB_COLORS,(void**)&bufferbyte,NULL,0);
if(buffer==NULL)
{
MessageBoxA(NULL,"bitmap2=NULL","ERROR",MB_OK);
}

// Loop through all pixels and their RGB components
for(int i=0;i&lt;1024*768*3;i++)
{
bufferbyte=rand()%255;
}
}
// Render
void W32WindowWrapper::Render()
{
// Loop through all pixels and their RGB components
for(int i=0;i&lt;1024*768*3;i++)
{
bufferbyte=rand()%255;
}

InvalidateRect(m_hWnd,NULL,true);
}

//------------------------------------------------------------------
case WM_PAINT:

m_HDC=BeginPaint(m_hWnd,&m_ps);


hdcmem=CreateCompatibleDC(m_HDC);

SelectObject(hdcmem,buffer);
BitBlt(m_HDC,0,0,1024,768,hdcmem,0,0,SRCCOPY);


DeleteDC(m_HDC);
DeleteObject(buffer);

EndPaint(m_hWnd,&m_ps);

break;




[Edited by - n3Xus on July 30, 2008 4:15:12 PM]

Share this post


Link to post
Share on other sites
if I put
buffer=CreateDIBSection(hdcmem,&bmi,DIB_RGB_COLORS,(void**)&bufferbyte,NULL,0);
to the Render function (so it gets run every frame) than the new image is drawn (very slowly that is)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!