Archived

This topic is now archived and is closed to further replies.

BauerGL

Strange error with lesson 7 keyboard control

Recommended Posts

Hi, I have a problem with lesson nr 7 and the keyboard control part. The thing is this, that everything works well except that when I for example push the VK_LEFT button, the object spins according to what it should. But when I push the VK_RIGHT button the object just stops and i can''t rotate it on that axis anymore. The same goes for zooming and VK_UP/DOWN. It''s probably just some stupid typo somewhere but i can''t find it. :[ For the code part, i have it almost exactly like lesson 7, except that i have all the keyboard control in a separate function. So any tips on what could be wrong is appreciated. Thanks, Bauer. // Can''t come up with somethin good.

Share this post


Link to post
Share on other sites
Might be a point to begin with :

Check that you''re not setting a wrong rotation value by pressing VK_RIGHT. It would indeed stop rotating if you set the rotation value to 0 or something like that. Further, you might check that you''re not re-setting the rotation-value anywhere in the program. You might do something like that for example for testing purposes, and forget that you did it later...

Share this post


Link to post
Share on other sites
Yeah, but the strange thing is that i can press VK_LEFT, and the object spins accordingly. But when i want it to rotate the other way around it just stops. And after it has stopped I cant rotate it anymore... thats the strange thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here is the Winmain code:

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) // Programmets start
{
Window.done = FALSE; // Används för att gå ut ur loopen

if(MessageBox(NULL,"Fullskärmsläge?","Fullskärm",MB_YESNO | MB_ICONQUESTION) == IDYES) // Fråga efter fullskärm
Window.fullscreen = TRUE; // Om NEJ, sätt fullskärm till falskt


if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen)) // Skapa GL fönster
return 0; // Om det misslyckas skicka felmeddelande

while(!Window.done)
{
if(PeekMessage(&Window.msg,NULL,0,0,PM_REMOVE)) // Finns det ett meddelande?
{
if(Window.msg.message == WM_QUIT) // Är det ett quit meddelande
Window.done = TRUE; // Sätt done till TRUE
else
{
TranslateMessage(&Window.msg); // Översätt meddelandet
DispatchMessage(&Window.msg);
}
}
else // Om det inte finns några meddelanden
{
if((Window.active && !DrawGLScene()) || Key[VK_ESCAPE]) // Är programmet aktivt
{
Window.done = TRUE; // avsluta programmet
}
else // annars rita scenen
{
SwapBuffers(hDC); // Byt buffer (dubble buffer)

// Kolla om någon knapp blir nedtryckt
LookKeyDown();

if(Key[VK_F1])
{
Key[VK_F1] = FALSE;
KillGLWindow();
Window.fullscreen =! Window.fullscreen;

if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen))
{ return 0;}
}
}
}
}
// Stäng ner
KillGLWindow(); // Döda fönstret
return Window.msg.wParam; // Avsluta programmet
}


And here is the LookKeyDown() function code, which looks for pressed keys:

void LookKeyDown() // Se efter tangentnedtryckningar
{
if(Key[VK_PRIOR])
z -= 0.02f;
if(Key[VK_NEXT])
z += 0.02f;

if(Key[VK_UP])
xspeed -= 0.1f;
if(Key[VK_DOWN])
xspeed += 0.1f;
if(Key[VK_LEFT])
yspeed -= 0.1f;
if(Key[VK_RIGHT])
yspeed += 0.1f;

}


I think thats all code that has to do with the keyboard control.

Share this post


Link to post
Share on other sites
Uhm that didn''t go too well... sorry for that one, im totally knew here. Please just ignore the above post.

Here we go again, this is the WinMain code:
  
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) // Programmets start

{
Window.done = FALSE; // Används för att gå ut ur loopen


if(MessageBox(NULL,"Fullskärmsläge?","Fullskärm",MB_YESNO | MB_ICONQUESTION) == IDYES) // Fråga efter fullskärm

Window.fullscreen = TRUE; // Om NEJ, sätt fullskärm till falskt



if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen)) // Skapa GL fönster

return 0; // Om det misslyckas skicka felmeddelande


while(!Window.done)
{
if(PeekMessage(&Window.msg,NULL,0,0,PM_REMOVE)) // Finns det ett meddelande?

{
if(Window.msg.message == WM_QUIT) // Är det ett quit meddelande

Window.done = TRUE; // Sätt done till TRUE

else
{
TranslateMessage(&Window.msg); // Översätt meddelandet

DispatchMessage(&Window.msg);
}
}
else // Om det inte finns några meddelanden

{
if((Window.active && !DrawGLScene()) || Key[VK_ESCAPE]) // Är programmet aktivt

{
Window.done = TRUE; // avsluta programmet

}
else // annars rita scenen

{
SwapBuffers(hDC); // Byt buffer (dubble buffer)


// Kolla om någon knapp blir nedtryckt

LookKeyDown();

if(Key[VK_F1])
{
Key[VK_F1] = FALSE;
KillGLWindow();
Window.fullscreen =! Window.fullscreen;

if(!CreateGLWindow(title,WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,Window.fullscreen))
{ return 0;}
}
}
}
}
// Stäng ner

KillGLWindow(); // Döda fönstret

return Window.msg.wParam; // Avsluta programmet

}


And here is the LookKeyDown() function, which looks for pressed keys.
  
void LookKeyDown() // Se efter tangentnedtryckningar

{
if(Key[VK_PRIOR])
z -= 0.02f;
if(Key[VK_NEXT])
z += 0.02f;

if(Key[VK_UP])
xspeed -= 0.1f;
if(Key[VK_DOWN])
xspeed += 0.1f;
if(Key[VK_LEFT])
yspeed -= 0.1f;
if(Key[VK_RIGHT])
yspeed += 0.1f;

}


Please forgive me for my n00biness above :]

Thanks, Bauer.

Share this post


Link to post
Share on other sites
I had a similar problem once. I think I fixed it by putting Parenthses in the if statements like this.

if (Key[VK_UP])
{
xspeed -= 0.1f;
}

don''t ask me why but that worked for me.


"To err is human, to really mess up requires a computer"

Share this post


Link to post
Share on other sites
quote:
BauerGL
Uhm that didn''t go too well... sorry for that one, im totally knew here. Please just ignore the above post.



Since you''re new I''ll introduce you to a nifty feature:
1) Hit edit on the post you want to remove
2) On that page check the box that says whether or not you want to delete it.
3) Hit the modify button (Don''t forget to include your name and pass)
4) Ta-Da! Your post has been removed freeing up the valuable GDNet server space.

Thanks!

Share this post


Link to post
Share on other sites
Floppy, if u had looked a little closer u would have seen that it stood "Anonymous Poster" in the first post, and then it's kinda hard to edit/delete that one... But it was my mistake so thanks anyway, i wont do it again.

[EDIT]
Prosper/LOADED, thank you so very much! I looked in the WndProc and found my stupid fault. In the code below that checks for pressed/released keys, i typed Key[lParam] instead of Key[wParam] in the WM_KEYUP.
So thank you so very much it really helped me!

  
case WM_KEYDOWN:
{
Key[wParam] = TRUE;
return 0;
}
case WM_KEYUP:
{
Key[wParam] = FALSE;
return 0;
}


// NeHe community roxx!

Edited by - BauerGL on December 20, 2001 5:46:28 AM

Share this post


Link to post
Share on other sites