Awesome code WickedX, kudos for you, I wish I could give you rep, thanks a lot
. I noticed dbSetJoystickDeadzone is not present here, well Im using GDK 1.0 or I think its this version, in any case its previous to 2.0, I checked the definition file for the input library and that function wasnt there and found no other function similar, well not that I think. So I did my best to limit the range of the values for left stick as I understood watching your code and explanation that the deadzone should be set so that when stick is in center or almost, the program reacts as nothing is being pushed. Also I did an extra condition for right stick because it was perfect but maybe my stick has a wider deadzone so sometimes it it showed a direction when coming back to center from other, so I think it works now for mine too.
However there is something that goes around my mind... is this code more or less standard for all twin sticks we can find in any controller? or we have to create different solutions for each gamepad? I dont have other gamepad here to check but as soon as I can I will try it of course but I bet you already know the answer to this.
Forgot to mention that there are some magical numbers, I just did many trial and error until the center status was correctly detected.
#include "DarkGDK.h"
#include "GlobStruct.h"
int dWidth, dHeight;
int dHWidth, dQWidth;
int rDeadZone;
int lDeadZone;
int cdFlag;
char* dName;
int RightStickY(void)
{
int Result;
Result = float(dbJoystickTwistZ())/32.767f-1000.0f;
if(dbAbs(Result)<=200)
{
Result=0;
}
return Result;
}
bool RightStickUp()
{
bool Result;
Result = false;
if (float(RightStickY())<=-1000.0f*float(rDeadZone)/100.0f)
Result = true;
return Result;
}
bool RightStickDown()
{
bool Result;
Result = false;
if (float(RightStickY())>=1000.0f*float(rDeadZone)/100.0f)
Result = true;
return Result;
}
bool RightStickLeft()
{
bool Result;
Result = false;
if (float(dbJoystickZ())<=-1000.0f*float(rDeadZone)/100.0f)
Result = true;
return Result;
}
bool RightStickRight()
{
bool Result;
Result = false;
if (float(dbJoystickZ())>=1000.0f*float(rDeadZone)/100.0f)
Result = true;
return Result;
}
void DarkGDK()
{
// Set fullscreen windowed at desktop resolution.
dWidth = dbDesktopWidth();
dHeight = dbDesktopHeight();
dHWidth = dWidth/2;
dQWidth = dWidth/4;
dbSetDisplayMode(dWidth, dHeight, dbScreenDepth());
dbMaximizeWindow();
dbSetWindowLayout(0, 0, 1);
// This sets the deadzone percentage for the funtions dbJoystickUp, dbJoystickDown, dbJoystickLeft
// and dbJoystickRight. The functions dbJoyStick{X, Y, Z} return a value between -1000 and 1000.
// If we set the deadzone to 1 this cause the dbJoystickUp not to be set until the dbJoystickY
// value reaches -10 (-1000*0.01).
//dbSetJoystickDeadzone(5);
rDeadZone = 20;
lDeadZone=200;
// Get number of installed control devices
dbPerformChecklistControlDevices();
cdFlag = dbChecklistQuantity();
dbEmptyChecklist();
// If control device is installed, get default device name.
if (cdFlag) {
dName = dbGetControlDevice();
}
dbSyncOn();
dbSyncRate(60);
dbBackdropOn();
dbColorBackdrop(0);
while (LoopGDK())
{
if (cdFlag) {
dbCenterText(dHWidth, 0, dName);
dbCenterText(dQWidth, 32, "Left Joystick");
if (dbJoystickUp() && dbAbs(dbJoystickY())>=lDeadZone)
{
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dQWidth, 64, "UP");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dQWidth, 64, "UP");
}
if (dbJoystickDown() && dbAbs(dbJoystickY())>=lDeadZone)
{
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dQWidth, 128, "DOWN");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dQWidth, 128, "DOWN");
}
if (dbJoystickLeft() && dbAbs(dbJoystickX())>=lDeadZone)
{
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dQWidth-32, 96, "LEFT");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dQWidth-32, 96, "LEFT");
}
if (dbJoystickRight() && dbAbs(dbJoystickX())>=lDeadZone)
{
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dQWidth+32, 96, "RIGHT");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dQWidth+32, 96, "RIGHT");
}
dbText(dWidth/4-128, 192, "Left Joystick X:");
dbSetCursor(dQWidth+8, 192);
dbPrint((LONGLONG)dbJoystickX());
dbText(dQWidth-128, 224, "Left Joystick Y:");
dbSetCursor(dQWidth+8, 224);
dbPrint((LONGLONG)dbJoystickY());
dbCenterText(dHWidth+dQWidth, 32, "Right Joystick");
if (RightStickUp()) {
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dHWidth+dQWidth, 64, "UP");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dHWidth+dQWidth, 64, "UP");
}
if (RightStickDown()) {
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dHWidth+dQWidth, 128, "DOWN");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dHWidth+dQWidth, 128, "DOWN");
}
if (RightStickLeft()) {
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dHWidth+dQWidth-32, 96, "LEFT");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dHWidth+dQWidth-32, 96, "LEFT");
}
if (RightStickRight()) {
g_pGlob->dwForeColor = 0xFFFF0000;
dbCenterText(dHWidth+dQWidth+32, 96, "RIGHT");
g_pGlob->dwForeColor = 0xFFFFFFFF;
}
else {
dbCenterText(dHWidth+dQWidth+32, 96, "RIGHT");
}
dbText(dHWidth+dQWidth-136, 192, "Right Joystick X:");
dbSetCursor(dHWidth+dQWidth+8, 192);
dbPrint((LONGLONG)dbJoystickZ());
dbText(dHWidth+dQWidth-136, 224, "Right Joystick Y:");
dbSetCursor(dHWidth+dQWidth+8, 224);
dbPrint((LONGLONG)RightStickY());
}
else {
dbSetCursor(0, 0);
dbPrint("No control device installed.");
}
dbSync();
}
SAFE_DELETE_ARRAY(dName);
return;
}