blobbo
2005.09.27, 10:47 PM
Hi all,
I'm trying to get back into programming games. I've isolated OpenGL as the source of my woes, and I've gone back to what I started with - Quartz! It's so user-friendly, and so simple to prototype stuff in Obj-C and Cocoa. My plan is to keep the model and controller abstract from the drawcode so that I can someday replace it with OpenGL code. That way I can still have fun while keeping the end-goal of a fast-drawing game alive...
Anyway, on to the beef. I think I've written a good animation loop that calls a physics method based on physics units. It should maintain a solid fps and smooth gameplay, methinks. Comments, please? It's very well commented - should be easy reading:
- (void)animate:(id)timer
{
/* Store Current time to calculate delta time */
NSDate *timeOfAnimation = [NSDate date];
/* Testing for printing of FPS, printing, etc */
if ([timeOfAnimation timeIntervalSinceDate:oldFPSTime] > 1.0)
{
NSLog(@"**** FPS: %d ****", frameCounter);
[oldFPSTime release];
oldFPSTime = [timeOfAnimation retain];
frameCounter = 0;
}
/* Calculate amount of "time" that needs to be animated, persae */
currentInterval = ([timeOfAnimation timeIntervalSinceDate:previousAnimationTime]
+ physicsLeftover);
[previousAnimationTime release];
previousAnimationTime = [timeOfAnimation retain];
/* Check that no huge amount of time has passed since the last physics event
was run. This eliminates jerky movement when client's computer lags */
if (currentInterval > (MAX_PHYSICS_UNITS * physicsInterval))
{
currentInterval = (MAX_PHYSICS_UNITS * physicsInterval);
}
/* If the current delta time is long enough to warrant completing a physics unit,
do so until the current interval is so reduced that it no longer is large enough
to warrant an entire unit. Leave the leftover for the next time around the
animation loop. Increment the fps counter and draw. */
if (currentInterval >= physicsInterval)
{
while (currentInterval >= physicsInterval)
{
currentInterval -= physicsInterval;
[self runPhysics];
}
[view setNeedsDisplay:YES];
frameCounter++;
}
physicsLeftover = currentInterval;
}
I'm trying to get back into programming games. I've isolated OpenGL as the source of my woes, and I've gone back to what I started with - Quartz! It's so user-friendly, and so simple to prototype stuff in Obj-C and Cocoa. My plan is to keep the model and controller abstract from the drawcode so that I can someday replace it with OpenGL code. That way I can still have fun while keeping the end-goal of a fast-drawing game alive...
Anyway, on to the beef. I think I've written a good animation loop that calls a physics method based on physics units. It should maintain a solid fps and smooth gameplay, methinks. Comments, please? It's very well commented - should be easy reading:
- (void)animate:(id)timer
{
/* Store Current time to calculate delta time */
NSDate *timeOfAnimation = [NSDate date];
/* Testing for printing of FPS, printing, etc */
if ([timeOfAnimation timeIntervalSinceDate:oldFPSTime] > 1.0)
{
NSLog(@"**** FPS: %d ****", frameCounter);
[oldFPSTime release];
oldFPSTime = [timeOfAnimation retain];
frameCounter = 0;
}
/* Calculate amount of "time" that needs to be animated, persae */
currentInterval = ([timeOfAnimation timeIntervalSinceDate:previousAnimationTime]
+ physicsLeftover);
[previousAnimationTime release];
previousAnimationTime = [timeOfAnimation retain];
/* Check that no huge amount of time has passed since the last physics event
was run. This eliminates jerky movement when client's computer lags */
if (currentInterval > (MAX_PHYSICS_UNITS * physicsInterval))
{
currentInterval = (MAX_PHYSICS_UNITS * physicsInterval);
}
/* If the current delta time is long enough to warrant completing a physics unit,
do so until the current interval is so reduced that it no longer is large enough
to warrant an entire unit. Leave the leftover for the next time around the
animation loop. Increment the fps counter and draw. */
if (currentInterval >= physicsInterval)
{
while (currentInterval >= physicsInterval)
{
currentInterval -= physicsInterval;
[self runPhysics];
}
[view setNeedsDisplay:YES];
frameCounter++;
}
physicsLeftover = currentInterval;
}