**I have giving three files here when i am using InputHandler in player it says Inputhandler is not declared and i can’t figured how can you help, sorry for the big file i am new here **
#ifndef InputHandler
#define InputHandler
#include <SDL2/SDL.h>
#include
#include
#include “Game.h”
class InputHandler{
public:
static InputHandler* Instance(){
if(tempInstance == nullptr){
tempInstance = new InputHandler();
return tempInstance;
}
return tempInstance;
}
void initialiseJoystick();
bool joysticksInitialised(){return m_bJoysticksInitialised;}
void update();
void clean();
// joy is the id to identify the joystick correctly
// stick is 1 for left stick and 2 for right stick
int xvalue(int joy , int stick);
int yvalue(int joy , int stick);
private:
InputHandler(){}
~InputHandler(){}
std::vector<std::pair<Vector2D* , Vector2D*>> m_joysticksValues;
std::vector<SDL_Joystick * > m_joysticks;// For storing more than one joystick
bool m_bJoysticksInitialised;
const int m_joystickDeadZone = 10000;
static InputHandler * tempInstance;
};
typedef InputHandler TheInputHandler;
#endif // InputHandler
#include “InputHandler.h”
InputHandler * InputHandler::tempInstance = nullptr;
void InputHandler::initialiseJoystick() {
if(SDL_WasInit(SDL_INIT_JOYSTICK)==0){
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
}
if(SDL_NumJoysticks() > 0){
for(int i =0; i<SDL_NumJoysticks();i++){
SDL_Joystick * joy = SDL_JoystickOpen(i);
if(SDL_JoystickOpen(i)!=nullptr){
m_joysticks.push_back(joy);
m_joysticksValues.push_back(std::make_pair(new Vector2D(0,0), new Vector2D(0,0)));
}
else{
std::cout<<SDL_GetError()<<std::endl;
}
}
SDL_JoystickEventState(SDL_ENABLE);
m_bJoysticksInitialised = true;
std::cout<<"Initialsed "<<m_joysticks.size()<<" joystick (s) "<<std::endl;
}
else{
m_bJoysticksInitialised = false;
}
}
void InputHandler::clean() {
if(m_bJoysticksInitialised){
for(int i=0;i<SDL_NumJoysticks();i++){
SDL_JoystickClose(m_joysticks[i]);
}
}
}
void InputHandler::update() {
SDL_Event event;
while(SDL_PollEvent(&event)){
//1
if(event.type == SDL_QUIT){
TheGame::Instance()->quit();
}
//2
if(event.type == SDL_JOYAXISMOTION){
int whichone = event.jaxis.which;
//left stick move left or right
if(event.jaxis.axis ==0){
if(event.jaxis.value > m_joystickDeadZone){
m_joysticksValues[whichone].first->setX(1);
}
else if(event.jaxis.value < - m_joystickDeadZone){
m_joysticksValues[whichone].first->setX(-1);
}
else{
m_joysticksValues[whichone].first->setX(0);
std::cout<<“check 0”<<std::endl;
}
}
if(event.jaxis.axis ==1){
if(event.jaxis.value > m_joystickDeadZone){
m_joysticksValues[whichone].first->setY(1);
}
else if(event.jaxis.value < - m_joystickDeadZone){
m_joysticksValues[whichone].first->setY(-1);
}
else{
m_joysticksValues[whichone].first->setY(0);
std::cout<<"check 1"<<std::endl;
}
}
if(event.jaxis.axis ==3){
if(event.jaxis.value > m_joystickDeadZone){
m_joysticksValues[whichone].second->setX(1);
}
else if(event.jaxis.value < - m_joystickDeadZone){
m_joysticksValues[whichone].second->setX(-1);
}
else{
m_joysticksValues[whichone].second->setX(0);
std::cout<<"check 3"<<std::endl;
}
}
if(event.jaxis.axis ==4){
if(event.jaxis.value > m_joystickDeadZone){
m_joysticksValues[whichone].second->setY(1);
}
else if(event.jaxis.value < - m_joystickDeadZone){
m_joysticksValues[whichone].second->setY(-1);
}
else{
m_joysticksValues[whichone].second->setY(0);
std::cout<<"check 4"<<std::endl;
}
}
}
//3
}
}
int InputHandler::xvalue(int joy, int stick){
if(m_joysticksValues.size()>0){
if(stick==1){
return m_joysticksValues[joy].first->getX();
}
else if(stick==2){
return m_joysticksValues[joy].second->getX();
}
}
return 0;
}
int InputHandler::yvalue(int joy, int stick){
if(m_joysticksValues.size()>0){
if(stick==1){
return m_joysticksValues[joy].first->getY();
}
else if(stick==2){
return m_joysticksValues[joy].second->getY();
}
}
return 0;
}
#ifndef Player
#define Player
#include “SDLGameObjects.h”
#include “InputHandler.h”
class Player : public SDLGameObjects{
public:
Player(LoaderParametes * Para):SDLGameObjects(Para){}
void draw() {
SDLGameObjects::draw();
//std::cout<<"draw Player"<<std::endl;
}
void update() {
handleInput();
currentFrame = int(((SDL_GetTicks())/100)%6);
SDLGameObjects::update();
}
void clean() {
}
private:
void handleInput(){
if(TheInputHandler::Instance()->joysticksInitialised()){
if(TheInputHandler::Instance()->xvalue(0,1)>0||TheInputHandler::Instance()->xvalue(0,1)<0){
m_velocity.setX(1*TheInputHandler::Instance()->xvalue(0,1));
}
if(TheInputHandler::Instance()->yvalue(0,1)>0||TheInputHandler::Instance()->yvalue(0,1)<0){
m_velocity.setY(1*TheInputHandler::Instance()->yvalue(0,1));
}
if(TheInputHandler::Instance()->xvalue(0,2)>0||TheInputHandler::Instance()->xvalue(0,2)<0){
m_velocity.setX(1*TheInputHandler::Instance()->xvalue(0,2));
}
if(TheInputHandler::Instance()->yvalue(0,2)>0||TheInputHandler::Instance()->yvalue(0,2)<0){
m_velocity.setY(1*TheInputHandler::Instance()->yvalue(0,2));
}
}
}
};
#endif // Player