29#include <shobjidl_core.h>
39#pragma region Initialization
47 const char appname[] =
"WangTiler";
49 WNDCLASSEX wndClass = {0};
51 wndClass.cbSize =
sizeof(WNDCLASSEX);
52 wndClass.style = CS_HREDRAW | CS_VREDRAW;
54 wndClass.cbClsExtra = 0;
55 wndClass.cbWndExtra = 0;
56 wndClass.hInstance = hInst;
57 wndClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
58 wndClass.hCursor = LoadCursor(
nullptr, IDC_ARROW);
59 wndClass.hbrBackground =
nullptr;
60 wndClass.lpszMenuName =
nullptr;
61 wndClass.lpszClassName = appname;
62 wndClass.hIconSm = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON2));
64 RegisterClassEx(&wndClass);
66 const DWORD dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
67 const DWORD dwStyleEx = WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME;
73 r.left = 0; r.right = w;
74 r.top = 0; r.bottom = h + GetSystemMetrics(SM_CYMENU);
75 AdjustWindowRectEx(&r, dwStyle, FALSE, dwStyleEx);
77 const HWND hwnd = CreateWindowEx(dwStyleEx, appname, appname, dwStyle,
78 CW_USEDEFAULT, CW_USEDEFAULT, r.right - r.left, r.bottom - r.top,
79 nullptr,
nullptr, hInst,
nullptr);
81 ShowWindow(hwnd, nShow);
89 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
90 ULONG_PTR gdiplusToken;
91 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput,
nullptr);
95#pragma endregion Initialization
112 Gdiplus::ImageCodecInfo* pCodecInfo =
nullptr;
113 if(FAILED(Gdiplus::GetImageEncodersSize(&num, &n)))
return E_FAIL;
114 if(n == 0)
return E_FAIL;
116 pCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(n));
117 if(pCodecInfo ==
nullptr)
return E_FAIL;
118 if(FAILED(GetImageEncoders(num, n, pCodecInfo)))
return E_FAIL;
120 for(UINT j=0; j<num && hr!=S_OK; j++)
121 if(wcscmp(pCodecInfo[j].MimeType, format) == 0){
122 *pClsid = pCodecInfo[j].Clsid;
142 COMDLG_FILTERSPEC filetypes[] = {
143 {L
"PNG Files", L
"*.png"}
146 std::wstring wstrFileName;
147 CComPtr<IFileSaveDialog> pDlg;
149 std::wstring wstrName = L
"Image" + std::to_wstring(n++);
150 CComPtr<IShellItem> pItem;
151 LPWSTR pwsz =
nullptr;
155 if(FAILED(pDlg.CoCreateInstance(__uuidof(FileSaveDialog))))
return E_FAIL;
157 pDlg->SetFileTypes(_countof(filetypes), filetypes);
158 pDlg->SetTitle(L
"Save Image");
159 pDlg->SetFileName(wstrName.c_str());
160 pDlg->SetDefaultExtension(L
"png");
162 if(FAILED(pDlg->Show(hwnd)))
return E_FAIL;
163 if(FAILED(pDlg->GetResult(&pItem)))
return E_FAIL;
164 if(FAILED(pItem->GetDisplayName(SIGDN_FILESYSPATH, &pwsz)))
return E_FAIL;
169 if(FAILED(
GetEncoderClsid((WCHAR*)L
"image/png", &clsid)))
return E_FAIL;
170 pBitmap->Save(pwsz, &clsid,
nullptr);
177#pragma endregion Save
182#pragma region Create menu functions
188 HMENU hMenu = CreateMenu();
194 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"&File");
202 HMENU hMenu = CreateMenu();
209 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"&Tileset");
217 HMENU hMenu = CreateMenu();
219 AppendMenuW(hMenu, MF_STRING,
IDM_HELP_HELP, L
"Display help...");
221 AppendMenuW(hParent, MF_POPUP, (UINT_PTR)hMenu, L
"&Help");
224#pragma endregion Create menu functions
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Window procedure.
HMENU CreateTilesetMenu(HMENU hParent)
Create Tileset menu.
void CreateFileMenu(HMENU hParent)
Create File menu.
HRESULT GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
ULONG_PTR InitGDIPlus()
Initialize GDI+.
void CreateHelpMenu(HMENU hParent)
Create Help menu.
HRESULT SaveBitmap(HWND hwnd, Gdiplus::Bitmap *pBitmap)
Save bitmap to file.
void InitWindow(HINSTANCE hInst, INT nShow, WNDPROC WndProc)
Initialize window.
Interface for some helpful Windows-specific functions.
#define IDM_TILESET_FLOWER
Menu id for flower tileset.
#define IDM_FILE_SAVE
Menu id for Save.
#define IDM_TILESET_GRASS
Menu id for grass tileset.
#define IDM_FILE_QUIT
Menu id for Quit.
#define IDM_FILE_GENERATE
Menu id for Generate.
#define IDM_HELP_HELP
Menu id for display help.
#define IDM_TILESET_MUD
Menu id for dirt tileset.
#define IDM_HELP_ABOUT
Menu id for display About info.
#define IDM_TILESET_DEFAULT
Menu id for default tileset.