Incremental Engine  1.0.6
A 2D Game Engine to create Idle Games
IncrementalEngine::Bitmap Class Reference

#include <Bitmap.h>

Inheritance diagram for IncrementalEngine::Bitmap:
IncrementalEngine::TextureBase IncrementalEngine::Drawable

Classes

struct  VertexType
 

Public Member Functions

 Bitmap ()
 
 ~Bitmap () override
 
HRESULT Create (Texture *texture)
 
virtual HRESULT Draw (ID3D11DeviceContext *deviceContext) override
 
int GetIndexCount ()
 
- Public Member Functions inherited from IncrementalEngine::TextureBase
 TextureBase ()
 
virtual ~TextureBase ()
 
void Update (Transform transform, FloatRect bounds)
 
TextureGetTexture ()
 
D3DXVECTOR2 GetSize ()
 

Private Member Functions

HRESULT InitializeBuffers ()
 
HRESULT UpdateBuffers (ID3D11DeviceContext *deviceContext)
 
void RenderBuffers (ID3D11DeviceContext *deviceContext)
 

Private Attributes

ID3D11Buffer * _vertexBuffer
 
ID3D11Buffer * _indexBuffer
 
int _vertexCount
 
int _indexCount
 
Transform _previousTransform
 
FloatRect _previousBounds
 

Additional Inherited Members

- Protected Member Functions inherited from IncrementalEngine::Drawable
void CopyParameters (Drawable *drawable)
 
- Protected Attributes inherited from IncrementalEngine::TextureBase
Texture_texture
 
Transform _transform
 
FloatRect _bounds
 
- Protected Attributes inherited from IncrementalEngine::Drawable
RenderWindow_renderWindow
 
ShaderManager_shaderManager
 
ID3D11Device * _device
 

Detailed Description

Definition at line 9 of file Bitmap.h.

Constructor & Destructor Documentation

◆ Bitmap()

IncrementalEngine::Bitmap::Bitmap ( )

Definition at line 7 of file Bitmap.cpp.

7  :
8  _vertexBuffer(NULL),
9  _indexBuffer(NULL)
10  {
11  _previousTransform = Transform();
12  _previousBounds = { 0, 0, 0, 0 };
13  }

◆ ~Bitmap()

IncrementalEngine::Bitmap::~Bitmap ( )
override

Definition at line 15 of file Bitmap.cpp.

16  {
17  CHECKED_RELEASE(_indexBuffer);
18  CHECKED_RELEASE(_vertexBuffer);
19  }

Member Function Documentation

◆ Create()

HRESULT IncrementalEngine::Bitmap::Create ( Texture texture)

Definition at line 21 of file Bitmap.cpp.

22  {
23  HRESULT result;
24 
25  _previousTransform = Transform();
26 
27  result = InitializeBuffers();
28  if (FAILED(result))
29  {
30  MessageBox(_renderWindow->GetHWND(), L"Could not initialize Bitmap Buffers.", L"Error", MB_OK);
31  return CO_E_ERRORINAPP;
32  }
33 
34  result = texture->Init(_device);
35  if (FAILED(result))
36  {
37  MessageBox(_renderWindow->GetHWND(), L"Could not load Bitmap Texture.", L"Error", MB_OK);
38  return CO_E_ERRORINAPP;
39  }
40 
41  _texture = texture;
42 
43  return S_OK;
44  }

◆ Draw()

HRESULT IncrementalEngine::Bitmap::Draw ( ID3D11DeviceContext *  deviceContext)
overridevirtual

Implements IncrementalEngine::Drawable.

Definition at line 46 of file Bitmap.cpp.

47  {
48  HRESULT result;
49 
50  result = UpdateBuffers(deviceContext);
51  if (FAILED(result))
52  {
53  return CO_E_ERRORINAPP;
54  }
55 
56  RenderBuffers(deviceContext);
57 
59  if (FAILED(result))
60  {
61  return CO_E_ERRORINAPP;
62  }
63 
64  return S_OK;
65  }

◆ GetIndexCount()

int IncrementalEngine::Bitmap::GetIndexCount ( )

Definition at line 67 of file Bitmap.cpp.

68  {
69  return _indexCount;
70  }

◆ InitializeBuffers()

HRESULT IncrementalEngine::Bitmap::InitializeBuffers ( )
private

Definition at line 72 of file Bitmap.cpp.

73  {
74  VertexType* vertices;
75  unsigned long* indices;
76  D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
77  D3D11_SUBRESOURCE_DATA vertexData, indexData;
78  HRESULT result;
79 
80  _indexCount = _vertexCount = 6; //TODO: Move into a constant
81 
82  vertices = new VertexType[_vertexCount];
83  if (!vertices)
84  {
85  return CO_E_ERRORINAPP;
86  }
87 
88  indices = new unsigned long[_indexCount];
89  if (!indices)
90  {
91  return CO_E_ERRORINAPP;
92  }
93 
94  memset(vertices, 0, (sizeof(VertexType) * _vertexCount));
95 
96  for (int i = 0; i < _indexCount; i++)
97  {
98  indices[i] = i;
99  }
100 
101  vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
102  vertexBufferDesc.ByteWidth = sizeof(VertexType) * _vertexCount;
103  vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
104  vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
105  vertexBufferDesc.MiscFlags = 0;
106  vertexBufferDesc.StructureByteStride = 0;
107 
108  vertexData.pSysMem = vertices;
109  vertexData.SysMemPitch = 0;
110  vertexData.SysMemSlicePitch = 0;
111 
112  result = _device->CreateBuffer(&vertexBufferDesc, &vertexData, &_vertexBuffer);
113  if (FAILED(result))
114  {
115  return CO_E_ERRORINAPP;
116  }
117 
118  indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
119  indexBufferDesc.ByteWidth = sizeof(unsigned long) * _indexCount;
120  indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
121  indexBufferDesc.CPUAccessFlags = 0;
122  indexBufferDesc.MiscFlags = 0;
123  indexBufferDesc.StructureByteStride = 0;
124 
125  indexData.pSysMem = indices;
126  indexData.SysMemPitch = 0;
127  indexData.SysMemSlicePitch = 0;
128 
129  result = _device->CreateBuffer(&indexBufferDesc, &indexData, &_indexBuffer);
130  if (FAILED(result))
131  {
132  return CO_E_ERRORINAPP;
133  }
134 
135  delete[] vertices;
136  vertices = 0;
137 
138  delete[] indices;
139  indices = 0;
140 
141  return S_OK;
142  }

◆ RenderBuffers()

void IncrementalEngine::Bitmap::RenderBuffers ( ID3D11DeviceContext *  deviceContext)
private

Definition at line 211 of file Bitmap.cpp.

212  {
213  unsigned int stride = sizeof(VertexType);
214  unsigned int offset = 0;
215 
216  deviceContext->IASetVertexBuffers(0, 1, &_vertexBuffer, &stride, &offset);
217  deviceContext->IASetIndexBuffer(_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
218  deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
219  }

◆ UpdateBuffers()

HRESULT IncrementalEngine::Bitmap::UpdateBuffers ( ID3D11DeviceContext *  deviceContext)
private

Definition at line 144 of file Bitmap.cpp.

145  {
147  {
148  return S_OK;
149  }
150 
151  VertexType* vertices;
152  D3D11_MAPPED_SUBRESOURCE mappedResource;
153  VertexType* verticesPtr;
154  HRESULT result;
155 
158 
159  vertices = new VertexType[_vertexCount];
160  if (!vertices)
161  {
162  return CO_E_ERRORINAPP;
163  }
164 
165  float left = _bounds.left();
166  float right = left + _bounds.width();
167  float top = _bounds.top();
168  float bottom = top + _bounds.height();
169 
170  D3DXVECTOR2 topLeft = { left, top };
171  D3DXVECTOR2 bottomRight = { right, bottom };
172  D3DXVECTOR2 topRight = { right, top };
173  D3DXVECTOR2 bottomLeft = { left, bottom };
174 
175  topLeft = _transform * topLeft;
176  bottomRight = _transform * bottomRight;
177  topRight = _transform * topRight;
178  bottomLeft = _transform * bottomLeft;
179 
180  vertices[0].position = D3DXVECTOR3(topLeft.x, topLeft.y, 0.0f);
181  vertices[0].texture = D3DXVECTOR2(0.0f, 0.0f);
182  vertices[1].position = D3DXVECTOR3(bottomRight.x, bottomRight.y, 0.0f);
183  vertices[1].texture = D3DXVECTOR2(1.0f, 1.0f);
184  vertices[2].position = D3DXVECTOR3(bottomLeft.x, bottomLeft.y, 0.0f);
185  vertices[2].texture = D3DXVECTOR2(0.0f, 1.0f);
186 
187  vertices[3].position = D3DXVECTOR3(topLeft.x, topLeft.y, 0.0f);
188  vertices[3].texture = D3DXVECTOR2(0.0f, 0.0f);
189  vertices[4].position = D3DXVECTOR3(topRight.x, topRight.y, 0.0f);
190  vertices[4].texture = D3DXVECTOR2(1.0f, 0.0f);
191  vertices[5].position = D3DXVECTOR3(bottomRight.x, bottomRight.y, 0.0f);
192  vertices[5].texture = D3DXVECTOR2(1.0f, 1.0f);
193 
194  result = deviceContext->Map(_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
195  if (FAILED(result))
196  {
197  return CO_E_ERRORINAPP;
198  }
199 
200  verticesPtr = (VertexType*)mappedResource.pData;
201  memcpy(verticesPtr, (void*)vertices, (sizeof(VertexType) * _vertexCount));
202 
203  deviceContext->Unmap(_vertexBuffer, 0);
204 
205  delete[] vertices;
206  vertices = 0;
207 
208  return S_OK;
209  }

Member Data Documentation

◆ _indexBuffer

ID3D11Buffer* IncrementalEngine::Bitmap::_indexBuffer
private

Definition at line 28 of file Bitmap.h.

◆ _indexCount

int IncrementalEngine::Bitmap::_indexCount
private

Definition at line 30 of file Bitmap.h.

◆ _previousBounds

FloatRect IncrementalEngine::Bitmap::_previousBounds
private

Definition at line 33 of file Bitmap.h.

◆ _previousTransform

Transform IncrementalEngine::Bitmap::_previousTransform
private

Definition at line 32 of file Bitmap.h.

◆ _vertexBuffer

ID3D11Buffer* IncrementalEngine::Bitmap::_vertexBuffer
private

Definition at line 27 of file Bitmap.h.

◆ _vertexCount

int IncrementalEngine::Bitmap::_vertexCount
private

Definition at line 30 of file Bitmap.h.

IncrementalEngine::Bitmap::_vertexCount
int _vertexCount
Definition: Bitmap.h:30
IncrementalEngine::Drawable::_device
ID3D11Device * _device
Definition: Drawable.h:18
IncrementalEngine::FloatRect::left
float left() const
Definition: FloatRect.cpp:25
IncrementalEngine::Bitmap::_previousBounds
FloatRect _previousBounds
Definition: Bitmap.h:33
IncrementalEngine::Bitmap::_indexCount
int _indexCount
Definition: Bitmap.h:30
IncrementalEngine::FloatRect::top
float top() const
Definition: FloatRect.cpp:15
IncrementalEngine::FloatRect::width
float width() const
Definition: FloatRect.cpp:35
IncrementalEngine::Bitmap::_indexBuffer
ID3D11Buffer * _indexBuffer
Definition: Bitmap.h:28
IncrementalEngine::Bitmap::RenderBuffers
void RenderBuffers(ID3D11DeviceContext *deviceContext)
Definition: Bitmap.cpp:211
IncrementalEngine::Drawable::_shaderManager
ShaderManager * _shaderManager
Definition: Drawable.h:17
IncrementalEngine::TextureBase::_bounds
FloatRect _bounds
Definition: TextureBase.h:28
IncrementalEngine::Texture::GetTexture
ID3D11ShaderResourceView * GetTexture()
Definition: Texture.cpp:53
IncrementalEngine::Bitmap::UpdateBuffers
HRESULT UpdateBuffers(ID3D11DeviceContext *deviceContext)
Definition: Bitmap.cpp:144
IncrementalEngine::Bitmap::InitializeBuffers
HRESULT InitializeBuffers()
Definition: Bitmap.cpp:72
IncrementalEngine::TextureBase::_transform
Transform _transform
Definition: TextureBase.h:27
IncrementalEngine::Bitmap::_previousTransform
Transform _previousTransform
Definition: Bitmap.h:32
IncrementalEngine::Bitmap::_vertexBuffer
ID3D11Buffer * _vertexBuffer
Definition: Bitmap.h:27
IncrementalEngine::Drawable::_renderWindow
RenderWindow * _renderWindow
Definition: Drawable.h:16
IncrementalEngine::FloatRect::height
float height() const
Definition: FloatRect.cpp:45
IncrementalEngine::ShaderManager::RenderTextureShader
HRESULT RenderTextureShader(int indexCount, ID3D11ShaderResourceView *texture)
Definition: ShaderManager.cpp:126
IncrementalEngine::RenderWindow::GetHWND
HWND GetHWND()
Definition: RenderWindow.cpp:105
IncrementalEngine::TextureBase::_texture
Texture * _texture
Definition: TextureBase.h:26