I'm developing a small program that is supposed to simulate a plane flying and to show the position relative to a ground station on a gauge.
I wrote the code to draw the gauge with GDI, but I'm not sure how to calculate the position of the plane efficiently.
The plane position will be simply plotted on the form with a god's eye view.
My initial thought was to use a timer set to, say, 0.1s, and calculate how much the plane has moved by calculating the exact time difference between subsequent frames. I belive this method can be quite inefficient.
The second idea I had was to use XNA snd determine the movement in the update method. I consider this to be an overkill for the purpose of my program.
It's probably best to measure time on an absolute scale (from the start of the simulation) rather than constantly calculating time distances between frames; otherwise you will probably accumulate some error as frames progress. GDI might be too slow for your purposes (depends on how complex the rendering; if it's quite simple it will probably be OK, otherwise consider DirectX).
I think the timer will be too inefficient; instead try something like:
frameRate = 1000 / 30; //30 Frames per second
while( true )
//Get the time of the frame
//Update the plane position
//Draw the new position
//Some simulation termination
if( someCondition )
//Get the absolute time
// Wait for the next frame
Thread.Sleep( Math.Max(0,frameRate -timeToExecuteThisLoopInstance) );
Hope that helps!
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
You are right about time measurement, it's also easier to implement it they way you suggested.
Graphics are pretty simple, the output is something that looks like this: http://www.astronautics.com/products...s/5ATI-HSI.gif (I found this image online).
I divided the image into layers, as some of them are always the same, some just rotate and some others change. I don't expect this to require heavy CPU usage.
I haven't worked a lot with DirectX so far, I just used DirectMusic and DirectSound in a small program I made a few years ago. I'll look into Direct2D as well, I like the idea of having smoother gfx than GDI can provide.
I'll try to write the update loop as you recommended, calculating the position is pretty simple stuff so the thread shouldn't take too much time to perform the operations.