OSDN Git Service

make part10 demo
[moflib/moflib.git] / moflib-1.0 / src / mof / Application.cpp
1 #include "mof/Application.hpp"
2 #include "mof/GraphicsDevice.hpp"
3 #include "mof/Window.hpp"
4 #include "mof/ConsoleIO.hpp"
5 #include "mof/Timer.hpp"
6 #include <stdexcept>
7 #include "mof/Captor.hpp"
8 #include "mof/InputDevice.hpp"
9 #include "mof/private/GraphicsDeviceImpl.hpp"
10 #include "mof/utilities.hpp"
11 #include "mof/Finalizer.hpp"
12
13 #include "mof/sound/SoundDevice.hpp"
14
15 namespace 
16 {
17     double frame_interval_;///< \83t\83\8c\81[\83\80\82Ì\8aÔ\8au\81i\83~\83\8a\95b\81j
18     int m_nUpdatingPerFrame;
19     mof::Window* m_pWindow = NULL;
20         bool enable_output_frame_;
21         bool enable_output_audio_;
22         mof::Application::InputMode input_mode_;
23 }
24
25 namespace mof
26 {
27     void Application::initialize
28         ( 
29             const mof::tstring& appname , 
30             int width , 
31             int height ,
32             bool fullscreen ,
33             double fps, 
34             int nUpdatingPerFrame,
35                 InputMode input_mode,
36                 bool enable_output_frame,
37                 bool enable_output_audio
38         )
39     {
40                 frame_interval_ = 1.0f / fps * 1000;
41         m_nUpdatingPerFrame = nUpdatingPerFrame;  
42                 enable_output_frame_ = enable_output_frame;
43                 enable_output_audio_ = enable_output_audio;
44                 input_mode_ = input_mode;
45
46         m_pWindow = new mof::Window( appname , width , height , fullscreen );
47         mof::GraphicsDevice::initialize( m_pWindow->getHandler(), 0, width , height , fullscreen );
48             mof::InputDevice::initialize();
49                 mof::sound::SoundDevice::initialize(m_pWindow->getHandler(), enable_output_audio_);
50  
51     }
52     
53     void Application::finalize( )
54     {
55         mof::GraphicsDevice::finalize();
56         mof::InputDevice::finalize();
57         mof::sound::SoundDevice::finalize();
58         delete m_pWindow;
59     }
60     
61 //{{{ run 
62     void Application::run(scene& s) 
63     {
64             MSG msg;
65         ZeroMemory( &msg, sizeof(msg) );
66             mof::Timer timer;
67             mof::Timer fpsTimer;
68             int frameCount = 0;
69
70         mof::Captor Captor( _T("output"));
71         int sleepingTime = 0;
72         for( ; ; )
73         {
74             while( PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE ) )
75             {
76                                 // \83^\83C\83}\82ð\82Æ\82ß\82é
77                                 timer.pause();
78                                 fpsTimer.pause();
79                         if( msg.message == WM_QUIT )return;
80                         GetMessage( &msg , NULL , 0 , 0 );
81                 TranslateMessage( &msg );
82                 DispatchMessage( &msg );
83             }
84                         // \83^\83C\83}\82ª\8e~\82Ü\82Á\82Ä\82¢\82ê\82Î\81A\8dÄ\8a
85                         timer.start();
86                         fpsTimer.start();
87                 
88                     for( int i = 0 ; i < m_nUpdatingPerFrame ; i++ )s.update();//\83V\81[\83\93\82Ì\8f\88\97\9d
89
90                 
91                     mof::GraphicsDevice::beginScene();
92                     s.draw();//\83V\81[\83\93\82Ì\95`\89æ
93                     if (enable_output_frame_) Captor.capture();
94                     mof::GraphicsDevice::endScene();
95
96                     //FPS\82Ì\8cv\91ª
97                     double true_past_time = timer.getTime();
98                         double past_time = frame_interval_ * (frameCount + 1);
99                     if(past_time > true_past_time) {
100                             Sleep(static_cast<int>(past_time - true_past_time));
101                             sleepingTime += past_time - true_past_time;
102                     }
103                     
104                     //timer.reset();
105                     
106                     if(++frameCount % 500 == 0)
107                     {
108                             float fps = 500.0f / static_cast<float>(fpsTimer.getTime() - sleepingTime) * 1000;
109                             fpsTimer.reset();
110                             sleepingTime = 0;
111                             DEBUG_PRINT( "FPS --- " << fps );
112                         }
113             } // for
114                 
115
116     } 
117 //}}}
118         
119     void Application::quit( )
120     {
121             PostQuitMessage(0);
122     }
123
124
125         Application::InputMode Application::input_mode() {
126                 return input_mode_;
127         }
128 }
129
130