Archived

This topic is now archived and is closed to further replies.

omegasyphon

directinput problem

Recommended Posts

when i go to run my program i get an unhandled exception from this line how would i correct this? when i debug, it goes to the GetDeviceState line.
    

int keycontrol(GLfloat x)
{

	HRESULT hr = lpdikey->GetDeviceState(sizeof(buffer),&buffer);

		if (KEYDOWN(buffer,DIK_ESCAPE))
		PostQuitMessage(0);
		if (KEYDOWN(buffer,DIK_LEFT))
			x -= .5f;
		if (KEYDOWN(buffer,DIK_RIGHT))
			x += 0.5f;

	return x;
}

}

  
Edited by - omegasyphon on June 26, 2001 4:10:46 PM

Share this post


Link to post
Share on other sites
u forst me to do this , mycode is pretty short tho

  
#define INITGUID
#include <windows.h>
#include <fstream.h>
#include <glut.h>
#include <stdio.h>
#include <math.h>
#include <objbase.h>
#include "dinput.h"
#include "vars.h"

#define glpi 3.1415f

GLfloat xrot,yrot;
GLint bx=0,by=0;
int mstate = 0;

LPDIRECTINPUTDEVICE7 lpdikey = NULL;
LPDIRECTINPUT7 lpdi = NULL;
UCHAR buffer[256];


HWND main_window_handle = NULL; // save the window handle

HINSTANCE main_instance = NULL; // globally track hinstance




int initcontrols()
{
if (DirectInputCreateEx(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput7,(void**)&lpdi,NULL)!=DI_OK)
return 0;
if (lpdi->CreateDeviceEx(GUID_SysKeyboard,IID_IDirectInputDevice7,(void**)&lpdikey,NULL)!=DI_OK)
return 0;
if (lpdikey->SetCooperativeLevel(main_window_handle,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)!=DI_OK)
return 0;
if (lpdikey->SetDataFormat(&c_dfDIKeyboard)!=DI_OK)
return 0;
lpdikey->Acquire();
mstate=1;
return 1;
}

int keycontrol(GLfloat x)
{

if (lpdikey->GetDeviceState(256,buffer)!=DI_OK)
return 0;

if (buffer[DIK_ESCAPE] & 0x80)
PostQuitMessage(0);
if (buffer[DIK_LEFT] & 0x80)
x -= .5f;
if (buffer[DIK_RIGHT] & 0x80)
x += 0.5f;

return x;
}

int killkeyboard()
{
// first unacquire the mouse

lpdikey->Unacquire();

// now release the mouse

lpdikey->Release();

// release directinput

lpdi->Release();
return 1;
}

void renderscene()
{
GLfloat x;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glRotatef(x,1.0f,0.0f,0.0f);
glRotatef(30.0f,0.0f,0.0f,1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,cords);
glDrawElements(GL_TRIANGLE_STRIP,1600,GL_UNSIGNED_INT,corners);
glPopMatrix();
glutSwapBuffers();
}

void setuprc()
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
glColor3f(0.0f,1.0f,0.0f);
}

void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;

// Prevent a divide by zero

if(h == 0)
h = 1;

// Set Viewport to window dimensions

glViewport(0, 0, w, h);

// Reset projection matrix stack

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)

if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


void main()
{
GLfloat x=0.0;
if (mstate==0)
initcontrols();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("game engine console");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(renderscene);
setuprc();
keycontrol(x);
killkeyboard();
glutMainLoop();
}

Share this post


Link to post
Share on other sites
Is buffer created:

char buffer[256];

of char* buffer = new char[256] ?

If it is the latter then sizeof( buffer ) is gong to evaluate to the size of a pointer and not the size of your buffer.


Seeya
Krippy

Edited by - krippy2k on June 29, 2001 11:55:25 AM

Share this post


Link to post
Share on other sites
Are you positive that lpdikey is a valid DI pointer when you get to this code?

What does the exception say? Access violation?

Share this post


Link to post
Share on other sites
Ok, 0xc0000005 is an access violation. It means that you are trying to access memory that you do not have permission to access.

Either lpdikey or buffer are not properly initialized before this call. I would be willing to bet that it is lpdikey. Post the code that you use to initialize lpdikey.

Seeya
Krippy

Share this post


Link to post
Share on other sites
this is what is initialized as the lpdikey

  
if (lpdi->CreateDeviceEx(GUID_SysKeyboard,IID_IDirectInputDevice7,(void**)&lpdikey,NULL))
return (false);

Share this post


Link to post
Share on other sites
Hmm not sure...
In your code, you use main_instance and main_window_handle for initialization.
But, you init controls (You call init_controls()) before you even create the window ( glutCreateWindow("game engine console"); ).
So what''s the deal?


- Goblineye Entertainment
The road to success is always under construction

Share this post


Link to post
Share on other sites
Ok I must have been tired when I read this thread last. I thought that full code up there was posted by somebody else as an example so I didn''t really look at it. lol. Oh well.

I see what the problem is now. The reason lpdikey is NULL is because you will never even try to create it. Your first call to:
if (DirectInputCreateEx(main_instance,DIRECTINPUT_VERSION,IID_IDirectInput7,(void**)&lpdi,NULL)!=DI_OK)
return 0;

Is going to fail every time. The reason is you are passing ''main_instance'' to it and main_instance is never initialized. Nor will it likely ever be initialized in this application because you are pretty much creating a console application and using glut. I''m not sure if it is possible to retrieve an HINSTANCE this way.

I think you should take a look at NeHe''s tutorials Here and see how he initializes his window. The WinMain function will receive an HINSTANCE which you need to copy to main_instance before calling initcontrols().

Using glut in main() makes things a little more portable, but the problem you are having is with DirectX which isn''t portable at all anyways and is not meant to work that way.

Seeya
Krippy

Share this post


Link to post
Share on other sites
Well you can use DX and Glut at the same time, but it must be a Win32 application for DX to work. If you are using MSVC create your project as a Win32 Application. Other compiler I''m not really sure what you need to do, but it should involve using WinMain instead of main().

Share this post


Link to post
Share on other sites