Page 1 of 1

Compiler throws an error

Posted: October 30th, 2017, 5:33 pm
by Devil_Jin
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: Select all

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.

Re: Compiler throws an error

Posted: October 30th, 2017, 6:12 pm
by Chrajdal
Hey!
You are forgetting that KeyIsPressed is a member function of Keyboard class.

try this:

Code: Select all

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

Re: Compiler throws an error

Posted: October 30th, 2017, 6:24 pm
by Devil_Jin
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.

Re: Compiler throws an error

Posted: October 30th, 2017, 7:02 pm
by Chrajdal
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: Select all

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: Select all

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 :-)

Re: Compiler throws an error

Posted: October 31st, 2017, 12:00 am
by albinopapa
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: Select all

// 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.

Re: Compiler throws an error

Posted: October 31st, 2017, 1:08 am
by chili
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.

Re: Compiler throws an error

Posted: October 31st, 2017, 3:54 am
by Devil_Jin
thanks for the help.
i'll stick with the if statement , until my balls grow huge.