|
 |
|
 |
02-11-2003, 03:05 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Virginia
Posts: 2
|
C++ help (Real Time)
Hey this is my first post here, so i dont know if you guys have the haze the newbie policy or anything like that, but i need some quick help.
I'm in my second year of Computer Science (C++) and we have to do a dossier, where basically its a decent size program that follow several requirements. My is a basic game, divider down the middle, one person on each side. Object is to avoid the shot from the oppent but to shoot them. Simple.
The only problem i have is im trying to make it in a real time format, an accquintence kept on yelling about key buffering and sent me an example that made no sense, so i was wondering if one of you guys could help me with this. I can send you my program in the works if you want to see what im talking about.
|
|
|
02-12-2003, 02:51 AM
|
#2 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Orlando, Florida
Posts: 9
|
Re: C++ help (Real Time)
Quote:
Originally posted by Gorbash9k
Hey this is my first post here, so i dont know if you guys have the haze the newbie policy or anything like that, but i need some quick help.
I'm in my second year of Computer Science (C++) and we have to do a dossier, where basically its a decent size program that follow several requirements. My is a basic game, divider down the middle, one person on each side. Object is to avoid the shot from the oppent but to shoot them. Simple.
The only problem i have is im trying to make it in a real time format, an accquintence kept on yelling about key buffering and sent me an example that made no sense, so i was wondering if one of you guys could help me with this. I can send you my program in the works if you want to see what im talking about.
|
firstly, welcome to the forums. I'm new myself and I would say I am fairly certain a website called "codenewbies" would not be about hazing newbies :-)
On to your dilema. Bascially what you want is to setup an infinite loop. You will process very tiny descrete units of time in each loop. For a general algorithm:
Start game;
while(Game!=done){
get keyboard input
update game to next 'cycle'
if (winner) Game = done;
}
You are concerned with the keyboard input. I believe there is an easy way to do this in C but can't think of it right now. Your friends example of keyboard buffering sounds like it might do the trick. If you can, post the example on the forums or a webpage. I will try to explain it the best I can.
|
|
|
02-12-2003, 11:50 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Virginia
Posts: 2
|
I should have told my problem more indepth, I needed a way for the game to keep running while the user is inputting his command. I finally though of a way though, I'm going to set a timer of some small amount, where if the user doesnt input when the timer runs out, the computer will just put in a NULL response and continue on. My problem now is how to set a timer.
Anyways this is the program that my friend sent me.
/////////////////////////////////////////////////////////////////////////////////
#include <conio.h>
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char tmp;
system("cls");
cout << "Hit a key, please ('q' to quit)." << endl;
do
{
while(!_kbhit());
tmp = _getch();
cout << tmp;
} while (tmp != 'q');
cout << endl << endl;
system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
|
|
|
02-26-2003, 05:18 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: NJ
Posts: 7
|
i've noticed that conio.h sometimes makes weird faults..it's supposed to give the program the ability to clear screen , but sometimes it makes errors that even the debugger doesn't pick up on....maybe that's why things aren't working right?
|
|
|
02-27-2003, 01:36 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: NJ
Posts: 7
|
Quote:
Originally posted by ShadowSoft
i've noticed that conio.h sometimes makes weird faults..it's supposed to give the program the ability to clear screen , but sometimes it makes errors that even the debugger doesn't pick up on....maybe that's why things aren't working right?
|
forget i mentioned that..sorry i'm a fool that has nothing to do with your problem.
|
|
|
03-23-2003, 10:08 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: In a van, down by the river.
Posts: 24
|
Oohh... that's going to hurt.
Quote:
Originally posted by Gorbash9k
I should have told my problem more indepth, I needed a way for the game to keep running while the user is inputting his command. I finally though of a way though, I'm going to set a timer of some small amount, where if the user doesnt input when the timer runs out, the computer will just put in a NULL response and continue on. My problem now is how to set a timer.
Anyways this is the program that my friend sent me.
/////////////////////////////////////////////////////////////////////////////////
#include <conio.h>
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char tmp;
system("cls");
cout << "Hit a key, please ('q' to quit)." << endl;
do
{
while(!_kbhit());
tmp = _getch();
cout << tmp;
} while (tmp != 'q');
cout << endl << endl;
system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
|
I might just be a pompus, rotund, and arrogant pile of unnecessary carbon, but it sounds like you might need to do some multi-threading. Haveing the timer count down and getting commands at the EXACT same time? I would think so.
Then again, I'm a n00b.
Multi threading ... quite frankly, I hate the stuff. (Probably because I've never been able to do it.)
I wish I could have been more help. Sorry if I've said anything stupid.
THen again, if you just want to have the user enter something AND THEN update the timer, Pussnuts's idea is a pretty good one.
Have a nice day.
-- Jo
|
|
|
03-27-2003, 11:07 AM
|
#7 (permalink)
|
|
Code Monkey
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
|
Another twist...
Hello,
I am thinking about this all 20 minutes before a calc II test, so I will make it brief. Another method you could employ to solve your problem is do the reverse. Wait for input in the main() loop, and multi-thread to update the game. For instance...(this is using C signal processing and setting alarms to do the trick...posix stuff I believe, so windows should have this)
Code:
#include <iostream>
#include <signal.h>
#include <unistd.h>
using namespace std;
// Handle the alarm signal and reset the alarm to go off again
// Alarm goes for 6 second intervals
void updateGame (int nothing) {
cout << "We are updating the game now\n";
alarm (6);
}
int main (int argc, char * argv[]) {
struct sigaction timaction, old_timaction;
timaction.sa_handler = updateGame;
sigemptyset (&timaction.sa_mask);
timaction.sa_flags = 0;
// Set signal for alarm to go to Update Game
sigaction(SIGALRM, &timaction, &old_timaction);
alarm (6); // Set the alarm
// Process keyboard input
while (1) {
// Handle keyboard input here
}
// Clean up, and quit
return 0;
}
Using this method, the keyboard does not have to wait to get input, but the game still updates (without threads, I might add), so you have the same memory to mess with, but still have two seperate processes going on (sort of). I hope this helps.
Yet another way is to use fork() and threads, but make sure it does it in memory-shared mode...then you can while inside the other thread, and get keyboard input in the main thread, so it just does it as fast as possible (an eats the CPU time like no other) Anyway, just some suggestions...hope the code helps...
Ted Morse
PS: I didn't test this code, or even try to compile it, so don't think its perfect or will even work/make sense/anything.
** EDITED: I know the code works now on a linux-based system using g++ **
__________________
while(1) fork();
|
|
|
03-27-2003, 07:26 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Mar 2003
Location: Arlington, TX
Posts: 1
|
I made a game in my high school AP CS class. It was my rendition of Duck Hunt using CMU Graphics. I currently have the source up @ my website.
http://interrupt.ath.cx/code/Duck_Hunt.zip
It's there if you want some idea how to start. Good luck.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:40 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|