16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-18 00:49:19 +02:00

build 191212. gamepad input configuration support, minor bug fixes

This commit is contained in:
tmj-fstate
2019-12-15 16:48:55 +01:00
parent a54924c48d
commit 7c43f80ccf
12 changed files with 427 additions and 240 deletions

View File

@@ -6844,7 +6844,7 @@ TDynamicObject::powertrain_sounds::render( TMoverParameters const &Vehicle, doub
volume = 0.0; volume = 0.0;
if( false == motors.empty() ) { if( false == motors.empty() ) {
if( std::abs( Vehicle.enrot ) > 0.01 ) { if( std::abs( Vehicle.nrot ) > 0.01 ) {
auto const &motor { motors.front() }; auto const &motor { motors.front() };
// frequency calculation // frequency calculation

View File

@@ -745,7 +745,6 @@ opengl_texture::bind(size_t unit) {
if (units[unit] == id) if (units[unit] == id)
return true; return true;
if (GLAD_GL_ARB_direct_state_access) if (GLAD_GL_ARB_direct_state_access)
{ {
glBindTextureUnit(unit, id); glBindTextureUnit(unit, id);
@@ -759,7 +758,6 @@ opengl_texture::bind(size_t unit) {
} }
glBindTexture(target, id); glBindTexture(target, id);
} }
units[unit] = id; units[unit] = id;
return true; return true;
@@ -782,7 +780,6 @@ opengl_texture::unbind(size_t unit)
//todo: for other targets //todo: for other targets
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
units[unit] = 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 void
texture_manager::unit( GLint const Textureunit ) { texture_manager::unit( GLint const Textureunit ) {
if( m_activeunit == Textureunit ) { return; } if( opengl_texture::m_activeunit == Textureunit ) { return; }
m_activeunit = Textureunit; opengl_texture::m_activeunit = Textureunit;
::glActiveTexture( Textureunit ); ::glActiveTexture( GL_TEXTURE0 + Textureunit );
} }
// ustalenie numeru tekstury, wczytanie jeśli jeszcze takiej nie było // 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 void
texture_manager::bind( std::size_t const Unit, texture_handle const Texture ) { 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) if (Texture != null_handle)
mark_as_used(Texture).bind(Unit); mark_as_used(Texture).bind(Unit);
else else
@@ -1262,8 +1252,8 @@ void
texture_manager::update() { texture_manager::update() {
if( m_garbagecollector.sweep() > 0 ) { if( m_garbagecollector.sweep() > 0 ) {
for( auto &unit : m_units ) { for( auto &unit : opengl_texture::units ) {
unit.texture = -1; unit = -1;
} }
} }
} }

View File

@@ -61,6 +61,7 @@ struct opengl_texture {
GLenum target = GL_TEXTURE_2D; GLenum target = GL_TEXTURE_2D;
static std::array<GLuint, gl::MAX_TEXTURES + 2> units; static std::array<GLuint, gl::MAX_TEXTURES + 2> units;
static GLint m_activeunit;
private: private:
// methods // methods
@@ -96,8 +97,6 @@ private:
static std::unordered_map<GLint, int> precompressed_formats; static std::unordered_map<GLint, int> precompressed_formats;
static std::unordered_map<GLint, GLint> drivercompressed_formats; static std::unordered_map<GLint, GLint> drivercompressed_formats;
static std::unordered_map<GLint, std::unordered_map<GLint, GLint>> mapping; static std::unordered_map<GLint, std::unordered_map<GLint, GLint>> mapping;
static GLint m_activeunit;
}; };
typedef int texture_handle; typedef int texture_handle;
@@ -108,8 +107,6 @@ public:
texture_manager(); texture_manager();
~texture_manager() { delete_textures(); } ~texture_manager() { delete_textures(); }
void
assign_units( GLint const Helper, GLint const Shadows, GLint const Normals, GLint const Diffuse );
// activates specified texture unit // activates specified texture unit
void void
unit( GLint const Textureunit ); unit( GLint const Textureunit );
@@ -141,11 +138,6 @@ private:
typedef std::unordered_map<std::string, std::size_t> index_map; 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: // methods:
// checks whether specified texture is in the texture bank. returns texture id, or npos. // checks whether specified texture is in the texture bank. returns texture id, or npos.
texture_handle texture_handle
@@ -161,8 +153,6 @@ private:
texturetimepointpair_sequence m_textures; texturetimepointpair_sequence m_textures;
index_map m_texturemappings; index_map m_texturemappings;
garbage_collector<texturetimepointpair_sequence> m_garbagecollector { m_textures, 600, 60, "texture" }; 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 // reduces provided data image to half of original size, using basic 2x2 average

View File

@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
#include "Logs.h" #include "Logs.h"
#include "Timer.h" #include "Timer.h"
#include "utilities.h" #include "utilities.h"
#include "parser.h"
glm::vec2 circle_to_square( glm::vec2 const &Point, int const Roundness = 0 ) { 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 ); 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 bool
gamepad_input::init() { 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. // 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 // not exactly flexible, but for quick hack it'll do
auto const name = glfwGetJoystickName( GLFW_JOYSTICK_1 ); auto const name = glfwGetJoystickName( GLFW_JOYSTICK_1 );
@@ -67,18 +59,18 @@ gamepad_input::init() {
else { else {
// no joystick, // no joystick,
WriteLog( "No gamepad detected" ); WriteLog( "No gamepad detected" );
m_axes.clear();
m_buttons.clear();
return false; return false;
} }
int count; int count;
glfwGetJoystickAxes( m_deviceid, &count ); glfwGetJoystickAxes( m_deviceid, &count );
m_axes.resize( count ); m_inputaxes.assign( count, { 0.0f, 0.0f, {} } );
glfwGetJoystickButtons( m_deviceid, &count ); glfwGetJoystickButtons( m_deviceid, &count );
m_buttons.resize( count ); m_inputbuttons.assign( count, { GLFW_RELEASE, -1, user_command::none } );
recall_bindings();
return true; return true;
} }
@@ -92,222 +84,414 @@ gamepad_input::poll() {
return; return;
} }
int count; std::size_t idx = 0; int count;
std::size_t idx = 0;
// poll button state // poll button state
auto const buttons = glfwGetJoystickButtons( m_deviceid, &count ); auto const buttons = glfwGetJoystickButtons( m_deviceid, &count );
if( count ) { // safety check in case joystick gets pulled out 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 // button pressed or released, both are important
on_button( on_button(
static_cast<gamepad_button>( idx ), idx,
( buttons[ idx ] == 1 ? ( buttons[ idx ] == 1 ?
GLFW_PRESS : GLFW_PRESS :
GLFW_RELEASE ) ); GLFW_RELEASE ) );
} }
else { else {
// otherwise we only pass info about button being held down // otherwise we only pass info about button being held down
if( button == 1 ) { if( button.state == GLFW_PRESS ) {
on_button( on_button(
static_cast<gamepad_button>( idx ), idx,
GLFW_REPEAT ); GLFW_REPEAT );
} }
} }
button = buttons[ idx ]; button.state = buttons[ idx ];
++idx; ++idx;
} }
} }
// poll axes state // poll axes state
idx = 0; idx = 0;
glm::vec2 leftstick, rightstick, triggers;
auto const axes = glfwGetJoystickAxes( m_deviceid, &count ); auto const axes = glfwGetJoystickAxes( m_deviceid, &count );
if( count ) { if( count ) {
// safety check in case joystick gets pulled out // safety check in case joystick gets pulled out
if( count >= 2 ) { for( auto &axis : m_inputaxes ) {
leftstick = glm::vec2( axis.state = axes[ idx ];
( std::abs( axes[ gamepad_axes::leftstick_x ] ) > m_deadzone ? ++idx;
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 ) );
} }
} }
process_axes( leftstick, rightstick, triggers ); process_axes();
} }
void 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 ) { for( auto &bindingtarget : Targets ) {
// NOTE: this is rigid coupling, down the road we should support more flexible binding of functions with buttons // grab command(s) associated with the input pin
case gamepad_button::a: auto const bindingcommandname{ Input.getToken<std::string>() };
case gamepad_button::b: if( true == bindingcommandname.empty() ) {
case gamepad_button::x: // no tokens left, may as well complain...
case gamepad_button::y: { 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 ) { bool
// TODO: send GLFW_RELEASE for whatever command could be issued by the mode active until now gamepad_input::recall_bindings() {
// if the button was released the stick switches to control the movement
m_mode = control_mode::entity; cParser bindingparser( "eu07_input-gamepad.ini", cParser::buffer_FILE );
// zero the stick and the accumulator so the input won't bleed between modes if( false == bindingparser.ok() ) {
m_leftstick = glm::vec2(); return false;
m_modeaccumulator = 0.0f; }
// 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 { else {
// otherwise set control mode to match pressed button // regular button, single bound command
m_mode = static_cast<control_mode>( Button ); 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 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( m_relay.post(
user_command::viewturn, button.binding,
turnx, 0,
turny, 0,
GLFW_PRESS, Action,
// as we haven't yet implemented either item id system or multiplayer, the 'local' controlled vehicle and entity have temporary ids of 0 // 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 // TODO: pass correct entity id once the missing systems are in place
0 ); 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 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 input_type inputtype;
auto const deltatime = Timer::GetDeltaTime() * 15.0; user_command boundcommand1, boundcommand2;
auto const &lookup = m_modecommands.at( static_cast<size_t>( m_mode ) ); auto binding { std::tie( inputtype, boundcommand1, boundcommand2 ) };
if( Value >= 0.0f ) { // since some commands can potentially collect values from two different axes we can't post them directly
if( m_modeaccumulator < 0.0f ) { // instead, we use a small scratchpad to first put these commands together, then post them in their completed state
// reset accumulator if we're going in the other direction i.e. issuing opposite control std::unordered_map<user_command, std::tuple< double, double, int > > commands;
// this also means we should indicate the previous command no longer applies // HACK: generate movement reset, it'll be eiter overriden by actual movement command, or issued if another control mode was activated
// (normally it's handled when the stick enters dead zone, but it's possible there's no actual dead zone) commands[ user_command::movehorizontal ] = { 0.0, 0.0, GLFW_PRESS };
m_relay.post(
lookup.second, for( auto &axis : m_inputaxes ) {
0, 0,
GLFW_RELEASE, if( axis.bindings.empty() ) { continue; }
Recipient );
m_modeaccumulator = 0.0f; auto const lookup { axis.bindings.find( m_mode ) };
} if( lookup == axis.bindings.end() ) { continue; }
if( Value > m_deadzone ) {
m_modeaccumulator += ( Value - m_deadzone ) / ( 1.0 - m_deadzone ) * deltatime; binding = lookup->second;
// 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 switch( inputtype ) {
while( m_modeaccumulator > 1.0f ) { case input_type::threestate: {
// send commands if the accumulator(s) was filled // TODO: separate multiplier for each mode, to allow different, customizable sensitivity for each control
m_relay.post( auto const deltatime { Timer::GetDeltaTime() * 15.0 };
lookup.first, if( axis.state >= 0.0f ) {
0, 0, // first bound command selected
GLFW_PRESS, if( axis.accumulator < 0.0f ) {
Recipient ); // we were issuing the other command, post notification that's no longer the case
m_modeaccumulator -= 1.0f; 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 &param { (
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 { // issue remaining, assembled commands
if( m_modeaccumulator > 0.0f ) { for( auto const &command : commands ) {
// reset accumulator if we're going in the other direction i.e. issuing opposite control auto const param1 { std::get<0>( command.second ) };
// this also means we should indicate the previous command no longer applies auto const param2 { std::get<1>( command.second ) };
// (normally it's handled when the stick enters dead zone, but it's possible there's no actual dead zone) 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( m_relay.post(
lookup.first, command.first,
0, 0, param1,
GLFW_RELEASE, param2,
Recipient ); std::get<2>( command.second ),
m_modeaccumulator = 0.0f; // 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
if( Value < m_deadzone ) { 0 );
m_modeaccumulator += ( Value + m_deadzone ) / ( 1.0 - m_deadzone ) * deltatime; lastparams = std::tie( param1, param2 );
// 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;
} }
} }
} }

View File

@@ -16,7 +16,7 @@ class gamepad_input {
public: public:
// constructors // constructors
gamepad_input(); gamepad_input() = default;
// methods // methods
// checks state of the controls and sends issued commands // checks state of the controls and sends issued commands
@@ -27,6 +27,7 @@ public:
private: private:
// types // types
/*
enum gamepad_button { enum gamepad_button {
a, a,
b, b,
@@ -51,34 +52,43 @@ private:
lefttrigger, lefttrigger,
righttrigger righttrigger
}; };
enum class control_mode { */
entity = -1, enum class input_type {
vehicle_mastercontroller, threestate, // two commands, mapped to positive and negative axis value respectively; press and release events on state change
vehicle_trainbrake, impulse, // one command; press event when set, release when cleared
vehicle_secondarycontroller, value, // one command; press event, axis value passed as specified param (stored as second 'user_command')
vehicle_independentbrake 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 // methods
void on_button( gamepad_button const Button, int const Action ); bool recall_bindings();
void process_axes( glm::vec2 Leftstick, glm::vec2 const &Rightstick, glm::vec2 const &Triggers ); 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 process_mode( float const Value, std::uint16_t const Recipient ); void on_button( int const Button, int const Action );
void process_axes();
// members // members
command_relay m_relay; command_relay m_relay;
float_sequence m_axes; inputbutton_sequence m_inputbuttons;
char_sequence m_buttons; inputaxis_sequence m_inputaxes;
int m_deviceid{ -1 }; int m_deviceid{ -1 };
control_mode m_mode{ control_mode::entity }; int m_mode{ -1 }; // currently active control mode
commandpair_sequence m_modecommands; // sets of commands issued depending on the active control mode
float m_deadzone{ 0.15f }; // TODO: allow to configure this float m_deadzone{ 0.15f }; // TODO: allow to configure this
glm::vec2 m_leftstick; // double m_modeaccumulator{ 0.0 }; // used to throttle command input rate for vehicle controls
glm::vec2 m_rightstick; std::unordered_map< user_command, std::tuple<double, double> > m_lastcommandparams; // cached parameters for last issued command of given type
glm::vec2 m_triggers;
double m_modeaccumulator{ 0.0 }; // used to throttle command input rate for vehicle controls
}; };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@@ -182,7 +182,7 @@ opengl_vbogeometrybank::bind_streams( gfx::stream_units const &Units, unsigned i
} }
if( Streams & gfx::stream::texture ) { if( Streams & gfx::stream::texture ) {
for( auto unit : Units.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 ) ); ::glTexCoordPointer( 2, GL_FLOAT, sizeof( gfx::basic_vertex ), reinterpret_cast<void const *>( 24 ) );
::glEnableClientState( GL_TEXTURE_COORD_ARRAY ); ::glEnableClientState( GL_TEXTURE_COORD_ARRAY );
} }
@@ -190,7 +190,7 @@ opengl_vbogeometrybank::bind_streams( gfx::stream_units const &Units, unsigned i
} }
else { else {
for( auto unit : Units.texture ) { for( auto unit : Units.texture ) {
::glClientActiveTexture( unit ); ::glClientActiveTexture( GL_TEXTURE0 + unit );
::glDisableClientState( GL_TEXTURE_COORD_ARRAY ); ::glDisableClientState( GL_TEXTURE_COORD_ARRAY );
} }
m_activetexturearrays.clear(); // NOTE: we're simplifying here, since we always toggle the same texture coord sets 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_NORMAL_ARRAY );
::glDisableClientState( GL_COLOR_ARRAY ); ::glDisableClientState( GL_COLOR_ARRAY );
for( auto unit : m_activetexturearrays ) { for( auto unit : m_activetexturearrays ) {
::glClientActiveTexture( unit ); ::glClientActiveTexture( GL_TEXTURE0 + unit );
::glDisableClientState( GL_TEXTURE_COORD_ARRAY ); ::glDisableClientState( GL_TEXTURE_COORD_ARRAY );
} }

View File

@@ -51,7 +51,6 @@ opengl_renderer::Init( GLFWwindow *Window ) {
Global.BasicRenderer ? Global.BasicRenderer ?
std::vector<GLint>{ m_diffusetextureunit } : std::vector<GLint>{ m_diffusetextureunit } :
std::vector<GLint>{ m_normaltextureunit, 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 ); ui_layer::set_unit( m_diffusetextureunit );
m_precipitationrenderer.set_unit( m_diffusetextureunit ); m_precipitationrenderer.set_unit( m_diffusetextureunit );
select_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 ); auto const &material = m_materials.material( Material );
if( false == Global.BasicRenderer ) { 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 & opengl_material const &
@@ -1715,7 +1714,7 @@ opengl_renderer::Fetch_Texture( std::string const &Filename, bool const Loadnow,
void void
opengl_renderer::Bind_Texture( texture_handle const Texture ) { opengl_renderer::Bind_Texture( texture_handle const Texture ) {
m_textures.bind( m_diffusetextureunit - GL_TEXTURE0, Texture ); m_textures.bind( m_diffusetextureunit, Texture );
} }
void void
@@ -4195,7 +4194,7 @@ opengl_renderer::Init_caps() {
if( true == Global.BasicRenderer ) { if( true == Global.BasicRenderer ) {
WriteLog( "Basic renderer selected, shadow and reflection mapping will be disabled" ); WriteLog( "Basic renderer selected, shadow and reflection mapping will be disabled" );
Global.RenderShadows = false; Global.RenderShadows = false;
m_diffusetextureunit = GL_TEXTURE0; m_diffusetextureunit = 0;
m_helpertextureunit = -1; m_helpertextureunit = -1;
m_shadowtextureunit = -1; m_shadowtextureunit = -1;
m_normaltextureunit = -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" ); WriteLog( "Less than 4 texture units, shadow and reflection mapping will be disabled" );
Global.BasicRenderer = true; Global.BasicRenderer = true;
Global.RenderShadows = false; Global.RenderShadows = false;
m_diffusetextureunit = GL_TEXTURE0; m_diffusetextureunit = 0;
m_helpertextureunit = -1; m_helpertextureunit = -1;
m_shadowtextureunit = -1; m_shadowtextureunit = -1;
m_normaltextureunit = -1; m_normaltextureunit = -1;

View File

@@ -307,10 +307,10 @@ private:
double m_environmentupdatetime { 0.0 }; // time of the most recent environment map update double m_environmentupdatetime { 0.0 }; // time of the most recent environment map update
glm::dvec3 m_environmentupdatelocation; // coordinates of most recent environment map update glm::dvec3 m_environmentupdatelocation; // coordinates of most recent environment map update
int m_helpertextureunit { GL_TEXTURE0 }; int m_helpertextureunit { 0 };
int m_shadowtextureunit { GL_TEXTURE1 }; int m_shadowtextureunit { 1 };
int m_normaltextureunit { GL_TEXTURE2 }; int m_normaltextureunit { 2 };
int m_diffusetextureunit{ GL_TEXTURE3 }; int m_diffusetextureunit{ 3 };
units_state m_unitstate; units_state m_unitstate;
unsigned int m_framestamp; // id of currently rendered gfx frame unsigned int m_framestamp; // id of currently rendered gfx frame

View File

@@ -61,7 +61,7 @@ public:
// assign texturing hardware unit // assign texturing hardware unit
static static
void void
set_unit( GLint const Textureunit ) { m_textureunit = Textureunit; } set_unit( GLint const Textureunit ) { m_textureunit = GL_TEXTURE0 + Textureunit; }
static static
void void
shutdown(); shutdown();

View File

@@ -193,6 +193,19 @@ std::vector<std::string> Split(const std::string &s)
return elems; 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::string to_string(int Value)
{ {
std::ostringstream o; std::ostringstream o;

View File

@@ -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> &elems);
std::vector<std::string> Split(const std::string &s, char delim); std::vector<std::string> Split(const std::string &s, char delim);
//std::vector<std::string> Split(const std::string &s); //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(int Value);
std::string to_string(unsigned int Value); std::string to_string(unsigned int Value);

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#define VERSION_MAJOR 19 #define VERSION_MAJOR 19
#define VERSION_MINOR 1212 #define VERSION_MINOR 1214
#define VERSION_REVISION 0 #define VERSION_REVISION 0