Avionics
Dropship Simulator
Sprite.h
Go to the documentation of this file.
1 #pragma once
2 
4 class Sprite
5 {
6 private:
7  ID3DX10Sprite* pSprite = nullptr;
8  D3DX10_SPRITE dxSprite;
9  int elementX = 0, elementY = 0, screenHeight = 0;
10  Logger* logger = nullptr;
11  Bus* bus = nullptr;
12  bool flip = false;
13 
14 public:
15  D3D10_TEXTURE2D_DESC desc; // to store details of the texture
16  int x = 0, y = 0, width = 0, height = 0;
17  float z = 1.0f;
18  std::string pathAndFilename;
19  std::string elementName;
20  D3DXCOLOR color = 0xFFFFFFFF;
21  float rotate = 0.0f;
22  bool normallyHidden = false;
23  D3DXVECTOR2 texCoord = D3DXVECTOR2(0, 0);
24  D3DXVECTOR2 texSize = D3DXVECTOR2(1, 1);
25 
26  void Initialize(Logger* prmLogger, Bus* prmBus, DEVICE_OBJECT* prmpDevice, int prmScreenHeight, int prmElementX, int prmElementY, bool prmFlip)
27  {
28  logger = prmLogger;
29  bus = prmBus;
32  pSprite = prmpDevice->pSprite;
33  elementX = prmElementX;
34  elementY = prmElementY;
35  screenHeight = prmScreenHeight;
36  flip = prmFlip;
37 
38  dxSprite.pTexture = prmpDevice->GetTextureSRV(prmLogger, pathAndFilename);
39  if (dxSprite.pTexture)
40  {
41  ID3D10Resource* resource;
42  dxSprite.pTexture->GetResource(&resource);
43  static_cast<ID3D10Texture2D*>(resource)->GetDesc(&desc);
44  SAFE_RELEASE(resource);
45  }
46 
47  Update();
48  }
49 
50  void Update()
51  {
52  dxSprite.TexCoord = texCoord;
53  dxSprite.TexSize = texSize;
54 
55  D3DXMATRIX matScaling;
56  D3DXMatrixScaling(&matScaling, static_cast<float>(width), static_cast<float>(height), 1.0f);
57  D3DXMATRIX matTranslation;
58  D3DXMatrixTranslation(&matTranslation, static_cast<float>(elementX + x + width / 2), static_cast<float>(screenHeight - (elementY + y + height / 2)), z);
59  D3DXMATRIX matRotation;
60  D3DXMatrixRotationZ(&matRotation, rotate);
61  dxSprite.matWorld = matScaling * matRotation * matTranslation;
62  }
63 
64  void Draw()
65  {
66  if (normallyHidden && !bus->IsVisible(elementName)) return;
67 
68  if (bus->IsDim(elementName))
69  dxSprite.ColorModulate = D3DXCOLOR(color.r * 0.5f, color.g * 0.5f, color.b * 0.5f, color.a);
70  else
71  dxSprite.ColorModulate = color;
72 
73  HRESULT hr;
74  if (FAILED(hr = pSprite->DrawSpritesBuffered(&dxSprite, 1)))
75  {
76  logger->Log("Sprite::Draw DrawSpritesBuffered failed!", Logger::Error, hr);
77  }
78  }
79 
81  {
82  RtlZeroMemory(&dxSprite, sizeof(D3DX10_SPRITE));
83  }
84 
86  {
89  dxSprite.pTexture = nullptr;
90  }
91 };
bool IsVisible(std::string elementName)
Definition: Bus.h:316
ID3DX10Sprite * pSprite
Definition: DeviceObject.h:9
int elementX
Definition: Sprite.h:9
Definition: Logger.h:5
~Sprite()
Definition: Sprite.h:85
bool normallyHidden
Definition: Sprite.h:22
int y
Definition: Sprite.h:16
D3DXVECTOR2 texSize
Definition: Sprite.h:24
void Update()
Definition: Sprite.h:50
Sprite()
Definition: Sprite.h:80
D3D10_TEXTURE2D_DESC desc
Definition: Sprite.h:15
float rotate
Definition: Sprite.h:21
int x
Definition: Sprite.h:16
float z
Definition: Sprite.h:17
D3DX10_SPRITE dxSprite
Definition: Sprite.h:8
bool flip
Definition: Sprite.h:12
D3DXVECTOR2 texCoord
Definition: Sprite.h:23
int height
Definition: Sprite.h:16
int elementY
Definition: Sprite.h:9
Definition: Bus.h:12
Definition: Sprite.h:4
void Draw()
Definition: Sprite.h:64
D3DXCOLOR color
Definition: Sprite.h:20
int width
Definition: Sprite.h:16
std::string elementName
Definition: Sprite.h:19
std::string pathAndFilename
Definition: Sprite.h:18
Logger * logger
Definition: Sprite.h:10
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
Bus * bus
Definition: Sprite.h:11
ID3D10ShaderResourceView * GetTextureSRV(Logger *prmLogger, std::string pathAndFilename)
Definition: DeviceObject.h:15
bool IsDim(std::string elementName)
Definition: Bus.h:300
ID3DX10Sprite * pSprite
Definition: Sprite.h:7
void Initialize(Logger *prmLogger, Bus *prmBus, DEVICE_OBJECT *prmpDevice, int prmScreenHeight, int prmElementX, int prmElementY, bool prmFlip)
Definition: Sprite.h:26
int screenHeight
Definition: Sprite.h:9