C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 10th, 2004, 08:00 AM
COS214 COS214 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: In the south of Africa
Posts: 5 COS214 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Maze game: input char without pressing enter

I have this C++ project that involves creating a console maze game, the problem is
that i need to read input from the keyboard and get that character immediately without
having to press enter, so for instance the grid has a hero:
xxxxxxxxxxxxxx and if the user presses the <- key the hero moves left.
x x How can i read a in instantly?
x H x
x x
xxxxxxxxxxxxxx

Reply With Quote
  #2  
Old June 10th, 2004, 09:18 AM
_julien_ _julien_ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 11 _julien_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
On which OS ?

--
julien barbier

Reply With Quote
  #3  
Old June 11th, 2004, 12:05 AM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
//in windows the answer is simple!

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define VK_A 0x41
#define VK_B 0x42
#define VK_C 0x43
#define VK_D 0x44
#define VK_E 0x45
#define VK_F 0x46
#define VK_G 0x47
#define VK_H 0x48
#define VK_I 0x49
#define VK_J 0x4A
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_M 0x4D
#define VK_N 0x4E
#define VK_O 0x4F
#define VK_P 0x50
#define VK_Q 0x51
#define VK_R 0x52
#define VK_S 0x53
#define VK_T 0x54
#define VK_U 0x55
#define VK_V 0x56
#define VK_W 0x57
#define VK_X 0x58
#define VK_Y 0x59
#define VK_Z 0x5A
#include <iostream>
#include <conio.c> // the library for the gotoxy() function
#include <winbase.h> // the library for the Sleep() function

using namespace std;

main()
{
int x = 5;
int y = 5;
int tempx;
int tempy;
int altered = 0;
int returned = 0;

system("cls");
while (returned == 0)
{
gotoxy(tempx, tempy);
cout<<" ";
tempx = x;
tempy = y;
gotoxy(x,y);
cout<<"dan";
while (altered == 0)
{
if (KEY_DOWN(VK_UP))
{
y = y-1;
altered =1;
Sleep(200);
}
else if (KEY_DOWN(VK_DOWN))
{
y = y+1;
altered =1;
Sleep(200);
}
else if (KEY_DOWN(VK_LEFT))
{
x = x-1;
altered =1;
Sleep(200);
}
else if (KEY_DOWN(VK_RIGHT))
{
x = x+1;
altered =1;
Sleep(200);
}
else if (KEY_DOWN(VK_RETURN))
{
returned = 1;
altered =1;
Sleep(200);
}

}
altered = 0;
}

}

//im too lazy to comment that, but compile it and run it and mod it to your liking. basically what it does is query the key state of the keyboard and when the key is pressed, well, it knows.... one thing you have to remember is the Sleep function because otherwise it recognizes the key pressing too fast and runs through the loop several times before you lift your finger

//-drizzle

Reply With Quote
  #4  
Old June 11th, 2004, 03:20 AM
_julien_ _julien_ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 11 _julien_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
About the sleeps: normally you can adjust
the fact that you will not receive several
key messages without having the key_up.
I don't know if conio does that, but if not
you can adjust it yourself.

--
julien barbier

Reply With Quote
  #5  
Old June 11th, 2004, 07:23 AM
COS214 COS214 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: In the south of Africa
Posts: 5 COS214 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
to _julien_

hi we are on linux redhat 9

Reply With Quote
  #6  
Old June 11th, 2004, 10:52 AM
_julien_ _julien_ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 11 _julien_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
so you should switch to non ECHO and non ICANON
mode for you term, and switch to O_NONBLOCK.
Then you'll just have to read from 0 you keycode.
That gives something like that:
Code:
 
/*
** main.c for in /u/ept3/barbie_j/c
**
** Made by julien barbier
** Login julien dot barbier at epitech dot net
**
** Started on Fri Jun 11 15:48:02 2004 julien barbier
** Last update Fri Jun 11 15:56:14 2004 julien barbier
*/
 
#include		<fcntl.h>
#include		<sys/ioctl.h>
#if defined(__NetBSD__)
# include <termios.h>
# include <termcap.h>
#endif
#if defined(__sun)
# define__lint
# include <sys/types.h>
# include <stropts.h>
# include <curses.h>
# include <sys/conf.h>
# include <termio.h>
# include <term.h>
char *tgoto(char *cap, int col, int row);
#endif
#if defined(__alpha)
# include <termio.h>
# include <term.h>
#endif
#define SLEEP_TIME 1000
 
void	initialise_ios_general(void)
{
#if (defined(__NetBSD__) || defined(__alpha))
struct termios		termios;
int				 status;
status = ioctl(0, TIOCGETA, &termios);
if (status == -1)
	exit(-1);
termios.c_lflag &= ~(ICANON | ECHO);
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
status = ioctl(0, TIOCSETA, &termios);
if (status == -1)
	exit(-1);
status = fcntl(0, F_SETFL, O_NONBLOCK);
if (status == -1)
	exit(-1);
#endif
}
 
void	initialise_ios_sun(void)
{
#if defined(__sun)
struct termio termio;
intstatus;
status = ioctl(0, TCGETA, &termio);
if (status == -1)
	exit(-1);
termio.c_lflag &= ~(ICANON | ECHO);
termio.c_cc[VMIN] = 1;
termio.c_cc[VTIME] = 0;
status = ioctl(0, TCSETA, &termio);
if (status == -1)
	exit(-1);
status = fcntl(0, F_SETFL, O_NONBLOCK);
if (status == -1)
	exit(-1);
#endif
}
void	initialise_ios(void)
{
#if defined(__sun)
initialise_ios_sun();
#else
initialise_ios_general();
#endif
}
 
unsigned int	get_key(void)
{
unsigned char c;
unsigned int key;
while (read(0, &c, 1) != -1)
	;
while (read(0, &c, 1) < 0)
	usleep(0);
key = c;
usleep(SLEEP_TIME);
while (read(0, &c, 1) != -1)
	{
	 key <<= 8;
	 key |= c;
	}
return (key);
}
 
int			 main(void)
{
unsigned int key;
initialise_ios();
while (1)
	{
	 key = get_key();
	 printf("0x%x\n", key);
	}
return (0);
}
 

Sorry I don't have Red Hat here, but this code works on
netbsd x86, sun and alpha.
So it would be easy to adapt to your distribution.

Feel free to email me if you encounter a problem.

--
julien barbier

Reply With Quote
  #7  
Old June 11th, 2004, 08:56 PM
COS214 COS214 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: In the south of Africa
Posts: 5 COS214 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks

thanks to everyone for all the help

Reply With Quote
  #8  
Old June 11th, 2004, 11:10 PM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
just so you know.... i found that the sleep function does work the best.... and the time is in milliseconds so its not that much of a delay

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Maze game: input char without pressing enter


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT