change: get HINSTANCE from GetModuleHandle

This commit is contained in:
Haibo 2018-10-28 20:23:47 +08:00
parent 24206e00b6
commit 301c99018b
5 changed files with 50 additions and 52 deletions

View File

@ -43,6 +43,10 @@
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
#endif #endif
#ifndef NOMINMAX
# define NOMINMAX
#endif
#ifndef DIRECTINPUT_VERSION #ifndef DIRECTINPUT_VERSION
# define DIRECTINPUT_VERSION 0x0800 # define DIRECTINPUT_VERSION 0x0800
#endif #endif
@ -90,12 +94,6 @@
#pragma comment(lib, "shlwapi.lib") #pragma comment(lib, "shlwapi.lib")
#ifndef HINST_THISCOMPONENT
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
# define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
#endif
#if _MSC_VER >= 1800 #if _MSC_VER >= 1800
# define E2D_OP_EXPLICIT explicit # define E2D_OP_EXPLICIT explicit
#else #else
@ -121,15 +119,6 @@
#endif #endif
#ifdef max
# undef max
#endif
#ifdef min
# undef min
#endif
#ifndef E2D_WARNING #ifndef E2D_WARNING
# if defined( DEBUG ) || defined( _DEBUG ) # if defined( DEBUG ) || defined( _DEBUG )
# define E2D_WARNING(msg) do { ::OutputDebugStringW(L"[easy2d] Warning: " _CRT_WIDE(msg) L"\r\n"); } while(0) # define E2D_WARNING(msg) do { ::OutputDebugStringW(L"[easy2d] Warning: " _CRT_WIDE(msg) L"\r\n"); } while(0)

View File

@ -266,7 +266,8 @@ void easy2d::Game::DrawScene()
void easy2d::Game::Init() void easy2d::Game::Init()
{ {
WNDCLASSEX wcex = { 0 }; HINSTANCE hinstance = GetModuleHandle(nullptr);
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpszClassName = REGISTER_CLASS; wcex.lpszClassName = REGISTER_CLASS;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
@ -274,7 +275,7 @@ void easy2d::Game::Init()
wcex.hIcon = nullptr; wcex.hIcon = nullptr;
wcex.cbClsExtra = 0; wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR); wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT; wcex.hInstance = hinstance;
wcex.hbrBackground = nullptr; wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr; wcex.lpszMenuName = nullptr;
wcex.hCursor = ::LoadCursor(nullptr, IDC_ARROW); wcex.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
@ -282,7 +283,7 @@ void easy2d::Game::Init()
if (icon_ != 0) if (icon_ != 0)
{ {
wcex.hIcon = (HICON)::LoadImage( wcex.hIcon = (HICON)::LoadImage(
HINST_THISCOMPONENT, hinstance,
MAKEINTRESOURCE(icon_), MAKEINTRESOURCE(icon_),
IMAGE_ICON, IMAGE_ICON,
0, 0,
@ -309,13 +310,13 @@ void easy2d::Game::Init()
int(client_rect.size.height), int(client_rect.size.height),
nullptr, nullptr,
nullptr, nullptr,
HINST_THISCOMPONENT, hinstance,
this this
); );
if (hwnd_ == nullptr) if (hwnd_ == nullptr)
{ {
::UnregisterClass(REGISTER_CLASS, HINST_THISCOMPONENT); ::UnregisterClass(REGISTER_CLASS, hinstance);
throw RuntimeError("Create window failed"); throw RuntimeError("Create window failed");
return; return;
} }
@ -463,8 +464,9 @@ void easy2d::Game::SetIcon(int resource_id)
if (hwnd_) if (hwnd_)
{ {
HINSTANCE hinstance = GetModuleHandle(nullptr);
HICON icon = (HICON)::LoadImage( HICON icon = (HICON)::LoadImage(
HINST_THISCOMPONENT, hinstance,
MAKEINTRESOURCE(resource_id), MAKEINTRESOURCE(resource_id),
IMAGE_ICON, IMAGE_ICON,
0, 0,

View File

@ -30,10 +30,12 @@ easy2d::Input::Input(HWND hwnd)
ZeroMemory(key_buffer_, sizeof(key_buffer_)); ZeroMemory(key_buffer_, sizeof(key_buffer_));
ZeroMemory(&mouse_state_, sizeof(mouse_state_)); ZeroMemory(&mouse_state_, sizeof(mouse_state_));
HINSTANCE hinstance = GetModuleHandle(nullptr);
// 初始化接口对象 // 初始化接口对象
ThrowIfFailed( ThrowIfFailed(
DirectInput8Create( DirectInput8Create(
HINST_THISCOMPONENT, hinstance,
DIRECTINPUT_VERSION, DIRECTINPUT_VERSION,
IID_IDirectInput8, IID_IDirectInput8,
(void**)&direct_input_, (void**)&direct_input_,

View File

@ -189,30 +189,34 @@ bool easy2d::Image::CacheBitmap(const Resource& res)
return true; return true;
} }
IWICImagingFactory *imaging_factory = Device::GetGraphics()->GetImagingFactory(); HRESULT hr;
ID2D1HwndRenderTarget* render_target = Device::GetGraphics()->GetRenderTarget();
IWICBitmapDecoder *decoder = nullptr; HINSTANCE hinstance = GetModuleHandle(nullptr);
IWICBitmapFrameDecode *source = nullptr; IWICImagingFactory* imaging_factory = Device::GetGraphics()->GetImagingFactory();
IWICStream *stream = nullptr; ID2D1HwndRenderTarget* render_target = Device::GetGraphics()->GetRenderTarget();
IWICFormatConverter *converter = nullptr; IWICBitmapDecoder* decoder = nullptr;
ID2D1Bitmap *bitmap = nullptr; IWICBitmapFrameDecode* source = nullptr;
HRSRC res_handle = nullptr; IWICStream* stream = nullptr;
HGLOBAL res_data_handle = nullptr; IWICFormatConverter* converter = nullptr;
void *image_file = nullptr; ID2D1Bitmap* bitmap = nullptr;
DWORD image_file_size = 0; HRSRC res_handle = nullptr;
HGLOBAL res_data_handle = nullptr;
LPVOID image_file = nullptr;
DWORD image_file_size = 0;
// 定位资源 // 定位资源
res_handle = ::FindResourceW( res_handle = ::FindResourceW(
HINST_THISCOMPONENT, hinstance,
MAKEINTRESOURCE(res.id), MAKEINTRESOURCE(res.id),
(LPCWSTR)res.type (LPCWSTR)res.type
); );
HRESULT hr = res_handle ? S_OK : E_FAIL; hr = res_handle ? S_OK : E_FAIL;
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// 加载资源 // 加载资源
res_data_handle = ::LoadResource(HINST_THISCOMPONENT, res_handle); res_data_handle = ::LoadResource(hinstance, res_handle);
hr = res_data_handle ? S_OK : E_FAIL; hr = res_data_handle ? S_OK : E_FAIL;
} }
@ -228,7 +232,7 @@ bool easy2d::Image::CacheBitmap(const Resource& res)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
// 计算大小 // 计算大小
image_file_size = ::SizeofResource(HINST_THISCOMPONENT, res_handle); image_file_size = ::SizeofResource(hinstance, res_handle);
hr = image_file_size ? S_OK : E_FAIL; hr = image_file_size ? S_OK : E_FAIL;
} }
@ -322,14 +326,14 @@ bool easy2d::Image::CacheBitmap(const String & file_name)
// 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径
String image_file_path = image_file.GetPath(); String image_file_path = image_file.GetPath();
Graphics * graphics_device = Device::GetGraphics(); Graphics* graphics_device = Device::GetGraphics();
IWICImagingFactory *imaging_factory = graphics_device->GetImagingFactory(); IWICImagingFactory* imaging_factory = graphics_device->GetImagingFactory();
ID2D1HwndRenderTarget* render_target = graphics_device->GetRenderTarget(); ID2D1HwndRenderTarget* render_target = graphics_device->GetRenderTarget();
IWICBitmapDecoder *decoder = nullptr; IWICBitmapDecoder* decoder = nullptr;
IWICBitmapFrameDecode *source = nullptr; IWICBitmapFrameDecode* source = nullptr;
IWICStream *stream = nullptr; IWICStream* stream = nullptr;
IWICFormatConverter *converter = nullptr; IWICFormatConverter* converter = nullptr;
ID2D1Bitmap *bitmap = nullptr; ID2D1Bitmap* bitmap = nullptr;
// 创建解码器 // 创建解码器
HRESULT hr = imaging_factory->CreateDecoderFromFilename( HRESULT hr = imaging_factory->CreateDecoderFromFilename(

View File

@ -106,27 +106,28 @@ namespace easy2d
HRSRC res_info; HRSRC res_info;
HGLOBAL res_data; HGLOBAL res_data;
DWORD res_size; DWORD res_size;
void* res; LPVOID res;
IStream* stream = nullptr; HINSTANCE hinstance = GetModuleHandle(nullptr);
IMFByteStream* byte_stream = nullptr; IStream* stream = nullptr;
IMFSourceReader* reader = nullptr; IMFByteStream* byte_stream = nullptr;
IMFSourceReader* reader = nullptr;
res_info = FindResourceW(HINST_THISCOMPONENT, res_name, res_type); res_info = FindResourceW(hinstance, res_name, res_type);
if (res_info == nullptr) if (res_info == nullptr)
{ {
TraceError(L"FindResource"); TraceError(L"FindResource");
return false; return false;
} }
res_data = LoadResource(HINST_THISCOMPONENT, res_info); res_data = LoadResource(hinstance, res_info);
if (res_data == nullptr) if (res_data == nullptr)
{ {
TraceError(L"LoadResource"); TraceError(L"LoadResource");
return false; return false;
} }
res_size = SizeofResource(HINST_THISCOMPONENT, res_info); res_size = SizeofResource(hinstance, res_info);
if (res_size == 0) if (res_size == 0)
{ {
TraceError(L"SizeofResource"); TraceError(L"SizeofResource");