Jump to content
  • Advertisement
Sign in to follow this  

Blitting Problem

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

Hello all! Im sorry for reposting about the following subject, but I have lost my link to my previous thread. The problem I am having is when blitting from a compatible dc to the window dc. I am going to post my button class. At startup it blits the correct button perfectly. As soon as I move the window, it blits the entire image, that is all the images of the button in all the states. Basically it blits the entire bitmap. I made sure my coordinates stay the same, they are not being modified after button_create(). So I have no idea why it blits the entire button image on a resize. I know this is a lot of code, but I have tried fixing it myself to no avail. I am sorry for the huge post. button_on prefixed methods deal with window messages and get called from the window's WndProc().
/* Create the button */
bool gapi_button::button_create( HWND window, RECT position, RECT normal_coords, RECT hover_coords, 
	RECT down_coords, wstring imagefile )
	{
		/* Make sure we have no resources if we creating */
		button_destroy();

		/* Load the button image resource */
		button_image = g_resmgr.load_resource( imagefile, window );
	
		/* Make sure we loaded the image */
		if ( ! button_image )
			{
				return false;
			}

		/* Set local variables */
		button_window =  window;
		CopyRect( &button_position, &position );				
		CopyRect( &normal_state_coords, &normal_coords );			
		CopyRect( &hover_state_coords, &hover_coords );				
		CopyRect( &down_state_coords, &down_coords );				

		/* Need to invalidate button region for painting */
		InvalidateRect( button_window, &button_position, true ); 

		/* Done */
		return true;
	}

/* Destroy the button */
void gapi_button::button_destroy( void )
	{
		/* Release reference to image resource */
		if ( button_image )
			{	
				g_resmgr.release( button_image );
				button_image = 0;
			}

		/* Null variables */
		button_window = 0;
		button_state = BS_NORMAL;
		button_normaldraw = false;
	}

/* When the window requires re-painting */
void gapi_button::button_on_paint( HDC &hdc )
	{
		switch ( button_state )
		{

		/* Button is in normal/up state */
		case BS_NORMAL:
			{
				/* Blit the button on normal/up state */
				if ( button_image )
					{
						button_image->draw_image( hdc, button_position, normal_state_coords );
					}

				/* Set normal draw flag, so we dont keep redrawing */
				button_normaldraw = true;

				return;
			};

		/* Button is in hover state */
		case BS_HOVER:
			{
				/* Blit the button in hover state */
				if ( button_image )
					{
						button_image->draw_image( hdc, button_position, hover_state_coords );
					}

				/* Set normal draw flag, so we dont keep redrawing */
				button_normaldraw = false;

				return;
			};

		/* Button is in down state */
		case BS_DOWN:
			{
				/* Blit the button in down state */
				if ( button_image )
					{
						button_image->draw_image( hdc, button_position, down_state_coords );
					}

				/* Set normal draw flag, so we dont keep redrawing */
				button_normaldraw = false;

				return;
			};
		}
	}

/* When the window has the mouse left button down */
void gapi_button::button_on_lbutton_down( WPARAM &wparam, LPARAM &lparam )
	{
		/* Get mouse x/y coordinates */
		POINT mouse = { LOWORD(lparam), HIWORD(lparam) };

		/* Check to see if the mouse is in the button region  */
		if ( (mouse.x >= button_position.left) && ( mouse.x <= button_position.right) && ( mouse.y <= button_position.bottom ) && 
			( mouse.y >= button_position.top ))
			{
				/* Set button state to down */
				button_state = BS_DOWN;

				/* Need to invalidate button region for painting */
				InvalidateRect( button_window, &button_position, true ); 
			}
	}

/* When the window has the mouse left button up */
void gapi_button::button_on_lbutton_up( WPARAM &wparam, LPARAM &lparam )
	{
		/* Get mouse x/y coordinates */
		POINT mouse = { LOWORD(lparam), HIWORD(lparam) };

		/* Check to see if the mouse is in the button region  */
		if ( (mouse.x >= button_position.left) && ( mouse.x <= button_position.right) && ( mouse.y <= button_position.bottom ) && 
			( mouse.y >= button_position.top ))
			{
				/* Set button state to hover */
				button_state = BS_HOVER;

				/* Need to invalidate button region for painting */
				InvalidateRect( button_window, &button_position, true ); 
			}
	}

/* When the mouse moves */
void gapi_button::button_on_mousemove( WPARAM &wparam, LPARAM &lparam )
	{
		/* Get mouse x/y coordinates */
		POINT mouse = { LOWORD(lparam), HIWORD(lparam) };

		/* Check to see if the mouse is in the button region  */
		if ( (mouse.x >= button_position.left) && ( mouse.x <= button_position.right) && ( mouse.y <= button_position.bottom ) && 
			( mouse.y >= button_position.top ))
			{
				/* If the button is down and is within button coordinates, no need to go into hover mode */
				if ( button_state == BS_DOWN )
					return;

				/* Set button state to hover */
				button_state = BS_HOVER;

				/* Need to invalidate button region for painting */
				InvalidateRect( button_window, &button_position, true ); 
			}

		/* We use m_bNormalBlit to make sure we dont repeatedly blit the normal button image if were outside
			its region */
		else if ( ! button_normaldraw )
			{
				/* Set button state to normal */
				button_state  = BS_NORMAL;

				/* Need to invalidate button region for painting */
				InvalidateRect( button_window, &button_position, true ); 
			}
	}


/* Draw the image onto a window */
void gapi_image::draw_image( HDC &hdc, RECT &position, RECT &imagepos )
	{
		/* Select the bitmap into our device context */
		HGDIOBJ oldobj = SelectObject( image_dc, image_bitmap );

		/* Blit the image onto the specified device context */
		BitBlt( hdc, position.left, position.top, position.right, position.bottom, image_dc, 
			imagepos.left, imagepos.top, SRCCOPY );

		/* Select the object back to free up resources */
		SelectObject( image_dc, oldobj );
	}

gapi_image is an image management class, draw_image() basically blits the button dc onto the window dc. Please Help!

Share this post


Link to post
Share on other sites
Advertisement
Couple things to help with your game dev experience:

1) You can see all posts you've made by clicking Control Pannel at the top of this page (under members). Once in there, click View Profile.

2) When sumitting large chunks of code like that use the source tags, not the code tags.

As for your question... err...

Matt Hughson

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!