Pixel Trouble

This is a discussion on Pixel Trouble within the Windows Programming forums, part of the Platform Specific Boards category; Hi all. I'm currently writing an implementation of Conways game of life. I've written the engine code (at least a ...

  1. #1
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192

    Pixel Trouble

    Hi all.

    I'm currently writing an implementation of Conways game of life. I've written the engine code (at least a first draft of it) and now I'm learning windows so I can test the engine.

    At the moment I have a program to draw animated random pixels on the screen using SetPixel. (Once I understand this then I'll plug the life engine into it.) This works o.k. - But sometimes it draws GREEN pixels (correct) and sometimes it draws GREY pixels (wrong). I've tried changing the code to make white pixels - but they still turn grey.

    When I move the window around the screen, the colour changes seemingly at random. I can't understand this at all.

    I'm using LCC-win32, Win98SE, and Petzold's "Programming Windows".

    Here's the source.

    Code:
    // Test for plotting pixels.
    
    #include <windows.h>
    #include <math.h>
    #include <stdlib.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
    	static TCHAR szAppName[] = TEXT ("Pixels");
    	HWND hwnd;
    	MSG	msg;
    	WNDCLASS wndclass;
    
    	wndclass.style = CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc = WndProc;
    	wndclass.cbClsExtra =0;
    	wndclass.cbWndExtra = 0;
    	wndclass.hInstance = hInstance;
    	wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    	wndclass.lpszMenuName = NULL;
    	wndclass.lpszClassName = szAppName;
    
    	if (!RegisterClass (&wndclass))
    	{
    		MessageBox (NULL, TEXT ("Program requires Windows NT!"), szAppName, MB_ICONERROR);
    
    		return 0;
    	}
    
    	hwnd = CreateWindow (szAppName, TEXT ("Random Pixels"),
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT,
    		CW_USEDEFAULT, CW_USEDEFAULT,
    		NULL, NULL, hInstance, NULL);
    
    	ShowWindow (hwnd, iCmdShow);
    	UpdateWindow (hwnd);
    
    
    
    	while (GetMessage (&msg, NULL, 0, 0))
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
    	return msg.wParam;
    }
    
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
    	int i,a,b;
    
    	static int cxClient, cyClient;
    	HDC hdc;
    	PAINTSTRUCT ps;
    
    	switch (message)
    	{
    		case WM_CREATE:
    			SetTimer (hwnd, 1, 100, NULL);
    			return 0;
    
    		case WM_SIZE:
    			cxClient = LOWORD (lParam);
    			cyClient = HIWORD (lParam);
    			return 0;
    
    		case WM_TIMER:
    			InvalidateRect (hwnd, NULL, TRUE);
    			return 0;
    
    		case WM_PAINT:
    
    			hdc = BeginPaint (hwnd, &ps);
    
    			for (int i=0; i<1000; i++)
    			{
    				a = rand()%124;
    				b = rand()%124;
    
    				SetPixel (hdc, a, b, 0x0000FF00);
    			}
    
    			EndPaint (hwnd, &ps);
    
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage (0);
    			return 0;
    
    	}
    
    	return DefWindowProc (hwnd, message, wParam, lParam);
    }

  2. #2
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Follow-up:

    The above test was done in 16bit colour, if I set the display to 32bit the problem goes away. In 8 bit it gets worse (sometimes pixels are green (correct) sometimes white or grey.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Looks OK....

    Is the pixel being set incorrectly by SetPixel() or getting some default colour because it is not set at all?


    Try,

    InvalidateRect (hwnd, NULL, FALSE);//don't erase background

    or

    hdc = BeginPaint (hwnd, &ps);

    //clear the draw area
    FillRect(ps.hdc ,&ps.rcRect ,GetStockObject( WHITE_BRUSH ) );

    for (int i=0; i<1000; i++)

    and

    Test the return from SetPixel() is the correct colour.
    Use GetLastError() if it is not.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Thanks for the reply Novacain.



    Try,

    InvalidateRect (hwnd, NULL, FALSE);//don't erase background
    I tried this (In fact it was a bug in the original version !) but the same effect happens.


    hdc = BeginPaint (hwnd, &ps);

    //clear the draw area
    FillRect(ps.hdc ,&ps.rcRect ,GetStockObject( WHITE_BRUSH ) );
    My compiler flags an error at &ps.rcRect - it doesn't know what to do with rcRect - should I define this myself somewhere?

  5. #5
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    I think you meant &ps.rcPaint - this works but the bug still remains.
    What do I do with the result of GetLastError?

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    use GetLastError() straight away and look at FormatMessage() or look up the error code in the help ('error codes [win32]' in my version of MSVC).
    Last edited by novacain; 04-12-2004 at 08:31 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by novacain
    use GetLastError() straight away and look at FormatMessage() or look up the error code in the help ('error codes [win32]' in my version of MSVC).
    Or, if you're using MSVC, you can use @err in the watch window and it'll translate it for you. Nice undocumented feature.

    I am actually writing a paper right now full of MSVC tricks and tips. I'll hopefully have a link for that sometime in the future.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing pixel colour
    By redruby147 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 05:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Reading pixel color data from screen
    By JJFMJR in forum Windows Programming
    Replies: 8
    Last Post: 08-22-2008, 12:27 PM
  4. Getting Pixel Colour Screen
    By god_of_war in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2006, 12:17 PM
  5. Creating pixel graphics in MFC
    By Kristian25 in forum Windows Programming
    Replies: 2
    Last Post: 01-09-2003, 12:39 PM

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