mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
build 191212. gamepad input configuration support, minor bug fixes
This commit is contained in:
@@ -6844,7 +6844,7 @@ TDynamicObject::powertrain_sounds::render( TMoverParameters const &Vehicle, doub
|
||||
volume = 0.0;
|
||||
if( false == motors.empty() ) {
|
||||
|
||||
if( std::abs( Vehicle.enrot ) > 0.01 ) {
|
||||
if( std::abs( Vehicle.nrot ) > 0.01 ) {
|
||||
|
||||
auto const &motor { motors.front() };
|
||||
// frequency calculation
|
||||
|
||||
24
Texture.cpp
24
Texture.cpp
@@ -745,7 +745,6 @@ opengl_texture::bind(size_t unit) {
|
||||
|
||||
if (units[unit] == id)
|
||||
return true;
|
||||
|
||||
if (GLAD_GL_ARB_direct_state_access)
|
||||
{
|
||||
glBindTextureUnit(unit, id);
|
||||
@@ -759,7 +758,6 @@ opengl_texture::bind(size_t unit) {
|
||||
}
|
||||
glBindTexture(target, id);
|
||||
}
|
||||
|
||||
units[unit] = id;
|
||||
|
||||
return true;
|
||||
@@ -782,7 +780,6 @@ opengl_texture::unbind(size_t unit)
|
||||
//todo: for other targets
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
units[unit] = 0;
|
||||
}
|
||||
|
||||
@@ -1115,22 +1112,13 @@ opengl_texture::flip_vertical() {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
texture_manager::assign_units( GLint const Helper, GLint const Shadows, GLint const Normals, GLint const Diffuse ) {
|
||||
|
||||
m_units[ 0 ].unit = Helper;
|
||||
m_units[ 1 ].unit = Shadows;
|
||||
m_units[ 2 ].unit = Normals;
|
||||
m_units[ 3 ].unit = Diffuse;
|
||||
}
|
||||
|
||||
void
|
||||
texture_manager::unit( GLint const Textureunit ) {
|
||||
|
||||
if( m_activeunit == Textureunit ) { return; }
|
||||
if( opengl_texture::m_activeunit == Textureunit ) { return; }
|
||||
|
||||
m_activeunit = Textureunit;
|
||||
::glActiveTexture( Textureunit );
|
||||
opengl_texture::m_activeunit = Textureunit;
|
||||
::glActiveTexture( GL_TEXTURE0 + Textureunit );
|
||||
}
|
||||
|
||||
// ustalenie numeru tekstury, wczytanie jeśli jeszcze takiej nie było
|
||||
@@ -1231,6 +1219,8 @@ texture_manager::create( std::string Filename, bool const Loadnow, GLint Formath
|
||||
void
|
||||
texture_manager::bind( std::size_t const Unit, texture_handle const Texture ) {
|
||||
|
||||
if( Unit == -1 ) { return; } // no texture unit, nothing to bind the texture to
|
||||
|
||||
if (Texture != null_handle)
|
||||
mark_as_used(Texture).bind(Unit);
|
||||
else
|
||||
@@ -1262,8 +1252,8 @@ void
|
||||
texture_manager::update() {
|
||||
|
||||
if( m_garbagecollector.sweep() > 0 ) {
|
||||
for( auto &unit : m_units ) {
|
||||
unit.texture = -1;
|
||||
for( auto &unit : opengl_texture::units ) {
|
||||
unit = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
Texture.h
12
Texture.h
@@ -61,6 +61,7 @@ struct opengl_texture {
|
||||
|
||||
GLenum target = GL_TEXTURE_2D;
|
||||
static std::array<GLuint, gl::MAX_TEXTURES + 2> units;
|
||||
static GLint m_activeunit;
|
||||
|
||||
private:
|
||||
// methods
|
||||
@@ -96,8 +97,6 @@ private:
|
||||
static std::unordered_map<GLint, int> precompressed_formats;
|
||||
static std::unordered_map<GLint, GLint> drivercompressed_formats;
|
||||
static std::unordered_map<GLint, std::unordered_map<GLint, GLint>> mapping;
|
||||
|
||||
static GLint m_activeunit;
|
||||
};
|
||||
|
||||
typedef int texture_handle;
|
||||
@@ -108,8 +107,6 @@ public:
|
||||
texture_manager();
|
||||
~texture_manager() { delete_textures(); }
|
||||
|
||||
void
|
||||
assign_units( GLint const Helper, GLint const Shadows, GLint const Normals, GLint const Diffuse );
|
||||
// activates specified texture unit
|
||||
void
|
||||
unit( GLint const Textureunit );
|
||||
@@ -141,11 +138,6 @@ private:
|
||||
|
||||
typedef std::unordered_map<std::string, std::size_t> index_map;
|
||||
|
||||
struct texture_unit {
|
||||
GLint unit { 0 };
|
||||
texture_handle texture { null_handle }; // current (most recently bound) texture
|
||||
};
|
||||
|
||||
// methods:
|
||||
// checks whether specified texture is in the texture bank. returns texture id, or npos.
|
||||
texture_handle
|
||||
@@ -161,8 +153,6 @@ private:
|
||||
texturetimepointpair_sequence m_textures;
|
||||
index_map m_texturemappings;
|
||||
garbage_collector<texturetimepointpair_sequence> m_garbagecollector { m_textures, 600, 60, "texture" };
|
||||
std::array<texture_unit, 4> m_units;
|
||||
GLint m_activeunit { 0 };
|
||||
};
|
||||
|
||||
// reduces provided data image to half of original size, using basic 2x2 average
|
||||
|
||||
534
gamepadinput.cpp
534
gamepadinput.cpp
@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "Logs.h"
|
||||
#include "Timer.h"
|
||||
#include "utilities.h"
|
||||
#include "parser.h"
|
||||
|
||||
glm::vec2 circle_to_square( glm::vec2 const &Point, int const Roundness = 0 ) {
|
||||
|
||||
@@ -43,20 +44,11 @@ glm::vec2 circle_to_square( glm::vec2 const &Point, int const Roundness = 0 ) {
|
||||
return interpolate( Point, squared, factor );
|
||||
}
|
||||
|
||||
gamepad_input::gamepad_input() {
|
||||
|
||||
m_modecommands = {
|
||||
|
||||
{ user_command::mastercontrollerincrease, user_command::mastercontrollerdecrease },
|
||||
{ user_command::trainbrakedecrease, user_command::trainbrakeincrease },
|
||||
{ user_command::secondcontrollerincrease, user_command::secondcontrollerdecrease },
|
||||
{ user_command::independentbrakedecrease, user_command::independentbrakeincrease }
|
||||
};
|
||||
}
|
||||
|
||||
bool
|
||||
gamepad_input::init() {
|
||||
|
||||
m_inputaxes.clear();
|
||||
m_inputbuttons.clear();
|
||||
// NOTE: we're only checking for joystick_1 and rely for it to stay connected throughout.
|
||||
// not exactly flexible, but for quick hack it'll do
|
||||
auto const name = glfwGetJoystickName( GLFW_JOYSTICK_1 );
|
||||
@@ -67,18 +59,18 @@ gamepad_input::init() {
|
||||
else {
|
||||
// no joystick,
|
||||
WriteLog( "No gamepad detected" );
|
||||
m_axes.clear();
|
||||
m_buttons.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
int count;
|
||||
|
||||
glfwGetJoystickAxes( m_deviceid, &count );
|
||||
m_axes.resize( count );
|
||||
m_inputaxes.assign( count, { 0.0f, 0.0f, {} } );
|
||||
|
||||
glfwGetJoystickButtons( m_deviceid, &count );
|
||||
m_buttons.resize( count );
|
||||
m_inputbuttons.assign( count, { GLFW_RELEASE, -1, user_command::none } );
|
||||
|
||||
recall_bindings();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -92,222 +84,414 @@ gamepad_input::poll() {
|
||||
return;
|
||||
}
|
||||
|
||||
int count; std::size_t idx = 0;
|
||||
int count;
|
||||
std::size_t idx = 0;
|
||||
// poll button state
|
||||
auto const buttons = glfwGetJoystickButtons( m_deviceid, &count );
|
||||
if( count ) { // safety check in case joystick gets pulled out
|
||||
for( auto &button : m_buttons ) {
|
||||
for( auto &button : m_inputbuttons ) {
|
||||
|
||||
if( button != buttons[ idx ] ) {
|
||||
if( button.state != buttons[ idx ] ) {
|
||||
// button pressed or released, both are important
|
||||
on_button(
|
||||
static_cast<gamepad_button>( idx ),
|
||||
idx,
|
||||
( buttons[ idx ] == 1 ?
|
||||
GLFW_PRESS :
|
||||
GLFW_RELEASE ) );
|
||||
}
|
||||
else {
|
||||
// otherwise we only pass info about button being held down
|
||||
if( button == 1 ) {
|
||||
if( button.state == GLFW_PRESS ) {
|
||||
|
||||
on_button(
|
||||
static_cast<gamepad_button>( idx ),
|
||||
idx,
|
||||
GLFW_REPEAT );
|
||||
}
|
||||
}
|
||||
button = buttons[ idx ];
|
||||
button.state = buttons[ idx ];
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
|
||||
// poll axes state
|
||||
idx = 0;
|
||||
glm::vec2 leftstick, rightstick, triggers;
|
||||
auto const axes = glfwGetJoystickAxes( m_deviceid, &count );
|
||||
if( count ) {
|
||||
// safety check in case joystick gets pulled out
|
||||
if( count >= 2 ) {
|
||||
leftstick = glm::vec2(
|
||||
( std::abs( axes[ gamepad_axes::leftstick_x ] ) > m_deadzone ?
|
||||
axes[ gamepad_axes::leftstick_x ] :
|
||||
0.0f ),
|
||||
( std::abs( axes[ gamepad_axes::leftstick_y ] ) > m_deadzone ?
|
||||
axes[ gamepad_axes::leftstick_y ] :
|
||||
0.0f ) );
|
||||
}
|
||||
if( count >= 4 ) {
|
||||
rightstick = glm::vec2(
|
||||
( std::abs( axes[ gamepad_axes::rightstick_x ] ) > m_deadzone ?
|
||||
axes[ gamepad_axes::rightstick_x ] :
|
||||
0.0f ),
|
||||
( std::abs( axes[ gamepad_axes::rightstick_y ] ) > m_deadzone ?
|
||||
axes[ gamepad_axes::rightstick_y ] :
|
||||
0.0f ) );
|
||||
}
|
||||
if( count >= 6 ) {
|
||||
triggers = glm::vec2(
|
||||
( axes[ gamepad_axes::lefttrigger ] > m_deadzone ?
|
||||
axes[ gamepad_axes::lefttrigger ] :
|
||||
0.0f ),
|
||||
( axes[ gamepad_axes::righttrigger ] > m_deadzone ?
|
||||
axes[ gamepad_axes::righttrigger ] :
|
||||
0.0f ) );
|
||||
for( auto &axis : m_inputaxes ) {
|
||||
axis.state = axes[ idx ];
|
||||
++idx;
|
||||
}
|
||||
}
|
||||
process_axes( leftstick, rightstick, triggers );
|
||||
process_axes();
|
||||
}
|
||||
|
||||
void
|
||||
gamepad_input::on_button( gamepad_button const Button, int const Action ) {
|
||||
gamepad_input::bind( std::vector< std::reference_wrapper<user_command> > &Targets, cParser &Input, std::unordered_map<std::string, user_command> const &Translator, std::string const Point ) {
|
||||
|
||||
switch( Button ) {
|
||||
// NOTE: this is rigid coupling, down the road we should support more flexible binding of functions with buttons
|
||||
case gamepad_button::a:
|
||||
case gamepad_button::b:
|
||||
case gamepad_button::x:
|
||||
case gamepad_button::y: {
|
||||
for( auto &bindingtarget : Targets ) {
|
||||
// grab command(s) associated with the input pin
|
||||
auto const bindingcommandname{ Input.getToken<std::string>() };
|
||||
if( true == bindingcommandname.empty() ) {
|
||||
// no tokens left, may as well complain...
|
||||
WriteLog( "Gamepad binding for " + Point + " didn't specify associated command(s)" );
|
||||
// ...can't quit outright though, as provided references are likely to be unitialized
|
||||
bindingtarget.get() = user_command::none;
|
||||
continue;
|
||||
}
|
||||
auto const commandlookup = Translator.find( bindingcommandname );
|
||||
if( commandlookup == Translator.end() ) {
|
||||
WriteLog( "Gamepad binding for " + Point + " specified unknown command, \"" + bindingcommandname + "\"" );
|
||||
bindingtarget.get() = user_command::none;
|
||||
}
|
||||
else {
|
||||
bindingtarget.get() = commandlookup->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( Action == GLFW_RELEASE ) {
|
||||
// TODO: send GLFW_RELEASE for whatever command could be issued by the mode active until now
|
||||
// if the button was released the stick switches to control the movement
|
||||
m_mode = control_mode::entity;
|
||||
// zero the stick and the accumulator so the input won't bleed between modes
|
||||
m_leftstick = glm::vec2();
|
||||
m_modeaccumulator = 0.0f;
|
||||
bool
|
||||
gamepad_input::recall_bindings() {
|
||||
|
||||
cParser bindingparser( "eu07_input-gamepad.ini", cParser::buffer_FILE );
|
||||
if( false == bindingparser.ok() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// build helper translation tables
|
||||
std::unordered_map<std::string, user_command> nametocommandmap;
|
||||
std::size_t commandid = 0;
|
||||
for( auto const &description : simulation::Commands_descriptions ) {
|
||||
nametocommandmap.emplace(
|
||||
description.name,
|
||||
static_cast<user_command>( commandid ) );
|
||||
++commandid;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, input_type> nametotypemap {
|
||||
{ "3state", input_type::threestate },
|
||||
{ "value", input_type::value },
|
||||
{ "value_invert", input_type::value_invert } };
|
||||
|
||||
// NOTE: to simplify things we expect one entry per line, and whole entry in one line
|
||||
while( true == bindingparser.getTokens( 1, true, "\n" ) ) {
|
||||
|
||||
std::string bindingentry;
|
||||
bindingparser >> bindingentry;
|
||||
cParser entryparser( bindingentry );
|
||||
|
||||
if( false == entryparser.getTokens( 1, true, "\n\r\t " ) ) { continue; }
|
||||
|
||||
std::string bindingpoint {};
|
||||
entryparser >> bindingpoint;
|
||||
auto const splitbindingpoint { split_index( bindingpoint ) };
|
||||
|
||||
if( splitbindingpoint.first == "axis" ) {
|
||||
// one or more sets of: [modeIDX] input type, parameters
|
||||
// [optional] modeIDX associates the set with control mode IDX
|
||||
// input types:
|
||||
// -- range commandname IDX; axis value is passed as paramIDX of commandname
|
||||
// -- 3state commandname commandname; positive axis value issues first commandname, negative value issues second commandname
|
||||
|
||||
auto const axisindex { splitbindingpoint.second };
|
||||
// sanity check, connected gamepad isn't guaranteed to have that many axes
|
||||
if( axisindex >= m_inputaxes.size() ) { continue; }
|
||||
|
||||
int controlmode { -1 }; // unless stated otherwise the set will be associated with the default control mode
|
||||
|
||||
while( true == entryparser.getTokens( 1, true, "\n\r\t " ) ) {
|
||||
|
||||
std::string key {};
|
||||
entryparser >> key;
|
||||
// check for potential mode indicator
|
||||
auto const splitkey { split_index( key ) };
|
||||
if( splitkey.first == "mode" ) {
|
||||
// indicate we'll be processing specified mode
|
||||
controlmode = splitkey.second;
|
||||
continue;
|
||||
}
|
||||
// if we're still here handle the original key as binding type
|
||||
std::string const bindingtypename { key };
|
||||
auto const typelookup = nametotypemap.find( bindingtypename );
|
||||
if( typelookup == nametotypemap.end() ) {
|
||||
|
||||
WriteLog( "Gamepad binding for " + bindingpoint + " specified unknown control type, \"" + bindingtypename + "\"" );
|
||||
}
|
||||
else {
|
||||
|
||||
std::vector< std::reference_wrapper<user_command> > bindingtargets;
|
||||
auto const bindingtype { typelookup->second };
|
||||
std::get<0>( m_inputaxes[ axisindex ].bindings[ controlmode ] ) = bindingtype;
|
||||
// retrieve regular commands associated with the axis and mode
|
||||
switch( bindingtype ) {
|
||||
case input_type::value:
|
||||
case input_type::value_invert: {
|
||||
bindingtargets.emplace_back( std::ref( std::get<1>( m_inputaxes[ axisindex ].bindings[ controlmode ] ) ) );
|
||||
break;
|
||||
}
|
||||
case input_type::threestate: {
|
||||
bindingtargets.emplace_back( std::ref( std::get<1>( m_inputaxes[ axisindex ].bindings[ controlmode ] ) ) );
|
||||
bindingtargets.emplace_back( std::ref( std::get<2>( m_inputaxes[ axisindex ].bindings[ controlmode ] ) ) );
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
bind( bindingtargets, entryparser, nametocommandmap, bindingpoint );
|
||||
// handle potential remaining input type-specific parameters
|
||||
switch( bindingtype ) {
|
||||
case input_type::value:
|
||||
case input_type::value_invert: {
|
||||
auto const paramidxname { entryparser.getToken<std::string>() };
|
||||
auto const paramidx { (
|
||||
// exceptions
|
||||
paramidxname == "y" ? 1 :
|
||||
paramidxname == "2" ? 1 : // human-readable param index starts with 1
|
||||
// default
|
||||
0 ) };
|
||||
std::get<2>( m_inputaxes[ axisindex ].bindings[ controlmode ] ) = static_cast<user_command>( paramidx );
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( splitbindingpoint.first == "button" ) {
|
||||
|
||||
auto const buttonindex { splitbindingpoint.second };
|
||||
// sanity check, connected gamepad isn't guaranteed to have that many buttons
|
||||
if( buttonindex >= m_inputbuttons.size() ) { continue; }
|
||||
|
||||
auto const bindingtype { entryparser.getToken<std::string>() };
|
||||
if( bindingtype == "mode" ) {
|
||||
// special case, mode selector
|
||||
if( true == entryparser.getTokens( 1, true, "\n\r\t " ) ) {
|
||||
entryparser >> m_inputbuttons[ buttonindex ].mode;
|
||||
}
|
||||
else {
|
||||
WriteLog( "Gamepad binding for " + bindingpoint + " didn't specify mode index" );
|
||||
}
|
||||
}
|
||||
else {
|
||||
// otherwise set control mode to match pressed button
|
||||
m_mode = static_cast<control_mode>( Button );
|
||||
// regular button, single bound command
|
||||
std::vector< std::reference_wrapper<user_command> > bindingtargets;
|
||||
bindingtargets.emplace_back( std::ref( m_inputbuttons[ buttonindex ].binding ) );
|
||||
bind( bindingtargets, entryparser, nametocommandmap, bindingpoint );
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
gamepad_input::process_axes( glm::vec2 Leftstick, glm::vec2 const &Rightstick, glm::vec2 const &Triggers ) {
|
||||
gamepad_input::on_button( int const Button, int const Action ) {
|
||||
|
||||
auto const &button { m_inputbuttons[ Button ] };
|
||||
|
||||
if( button.mode >= 0 ) {
|
||||
|
||||
switch( Action ) {
|
||||
case GLFW_PRESS: {
|
||||
for( auto &axis : m_inputaxes ) {
|
||||
axis.accumulator = 0.0f;
|
||||
}
|
||||
[[fallthrough]];
|
||||
}
|
||||
case GLFW_REPEAT: {
|
||||
m_mode = Button;
|
||||
break;
|
||||
}
|
||||
case GLFW_RELEASE: {
|
||||
if( m_mode == Button ) {
|
||||
m_mode = -1;
|
||||
for( auto &axis : m_inputaxes ) {
|
||||
axis.accumulator = 0.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( button.binding != user_command::none ) {
|
||||
|
||||
// right stick, look around
|
||||
if( ( Rightstick.x != 0.0f ) || ( Rightstick.y != 0.0f ) ) {
|
||||
// TODO: make toggles for the axis flip
|
||||
auto const deltatime = Timer::GetDeltaRenderTime() * 60.0;
|
||||
double const turnx = Rightstick.x * 10.0 * deltatime;
|
||||
double const turny = -Rightstick.y * 10.0 * deltatime;
|
||||
m_relay.post(
|
||||
user_command::viewturn,
|
||||
turnx,
|
||||
turny,
|
||||
GLFW_PRESS,
|
||||
button.binding,
|
||||
0,
|
||||
0,
|
||||
Action,
|
||||
// as we haven't yet implemented either item id system or multiplayer, the 'local' controlled vehicle and entity have temporary ids of 0
|
||||
// TODO: pass correct entity id once the missing systems are in place
|
||||
0 );
|
||||
}
|
||||
|
||||
// left stick, either movement or controls, depending on currently active mode
|
||||
if( m_mode == control_mode::entity ) {
|
||||
|
||||
if( ( Leftstick.x != 0.0 || Leftstick.y != 0.0 )
|
||||
|| ( m_leftstick.x != 0.0 || m_leftstick.y != 0.0 ) ) {
|
||||
m_relay.post(
|
||||
user_command::movehorizontal,
|
||||
Leftstick.x,
|
||||
Leftstick.y,
|
||||
GLFW_PRESS,
|
||||
0 );
|
||||
}
|
||||
}
|
||||
else {
|
||||
// vehicle control modes
|
||||
process_mode( Leftstick.y, 0 );
|
||||
}
|
||||
|
||||
m_rightstick = Rightstick;
|
||||
m_leftstick = Leftstick;
|
||||
m_triggers = Triggers;
|
||||
}
|
||||
|
||||
void
|
||||
gamepad_input::process_mode( float const Value, std::uint16_t const Recipient ) {
|
||||
gamepad_input::process_axes() {
|
||||
|
||||
// TODO: separate multiplier for each mode, to allow different, customizable sensitivity for each control
|
||||
auto const deltatime = Timer::GetDeltaTime() * 15.0;
|
||||
auto const &lookup = m_modecommands.at( static_cast<size_t>( m_mode ) );
|
||||
input_type inputtype;
|
||||
user_command boundcommand1, boundcommand2;
|
||||
auto binding { std::tie( inputtype, boundcommand1, boundcommand2 ) };
|
||||
|
||||
if( Value >= 0.0f ) {
|
||||
if( m_modeaccumulator < 0.0f ) {
|
||||
// reset accumulator if we're going in the other direction i.e. issuing opposite control
|
||||
// this also means we should indicate the previous command no longer applies
|
||||
// (normally it's handled when the stick enters dead zone, but it's possible there's no actual dead zone)
|
||||
m_relay.post(
|
||||
lookup.second,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
Recipient );
|
||||
m_modeaccumulator = 0.0f;
|
||||
}
|
||||
if( Value > m_deadzone ) {
|
||||
m_modeaccumulator += ( Value - m_deadzone ) / ( 1.0 - m_deadzone ) * deltatime;
|
||||
// we're making sure there's always a positive charge left in the accumulator,
|
||||
// to more reliably decect when the stick goes from active to dead zone, below
|
||||
while( m_modeaccumulator > 1.0f ) {
|
||||
// send commands if the accumulator(s) was filled
|
||||
m_relay.post(
|
||||
lookup.first,
|
||||
0, 0,
|
||||
GLFW_PRESS,
|
||||
Recipient );
|
||||
m_modeaccumulator -= 1.0f;
|
||||
// since some commands can potentially collect values from two different axes we can't post them directly
|
||||
// instead, we use a small scratchpad to first put these commands together, then post them in their completed state
|
||||
std::unordered_map<user_command, std::tuple< double, double, int > > commands;
|
||||
// HACK: generate movement reset, it'll be eiter overriden by actual movement command, or issued if another control mode was activated
|
||||
commands[ user_command::movehorizontal ] = { 0.0, 0.0, GLFW_PRESS };
|
||||
|
||||
for( auto &axis : m_inputaxes ) {
|
||||
|
||||
if( axis.bindings.empty() ) { continue; }
|
||||
|
||||
auto const lookup { axis.bindings.find( m_mode ) };
|
||||
if( lookup == axis.bindings.end() ) { continue; }
|
||||
|
||||
binding = lookup->second;
|
||||
|
||||
switch( inputtype ) {
|
||||
case input_type::threestate: {
|
||||
// TODO: separate multiplier for each mode, to allow different, customizable sensitivity for each control
|
||||
auto const deltatime { Timer::GetDeltaTime() * 15.0 };
|
||||
if( axis.state >= 0.0f ) {
|
||||
// first bound command selected
|
||||
if( axis.accumulator < 0.0f ) {
|
||||
// we were issuing the other command, post notification that's no longer the case
|
||||
if( boundcommand2 != user_command::none ) {
|
||||
m_relay.post(
|
||||
boundcommand2,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
0 );
|
||||
axis.accumulator = 0.0f;
|
||||
}
|
||||
}
|
||||
if( boundcommand1 != user_command::none ) {
|
||||
if( axis.state > m_deadzone ) {
|
||||
axis.accumulator += ( axis.state - m_deadzone ) / ( 1.0 - m_deadzone ) * deltatime;
|
||||
// we're making sure there's always a positive charge left in the accumulator,
|
||||
// to more reliably decect when the stick goes from active to dead zone, below
|
||||
while( axis.accumulator > 1.0f ) {
|
||||
// send commands if the accumulator(s) was filled
|
||||
m_relay.post(
|
||||
boundcommand1,
|
||||
0, 0,
|
||||
GLFW_PRESS,
|
||||
0 );
|
||||
axis.accumulator -= 1.0f;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// if the accumulator isn't empty it's an indicator the stick moved from active to neutral zone
|
||||
// indicate it with proper RELEASE command
|
||||
m_relay.post(
|
||||
boundcommand1,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
0 );
|
||||
axis.accumulator = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// second bound command selected
|
||||
if( axis.accumulator > 0.0f ) {
|
||||
// we were issuing the other command, post notification that's no longer the case
|
||||
if( boundcommand1 != user_command::none ) {
|
||||
m_relay.post(
|
||||
boundcommand1,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
0 );
|
||||
axis.accumulator = 0.0f;
|
||||
}
|
||||
}
|
||||
if( boundcommand1 != user_command::none ) {
|
||||
if( axis.state < -m_deadzone ) {
|
||||
axis.accumulator += ( axis.state + m_deadzone ) / ( 1.0 - m_deadzone ) * deltatime;
|
||||
// we're making sure there's always a positive charge left in the accumulator,
|
||||
// to more reliably decect when the stick goes from active to dead zone, below
|
||||
while( axis.accumulator < -1.0f ) {
|
||||
// send commands if the accumulator(s) was filled
|
||||
m_relay.post(
|
||||
boundcommand2,
|
||||
0, 0,
|
||||
GLFW_PRESS,
|
||||
0 );
|
||||
axis.accumulator += 1.0f;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// if the accumulator isn't empty it's an indicator the stick moved from active to neutral zone
|
||||
// indicate it with proper RELEASE command
|
||||
m_relay.post(
|
||||
boundcommand2,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
0 );
|
||||
axis.accumulator = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case input_type::value:
|
||||
case input_type::value_invert: {
|
||||
auto &command { commands[ boundcommand1 ] };
|
||||
std::get<int>( command ) = GLFW_PRESS;
|
||||
auto ¶m { (
|
||||
static_cast<int>( boundcommand2 ) == 0 ? // for this type boundcommand2 stores param index
|
||||
std::get<0>( command ) :
|
||||
std::get<1>( command ) ) };
|
||||
param = axis.state;
|
||||
if( std::abs( param ) < m_deadzone ) {
|
||||
param = 0.0;
|
||||
}
|
||||
else {
|
||||
param = (
|
||||
param > 0.0 ?
|
||||
( param - m_deadzone ) / ( 1.0 - m_deadzone ) :
|
||||
( param + m_deadzone ) / ( 1.0 - m_deadzone ) );
|
||||
}
|
||||
if( param != 0.0 ) {
|
||||
if( inputtype == input_type::value_invert ) {
|
||||
param *= -1.0;
|
||||
}
|
||||
// special case, viewturn receives some param scaling
|
||||
if( boundcommand1 == user_command::viewturn ) {
|
||||
param *= 10.0 * ( Timer::GetDeltaRenderTime() * 60.0 );
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// if the accumulator isn't empty it's an indicator the stick moved from active to neutral zone
|
||||
// indicate it with proper RELEASE command
|
||||
m_relay.post(
|
||||
lookup.first,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
Recipient );
|
||||
m_modeaccumulator = 0.0f;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if( m_modeaccumulator > 0.0f ) {
|
||||
// reset accumulator if we're going in the other direction i.e. issuing opposite control
|
||||
// this also means we should indicate the previous command no longer applies
|
||||
// (normally it's handled when the stick enters dead zone, but it's possible there's no actual dead zone)
|
||||
// issue remaining, assembled commands
|
||||
for( auto const &command : commands ) {
|
||||
auto const param1 { std::get<0>( command.second ) };
|
||||
auto const param2 { std::get<1>( command.second ) };
|
||||
auto &lastparams { m_lastcommandparams[ command.first ] };
|
||||
if( ( param1 != 0.0 ) || ( std::get<0>( lastparams ) != 0.0 )
|
||||
|| ( param2 != 0.0 ) || ( std::get<1>( lastparams ) != 0.0 ) ) {
|
||||
m_relay.post(
|
||||
lookup.first,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
Recipient );
|
||||
m_modeaccumulator = 0.0f;
|
||||
}
|
||||
if( Value < m_deadzone ) {
|
||||
m_modeaccumulator += ( Value + m_deadzone ) / ( 1.0 - m_deadzone ) * deltatime;
|
||||
// we're making sure there's always a negative charge left in the accumulator,
|
||||
// to more reliably decect when the stick goes from active to dead zone, below
|
||||
while( m_modeaccumulator < -1.0f ) {
|
||||
// send commands if the accumulator(s) was filled
|
||||
m_relay.post(
|
||||
lookup.second,
|
||||
0, 0,
|
||||
GLFW_PRESS,
|
||||
Recipient );
|
||||
m_modeaccumulator += 1.0f;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// if the accumulator isn't empty it's an indicator the stick moved from active to neutral zone
|
||||
// indicate it with proper RELEASE command
|
||||
m_relay.post(
|
||||
lookup.second,
|
||||
0, 0,
|
||||
GLFW_RELEASE,
|
||||
Recipient );
|
||||
m_modeaccumulator = 0.0f;
|
||||
command.first,
|
||||
param1,
|
||||
param2,
|
||||
std::get<2>( command.second ),
|
||||
// as we haven't yet implemented either item id system or multiplayer, the 'local' controlled vehicle and entity have temporary ids of 0
|
||||
// TODO: pass correct entity id once the missing systems are in place
|
||||
0 );
|
||||
lastparams = std::tie( param1, param2 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class gamepad_input {
|
||||
|
||||
public:
|
||||
// constructors
|
||||
gamepad_input();
|
||||
gamepad_input() = default;
|
||||
|
||||
// methods
|
||||
// checks state of the controls and sends issued commands
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
|
||||
private:
|
||||
// types
|
||||
/*
|
||||
enum gamepad_button {
|
||||
a,
|
||||
b,
|
||||
@@ -51,34 +52,43 @@ private:
|
||||
lefttrigger,
|
||||
righttrigger
|
||||
};
|
||||
enum class control_mode {
|
||||
entity = -1,
|
||||
vehicle_mastercontroller,
|
||||
vehicle_trainbrake,
|
||||
vehicle_secondarycontroller,
|
||||
vehicle_independentbrake
|
||||
*/
|
||||
enum class input_type {
|
||||
threestate, // two commands, mapped to positive and negative axis value respectively; press and release events on state change
|
||||
impulse, // one command; press event when set, release when cleared
|
||||
value, // one command; press event, axis value passed as specified param (stored as second 'user_command')
|
||||
value_invert // the passed value is additionally multiplied by -1
|
||||
};
|
||||
typedef std::vector<float> float_sequence;
|
||||
typedef std::vector<char> char_sequence;
|
||||
typedef std::vector< std::pair< user_command, user_command > > commandpair_sequence;
|
||||
|
||||
struct input_button {
|
||||
unsigned char state; // last polled state
|
||||
int mode; // associated control mode
|
||||
user_command binding; // associated command
|
||||
};
|
||||
|
||||
struct input_axis {
|
||||
float state; // last polled state
|
||||
float accumulator; // multipurpose helper variable
|
||||
std::unordered_map< int, std::tuple< input_type, user_command, user_command > > bindings; // associated commands for respective modes
|
||||
};
|
||||
|
||||
using inputbutton_sequence = std::vector<input_button>;
|
||||
using inputaxis_sequence = std::vector<input_axis>;
|
||||
// methods
|
||||
void on_button( gamepad_button const Button, int const Action );
|
||||
void process_axes( glm::vec2 Leftstick, glm::vec2 const &Rightstick, glm::vec2 const &Triggers );
|
||||
void process_mode( float const Value, std::uint16_t const Recipient );
|
||||
bool recall_bindings();
|
||||
void bind( std::vector< std::reference_wrapper<user_command> > &Targets, cParser &Input, std::unordered_map<std::string, user_command> const &Translator, std::string const Point );
|
||||
void on_button( int const Button, int const Action );
|
||||
void process_axes();
|
||||
|
||||
// members
|
||||
command_relay m_relay;
|
||||
float_sequence m_axes;
|
||||
char_sequence m_buttons;
|
||||
inputbutton_sequence m_inputbuttons;
|
||||
inputaxis_sequence m_inputaxes;
|
||||
int m_deviceid{ -1 };
|
||||
control_mode m_mode{ control_mode::entity };
|
||||
commandpair_sequence m_modecommands; // sets of commands issued depending on the active control mode
|
||||
int m_mode{ -1 }; // currently active control mode
|
||||
float m_deadzone{ 0.15f }; // TODO: allow to configure this
|
||||
glm::vec2 m_leftstick;
|
||||
glm::vec2 m_rightstick;
|
||||
glm::vec2 m_triggers;
|
||||
double m_modeaccumulator{ 0.0 }; // used to throttle command input rate for vehicle controls
|
||||
// double m_modeaccumulator{ 0.0 }; // used to throttle command input rate for vehicle controls
|
||||
std::unordered_map< user_command, std::tuple<double, double> > m_lastcommandparams; // cached parameters for last issued command of given type
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -182,7 +182,7 @@ opengl_vbogeometrybank::bind_streams( gfx::stream_units const &Units, unsigned i
|
||||
}
|
||||
if( Streams & gfx::stream::texture ) {
|
||||
for( auto unit : Units.texture ) {
|
||||
::glClientActiveTexture( unit );
|
||||
::glClientActiveTexture( GL_TEXTURE0 + unit );
|
||||
::glTexCoordPointer( 2, GL_FLOAT, sizeof( gfx::basic_vertex ), reinterpret_cast<void const *>( 24 ) );
|
||||
::glEnableClientState( GL_TEXTURE_COORD_ARRAY );
|
||||
}
|
||||
@@ -190,7 +190,7 @@ opengl_vbogeometrybank::bind_streams( gfx::stream_units const &Units, unsigned i
|
||||
}
|
||||
else {
|
||||
for( auto unit : Units.texture ) {
|
||||
::glClientActiveTexture( unit );
|
||||
::glClientActiveTexture( GL_TEXTURE0 + unit );
|
||||
::glDisableClientState( GL_TEXTURE_COORD_ARRAY );
|
||||
}
|
||||
m_activetexturearrays.clear(); // NOTE: we're simplifying here, since we always toggle the same texture coord sets
|
||||
@@ -206,7 +206,7 @@ opengl_vbogeometrybank::release_streams() {
|
||||
::glDisableClientState( GL_NORMAL_ARRAY );
|
||||
::glDisableClientState( GL_COLOR_ARRAY );
|
||||
for( auto unit : m_activetexturearrays ) {
|
||||
::glClientActiveTexture( unit );
|
||||
::glClientActiveTexture( GL_TEXTURE0 + unit );
|
||||
::glDisableClientState( GL_TEXTURE_COORD_ARRAY );
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,6 @@ opengl_renderer::Init( GLFWwindow *Window ) {
|
||||
Global.BasicRenderer ?
|
||||
std::vector<GLint>{ m_diffusetextureunit } :
|
||||
std::vector<GLint>{ m_normaltextureunit, m_diffusetextureunit } );
|
||||
m_textures.assign_units( m_helpertextureunit, m_shadowtextureunit, m_normaltextureunit, m_diffusetextureunit ); // TODO: add reflections unit
|
||||
ui_layer::set_unit( m_diffusetextureunit );
|
||||
m_precipitationrenderer.set_unit( m_diffusetextureunit );
|
||||
select_unit( m_diffusetextureunit );
|
||||
@@ -1681,9 +1680,9 @@ opengl_renderer::Bind_Material( material_handle const Material, TSubModel *sm )
|
||||
|
||||
auto const &material = m_materials.material( Material );
|
||||
if( false == Global.BasicRenderer ) {
|
||||
m_textures.bind( m_normaltextureunit - GL_TEXTURE0, material.textures[1] );
|
||||
m_textures.bind( m_normaltextureunit, material.textures[1] );
|
||||
}
|
||||
m_textures.bind( m_diffusetextureunit - GL_TEXTURE0, material.textures[0] );
|
||||
m_textures.bind( m_diffusetextureunit, material.textures[0] );
|
||||
}
|
||||
|
||||
opengl_material const &
|
||||
@@ -1715,7 +1714,7 @@ opengl_renderer::Fetch_Texture( std::string const &Filename, bool const Loadnow,
|
||||
void
|
||||
opengl_renderer::Bind_Texture( texture_handle const Texture ) {
|
||||
|
||||
m_textures.bind( m_diffusetextureunit - GL_TEXTURE0, Texture );
|
||||
m_textures.bind( m_diffusetextureunit, Texture );
|
||||
}
|
||||
|
||||
void
|
||||
@@ -4195,7 +4194,7 @@ opengl_renderer::Init_caps() {
|
||||
if( true == Global.BasicRenderer ) {
|
||||
WriteLog( "Basic renderer selected, shadow and reflection mapping will be disabled" );
|
||||
Global.RenderShadows = false;
|
||||
m_diffusetextureunit = GL_TEXTURE0;
|
||||
m_diffusetextureunit = 0;
|
||||
m_helpertextureunit = -1;
|
||||
m_shadowtextureunit = -1;
|
||||
m_normaltextureunit = -1;
|
||||
@@ -4207,7 +4206,7 @@ opengl_renderer::Init_caps() {
|
||||
WriteLog( "Less than 4 texture units, shadow and reflection mapping will be disabled" );
|
||||
Global.BasicRenderer = true;
|
||||
Global.RenderShadows = false;
|
||||
m_diffusetextureunit = GL_TEXTURE0;
|
||||
m_diffusetextureunit = 0;
|
||||
m_helpertextureunit = -1;
|
||||
m_shadowtextureunit = -1;
|
||||
m_normaltextureunit = -1;
|
||||
|
||||
@@ -307,10 +307,10 @@ private:
|
||||
double m_environmentupdatetime { 0.0 }; // time of the most recent environment map update
|
||||
glm::dvec3 m_environmentupdatelocation; // coordinates of most recent environment map update
|
||||
|
||||
int m_helpertextureunit { GL_TEXTURE0 };
|
||||
int m_shadowtextureunit { GL_TEXTURE1 };
|
||||
int m_normaltextureunit { GL_TEXTURE2 };
|
||||
int m_diffusetextureunit{ GL_TEXTURE3 };
|
||||
int m_helpertextureunit { 0 };
|
||||
int m_shadowtextureunit { 1 };
|
||||
int m_normaltextureunit { 2 };
|
||||
int m_diffusetextureunit{ 3 };
|
||||
units_state m_unitstate;
|
||||
|
||||
unsigned int m_framestamp; // id of currently rendered gfx frame
|
||||
|
||||
@@ -61,7 +61,7 @@ public:
|
||||
// assign texturing hardware unit
|
||||
static
|
||||
void
|
||||
set_unit( GLint const Textureunit ) { m_textureunit = Textureunit; }
|
||||
set_unit( GLint const Textureunit ) { m_textureunit = GL_TEXTURE0 + Textureunit; }
|
||||
static
|
||||
void
|
||||
shutdown();
|
||||
|
||||
@@ -193,6 +193,19 @@ std::vector<std::string> Split(const std::string &s)
|
||||
return elems;
|
||||
}
|
||||
|
||||
std::pair<std::string, int>
|
||||
split_index( std::string const &Key ) {
|
||||
|
||||
auto const indexstart{ Key.find_first_of( "-1234567890" ) };
|
||||
auto const indexend{ Key.find_first_not_of( "-1234567890", indexstart ) };
|
||||
if( indexstart != std::string::npos ) {
|
||||
return {
|
||||
Key.substr( 0, indexstart ),
|
||||
std::stoi( Key.substr( indexstart, indexend - indexstart ) ) };
|
||||
}
|
||||
return { Key, 0 };
|
||||
}
|
||||
|
||||
std::string to_string(int Value)
|
||||
{
|
||||
std::ostringstream o;
|
||||
|
||||
@@ -110,6 +110,7 @@ std::string ExchangeCharInString( std::string const &Source, char const From, ch
|
||||
std::vector<std::string> &Split(const std::string &s, char delim, std::vector<std::string> &elems);
|
||||
std::vector<std::string> Split(const std::string &s, char delim);
|
||||
//std::vector<std::string> Split(const std::string &s);
|
||||
std::pair<std::string, int> split_index( std::string const &Key );
|
||||
|
||||
std::string to_string(int Value);
|
||||
std::string to_string(unsigned int Value);
|
||||
|
||||
Reference in New Issue
Block a user