![]() |
|
#1
|
|||
|
|||
Running several Programs at the same timeI have tried to search the tutorial to find a function for time but was unable to find it.I hope that someone will be able to help me in this forum.
Although i am new in C i want to make a simple program (using C) that can do the foll.: I want to continuously rotate a horizontal line (at 90 degrees) at a specific time interval and i want to flicker a dot at different time intervals + move a circle every pixel upward. All this must be done simultaneously. To illustrate this: Pseudocode: Code:
I have been told that i can use the time function to achieve the above. If there is a mean of using the inner clock of the computer to do this that would probably be easier. This means i could use a function from C porably found in the <time.h> library to set the time as what it is. E.g it is 14:01 23 seconds. I see the horizontal line, dot and circle. At 14:01 25sec vertical line drawn. At 14:01 27sec horizontal line drawn AND dot flicker At 14:01 28sec circle goes one pixel upward At 14:01 29sec horizontal line drawn again This should continue for a long time. How will i be able to tell the computer that it should know the time and for every interval of 2 4 and 5 seconds it should do these program. As a conclusion if this program do work, I will be able to see a line rotating at 90 degrees, a circle moving upward and a dot flickering. If ever this work... I thought about making the computer count a very large number but this stop only ONE thing at a time (the dot flicker and when it finishes ,if ever, the cirlcle moves up and then finally the line rotating). Thanks. Last edited by LuciWiz : 29-Dec-2005 at 22:04.
Reason: Edited tags
|
|
#2
|
|||
|
|||
Re: Running several Programs at the same timeQuote:
The standard C library function time() returns a value that is equal to the number of seconds since midnight, Jan 1, 1970. Since its value changes every second, some people use it to create a program delay of about a second. Now, if you are really going to do big-time graphics, you probably won't limit yourself to standard C functions, but will probably use some graphics program interface, such as Microsoft Windows, X-Windows, GL, FLTK, or others which occasionally make their way to the surface on this and other forums. Many of these higher-level APIs (Applications Programming Interfaces) have timer functions that processes can start, and which may give finer granularity than one second. Some implementations of C have functions like sleep() or usleep() that have some advantages over just continuously calling time() until the value changes. However, sticking to what you started with, here is a program that uses a function to wait about a second before returning. The advantage to using this is, say you want to simulate some alarm system that turns on some security light at 8:00 pm every day and turns it off at 6:00 am, except for Saturday and Sunday, in which case it turns the light on at 5:00 pm and turns it off at 8:00 am. You really don't want to wait a week or three to run the program in real time, so you make your simulation interval much shorter. That is, the entire sequence can be simulated quickly by using a "null" function (one that has no explicit delay --- it just returns immediately). CPP / C++ / C Code:
In this case, the program takes about a minute to run, and simply prints integer values 0 through 59 with one value printed about every second. Now, let's address graphics. I'm not going to do anything with graphics, since there are no standard C library functions the deal with that subject. I'll just make a program that prints out the current values of things when anything changes. Suppose there is one object that we want to manipulate: A dot. The dot has two states (color == red and color == black). This would be a good place to define and use an enumerated data type, but to keep things simple, I will let dot_state be an int, and I will define the "red" state to correspond to an integer value of 0 and the "black" state to be an integer value of 0. Now, suppose I want to start the dot with a value ov "red" and I want to make it change state every 2 seconds (here I will define a "tick" to be a second, and I will use the wait_a_tick() function from the previous example. Furthermore, suppose I want the color to change values every two seconds until 10 seconds have elapsed, after which time no more changes will occur. CPP / C++ / C Code:
Here's my output: Code:
The value for simulated time 0 is the initial value. The other values represent time and new value for changes. Now, I could have made it a little simpler or a little more complicated. In fact, I would have a separate function to take care of the actual state and a separate function to print the state, rather that putting in the main program. (Since I will be having more than one object, it probably would be a cleaner program to modularize the functionality. Furthermore, I probably would have made a struct for the dot object, having a member that is the state change period and another member for the state. Then I could pass the address of the struct to the various (evaluation and display functions). Now, it's time for you to contribute. Add functions for the line and the circle to tell when to change state. Add code to change their values and change the print statement to print out state values of all of the objects whenever one or more of the objects has changed. So, the main loop could look something like the following: CPP / C++ / C Code:
There are lots (and lots) of ways to do things like this. This is a way to get started. It might also motivate you to learn the complexities of Windows programming (with callback functions and graphics object manipulations) to do anything more complicated than a line, a dot, and a circle. Regards, Dave |
|
#3
|
|||
|
|||
Re: Running several Programs at the same timeThanks for your help!
|
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simulation Problem | wu_weidong | CPP / C++ Forum | 7 | 12-Mar-2005 22:56 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| time Problem | zuzupus | MySQL / PHP Forum | 9 | 24-Jul-2003 07:02 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The