Nested if else and for loops problem in game engine

Hi I am trying to build the game engine from the book SDL Game Development by Shaun Mitchell, the book is very hard to follow and frequently gives examples of code with out saying which file to put it in or telling you any parts that need deleting. I was on the section Listening for and handling axis movement which I got to compile fine and moved a character around on the screen with the joy stick. I move on to the next section Dealing with joystick button input where it tells me to add

 
std::vector<std::vector<bool>> m_buttonStates;

the book does not say where this should go so I put it in InputHandler.h in the InputHandler class as private

1
2
3
4
bool getButtonState(int joy, int buttonNumber)
{
return m_buttonStates[joy] [buttonNumber];
}

the book did not say where this should go so I put it in the public section of the InputHandler class in InputHandler.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i = 0; i < SDLNumJoysticks(); i++)
{
SDL_Joystick* joy = SDL_JoystickOpen(i);
if(SDL_JoystickOpened(i))
{
m_joysticks.push_back(joy);
m_joystickValues.push_back(std::make_pair(new
Vector2D(0,0),new Vector2D(0,))));

std::vector<bool> tempButtons;

for(int j = 0; j < SDL_JoystickNumButtons(joy); j++)
{
tempButtons.push_back(false);
}

m_buttonStates.push_back(tempButtons);
}
}


with the last section the book suggests I put the fragment of code in the initialiseJoysticks function in InputHandler.cpp in the DealingWithJoystickButtonInput section but it does not say if I have to delete any code from the previous version of the file in Listening for and handling axis movement. Please can someone help me sort out the braces in the nested if else and for loops in InputHandler.cpp initialiseJoystick function in DealingWithJoystickButtonInput as I am a noob and finding it really hard to sort out these nested loops. Thanks

also the book suggested I add

1
2
3
4
5
6
7
8
9
10
11
12
13
if(event.type == SDL_JOYBUTTONDOWN)
{
int whichOne = event.jaxis.which;

m_buttonStates[whichOne][event.jbutton.button] = true;
}

if(event.type == SDL_JOYBUTTONUP)
{
int whichOne = event.jaxis.which;

m_buttonStates[whichOne][event.jbutton.button] = false;
}


I put this in the update function, is that where it should go? the book doesn't say.

ListeningForAndHandlingAxisMovement - InputHandler.h
https://drive.google.com/open?id=1r49khpoieE74IVCv4XjvpEs3EXBq0037
ListeningForAndHandlingAxisMovement- InputHandler.cpp
https://drive.google.com/open?id=10pqx2BPJ-AfMfUhxpaLrRNAFi6PKkAWn

DealingWithJoyStickButtonInput - InputHandler.cpp
https://drive.google.com/open?id=1N7BjuRDep-ru2tc9gw5l-2yl-pzDbFNW
Last edited on
I agree with your first 2 choices

I'd assume that the for loop you posted overwrite this section

1
2
3
4
5
6
7
8
9

	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(); 				} 			}




But I could be completely wrong but that would be my guess.

As far as the fragment to add to the update function I'd assume that would go just inside the while loop.
So at the end between the 4x} put it in between the top 2 and the bottom 2 }
Hi I dont think the book wants me to take out any of the for loops, but after chatting with a developer on codementor I have the following new question regarding this same problem.

I started with :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#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))
				{
					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();
				}
			}
		
		
		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 == 2)
{
	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->getY();
		}
		else if(stick == 2)
		{
			return m_joystickValues[joy].second->getY();
		}
	}
	return 0;
}



and the book suggests I add :

1
2
3
4
5
6
7
8
9
10
					std::vector<bool> tempButtons;
					
					for(int j = 0; j < SDL_JoystickNumButtons(joy); j++)
					{
						tempButtons.push_back(false);
					}
						
				    m_buttonStates.push_back(tempButtons);
					}
				}


underneath the if statement on line 18, my question is how do I integrate this code into the existing code? its only a for loop with a statement underneath it, another important question, which if, else or for loop is the m_buttonStates.push_back statement supposed to belong to ? If I knew that I would probably be able to format the code.
Thanks.
Topic archived. No new replies allowed.