-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTyper.cpp
More file actions
204 lines (190 loc) · 3.76 KB
/
Typer.cpp
File metadata and controls
204 lines (190 loc) · 3.76 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Author: Brendon Kofink
* Johann Rajadurai
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Ball Game
* Assignment Description: user can launch balls to hit objects.
* Due Date: 12/08/2021
* Date Created: 11/30/2021
* Date Last Modified: 12/03/2021
*/
#include "Typer.h"
//Constructor
TextObject::TextObject(string text, vec2 pos, int size, Color color, bool back, bool del)
{
this->text = text;
this->pos = pos;
this->size = size;
this->layer = layer;
this->color = color;
this->back = back;
this->del = del;
}
Typer::Typer()
{
ImportLetters();
ImportNumbers();
ImportSymbols();
}
//Each of these imports does the same thing.
//Just loop through the proper file and add to array.
void Typer::ImportLetters()
{
ifstream file("A.txt");
char c;
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
file.get(c);
if (c == '1')
{
characters[i][j][k] = true;
}
else
{
characters[i][j][k] = false;
}
}
file.ignore();
}
}
file.close();
}
void Typer::ImportNumbers()
{
ifstream file("num.txt");
char c;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
file.get(c);
if (c == '1')
{
characters[i + 26][j][k] = true;
}
else
{
characters[i + 26][j][k] = false;
}
}
file.ignore();
}
}
file.close();
}
void Typer::ImportSymbols()
{
ifstream file("special.txt");
char c;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
file.get(c);
if (c == '1')
{
characters[i + 36][j][k] = true;
}
else
{
characters[i + 36][j][k] = false;
}
}
file.ignore();
}
}
file.close();
}
bool Typer::TryLetter(char c, int &index)
{
bool found = false;
if (c == '!')
{
index = 36;
found = true;
}
else if (c == ':')
{
index = 37;
found = true;
}
else if (c == '.')
{
index = 38;
found = true;
}
else if (c == '-')
{
index = 39;
found = true;
}
else if (c == '+')
{
index = 40;
found = true;
}
return found;
}
//Helper function
void Typer::DrawBox(SDL_Plotter &g, int x, int y, int size, Color c)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (x+i >= 0 && x+i < g.getCol() && y+j >= 0 && y+j < g.getRow())
{
g.plotPixel(x + i, y + j, c.R, c.G, c.B);
}
}
}
}
void Typer::Write(string word, SDL_Plotter &g, vec2 pos, Color c, int size, bool back)
{
int character;
for (int i = 0; i < word.length(); i++)
{
//Does different things depending what the character is.
if (isalpha(word[i]))
{
character = toupper(word[i]) - 'A';
}
else if (isdigit(word[i]))
{
character = word[i] - '0' + 26;
}
else if (!TryLetter(word[i], character))
{
character = 0;
}
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
if (characters[character][j][k] && word[i] != ' ')
{
//Draw pixel at pos + i, 5 is size of character * size of font.
DrawBox(g, pos.x + i * 5 * size + i * size + k * size, pos.y + j * size, size, c);
}
else if (back)
{
DrawBox(g, pos.x + i * 5 * size + i * size + k * size, pos.y + j * size, size, BLANK);
}
}
}
}
}
//Writes TextObject instead of normal parameters.
void Typer::Write(TextObject text, SDL_Plotter &g)
{
Write(text.text, g, text.pos, text.color, text.size, text.back);
}