Link to home
Start Free TrialLog in
Avatar of kyriakos70
kyriakos70

asked on

directx directinput how to recognize mouse press on bitmap

Hello,
I have an application in VC++ and directx, I am doing the initialization and the main loop and I insert a bitmap, what I want is how to recognize when I press the mouse (only when is down left button) on the bitmap, and when the mouse is pressed to change the bitmap with another bitmap and when is released to revert back to the fitst bitmap (I thing direct input is the right api).

Thank you
Kyriakos
(I will provide code if necessary)
Avatar of lucky_james
lucky_james
Flag of India image

you can check the mouse state.
check out:
http://www.two-kings.de/tutorials/dinput/dinput03.html
Avatar of Member_2_5069294
Member_2_5069294

What are you asking exactly?  How to use Direct Input to get mouse input?  How to change the bitmap?  How to detect the mouse is over the bitmap when you click?
Avatar of kyriakos70

ASKER

satsumo, all three you mention this is what I am asking, firstly how to detect when the mouse is over the bitmap, I know how to initialize directinput, and second when the mouse is pressed on the bitmap theprocedure of this so I can change the bitmap I think with something like this, I use quads to make a 2d game.
IDirect3DTexture9 *LoadTexture(char *fileName);

Open in new window

Can I ask you something more? how can I use this code but put the sprite in a specific position on the window?

Thank you
D3DXCreateTextureFromFile(d3ddev, "Panel1.png", &sprite);

Open in new window

I have this code I have to change the values to the specific area of the first or the second line?

Thank you
kYRIAKOS
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); //center at the upperleftcorner
D3DXVECTOR3 position(50.0f, 50.0f, 0.0f);

Open in new window

OK,
What I want when the mouse is pressed on a specific x,y position to change the bitmap.
Code below.

I draw the sprite and put it in a  position (2nd line of code), I have the 4rth line of code can I use an if...then...else and insert in the function of the mouse the x,y of the sprite position? and how to enter the whole bitmap area?

Thank you
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    
D3DXVECTOR3 position(150.0f, 150.0f, 0.0f);
d3dspt->Draw(sprite1, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
GetCursorPos(&MousePos);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_2_5069294
Member_2_5069294

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you,
That is all I wanted, by the way ir is a full screen 2d application...game.

Thank you satsumo
Kyriakos
Thank you, and good luck with the game.
One more thing how to change the position of the sprite I try the below code and nothing happens, I will include the code of the main loop too, there are two the main loop and renderframe procedures. And this code gives a continious frame rate or just one frame and how to make it continious?

Thank you
Kyriakos
while(TRUE)
    {
        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        render_frame();

       // check the 'escape' key
        if(KEY_DOWN(VK_ESCAPE))
            PostMessage(hWnd, WM_DESTROY, 0, 0);

        while ((GetTickCount() - starting_point) < 25);
    }

    // clean up DirectX and COM
    cleanD3D();

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

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



void render_frame(void)
{
    // clear the window to a deep blue
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();    // begins the 3D scene
    d3dspt->Begin(NULL);    // begin sprite drawing
   // draw the sprite
      	D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);    // center at the upper-left corner
    D3DXVECTOR3 position(150.0f, 150.0f, 0.0f);    // position at 50, 50 with no depth 
	d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
	d3dspt->End();    // end sprite drawing
    d3ddev->EndScene();    // ends the 3D scene
    d3ddev->Present(NULL, NULL, NULL, NULL);
    return;
}

Open in new window

That code should give continuous frames, not faster than 40 fps.  Which is a good idea, otherwise the game would probably run much too fast.

I think the game is rendering, its just that nothing changes.  You always set position to [150, 150] when you draw the sprite.  For it to move you need to position to be a variable.  You can change it either by programming, in response to keys presses or the mouse.

The last example is the most generally useful one.  Each of the moving objects in your game will have its own position vector.  You can combine these, have sprite one moving in a circle, sprite 2 following the mouse and sprite 3 moving with the keys.

Example code below:

This code move the sprite in a circle around [150, 150], the radius of the circle is 50 pixels:

#include <math.h>

float angle = 0.f;

void render_frame(void)
{
   D3DXVECTOR3 position(0.f, 0.f, 0.f);
   D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
   position.x = 150 + (50.f * (float) sin (angle));
   position.y = 150 + (50.f * (float) cos (angle));
   angle += 0.01f;
   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();
    d3dspt->Begin(NULL); 
    d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
    d3dspt->End(); 
    d3ddev->EndScene();
    d3ddev->Present(NULL, NULL, NULL, NULL);
}


This code makes the sprite follow the mouse:

void render_frame(void)
{
   POINT cursor;
   D3DXVECTOR3 position(0.f, 0.f, 0.f);
   D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
   GetCursorPos (&cursor);
   position.x = (float) cursor.x;
   position.y = (float) cursor.y;

   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();
    d3dspt->Begin(NULL); 
    d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
    d3dspt->End(); 
    d3ddev->EndScene();
    d3ddev->Present(NULL, NULL, NULL, NULL);
}


This code makes the sprite move when the keys are pressed, the sprite starts at [150, 150] and moves 2 pixels when you press the WASD keys:

D3DXVECTOR3 position(150.f, 150.f, 0.f);

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch(message)
   {
      case WM_CHAR:
         switch (wParam)
         {
            case 'd':
              position.x += 2;
              break;

            case 'a':
              position.x -= 2;
              break;

            case 's':
              position.y += 2;
              break;

            case 'w':
              position.y -= 2;
              break;
         }

         break;

      case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
   }

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

void render_frame(void)
{
   D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);

   d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();
    d3dspt->Begin(NULL); 
    d3dspt->Draw(sprite, NULL, &center, &position, D3DCOLOR_XRGB(255, 255, 255));
    d3dspt->End(); 
    d3ddev->EndScene();
    d3ddev->Present(NULL, NULL, NULL, NULL);
}

Open in new window