-
Notifications
You must be signed in to change notification settings - Fork 1
/
PngAnimation.h
290 lines (273 loc) · 6.82 KB
/
PngAnimation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#pragma once
#pragma warning(push)
#pragma warning(disable : 4458) // declaration of 'nativeCap' hides class member.
#include <gdiplus.h>
#pragma warning(pop)
#pragma comment (lib, "Gdiplus.lib")
#include <mmsystem.h>
#pragma comment (lib, "Winmm.lib") // for Multimedia Timers.
#include <new>
#include <cassert>
struct PngAnimation
{
///////////////////////////////////////
// PUBLIC
///////////////////////////////////////
PngAnimation()
{ m_pBmp = NULL;
}
~PngAnimation()
{ Destroy();
}
//
bool Create(HMODULE moduleRes/*or null*/, unsigned int idPngRes, int numberFrame)
{ assert(numberFrame>1);
//
Destroy();
if( !LoadImage(moduleRes,idPngRes,&m_pBmp/*out*/) )
return false;
//
m_iNumberFrame = numberFrame;
m_szFrame.cx = m_pBmp->GetWidth() / numberFrame;
m_szFrame.cy = m_pBmp->GetHeight();
//
return true;
}
void Destroy()
{ m_Timer.KillTimer();
if(m_pBmp)
{ ::delete m_pBmp;
m_pBmp = NULL;
}
}
bool IsCreated() const
{ return m_pBmp!=NULL;
}
//
bool Start(HWND parent, int x, int y, COLORREF clrBack, int time/*ms*/, bool infinitely)
{ assert( IsCreated() );
assert( ::IsWindow(parent) );
assert(time>0);
//
if( !IsCreated() )
return false;
//
m_Timer.KillTimer();
m_hParentWnd = parent;
m_ptFrame.x = x;
m_ptFrame.y = y;
m_clrBack = clrBack;
m_iTime = time;
m_bInfinitely = infinitely;
m_iCurFrame = 0;
if( !m_Timer.SetTimer(this,&PngAnimation::OnTimer,m_iTime) )
return false;
OnTimer();
return true;
}
void Stop()
{ m_Timer.KillTimer();
}
bool IsActive() const
{ return m_Timer.IsActive();
}
//
RECT GetArea() const
{ const RECT rect = {m_ptFrame.x,m_ptFrame.y,m_ptFrame.x+m_szFrame.cx,m_ptFrame.y+m_szFrame.cy};
return rect;
}
///////////////////////////////////////
// PRIVATE
///////////////////////////////////////
private:
void OnTimer() // callback from Timer object.
{ HDC hDC = ::GetDC(m_hParentWnd);
if(hDC)
{ VirtualWindow virtwnd(hDC,m_ptFrame,m_szFrame);
//
// Background filling.
RECT rcFill = {0,0,m_szFrame.cx,m_szFrame.cy};
if(virtwnd==hDC) // VirtualWindow wasn't created.
::OffsetRect(&rcFill,m_ptFrame.x,m_ptFrame.y);
HBRUSH brush = ::CreateSolidBrush(m_clrBack);
::FillRect(virtwnd,&rcFill,brush);
::DeleteObject(brush);
// Frame rendering.
Gdiplus::Graphics gr(virtwnd);
gr.DrawImage(m_pBmp,rcFill.left,rcFill.top,m_szFrame.cx*m_iCurFrame,0,m_szFrame.cx,m_szFrame.cy,Gdiplus::UnitPixel);
//
if(++m_iCurFrame == m_iNumberFrame)
{ m_iCurFrame = 0;
if(!m_bInfinitely)
m_Timer.KillTimer();
}
virtwnd.Release(); // show frame.
::ReleaseDC(m_hParentWnd,hDC);
}
}
private:
bool LoadImage(HMODULE moduleRes/*or null*/, unsigned int resID, Gdiplus::Bitmap **bmp/*out*/) const
{ assert(resID);
//
if(!moduleRes)
moduleRes = ::GetModuleHandle(NULL);
if(moduleRes)
{ HRSRC hRsrc = ::FindResource(moduleRes,MAKEINTRESOURCE(resID),_T("PNG"));
if(hRsrc)
{ HGLOBAL hGlobal = ::LoadResource(moduleRes,hRsrc);
if(hGlobal)
{ const void *lpBuffer = ::LockResource(hGlobal);
if(lpBuffer)
{ const unsigned int uiSize = static_cast<unsigned int>( ::SizeofResource(moduleRes,hRsrc) );
HGLOBAL hRes = ::GlobalAlloc(GMEM_MOVEABLE, uiSize);
if(hRes)
{ void *lpResBuffer = ::GlobalLock(hRes);
if(lpResBuffer)
{ memcpy(lpResBuffer, lpBuffer, uiSize);
IStream *pStream = NULL;
if(::CreateStreamOnHGlobal(hRes, FALSE, &pStream/*out*/)==S_OK)
{ *bmp = ::new (std::nothrow) Gdiplus::Bitmap(pStream,FALSE);
pStream->Release();
}
::GlobalUnlock(lpResBuffer);
}
::GlobalFree(hRes);
}
UnlockResource(hGlobal);
}
::FreeResource(hGlobal);
}
}
}
if(*bmp && (*bmp)->GetLastStatus()!=Gdiplus::Ok)
{ ::delete *bmp;
*bmp = NULL;
return false;
}
return (*bmp)!=NULL;
}
private:
struct VirtualWindow
{ VirtualWindow(HDC dcDst, POINT ptDst, SIZE szDst)
{ assert(dcDst);
//
m_bmpSrc = NULL;
m_dcSrc = ::CreateCompatibleDC(dcDst);
if(m_dcSrc)
{ m_bmpSrc = ::CreateCompatibleBitmap(dcDst,szDst.cx,szDst.cy);
if(!m_bmpSrc)
{ ::DeleteDC(m_dcSrc);
m_dcSrc = dcDst;
}
else
{ ::SelectObject(m_dcSrc,m_bmpSrc);
m_dcDst = dcDst;
m_ptDst = ptDst;
m_szDst = szDst;
}
}
else
m_dcSrc = dcDst;
}
~VirtualWindow()
{ Release();
}
//
operator HDC() const
{ return m_dcSrc;
}
void Release()
{ if(m_bmpSrc)
{ ::BitBlt(m_dcDst,m_ptDst.x,m_ptDst.y,m_szDst.cx,m_szDst.cy,m_dcSrc,0,0,SRCCOPY);
::DeleteObject(m_bmpSrc);
::DeleteDC(m_dcSrc);
m_bmpSrc = NULL;
}
}
private:
HDC m_dcSrc, m_dcDst;
POINT m_ptDst;
SIZE m_szDst;
HBITMAP m_bmpSrc;
};
private:
struct Timer
{ Timer()
{ m_hTimerWnd = NULL;
m_uTimerID = 0;
}
~Timer()
{ KillTimer();
}
//
bool SetTimer(PngAnimation *obj, void(PngAnimation::*func)(), int time/*ms*/)
{ m_pNotifyObj = obj;
m_pNotifyFunc = func;
//
if( (m_hTimerWnd = CreateTimerWindow())==NULL ||
(m_uTimerID = ::timeSetEvent(time,0,TimerProc,reinterpret_cast<DWORD_PTR>(this),TIME_PERIODIC))==0)
{
KillTimer();
return false;
}
return true;
}
void KillTimer()
{ if(m_uTimerID)
{ ::timeKillEvent(m_uTimerID);
m_uTimerID = 0;
}
if(m_hTimerWnd)
{ ::DestroyWindow(m_hTimerWnd);
m_hTimerWnd = NULL;
}
}
//
bool IsActive() const
{ return m_hTimerWnd!=NULL;
}
protected:
PngAnimation *m_pNotifyObj;
void(PngAnimation::*m_pNotifyFunc)();
HWND m_hTimerWnd;
unsigned int m_uTimerID;
protected:
static void __stdcall TimerProc(unsigned int /*uID*/, unsigned int /*uMsg*/, DWORD_PTR dwUser, DWORD_PTR /*dw1*/, DWORD_PTR /*dw2*/)
{ Timer const *timer = reinterpret_cast<Timer const *>(dwUser);
::SendMessage(timer->m_hTimerWnd,WM_USER,dwUser,0);
}
//
HWND CreateTimerWindow() const
{ const TCHAR className[] = _T("{77E7379C-051F-43F7-8B17-D73117493DBD}");
HMODULE module = ::GetModuleHandle(NULL);
WNDCLASS wc;
memset(&wc,0,sizeof(wc));
if(::GetClassInfo(module,className,&wc/*out*/)==0)
{ wc.lpfnWndProc = WindowProc;
wc.hInstance = module;
wc.lpszClassName = className;
if(::RegisterClass(&wc)==0)
return NULL;
}
return ::CreateWindowEx(0,className,_T(""),WS_POPUP,0,0,0,0,NULL,NULL,module,NULL);
}
//
static LRESULT __stdcall WindowProc(HWND hwnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam)
{ const LRESULT res = ::DefWindowProc(hwnd, uMsg, wParam, lParam);
if(uMsg==WM_USER)
{ Timer *timer = reinterpret_cast<Timer *>(wParam);
(timer->m_pNotifyObj->*timer->m_pNotifyFunc)();
}
return res;
}
};
private:
Gdiplus::Bitmap *m_pBmp;
Timer m_Timer;
HWND m_hParentWnd;
int m_iCurFrame,m_iNumberFrame, m_iTime;
POINT m_ptFrame;
SIZE m_szFrame;
COLORREF m_clrBack;
bool m_bInfinitely;
};