Jump to content
  • Advertisement
Sign in to follow this  
Enerjak

Something really weird with my mouse!!

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

Ok, for some strange reason, my mouse in game isn't working. i'm trying to move the paddle with the mopuse but nothing is happening. I tested the keyboard ingame and it works. I don't know what else to do. it's driving me nuts. Maybe you guys can give me some pointers or something? the source code is from Begining Game Programming by jonathan Harbour. I can't for the death of me get why the mouse isn't working ingame. it's driving me nuts.

Share this post


Link to post
Share on other sites
Advertisement
Try doing a little bit of debugging if you haven't already: try printing out the input that you are capturing from the mouse and see if you are indeed getting any sort of input. If you are, but your paddle isn't moving then you know it is something you are doing wrong. If you aren't getting any sort of input then that probably means there is something wrong with how you are getting the input.

If all else fails, try posting some code so others can have a look at it.

Share this post


Link to post
Share on other sites
Here's what I just read:
"Hi guys, I have a problem where I do something but what I get isn't what I thought, what's wrong?"

Notice the similarities? We need a lot more information than what you've given. What language? What API? Are you using DirectInput? (If so, abandon it now, really) What debugging have you already done? Are you reading data from the mouse? Are you acting on it? Is your application telling you that it is in fact getting mouse input?

Share this post


Link to post
Share on other sites
I'm using direct input. and the application isn't getting mouse input. What input API would you recommend? also this is my code in my main game.cpp file.



// Beginning Game Programming, Second Edition
// Game Framework
// Main Game source code file


#include "Bin/game.h"

#define BLACK D3DCOLOR_ARGB(0,0,0,0)

LPDIRECT3DTEXTURE9 Player_Paddle;
SPRITE Player_Sprite;

LPD3DXSPRITE Sprite_Handler;

void DrawPlayer();

long start = GetTickCount();

LPD3DXFONT Fonts;

//initializes the game
int Game_Init(HWND hwnd)
{

D3DXCreateFontA(d3ddev, // the Direct3D Device
20, 0, // font size twenty with the default width
FW_NORMAL, // normal font weight
1, // no mipmap levels
FALSE, // not italic
DEFAULT_CHARSET, // default character set
OUT_DEFAULT_PRECIS, // default precision
DEFAULT_QUALITY, // default quality
DEFAULT_PITCH || FF_DONTCARE, // more defaults...
"Arial", // typeface "Arial"
&Fonts); // address of the font object created above


//initialize keyboard
if (!Init_Keyboard(hwnd))
{
MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
return 0;
}

if (!Init_Mouse(hwnd))
{
MessageBoxA(NULL,"Mouse Could not be initialized","Error",MB_OK);
return 0;
}

D3DXCreateSprite(d3ddev,&Sprite_Handler);

Player_Paddle = LoadTexture("Paddles/Player_Paddle/Player_Paddle.bmp",D3DCOLOR_XRGB(255,0,255));

Poll_Mouse();

Player_Sprite.x = 200;
Player_Sprite.y = 100;
Player_Sprite.width = 100;
Player_Sprite.height = 20;
Player_Sprite.movex = 3;



//return okay
return 1;
}


//the main game loop
void Game_Run(HWND hwnd)
{
//clear the screen here if needed
d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,1);



if(GetTickCount() - start >= 30)
{
start = GetTickCount();

Poll_Keyboard();

if(Key_Down(DIK_LEFT))
Player_Sprite.x -= Player_Sprite.movex;

if(Key_Down(DIK_RIGHT))
Player_Sprite.x += Player_Sprite.movex;
}

if (d3ddev->BeginScene())
{
char s[100];
sprintf(s,"MouseX: %d",Mouse_X());
RECT textrect;
SetRect(&textrect,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);

Fonts->DrawTextA(
NULL,
s,
10,
&textrect,
DT_LEFT,
D3DCOLOR_XRGB(255,255,255));

DrawPlayer();


d3ddev->EndScene();
}

d3ddev->Present(NULL, NULL, NULL, NULL);

Poll_Keyboard();
if (Key_Down(DIK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);

Poll_Mouse();
//use Mouse_X(), Mouse_Y(), and Mouse_Button()
}

void Game_End(HWND hwnd)
{
//delete all objects from memory
//including surfaces, sprites, sounds, models,
//and anything else you used that allocated memory
//DirectX objects are removed automatically in dxgraphics.cpp
}

void DrawPlayer()
{
Sprite_Handler->Begin(D3DXSPRITE_ALPHABLEND);

D3DXVECTOR3 Player_Pos((float)Player_Sprite.x, (float)Player_Sprite.y,0);

Sprite_Handler->Draw(
Player_Paddle,
NULL,
NULL,
&Player_Pos,
D3DCOLOR_XRGB(255,255,255));

Sprite_Handler->End();
}




and this is the initialization for Direct input.



#include "dxinput.h"

#define BUTTON_DOWN(obj, button) (obj.rgbButtons[button] & 0x80)

LPDIRECTINPUT8 dinput;
LPDIRECTINPUTDEVICE8 dimouse;
LPDIRECTINPUTDEVICE8 dikeyboard;
DIMOUSESTATE mouse_state;

//keyboard input
char keys[256];

int Init_DirectInput(HWND hwnd)
{
//initialize DirectInput object
HRESULT result = DirectInput8Create(
GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&dinput,
NULL);

if (result != DI_OK)
return 0;

//initialize the mouse
result = dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL);
if (result != DI_OK)
return 0;

//initialize the keyboard
result = dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL);
if (result != DI_OK)
return 0;

//clean return
return 1;
}


int Init_Mouse(HWND hwnd)
{
//set the data format for mouse input
HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse);
if (result != DI_OK)
return 0;

//set the cooperative level
//this will also hide the mouse pointer
result = dimouse->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
if (result != DI_OK)
return 0;

//acquire the mouse
result = dimouse->Acquire();
if (result != DI_OK)
return 0;

//give the go-ahead
return 1;
}

int Mouse_X()
{
return mouse_state.lX;
}

int Mouse_Y()
{
return mouse_state.lY;
}

int Mouse_Button(int button)
{
return BUTTON_DOWN(mouse_state, button);
}

void Poll_Mouse()
{
dimouse->GetDeviceState(sizeof(mouse_state), (LPVOID)&mouse_state);
}

void Kill_Mouse()
{
if (dimouse != NULL)
{
dimouse->Unacquire();
dimouse->Release();
dimouse = NULL;
}
}

int Init_Keyboard(HWND hwnd)
{
//set the data format for mouse input
HRESULT result = dikeyboard->SetDataFormat(&c_dfDIKeyboard);
if (result != DI_OK)
return 0;

//set the cooperative level
result = dikeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
if (result != DI_OK)
return 0;

//acquire the mouse
result = dikeyboard->Acquire();
if (result != DI_OK)
return 0;

//give the go-ahead
return 1;
}

void Poll_Keyboard()
{
dikeyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys);
}

int Key_Down(int key)
{
return (keys[key] & 0x80);
}

void Kill_Keyboard()
{
if (dikeyboard != NULL)
{
dikeyboard->Unacquire();
dikeyboard->Release();
dikeyboard = NULL;
}
}


I checked the mouse input and it seems that the app just doesn't want to use the mouse. so it's not my code because i've looked at the code from the book and i've followed it pretty closely.

Share this post


Link to post
Share on other sites
Quote:
Original post by Enerjak
I'm using direct input. and the application isn't getting mouse input. What input API would you recommend? also this is my code in my main game.cpp file.

*** Source Snippet Removed ***

and this is the initialization for Direct input.

*** Source Snippet Removed *** I checked the mouse input and it seems that the app just doesn't want to use the mouse. so it's not my code because i've looked at the code from the book and i've followed it pretty closely.
First, don't use DirectInput (For the multitude of reasons in the link in my last post). Just use window messages (WM_KEYUP, WM_KEYDOWN, WM_MOUSEMOVE, WM_LBUTTONDOWN, etc) - they'll be getting sent to your application anyway.
Secondly, this is an extremely bad thing to do to any COM interface, but DirectInput particularly (Because it can return S_FALSE for some calls): if (result != DI_OK). You should always be using the SUCCEEDED and FAILED macros instead.

Share this post


Link to post
Share on other sites
I find it strange that ALOT of books use it. also, how would you get the mouse X,Y,Z? is there like WM_MOUSEX? also i just got Ultimate game programming with DirectX and of course, the CD was missing. Anyways, i appreciate all the help you've given me EvilSteve.

Share this post


Link to post
Share on other sites
Quote:
Original post by Enerjak
I find it strange that ALOT of books use it. also, how would you get the mouse X,Y,Z? is there like WM_MOUSEX? also i just got Ultimate game programming with DirectX and of course, the CD was missing. Anyways, i appreciate all the help you've given me EvilSteve.
I never understood why books cover it either. The only possible use for it is if you need high-DPI mouse input (I.e. you need to be able to detect mouse movements of less than a pixel), and you need to run on Windows 98. All the arguments about DirectInput being faster or more responsive is just lies, DirectInput is actually slower than window messages, because it just spawns another thread in the background to read windows messages.

As for getting the X and Y positions of the mouse, your app window will be sent a WM_MOUSEMOVE message whenever the mouse moves over the window - which contains the cursor's X and Y positions, or you can use GetCursorPos() to get the mouse potision in screen space, then ScreenToClient() to convert that point to a point relative to your client area. The WM_LBUTTONDOWN, etc messages also contain the current cursor position as part of the message (And you should always use that coordinate instead of anything else when processing a mouse click, because otherwise you can get weird laggy input - just like CodeWarrior's IDE does >_<).

For the Z coordinate, there's the WM_MOUSEWHEEL message.

Share this post


Link to post
Share on other sites
So since it passes the WM_MOVE every time the cursor is over the window i should use GetCursorPos(Point) and see if the X and Y are being moved then move the paddle according to the way it's being done?



LPPOINT Point

GetCursorPos(&Point)

if(point.X > 0)
Player_Paddle.y += Player_Paddle.movey;?




Maybe a little code snippert might help me? I'm just trying to make a simple Pong game, is that too much to ask from life? Apparantly so.

Share this post


Link to post
Share on other sites
Hi,
I would suggest taking the time to actually look at these different input methods and leaving the game development out of the equation.

If you are comfortable with winforms development, try and move a button or any control on a form in real time using the mouse as the same principal applies when you are moving a paddle in pong.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
Ok, i'll try. but you have to answer me this: does this code look good to you?



#include "Cor_DirectInput.h"

LPDIRECTINPUT8 Cor_DirectInput;
LPDIRECTINPUTDEVICE8 Cor_Keyboard;
LPDIRECTINPUTDEVICE8 Cor_Mouse;
DIMOUSESTATE2 Cor_MouseState;

char keys[256];

int Cor_DirectInputFunc()
{
HRESULT result = DirectInput8Create(
GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(VOID**)&Cor_DirectInput,
NULL);

if(FAILED(result))
{
MessageBoxA(NULL,"DirectInput didn't initialize","Error",MB_OK);
return 0;
}

result = Cor_DirectInput->CreateDevice(GUID_SysMouse,&Cor_Mouse,NULL);

if(FAILED(result))
{
MessageBoxA(NULL,"The mouse could not be created.","Error",MB_OK);
return 0;
}

result = Cor_DirectInput->CreateDevice(GUID_SysKeyboard,&Cor_Keyboard,NULL);
if(FAILED(result))
{
MessageBoxA(NULL,"The keyboard could notg be initialized","Error",MB_OK);
return 0;
}

return 1;
}

int Cor_InitMouse(HWND hwnd)
{
HRESULT result = Cor_Mouse->SetDataFormat(&c_dfDIMouse);
if(FAILED(result))
{
return 0;
}

result = Cor_Mouse->SetCooperativeLevel(hwnd,
DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);

if(FAILED(result))
{
return 0;
}

result = Cor_Mouse->Acquire();

if(FAILED(result))
{
return 0;
}

return 1;
}

int Cor_InitKeyboard(HWND hwnd)
{
HRESULT result = Cor_Keyboard->SetDataFormat(&c_dfDIKeyboard);
if(FAILED(result))
{
return 0;
}

result = Cor_Keyboard->SetCooperativeLevel(hwnd,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);

if(FAILED(result))
{
return 0;
}

result = Cor_Keyboard->Acquire();
if(FAILED(result))
{
return 0;
}
return 1;
}

int Mouse_X()
{
return Cor_MouseState.lX;
}

int Mouse_Y()
{
return Cor_MouseState.lY;
}

int Mouse_Z()
{
return Cor_MouseState.lZ;
}

int Key_Down(int key)
{
return keys[key] & 0x80;
}

int Mouse_Button(int button)
{
return BUTTON_DOWN(Cor_MouseState,button);
}

void Update_Mouse()
{
Cor_Mouse->GetDeviceState(sizeof(Cor_MouseState),(LPVOID)&Cor_MouseState);
}

void Update_Keyboard()
{
Cor_Keyboard->GetDeviceState(sizeof(keys),(LPVOID)&keys);
}




I know it's the code because the mouse example works fine and the mouse works fine in the things i do. I just want to know if i'm doing something wrong when updating the mouse, or maybe it just doesn't want to update the X and Y pos? I'll try what you guys have said but i just want to know WTH is wrong with this code......

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!