// Gamepad Basics - using a second thread for speed
// This code is more or less the same as the one previously, but if you ran
// it you'd notice a fps of 12-20...which just isn't good enough! If we
// put the gamepad polling code in another thread, and just access the
// data when we need it, we'll get a fixed fps of 25fps.
// Add header file for xinput.h
// Add the library xinput.lib
#include
<openxdk.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
"OpenXDK\include\xinput\xinput.h"
void
DataDisplay(stXPAD * pPad)
{
long xpos=50;
long ypos=105; long height=12;
char bufA[1000];
// Large temp char buffer for our text
sprintf(bufA, "%s", "GamePad One Information Data: by bkenwright@xbdev.net");
vga_print(xpos, ypos+=height, bufA);
ypos+=5;
sprintf(bufA, "structsize: 0x%x", pPad->structsize);
vga_print(xpos, ypos+=height, bufA);
sprintf(bufA, "pad: %d (1 up 2 down 4 left 8 right)", pPad->pad);
vga_print(xpos, ypos+=height, bufA);
for(int i=0;
i<6; i++)
{
sprintf(bufA, "keys[%d]: %d (A B X Y Black White)", i, pPad->keys[i]);
vga_print(xpos, ypos+=height, bufA);
}//
End for loop
sprintf(bufA, "trig_left: 0x%x", pPad->trig_left);
vga_print(xpos, ypos+=height, bufA);
sprintf(bufA, "trig_right: 0x%x", pPad->trig_right);
vga_print( xpos, ypos+=height, bufA);
sprintf(bufA, "stick_left_x: 0x%x", pPad->stick_left_x);
vga_print(xpos, ypos+=height, bufA);
sprintf(bufA, "stick_left_y: 0x%x", pPad->stick_left_y);
vga_print( xpos, ypos+=height, bufA);
sprintf(bufA, "stick_right_x: 0x%x", pPad->stick_right_x);
vga_print(xpos, ypos+=height, bufA);
sprintf(bufA, "stick_right_y: 0x%x", pPad->stick_right_y);
vga_print(xpos, ypos+=height, bufA);
}//
End of DataDisplay(..)
static
void otherthread(PVOID parm1, PVOID parm2)
{
// Small delay loop
for(int i=0;
i<1000; i++)
for(int
j=0; j<1000; j++)
__asm{ nop }
stXINPUT xin;
xInitInput(&xin);
stXPAD * pad = (stXPAD*)parm1;
while(true)
{
// Small delay loop
for(int
i=0; i<1000; i++)
for(int
j=0; j<1000; j++)
__asm{ nop }
xGetPadInput(pad, &xin);
}//
end while loop
}//
end of otherthread(..)
void
XBoxStartup()
{
// We put the init and polling of the gamepad in
another thread!, thats
// what the next few lines of code are for. -
Something to note though,
// is that the frame rate seems to be reduced to
25fps...where before it
// was 50fps..hmmm
stXPAD pad;
HANDLE hThread = 0;
ULONG Id = 0;
LARGE_INTEGER Timeout;
ULONG Status;
Timeout.QuadPart = 0;
PsCreateSystemThreadEx(&hThread, //
ThreadHandle
0,
// ThreadExtraSize
65536,
// KernelStackSize
0,
// TlsDataSize
&Id,
// ThreadId OPTIONAL
(void*)&pad,
// StartContext1
NULL,
// StartContext2
FALSE,
// CreateSuspended
FALSE,
// DebugStack
(PKSTART_ROUTINE)&otherthread); //
StartRoutine
// Our main game/app loop
//let's init the screen
vga_init_mode(MODE_640x480x32); // Some
possible modes all defined in
// xvga.h
//
MODE_320x240x32
//
MODE_640x240x32
//
MODE_640x480x32 etc...plus many more
float last = (float)KeTickCount;
int fps = 0;
// A counter to show somethink ticking away on the
screen
int testcount = 0;
while(1)
{
// Display our gamepad data that we've
read in
DataDisplay( &pad);
// In game reset - both left and right
trigger and the black button
if( (pad.trig_left == 0xFF) && (pad.trig_right
== 0xFF ) && (pad.keys[4]>100) )
break;
// A list of all these functions can
be found at the top of xvga.h
// Comment this line out to watch the
frame rate go up...on my xbox
// I comment this out, and my frame
rate is nearly 1000fps!...wow!
// but of course you can notice the
flicker :-/
vga_vsync();
// Wait for Vertical Blank
vga_flip();
// Flip
vga_clear();
// Clear screen
fps = (int)(1.0f/(((float)KeTickCount
- last) / 1000.0f));
fps = fps*2;
last = (float)KeTickCount;
// Print to screen
char buffer[300];
// large temp text buffer
sprintf(buffer,"OpenXDK Sample: %d fps", fps);
vga_print(50, 50, buffer);
sprintf(buffer, "Dynamic Counter Value: %d", testcount++);
vga_print(50, 75, buffer);
sprintf(buffer, "Press LEFT AND RIGHT TRIGGER then the BLACK
BUTTON to reset");
vga_print(50, 90, buffer);
}//
End while loop
}//End
|