Compiler throws an error

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Devil_Jin
Posts: 3
Joined: October 23rd, 2017, 5:43 am

Compiler throws an error

Post by Devil_Jin » October 30th, 2017, 5:33 pm

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.

User avatar
Chrajdal
Posts: 22
Joined: December 15th, 2012, 2:18 pm

Re: Compiler throws an error

Post by Chrajdal » October 30th, 2017, 6:12 pm

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

Devil_Jin
Posts: 3
Joined: October 23rd, 2017, 5:43 am

Re: Compiler throws an error

Post by Devil_Jin » October 30th, 2017, 6:24 pm

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.

User avatar
Chrajdal
Posts: 22
Joined: December 15th, 2012, 2:18 pm

Re: Compiler throws an error

Post by Chrajdal » October 30th, 2017, 7:02 pm

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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Compiler throws an error

Post by albinopapa » October 31st, 2017, 12:00 am

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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Compiler throws an error

Post by chili » October 31st, 2017, 1:08 am

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

Devil_Jin
Posts: 3
Joined: October 23rd, 2017, 5:43 am

Re: Compiler throws an error

Post by Devil_Jin » October 31st, 2017, 3:54 am

thanks for the help.
i'll stick with the if statement , until my balls grow huge.

Post Reply