forked from BuildSucceeded/2D-Platformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngineBase.cpp
More file actions
164 lines (126 loc) · 3.4 KB
/
EngineBase.cpp
File metadata and controls
164 lines (126 loc) · 3.4 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
#include "framework.h"
#include "Settings.h"
#include "Point2D.h"
#include "EngineBase.h"
#include "GameObjectBase.h"
#pragma comment(lib, "d2d1")
#pragma comment(lib, "dwrite")
#pragma comment(lib, "Windowscodecs.lib")
EngineBase::EngineBase() : m_pDirect2dFactory(NULL), m_pRenderTarget(NULL)
{
}
EngineBase::~EngineBase()
{
SafeRelease(&m_pDirect2dFactory);
SafeRelease(&m_pRenderTarget);
}
HRESULT EngineBase::InitializeD2D(HWND m_hwnd)
{
// Initializes Direct2D, for drawing
D2D1_SIZE_U size = D2D1::SizeU(RESOLUTION_X, RESOLUTION_Y);
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
m_pDirect2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_hwnd, size, V_SYNC ? D2D1_PRESENT_OPTIONS_NONE : D2D1_PRESENT_OPTIONS_IMMEDIATELY),
&m_pRenderTarget
);
return S_OK;
}
void EngineBase::MousePosition(int x, int y)
{
mousePosition.x = x;
mousePosition.y = y;
}
void EngineBase::KeyUp(WPARAM wParam)
{
}
void EngineBase::KeyDown(WPARAM wParam)
{
}
void EngineBase::MouseButtonUp(bool left, bool right)
{
}
void EngineBase::MouseButtonDown(bool left, bool right)
{
}
void EngineBase::AddGameObject(GameObjectBase* gameObj)
{
gameObj->engine = this;
objectList.push_back(gameObj);
}
void EngineBase::RemoveGameObject(GameObjectBase* gameObj)
{
objectList.remove(gameObj);
}
void EngineBase::Logic(double elapsedTime)
{
std::list<GameObjectBase*>::iterator iter;
for (iter = objectList.begin(); iter != objectList.end(); iter++)
(*iter)->Logic(elapsedTime);
}
HRESULT EngineBase::Draw()
{
// Draws the elements in the game using Direct2D
HRESULT hr;
m_pRenderTarget->BeginDraw();
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_pRenderTarget->Clear(D2D1::ColorF(D2D_BACKGROUND_COLOR));
std::list<GameObjectBase*>::iterator iter;
for (iter = objectList.begin(); iter != objectList.end(); iter++)
(*iter)->Draw(m_pRenderTarget);
hr = m_pRenderTarget->EndDraw();
return S_OK;
}
ID2D1Bitmap* EngineBase::LoadImage(LPCWSTR imageFile)
{
IWICBitmapDecoder* pDecoder = NULL;
IWICBitmapFrameDecode* pSource = NULL;
IWICFormatConverter* pConverter = NULL;
IWICImagingFactory* pIWICFactory = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, reinterpret_cast<void**>(&pIWICFactory));
HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
imageFile,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
ID2D1Bitmap* toReturn = NULL;
if (SUCCEEDED(hr))
{
// Create a Direct2D bitmap from the WIC bitmap.
hr = m_pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
&toReturn
);
}
SafeRelease(&pIWICFactory);
SafeRelease(&pDecoder);
SafeRelease(&pSource);
SafeRelease(&pConverter);
return toReturn;
}