Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/emu-ui/theme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ struct Metrics {
int16_t hint_h; ///< the button hints at the bottom
int16_t row_h; ///< a game or setting row
int16_t focus_w; ///< the focus ring
/// A backdrop's pattern tile repeats the pattern to at least this many
/// pixels a side (0: one period, a few pixels). LVGL draws a tiled image
/// one tile at a time, so a big screen covered in 6 px tiles costs tens of
/// thousands of tiny draws per redraw; 128 px tiles take a few dozen.
int16_t pattern_px;
bool compact; ///< a small screen: overlays fill it, covers are not scaled

/// The ESP32-S3-BOX's 320x240 (Montserrat 12 / 14 / 16)
Expand Down
6 changes: 4 additions & 2 deletions include/emu-ui/widgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ lv_obj_t *icon_create(lv_obj_t *parent, Shape shape, uint32_t accent, int h);
lv_obj_t *badge_create(lv_obj_t *parent, const ConsoleStyle &style, const char *name, int h, const lv_font_t *font);

/// The console's background tile (RGB565, made once per console and kept).
const lv_draw_buf_t *pattern_tile(const ConsoleStyle &style);
const lv_draw_buf_t *pattern_tile(const ConsoleStyle &style, int min_px = 0);

/// A console's background behind a screen or a tile: its color with its
/// pattern tiled over it. The first child of `parent`, filling it.
class Backdrop {
public:
void create(lv_obj_t *parent);
/// `pattern_px`: Metrics::pattern_px
void create(lv_obj_t *parent, int pattern_px = 0);
void set(const ConsoleStyle &style);
const ConsoleStyle *shown() const { return shown_; }

private:
lv_obj_t *parent_{nullptr};
lv_obj_t *image_{nullptr};
int pattern_px_{0};
const ConsoleStyle *shown_{nullptr};
};

Expand Down
4 changes: 2 additions & 2 deletions src/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void Launcher::build_ui() {
screen_ = lv_obj_create(nullptr);
lv_obj_remove_flag(screen_, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_style_bg_color(screen_, rgb(color::background), 0);
backdrop_.create(screen_);
backdrop_.create(screen_, m_.pattern_px);

// the status bar
bar_ = box_create(screen_);
Expand Down Expand Up @@ -628,7 +628,7 @@ void Launcher::open_picker() {
lv_obj_set_style_outline_width(tile, m_.focus_w, LV_STATE_CHECKED);
lv_obj_set_style_outline_pad(tile, 1, LV_STATE_CHECKED);
auto bd = std::make_unique<Backdrop>();
bd->create(tile);
bd->create(tile, m_.pattern_px);
bd->set(key.empty() ? console_style("other") : style);
picker_backdrops_.push_back(std::move(bd));
auto *stripe = box_create(tile);
Expand Down
2 changes: 1 addition & 1 deletion src/pause_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void PauseMenu::build_ui() {
screen_ = lv_obj_create(nullptr);
lv_obj_remove_flag(screen_, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_style_bg_color(screen_, rgb(color::background), 0);
backdrop_.create(screen_);
backdrop_.create(screen_, m_.pattern_px);

auto *bar = box_create(screen_);
const int bar_h = compact ? 20 : m_.bar_h;
Expand Down
2 changes: 2 additions & 0 deletions src/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Metrics Metrics::compact_320x240() {
.hint_h = 20,
.row_h = 22,
.focus_w = 2,
.pattern_px = 0,
.compact = true,
};
}
Expand All @@ -96,6 +97,7 @@ Metrics Metrics::large_1280x720() {
.hint_h = 56,
.row_h = 60,
.focus_w = 3,
.pattern_px = 64,
.compact = false,
};
}
Expand Down
34 changes: 22 additions & 12 deletions src/widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ uint32_t mix(uint32_t a, uint32_t b, int t) {
return ch(16) | ch(8) | ch(0);
}

lv_draw_buf_t *make_tile(const ConsoleStyle &s) {
lv_draw_buf_t *make_tile(const ConsoleStyle &s, int min_px) {
// (the marks are the secondary color, faint over the background, so text
// on top stays readable)
const uint16_t bg = rgb565(s.background);
Expand All @@ -167,11 +167,16 @@ lv_draw_buf_t *make_tile(const ConsoleStyle &s) {
case Pattern::PIXELS: w = h = 16; break;
case Pattern::NONE: w = h = 8; break;
}
auto *buf = lv_draw_buf_create(w, h, LV_COLOR_FORMAT_RGB565, LV_STRIDE_AUTO);
// the period, repeated to at least min_px a side
const int tw = min_px > w ? (min_px + w - 1) / w * w : w;
const int th = min_px > h ? (min_px + h - 1) / h * h : h;
auto *buf = lv_draw_buf_create(tw, th, LV_COLOR_FORMAT_RGB565, LV_STRIDE_AUTO);
if (!buf) return nullptr;
for (int y = 0; y < h; y++) {
auto *row = reinterpret_cast<uint16_t *>(buf->data + y * buf->header.stride);
for (int x = 0; x < w; x++) {
for (int ty = 0; ty < th; ty++) {
auto *row = reinterpret_cast<uint16_t *>(buf->data + ty * buf->header.stride);
const int y = ty % h;
for (int tx = 0; tx < tw; tx++) {
const int x = tx % w;
uint16_t c = bg;
switch (s.pattern) {
case Pattern::RIDGES: c = y < 3 ? mark : bg; break;
Expand All @@ -183,35 +188,40 @@ lv_draw_buf_t *make_tile(const ConsoleStyle &s) {
case Pattern::PIXELS: c = ((x / 8) ^ (y / 8)) & 1 ? rgb565(mix(s.background, s.secondary, 10)) : bg; break;
case Pattern::NONE: break;
}
row[x] = c;
row[tx] = c;
}
}
return buf;
}

} // namespace

const lv_draw_buf_t *pattern_tile(const ConsoleStyle &style) {
const lv_draw_buf_t *pattern_tile(const ConsoleStyle &style, int min_px) {
// a fixed table rather than a std::map: no heap node per console (on a
// board that keeps small allocations in internal RAM they add up); the
// styles are console_style()'s 14 static entries, the tiles LVGL buffers
struct Entry {
const ConsoleStyle *style;
int min_px;
lv_draw_buf_t *tile;
};
static Entry tiles[16];
for (auto &e : tiles) {
if (!e.style) e.style = &style;
if (e.style == &style) {
if (!e.tile) e.tile = make_tile(style);
if (!e.style) {
e.style = &style;
e.min_px = min_px;
}
if (e.style == &style && e.min_px == min_px) {
if (!e.tile) e.tile = make_tile(style, min_px);
return e.tile;
}
}
return nullptr; // (more styles than slots: the backdrop keeps its color)
}

void Backdrop::create(lv_obj_t *parent) {
void Backdrop::create(lv_obj_t *parent, int pattern_px) {
parent_ = parent;
pattern_px_ = pattern_px;
image_ = lv_image_create(parent);
lv_obj_remove_flag(image_, LV_OBJ_FLAG_CLICKABLE);
lv_obj_add_flag(image_, LV_OBJ_FLAG_IGNORE_LAYOUT);
Expand All @@ -230,7 +240,7 @@ void Backdrop::set(const ConsoleStyle &style) {
return;
}
lv_obj_remove_flag(image_, LV_OBJ_FLAG_HIDDEN);
lv_image_set_src(image_, pattern_tile(style));
lv_image_set_src(image_, pattern_tile(style, pattern_px_));
}

void HintBar::create(lv_obj_t *parent, const Metrics &m) {
Expand Down