Sign in to follow this  
xM1k3x

A small direct X issue....*(SOLVED)*

Recommended Posts

Hey all I am having a small problem with my program and I was hoping that someone could give me a little insight as too why I am having this problem. So to give you some background I am using C++( although some of the framework is in C) and DirectX 9 March 2008 SDK I have a two files called: dxinput.h and dxinput.cpp. These files handle input for my game using directx function. Ok so far so good. I have another file called game.cpp this is my main game file. Now everything was working great until I added the before mentioned dxinput files( I was using the windows API to get input before). Now my program will start and only show a black screen and nothing, oddly enough if I hit the escape key it will exit just as I programmed it to do in dxinput.cpp So I am confused, when I debug I see that there is an access violation in dxinput.cpp but cant figure out why so I am hoping someone here can: -------------------------------------------- The dxinput.cpp file -------------------------------------------- #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; } } ------------------------------------------- The game.cpp file ------------------------------------------- #include "game.h" LPDIRECT3DTEXTURE9 player_ship; PlayerShip Player; LPDIRECT3DSURFACE9 back; LPD3DXSPRITE sprite_handler; //timing variable long start = GetTickCount(); HRESULT result; //initializes the game int Game_Init(HWND hwnd) { //set random number seed srand(time(NULL)); if(!Init_Mouse(hwnd)) { MessageBox(hwnd, "Error initializing the mouse","Error",MB_OK); } if(!Init_Keyboard(hwnd)) { MessageBox(hwnd, "Error initializing the keyboard","Error",MB_OK); } //create sprite handler object result = D3DXCreateSprite(d3ddev, &sprite_handler); if (result != D3D_OK) return 0; //load the background image back = LoadSurface("cloudbackground2.bmp", NULL); if (back == NULL) return 0; player_ship = LoadTexture("PLAYERSHIP.bmp",D3DCOLOR_XRGB(255,0,255)); if (player_ship == NULL) return 0; Player.x = 270; Player.y = 300; Player.width = 96; Player.height = 96; Player.movex = 5; Player.movey = 5; //return okay return 1; } //the main game loop void Game_Run(HWND hwnd) { //make sure the Direct3D device is valid if (d3ddev == NULL) return; Poll_Mouse(); Poll_Keyboard(); //after short delay, ready for next frame? //this keeps the game running at a steady frame rate if (GetTickCount() - start >= 30) { //reset timing start = GetTickCount(); //move the sprite Player.CallMovement(); } //start rendering if (d3ddev->BeginScene()) { //erase the entire background d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE); //start sprite handler sprite_handler->Begin(D3DXSPRITE_ALPHABLEND); //create vector to update sprite position D3DXVECTOR3 position((float)Player.x, (float)Player.y, 0); //draw the sprite sprite_handler->Draw( player_ship, NULL, NULL, &position, D3DCOLOR_XRGB(255,255,255)); //stop drawing sprite_handler->End(); //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(DIK_ESCAPE)) PostMessage(hwnd, WM_DESTROY, 0, 0); } //frees memory and cleans up before the game ends void Game_End(HWND hwnd) { if (player_ship != NULL) player_ship->Release(); if (back != NULL) back->Release(); if (sprite_handler != NULL) sprite_handler->Release(); } ------------------------------- The debugger shows that the problem is in the dxinput.cpp file in the: int Init_Mouse(HWND hwnd) { //set the data format for mouse input HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse); specifically the HRESULT line. So if anyone could help that would be amazing and I would be very greatful. Thanks [Edited by - xM1k3x on July 2, 2008 3:22:24 PM]

Share this post


Link to post
Share on other sites
1. Looks like you Call Init_Mouse before you ever call Init_DirectInput. Therefore dimouse is uninitialized and points to garbage.

2. You probably don't want to be using DirectInput for Mouse and Keyboard input anyway

3. WM_DESTROY is a notification, not a message. In other words it happens in response to a window being destroyed, rather than actually causing a window to be destroyed. Sending it to a window is a bit like making a thunder-like noise and expecting lighting to strike. To destroy a window, use DestroyWindow.

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
1. Looks like you Call Init_Mouse before you ever call Init_DirectInput. Therefore dimouse is uninitialized and points to garbage.

2. You probably don't want to be using DirectInput for Mouse and Keyboard input anyway

3. WM_DESTROY is a notification, not a message. In other words it happens in response to a window being destroyed, rather than actually causing a window to be destroyed. Sending it to a window is a bit like making a thunder-like noise and expecting lighting to strike. To destroy a window, use DestroyWindow.



Where exactly should I call Init_DirectInput? are you saying in the game.cpp like before:

if (!Init_Mouse)

?

Thanks for the response!

Share this post


Link to post
Share on other sites
Quote:
Original post by xM1k3x
Quote:
Original post by MJP
1. Looks like you Call Init_Mouse before you ever call Init_DirectInput. Therefore dimouse is uninitialized and points to garbage.

2. You probably don't want to be using DirectInput for Mouse and Keyboard input anyway

3. WM_DESTROY is a notification, not a message. In other words it happens in response to a window being destroyed, rather than actually causing a window to be destroyed. Sending it to a window is a bit like making a thunder-like noise and expecting lighting to strike. To destroy a window, use DestroyWindow.



Where exactly should I call Init_DirectInput? are you saying in the game.cpp like before:

if (!Init_Mouse)

?

Thanks for the response!



Ok I put:

if (!Init_DirectInput(hwnd))
{
MessageBox(hwnd, "Error initializing the Direct Input","Error",MB_OK);
}

before the mouse init and now my program works thanks so much for all the help and I will use DestroyWindow instead.

Thanks again I appreciate it!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this