View Single Post
Old 03-27-2003, 11:07 AM   #7 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
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();
ender is offline   Reply With Quote