Index: tests/TestMain.h ================================================================== --- tests/TestMain.h +++ tests/TestMain.h @@ -32,7 +32,9 @@ of_point_t position; of_dimension_t scale; BOOL running; ogk_rotation_t rotation; ogk_color_t tint; + OFThread *animationThread; + BOOL stopAnimation; } @end Index: tests/TestMain.m ================================================================== --- tests/TestMain.m +++ tests/TestMain.m @@ -21,10 +21,16 @@ #import "OGKDisplay.h" #import "OGKEvent.h" #import "OGKEventQueue.h" #import "OGKBitmap.h" #import "TestMain.h" + +@interface TestMain () +- (void)draw; +- (void)handleEvents; +- (void)toggleAnimation; +@end OF_APPLICATION_DELEGATE(TestMain) @implementation TestMain - (void)display: (OGKDisplay*)display @@ -68,10 +74,13 @@ rotation.angle -= M_PI / 128; break; case OGK_KEY_RIGHT: rotation.angle += M_PI / 128; break; + case OGK_KEY_A: + [self toggleAnimation]; + break; case OGK_KEY_Q: running = NO; break; } } @@ -112,17 +121,46 @@ [eventQueue handleEvents]; } - (void)draw { + ogk_rotation_t localRotation; + @synchronized (self) { + localRotation = rotation; + } + [OGKBitmap clearToColor: OGK_COLOR_BLACK]; [bitmap drawAtPosition: position scale: scale - rotation: rotation + rotation: localRotation tint: tint]; [display update]; } + +- (void)toggleAnimation +{ + @synchronized (self) { + if (animationThread != nil) { + stopAnimation = YES; + [animationThread join]; + animationThread = nil; + stopAnimation = NO; + return; + } + + animationThread = [OFThread threadWithBlock: ^ (id object) { + while (!stopAnimation) { + @synchronized (self) { + rotation.angle -= M_PI / 256; + } + [OFThread sleepForTimeInterval: 0.01]; + } + return nil; + }]; + [animationThread start]; + } +} - (void)applicationDidFinishLaunching { ogk_display_flags_t flags = OGK_DISPLAY_FLAGS_RESIZABLE |