Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Compiler throws an error
 Post Posted: October 30th, 2017, 5:33 pm 
 

Joined: October 23rd, 2017, 5:43 am
Posts: 3
first of all Hello everyone dead or alive person on this forum!
i'm one of those LAME-ASS peeps who watch chillie's tutorial but never breaks their promise to never ever join the forum but today i broke mine and here i'm.
i secretly been watching chillie's tutorial to learn the magic of c++ without opening the compiler or writing a single line of code.
and guess what my first code punched me in the face really bad help is required to solve this issue or someone can guide me that i'm being a dick with the code.
eat a few pills of controlling your blood pressure before proceeding forward.
Code:
void Game::ComposeFrame()
{
   switch (wnd.kbd.KeyIsPressed)
   {
   case VK_UP :
      faceY = faceY + 1;
      break;
   }
   DrawFace(faceX, faceY);
}

everything works pretty damn fine if i choose the so called "if" statement .
here is the error.
Microsoft (R) C/C++ Optimizing Compiler has stopped working.
tell me if you need more info.


Top 
 Profile  
Reply with quote  
 Post Posted: October 30th, 2017, 6:12 pm 
User avatar

Joined: December 15th, 2012, 2:18 pm
Posts: 22
Hey!
You are forgetting that KeyIsPressed is a member function of Keyboard class.

try this:
Code:
if(wnd.kbd.KeyIsPressed(VK_UP))
{
    faceY = faceY + 1;
}


:)


Top 
 Profile  
Reply with quote  
 Post Posted: October 30th, 2017, 6:24 pm 
 

Joined: October 23rd, 2017, 5:43 am
Posts: 3
so by that you mean i cannot use this feature via switch statement?
i mean i have already implemented the same code via multiplr if statements i just wanted to learn switch statement via manipulating the virtual keyboard by using it in the switch statement.


Top 
 Profile  
Reply with quote  
 Post Posted: October 30th, 2017, 7:02 pm 
User avatar

Joined: December 15th, 2012, 2:18 pm
Posts: 22
Devil_Jin wrote:
so by that you mean i cannot use this feature via switch statement?
i mean i have already implemented the same code via multiplr if statements i just wanted to learn switch statement via manipulating the virtual keyboard by using it in the switch statement.


Code:
bool KeyIsPressed( unsigned char keycode ) const;

^-- it returns bool, that means that switch could go to only two cases:

case where keycode IS pressed
or
case where keycode IS NOT pressed


It think you want something like this:

Code:
void Game::ComposeFrame()
{
   switch (wnd.kbd.ReadChar()) // please note () there - ReadChar is also member function
   {
      case VK_UP :      faceY++; break;
      case VK_DOWN : faceY--; break;
      case VK_LEFT : faceX--; break;
      case VK_RIGHT : faceX++; break;
   }
   DrawFace(faceX, faceY);
}


This should work (I haven't tested it). But the problem is, that when you press for example up and left at the same time, the face won't move "diagonally". But that might also be what you want, I'm just pointing it out :-)


Top 
 Profile  
Reply with quote  
 Post Posted: October 31st, 2017, 12:00 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
Yeah, the Keyboard::KeyIsPressed(char) function checks to see if the key you pass in is pressed or not.
The Keyboard::ReadKey() function will return a Keyboard::Event letting you know what key is pressed or released.
To get the code, you'd have to call Keyboard::ReadKey() to get the event, then Keyboard::Event::GetCode() to get the code.
Code:
// Loop until the queue is empty
while( !wnd.kbd.KeyIsEmpty() )
{
     const Keyboard::Event e = wnd.kbd.ReadKey();
     switch( e.GetCode() )
     {
          case VK_UP:
               if( wnd.kbd.KeyIsPressed( VK_LEFT )
               {
                    --faceX;
               }
               else if(wnd.kbd.KeyIsPressed( VK_RIGHT )
               {
                    ++faceX;
               }
               --faceY;
               break;
          case VK_DOWN:
               if( wnd.kbd.KeyIsPressed( VK_LEFT )
               {
                    --faceX;
               }
               else if(wnd.kbd.KeyIsPressed( VK_RIGHT )
               {
                    ++faceX;
               }
               ++faceY;
               break;
          case VK_LEFT:
               --faceX;
               break;
          case VK_RIGHT:
               ++faceX;
               break;
     }
}

I wouldn't use it like this, I would stick to if statemenets, but if you only need to know about single keystrokes at a time, switch statements might be a good alternative.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post Posted: October 31st, 2017, 1:08 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3538
Location: Japan
Use if statements to poll the current state of the keyboard at any given point in time. Switch is a bad fit here.

Later on when your balls are bigger you can do event handling with ReadKey() :lol:

That one is a good fit for switch.

ReadChar() is mainly used for getting input for a text field.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post Posted: October 31st, 2017, 3:54 am 
 

Joined: October 23rd, 2017, 5:43 am
Posts: 3
thanks for the help.
i'll stick with the if statement , until my balls grow huge.


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 7 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron