Thread: GDI - Drawing to the screen correctly

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    GDI - Drawing to the screen correctly

    Okay. So I made this code to draw a grid of rectangles to the screen

    Code:
    	for(int i = 0; i < _x; ++i)
    	{
    		for(int j = 0; j < _y; ++j)
    		{
    			if(scr[_i][_j] != scr[i][j])
    			{
    				hBrush = ::CreateSolidBrush(scr[i][j]);
    				hPen   = ::CreatePen(PS_SOLID, 1, scr[i][j]);
    
    				oldPen   = (HPEN)   ::SelectObject(hdc, hPen);
    				oldBrush = (HBRUSH) ::SelectObject(hdc, hBrush);
    			}
    
    			::Rectangle(hdc, i * width, j * height, 
    					(i + 1) * width, (j + 1) * height);
    
    			if(scr[_i][_j] != scr[i][j])
    			{
    				::SelectObject(hdc, oldPen);
    				::SelectObject(hdc, oldBrush);
    
    				::DeleteObject(hPen);
    				::DeleteObject(hBrush);
    			}
    			_j = j;
    			_i = i;
    		}
    	}
    Of course it's impractical, I'll try to manage a DIB instead of a grid like this later, but right now it's not working. The logic is that, if the pixel immediately previous to the one being drawn is the same color, then don't recreate the color and delete it again and just use the same things. It skips pixels when I do this though, and works fine if I comment out those if's. Is there any way to fix this?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You're not managing your brush/pen correctly, and deleting them too soon. That second if shouldn't be "if the pixel before me isn't the same", it should be, "if the pixel after me isn't the same".

    This is a tad tricky though, you could move your delete statements to the first if:
    Code:
    if(pixel_color_change (watch out for pixel #1!))
    {
      Delete old brushes, if any (watch out for pixel #1!)
      Create new brushes
      Select, etc.
    }
    Draw!
    And then delete and reset your brushes and pens when done.

    EDIT: If you step through yours by hand, you'll see that when two pixels in a row are the same, then you're deleting the second pixels brush/pen.
    Last edited by Cactus_Hugger; 06-24-2006 at 09:55 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL + Keys
    By Livijn in forum Game Programming
    Replies: 24
    Last Post: 05-11-2007, 04:31 PM
  2. Laptop LCD Screen
    By mkylman in forum Tech Board
    Replies: 8
    Last Post: 11-11-2006, 08:27 PM
  3. Drawing something on the screen...
    By Finchie_88 in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2004, 12:23 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM