2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2018-09-05 13:17:07 +08:00
# include "..\e2dmodule.h"
2018-09-06 23:26:32 +08:00
# include "..\e2dobject.h"
2018-10-03 18:04:04 +08:00
# include "..\e2dtool.h"
2018-08-15 23:30:23 +08:00
# include "..\e2dtransition.h"
2018-07-21 22:57:21 +08:00
# include <thread>
2018-09-30 14:54:43 +08:00
# include <imm.h>
# pragma comment (lib ,"imm32.lib")
2018-07-21 22:57:21 +08:00
2018-09-30 14:54:43 +08:00
# define WINDOW_STYLE WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME
# define REGISTER_CLASS L"Easy2DApp"
2018-01-30 16:45:38 +08:00
2018-08-28 00:06:10 +08:00
2018-10-03 18:04:04 +08:00
static e2d : : Game * instance = nullptr ;
2018-08-28 00:06:10 +08:00
2018-07-03 01:49:20 +08:00
e2d : : Game : : Game ( )
2018-09-30 14:54:43 +08:00
: hwnd_ ( nullptr )
, quit_ ( true )
2018-09-04 22:42:34 +08:00
, curr_scene_ ( nullptr )
, next_scene_ ( nullptr )
, transition_ ( nullptr )
2018-10-03 18:04:04 +08:00
, title_ ( L " Easy2D Game " )
, width_ ( 640 )
, height_ ( 480 )
, icon_ ( 0 )
, debug_mode_ ( false )
2018-07-03 01:49:20 +08:00
{
2018-10-03 18:04:04 +08:00
if ( instance )
{
throw RuntimeException ( " ͬʱֻ<EFBFBD> ܴ<EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϸʵ<EFBFBD> <EFBFBD> " ) ;
}
instance = this ;
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-07 00:28:54 +08:00
SafeRelease ( transition_ ) ;
SafeRelease ( curr_scene_ ) ;
SafeRelease ( next_scene_ ) ;
2018-10-03 18:04:04 +08:00
Image : : ClearCache ( ) ;
Device : : Destroy ( ) ;
2018-09-30 14:54:43 +08:00
if ( hwnd_ )
{
: : DestroyWindow ( hwnd_ ) ;
}
2018-10-03 18:04:04 +08:00
instance = nullptr ;
2018-09-02 14:03:04 +08:00
: : CoUninitialize ( ) ;
2018-07-03 18:16:26 +08:00
}
2018-10-03 18:04:04 +08:00
void e2d : : Game : : Run ( const Options & options )
2018-09-30 14:54:43 +08:00
{
2018-10-03 18:04:04 +08:00
title_ = options . title ;
width_ = options . width ;
height_ = options . height ;
icon_ = options . icon ;
debug_mode_ = options . debug_mode ;
2018-09-30 14:54:43 +08:00
2018-10-03 18:04:04 +08:00
// <20> <> ʼ <EFBFBD> <CABC>
Init ( ) ;
2018-09-30 14:54:43 +08:00
2018-10-03 18:04:04 +08:00
// <20> <> ʼ
Start ( ) ;
2018-09-30 14:54:43 +08:00
2018-10-03 18:04:04 +08:00
// ˢ<> ³<EFBFBD> <C2B3> <EFBFBD>
: : ShowWindow ( hwnd_ , SW_SHOWNORMAL ) ;
: : UpdateWindow ( hwnd_ ) ;
UpdateScene ( ) ;
2018-07-29 13:44:53 +08:00
2018-10-03 18:04:04 +08:00
// <20> <> <EFBFBD> <EFBFBD>
2018-09-30 14:54:43 +08:00
const int min_interval = 5 ;
2018-09-04 22:42:34 +08:00
Time last = Time : : Now ( ) ;
2018-09-30 14:54:43 +08:00
MSG msg = { 0 } ;
2018-08-02 00:27:45 +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-30 14:54:43 +08:00
if ( dur . Milliseconds ( ) > min_interval )
2018-01-30 16:45:38 +08:00
{
2018-08-02 00:27:45 +08:00
last = now ;
2018-10-03 18:04:04 +08:00
Device : : GetInput ( ) - > Flush ( ) ;
2018-08-19 15:11:20 +08:00
2018-09-07 23:47:21 +08:00
UpdateScene ( ) ;
2018-09-04 22:42:34 +08:00
DrawScene ( ) ;
2018-09-07 23:47:21 +08:00
2018-09-30 14:54:43 +08:00
while ( : : PeekMessage ( & msg , nullptr , 0 , 0 , PM_REMOVE ) )
{
: : TranslateMessage ( & msg ) ;
: : DispatchMessage ( & msg ) ;
}
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-30 14:54:43 +08:00
int wait = min_interval - 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 : : 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-10 15:28:19 +08:00
void e2d : : Game : : EnterScene ( Scene * scene , Transition * transition )
2018-01-30 16:45:38 +08:00
{
2018-09-10 15:28:19 +08:00
if ( scene = = nullptr )
{
WARN ( " Next scene is null pointer! " ) ;
2018-08-15 23:30:23 +08:00
return ;
2018-09-10 15:28:19 +08:00
}
if ( next_scene_ ! = nullptr )
{
WARN ( " Scene is transitioning... " ) ;
return ;
}
2018-08-15 23:30:23 +08:00
2018-09-07 00:28:54 +08:00
if ( next_scene_ )
{
next_scene_ - > Release ( ) ;
}
2018-09-04 22:42:34 +08:00
next_scene_ = scene ;
next_scene_ - > Retain ( ) ;
2018-08-15 23:30:23 +08:00
2018-09-10 15:28:19 +08:00
if ( transition )
2018-08-15 23:30:23 +08:00
{
2018-09-10 15:28:19 +08:00
if ( transition_ )
{
transition_ - > Stop ( ) ;
transition_ - > Release ( ) ;
}
transition_ = transition ;
transition_ - > Retain ( ) ;
2018-08-15 23:30:23 +08:00
2018-10-03 18:04:04 +08:00
transition_ - > Init ( curr_scene_ , next_scene_ , this ) ;
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-10-03 22:43:21 +08:00
if ( curr_scene_ )
{
curr_scene_ - > Update ( ) ;
}
if ( next_scene_ )
{
next_scene_ - > Update ( ) ;
}
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-10-03 18:04:04 +08:00
auto graphics = Device : : GetGraphics ( ) ;
2018-09-30 14:54:43 +08:00
graphics - > BeginDraw ( ) ;
if ( transition_ )
2018-08-15 23:30:23 +08:00
{
2018-09-30 14:54:43 +08:00
transition_ - > Draw ( ) ;
}
else if ( curr_scene_ )
{
curr_scene_ - > Draw ( ) ;
}
if ( debug_mode_ )
{
2018-10-03 18:04:04 +08:00
if ( curr_scene_ & & curr_scene_ - > GetRoot ( ) )
{
graphics - > GetRenderTarget ( ) - > SetTransform ( D2D1 : : Matrix3x2F : : Identity ( ) ) ;
graphics - > GetSolidBrush ( ) - > SetOpacity ( 1.f ) ;
curr_scene_ - > GetRoot ( ) - > DrawBorder ( ) ;
}
if ( next_scene_ & & next_scene_ - > GetRoot ( ) )
{
graphics - > GetRenderTarget ( ) - > SetTransform ( D2D1 : : Matrix3x2F : : Identity ( ) ) ;
graphics - > GetSolidBrush ( ) - > SetOpacity ( 1.f ) ;
next_scene_ - > GetRoot ( ) - > DrawBorder ( ) ;
}
2018-09-30 14:54:43 +08:00
graphics - > DrawDebugInfo ( ) ;
}
graphics - > EndDraw ( ) ;
}
2018-10-03 18:04:04 +08:00
e2d : : Game * e2d : : Game : : GetInstance ( )
{
return instance ;
}
2018-09-30 14:54:43 +08:00
void e2d : : Game : : Init ( )
{
WNDCLASSEX wcex = { 0 } ;
wcex . cbSize = sizeof ( WNDCLASSEX ) ;
wcex . lpszClassName = REGISTER_CLASS ;
wcex . style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wcex . lpfnWndProc = Game : : WndProc ;
wcex . hIcon = nullptr ;
wcex . cbClsExtra = 0 ;
wcex . cbWndExtra = sizeof ( LONG_PTR ) ;
wcex . hInstance = HINST_THISCOMPONENT ;
wcex . hbrBackground = nullptr ;
wcex . lpszMenuName = nullptr ;
wcex . hCursor = : : LoadCursor ( nullptr , IDC_ARROW ) ;
if ( icon_ ! = 0 )
{
wcex . hIcon = ( HICON ) : : LoadImage (
HINST_THISCOMPONENT ,
MAKEINTRESOURCE ( icon_ ) ,
IMAGE_ICON ,
0 ,
0 ,
LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE
) ;
}
// ע<> ᴰ<EFBFBD> <E1B4B0> <EFBFBD> <EFBFBD>
RegisterClassEx ( & wcex ) ;
// <20> <> <EFBFBD> 㴰<EFBFBD> ڴ<EFBFBD> С
Rect client_rect = Locate ( width_ , height_ ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
hwnd_ = : : CreateWindowEx (
NULL ,
REGISTER_CLASS ,
( LPCTSTR ) title_ ,
WINDOW_STYLE ,
int ( client_rect . origin . x ) ,
int ( client_rect . origin . y ) ,
int ( client_rect . size . width ) ,
int ( client_rect . size . height ) ,
nullptr ,
nullptr ,
HINST_THISCOMPONENT ,
this
) ;
2018-10-03 18:04:04 +08:00
if ( hwnd_ = = nullptr )
2018-09-30 14:54:43 +08:00
{
2018-10-03 18:04:04 +08:00
: : UnregisterClass ( REGISTER_CLASS , HINST_THISCOMPONENT ) ;
throw RuntimeException ( " Create window failed " ) ;
return ;
}
// <20> <> ʼ <EFBFBD> <CABC> <EFBFBD> 豸
Device : : Init ( hwnd_ ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> 뷨
: : ImmAssociateContext ( hwnd_ , nullptr ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ˵<EFBFBD> <CBB5> <EFBFBD> ģʽ <C4A3> <CABD> <EFBFBD> <EFBFBD> <F2BFAABF> <EFBFBD> ̨
HWND console = : : GetConsoleWindow ( ) ;
// <20> رտ<D8B1> <D5BF> <EFBFBD> ̨
if ( debug_mode_ )
{
if ( console )
2018-09-30 14:54:43 +08:00
{
2018-10-03 18:04:04 +08:00
: : ShowWindow ( console , SW_SHOWNORMAL ) ;
}
else
{
// <20> <> ʾ һ <CABE> <D2BB> <EFBFBD> ¿<EFBFBD> <C2BF> <EFBFBD> ̨
if ( : : AllocConsole ( ) )
{
console = : : GetConsoleWindow ( ) ;
// <20> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
FILE * stdoutStream , * stdinStream , * stderrStream ;
freopen_s ( & stdoutStream , " conout$ " , " w+t " , stdout ) ;
freopen_s ( & stdinStream , " conin$ " , " r+t " , stdin ) ;
freopen_s ( & stderrStream , " conout$ " , " w+t " , stderr ) ;
// <20> <> <EFBFBD> ÿ<EFBFBD> <C3BF> <EFBFBD> ̨<EFBFBD> رհ <D8B1> ť
HMENU hmenu = : : GetSystemMenu ( console , FALSE ) ;
: : RemoveMenu ( hmenu , SC_CLOSE , MF_BYCOMMAND ) ;
}
2018-09-30 14:54:43 +08:00
}
}
else
{
2018-10-03 18:04:04 +08:00
if ( console )
{
: : ShowWindow ( console , SW_HIDE ) ;
}
2018-09-30 14:54:43 +08:00
}
2018-10-03 18:04:04 +08:00
quit_ = false ;
2018-09-30 14:54:43 +08:00
}
e2d : : Rect e2d : : Game : : Locate ( int width , int height )
{
int max_width = : : GetSystemMetrics ( SM_CXSCREEN ) ;
int max_height = : : GetSystemMetrics ( SM_CYSCREEN ) ;
2018-10-03 18:04:04 +08:00
HDC hdc = : : GetDC ( 0 ) ;
int dpi_x = GetDeviceCaps ( hdc , LOGPIXELSX ) ;
int dpi_y = GetDeviceCaps ( hdc , LOGPIXELSY ) ;
2018-10-03 22:43:21 +08:00
: : ReleaseDC ( 0 , hdc ) ;
2018-09-30 14:54:43 +08:00
RECT rect = { 0 , 0 , LONG ( ceil ( width * dpi_x / 96.f ) ) , LONG ( ceil ( height * dpi_y / 96.f ) ) } ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʵĴ<CAB5> <C4B4> ڴ<EFBFBD> С
: : AdjustWindowRectEx ( & rect , WINDOW_STYLE , FALSE , NULL ) ;
width = static_cast < int > ( rect . right - rect . left ) ;
height = static_cast < int > ( rect . bottom - rect . top ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ĵ<EFBFBD> <C4B4> ڴ<EFBFBD> С <EFBFBD> ȷֱ<C8B7> <D6B1> ʴ<EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
WARN_IF ( max_width < width | | max_height < height , " The window Is larger than screen! " ) ;
width = std : : min ( width , max_width ) ;
height = std : : min ( height , max_height ) ;
Rect client_rect (
static_cast < float > ( ( max_width - width ) / 2 ) ,
static_cast < float > ( ( max_height - height ) / 2 ) ,
static_cast < float > ( width ) ,
static_cast < float > ( height )
) ;
return std : : move ( client_rect ) ;
}
int e2d : : Game : : GetWidth ( ) const
{
return width_ ;
}
int e2d : : Game : : GetHeight ( ) const
{
return height_ ;
}
e2d : : Size e2d : : Game : : GetSize ( ) const
{
e2d : : Size size (
static_cast < float > ( width_ ) ,
static_cast < float > ( height_ )
) ;
return std : : move ( size ) ;
}
HWND e2d : : Game : : GetHWnd ( ) const
{
return hwnd_ ;
}
const e2d : : String & e2d : : Game : : GetTitle ( ) const
{
return title_ ;
}
void e2d : : Game : : SetSize ( int width , int height )
{
if ( width_ = = width & & height_ = = height )
return ;
width_ = width ;
height_ = height ;
2018-10-03 18:04:04 +08:00
if ( hwnd_ )
{
Rect rect = Locate ( width , height ) ;
: : MoveWindow (
hwnd_ ,
int ( rect . origin . x ) ,
int ( rect . origin . y ) ,
int ( rect . size . width ) ,
int ( rect . size . height ) ,
TRUE
) ;
}
2018-09-30 14:54:43 +08:00
}
void e2d : : Game : : SetTitle ( const String & title )
{
title_ = title ;
2018-10-03 18:04:04 +08:00
if ( hwnd_ )
{
: : SetWindowText ( hwnd_ , ( LPCWSTR ) title ) ;
}
2018-09-30 14:54:43 +08:00
}
void e2d : : Game : : SetIcon ( int resource_id )
{
icon_ = resource_id ;
2018-10-03 18:04:04 +08:00
if ( hwnd_ )
{
HICON icon = ( HICON ) : : LoadImage (
HINST_THISCOMPONENT ,
MAKEINTRESOURCE ( resource_id ) ,
IMAGE_ICON ,
0 ,
0 ,
LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE
) ;
: : SendMessage ( hwnd_ , WM_SETICON , ICON_BIG , ( LPARAM ) icon ) ;
: : SendMessage ( hwnd_ , WM_SETICON , ICON_SMALL , ( LPARAM ) icon ) ;
}
2018-09-30 14:54:43 +08:00
}
LRESULT e2d : : Game : : WndProc ( HWND hwnd , UINT msg , WPARAM w_param , LPARAM l_param )
{
LRESULT result = 0 ;
if ( msg = = WM_CREATE )
{
LPCREATESTRUCT pcs = ( LPCREATESTRUCT ) l_param ;
Game * game = ( Game * ) pcs - > lpCreateParams ;
: : SetWindowLongPtrW (
hwnd ,
GWLP_USERDATA ,
PtrToUlong ( game )
) ;
result = 1 ;
}
else
{
bool has_handled = false ;
Game * game = reinterpret_cast < Game * > (
static_cast < LONG_PTR > (
: : GetWindowLongPtrW ( hwnd , GWLP_USERDATA )
)
) ;
switch ( msg )
2018-08-15 23:30:23 +08:00
{
2018-09-30 14:54:43 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
case WM_LBUTTONUP :
case WM_LBUTTONDOWN :
case WM_LBUTTONDBLCLK :
case WM_MBUTTONUP :
case WM_MBUTTONDOWN :
case WM_MBUTTONDBLCLK :
case WM_RBUTTONUP :
case WM_RBUTTONDOWN :
case WM_RBUTTONDBLCLK :
case WM_MOUSEMOVE :
case WM_MOUSEWHEEL :
{
if ( game - > IsTransitioning ( ) )
break ;
auto curr_scene = game - > GetCurrentScene ( ) ;
if ( curr_scene )
{
curr_scene - > Dispatch ( MouseEvent ( msg , w_param , l_param ) ) ;
}
}
result = 0 ;
has_handled = true ;
break ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
case WM_KEYDOWN :
case WM_KEYUP :
{
if ( game - > IsTransitioning ( ) )
break ;
auto curr_scene = game - > GetCurrentScene ( ) ;
if ( curr_scene )
{
curr_scene - > Dispatch ( KeyEvent ( msg , w_param , l_param ) ) ;
}
}
result = 0 ;
has_handled = true ;
break ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ڴ<EFBFBD> С <EFBFBD> 仯<EFBFBD> <E4BBAF> Ϣ
case WM_SIZE :
{
UINT width = LOWORD ( l_param ) ;
UINT height = HIWORD ( l_param ) ;
if ( w_param = = SIZE_RESTORED )
{
2018-10-03 18:04:04 +08:00
HDC hdc = : : GetDC ( 0 ) ;
int dpi_x = GetDeviceCaps ( hdc , LOGPIXELSX ) ;
int dpi_y = GetDeviceCaps ( hdc , LOGPIXELSY ) ;
2018-10-03 22:43:21 +08:00
: : ReleaseDC ( 0 , hdc ) ;
2018-09-30 14:54:43 +08:00
game - > width_ = static_cast < int > ( width * 96.f / dpi_x ) ;
game - > height_ = static_cast < int > ( height * 96.f / dpi_y ) ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> յ<EFBFBD> һ <EFBFBD> <D2BB> WM_SIZE <20> <> Ϣ<EFBFBD> <CFA2> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ⱦ
// Ŀ<> <C4BF> <EFBFBD> Ĵ<EFBFBD> С <EFBFBD> <D0A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ܻ<EFBFBD> <DCBB> <EFBFBD> <EFBFBD> <EFBFBD> ʧ<EFBFBD> ܣ<EFBFBD> <DCA3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ժ<EFBFBD> <D4BA> <EFBFBD> <EFBFBD> п<EFBFBD> <D0BF> ܵ<EFBFBD>
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ<EFBFBD> <CEAA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> ε<EFBFBD> <CEB5> <EFBFBD> EndDraw ʱ<> <CAB1> <EFBFBD> <EFBFBD>
2018-10-03 18:04:04 +08:00
auto render_target = Device : : GetGraphics ( ) - > GetRenderTarget ( ) ;
2018-09-30 14:54:43 +08:00
if ( render_target )
{
2018-10-03 18:04:04 +08:00
render_target - > Resize ( D2D1 : : SizeU ( game - > width_ , game - > height_ ) ) ;
2018-09-30 14:54:43 +08:00
}
2018-08-15 23:30:23 +08:00
}
2018-09-30 14:54:43 +08:00
break ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ڱ<EFBFBD> <DAB1> <EFBFBD> <EFBFBD> 仯<EFBFBD> <E4BBAF> Ϣ
case WM_SETTEXT :
{
game - > title_ = ( const wchar_t * ) l_param ;
}
break ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ֱ<EFBFBD> <D6B1> ʱ仯<CAB1> <E4BBAF> Ϣ
case WM_DISPLAYCHANGE :
{
// <20> ػ<EFBFBD> <D8BB> ͻ<EFBFBD> <CDBB> <EFBFBD>
: : InvalidateRect ( hwnd , nullptr , FALSE ) ;
}
result = 0 ;
has_handled = true ;
break ;
// <20> ػ洰<D8BB> <E6B4B0>
case WM_PAINT :
{
game - > DrawScene ( ) ;
: : ValidateRect ( hwnd , nullptr ) ;
}
result = 0 ;
has_handled = true ;
break ;
// <20> <> <EFBFBD> ڹر<DAB9> <D8B1> <EFBFBD> Ϣ
case WM_CLOSE :
{
2018-10-03 22:43:21 +08:00
if ( game - > OnExit ( ) )
2018-09-30 14:54:43 +08:00
{
game - > Quit ( ) ;
}
}
result = 0 ;
has_handled = true ;
break ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
case WM_DESTROY :
{
: : PostQuitMessage ( 0 ) ;
}
result = 1 ;
has_handled = true ;
break ;
}
if ( ! has_handled )
2018-08-15 23:30:23 +08:00
{
2018-09-30 14:54:43 +08:00
result = : : DefWindowProc ( hwnd , msg , w_param , l_param ) ;
2018-08-15 23:30:23 +08:00
}
}
2018-09-30 14:54:43 +08:00
return result ;
2018-01-30 16:45:38 +08:00
}