Skip to content

Commit

Permalink
25.1.26
Browse files Browse the repository at this point in the history
[Engine]
* Move all joystick related code to a new 'joystick.hpp' module and tidy up names and clean out redundant code.
* Increase supported buttons from GLFW_BUTTON_LAST+1 to 128 and protect from potential overflow.
* Increase supported axes from GLFW_AXIS_LAST+1 to 8 and protect from overflow.
* Show more information with 'input' console command.
  • Loading branch information
Mhatxotic committed Jan 26, 2025
1 parent 35b98d0 commit 1d78f46
Show file tree
Hide file tree
Showing 9 changed files with 692 additions and 545 deletions.
88 changes: 47 additions & 41 deletions src/conlib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,71 +889,77 @@ cConsole->AddLineA(sTable.Finish(),
/* ========================================================================= */
{ "input", 1, 2, CFL_VIDEO, [](const Args &aArgs){
/* ------------------------------------------------------------------------- */
// Get joysticks data
const JoyList &jlList = cInput->GetConstJoyList();
// Required joystick namespace
using namespace IJoystick::P;
// If argument specified
if(aArgs.size() > 1)
{ // Convert parmeter to number
const size_t stArg = StrToNum<size_t>(aArgs[1]);
if(stArg >= jlList.size())
if(stArg >= cInput->JoyGetCount())
return cConsole->AddLine("Joystick index argument invalid!");
// Get joystick information and return if not connected
const JoyInfo &jiRef = jlList[stArg];
if(jiRef.IsDisconnected())
const JoyInfo &jiRef = cInput->JoyGetConst(stArg);
if(jiRef.JoyIsDisconnected())
return cConsole->AddLine("Joystick is not being polled right now!");
// Build axis state data
Statistic sAxes;
sAxes.Header();
for(size_t stIndex = 0; stIndex < jiRef.GetAxisCount(); ++stIndex)
sAxes.Header(StrFormat("A$$$$", setw(2), right, setfill('0'), stIndex));
// Buffered
sAxes.Data("BUF");
for(size_t stIndex = 0; stIndex < jiRef.GetAxisCount(); ++stIndex)
sAxes.DataN(jiRef.GetAxisState(stIndex));
// Unbuffered
sAxes.Data("RAW");
for(size_t stIndex = 0; stIndex < jiRef.GetAxisCount(); ++stIndex)
sAxes.DataN(jiRef.GetUnbufferedAxisState(stIndex), 1);
// Buffered headers
StdForEach(seq, jiRef.JoyAxisListBegin(), jiRef.JoyAxisListEnd(),
[&sAxes](const JoyAxisInfo &jaiRef) { sAxes.Header(StrFormat("BA$$$$",
setw(2), right, setfill('0'), jaiRef.AxisGetId()), true); });
// Unbuffered headers
StdForEach(seq, jiRef.JoyAxisListBegin(), jiRef.JoyAxisListEnd(),
[&sAxes](const JoyAxisInfo &jaiRef) { sAxes.Header(StrFormat("UA$$$$",
setw(2), right, setfill('0'), jaiRef.AxisGetId()), true); });
// Reserve memory for data entries
sAxes.Reserve(jiRef.JoyAxisListCount() * 2);
// Buffered state
StdForEach(seq, jiRef.JoyAxisListBegin(), jiRef.JoyAxisListEnd(),
[&sAxes](const JoyAxisInfo &jaiRef)
{ sAxes.DataN(jaiRef.AxisGetBufferedState()); });
// Unbuffered state
StdForEach(seq, jiRef.JoyAxisListBegin(), jiRef.JoyAxisListEnd(),
[&sAxes](const JoyAxisInfo &jaiRef)
{ sAxes.DataN(jaiRef.AxisGetUnbufferedState(), 1); });
// Build button state data
Statistic sButtons;
sButtons.Header();
for(size_t stIndex = 0; stIndex < jiRef.GetButtonCount(); ++stIndex)
sButtons.Header(StrFormat("B$$$$", setw(2), right, setfill('0'), stIndex));
// Buffered
sButtons.Data("BUF");
for(size_t stIndex = 0; stIndex < jiRef.GetButtonCount(); ++stIndex)
sButtons.DataN(jiRef.GetButtonState(stIndex));
// Unbuffered
sButtons.Data("RAW");
for(size_t stIndex = 0; stIndex < jiRef.GetButtonCount(); ++stIndex)
sButtons.DataN(jiRef.GetUnbufferedButtonState(stIndex));
// Buffered headers
StdForEach(seq, jiRef.JoyButtonListBegin(), jiRef.JoyButtonListEnd(),
[&sButtons](const JoyButtonInfo &jbiRef)
{ sButtons.Header(StrFormat("B$$$$",
setw(2), right, setfill('0'), jbiRef.ButtonGetId()), true); });
// Reserve memory for data entries
sButtons.Reserve(jiRef.JoyButtonListCount());
// Buffered state
StdForEach(seq, jiRef.JoyButtonListBegin(), jiRef.JoyButtonListEnd(),
[&sButtons](const JoyButtonInfo &jbiRef)
{ sButtons.DataN(jbiRef.ButtonGetState()); });
// Dump to console output and return
return cConsole->AddLineF("$$Data for $ '$' at index $.", sAxes.Finish(),
sButtons.Finish(), jiRef.GetGamepadOrJoystickString(), jiRef.IdentGet(),
sButtons.Finish(), jiRef.JoyGetGamepadOrJoystickString(), jiRef.IdentGet(),
stArg);
} // Joysticks connected
size_t stConnected = 0;
// Make a table to automatically format our data neatly
} // Make a table to automatically format our data neatly
Statistic sTable;
sTable.Header("ID").Header("FL").Header("AX").Header("BT")
.Header("NAME", false).Reserve(jlList.size());
.Header("GUID", false).Header("NAME", false)
.Header("IDENT", false).Reserve(cInput->JoyGetCount());
// For each joystick
const JoyList &jlList = cInput->JoyListGetConst();
for(const JoyInfo &jiRef : jlList)
{ // If joystick is connected
if(jiRef.IsDisconnected())
if(jiRef.JoyIsDisconnected())
{ // If joystick name was empty then ignore it. Still show disconnected ones
if(jiRef.IdentGet().empty()) continue;
} // Connected
else ++stConnected;
// Store data
sTable.DataN(jiRef.GetId()).Data(StrFromEvalTokens({
{ jiRef.FlagIsSet(JF_GAMEPAD), 'G' },
{ jiRef.FlagIsSet(JF_CONNECTED), 'C' }
})).DataN(jiRef.GetAxisCount()).DataN(jiRef.GetButtonCount())
} // Store data
sTable.DataN(jiRef.JoyGetId()).Data(StrFromEvalTokens({
{ jiRef.JoyIsGamepad(), 'G' }, { jiRef.JoyIsConnected(), 'C' }
})).DataN(jiRef.JoyAxisListCount()).DataN(jiRef.JoyButtonListCount())
.Data(jiRef.JoyGUID()).Data(jiRef.JoyGamePadName())
.Data(jiRef.IdentGet());
} // Print totals
cConsole->AddLineF("$$ connected ($ supported).", sTable.Finish(),
StrCPluraliseNum(stConnected, "input", "inputs"), jlList.size());
StrCPluraliseNum(cInput->JoyGetConnected(), "input", "inputs"),
jlList.size());
/* ------------------------------------------------------------------------- */
} }, // End of 'input' function
/* ========================================================================= */
Expand Down
2 changes: 1 addition & 1 deletion src/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Core final : // Members initially private
{ // Set main fbo by default on each frame
cFboCore->ActivateMain();
// Poll joysticks
cInput->PollJoysticks();
cInput->JoyPoll();
// Execute a tick for each frame missed
cLua->ExecuteMain();
// Break if we've caught up
Expand Down
1 change: 1 addition & 0 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace E { // Start of engine namespace
#include "file.hpp" // FStream+FileMap class header
#include "clip.hpp" // Clipboard class header
#include "congraph.hpp" // Console rendering class header
#include "joystick.hpp" // Joystick structure class header
#include "input.hpp" // Input handling class header
#include "display.hpp" // Window handling class header
#include "mask.hpp" // BitMask system header
Expand Down
10 changes: 5 additions & 5 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#define VER_AUTHOR "Mhatxotic Design" // Author of engine
#define VER_MAJOR 25 // Version major (year)
#define VER_MINOR 1 // Version minor (month)
#define VER_BUILD 25 // Version build (day)
#define VER_REV 1 // Version rev (build#)
#define VER_STR_NQ 25,1,25,1 // Version as literal
#define VER_STR "25.1.25.1" // Version as string
#define VER_DATE "Sat Jan 25 00:32:33 2025 +0000" // Compilation date
#define VER_BUILD 26 // Version build (day)
#define VER_REV 24 // Version rev (build#)
#define VER_STR_NQ 25,1,26,24 // Version as literal
#define VER_STR "25.1.26.24" // Version as string
#define VER_DATE "Sun Jan 26 00:26:37 2025 +0000" // Compilation date
/* == EoF =========================================================== EoF == */
12 changes: 12 additions & 0 deletions src/glfwutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ static const unsigned char *GlFWGetJoystickButtons(int iJ, int &iJAB)
/* -- Joystick other ------------------------------------------------------- */
static const char *GlFWGetJoystickName(const int iJ)
{ return glfwGetJoystickName(iJ); }
/* -- Joystick is actually a game controller ------------------------------- */
static bool GlFWJoystickIsGamepad(const int iJ)
{ return glfwJoystickIsGamepad(iJ); }
/* -- Get gamepad name ----------------------------------------------------- */
const char *GlFWGetGamepadName(const int iJ)
{ return glfwGetGamepadName(iJ); }
/* -- Get joystick unique identification number ---------------------------- */
const char *GlFWGetJoystickGUID(const int iJ)
{ return glfwGetJoystickGUID(iJ); }
/* -- Return if joystick is present ---------------------------------------- */
bool GlFWJoystickPresent(const int iJ)
{ return glfwJoystickPresent(iJ); }
/* -- Set swap interval ---------------------------------------------------- */
static void GlFWSetVSync(const int iI) { glfwSwapInterval(iI); }
/* -- Wait for window event ------------------------------------------------ */
Expand Down
Loading

0 comments on commit 1d78f46

Please sign in to comment.