2018-04-21 21:24:46 +08:00
# include "..\e2dbase.h"
2018-08-15 23:30:23 +08:00
# include "..\e2dnode.h"
# include "..\e2dtransition.h"
2018-04-21 21:24:46 +08:00
# include "..\e2dmanager.h"
2018-04-24 09:02:06 +08:00
# include "..\e2dtool.h"
2018-07-21 22:57:21 +08:00
# include <thread>
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
e2d : : Game * e2d : : Game : : instance_ = nullptr ;
2018-08-28 00:06:10 +08:00
2018-09-04 22:42:34 +08:00
e2d : : Game * e2d : : Game : : GetInstance ( )
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if ( ! instance_ )
instance_ = new ( std : : nothrow ) Game ;
return instance_ ;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : DestroyInstance ( )
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if ( instance_ )
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
delete instance_ ;
instance_ = nullptr ;
2018-08-28 00:06:10 +08:00
}
}
2018-07-03 01:49:20 +08:00
e2d : : Game : : Game ( )
2018-09-04 22:42:34 +08:00
: quit_ ( true )
, paused_ ( false )
, curr_scene_ ( nullptr )
, next_scene_ ( nullptr )
, transition_ ( nullptr )
2018-07-03 01:49:20 +08:00
{
2018-09-02 14:03:04 +08:00
: : CoInitialize ( nullptr ) ;
2018-07-03 01:49:20 +08:00
}
2018-07-03 18:16:26 +08:00
e2d : : Game : : ~ Game ( )
{
2018-09-02 14:03:04 +08:00
: : CoUninitialize ( ) ;
2018-07-03 18:16:26 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : Start ( )
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
quit_ = false ;
auto window = Window : : GetInstance ( ) ;
auto input = Input : : GetInstance ( ) ;
auto renderer = Renderer : : GetInstance ( ) ;
auto timer = Timer : : GetInstance ( ) ;
2018-07-29 13:44:53 +08:00
2018-08-02 00:27:45 +08:00
const int minInterval = 5 ;
2018-09-04 22:42:34 +08:00
Time last = Time : : Now ( ) ;
HWND hWnd = window - > GetHWnd ( ) ;
2018-08-02 00:27:45 +08:00
2018-07-21 22:57:21 +08:00
: : ShowWindow ( hWnd , SW_SHOWNORMAL ) ;
: : UpdateWindow ( hWnd ) ;
2018-09-04 22:42:34 +08:00
window - > Poll ( ) ;
UpdateScene ( ) ;
2018-07-29 01:43:15 +08:00
2018-09-04 22:42:34 +08:00
while ( ! quit_ )
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
auto now = Time : : Now ( ) ;
2018-08-02 00:27:45 +08:00
auto dur = now - last ;
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
if ( dur . Milliseconds ( ) > minInterval )
2018-01-30 16:45:38 +08:00
{
2018-08-02 00:27:45 +08:00
last = now ;
2018-09-04 22:42:34 +08:00
input - > Update ( ) ;
2018-08-19 15:11:20 +08:00
2018-09-04 22:42:34 +08:00
if ( ! paused_ )
2018-08-19 15:11:20 +08:00
{
2018-09-04 22:42:34 +08:00
timer - > Update ( ) ;
UpdateScene ( ) ;
2018-08-19 15:11:20 +08:00
}
2018-09-04 22:42:34 +08:00
DrawScene ( ) ;
window - > Poll ( ) ;
GC : : GetInstance ( ) - > Flush ( ) ;
2018-01-30 16:45:38 +08:00
}
else
{
2018-08-12 14:05:12 +08:00
// ID2D1HwndRenderTarget <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ˴ <EFBFBD> ֱͬ<D6B1> <CDAC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ⱦʱ<C8BE> <CAB1> <EFBFBD> ȴ<EFBFBD> <C8B4> <EFBFBD> ʾ <EFBFBD> <CABE> ˢ<EFBFBD> £<EFBFBD>
2018-07-29 13:44:53 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ˷dz<CBB7> <C7B3> ȶ<EFBFBD> <C8B6> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> ã<EFBFBD> <C3A3> <EFBFBD> <EFBFBD> Դ<D4B4> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ҫ<EFBFBD> ֶ<EFBFBD> <D6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ߳̽<DFB3> <CCBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1>
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> Ĵ<EFBFBD> <C4B4> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ Щ<D2BB> <D0A9> <EFBFBD> <EFBFBD> <EFBFBD> £<EFBFBD> <C2A3> <EFBFBD> <EFBFBD> 細<EFBFBD> <E7B4B0> <EFBFBD> <EFBFBD> С <EFBFBD> <D0A1> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ̣߳<DFB3> <CCA3> <EFBFBD> ֹռ <D6B9> ù<EFBFBD> <C3B9> <EFBFBD> CPU <20> <>
2018-09-04 22:42:34 +08:00
int wait = minInterval - dur . Milliseconds ( ) ;
2018-07-29 13:44:53 +08:00
if ( wait > 1 )
{
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( wait ) ) ;
}
2018-01-30 16:45:38 +08:00
}
}
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : Pause ( )
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
paused_ = true ;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : Resume ( )
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
if ( paused_ & & ! quit_ )
2018-05-24 00:58:16 +08:00
{
2018-09-04 22:42:34 +08:00
Timer : : GetInstance ( ) - > UpdateTime ( ) ;
2018-09-05 00:08:03 +08:00
if ( curr_scene_ )
curr_scene_ - > UpdateActionsTime ( ) ;
2018-05-24 00:58:16 +08:00
}
2018-09-04 22:42:34 +08:00
paused_ = false ;
2018-05-10 18:18:02 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d : : Game : : IsPaused ( )
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return paused_ ;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : Quit ( )
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
quit_ = true ;
2018-01-30 16:45:38 +08:00
}
2018-09-04 23:20:00 +08:00
void e2d : : Game : : EnterScene ( Scene * scene )
2018-01-30 16:45:38 +08:00
{
2018-08-15 23:30:23 +08:00
if ( ! scene )
return ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8>
2018-09-04 22:42:34 +08:00
if ( next_scene_ ) next_scene_ - > Release ( ) ;
next_scene_ = scene ;
next_scene_ - > Retain ( ) ;
2018-08-15 23:30:23 +08:00
}
2018-09-04 23:20:00 +08:00
void e2d : : Game : : EnterScene ( Transition * transition )
2018-08-15 23:30:23 +08:00
{
if ( ! transition )
return ;
2018-09-04 23:20:00 +08:00
EnterScene ( transition - > in_scene_ ) ;
2018-08-15 23:30:23 +08:00
2018-09-04 22:42:34 +08:00
if ( transition_ )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
transition_ - > Stop ( ) ;
transition_ - > Release ( ) ;
2018-08-15 23:30:23 +08:00
}
2018-09-04 22:42:34 +08:00
transition_ = transition ;
transition_ - > Retain ( ) ;
2018-08-15 23:30:23 +08:00
// <20> <> ʼ <EFBFBD> <CABC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> л<EFBFBD> <D0BB> <EFBFBD> <EFBFBD> <EFBFBD>
2018-09-04 22:42:34 +08:00
if ( ! transition_ - > Init ( this , curr_scene_ ) )
2018-08-15 23:30:23 +08:00
{
WARN ( " Transition initialize failed! " ) ;
2018-09-04 22:42:34 +08:00
transition_ - > Release ( ) ;
transition_ = nullptr ;
2018-08-15 23:30:23 +08:00
}
}
2018-09-04 22:42:34 +08:00
e2d : : Scene * e2d : : Game : : GetCurrentScene ( )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
return curr_scene_ ;
2018-08-15 23:30:23 +08:00
}
2018-09-04 22:42:34 +08:00
bool e2d : : Game : : IsTransitioning ( ) const
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
return transition_ ! = nullptr ;
2018-08-15 23:30:23 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : UpdateScene ( )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
if ( transition_ )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
transition_ - > Update ( ) ;
2018-08-15 23:30:23 +08:00
2018-09-04 22:42:34 +08:00
if ( transition_ - > IsDone ( ) )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
transition_ - > Release ( ) ;
transition_ = nullptr ;
2018-08-15 23:30:23 +08:00
}
else
{
return ;
}
}
2018-09-04 22:42:34 +08:00
if ( next_scene_ )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
if ( curr_scene_ )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
curr_scene_ - > OnExit ( ) ;
2018-09-04 23:20:00 +08:00
curr_scene_ - > Release ( ) ;
2018-08-15 23:30:23 +08:00
}
2018-09-04 22:42:34 +08:00
next_scene_ - > OnEnter ( ) ;
2018-08-15 23:30:23 +08:00
2018-09-04 22:42:34 +08:00
curr_scene_ = next_scene_ ;
next_scene_ = nullptr ;
2018-08-15 23:30:23 +08:00
}
}
2018-09-04 22:42:34 +08:00
void e2d : : Game : : DrawScene ( )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
Renderer : : GetInstance ( ) - > BeginDraw ( ) ;
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
if ( transition_ )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
transition_ - > Draw ( ) ;
2018-08-15 23:30:23 +08:00
}
2018-09-04 22:42:34 +08:00
else if ( curr_scene_ )
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
curr_scene_ - > Visit ( ) ;
2018-08-15 23:30:23 +08:00
}
}
2018-09-04 22:42:34 +08:00
Renderer : : GetInstance ( ) - > EndDraw ( ) ;
2018-01-30 16:45:38 +08:00
}