Magic_Game/Easy2D/Base/EApp.cpp

645 lines
13 KiB
C++
Raw Normal View History

#include "..\ebase.h"
2017-10-10 01:14:03 +08:00
#include "..\Win\winbase.h"
2017-10-13 20:16:31 +08:00
#include "..\emsg.h"
2017-10-13 11:42:36 +08:00
#include "..\etools.h"
2017-10-10 01:14:03 +08:00
#include <stack>
#include <chrono>
#include <thread>
using namespace std::chrono;
2017-10-12 02:44:44 +08:00
using namespace std::this_thread;
#ifndef HINST_THISCOMPONENT
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
#endif
2017-09-10 23:56:52 +08:00
e2d::EApp * s_pInstance = nullptr;
std::stack<e2d::EScene*> s_SceneStack;
2017-10-12 02:44:44 +08:00
e2d::EApp::EApp()
: m_bRunning(false)
2017-10-13 11:42:36 +08:00
, m_ClearColor(EColor::Black)
2017-10-12 02:44:44 +08:00
, m_pCurrentScene(nullptr)
, m_pNextScene(nullptr)
2017-09-10 23:56:52 +08:00
{
s_pInstance = this; // <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
2017-10-12 02:44:44 +08:00
e2d::EApp::~EApp()
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
SafeReleaseInterface(&GetFactory());
SafeReleaseInterface(&GetRenderTarget());
2017-09-10 23:56:52 +08:00
}
e2d::EApp * e2d::EApp::get()
2017-09-10 23:56:52 +08:00
{
2017-10-14 01:07:34 +08:00
ASSERT(s_pInstance != nullptr, "Nonexistent EApp instance.");
2017-10-12 02:44:44 +08:00
return s_pInstance; // <20><>ȡ EApp <20><>Ψһʵ<D2BB><CAB5>
2017-09-10 23:56:52 +08:00
}
bool e2d::EApp::init(e2d::EString title, e2d::ESize size, bool bShowConsole /* = false */)
2017-09-10 23:56:52 +08:00
{
return init(title, size.cx, size.cy, bShowConsole);
2017-10-12 02:44:44 +08:00
}
2017-10-14 01:07:34 +08:00
#include <iostream>
bool e2d::EApp::init(e2d::EString title, UINT32 width, UINT32 height, bool bShowConsole /* = false */)
2017-10-12 02:44:44 +08:00
{
HRESULT hr;
hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
2017-10-13 11:42:36 +08:00
// <20>رտ<D8B1><D5BF><EFBFBD>̨
2017-10-14 01:07:34 +08:00
if (bShowConsole)
2017-10-12 02:44:44 +08:00
{
2017-10-14 01:07:34 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD>̨
if (!GetConsoleWindow())
2017-10-12 02:44:44 +08:00
{
2017-10-14 01:07:34 +08:00
// <20><>ʾһ<CABE><D2BB><EFBFBD>¿<EFBFBD><C2BF><EFBFBD>̨
if (AllocConsole())
{
FILE *stream;
freopen_s(&stream, "CONOUT$", "w+t", stdout);
freopen_s(&stream, "CONOUT$", "w+t", stderr);
freopen_s(&stream, "CONIN$", "r+t", stdin);
}
else
{
MessageBox(nullptr, L"Alloc Console Failed!", L"Error", MB_OK);
}
2017-10-12 02:44:44 +08:00
}
}
2017-10-14 01:07:34 +08:00
else
{
FreeConsole();
}
2017-10-12 02:44:44 +08:00
// <20><>ʼ<EFBFBD><CABC> device-indpendent <20><>Դ
// <20><><EFBFBD><EFBFBD> Direct2D factory.
hr = _createDeviceIndependentResources();
2017-10-13 11:42:36 +08:00
}
if (SUCCEEDED(hr))
{
// ע<><EFBFBD><E1B4B0><EFBFBD><EFBFBD>
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = EApp::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcex.lpszClassName = L"E2DApp";
RegisterClassEx(&wcex);
// Because the CreateWindow function takes its size in pixels,
// obtain the system DPI and use it to scale the window size.
FLOAT dpiX, dpiY;
// The factory returns the current system DPI. This is also the value it will use
// to create its own windows.
GetFactory()->GetDesktopDpi(&dpiX, &dpiY);
m_sTitle = title;
// Create the window.
GetHWnd() = CreateWindow(
L"E2DApp",
m_sTitle.c_str(),
2017-10-13 17:14:00 +08:00
WS_OVERLAPPEDWINDOW | CS_DBLCLKS,
2017-10-13 11:42:36 +08:00
CW_USEDEFAULT,
CW_USEDEFAULT,
static_cast<UINT>(ceil(width * dpiX / 96.f)),
static_cast<UINT>(ceil(height * dpiY / 96.f)),
NULL,
NULL,
HINST_THISCOMPONENT,
this
);
2017-10-12 02:44:44 +08:00
2017-10-13 11:42:36 +08:00
hr = GetHWnd() ? S_OK : E_FAIL;
if (FAILED(hr))
2017-10-12 02:44:44 +08:00
{
2017-10-13 11:42:36 +08:00
UnregisterClass(L"E2DApp", HINST_THISCOMPONENT);
MessageBox(nullptr, L"Create Window Failed!", L"Error", MB_OK);
2017-10-12 02:44:44 +08:00
}
2017-10-14 01:07:34 +08:00
else
{
// <20><>ȡ<EFBFBD><C8A1>Ļ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
tagRECT rcWindow;
GetWindowRect(GetHWnd(), &rcWindow);
// <20>޸Ĵ<DEB8><C4B4>ڴ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
MoveWindow(
GetHWnd(),
(screenWidth - (rcWindow.right - rcWindow.left)) / 2,
(screenHeight - (rcWindow.bottom - rcWindow.top)) / 2,
(rcWindow.right - rcWindow.left),
(rcWindow.bottom - rcWindow.top),
FALSE
);
}
2017-10-12 02:44:44 +08:00
}
if (FAILED(hr))
{
MessageBox(nullptr, L"Initialize Failed!", L"Error", MB_OK);
}
2017-10-12 02:44:44 +08:00
return SUCCEEDED(hr);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ
void e2d::EApp::run()
{
2017-10-14 01:07:34 +08:00
ASSERT(m_pNextScene != nullptr, "Next scene NULL pointer exception.");
2017-10-13 11:42:36 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_enterNextScene();
2017-10-12 02:44:44 +08:00
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
ShowWindow(GetHWnd(), SW_SHOWNORMAL);
UpdateWindow(GetHWnd());
2017-09-10 23:56:52 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ
m_bRunning = true;
2017-10-12 02:44:44 +08:00
MSG msg;
2017-10-10 01:14:03 +08:00
2017-09-10 23:56:52 +08:00
while (m_bRunning)
{
2017-10-12 02:44:44 +08:00
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
2017-09-10 23:56:52 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
2017-10-12 02:44:44 +08:00
if (msg.message == WM_QUIT)
2017-10-10 01:14:03 +08:00
{
2017-10-12 02:44:44 +08:00
m_bRunning = false;
break;
2017-10-10 01:24:55 +08:00
}
2017-10-12 02:44:44 +08:00
TranslateMessage(&msg);
DispatchMessage(&msg);
2017-09-10 23:56:52 +08:00
}
else
{
// ִ<><D6B4><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
_mainLoop();
}
2017-09-10 23:56:52 +08:00
}
// <20><>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>һ<EFBFBD><D2BB>ѭ<EFBFBD><D1AD>
2017-10-14 01:07:34 +08:00
_onControl();
2017-09-10 23:56:52 +08:00
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ռ<EFBFBD><D5BC>
free();
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
void e2d::EApp::_mainLoop()
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
static steady_clock::time_point nNow;
static steady_clock::time_point nLast = steady_clock::now();
// ֡<><D6A1><EFBFBD><EFBFBD>
static LONGLONG nAnimationInterval = 17LL;
// ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static LONGLONG nInterval = 0LL;
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
static LONGLONG nWaitMS = 0L;
// ˢ<>¼<EFBFBD>ʱ
nNow = steady_clock::now();
// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
nInterval = duration_cast<milliseconds>(nNow - nLast).count();
// <20>жϼ<D0B6><CFBC><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7>
if (nInterval >= nAnimationInterval)
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
// <20><>¼<EFBFBD><C2BC>ǰʱ<C7B0><CAB1>
nLast = nNow;
2017-10-13 11:42:36 +08:00
// ִ<><D6B4><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
2017-10-12 02:44:44 +08:00
_onControl();
// ˢ<><CBA2><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD>
_onRender();
2017-09-10 23:56:52 +08:00
}
else
{
2017-10-12 02:44:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
nWaitMS = nAnimationInterval - nInterval - 1;
// <20><><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3>ͷ<EFBFBD> CPU ռ<><D5BC>
if (nWaitMS > 1LL)
{
std::this_thread::sleep_for(milliseconds(nWaitMS));
}
2017-09-10 23:56:52 +08:00
}
}
2017-10-12 02:44:44 +08:00
void e2d::EApp::_onControl()
2017-09-10 23:56:52 +08:00
{
2017-09-18 23:59:08 +08:00
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ָ<EFBFBD>벻Ϊ<EBB2BB><CEAA>ʱ<EFBFBD><CAB1><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
if (m_pNextScene)
2017-09-10 23:56:52 +08:00
{
2017-09-18 23:59:08 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
2017-09-10 23:56:52 +08:00
_enterNextScene();
}
// <20><><EFBFBD>Ե<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>
2017-10-14 01:07:34 +08:00
ASSERT(m_pCurrentScene != nullptr, "Current scene NULL pointer exception.");
2017-09-10 23:56:52 +08:00
2017-10-12 02:44:44 +08:00
//Timer::__exec(); // <20><>ʱ<EFBFBD><CAB1>ִ<EFBFBD>г<EFBFBD><D0B3><EFBFBD>
//ActionManager::__exec(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>г<EFBFBD><D0B3><EFBFBD>
2017-10-13 11:42:36 +08:00
EObjectManager::__flush(); // ˢ<><CBA2><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
// This method discards device-specific
// resources if the Direct3D device dissapears during execution and
// recreates the resources the next time it's invoked.
2017-10-14 01:07:34 +08:00
void e2d::EApp::_onRender()
2017-10-10 01:14:03 +08:00
{
2017-10-12 02:44:44 +08:00
HRESULT hr = S_OK;
hr = _createDeviceResources();
2017-10-12 02:44:44 +08:00
if (SUCCEEDED(hr))
2017-10-10 01:14:03 +08:00
{
2017-10-12 02:44:44 +08:00
GetRenderTarget()->BeginDraw();
2017-10-10 01:14:03 +08:00
2017-10-12 02:44:44 +08:00
GetRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
2017-09-10 23:56:52 +08:00
2017-10-12 02:44:44 +08:00
GetRenderTarget()->Clear(D2D1::ColorF(m_ClearColor));
2017-09-27 17:56:28 +08:00
2017-10-13 11:42:36 +08:00
m_pCurrentScene->_onRender(); // <20><><EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
2017-09-10 23:56:52 +08:00
2017-10-12 02:44:44 +08:00
hr = GetRenderTarget()->EndDraw();
}
if (hr == D2DERR_RECREATE_TARGET)
{
hr = S_OK;
_discardDeviceResources();
2017-10-12 02:44:44 +08:00
}
2017-10-14 01:07:34 +08:00
if (FAILED(hr))
{
exit(EXIT_FAILURE);
}
2017-09-27 17:56:28 +08:00
}
2017-10-12 02:44:44 +08:00
void e2d::EApp::setWindowSize(int width, int height)
2017-09-10 23:56:52 +08:00
{
2017-09-12 12:53:34 +08:00
// <20><>ȡ<EFBFBD><C8A1>Ļ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD>
2017-10-12 02:44:44 +08:00
tagRECT rcWindow;
2017-09-12 12:53:34 +08:00
GetWindowRect(GetHWnd(), &rcWindow);
// <20><>ȡ<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>С
2017-10-12 02:44:44 +08:00
tagRECT rcClient;
2017-09-12 12:53:34 +08:00
GetClientRect(GetHWnd(), &rcClient);
// <20><><EFBFBD><EFBFBD><EFBFBD>߿<EFBFBD><DFBF><EFBFBD>С
width += (rcWindow.right - rcWindow.left) - (rcClient.right - rcClient.left);
height += (rcWindow.bottom - rcWindow.top) - (rcClient.bottom - rcClient.top);
// <20>޸Ĵ<DEB8><C4B4>ڴ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
2017-10-14 01:07:34 +08:00
MoveWindow(GetHWnd(), (screenWidth - width) / 2, (screenHeight - height) / 2, width, height, TRUE);
2017-09-10 23:56:52 +08:00
}
void e2d::EApp::setWindowSize(e2d::ESize size)
2017-09-27 17:56:28 +08:00
{
setWindowSize(size.cx, size.cy);
2017-09-27 17:56:28 +08:00
}
void e2d::EApp::setWindowTitle(e2d::EString title)
2017-09-10 23:56:52 +08:00
{
// <20><><EFBFBD>ô<EFBFBD><C3B4>ڱ<EFBFBD><DAB1><EFBFBD>
SetWindowText(GetHWnd(), title.c_str());
// <20><><EFBFBD>浱ǰ<E6B5B1><C7B0><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD>޸Ĵ<DEB8><C4B4>ڴ<EFBFBD>Сʱ<D0A1>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
2017-10-14 01:07:34 +08:00
get()->m_sTitle = title;
2017-09-10 23:56:52 +08:00
}
e2d::EString e2d::EApp::getTitle()
2017-09-20 14:52:50 +08:00
{
2017-10-14 01:07:34 +08:00
return get()->m_sTitle;
2017-09-20 14:52:50 +08:00
}
e2d::ESize e2d::EApp::getSize()
{
return e2d::ESize(GetRenderTarget()->GetPixelSize().width, GetRenderTarget()->GetPixelSize().height);
}
UINT32 e2d::EApp::getWidth()
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
return GetRenderTarget()->GetPixelSize().width;
2017-09-10 23:56:52 +08:00
}
UINT32 e2d::EApp::getHeight()
2017-10-12 02:44:44 +08:00
{
return GetRenderTarget()->GetPixelSize().height;
}
void e2d::EApp::enterScene(EScene * scene, bool save /* = true */)
2017-09-10 23:56:52 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
2017-10-14 01:07:34 +08:00
get()->m_pNextScene = scene;
2017-09-10 23:56:52 +08:00
// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ƿ񱣴浱ǰ<E6B5B1><C7B0><EFBFBD><EFBFBD>
2017-10-14 18:43:32 +08:00
if (get()->m_pCurrentScene)
{
get()->m_pCurrentScene->m_bWillSave = save;
}
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
void e2d::EApp::backScene()
2017-09-10 23:56:52 +08:00
{
// <20><>ջ<EFBFBD><D5BB>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EBA3AC>Ϊ<EFBFBD><CEAA>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
2017-10-14 01:07:34 +08:00
get()->m_pNextScene = s_SceneStack.top();
2017-10-14 18:43:32 +08:00
s_SceneStack.pop();
2017-09-10 23:56:52 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>浱ǰ<E6B5B1><C7B0><EFBFBD><EFBFBD>
2017-10-14 18:43:32 +08:00
if (get()->m_pCurrentScene)
{
get()->m_pCurrentScene->m_bWillSave = false;
}
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
void e2d::EApp::clearScene()
2017-09-18 23:59:08 +08:00
{
// <20><><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD>ջ
2017-10-10 01:14:03 +08:00
while (s_SceneStack.size())
2017-09-18 23:59:08 +08:00
{
2017-10-10 01:14:03 +08:00
auto temp = s_SceneStack.top();
2017-10-12 02:44:44 +08:00
SafeDelete(&temp);
2017-10-10 01:14:03 +08:00
s_SceneStack.pop();
2017-09-18 23:59:08 +08:00
}
}
e2d::EScene * e2d::EApp::getCurrentScene()
2017-10-12 02:44:44 +08:00
{
2017-10-14 01:07:34 +08:00
return get()->m_pCurrentScene;
}
void e2d::EApp::setAppName(e2d::EString appname)
2017-09-20 14:52:50 +08:00
{
2017-09-27 17:56:28 +08:00
s_pInstance->m_sAppName = appname;
2017-09-20 14:52:50 +08:00
}
e2d::EString e2d::EApp::getAppName()
2017-09-20 14:52:50 +08:00
{
2017-09-27 17:56:28 +08:00
return s_pInstance->m_sAppName;
2017-09-20 14:52:50 +08:00
}
void e2d::EApp::setBkColor(EColor::Enum color)
2017-10-12 02:44:44 +08:00
{
2017-10-14 01:07:34 +08:00
get()->m_ClearColor = color;
2017-10-12 02:44:44 +08:00
}
void e2d::EApp::close()
{
ShowWindow(GetHWnd(), SW_HIDE);
}
void e2d::EApp::show()
2017-09-12 12:53:34 +08:00
{
2017-10-12 02:44:44 +08:00
ShowWindow(GetHWnd(), SW_NORMAL);
2017-09-12 12:53:34 +08:00
}
void e2d::EApp::free()
2017-10-12 02:44:44 +08:00
{
// <20>ͷų<CDB7><C5B3><EFBFBD><EFBFBD>ڴ<EFBFBD>
2017-10-14 01:07:34 +08:00
SafeDelete(&get()->m_pCurrentScene);
SafeDelete(&get()->m_pNextScene);
2017-10-12 02:44:44 +08:00
// <20><><EFBFBD>ճ<EFBFBD><D5B3><EFBFBD>ջ
while (s_SceneStack.size())
{
auto temp = s_SceneStack.top();
SafeDelete(&temp);
s_SceneStack.pop();
}
2017-10-14 18:43:32 +08:00
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>
2017-10-12 02:44:44 +08:00
//Timer::clearAllTimers();
2017-10-14 01:07:34 +08:00
EMsgManager::clearAllKeyboardListeners();
EMsgManager::clearAllMouseListeners();
2017-10-12 02:44:44 +08:00
//ActionManager::clearAllActions();
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
2017-10-14 01:07:34 +08:00
EObjectManager::clearAllObjects();
2017-10-12 02:44:44 +08:00
}
void e2d::EApp::quit()
2017-10-12 02:44:44 +08:00
{
2017-10-14 01:07:34 +08:00
get()->m_bRunning = false;
2017-10-12 02:44:44 +08:00
}
void e2d::EApp::end()
2017-10-12 02:44:44 +08:00
{
2017-10-14 01:07:34 +08:00
get()->m_bRunning = false;
2017-10-12 02:44:44 +08:00
}
void e2d::EApp::_enterNextScene()
2017-09-10 23:56:52 +08:00
{
2017-10-06 16:40:10 +08:00
// ִ<>е<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> onExit <20><><EFBFBD><EFBFBD>
if (m_pCurrentScene)
{
m_pCurrentScene->onExit();
2017-10-14 18:43:32 +08:00
if (m_pCurrentScene->m_bWillSave)
2017-10-06 16:40:10 +08:00
{
// <20><>Ҫ<EFBFBD><D2AA><EFBFBD>浱ǰ<E6B5B1><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>
2017-10-10 01:14:03 +08:00
s_SceneStack.push(m_pCurrentScene);
2017-10-06 16:40:10 +08:00
}
else
{
2017-10-12 02:44:44 +08:00
SafeDelete(&m_pCurrentScene);
2017-10-06 16:40:10 +08:00
}
}
2017-10-14 01:07:34 +08:00
m_pNextScene->onEnter(); // ִ<><D6B4><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> onEnter <20><><EFBFBD><EFBFBD>
2017-10-06 16:40:10 +08:00
m_pCurrentScene = m_pNextScene; // <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
m_pNextScene = nullptr; // <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
// Creates resources that are not bound to a particular device.
// Their lifetime effectively extends for the duration of the
// application.
HRESULT e2d::EApp::_createDeviceIndependentResources()
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
HRESULT hr = S_OK;
2017-09-10 23:56:52 +08:00
2017-10-12 02:44:44 +08:00
// Create a Direct2D factory.
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &GetFactory());
2017-09-10 23:56:52 +08:00
if (FAILED(hr))
{
2017-10-13 11:42:36 +08:00
MessageBox(nullptr, L"Create Device Independent Resources Failed!", L"Error", MB_OK);
}
2017-10-12 02:44:44 +08:00
return hr;
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
// Creates resources that are bound to a particular
// Direct3D device. These resources need to be recreated
// if the Direct3D device dissapears, such as when the isVisiable
2017-10-12 02:44:44 +08:00
// changes, the window is remoted, etc.
HRESULT e2d::EApp::_createDeviceResources()
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
HRESULT hr = S_OK;
2017-10-12 02:44:44 +08:00
if (!GetRenderTarget())
{
RECT rc;
GetClientRect(GetHWnd(), &rc);
D2D1_SIZE_U size = D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top
);
// Create a Direct2D render target.
hr = GetFactory()->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(GetHWnd(), size),
&GetRenderTarget()
);
}
2017-10-13 11:42:36 +08:00
if (FAILED(hr))
{
MessageBox(nullptr, L"Create Device Resources Failed!", L"Error", MB_OK);
}
2017-10-12 02:44:44 +08:00
return hr;
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
// Discards device-dependent resources. These resources must be
// recreated when the Direct3D device is lost.
void e2d::EApp::_discardDeviceResources()
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
SafeReleaseInterface(&GetRenderTarget());
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
// If the application receives a WM_SIZE message, this method
// re2d::ESizes the render target appropriately.
2017-10-12 02:44:44 +08:00
void e2d::EApp::_onResize(UINT width, UINT height)
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
if (GetRenderTarget())
{
// Note: This method can fail, but it's okay to ignore the
// error here, because the error will be returned again
// the next time EndDraw is called.
GetRenderTarget()->Resize(D2D1::SizeU(width, height));
}
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
// Handles window messages.
LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
LRESULT result = 0;
if (message == WM_CREATE)
2017-09-10 23:56:52 +08:00
{
2017-10-12 02:44:44 +08:00
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
2017-10-13 11:42:36 +08:00
e2d::EApp *pEApp = (e2d::EApp *)pcs->lpCreateParams;
2017-10-12 02:44:44 +08:00
::SetWindowLongPtrW(
2017-10-13 11:42:36 +08:00
hWnd,
2017-10-12 02:44:44 +08:00
GWLP_USERDATA,
PtrToUlong(pEApp)
);
result = 1;
2017-09-10 23:56:52 +08:00
}
2017-10-12 02:44:44 +08:00
else
{
2017-10-13 11:42:36 +08:00
e2d::EApp *pEApp = reinterpret_cast<e2d::EApp *>(static_cast<LONG_PTR>(
2017-10-12 02:44:44 +08:00
::GetWindowLongPtrW(
2017-10-13 11:42:36 +08:00
hWnd,
2017-10-12 02:44:44 +08:00
GWLP_USERDATA
)));
bool wasHandled = false;
if (pEApp)
{
switch (message)
{
/*case WM_ACTIVATE:
{
if (LOWORD(wParam) == WA_INACTIVE)
{
MSG msg;
do
{
GetMessage(&msg, nullptr, 0, 0);
} while (msg.wParam != WA_ACTIVE);
}
}*/
2017-10-13 11:42:36 +08:00
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:
{
2017-10-13 17:14:00 +08:00
EMsgManager::MouseProc(message, wParam, lParam);
}
result = 0;
break;
case WM_KEYDOWN:
case WM_KEYUP:
{
EMsgManager::KeyboardProc(message, wParam, lParam);
2017-10-13 11:42:36 +08:00
}
result = 0;
break;
2017-10-12 02:44:44 +08:00
case WM_SIZE:
{
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
pEApp->_onResize(width, height);
}
result = 0;
wasHandled = true;
break;
case WM_DISPLAYCHANGE:
{
2017-10-13 11:42:36 +08:00
InvalidateRect(hWnd, NULL, FALSE);
2017-10-12 02:44:44 +08:00
}
result = 0;
wasHandled = true;
break;
case WM_PAINT:
{
pEApp->_onRender();
2017-10-13 11:42:36 +08:00
ValidateRect(hWnd, NULL);
2017-10-12 02:44:44 +08:00
}
result = 0;
wasHandled = true;
break;
case WM_DESTROY:
{
2017-10-14 01:07:34 +08:00
if (GetConsoleWindow())
{
FreeConsole();
}
2017-10-12 02:44:44 +08:00
PostQuitMessage(0);
}
result = 1;
wasHandled = true;
break;
}
}
if (!wasHandled)
{
2017-10-13 11:42:36 +08:00
result = DefWindowProc(hWnd, message, wParam, lParam);
2017-10-12 02:44:44 +08:00
}
}
return result;
2017-09-10 23:56:52 +08:00
}