Drawing pixels on a window?

This is a discussion on Drawing pixels on a window? within the Windows Programming forums, part of the Platform Specific Boards category; hi! i want to make a software-renderer, like the one i did before in an other programming language. this time, ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    Drawing pixels on a window?

    hi!
    i want to make a software-renderer, like the one i did before in an other programming language. this time, i'll do it in C++. the problem is: how do i draw pixels on the window (FAST) ?
    something like a pointer to the screen buffer would be nice
    thanks for helping.

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
    {
    	WNDCLASS wc;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = TEXT("MainWindow");
    	wc.lpszMenuName = 0;
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    	RegisterClass(&wc);
    
    	HWND hwnd = CreateWindow(
    		TEXT("MainWindow"),
    		TEXT("Test"),
    		WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
    		100, 100,
    		640, 480,
    		NULL, NULL,
    		hInstance, NULL
    	);
    	ShowWindow(hwnd, iCmdShow);
    
    	MSG msg;
    	while (GetMessage(&msg, NULL, 0, 0 ))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
    {
    	switch (message)
    	{
    		case WM_PAINT:
    			HDC hdc;
    			PAINTSTRUCT ps;
    			hdc = BeginPaint(hwnd, &ps);
    			for (int y = 0; y < 480; y++)
    			{
    				for (int x = 0; x < 640; x++)
    				{
    					SetPixel(hdc, x, y, 0xffffff);
    				}
    			}
    			EndPaint(hwnd, &ps);
    			break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    	}
    	return DefWindowProc(hwnd, message, wparam, lparam);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The simple solution for "pointer to the frame buffer" is to do double-buffered drawing, where you use the BitBlt function to copy your pixels to the actual screen. So, create yourself a piece of memory big enough to hold all your pixels, then draw to your hearts content with a pointer to that memory, and when you are done, use BitBlt() to copy it in the "WM_PAINT" section.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    sounds like what i needed. i have an array with a pointer now, and how exactly do i handle the BitBlt() function now?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let Me Google that for you.

    You want the second hit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    lol. i already discovered this function, but there is no argument where i can give my pointer to the screen buffer array??

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you need to CreatCompatibleBitmap(), and then use SetDIBits(), using the address of your pixels, and then use the handle from CreateCompatibleBitmap() to do the actual BitBlt() operation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    do you have any example for that?
    forgive my dumbness

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Devils Child View Post
    do you have any example for that?
    forgive my dumbness
    No, I'm just reading the MSDN web-site and some basic understanding of GDI in general (I did two years of writing Windows graphics device drivers).

    This seems like it's in the right general direction: http://www.winprog.org/tutorial/bitmaps.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    and how do i access the memory?
    although, i thought you'd use a surface instead of a 'bitmap'?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Is it me or is setting everything pixel per pixel still going to be excruciatingly slow? I think best is to avoid that while you can. And with what you're doing, drawing a square, I bet it's possible without setting it pixel per pixel. Although I've never done windows api coding...

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    fine. programming a software renderer requires setting everything pixel-per-pixel.
    and believe me: it's not slow!

    so how do i do it now?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Using SetDIBits, you can set a Bitmap to point to memory that you have allocated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    alright. got it now working
    thx a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 07:57 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 09:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21