From f2e66d0d034d79adc10d495f8b7661ef17831202 Mon Sep 17 00:00:00 2001 From: oioitff Date: Fri, 19 Apr 2013 17:21:16 +0800 Subject: [PATCH] Use native code instead of ATL::CImage --- Windows/WndMainWindow.cpp | 50 ++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index d3901c1dcf..560d6b3624 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -2,7 +2,6 @@ #include #include -#include #include "base/NativeApp.h" #include "Globals.h" @@ -34,6 +33,7 @@ #include "Windows/W32Util/Misc.h" #include "GPU/GPUInterface.h" #include "GPU/GPUState.h" +#include "native/image/png_load.h" #ifdef THEMES #include "XPTheme.h" @@ -870,7 +870,7 @@ namespace MainWindow return 1; } - BOOL LoadImageFromResource(CImage *image, HINSTANCE hInstance, LPCTSTR pszResourceName, LPCTSTR lpType) + HBITMAP LoadImageFromResource(HINSTANCE hInstance,LPCTSTR pszResourceName, LPCTSTR lpType) { HRSRC hrsrc = FindResource(hInstance, pszResourceName, lpType); if (!hrsrc) @@ -879,31 +879,37 @@ namespace MainWindow BYTE *lpRsrc = (BYTE*)LoadResource(hInstance, hrsrc); if (!lpRsrc) return FALSE; - HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, dwlen); - BYTE* pmem = (BYTE*)GlobalLock(hMem); - memcpy(pmem, lpRsrc, dwlen); - GlobalUnlock(hMem); - IStream* pstm; - CreateStreamOnHGlobal(hMem, FALSE, &pstm); - BOOL bResult = (image->Load(pstm) == S_OK); - pstm->Release(); - GlobalFree(hMem); + int width, height; + unsigned char *image_data = 0; + bool bResult = pngLoadPtr(lpRsrc, dwlen, &width, &height, &image_data, false); FreeResource(lpRsrc); - return bResult; + if (!bResult) + return 0; + HBITMAP hbm = CreateBitmap(width, height, 1, 32, image_data); + free(image_data); + return hbm; + } + void BitBlt(HBITMAP hbm, HDC dstDC, int dstX, int dstY, int width, int height, int srcX, int srcY) + { + HDC hCompDC = CreateCompatibleDC(dstDC); + HBITMAP oldbm = (HBITMAP)SelectObject(hCompDC, hbm); + BitBlt(dstDC, dstX, dstY, width, height, hCompDC, srcX, srcY, SRCCOPY); + SelectObject(hCompDC, oldbm); + DeleteObject(hCompDC); } // Message handler for control box. LRESULT CALLBACK Controls(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { - static CImage image; + static HBITMAP hbm = 0; switch (message) { case WM_INITDIALOG: W32Util::CenterWindow(hDlg); { // TODO: connect to keyboard device instead - if (image.IsNull()) - LoadImageFromResource(&image, hInst, MAKEINTRESOURCE(IDB_IMAGE_PSP), "IMAGE"); + if (!hbm) + hbm = LoadImageFromResource(hInst, MAKEINTRESOURCE(IDB_IMAGE_PSP), "IMAGE"); int key_pad_size = (IDC_EDIT_KEYRIGHT - IDC_EDIT_KEY_TURBO + 1); for (u32 i = 0; i <= IDC_EDIT_KEY_ANALOG_RIGHT - IDC_EDIT_KEY_TURBO; i++) { HWND hEdit = GetDlgItem(hDlg, IDC_EDIT_KEY_TURBO + i); @@ -921,9 +927,11 @@ namespace MainWindow { PAINTSTRUCT pst; HDC hdc = BeginPaint(hDlg, &pst); - int width = image.GetWidth(); - int height = image.GetHeight(); - image.BitBlt(hdc, 0, 0, width, height, 0 , 0); + BITMAP bm; + GetObject(hbm, sizeof(BITMAP), &bm); + int width = bm.bmWidth; + int height = bm.bmHeight; + BitBlt(hbm, hdc, 0, 0, width, height, 0 , 0); EndPaint(hDlg, &pst); return TRUE; } @@ -942,7 +950,7 @@ namespace MainWindow RECT rc = getRedrawRect(hEdit); RECT clientrc; GetClientRect(hEdit, &clientrc); - image.BitBlt(hdc, 0, 0, rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top); + BitBlt(hbm, hdc, 0, 0, rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top); char str[11]; GetWindowTextA(hEdit, str, 10); DrawTextA(hdc, str, strlen(str), &clientrc, DT_CENTER|DT_SINGLELINE); @@ -963,6 +971,10 @@ namespace MainWindow } UnhookWindowsHookEx(pKeydownHook); EndDialog(hDlg, LOWORD(wParam)); + if (hbm) { + DeleteObject(hbm); + hbm = 0; + } return TRUE; } break;