-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.cpp
executable file
·115 lines (96 loc) · 2.88 KB
/
Screen.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
#include "Screen.h"
Screen::Screen() {
system("reset");
frameBufferFileDescriptor = open("/dev/fb0",O_RDWR);
if (frameBufferFileDescriptor == -1){
perror("Error :");
printf("Error opening dev/fb");
exit(1);
}
initializeScreenInfo();
frameBuffer = (char*)mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, frameBufferFileDescriptor, 0);
screenSize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
width = vinfo.xres;
height = vinfo.yres;
pseudoFrame = new char[screenSize];
memset(pseudoFrame,0,screenSize);
}
void Screen::initializeScreenInfo() {
if (ioctl (frameBufferFileDescriptor, FBIOGET_VSCREENINFO, &vinfo)) {
perror("Error ");
exit(3);
}
if (ioctl (frameBufferFileDescriptor, FBIOGET_FSCREENINFO, &finfo)) {
perror("Error ");
exit(2);
}
}
void Screen::drawPixel(Pixel pixel) {
Point position = (pixel.getPosition());
if (position.x>= width || position.x < 0 || position.y >=height || position.y <0 )
return;
int location = (position.x+vinfo.xoffset) * (vinfo.bits_per_pixel/8)
+ (position.y+vinfo.yoffset) * finfo.line_length;
Color color = (pixel.getColor());
*(pseudoFrame+location) = color.blue;
*(pseudoFrame+location+1) = color.green;
*(pseudoFrame+location+2) = color.red;
*(pseudoFrame+location+3) = color.alpha;
}
void Screen::draw(Drawable *drawable) {
vector<Pixel> pixels = drawable->getPixels();
for (int i=0;i<pixels.size();i++) {
drawPixel(pixels[i]);
}
}
Screen::~Screen() {
munmap(frameBuffer,screenSize);
delete pseudoFrame;
close(frameBufferFileDescriptor);
}
void Screen::endBatch() {
memcpy ( frameBuffer, pseudoFrame, screenSize );
}
void Screen::draw(Drawable *drawable,bool isflip){
if(!isflip){
draw(drawable);
}
else{
vector<Pixel> result;
//Hitung sumbu vertikal
vector<Pixel> group_of_point=drawable->getPixels();
vector<Pixel>::iterator it;
int vertical_axis=0;
int numb=0;
for(it=group_of_point.begin();it!=group_of_point.end();it++){
vertical_axis+=(it->getPosition()).x;numb++;
}
vertical_axis/=numb;
//transformasi
for(it=group_of_point.begin();it!=group_of_point.end();it++){
int temp=(it->getPosition()).x;
temp=2*vertical_axis-temp;
Point pt(temp,(it->getPosition()).y);
Pixel px(pt,it->getColor());
result.push_back(px);
}
for(it=result.begin();it!=result.end();it++)
drawPixel(*it);
}
}
void Screen::draw(Drawable *drawable,float scale){
vector<Pixel> explosion_coordinate=drawable->getPixels();
vector<Pixel>::iterator it;
vector<Pixel> result;
int x_center=0,ycenter=0;
int numb_of_pixels=0;
for(it=explosion_coordinate.begin();it!=explosion_coordinate.end();it++){
Point point((int)(scale*(it->getPosition().x)),(int)(scale*(it->getPosition().y)));
Pixel pixel(point,it->getColor());
result.push_back(pixel);
}
for(it=result.begin();it!=result.end();it++) drawPixel(*it);
}
void Screen::beginBatch() {
memset(pseudoFrame,0,screenSize);
}