-
Notifications
You must be signed in to change notification settings - Fork 72
/
qtframebuffer.cpp
87 lines (72 loc) · 2.5 KB
/
qtframebuffer.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
#include "qtframebuffer.h"
#include <array>
#include <cassert>
#include <QImage>
#include <QPainter>
#include <QGuiApplication>
#include <QScreen>
#include "core/debug.h"
#include "core/emu.h"
#include "core/lcd.h"
#include "core/misc.h"
#include "qtkeypadbridge.h"
QImage renderFramebuffer()
{
static std::array<uint16_t, 320 * 240> framebuffer;
lcd_cx_draw_frame(framebuffer.data());
QImage::Format format = QImage::Format_RGB16;
if(!emulate_cx)
{
format = QImage::Format_RGB444;
uint16_t *px = framebuffer.data();
for(unsigned int i = 0; i < 320*240; ++i)
{
uint8_t pix = *px & 0xF;
uint16_t n = (pix << 8) | (pix << 4) | pix;
*px = ~n & 0xFFF;
++px;
}
}
QImage image(reinterpret_cast<const uchar*>(framebuffer.data()), 320, 240, 320 * 2, format);
return image;
}
void paintFramebuffer(QPainter *p)
{
#ifdef IS_IOS_BUILD
// Apparently, this is needed (will be 2 on retina screens)
// TODO: actually make sure Android doesn't need that as well
static const double devicePixelRatio = ((QGuiApplication*)QCoreApplication::instance())->primaryScreen()->devicePixelRatio();
#else
// Has to be 1 on desktop, even on retina (tested on OS X 10.11 with one retina, one non-retina, and both ; same on Win VM)
static const double devicePixelRatio = 1;
#endif
QRect painterWindowScaled(p->window().topLeft(), p->window().size() / devicePixelRatio);
if(hdq1w.lcd_contrast == 0)
{
p->fillRect(painterWindowScaled, emulate_cx ? Qt::black : Qt::white);
p->setPen(emulate_cx ? Qt::white : Qt::black);
p->drawText(painterWindowScaled, Qt::AlignCenter, QObject::tr("LCD turned off"));
}
else
{
QImage image = renderFramebuffer().scaled(p->window().size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
image.setDevicePixelRatio(devicePixelRatio);
p->drawImage((p->window().width() - image.width()) / 2, (p->window().height() - image.height()) / 2, image);
}
if(in_debugger)
{
p->setCompositionMode(QPainter::CompositionMode_SourceOver);
p->fillRect(painterWindowScaled, QColor(30, 30, 30, 150));
p->setPen(Qt::white);
p->drawText(painterWindowScaled, Qt::AlignCenter, QObject::tr("In debugger"));
}
}
QMLFramebuffer::QMLFramebuffer(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
installEventFilter(&qt_keypad_bridge);
}
void QMLFramebuffer::paint(QPainter *p)
{
paintFramebuffer(p);
}