-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpriteSheet.cpp
More file actions
119 lines (90 loc) · 3.46 KB
/
SpriteSheet.cpp
File metadata and controls
119 lines (90 loc) · 3.46 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
#include "SpriteSheet.h"
SpriteSheet::SpriteSheet(LPCWSTR filename, Graphics* gfx) {
this->gfx = gfx;
bmp = NULL;
HRESULT hr;
//------------------------------------------------------------------------------------------------
// WIC factory make. It can be used to load images from files. Call CoCreateInstance to create it
//------------------------------------------------------------------------------------------------
IWICImagingFactory* wicFactory = NULL;
hr = CoCreateInstance(
CLSID_WICImagingFactory, // CLS ID of the object we're making
NULL, // Not part of an aggregate
CLSCTX_INPROC_SERVER, // DLL runs in the same proces
IID_IWICImagingFactory, // Ref to interface that
(LPVOID*)&wicFactory // The pointer that will contain our factory
);
IWICBitmapDecoder* wicDecoder = NULL;
// Create a decoder
hr = wicFactory->CreateDecoderFromFilename(
filename, // The file name
NULL, // No preferred vendor
GENERIC_READ, // We're reading the file, nor writing
WICDecodeMetadataCacheOnLoad, // Cache on load*
&wicDecoder // The our decoder we're making
);
// pobieramy klatke naszego obrazu - ta liczba przy getframe to numer klatki - jezeli gif
IWICBitmapFrameDecode* wicFrame = nullptr;
hr = wicDecoder->GetFrame(0, &wicFrame);
IWICFormatConverter* wicConverter = NULL;
hr = wicFactory->CreateFormatConverter(&wicConverter);
// setup converter
hr = wicConverter->Initialize(
wicFrame,
GUID_WICPixelFormat32bppPRGBA, // pixel format - WIC pixel - 32bbp (bit per pixel) color palette - RGBA
WICBitmapDitherTypeNone, // irrevelant
NULL, // no pallete needed - irrevelent
0.0, // Alpha transparency - irrevelant
WICBitmapPaletteTypeCustom // irrevelant
);
// Use converter to crate D2D1Bitmap - finally
hr = gfx->GetRenderTarget()->CreateBitmapFromWicBitmap(
wicConverter,
NULL,
&bmp
);
// now we don't need this WIC objects
if (wicFactory) wicFactory->Release();
if (wicDecoder) wicDecoder->Release();
if (wicConverter) wicConverter->Release();
if (wicFrame) wicFrame->Release();
spriteWidth = bmp->GetSize().width;
spriteHeight = bmp->GetSize().height;
spritesAccross = 1;
}
SpriteSheet::SpriteSheet(LPCWSTR filename, Graphics* gfx, float spriteWidth, float spriteHeight) : SpriteSheet(filename, gfx) {
this->spriteWidth = spriteWidth;
this->spriteHeight = spriteHeight;
this->spritesAccross = static_cast<int>(bmp->GetSize().width / spriteWidth);
}
SpriteSheet::~SpriteSheet() {
if (bmp) bmp->Release();
}
//draw bitmap to the render target
void SpriteSheet::Draw() {
gfx->GetRenderTarget()->DrawBitmap(
bmp,
D2D1::RectF(0.0f, 0.0f,
bmp->GetSize().width, bmp->GetSize().height),
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
D2D1::RectF(0.0f, 0.0f,
bmp->GetSize().width, bmp->GetSize().height)
);
}
void SpriteSheet::Draw(int index, float x, float y) {
D2D_RECT_F src = D2D1::RectF(
(index % spritesAccross) * spriteWidth, // claculate position of sprite
(index / spritesAccross) * spriteHeight,
(index % spritesAccross) * spriteHeight + spriteWidth, //calculate size of sprite
(index / spritesAccross) * spriteHeight + spriteHeight
);
D2D_RECT_F dest = D2D1::RectF(x, y, x + spriteWidth, y + spriteHeight);
gfx->GetRenderTarget()->DrawBitmap(
bmp,
dest, //destination rectangle
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
src //source rectangle
);
}