forked from Legimet/nPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.cpp
191 lines (172 loc) · 6.04 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
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
// Screen routines for nPDF
// Copyright (C) 2014 Legimet
//
// This file is part of nPDF.
//
// nPDF is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// nPDF is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with nPDF. If not, see <http://www.gnu.org/licenses/>.
#include <algorithm>
#include <cstdlib>
#include <cstdint>
#include <libndls.h>
#include "Screen.hpp"
// Using double-buffering
namespace Screen {
uint8_t* const origBuf = static_cast<uint8_t*>(SCREEN_BASE_ADDRESS);
int curBuf = 0;
bool hasColors = false;
uint8_t *buf[2];
bool init() {
if (has_colors) {
hasColors = true;
}
buf[0] = new uint8_t[SCREEN_BYTES_SIZE+7]; // critor - screen buffer address MUST be divisible by 8
if (((int)buf[0])%8) buf[0] += 8-(((int)buf[0])%8); // critor
buf[1] = origBuf;
if (buf[0]) {
if (std::atexit(deinit)) {
deinit();
return false;
} else {
return true;
}
} else {
return false;
}
}
void deinit() {
delete[] buf[0];
buf[0] = nullptr;
SCREEN_BASE_ADDRESS = origBuf;
}
void switchBufs() {
SCREEN_BASE_ADDRESS = buf[curBuf];
curBuf ^= 1;
}
void setPixel(uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y) {
// On color models, each pixel is represented in 16-bit high color
// On classic models, each pixel is 4 bits grayscale, 0 is black and 15 is white
if (x < SCREEN_WIDTH && y < SCREEN_HEIGHT) {
unsigned int pos = y * SCREEN_WIDTH + x;
if (hasColors) {
(reinterpret_cast<volatile uint16_t*>(buf[curBuf]))[pos] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
} else if (pos % 2 == 0) {
buf[curBuf][pos / 2] = (buf[curBuf][pos / 2] & 0x0F) | (((30 * r + 59 * g + 11 * b) / 100) & 0xF0);
} else {
buf[curBuf][pos / 2] = (buf[curBuf][pos / 2] & 0xF0) | (((30 * r + 59 * g + 11 * b) / 100) >> 4);
}
}
}
void setPixel(uint8_t c, unsigned int x, unsigned int y) {
// On color models, each pixel is represented in 16-bit high color
// On classic models, each pixel is 4 bits grayscale, 0 is black and 15 is white
if (x < SCREEN_WIDTH && y < SCREEN_HEIGHT) {
unsigned int pos = y * SCREEN_WIDTH + x;
if (hasColors) {
(reinterpret_cast<volatile uint16_t*>(buf[curBuf]))[pos] = ((c >> 3) << 11) | ((c >> 2) << 5) | (c >> 3);
} else if (pos % 2 == 0) {
buf[curBuf][pos / 2] = (buf[curBuf][pos / 2] & 0x0F) | (c & 0xF0);
} else {
buf[curBuf][pos / 2] = (buf[curBuf][pos / 2] & 0xF0) | (c >> 4);
}
}
}
void showImgRGB(uint8_t *img, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1,
unsigned int w, unsigned int h, unsigned int wTotal) {
unsigned int pos;
for (unsigned int i = x1; i < x1 + w; i++) {
for (unsigned int j = y1; j < y1 + h; j++) {
pos = 3 * (wTotal * j + i);
setPixel(img[pos], img[pos + 1], img[pos + 2], x0 - x1 + i, y0 - y1 + j);
}
}
}
void showImgRGBA(uint8_t *img, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1,
unsigned int w, unsigned int h, unsigned int wTotal) {
unsigned int pos;
for (unsigned int i = x1; i < x1 + w; i++) {
for (unsigned int j = y1; j < y1 + h; j++) {
pos = 4 * (wTotal * j + i);
setPixel(img[pos], img[pos + 1], img[pos + 2], x0 - x1 + i, y0 - y1 + j);
}
}
}
void showImgGray(uint8_t *img, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1,
unsigned int w, unsigned int h, unsigned int wTotal) {
unsigned int pos;
for (unsigned int i = x1; i < x1 + w; i++) {
for (unsigned int j = y1; j < y1 + h; j++) {
pos = wTotal * j + i;
setPixel(img[pos], x0 - x1 + i, y0 - y1 + j);
}
}
}
void showImgGrayA(uint8_t *img, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1,
unsigned int w, unsigned int h, unsigned int wTotal) {
unsigned int pos;
for (unsigned int i = x1; i < x1 + w; i++) {
for (unsigned int j = y1; j < y1 + h; j++) {
pos = 2 * (wTotal * j + i);
setPixel(img[pos], x0 - x1 + i, y0 - y1 + j);
}
}
}
void fillScreen(uint8_t r, uint8_t g, uint8_t b) {
uint16_t color;
if (hasColors) {
color = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
} else {
color = (30 * r + 59 * g + 11 * b) / 100;
color = (color >> 4) * 0x1111;
}
std::fill(reinterpret_cast<volatile uint16_t*>(buf[curBuf]),
reinterpret_cast<volatile uint16_t*>(buf[curBuf] + SCREEN_BYTES_SIZE),
color);
}
void fillScreen(uint8_t c) {
uint16_t color;
if (hasColors) {
color = ((c >> 3) << 11) | ((c >> 2) << 5) | (c >> 3);
} else {
color = (c >> 4) * 0x1111;
}
std::fill(reinterpret_cast<volatile uint16_t*>(buf[curBuf]),
reinterpret_cast<volatile uint16_t*>(buf[curBuf] + SCREEN_BYTES_SIZE),
color);
}
void fillRect(uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int w,
unsigned int h) {
for (unsigned int i = x; i < x + w; i++) {
for (unsigned int j = y; j < y + h; j++) {
setPixel(r, g, b, i, j);
}
}
}
void fillRect(uint8_t c, unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
for (unsigned int i = x; i < x + w; i++) {
for (unsigned int j = y; j < y + h; j++) {
setPixel(c, i, j);
}
}
}
void drawVert(uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int h) {
for (unsigned int j = y; j < y + h; j++) {
setPixel(r, g, b, x, j);
}
}
void drawHoriz(uint8_t r, uint8_t g, uint8_t b, unsigned int x, unsigned int y, unsigned int w) {
for (unsigned int i = x; i < x + w; i++) {
setPixel(r, g, b, i, y);
}
}
}