-
Notifications
You must be signed in to change notification settings - Fork 3
/
GzDisplay.cpp
136 lines (123 loc) · 3.37 KB
/
GzDisplay.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
#include "stdafx.h"
#include "GzDisplay.h"
const short GzDisplay::MAXXRES(1024);
const short GzDisplay::MAXYRES(1024);
int GzNewFrameBuffer(char* & framebuffer, int width, int height)
{
if (width > GzDisplay::MAXXRES || width <= 0 || height > GzDisplay::MAXYRES || height <= 0)
{
return GZ_FAILURE;
}
// Allocate array space for Windows frame buffer.
// Within the part I know, the frame buffer will not be free-ed
framebuffer = new (std::nothrow) char [3*width*height];
// Nothrow version will return NULL if not able to allocate.
if (!framebuffer)
{
return GZ_FAILURE;
}
return GZ_SUCCESS;
}
int GzNewDisplay(GzDisplay* & display, int a_xRes, int a_yRes)
{
if (a_xRes > GzDisplay::MAXXRES || a_xRes <= 0 || a_yRes > GzDisplay::MAXYRES || a_yRes <= 0)
{
return GZ_FAILURE;
}
display = new (std::nothrow) GzDisplay(a_xRes, a_yRes);
// Nothrow version will return nullptr if not able to allocate.
if (!display || !(display->fbuf))
{
return GZ_FAILURE;
}
return GZ_SUCCESS;
}
GzDisplay::GzDisplay(int a_xRes, int a_yRes) :
xres(static_cast<short>(a_xRes)), yres(static_cast<short>(a_yRes))
{
this->fbuf = new (std::nothrow) GzPixel[xres * yres];
//if (!this->fbuf)
//{
//delete this;
//this = nullptr; // Not able to assign this to nullptr
//}
}
GzDisplay::GzDisplay() : GzDisplay(MAXXRES, MAXYRES)
{
}
GzDisplay::~GzDisplay()
{
delete[] this->fbuf;
}
int GzDisplay::index(int i, int j) const
{
return i + j * this->yres;
}
void GzDisplay::init(const GzColor &back)
{
this->bgColor = back;
//for (int j = 0; j < this->yres; ++j)
//{
//for (int i = 0; i < this->xres; ++i)
//{
//this->fbuf[index(i, j)].putColor(back);
//}
//}
}
void GzDisplay::putDisplay(int i, int j, const GzPixel &p)
{
if (i >= 0 && i < this->xres && j >= 0 && j < this->yres)
{
this->fbuf[index(i, j)] = p;
}
}
void GzDisplay::putDisplay(int i, int j, const GzColor &c)
{
if (i >= 0 && i < this->xres && j >= 0 && j < this->yres)
{
this->fbuf[index(i, j)].putColor(c);
}
}
int GzDisplay::getDisplay(int i, int j, GzPixel &get) const
{
if (i >= 0 && i < this->xres && j >= 0 && j < this->yres)
{
get = this->fbuf[index(i, j)];
return GZ_SUCCESS;
}
return GZ_FAILURE;
}
int GzDisplay::flush2File(FILE* outfile) const
{
int status(GZ_SUCCESS);
fprintf(outfile, "P6 %d %d 255\r", this->xres, this->yres);
for (int j = 0; j < this->yres; ++j)
{
for (int i = 0; i < this->xres; ++i)
{
GzPixel get;
status = status || this->getDisplay(i, j, get);
fprintf(outfile, "%c%c%c", (get.red) >> 4, (get.green) >> 4, (get.blue) >> 4);
}
}
return status;
}
int GzDisplay::flush2FrameBuffer(char* framebuffer) const
{
int status(GZ_SUCCESS);
for (int j = 0; j < this->yres; ++j)
{
for (int i = 0; i < this->xres; ++i)
{
GzPixel get;
status = status || this->getDisplay(i, j, get);
char *bufferPixel = framebuffer + 3 * index(i, j);
(*bufferPixel) = (get.blue) >> 4;
++bufferPixel;
(*bufferPixel) = (get.green) >> 4;
++bufferPixel;
(*bufferPixel) = (get.red) >> 4;
}
}
return status;
}