Reputation: 1
Ok, I am making a simple camera movement program in directx9. I take the input from the messages like so:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_KEYDOWN:
switch (wParam & 0x7F)
{
case 'W':
WDown = true;
case 'A':
ADown = true;
case 'S':
SDown = true;
case 'D':
DDown = true;
}
case WM_KEYUP:
switch (wParam & 0x7F)
{
case 'W':
WDown = false;
case 'A':
ADown = false;
case 'S':
SDown = false;
case 'D':
DDown = false;
}
}
}
// Render Frame code:
void Renderer::RenderOneFrame(bool w, bool a, bool s, bool d)
{
// Check Input
if(w)
m_camera.MoveForward(1);
else if(a)
m_camera.Strafe(-1);
else if(s)
m_camera.MoveForward(-1);
else if(d)
m_camera.Strafe(1);
}
Super simple right? I left out a lot of other code though, but it doesn't mess with these booleans at all, so they shouldn't be needed. Well, now here is the catch. It works perfectly if I use this code. Forward is forward, Right is right, Left is left, Back is back.
Problem is, I want to be able to move diagonally. So, obvious answer is get rid of else right? Well I do.
BUT, the program goes to chaos! Forward doesn't go forward! It doesn't do a thing! Left goes Backwards and Backwards goes backwards to the right! The only one that works right is right! Which is coincidentally the last input checked! But I have been pressing 1 key at a time, but they still are broken! What happened? Did I break visual studio? Is this program leaking into another dimension? What is going on?!?
Upvotes: 0
Views: 192
Reputation: 2372
Did you forget the "break" on switch case?
Example:
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_KEYDOWN:
switch (wParam & 0x7F)
{
case 'W':
WDown = true;
break;
case 'A':
ADown = true;
break;
case 'S':
SDown = true;
break;
case 'D':
DDown = true;
break;
}
break;
case WM_KEYUP:
switch (wParam & 0x7F)
{
case 'W':
WDown = false;
break;
case 'A':
ADown = false;
break;
case 'S':
SDown = false;
break;
case 'D':
DDown = false;
break;
}
}
break;
}
// Render Frame code:
void Renderer::RenderOneFrame(bool w, bool a, bool s, bool d)
{
// Check Input
if(w)
m_camera.MoveForward(1);
if(a)
m_camera.Strafe(-1);
if(s)
m_camera.MoveForward(-1);
if(d)
m_camera.Strafe(1);
}
Upvotes: 6