Thread: Using BitBlt() to display only a portion of a big bitmap

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Using BitBlt() to display only a portion of a big bitmap

    I have this bitmap which is a long rectangle. There are 7 different picture going across it. Each "block" has an integer called frame. It starts at 1. When frame increments, that SHOULD change what portion of the bitmap is displayed. Here is my failed attempt at doing this, it should make it more clear as to what I'm trying to do:
    Code:
    else if(blocks[i].explode == true)
    		{
                                                    width = bm.bmWidth;
    			SelectObject(hDCMem, Explosion);
    			width = (width/7)*(blocks[i].frame) - 1;
    			start = width - (width/7) + 1;
    			BitBlt(hDCBuffer, blocks[i].x + blocks[i].width/2, blocks[i].y + blocks[i].height/2, width, height, hDCMem, start, 0, SRCPAINT);
    			blocks[i].frame++;
    			if(blocks[i].frame == 7)
    			{
    				blocks[i].frame = 1;
    				blocks[i].explode = false;
    			}
    			Sleep(1000);
    		}
    the + and - 1 is just to make up for the 1 pixel separater between each portion of the bitmap

    oh and each time through, width starts as the width of the entire bitmap

    oh, and the problem with it is that it just doesn't display the correct portion
    Last edited by Leeman_s; 01-15-2003 at 07:56 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Usually in gaming frames are stored top down not left to right. This has to do with high they are arranged in memory. It will be faster if stored top to bottom. Anyways here is a quick example of how to index through them.
    Given a bitmap with 4 frames that is 64x256 ( width x height ) we can use the following.

    Code:
    RECT rPortion = { 0 };
    
    const int nBmpWidth = 64;  // Bitmap width
    const int nbmpHeight = 256; // Bitmap height
    
    const int nSprWidth = 64; // Sprite width
    const int nSprHeight = 64; // Sprite height
    
    const int nFrames = 4; // Frames of animation
    int i; 
    
    // Cycle through frames
    for( i = 0; i < nFrames; ++i )
    {
      SetRect( &rPortion, 0, (i * nSprHeight), nSprWidth, (i * nSprHeight) + nSprHeight );
    
      // Assume xPos and yPos already defined...
      BitBlt( hDC, xPos, yPos, nSprWidth, nSprHeight, hCDC, rPortion.left, rPortion.top, SRCCOPY );
    }
    Hopefully that makes sense. It shouldn't be too bad if you just think about it logically. Imagine the rectangle moving down vertically after each frame. The left and right portions won't change only the top. Then the bottom will always be the top + the height of the sprite. Good luck.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    alright, but I still don't get why this isn't working for me, going horizontally. i'm looking for a logical error here.

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Well, I don't know what to say. I tried your method and it worked! Thanks man, I'm not sure how it is different (mathematically) than how I did, but it worked. Thanks!

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    No problem, I was going to comment on yours but I would've rather seen the entire function. You start your indexing at 1 which is odd. So you might experience an "off by 1" frame indexing. I'm not really sure. Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. Display a bitmap image
    By MysticMizra in forum Windows Programming
    Replies: 7
    Last Post: 10-21-2002, 03:36 AM
  3. Saving a bitmap.
    By jdinger in forum Game Programming
    Replies: 4
    Last Post: 05-01-2002, 01:40 PM
  4. texture is all white in opengl!
    By Crossbow in forum Game Programming
    Replies: 7
    Last Post: 03-31-2002, 11:54 AM
  5. HELP: Error in program, won't display bitmap!
    By wmaster1 in forum Windows Programming
    Replies: 1
    Last Post: 12-01-2001, 02:42 PM