-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraphics.cpp
110 lines (88 loc) · 2.15 KB
/
Graphics.cpp
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
#include "Graphics.h"
#include "ScreenBuffer.h"
#include "arduino.h"
#include <string.h>
#include "Point.h"
Graphics* Graphics::_instance = 0;
Graphics::Graphics(ScreenBuffer* screenBuffer, Font* font) : _screenBuffer(screenBuffer), _font(font)
{
_instance = this;
}
Graphics::~Graphics(void)
{
_instance = 0;
}
Graphics* Graphics::GetInstance()
{
return _instance;
}
void Graphics::DrawRectangle(Vector2 pos, int width, int height, bool fill)
{
// Round vector to point
Point posPt(pos);
int bufferWidth = _screenBuffer->GetWidth();
int bufferHeight = _screenBuffer->GetHeight();
// Early clip
if (posPt.X > bufferWidth || posPt.Y > bufferHeight)
return;
int xMax = height + posPt.X;
int yMax = width + posPt.Y;
for (int row=posPt.X; row<xMax; row++)
{
for (int col=posPt.Y; col<yMax; col++)
{
_screenBuffer->Write(col, row, 1);
}
}
}
void Graphics::DrawLine(Vector2 v1, Vector2 v2)
{
// Get direction from v1 to v2
Vector2 dir = Vector2::Normalize(v2 - v1);
float distance = Vector2::Distance(v1, v2);
for (int i=0; i<distance; i++)
{
Point pixel(v1 + dir * i);
_screenBuffer->Write(pixel.X, pixel.Y, 1);
}
}
void Graphics::DrawString(const String& str, Point pos, int spacing)
{
Point offset = pos;
for (int i=0; i<str.length(); ++i)
{
char c = str[i];
// Draw the char
FontChar* fontChar = _font->Chars[(int)c];
DrawFontChar(fontChar, offset);
// Apply spacing
offset.X += spacing + fontChar->Width;
}
}
void Graphics::DrawChar(char c, Point pos)
{
FontChar* fontChar = _font->Chars[(int)c];
DrawFontChar(fontChar, pos);
}
void Graphics::DrawFontChar(FontChar* fontChar, Point pos)
{
int bufferWidth = _screenBuffer->GetWidth();
int bufferHeight = _screenBuffer->GetHeight();
// Early clip
if (pos.X > bufferWidth || pos.Y > bufferHeight)
return;
if (pos.X < -fontChar->Width || pos.Y < -fontChar->Height)
return;
// Draw each pixel of the char
for (int x=0; x<fontChar->Width; ++x)
{
int xPixel = pos.X + x;
for (int y=0; y<fontChar->Height; ++y)
{
int yPixel = pos.Y + y + fontChar->OffsetY;
// Render pixel
if (fontChar->GetValue(x, y) > 0)
_screenBuffer->Write(xPixel, yPixel, 1);
}
}
}