Avionics
Dropship Simulator
Text.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 A format specifier follows this prototype:
4 %[flags][width][.precision][length]specifier
5 
6 Where the specifier character at the end is the most significant component, since it defines the type and the interpretation of its corresponding argument:
7 specifier Output Example
8 d or i Signed decimal integer 392
9 u Unsigned decimal integer 7235
10 o Unsigned octal 610
11 x Unsigned hexadecimal integer 7fa
12 X Unsigned hexadecimal integer (uppercase) 7FA
13 f Decimal floating point 392.65
14 e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
15 E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
16 g Use the shortest representation: %e or %f 392.65
17 G Use the shortest representation: %E or %f 392.65
18 a Hexadecimal floating point, lowercase -0xc.90fep-2
19 A Hexadecimal floating point, uppercase -0XC.90FEP-2
20 c Character a
21 s String of characters sample
22 p Pointer address b8000000
23 % A % followed by another % character will write a single % to the stream.
24 
25 The format specifier can also contain sub-specifiers: flags, width, .precision and modifiers (in that order), which are optional and follow these specifications:
26 
27 flags description
28 - Left-justify within the given field width; Right justification is the default (see width sub-specifier).
29 + Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
30 (space) If no sign is going to be written, a blank space is inserted before the value.
31 # Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
32  Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
33 0 Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).
34 
35 width description
36 (number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
37 
38 .precision description
39 .number For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
40 For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
41 For g and G specifiers: This is the maximum number of significant digits to be printed.
42 For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
43 If the period is specified without an explicit value for precision, 0 is assumed.
44 */
45 
46 class Text
47 {
48 private:
49  RECT rect;
50  Logger* logger = nullptr;
51  Bus* bus = nullptr;
52  DEVICE_OBJECT* pDevice = nullptr;
53  ID3DX10Font* pFont = nullptr;
54  bool flip = false;
55 
56 public:
57  // config items
58  int x, y;
59  D3DXCOLOR color;
60  int width = 0, height = 0;
61  int flags = DT_NOCLIP | DT_SINGLELINE | DT_EXPANDTABS;
62  std::string formattedText;
63  std::string elementName;
64  int font = 0;
65 
66  void Initialize(Logger* prmLogger, Bus* prmBus, DEVICE_OBJECT* prmpDevice, int elementX, int elementY, ID3DX10Font* prmFont, bool prmFlip)
67  {
68  logger = prmLogger;
69  pDevice = prmpDevice;
70  pFont = prmFont;
71  flip = prmFlip;
72  bus = prmBus;
73 
74  rect.top = elementY + y;
75  if (height) rect.bottom = rect.top + height; else rect.bottom = 0;
76  rect.left = elementX + x;
77  if (width) rect.right = rect.left + width; else rect.right = 0;
78  }
79 
80  void Draw(WCHAR* str)
81  {
82  HRESULT hr;
83  D3DXCOLOR newcolor;
84  if (bus->IsDim(elementName))
85  newcolor = color * 0.5f;
86  else
87  newcolor = color;
88  if (FAILED(hr = pFont->DrawText(pDevice->pSprite, str, -1, &rect, flags, newcolor)))
89  {
90  logger->Log("Text::DrawText DrawText failed!", Logger::Fatal, hr);
91  }
92  }
93 };
Definition: Text.h:46
ID3DX10Sprite * pSprite
Definition: DeviceObject.h:9
D3DXCOLOR color
Definition: Text.h:59
int x
Definition: Text.h:58
int width
Definition: Text.h:60
RECT rect
Definition: Text.h:49
Definition: Logger.h:5
DEVICE_OBJECT * pDevice
Definition: Text.h:52
int font
Definition: Text.h:64
Bus * bus
Definition: Text.h:51
bool flip
Definition: Text.h:54
Definition: Bus.h:12
void Draw(WCHAR *str)
Definition: Text.h:80
std::string formattedText
Definition: Text.h:62
std::string elementName
Definition: Text.h:63
void Log(const char *msg, Level level=Info, int errorCode=0)
These have to be in this order.
Definition: Logger.cpp:16
int height
Definition: Text.h:60
Logger * logger
Definition: Text.h:50
bool IsDim(std::string elementName)
Definition: Bus.h:300
void Initialize(Logger *prmLogger, Bus *prmBus, DEVICE_OBJECT *prmpDevice, int elementX, int elementY, ID3DX10Font *prmFont, bool prmFlip)
Definition: Text.h:66
ID3DX10Font * pFont
Definition: Text.h:53
int flags
Definition: Text.h:61
int y
Definition: Text.h:58