-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTDraw.cpp
More file actions
184 lines (151 loc) · 4.31 KB
/
TDraw.cpp
File metadata and controls
184 lines (151 loc) · 4.31 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
#include "TDraw.hpp"
TDraw::TDraw(){}
TDraw::~TDraw(){}
void TDraw::init() {
setlocale(LC_ALL, "en_US.UTF-8");
initscr();
start_color();
cbreak();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
for( short i = 0; i < 8; ++i ) {
short v = ((float)i/7)*1000;
init_color(i, v, v, v);
init_pair(i, i, 0);
}
status1 = (char*)malloc(64*sizeof(char));
status2 = (char*)malloc(64*sizeof(char));
width = COLS;
height = LINES;
screen_width = width*SYM_WIDTH;
screen_height = height*SYM_HEIGHT;
screen = (wchar_t*)malloc(height*width*sizeof(wchar_t));
color_data = (char*)malloc(height*width*sizeof(char));
for(int i = 0; i < height*width; ++i) {
screen[i] = DOT_BASE;
color_data[i] = 4;
}
}
void TDraw::deinit() {
free(screen);
free(color_data);
free(status1);
free(status2);
endwin();
}
void TDraw::draw() {
wchar_t block[2] = {0, '\0'};
for(int i = 0; i < height*width; ++i) {
block[0] = screen[i];
attron(COLOR_PAIR(color_data[i]));
mvaddwstr(i/width, i%width, block);
attroff(COLOR_PAIR(color_data[i]));
}
attron(COLOR_PAIR(7));
mvaddstr(height-2, (width-strlen(status1))/2, status1);
mvaddstr(height-1, (width-strlen(status2))/2, status2);
attroff(COLOR_PAIR(7));
free(screen);
free(color_data);
refresh();
width = COLS;
height = LINES;
screen = (wchar_t*)malloc(height*width*sizeof(wchar_t));
color_data = (char*)malloc(height*width*sizeof(char));
for(int i = 0; i < height*width; ++i) {
screen[i] = DOT_BASE;
color_data[i] = 4;
}
screen_width = width*SYM_WIDTH;
screen_height = height*SYM_HEIGHT;
}
void TDraw::drawPolygon ( int* x, int* y, int sides, char color ) {
if( sides <= 0 ) return;
else if( sides == 1 ) { drawPixel( *x, *y, color); return; }
else {
for( int i = 0; i < sides; ++i ) {
drawLine( x[i], y[i], x[(i+1)%sides], y[(i+1)%sides], color);
}
}
}
void TDraw::drawRegPoly ( int x, int y, int rad, int sides, double rot, char color ) {
int polyX[sides], polyY[sides];
for( int i = 0; i < sides; ++i ) {
polyX[i] = x + rad*cos( ((float)i/sides)*(2*M_PI) + 3*M_PI/2 + rot);
polyY[i] = y + rad*sin( ((float)i/sides)*(2*M_PI) + 3*M_PI/2 + rot);
}
drawPolygon(polyX, polyY, sides, color);
}
void TDraw::drawRect ( int x, int y, int w, int h, char color ) {
int rectX[4], rectY[4];
rectX[0] = x; rectY[0] = y;
rectX[1] = x; rectY[1] = y + h;
rectX[2] = x + w; rectY[2] = y + h;
rectX[3] = x + w; rectY[3] = y;
drawPolygon( rectX, rectY, 4, color );
}
void TDraw::drawLine ( int x1, int y1, int x2, int y2, char color, char color2 ) {
int w = x2-x1, h = y2-y1;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
if( w < 0 ) dx1 = -1; else if( w > 0 ) dx1 = 1;
if( w < 0 ) dx2 = -1; else if( w > 0 ) dx2 = 1;
if( h < 0 ) dy1 = -1; else if( h > 0 ) dy1 = 1;
int longest = abs(w);
int shortest = abs(h);
if( longest <= shortest ) {
std::swap(longest, shortest);
if( h < 0 ) dy2 = -1; else if( h > 0 ) dy2 = 1;
dx2 = 0;
}
int num = longest/2;
if(color2 < 0) color2 = color;
int gradient = color;
for( int i = 0; i <= longest; ++i ) {
gradient = mapTo( i, 0, longest, color, color2 );
drawPixel( x1, y1, gradient);
num += shortest;
if( num >= longest ) {
num -= longest;
x1 += dx1;
y1 += dy1;
} else {
x1 += dx2;
y1 += dy2;
}
}
}
void TDraw::drawCircle ( int x, int y, int r, double precision, char color ) {
for( float a = 0; a < 2*M_PI; a+=precision ) {
drawPixel( x + r*cos(a), y + r*sin(a), color );
}
}
void TDraw::drawPixel ( int x, int y, char color ) {
if( x < 0 || x > width*SYM_WIDTH-1 || y < 0 || y > height*SYM_HEIGHT-1) return;
int id = x/SYM_WIDTH + (y/SYM_HEIGHT)*width;
#ifdef ASCII_MODE
if( y%2==0 ) {
screen[id] = ( screen[id]=='.' ? ':' : '\'' );
} else {
screen[id] = ( screen[id]=='\'' ? ':' : '.' );
}
#else
screen[id] |= PIXEL_MATRIX[y%SYM_HEIGHT][x%SYM_WIDTH];
#endif
color_data[id] = color;
}
int TDraw::GetScreenHeight() const {
return screen_height;
}
int TDraw::GetScreenWidth() const {
return screen_width;
}
void TDraw::Status1( char str[50] ) {
free(status1);
status1 = (char*)malloc(50*sizeof(char));
strcat(status1, str);
}
void TDraw::Status2( char str[50] ) {
free(status2);
status2 = (char*)malloc(50*sizeof(char));
strcat(status2, str);
}