/
textgraphics.cpp
193 lines (180 loc) · 4.91 KB
/
textgraphics.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
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
#include <iostream>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <chrono>
#include <ios>
#include <fstream>
#include <stdio.h>
#include <unistd.h>
#include "textgraphics.hpp"
#include "pixel.hpp"
#include "imgcontainer.hpp"
//#### Special member functions ####
//#### Constructor
Textgrafs::Textgrafs(){
std::cout << std::unitbuf; //Turns off the buffer
std::ios_base::sync_with_stdio(false); //Turn off sync with in stream
struct winsize w; //Get size of window
ioctl(0, TIOCGWINSZ, &w);
rows_ = w.ws_row;
cols_ = w.ws_col*0.5;
std::vector<std::string> p;
std::string k = " ";
//Pixel pix(200);
p.resize(cols_,k);
grid.resize(rows_,p);
//for(int y = 0; y < grid.size(); ++y){
//for(int x = 0; x < grid[y].size(); ++x){
// add_pixel(pix, x, y);
//}
//}
//std::cout << grid.size() << std::endl;
//std::cout << grid[0].size() << std::endl;
timer_ = std::chrono::system_clock::now(); //First timepoint
}
//#### Member functions
void Textgrafs::paint(){
//Used for continuous update
//Refer to print for manual control
if(next_tick()){ //Limit framerate
print(); //Print out the current grid
//save_old_grid();
}
clear_grid(); //This empties the grid. You need to enter what you painted every frame
}
void Textgrafs::cursorpos(int px, int py){
++px; //escape is 1 base
++py;
std::cout << "\033[" << py << ";" << px << "H";
}
std::string Textgrafs::cursorpos_str(int px, int py){
++px;
++py;
std::string s = "\033[" + std::to_string(px) + ";" + std::to_string(py) + "H";
return s;
}
void Textgrafs::hide_cursor(){
std::cout << "\033[?25l";//TODO look up this code
}
void Textgrafs::show_cursor(){
std::cout << "\033[?25h";
}
void Textgrafs::clear_screen(){
std::cout << "\033[2J";
}
void Textgrafs::save_old_grid(){
old_grid = grid;
}
/*
void Textgrafs::add_image(std::vector<std::vector<std::string>> & img_ref, int px, int py){
for(int y = 0; y < img_ref.size(); ++y){
for(int x = 0; x < img_ref[y].size(); ++x){
Pixel p(img_ref[y][x]);
add_pixel(p, px+x, py+y);
}
}
}
*/
void Textgrafs::add_image(const std::vector<std::vector<Pixel>> & imgref, int px, int py){
for(int y = 0; y < imgref.size(); ++y){
for(int x = 0; x < imgref[y].size(); ++x){
add_pixel(imgref[y][x], px+x, py+y);
}
}
}
void Textgrafs::add_image(const Img_container & img, int px, int py){
add_image(img.get_img(), px, py);
}
void Textgrafs::add_rect(const Pixel & p, int px, int py, int sizex, int sizey){
for(int y = py; y < py+sizey; ++y){
for(int x = px; x < px+sizex; ++x){
add_pixel(p, x, y);
}
}
}
void Textgrafs::print(){
cursorpos(0,0);
hide_cursor();
std::string s;
for(int y = 0; y < grid.size(); ++y){
for(int x = 0; x < grid[y].size();++x){
s+= grid[y][x];
}
s+= "\033[0m\n";
}
s.pop_back();
std::cout << s;
show_cursor();
}
void Textgrafs::fill_grid(const Pixel & p){
for(int y = 0; y < rows_; ++y){
for(int x = 0; x < cols_; ++x){
add_pixel(p, x, y);
}
}
}
void Textgrafs::clear_grid(){
//Fills the grid with space
for(int y = 0; y < grid.size(); ++y){
for(int x = 0; y < grid[y].size();++x){
Pixel p;
add_pixel(p, x, y);
}
}
}
void Textgrafs::clear_grid_specific(int px, int py, int sizex, int sizey){
for(int y = py; y < py+sizey; ++y){
for(int x = px; x < px+sizex; ++x){
Pixel p;
add_pixel(p,x,y);
}
}
}
bool Textgrafs::next_tick(){
//Calculate next frame
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::system_clock::now()-timer_);
if(time_span.count() > time_between_frames_){
timer_ = std::chrono::system_clock::now();
return true;
}
return false;
}
void Textgrafs::add_pixel(const Pixel & p, int px, int py){
if(px < 0 || px >= cols_) return;
if(py < 0 || py >= rows_) return;
//std::cout << "py " << py << std::endl;
//std::cout << "px " << px << std::endl;
//std::cout << "y size " << grid.size()<<std::endl;
//std::cout << "x size " << grid[py].size() <<std::endl;
grid[py][px] = p.get_str();
}
void Textgrafs::add_border(const Pixel & p, int px, int py, int sizex, int sizey){
for(int y = py; y < py+sizey; ++y){
for(int x = px; x < px+sizex; ++x){
if(x == px || y == py || x == px+sizex-1 || y == py+sizey-1){
add_pixel(p, x, y);
}
}
}
}
void Textgrafs::add_gameobject(const Gameobject & g){
add_image(g.get_img(), g.px_, g.py_);
}
void Textgrafs::add_gameobjects(const std::vector<Gameobject> & g){
for(auto i = g.begin(); i != g.end() ; ++i){
add_gameobject(*i);
}
}
void Textgrafs::add_fill(const Pixel & p, int px, int py, int sizex, int sizey){
for(int y = py; y < py+sizey; ++y){
for(int x = px; x < px+sizex; ++x){
add_pixel(p, x, y);
}
}
}
void Textgrafs::add_fill(int px, int py, int sizex, int sizey){
Pixel p (" ");
add_fill(p, px, py, sizex, sizey);
}