Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,752 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,793 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



BitBlt Issues.

 
Reply to this topicStart new topic

BitBlt Issues., I'm struggling with the use of BitBlt, StretchRect in this program

Csm Richardsson
post 18 Jun, 2008 - 01:16 AM
Post #1


New D.I.C Head

*
Joined: 18 Feb, 2008
Posts: 7


My Contributions


Hello,

I'm currently working on a small piece of code, in which the purpose is to take an image with four objects on it, crop one of them out and put some user controlled keypress events on it. (to move it around). (thanks to KYA for the help with the keypress events) smile.gif

My problem is that I've become completely stumped on how to go about doing this.

I've written the code out, and, have gotten the original image to display as I want it to, but as I mentioned above, that's not my projected outcome which I am looking for. I still need to learn how to use BitBlt or StretchRect in such a way as to display only the second sphere from my bitmap. (one with yellow circle on it).

I would be most grateful if anyone can help dig me out of this rut.
Following is the code, as I have it so far.

CODE


#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>

//application title
#define APPTITLE "Load_Bitmap"

//macros to read the keyboard asynchronously
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

//screen resolution
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768

//forward declarations
LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
ATOM MyRegisterClass(HINSTANCE);
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);

//Direct3D objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;

LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9 surface = NULL;


//window event callback function
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Game_End(hWnd);
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}


//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    //create the window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);

    //fill the struct with info
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC)WinProc;
    wc.cbClsExtra     = 0;
    wc.cbWndExtra     = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = APPTITLE;
    wc.hIconSm       = NULL;

    //set up the window with the class info
    return RegisterClassEx(&wc);
}


//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
    // declare variables
    MSG msg;

    // register the class
    MyRegisterClass(hInstance);

    // initialize application
    //note--got rid of initinstance
    HWND hWnd;

    //create a new window
    hWnd = CreateWindow(
       APPTITLE,                                //window class
       APPTITLE,                                //title bar
       WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP,   //window style
       CW_USEDEFAULT,                           //x position of window
       CW_USEDEFAULT,                           //y position of window
       SCREEN_WIDTH,                            //width of the window
       SCREEN_HEIGHT,                           //height of the window
       NULL,                                    //parent window
       NULL,                                    //menu
       hInstance,                               //application instance
       NULL);                                   //window parameters

    //was there an error creating the window?
    if (!hWnd)
      return FALSE;

    //display the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    
    //initialize the game
    if (!Game_Init(hWnd))
        return 0;


    // main message loop
    int done = 0;
    while (!done)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            //look for quit message
            if (msg.message == WM_QUIT)
                done = 1;

            //decode and pass messages on to WndProc
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
            //process game loop (else prevents running after window is closed)
            Game_Run(hWnd);
    }

    return msg.wParam;
}


int Game_Init(HWND hwnd)
{
    HRESULT result;

    //initialize Direct3D
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    if (d3d == NULL)
    {
        MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
        return 0;
    }

    //set Direct3D presentation parameters
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.hDeviceWindow = hwnd;

    //create Direct3D device
    d3d->CreateDevice(
        D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        hwnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp,
        &d3ddev);

    if (d3ddev == NULL)
    {
        MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
        return 0;
    }

    //set random number seed
    srand(time(NULL));

    //clear the backbuffer to black
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
    
    //create surface
    result = d3ddev->CreateOffscreenPlainSurface(
        1024,               //width of the surface
        768,                //height of the surface
        D3DFMT_X8R8G8B8,    //surface format
        D3DPOOL_DEFAULT,    //memory pool to use
        &surface,           //pointer to the surface
        NULL);              //reserved (always NULL)

    if (result != D3D_OK)
        return 1;

    //load surface from file into newly created surface
    result = D3DXLoadSurfaceFromFile(
        surface,            //destination surface
        NULL,               //destination palette
        NULL,               //destination rectangle
        "spheres.bmp",      //source filename
        NULL,               //source rectangle
        D3DX_DEFAULT,       //controls how image is filtered
        0,                  //for transparency (0 for none)
        NULL);              //source image info (usually NULL)

    //make sure file was loaded okay
    if (result != D3D_OK)
        return 1;

    //return okay
    return 1;
}

void Game_Run(HWND hwnd)
{
  RECT rect;
  static int x = 0;
  static int y = 0;

  const int rectWidth = 160;
  const int rectHeight = 160;

  rect.left = 160;
  rect.top = 0;
  rect.right = 319;
  rect.bottom = 163;
  d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE);
  //above are the four locations of the second sphere, from my bitmap pic
  //which is the sphere I want to display
  
  //make sure the Direct3D device is valid
    if (d3ddev == NULL)
        return;

    //start rendering
    if (d3ddev->BeginScene())
    {

        d3ddev->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
        
        //create pointer to the back buffer
        d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

        //draw surface to the backbuffer
        d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);


        //stop rendering
        d3ddev->EndScene();
    }



    //display the back buffer on the screen
    d3ddev->Present(NULL, NULL, NULL, NULL);

    //check for escape key (to exit program)
    if (KEY_DOWN(VK_ESCAPE))
        PostMessage(hwnd, WM_DESTROY, 0, 0);

}

void Game_End(HWND hwnd)
{
    //free the surface
    surface->Release();

    //release the Direct3D device
    if (d3ddev != NULL)
        d3ddev->Release();

    //release the Direct3D object
    if (d3d != NULL)
        d3d->Release();
}


As is listed in comment form, what my outcome should be is the display of the second sphere (with the yellow circle on it) from this bitmap.
Please, any help or advice would be most appreciated.

This post has been edited by Csm Richardsson: 18 Jun, 2008 - 10:18 PM


Attached File(s)
Attached File  spheres.bmp ( 307.55k ) Number of downloads: 6
User is offlineProfile CardPM

Go to the top of the page


Trogdor
post 18 Jun, 2008 - 02:32 AM
Post #2


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 430



Thanked 1 times

Dream Kudos: 125
My Contributions


can you add the [ / code ] tag ?
User is offlineProfile CardPM

Go to the top of the page

Csm Richardsson
post 18 Jun, 2008 - 02:50 AM
Post #3


New D.I.C Head

*
Joined: 18 Feb, 2008
Posts: 7


My Contributions


Yeah, sorry about that. blink.gif
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 18 Jun, 2008 - 06:24 AM
Post #4


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 430



Thanked 1 times

Dream Kudos: 125
My Contributions


Looks like DX9, sorry cant help you wwith that, but there are folks here that can.
It would help if you make the subject more clear in the thread-title.
Good luck.
User is offlineProfile CardPM

Go to the top of the page

mensahero
post 18 Jun, 2008 - 08:34 AM
Post #5


c0mput3rz Are Only Human

Group Icon
Joined: 26 May, 2008
Posts: 664



Thanked 17 times

Dream Kudos: 75
My Contributions


This would have a better chance in the c/c++ forum. Just a suggestion. You could PM a moderator and ask him if he could move this thread to the right forum.
User is offlineProfile CardPM

Go to the top of the page

Csm Richardsson
post 18 Jun, 2008 - 07:43 PM
Post #6


New D.I.C Head

*
Joined: 18 Feb, 2008
Posts: 7


My Contributions


Alright, I'll try that then. Thanks. smile.gif
User is offlineProfile CardPM

Go to the top of the page

KYA
post 18 Jun, 2008 - 08:15 PM
Post #7


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 2,430



Thanked 13 times

Dream Kudos: 1150
My Contributions


If I read correctly, you want to move a rectangle around based on key events? I would add a HandleKeys() function that executes prior to the rendering of the scene. Here is some code to get you on your way:

cpp


//prorotype
void HandleKeys(HWND);

//function stuff
void HandleKeys(HWND hWnd)
{
if (GetAsyncKeyState(VK_UP) < 0)
//move bitmap or whatever
else if (GetAsyncKeyState(VK_RIGHT) < 0)
//move bitmap or whatever
else if (GetAsyncKeyState(VK_DOWN) < 0)
//move bitmap or whatever
else if (GetAsyncKeyState(VK_LEFT) < 0)
//move bitmap or whatever
}

//put HandleKeys() just in front of game_run here
else {
//process game loop (else prevents running after window is closed)
HandleKeys(hWnd);
Game_Run(hWnd);
}

User is offlineProfile CardPM

Go to the top of the page

Csm Richardsson
post 18 Jun, 2008 - 08:41 PM
Post #8


New D.I.C Head

*
Joined: 18 Feb, 2008
Posts: 7


My Contributions


Yes that's half of it, and thank you for the starter.
I'm still picking up on all of the (VK_keynames), although as you post
it, is pretty self explanatory.

By chance, could you give me an idea, (or good reference point) on how I could get that second sphere cut out and displayed w/o being stretched to fit? I'm only just learning how to use BitBlt, StretchRect (which is where I'm struggling the most in) and the only things (tutorials, etc) I've been able to find, only lead up to where I am at. (which is getting the original bitmap to display)

User is offlineProfile CardPM

Go to the top of the page

KYA
post 18 Jun, 2008 - 10:38 PM
Post #9


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 2,430



Thanked 13 times

Dream Kudos: 1150
My Contributions


Based on the bitmap you gave, each ball appears to be a "frame". Move over a certain amount of pixels and you can draw specified parameters.

Of the top of my head, i can't think of the method name. I'll have to do some research. MSDN is a great resource for directX stuff including all of the documentation.
User is offlineProfile CardPM

Go to the top of the page

Csm Richardsson
post 19 Jun, 2008 - 12:22 AM
Post #10


New D.I.C Head

*
Joined: 18 Feb, 2008
Posts: 7


My Contributions


Right. You might have noticed in my code that I had the following;

CODE

RECT rect;
  static int x = 0;
  static int y = 0;

  const int rectWidth = 160;
  const int rectHeight = 160;

  rect.left = 160;
  rect.top = 0;
  rect.right = 319;
  rect.bottom = 163;
  d3ddev->StretchRect(surface, NULL, backbuffer, &rect, D3DTEXF_NONE);



The latter end of this code is the co-ordinates I have for the second sphere. (i.e. rect.left through rect.bottom) I just got lost as to how to continue with it, to clip out that sphere and display it.

Thank you.

I've been looking around MSDN myself, for that last half hour. So far, the most helpful link I've found is;

Capturing an Image

...but this is closer to what I have in my textbook.

This helps some...

Scaling an Image

...but I still don't understand how to apply the frame location of the second sphere, in the above snippet, to it, and make it work. crazy.gif
idk, what do you think's the best approach here?

This post has been edited by Csm Richardsson: 19 Jun, 2008 - 01:56 AM
User is offlineProfile CardPM

Go to the top of the page

Csm Richardsson
post 19 Jun, 2008 - 01:52 AM
Post #11


New D.I.C Head

*
Joined: 18 Feb, 2008
Posts: 7


My Contributions


I've also come across this MaskBlt function that seems to match the surface color to the destination color. Could this be used to, by all appearances, "clip" that second sphere from the original bitmap? Or is this more of a function to just take away the appearance of the bitmap being block, rather than the image that's on it?

MaskBlt
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 12:15PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month