ANimation with Turbo C
Hello All,
I'm writing a graphics application using Turbo C. The application is a simulation of "Car service".
Explanation:
There is a car service center in the high way. The cars arrive one by one and get serviced at the service center.
What I did?
Presently, one car arrives, gets serviced then moves ahead. Then another car arrives, gets serviced and moves ahead and so on. i.e unless the first car hasn't being serviced the second car cannot be serviced. IT is one to one way servicing.
What I want?
The first car arrives, enters service center. During the time the first car is being serviced, the second car should arrive, should be placed in the queue. Once the first car is finished servicing, it should move ahead and the second car should replace the first one and so on. i.e the servicing of the cars should happen simultaneously.
For eg.
Car1 arrives at service center.
Car2 follows Car1 to arrive at service center.
Car2 is queued until Car1 is being serviced.
Car1 has finished servicing, moves ahead the road.
Car2 replaces Car1 to get serviced. If there are other cars following Car2, they all should wait.
Car2 has finished servicing, moves, replaced by the other car behind it if any. and so on...
Please help.
Thanks in advance.
[1358 byte] By [
Jacko123] at [2007-11-20 11:26:27]

# 1 Re: ANimation with Turbo C
Ah, memories of Turbo C.
You've fairly well designed your intent, so what's the real question?
Is it specifically how to implement a queue?
You could do that with a circular queue implemented with an array. That's easier for C than a linked list that takes entries into the head, pulls them from the tail (a FIFO queue that's 'infinitely' extensible).
The circular queue implies a maximum queue size (the array the implements the queue has a fixed size). Two pointers, the head and tail, or insert and current points (any other similar names apply), start out at zero.
As you insert into the queue, the current point remains the same, but the insert point increments. The difference between these two pointers is the volume in the queue.
The current point identifies the item to be removed the next time something is pulled from the queue. When it is, it is incremented. At that point, if the current and insert points are the same, the queue is again empty.
When the insert point is incremented to the 'size' of the queue, it must wrap - it becomes zero again, rather than point beyond the end of the array. Same for the current point.
The rest is about how to advance the objects in an animation loop (since you don't have threads or events).
JVene at 2007-11-10 3:48:17 >

# 2 Re: ANimation with Turbo C
My question is how do I process cars simultaneously? i.e I want to use something like multi threading but don't know how to implement it in Turbo C.
I will present what I did till now.
|------|
_______________________| 1 | 2 | 3 |_____________________
Car1 arrives--> Car1 moves ahead-->
___________________________________________________________________
1, 2, and 3 are the three slots where cars wait. Slot 3 is the place where the car actually gets serviced.
This is what I have done now--> Works fine
Car1 arrives at service center.
Car1 knows that slot 3 is empty so goes for servicing.
Car1 once serviced moves ahead
Car2 then arrives, goes to slot 3 gets serviced and moves ahead. Same holds for other cars too.
I want to implement --> DONT know how??
BEFORE
|--------|
___________________ ____| 1 | 2 | Car1 |_____________________
Car2 arrives -->
___________________________________________________________________
AFTER
|--------|
___________________ ____| 1 | Car2 | Car1 |_____________________
Car3 arrives --> Car1 is "Serviced." moves ahead
___________________________________________________________________
so on...
Car1 arrives at service center.
Car1 knows that slot 3 is empty so goes for servicing.
Car2 follows Car1 to arrive at service center.
Car2 checks if slot 3 is empty.
Car2 is queued int slot 2.
Car1 has finished servicing, moves ahead the road.
Car2 replaces Car1 to get serviced. If there are other cars following Car2, they all should wait.
Car2 has finished servicing, moves, replaced by the other car behind it if any. and so on...
My question is once car1 starts and goes to slot3, how do I start other cars simultaneously?
My present algorithm is something like the following:
int main()
{
int i = 0;
while( i<3 )
{
Car1(); //moves the car
i++;
}
}
Car1()
{
while( x < xmax )
{
if( car is at slot 3 )
delay();
//... update screen for animation and move the car
}
}
I hope I'm clear...
# 3 Re: ANimation with Turbo C
That takes us back a bit, but then most current animations are done this way today.
You don't use delays, for one. You use states. If car2 approaches, a test is made to see if a car is in service, and if so, that puts car2 in the queue.
Each loop of the animation advances car1 through it's service paces, whatever that is. Each loop might be faster than real time, and that's ok. If service is an oil change and that takes 5 real time seconds (and perhaps your counting seconds as if they were minutes), then even if the animation loop is running 20 times a second, you don't advance the car1 from oil change to startup and check before leaving until 5 real time seconds elapses.
While that happens, car2's status (in the queue) is not altered, just waiting.
When car1's services are completed, it's advanced to 'out of service'.
At that point the animation loop checks to see if there's a car in queue to fill the availability in the service center, and if so, car2's state is changed (removed from the queue, placed in service, and it's service cycle(s) begin).
If you had multiple service lanes, this works the same way. At each loop, you study, in sequence, the events occurring at each service spot. Each car at each of those service locations is advanced in status according to it's own properties (one might be getting spark plugs, another an air filter, another fuel, etc.).
In other words, you must run through everything in sequence, but let the fact that the animation loop is cycling faster than some frame rate (at least, say, 5 frames or loops per second) make it SEEM like things are happening simultaneously.
It has been so for many years. Pacman, and those ghosts AND the pacman were all serviced in sequence, not in parallel. Each loop of the animation moved them in a tiny increment, then the loop repeats. Each one has it's own 'state' of operation, so they act independently, but since each frame advances some tiny bit, it works out like the frames of a film's animation.
JVene at 2007-11-10 3:50:13 >

