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
#endif
#ifndef NOMINMAX
# define NOMINMAX
#endif
#ifndef DIRECTINPUT_VERSION
# define DIRECTINPUT_VERSION 0x0800
#endif
@ -90,12 +94,6 @@
#pragma comment(lib, "shlwapi.lib")
#ifndef HINST_THISCOMPONENT
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
# define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)
#endif
#if _MSC_VER >= 1800
# define E2D_OP_EXPLICIT explicit
#else
@ -121,15 +119,6 @@
#endif
#ifdef max
# undef max
#endif
#ifdef min
# undef min
#endif
#ifndef E2D_WARNING
# if defined( DEBUG ) || defined( _DEBUG )
# 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()
{
WNDCLASSEX wcex = { 0 };
HINSTANCE hinstance = GetModuleHandle(nullptr);
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpszClassName = REGISTER_CLASS;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
@ -274,7 +275,7 @@ void easy2d::Game::Init()
wcex.hIcon = nullptr;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hInstance = hinstance;
wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr;
wcex.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
@ -282,7 +283,7 @@ void easy2d::Game::Init()
if (icon_ != 0)
{
wcex.hIcon = (HICON)::LoadImage(
HINST_THISCOMPONENT,
hinstance,
MAKEINTRESOURCE(icon_),
IMAGE_ICON,
0,
@ -309,13 +310,13 @@ void easy2d::Game::Init()
int(client_rect.size.height),
nullptr,
nullptr,
HINST_THISCOMPONENT,
hinstance,
this
);
if (hwnd_ == nullptr)
{
::UnregisterClass(REGISTER_CLASS, HINST_THISCOMPONENT);
::UnregisterClass(REGISTER_CLASS, hinstance);
throw RuntimeError("Create window failed");
return;
}
@ -463,8 +464,9 @@ void easy2d::Game::SetIcon(int resource_id)
if (hwnd_)
{
HINSTANCE hinstance = GetModuleHandle(nullptr);
HICON icon = (HICON)::LoadImage(
HINST_THISCOMPONENT,
hinstance,
MAKEINTRESOURCE(resource_id),
IMAGE_ICON,
0,

View File

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

View File

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

View File

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