-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cpp
More file actions
359 lines (299 loc) · 9.13 KB
/
image.cpp
File metadata and controls
359 lines (299 loc) · 9.13 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "stdafx.h"
#include "image.h"
#pragma comment(lib, "msimg32.lib")
//멤버 이니셜라이즈
image::image()
: _imageInfo(NULL),
_fileName(NULL),
_trans(false),
_transColor(RGB(0,0,0))
{
}
image::~image()
{
}
HRESULT image::init(int width, int height)
{
if (_imageInfo != NULL) release();
HDC hdc = GetDC(_hWnd);
_imageInfo = new IMAGE_INFO;
_imageInfo->hMemDC = CreateCompatibleDC(hdc); //빈 DC영역 생성해줌
_imageInfo->hBit = (HBITMAP)CreateCompatibleBitmap(hdc, width, height);
_imageInfo->hOBit = (HBITMAP)SelectObject(_imageInfo->hMemDC, _imageInfo->hBit);
_imageInfo->width = width;
_imageInfo->height = height;
//위에 초기화를 제대로했는데 잘 안됐다면?
if (_imageInfo == nullptr)
{
release();
return E_FAIL;
}
ReleaseDC(_hWnd, hdc);
return S_OK;
}
HRESULT image::init(const char * fileName, int width, int height, BOOL trans, COLORREF transColor)
{
if (_imageInfo != NULL) release();
//DC를 가져다 사용할땐 GetDC()
HDC hdc = GetDC(_hWnd);
int len = strlen(fileName);
_fileName = new CHAR[len + 1];
strcpy_s(_fileName, len + 1, fileName);
_trans = trans;
_transColor = transColor;
_imageInfo = new IMAGE_INFO;
_imageInfo->loadType = LOAD_FILE;
_imageInfo->resID = 0;
_imageInfo->hMemDC = CreateCompatibleDC(hdc);
_imageInfo->width = width;
_imageInfo->height = height;
_imageInfo->hBit = (HBITMAP)LoadImage(_hInstance, _fileName, IMAGE_BITMAP, _imageInfo->width, _imageInfo->height, LR_LOADFROMFILE);
_imageInfo->hOBit = (HBITMAP)SelectObject(_imageInfo->hMemDC, _imageInfo->hBit);
//만약에 이미지 초기화가 실패하면
if (_imageInfo->hBit == NULL)
{
release();
return E_FAIL;
}
//DC를 가져와서 사용하고 끝에 해제해준다
ReleaseDC(_hWnd, hdc);
return S_OK;
}
HRESULT image::init(const char* fileName, float x, float y, int width, int height, int frameX, int frameY, BOOL trans, COLORREF transColor)
{
if (_imageInfo != NULL) release();
//DC를 가져다 사용할땐 GetDC()
HDC hdc = GetDC(_hWnd);
int len = strlen(fileName);
_fileName = new CHAR[len + 1];
strcpy_s(_fileName, len + 1, fileName);
_trans = trans;
_transColor = transColor;
_imageInfo = new IMAGE_INFO;
_imageInfo->loadType = LOAD_FILE;
_imageInfo->resID = 0;
_imageInfo->hMemDC = CreateCompatibleDC(hdc);
_imageInfo->x = x - (width / 2);
_imageInfo->y = y - (height / 2);
_imageInfo->width = width;
_imageInfo->height = height;
_imageInfo->hBit = (HBITMAP)LoadImage(_hInstance, _fileName, IMAGE_BITMAP, _imageInfo->width, _imageInfo->height, LR_LOADFROMFILE);
_imageInfo->hOBit = (HBITMAP)SelectObject(_imageInfo->hMemDC, _imageInfo->hBit);
_imageInfo->frameWidth = width / frameX;
_imageInfo->frameHeight = height / frameY;
_imageInfo->maxFrameX = frameX - 1;
_imageInfo->maxFrameY = frameY - 1;
//만약에 이미지 초기화가 실패하면
if (_imageInfo->hBit == NULL)
{
release();
return E_FAIL;
}
//DC를 가져와서 사용하고 끝에 해제해준다
ReleaseDC(_hWnd, hdc);
return S_OK;
}
HRESULT image::init(const char* fileName, int width, int height, int frameX, int frameY, BOOL trans, COLORREF transColor)
{
if (_imageInfo != NULL) release();
//DC를 가져다 사용할땐 GetDC()
HDC hdc = GetDC(_hWnd);
int len = strlen(fileName);
_fileName = new CHAR[len + 1];
strcpy_s(_fileName, len + 1, fileName);
_trans = trans;
_transColor = transColor;
_imageInfo = new IMAGE_INFO;
_imageInfo->loadType = LOAD_FILE;
_imageInfo->resID = 0;
_imageInfo->hMemDC = CreateCompatibleDC(hdc);
_imageInfo->width = width;
_imageInfo->height = height;
_imageInfo->hBit = (HBITMAP)LoadImage(_hInstance, _fileName, IMAGE_BITMAP, _imageInfo->width, _imageInfo->height, LR_LOADFROMFILE);
_imageInfo->hOBit = (HBITMAP)SelectObject(_imageInfo->hMemDC, _imageInfo->hBit);
_imageInfo->frameWidth = width / frameX;
_imageInfo->frameHeight = height / frameY;
_imageInfo->maxFrameX = frameX - 1;
_imageInfo->maxFrameY = frameY - 1;
//만약에 이미지 초기화가 실패하면
if (_imageInfo->hBit == NULL)
{
release();
return E_FAIL;
}
//DC를 가져와서 사용하고 끝에 해제해준다
ReleaseDC(_hWnd, hdc);
return S_OK;
}
void image::ScaleImage(float ratio)
{
HDC hdc = GetDC(_hWnd);
HDC tempMemDC = CreateCompatibleDC(hdc);
HBITMAP newHbit = (HBITMAP)CreateCompatibleBitmap(hdc, _imageInfo->width * ratio, _imageInfo->height * ratio);
SelectObject(tempMemDC, newHbit);
StretchBlt(tempMemDC,
0, 0,
_imageInfo->width * ratio,
_imageInfo->height * ratio,
_imageInfo->hMemDC,
0, 0,
_imageInfo->width,
_imageInfo->height,
SRCCOPY
);
SelectObject(_imageInfo->hMemDC, _imageInfo->hOBit);
newHbit = (HBITMAP)CreateCompatibleBitmap(hdc, _imageInfo->width * ratio, _imageInfo->height * ratio);
_imageInfo->hBit = newHbit;
_imageInfo->hOBit = (HBITMAP)SelectObject(_imageInfo->hMemDC, newHbit);
BitBlt(_imageInfo->hMemDC, 0, 0, _imageInfo->width * ratio, _imageInfo->height * ratio, tempMemDC, 0, 0, SRCCOPY);
_imageInfo->width *= ratio;
_imageInfo->height *= ratio;
}
void image::release()
{
if (_imageInfo)
{
SelectObject(_imageInfo->hMemDC, _imageInfo->hOBit);
DeleteObject(_imageInfo->hBit);
DeleteDC(_imageInfo->hMemDC);
SAFE_DELETE(_imageInfo);
SAFE_DELETE(_fileName);
_trans = false;
_transColor = RGB(0, 0, 0);
}
}
void image::setTransColor(BOOL trans, COLORREF transColor)
{
_trans = trans;
_transColor = transColor;
}
void image::render(HDC hdc, int destX, int destY)
{
if (_trans)
{
GdiTransparentBlt(
hdc, //복사될 영역의 DC
destX, //복사될 좌표(left)
destY, //복사될 좌표(top)
_imageInfo->width, //복사될 크기 (가로크기)
_imageInfo->height, //복사될 크기 (세로크기)
_imageInfo->hMemDC, //복사해올 DC
0, 0, //복사해올 시작좌표(left, top)
_imageInfo->width, //복사해올 가로크기
_imageInfo->height, //복사해올 세로크기
_transColor //복사할때 제외할 픽셀값
);
}
else
{
//DC영역간의 고속복사
BitBlt(
hdc, //복사할 DC
destX, //복사할 좌표(left)
destY, //복사할 좌표(top)
_imageInfo->width, //복사할 가로크기
_imageInfo->height, //복사할 세로크기
_imageInfo->hMemDC, //복사될 DC
0, 0, //복사될 시작좌표(left, top)
SRCCOPY); //복사할때 변형일으키지말어라~
}
}
void image::render(HDC hdc, int destX, int destY, int sourX, int sourY, int sourWidth, int sourHeight)
{
if (_trans)
{
GdiTransparentBlt(
hdc, //복사될 영역의 DC
destX, //복사될 좌표(left)
destY, //복사될 좌표(top)
sourWidth, //복사될 크기 (가로크기)
sourHeight, //복사될 크기 (세로크기)
_imageInfo->hMemDC, //복사해올 DC
sourX, sourY, //복사해올 시작좌표(left, top)
sourWidth, //복사해올 가로크기
sourHeight, //복사해올 세로크기
_transColor //복사할때 제외할 픽셀값
);
}
else
{
//DC영역간의 고속복사
BitBlt(
hdc, //복사할 DC
destX, //복사할 좌표(left)
destY, //복사할 좌표(top)
sourWidth, //복사할 가로크기
sourHeight, //복사할 세로크기
_imageInfo->hMemDC, //복사될 DC
sourX, sourY, //복사될 시작좌표(left, top)
SRCCOPY); //복사할때 변형일으키지말어라~
}
}
void image::frameRender(HDC hdc, int destX, int destY)
{
if (_trans)
{
GdiTransparentBlt(
hdc, //복사될 영역의 DC
destX, //복사될 좌표(left)
destY, //복사될 좌표(top)
_imageInfo->frameWidth, //복사될 크기 (가로크기)
_imageInfo->frameHeight, //복사될 크기 (세로크기)
_imageInfo->hMemDC, //복사해올 DC
_imageInfo->currentFrameX * _imageInfo->frameWidth,
_imageInfo->currentFrameY * _imageInfo->frameHeight, //복사해올 시작좌표(left, top)
_imageInfo->frameWidth, //복사해올 가로크기
_imageInfo->frameHeight, //복사해올 세로크기
_transColor //복사할때 제외할 픽셀값
);
}
else
{
//DC영역간의 고속복사
BitBlt(
hdc, //복사할 DC
destX, //복사할 좌표(left)
destY, //복사할 좌표(top)
_imageInfo->frameWidth, //복사할 가로크기
_imageInfo->frameHeight,//복사할 세로크기
_imageInfo->hMemDC, //복사될 DC
_imageInfo->currentFrameX * _imageInfo->frameWidth,
_imageInfo->currentFrameY * _imageInfo->frameHeight, //복사될 시작좌표(left, top)
SRCCOPY); //복사할때 변형일으키지말어라~
}
}
void image::frameRender(HDC hdc, int destX, int destY, int currentFrameX, int currentFrameY)
{
_imageInfo->currentFrameX = currentFrameX;
_imageInfo->currentFrameY = currentFrameY;
if (_trans)
{
GdiTransparentBlt(
hdc, //복사될 영역의 DC
destX, //복사될 좌표(left)
destY, //복사될 좌표(top)
_imageInfo->frameWidth, //복사될 크기 (가로크기)
_imageInfo->frameHeight, //복사될 크기 (세로크기)
_imageInfo->hMemDC, //복사해올 DC
_imageInfo->currentFrameX * _imageInfo->frameWidth,
_imageInfo->currentFrameY * _imageInfo->frameHeight, //복사해올 시작좌표(left, top)
_imageInfo->frameWidth, //복사해올 가로크기
_imageInfo->frameHeight, //복사해올 세로크기
_transColor //복사할때 제외할 픽셀값
);
}
else
{
//DC영역간의 고속복사
BitBlt(
hdc, //복사할 DC
destX, //복사할 좌표(left)
destY, //복사할 좌표(top)
_imageInfo->frameWidth, //복사할 가로크기
_imageInfo->frameHeight,//복사할 세로크기
_imageInfo->hMemDC, //복사될 DC
_imageInfo->currentFrameX * _imageInfo->frameWidth,
_imageInfo->currentFrameY * _imageInfo->frameHeight, //복사될 시작좌표(left, top)
SRCCOPY); //복사할때 변형일으키지말어라~
}
}