Jump to content
  • Advertisement

Archived

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

Android_s

Console functions

This topic is 5707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to get the base to a console Tetris close, or just a educational test =). But now my problem is this; I am trying to get the character to fall continuously from the top to bottom, but it stops at ReadConsoleInput();. I have understood that it works like a cin and therefore halts(i have the function in a continous loop), but is there anyway around this? The code is as following:
  

void MoveBlock(BLOCK &Block)
{
	INPUT_RECORD InputRecord;
	DWORD Events=0;

	
	
			
	ReadConsoleInput(hInput, &InputRecord, 1, &Events);
	
	

	if(InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
	{
		if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
		{
			DrawBlock(Block, ERASE);
			if(Block.Position.X >= 10)
				Block.Position.X = 10;
			else
				Block.Position.X++;
			DrawBlock(Block, DRAW);
		}
		else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
		{
			DrawBlock(Block, ERASE);
			if(Block.Position.X <= 0)
				Block.Position.X = 0;
			else
				Block.Position.X--;
			DrawBlock(Block, DRAW);
		}
	}
	
	Sleep(500);
	DrawBlock(Block, ERASE);
	Block.Position.Y++;
	DrawBlock(Block, DRAW);
}
  

Share this post


Link to post
Share on other sites
Advertisement
You should look in to the function PeekConsoleInput(). It doesn''t wait for input like Read does, it just checks whats in the buffer. Just dont forget to remove the peeked data with ReadConsoleInput() or FlushConsoleInputBuffer() when you''ve found something with PeekConsoleInput(). Otherwise you''ll encounter problems
The msdn documentation should cover everything you want to know about these three functions, but just ask again if your haveing problems. I hope this will help you


"He who hasn''t hacked assembly language as a youth has no heart. He who does as an adult has no brain."

Share this post


Link to post
Share on other sites
Ok, I've tried what you told me but it just wont work at all. The code compiles and everything is just fine until you try to use the left/right keys. Nothing happens, no movements no chrashes...
The function looks like this now. Suggestions?

    
void MoveBlock(BLOCK &Block)
{
INPUT_RECORD InputRecord;
DWORD Events=0;




PeekConsoleInput(hInput, &InputRecord, 1, &Events);




if(InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
DrawBlock(Block, ERASE);
if(Block.Position.X >= 10)
Block.Position.X = 10;
else
Block.Position.X++;
DrawBlock(Block, DRAW);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
DrawBlock(Block, ERASE);
if(Block.Position.X <= 0)
Block.Position.X = 0;
else
Block.Position.X--;
DrawBlock(Block, DRAW);
}
FlushConsoleInputBuffer(hInput);
}

Sleep(500);
DrawBlock(Block, ERASE);
Block.Position.Y++;
DrawBlock(Block, DRAW);

if(Block.Position.Y >= 20)
{
DrawBlock(Block, ERASE);
Block.Position.Y = 0;
DrawBlock(Block, DRAW);
}
}


EDIT: I noticed i forgot to paste the FlushConsoleInputBuffer()into the browser before but it was there...

[edited by - Android_s on April 19, 2003 1:45:17 PM]

Share this post


Link to post
Share on other sites
Right obove ReadConsoleInput



//20 is the number of milliseconds to wait for user input
//m_hInput is your Input Handle
if(WAIT_TIMEOUT == WaitForSingleObject(m_hInput, 20))
return 0;



Jeff D

[edited by - Jeff D on April 19, 2003 3:05:14 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Jeff D
Right obove ReadConsoleInput



//20 is the number of milliseconds to wait for user input
//m_hInput is your Input Handle
if(WAIT_TIMEOUT == WaitForSingleObject(m_hInput, 20))
return 0;



Jeff D




How do you mean? Putting a return into a void function? Anyway i tried that and well...i didn''t work even when i changed the "void" to "int"...
Do you think you could elaborate?

Share this post


Link to post
Share on other sites
Show me your code I can help you better that way

EDIT:: Also you can just have return with nothing after it for a void function like:



//20 is the number of milliseconds to wait for user input
//m_hInput is your Input Handle
if(WAIT_TIMEOUT == WaitForSingleObject(m_hInput, 20))
return;



Jeff D

[edited by - Jeff D on April 19, 2003 4:25:10 PM]

Share this post


Link to post
Share on other sites
Ok, here is the complete source. Remember it''s only at test stage to get the falling and right/left to work.
I''ve implemented that what you suggested and it ALMOST works but not as i want it to


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

#define DRAW 1
#define ERASE 0
#define AREA_HEIGHT 19
#define AREA_WIDTH 9

struct BLOCK{
COORD Position;
int Direction;
};

void DrawBlock(BLOCK Block, int Draw);
void MoveBlock(BLOCK &Block);

HANDLE hInput, hOutput;

void DrawBlock(BLOCK Block, int Draw)
{
if(Draw)
{
SetConsoleCursorPosition(hOutput, Block.Position);
cout << "@";
}
else
{
SetConsoleCursorPosition(hOutput, Block.Position);
cout << " ";
}
}

void MoveBlock(BLOCK &Block)
{
INPUT_RECORD InputRecord;
DWORD Events=0;

if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 20))
return;

ReadConsoleInput(hInput, &InputRecord, 1, &Events);

if(InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
DrawBlock(Block, ERASE);
if(Block.Position.X >= 10)
Block.Position.X = 10;
else
Block.Position.X++;
DrawBlock(Block, DRAW);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
DrawBlock(Block, ERASE);
if(Block.Position.X <= 0)
Block.Position.X = 0;
else
Block.Position.X--;
DrawBlock(Block, DRAW);
}


FlushConsoleInputBuffer(hInput);
}

Sleep(500);
DrawBlock(Block, ERASE);
Block.Position.Y++;
DrawBlock(Block, DRAW);

if(Block.Position.Y >= 20)
{
DrawBlock(Block, ERASE);
Block.Position.Y = 0;
DrawBlock(Block, DRAW);
}
}

void main()
{
BLOCK Block;

hInput = GetStdHandle(STD_INPUT_HANDLE);
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleMode(hInput, ENABLE_PROCESSED_INPUT);

Block.Position.X = AREA_WIDTH / 2;
Block.Position.Y = 0;

SetConsoleCursorPosition(hInput, Block.Position);

DrawBlock(Block, DRAW);

while(1)
{
MoveBlock(Block);
}
}

Share this post


Link to post
Share on other sites
Good we getting somewhere now. Whats wrong is it to fast? To slow? Sometimes the input don''t register?

Ill play around with the code after you tell me what you want it to run like.

PS: sorry if it took awhile was at work


Jeff D

Share this post


Link to post
Share on other sites
Well, it''s hard to explain...but i''ll give it a try
If i use that ReadConsoleInput() the function just stops at that line (i think. It works if i remove it but then ofcourse i cant control the char...) and if i use PeekConsoleInput() it works but dosn''t register any key inputs.
I want it to run like this:
The char shall continously drop from top to bottom and one shall be able to move the char left/right, preferably several steps before it drops one line. If one dosn''t input anything it should just continue do fall.
Hmm, is this very simple or am I at the compleatly wrong track

Share this post


Link to post
Share on other sites
Alright I finally got back to you and fixed it up a little. The only difference I did was made getting the input its own function and it worked.


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

#define DRAW 1
#define ERASE 0
#define AREA_HEIGHT 19
#define AREA_WIDTH 9
#define LEFT 1000
#define RIGHT 1001

struct BLOCK{
COORD Position;
int Direction;
};

void DrawBlock(BLOCK Block, int Draw);
void MoveBlock(BLOCK &Block);

HANDLE hInput, hOutput;

int GetUserInput()
{

INPUT_RECORD InputRecord;
DWORD Events=0;

FlushConsoleInputBuffer(hInput);

if(WAIT_TIMEOUT == WaitForSingleObject(hInput, 500))
return 0;

ReadConsoleInput(hInput, &InputRecord, 1, &Events);

if(InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{

return RIGHT;

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

return LEFT;

}

}


}

void DrawBlock(BLOCK Block, int Draw)
{
if(Draw)
{
SetConsoleCursorPosition(hOutput, Block.Position);
cout << "@";
}
else
{
SetConsoleCursorPosition(hOutput, Block.Position);
cout << " ";
}
}

void MoveBlock(BLOCK &Block, int Direction)
{

if(Direction == LEFT)
{

DrawBlock(Block, ERASE);
if(Block.Position.X <= 0)
Block.Position.X = 0;
else
Block.Position.X--;
DrawBlock(Block, DRAW);

}

if(Direction == RIGHT)
{

DrawBlock(Block, ERASE);
if(Block.Position.X >= 10)
Block.Position.X = 10;
else
Block.Position.X++;
DrawBlock(Block, DRAW);

}

DrawBlock(Block, ERASE);
Block.Position.Y++;
DrawBlock(Block, DRAW);

if(Block.Position.Y >= 20)
{
DrawBlock(Block, ERASE);
Block.Position.Y = 0;
DrawBlock(Block, DRAW);
}
}

void main()
{
BLOCK Block;
int Input;

hInput = GetStdHandle(STD_INPUT_HANDLE);
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleMode(hInput, ENABLE_PROCESSED_INPUT);

Block.Position.X = AREA_WIDTH / 2;
Block.Position.Y = 0;

SetConsoleCursorPosition(hInput, Block.Position);

DrawBlock(Block, DRAW);

while(1)
{

Input = GetUserInput();
MoveBlock(Block, Input);

}
}


Sorry about the time it took to get this to you, I work a lot

Jeff D

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!