FlushConsoleInputBuffer AND logic check

J

Joshua Weston

I am new to C++ and I am very interested in becoming proficient. This post
is two-fold. One, I am having problems with this small test program. It is
meant to allow user input to control a "@" character as it moves around the
screen. I understand that it does not have error checking. The problem I am
having is that when I hit the arrow key it moves TWO spaces instead of one.
As I understand it, the FlushConsoleInputBuffer(handle) should prevent
this.... The strange thing is, at one point, it did! The first time I
compiled and ran this it seemed to work. I am using DevCpp 4.9.8.1. Now,
when I call the FlushConsoleInputBuffer it doesn't seem to be working. Is
there a problem with my code? Does anyone have any ideas? The SECOND part of
this, my first post of what I am sure will be an illustrious career (ya
sure), is to ask those who are willing to help me with the FlushConsole
problem to glance over my comments to make sure I am grasping the logic
correctly. All I ask is for any glaring problems to be pointed out. If
anyone can help me with this I would VERY much appreciate it. I am picking
this up quickly and maybe at some point I can return the favor. Code follows
below.

Thanks, Josh Weston

// *************** This is the beginning of main.cpp



#include <stdlib.h>
#include "Manipulation.h"

using namespace std;

HANDLE hInput, hOutput;

void DrawPlayer(PLAYER player, int Draw){

// This is the basis of OOP. When I need to change the player
representation on the screen,
// for any reason, whether to move the character or to erase the old. It
is a logical
// categorization of data. It is much easier, when you get used to it,
to call a function
// that does multiple things. Anytime I need to change the PR I just
send the coordinates
// and put in the DRAW type and it will do what I ask. Having written
the code for this
// function I can save much later on.

if(Draw){
SetConsoleCursorPosition(hOutput, player.position);
cout << "@";
} //end DRAW condition - sets "@" character to COORD of the passed
player.position - Not Referenced

else {
SetConsoleCursorPosition(hOutput, player.position);
cout << " ";
} //end ERASE condition - sets " " and erases old position

} // end DrawPlayer Function - Does not return a value, simply performs
onscreen manipulation

void MovePlayer(PLAYER &player){

// This is the function called within the endless loop in the Main()
function. First thing
// to notice is that the variable is referenced. the ampersand (&)
character means that
// when I pass in the "player" parameter in the Main() function I am not
passing along a COPY
// of the "player" to be manipulated and then destroyed when the
function ends. I am passing a
// REFERENCE to the actual location (address) of the "player" struct,
declared in the Main()
// code, so that when the function ends, the data is stored in the
"player" struct that was
// declared in Main(). The endless game loop that exists in Main()
passes the new "player"
// struct data right back into MovePlayer and the whole thing happens
again.

INPUT_RECORD InputRecord; // This variable is actually a structure
that stores player input DATA
// which is compatible with the EVENT
recognition needed to take input
// DATA from the player input BUFFER and
translate it into WINDOWS
// recognized Virtual KeyCodes or Mouse
Events that the code can
// manipulate and cause appropriate onscreen
events.

DWORD Events =0; // The ReadConsoleInput(1,2,3,4) parameters,
seen later, will call for a
// a 32bit variable in the 4th place of the
parameter to designate the
// number of events that have taken place.
For our purposes, it is only one.
// However, if I wanted to be able to hold
down "alt" and then press an arrow key
// so that the player would "run" in a
certain direction, I would want to
// look for more than one event during
ReadConsoleInput.

ReadConsoleInput(hInput, &InputRecord , 1 , &Events); // Reads user
input and stores it into InputRecord for
// later use. I
must find out if the program pauses here
// while waiting
for input or loops continuously while
// waiting for
input, which is also possible.

if (InputRecord.EventType == MOUSE_EVENT){ // The first condition the
conditional statements looks for is a
// Mouse Event. It Does not
detect the specific type.

if(InputRecord.Event.MouseEvent.dwButtonState ==
FROM_LEFT_1ST_BUTTON_PRESSED){ // We are only looking for

// a Left Button Click
DrawPlayer(player , ERASE); // First, we must erase the existing
representation. This is done now because
// "player" still contains the
COORDinates from the last loop.
player.position.X = InputRecord.Event.MouseEvent.dwMousePosition.X;
// Adjust the X-Axis based upon the click
player.position.Y = InputRecord.Event.MouseEvent.dwMousePosition.Y;
// Adjust the Y-Axis based upon the click

DrawPlayer(player, DRAW); // There! We have re-drawn the
representation based upon the Mouse Click COORDinates.
}} //end Mouse Input conditional statement. Work on conventions.

if (InputRecord.EventType == KEY_EVENT){ // Looking at all possible KEY
events now.

if (InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT){

DrawPlayer(player , ERASE);
player.position.X++;
DrawPlayer(player , DRAW);
} // End RIGHT move.

else if (InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT){

DrawPlayer(player , ERASE);
player.position.X--;
DrawPlayer(player , DRAW);
} // End Left move.

else if (InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP){

DrawPlayer(player , ERASE);
player.position.Y--;
DrawPlayer(player , DRAW);
} // End UP move.

else if (InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN){

DrawPlayer(player , ERASE);
player.position.Y++;
DrawPlayer(player , DRAW);
} // End DOWN move.

} // end KEY EVENT conditional statement
FlushConsoleInputBuffer (hInput); // Prevents strange behavior.
} // end MovePlayer function

int main()
{
PLAYER player; // Builds the PLAYER struct that is referenced by
MovePlayer

hInput = GetStdHandle(STD_INPUT_HANDLE); // Sets these HANDLES to
INPUT and OUTPUT HANDLES to "handle" the
hOutput = GetStdHandle(STD_OUTPUT_HANDLE); // data stream to the
console window.

SetConsoleMode(hInput, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT); //
Lets the following code know that we will
//
be handling Keyboard AND Mouse input.

player.position.X = SW / 2; // Start off in the middle.
player.position.Y = SH / 2;

while (1){
MovePlayer(player);
} // end While loop

system("pause");
return 0;
} //end Main() This should do it... lets see.



// ****************** THIS IS THE END OF main.cpp

// ******************** This is the beginning of Manipulation.h



#include <iostream>
#include <windows.h>
using namespace std;

#define NORTH 0 //Direction Manipulation Standards
#define SOUTH 2
#define EAST 1
#define WEST 3

#define DRAW 1 //Drawing Manipulation Standards
#define ERASE 0

#define SW 79 //Default Screen Size
#define SH 24

struct PLAYER {

COORD position;
int direction;
};
void DrawPlayer (PLAYER player, int Draw);
void MovePlayer (PLAYER &player);


// *********************** end of Manipulation.h
 
A

Artie Gold

Joshua said:
I am new to C++ and I am very interested in becoming proficient. This post
is two-fold. One, I am having problems with this small test program. It is
meant to allow user input to control a "@" character as it moves around the
screen. I understand that it does not have error checking. The problem I am

Obviously, since you've read the FAQ, you've posted here in error.

Naturally you realize that since you don't have a question about
standard C++, but rather one about libraries/extensions provided by a
specific vendor, you're off topic here.

Of course, since you've "done the right thing" and lurked for a while
before posting here, you merely mistyped the name of the appropriate
platform specific newsgroup as comp.lang.c++.

[snip]

HTH,
--ag

[please pardon the sarcastic tone]

BTW - as a general comment, posting hundreds of lines of code is not the
best way to get assistance -- even when you post in a proper forum.
 
J

Joshua Weston

Mr good friend Artie. I very much appreciate the constructive, albeit
sarcastic, feedback. I am new to this. While "ignorance of the law" is no
defense, I was unaware of the proper forum to voice my concern. I will apply
the proper "conventions" in the future. What I am wondering is the
following: do you have any feedback relative to my question? Yes, I will
post this message to the appropriate place, but since it is here, would it
be to much to ask to forgive me the error in posting and provide me with a
little help, as I am sure that your skills are more than capable of helping
me. I thank you, again, for your help, and do look forward to a civil and
open communication with everyone who posts in this, and associated,
newsgroups.

J.J. Weston

Artie Gold said:
Joshua said:
I am new to C++ and I am very interested in becoming proficient. This post
is two-fold. One, I am having problems with this small test program. It is
meant to allow user input to control a "@" character as it moves around the
screen. I understand that it does not have error checking. The problem I
am

Obviously, since you've read the FAQ, you've posted here in error.

Naturally you realize that since you don't have a question about
standard C++, but rather one about libraries/extensions provided by a
specific vendor, you're off topic here.

Of course, since you've "done the right thing" and lurked for a while
before posting here, you merely mistyped the name of the appropriate
platform specific newsgroup as comp.lang.c++.

[snip]

HTH,
--ag

[please pardon the sarcastic tone]

BTW - as a general comment, posting hundreds of lines of code is not the
best way to get assistance -- even when you post in a proper forum.
 
A

Artie Gold

Joshua said:
Mr good friend Artie. I very much appreciate the constructive, albeit
sarcastic, feedback. I am new to this. While "ignorance of the law" is no
defense, I was unaware of the proper forum to voice my concern. I will apply
the proper "conventions" in the future. What I am wondering is the
following: do you have any feedback relative to my question? Yes, I will
post this message to the appropriate place, but since it is here, would it
be to much to ask to forgive me the error in posting and provide me with a
little help, as I am sure that your skills are more than capable of helping
me. I thank you, again, for your help, and do look forward to a civil and
open communication with everyone who posts in this, and associated,
newsgroups.

J.J. Weston

Joshua:

You're being a good sport about this, so I'll be kind... ;-)

Well, no I can't help you -- not here.

Usenet, at its best, is an incredibly valuable resource. Much of its
potential -- particularly in regards to the technical newsgroups -- is
enhanced by maintaining topicality. Standard C++, all by itself, is a
rather large topic; adding in all the concerns of particular
implementations -- or the extensions they provide -- would make it
completely unwieldy. Much of the value of a particular newsgroup is
determined by the collective expertise of its lurkers, i.e. those who
are willing to answer questions (hopefully correctly). Keeping the
subject matter well defined tends to raise the level of discussion. As a
corollary, you are likely to get the best assistance for a particular
problem where the best expertise resides -- which in this case would be
a forum devoted to your particular platform (something in the
hierarchy, most likely).

One other point.
Before posting in a particular group do at least one of the following
(and, preferably, do both):
1) Read its FAQ (FAQs can be found through the use of www.faqs.org)
2) Lurk for a while -- read through postings to get a general sense of
what is considered topical, as well as what kinds of postings garner the
best responses.

Yes, there's a little bit of a learning curve involved. And it can be a
little like the school yard at times. But on the bright side, no one is
going to steal your lunch money or throw your books out on the street.
The worst that's likely to happen is having virtual electrons hurled at
you. ;-)

Oh yes -- and when you _do_ have a question that concerns standard C++,
by all means, ask here!

Best of luck,
--ag

Joshua said:
I am new to C++ and I am very interested in becoming proficient. This
post
is two-fold. One, I am having problems with this small test program. It
is
meant to allow user input to control a "@" character as it moves around
the
screen. I understand that it does not have error checking. The problem I
am

Obviously, since you've read the FAQ, you've posted here in error.

Naturally you realize that since you don't have a question about
standard C++, but rather one about libraries/extensions provided by a
specific vendor, you're off topic here.

Of course, since you've "done the right thing" and lurked for a while
before posting here, you merely mistyped the name of the appropriate
platform specific newsgroup as comp.lang.c++.

[snip]

HTH,
--ag

[please pardon the sarcastic tone]

BTW - as a general comment, posting hundreds of lines of code is not the
best way to get assistance -- even when you post in a proper forum.
 
J

Jim Fischer

Joshua said:
I am new to C++ and I am very interested in becoming proficient. This post
is two-fold. One, I am having problems with this small test program. It is
meant to allow user input to control a "@" character as it moves around the
screen. I understand that it does not have error checking. The problem I am
having is that when I hit the arrow key it moves TWO spaces instead of one.
As I understand it, the FlushConsoleInputBuffer(handle) should prevent
this.... The strange thing is, at one point, it did! The first time I
compiled and ran this it seemed to work. I am using DevCpp 4.9.8.1. Now,
when I call the FlushConsoleInputBuffer it doesn't seem to be working. Is
there a problem with my code? Does anyone have any ideas? The SECOND part of
this, my first post of what I am sure will be an illustrious career (ya
sure), is to ask those who are willing to help me with the FlushConsole
problem to glance over my comments to make sure I am grasping the logic
correctly. All I ask is for any glaring problems to be pointed out. If
anyone can help me with this I would VERY much appreciate it. I am picking
this up quickly and maybe at some point I can return the favor. Code follows
below.

FWIW, a good portion of your code is restricted to the Microsoft Windows
API. Since this stuff is defined by Microsoft (i.e., it is not part of
the C++ language proper), your best bet is to ask for assistance in the
Microsoft Windows programming newsgroups. [Likewise, if you need help
with the Linux API, the Linux programming newsgroups are the right
places to ask for help, etc.] For example:

comp.os.ms-windows.programmer.*

FWIW, Microsoft has a "peer-to-peer" newsgroup server where you can ask
Windows programming and Visual C++/Studio questions. The server's URL is

msnews.microsoft.com

On this newsgroup server, the Win32 programming newsgroups all start with,

microsoft.public.win32.programmer.*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top