forked from Legimet/nPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.cpp
153 lines (134 loc) · 4.74 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
// Screen routines for nPDF
// Copyright (C) 2014-2016 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 <cstdint>
#include <libndls.h>
#include "Screen.hpp"
namespace Screen {
scr_type_t type;
unsigned int size;
uint8_t *screen;
bool init() {
type = lcd_type() == SCR_320x240_4 ? SCR_320x240_4 : SCR_320x240_565;
if (!lcd_init(type))
return false;
size = (SCREEN_WIDTH*SCREEN_HEIGHT/2) * (type == SCR_320x240_4 ? 1 : 4);
screen = new uint8_t[size];
return true;
}
void deinit() {
delete[] screen;
lcd_init(SCR_TYPE_INVALID);
}
void display() {
lcd_blit(screen, type);
}
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 (type == SCR_320x240_565) {
reinterpret_cast<uint16_t*>(screen)[pos] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
} else if (pos % 2 == 0) {
screen[pos / 2] = (screen[pos / 2] & 0x0F) | (((30 * r + 59 * g + 11 * b) / 100) & 0xF0);
} else {
screen[pos / 2] = (screen[pos / 2] & 0xF0) | (((30 * r + 59 * g + 11 * b) / 100) >> 4);
}
}
}
void setPixel(uint8_t c, unsigned int x, unsigned int y) {
setPixel(c, c, c, x, y);
}
// The RGBA and GrayA functions display pixmaps with premultiplied alpha. The alpha component is
// ignored since we aren't compositing
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 wImg) {
unsigned int pos;
for (unsigned int i = y1; i < y1 + h; i++) {
for (unsigned int j = x1; j < x1 + w; j++) {
pos = 3 * (wImg * i + j);
setPixel(img[pos], img[pos + 1], img[pos + 2], x0 - x1 + j, y0 - y1 + i);
}
}
}
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 wImg) {
unsigned int pos;
for (unsigned int i = y1; i < y1 + h; i++) {
for (unsigned int j = x1; j < x1 + w; j++) {
pos = 4 * (wImg * i + j);
setPixel(img[pos], img[pos + 1], img[pos + 2], x0 - x1 + j, y0 - y1 + i);
}
}
}
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 wImg) {
unsigned int pos;
for (unsigned int i = y1; i < y1 + h; i++) {
for (unsigned int j = x1; j < x1 + w; j++) {
pos = wImg * i + j;
setPixel(img[pos], x0 - x1 + j, y0 - y1 + i);
}
}
}
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 wImg) {
unsigned int pos;
for (unsigned int i = y1; i < y1 + h; i++) {
for (unsigned int j = x1; j < x1 + w; j++) {
pos = 2 * (wImg * i + j);
setPixel(img[pos], x0 - x1 + j, y0 - y1 + i);
}
}
}
void fillScreen(uint8_t r, uint8_t g, uint8_t b) {
uint16_t color;
if (type == SCR_320x240_565) {
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*>(screen), reinterpret_cast<volatile uint16_t*>(screen + size), color);
}
void fillScreen(uint8_t c) {
fillScreen(c, c, c);
}
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) {
fillRect(c, c, c, x, y, w, h);
}
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);
}
}
}