Browse Source

Added new inputCodes to input as well as functions allowing for better input abstraction

master
Macoy Madson 6 years ago
parent
commit
70d6c3a98d
3 changed files with 58 additions and 4 deletions
  1. +41
    -0
      input/input.cpp
  2. +16
    -3
      input/input.hpp
  3. +1
    -1
      makefile

+ 41
- 0
input/input.cpp View File

@ -1,6 +1,7 @@
#ifndef INPUT_CPP
#define INPUT_CPP
#include "input.hpp"
#include <iostream>
inputManager::inputManager(sf::RenderWindow *newWin)
{
win=newWin;
@ -29,6 +30,20 @@ int inputManager::getMouseY()
{
return sf::Mouse::getPosition(*win).y;
}
//Universal button function. Call if you want to use both mouse
//and keyboard together (more abstract)
bool inputManager::isPressed(int code)
{
//Mouse code (they start at 1000)
if (code >= 1000)
{
inputCode::mouseCode mouseButton = static_cast<inputCode::mouseCode>(code);
return isMousePressed(mouseButton);
}
//Keyboard code (they start at 0)
inputCode::keyCode keyButton = static_cast<inputCode::keyCode>(code);
return isPressed(keyButton);
}
bool inputManager::isPressed(sf::Keyboard::Key key)
{
return sf::Keyboard::isKeyPressed(key);
@ -376,4 +391,30 @@ sf::Mouse::Button inputManager::convertMouseCode(inputCode::mouseCode button)
}
return newButton;
}
//Universal input function. Call if you want to use mouse and keyboard
//buttons or even return mouse position
int inputManager::getState(int code)
{
if (code >= 2000) //Mouse position code
{
switch(code)
{
case inputCode::mousePosition::MouseX:
return getMouseX();
break;
case inputCode::mousePosition::MouseY:
return getMouseY();
break;
default:
break;
}
}
else //Button code
{
return isPressed(code);
}
//Input code not recognized
std::cout << "WARNING: inputManager.getState(): Input code " << code << " not recognized! Returning 0\n";
return 0;
}
#endif

+ 16
- 3
input/input.hpp View File

@ -11,7 +11,7 @@ namespace inputCode
//Enums for key codes
//Keyboard codes (for input independence)
//An *almost* direct copy of SFML 2.0 sf::Key (see /usr/local/include/SFML/Window/Keyboard.hpp)
enum keyCode
enum keyCode:int
{
A = 0, ///< The A key
B, ///< The B key
@ -115,12 +115,18 @@ namespace inputCode
F15 ///< The F15 key
};
//Mouse codes
enum mouseCode
enum mouseCode:int
{
LeftMouse=0,
LeftMouse=1000,
RightMouse,
MiddleMouse
};
//Mouse position codes
enum mousePosition:int
{
MouseX=2000,
MouseY
};
}
class inputManager
@ -137,6 +143,9 @@ class inputManager
void setWindow(window *);
int getMouseX();
int getMouseY();
//Universal button function. Call if you want to use both mouse
//and keyboard together (more abstract)
bool isPressed(int code);
//Sadly, library dependant. Returns whether or not a key is pressed
bool isPressed(inputCode::keyCode);
//Note that this function is now deprecated; use isPressed(inputCode::keyCode) instead
@ -150,5 +159,9 @@ class inputManager
bool isMousePressed(inputCode::mouseCode);
//Note that this function is now deprecated; use isMousePressed(inputCode::keyCode) instead
bool isMousePressed(sf::Mouse::Button);
//Universal input function. Call if you want to use mouse and keyboard
//buttons or even return mouse position
int getState(int code);
};
#endif

+ 1
- 1
makefile View File

@ -1,4 +1,4 @@
FLAGS=g++ -c -Os -Wall
FLAGS=g++ -std=c++11 -c -Os -Wall
ARCHIVE_NAME=lib/base.a
OBJ_DIR=LOCAL_obj


Loading…
Cancel
Save