Where should I put this for loop?

I’m working through rhe book SDL Game Development by Shaun Mitchell. I’m in chapter 3 “Lisening for and handling axis movement” I declared “std::vector<std::pair<Vector2d*, Vector2d”>> m_joystickValues" in the header file. after the author presents a for loop to create a pair of Vector2d* in the m_joystickValues array but he doesn’t say where the for loop goes, does it go in the header file or the cpp file and whereabouts do you think he wants me to put it? I commented it out in lines 169-184 in InputHandler.cpp.

Thanks.

InputHandler.h

#ifndef __InputHandler__
#define __InputHandler__

#include <iostream>
#include <vector>
#include <utility>
#include <SDL2/SDL.h>
#include "Game.h"
#include "Vector2d.h"

class InputHandler
{
	public:
	
	static InputHandler* Instance()
	{
		if(s_pInstance == 0)
		{
			s_pInstance = new InputHandler();
		}
		return s_pInstance;
	}
	
	void update();
	void clean();
	
	void initialiseJoysticks();
	bool joysticksInitialised() {
		return m_bjoysticksInitialised; }
		
		int xvalue(int joy, int stick);
		int yvalue(int joy, int stick);
	
	private:
	
	InputHandler() {}
	
	static InputHandler* s_pInstance;
	
	const int m_joystickDeadZone = 1000;
	
	std::vector<SDL_Joystick*> m_joysticks;

	std::vector<std::pair<Vector2d*, Vector2d*>> m_joystickValues;

	bool m_bjoysticksInitialised;
	
};

typedef InputHandler TheInputHandler;

#endif /*defined (InputHandler) */

InputHandler.cpp

#include "InputHandler.h"

InputHandler* InputHandler::s_pInstance= 0;

void InputHandler::initialiseJoysticks()
{
	
	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) == 1)
				{
					m_joysticks.push_back(joy);
				}
				else
				{
					std::cout << SDL_GetError();
				}
			}
		
		
		SDL_JoystickEventState(SDL_ENABLE);
		m_bjoysticksInitialised = true;
		
		std::cout<< "Initialised " << m_joysticks.size() << 
		"joystick(s)";
		}
			else
			{
				m_bjoysticksInitialised = false;
			}
}


void InputHandler::clean()
{
	if(m_bjoysticksInitialised)
	{
		for(unsigned int i = 0; i < SDL_NumJoysticks(); i++)
		{
			SDL_JoystickClose(m_joysticks[i]);
		}
	}
}

void InputHandler::update()
{
	SDL_Event event;
	while(SDL_PollEvent(&event))
	{
		if(event.type == SDL_QUIT)
		{
			TheGame::Instance()->quit();
		}
	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_joystickValues[whichOne].first->setX(1);
			}
		else if(event.jaxis.value < -m_joystickDeadZone)
		{
			m_joystickValues[whichOne].first->setX(-1);
		}
		else
		{
			m_joystickValues[whichOne].first->setX(0);
		}
		
}

// left stick move up or down
if(event.jaxis.axis == 1)
{
	if(event.jaxis.value > m_joystickDeadZone)
	{
		m_joystickValues[whichOne].first->setY(1);
	}
	else if(event.jaxis.value < -m_joystickDeadZone)
	{
		m_joystickValues[whichOne].first->setY(-1);
	}
	else
	{
		m_joystickValues[whichOne].first->setY(0);
	}
}

//right stick move left or right
if(event.jaxis.axis == 3)
{
	if(event.jaxis.value > m_joystickDeadZone)
	{
		m_joystickValues[whichOne].second->setX(1);
	}
	else if(event.jaxis.value < -m_joystickDeadZone)
	{
		m_joystickValues[whichOne].second->setX(-1);
	}
	else
	{
		m_joystickValues[whichOne].second->setX(0);
	}
}

//right stick move up or down
if(event.jaxis.axis == 4)
{
	if(event.jaxis.value > m_joystickDeadZone)
	{
		m_joystickValues[whichOne].second->setY(1);
	}
	else if(event.jaxis.value < -m_joystickDeadZone)
	{
		m_joystickValues[whichOne].second->setY(-1);
	}
	else
	{
		m_joystickValues[whichOne].second->setY(0);
	}
}
}
}
}	

int InputHandler::xvalue(int joy, int stick)
{
	if(m_joystickValues.size() > 0)
	{
		if(stick == 1)
		{
			return m_joystickValues[joy].first->getX();
		}
		else if(stick == 2)
			{
				return m_joystickValues[joy].second->getX();
			}
	}
	return 0;
}

int InputHandler::yvalue(int joy, int stick)
{
	if(m_joystickValues.size() > 0)
	{
		if(stick == 1)
		{
			return m_joystickValues[joy].first->getX();
		}
		else if(stick == 2)
		{
			return m_joystickValues[joy].second->getY();
		}
	}
	return 0;
}

/*	
*	for(int i = 0; i < SDL_NumJoysticks(); i++)
*	{
*		SDL_Joystick* joy = SDL_JoystickOpen(i);
*		if(SDL_JoystickOpen(i))
*		{
*			m_joysticks.push_back(joy);
*			m_joystickValues.push_back(std::make_pair(new
*			Vector2d(0,0),new Vector2d(0,0))); //add our pair
*		}
*		else
*		{
*			std::cout<<SDL_GetError();
*		}
*	}
*/

In the InputHandler.h file, you’ve correctly declared
std::vector<std::pair<Vector2d*, Vector2d*>> m_joystickValues, which will be filled with information regarding the joystick(s) connected to the computer.

The for-loop code that you’ve commented out in your InputHandler.cpp file is the one that’s in the InputHandler::initialiseJoysticks() function.

So the correct code in the above named function is:

for(int i = 0; i < SDL_NumJoysticks(); i++)
{
	SDL_Joystick* joy = SDL_JoystickOpen(i);

	if(SDL_JoystickOpen(i) == 1)
	{
		m_joysticks.push_back(joy);

		m_joystickValues.push_back(std::make_pair(new Vector2D(0.0f, 0.0f), new Vector2D(0.0f, 0.0f)));
	}

	else
		std::cout << SDL_GetError();
}

m_joystickValues are then filled with information during runtime, in the InputHandler::update() function, and then finally polled/checked in the Player class’ Player::handleInput() function.