1 #ifndef __AppDelegate_H__
2 #define __AppDelegate_H__
3 
4 #include "OgrePlatform.h"
5 
6 #if OGRE_PLATFORM != OGRE_PLATFORM_APPLE
7 #error This header is for use with Mac OS X only
8 #endif
9 
10 #ifdef __OBJC__
11 
12 #import <Cocoa/Cocoa.h>
13 
14 // All this does is suppress some messages in the run log.  NSApplication does not
15 // implement buttonPressed and apps without a NIB have no target for the action.
16 @implementation NSApplication (_suppressUnimplementedActionWarning)
17 - (void) buttonPressed:(id)sender
18 {
19     /* Do nothing */
20 }
21 @end
22 
23 
24 #if defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
25 @interface AppDelegate : NSObject <NSApplicationDelegate>
26 #else
27 @interface AppDelegate : NSObject
28 #endif
29 {
30     NSTimer *mTimer;
31     DemoApp demo;
32 
33     NSDate *mDate;
34     double mLastFrameTime;
35     double mStartTime;
36 }
37 
38 - (void)go;
39 - (void)renderOneFrame:(id)sender;
40 
41 @property (retain) NSTimer *mTimer;
42 @property (nonatomic) double mLastFrameTime;
43 @property (nonatomic) double mStartTime;
44 
45 @end
46 
47 #if __LP64__
48 static id mAppDelegate;
49 #endif
50 
51 @implementation AppDelegate
52 
53 @synthesize mTimer;
54 @dynamic mLastFrameTime;
55 @dynamic mStartTime;
56 
57 - (double)mLastFrameTime
58 {
59     return mLastFrameTime;
60 }
61 
62 - (void)setLastFrameTime:(double)frameInterval
63 {
64     // Frame interval defines how many display frames must pass between each time the
65     // display link fires. The display link will only fire 30 times a second when the
66     // frame internal is two on a display that refreshes 60 times a second. The default
67     // frame interval setting of one will fire 60 times a second when the display refreshes
68     // at 60 times a second. A frame interval setting of less than one results in undefined
69     // behavior.
70     if (frameInterval >= 1)
71     {
72         mLastFrameTime = frameInterval;
73     }
74 }
75 
76 - (void)go {
77 
78     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
79     mLastFrameTime = 1;
80     mStartTime = 0;
81     mTimer = nil;
82 
83     try {
84         demo.startDemo();
85 
86         Ogre::Root::getSingleton().getRenderSystem()->_initRenderTargets();
87 
88         // Clear event times
89 		Ogre::Root::getSingleton().clearEventTimes();
catch(Ogre::Exception & e)90     } catch( Ogre::Exception& e ) {
91         std::cerr << "An exception has occurred: " <<
92         e.getFullDescription().c_str() << std::endl;
93     }
94 
95     mTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)(1.0f / 60.0f) * mLastFrameTime
96                                               target:self
97                                             selector:@selector(renderOneFrame:)
98                                             userInfo:nil
99                                              repeats:YES];
100     [pool release];
101 }
102 
103 - (void)applicationDidFinishLaunching:(NSNotification *)application {
104     mLastFrameTime = 1;
105     mStartTime = 0;
106     mTimer = nil;
107 
108     [self go];
109 }
110 
111 - (void)renderOneFrame:(id)sender
112 {
113     if(!OgreFramework::getSingletonPtr()->isOgreToBeShutDown() &&
114        Ogre::Root::getSingletonPtr() && Ogre::Root::getSingleton().isInitialised())
115     {
116 		if(OgreFramework::getSingletonPtr()->m_pRenderWnd->isActive())
117 		{
118 			mStartTime = OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU();
119 
120 			OgreFramework::getSingletonPtr()->m_pKeyboard->capture();
121 			OgreFramework::getSingletonPtr()->m_pMouse->capture();
122 
123 			OgreFramework::getSingletonPtr()->updateOgre(mLastFrameTime);
124 			OgreFramework::getSingletonPtr()->m_pRoot->renderOneFrame();
125 
126 			mLastFrameTime = OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU() - mStartTime;
127 		}
128     }
129     else
130     {
131         [mTimer invalidate];
132         mTimer = nil;
133         [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
134     }
135 }
136 
137 - (void)dealloc {
138     if(mTimer)
139     {
140         [mTimer invalidate];
141         mTimer = nil;
142     }
143 
144     [super dealloc];
145 }
146 
147 @end
148 
149 #endif
150 
151 #endif
152