diff --git a/include/emu-ui/theme.hpp b/include/emu-ui/theme.hpp index 54f2f93..5382331 100644 --- a/include/emu-ui/theme.hpp +++ b/include/emu-ui/theme.hpp @@ -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) diff --git a/include/emu-ui/widgets.hpp b/include/emu-ui/widgets.hpp index 52a8f6c..131b7ef 100644 --- a/include/emu-ui/widgets.hpp +++ b/include/emu-ui/widgets.hpp @@ -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}; }; diff --git a/src/launcher.cpp b/src/launcher.cpp index dd87f1a..d6b0620 100644 --- a/src/launcher.cpp +++ b/src/launcher.cpp @@ -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_); @@ -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(); - 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); diff --git a/src/pause_menu.cpp b/src/pause_menu.cpp index d777b52..80dbf78 100644 --- a/src/pause_menu.cpp +++ b/src/pause_menu.cpp @@ -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; diff --git a/src/theme.cpp b/src/theme.cpp index 880a183..64f2aa4 100644 --- a/src/theme.cpp +++ b/src/theme.cpp @@ -77,6 +77,7 @@ Metrics Metrics::compact_320x240() { .hint_h = 20, .row_h = 22, .focus_w = 2, + .pattern_px = 0, .compact = true, }; } @@ -96,6 +97,7 @@ Metrics Metrics::large_1280x720() { .hint_h = 56, .row_h = 60, .focus_w = 3, + .pattern_px = 64, .compact = false, }; } diff --git a/src/widgets.cpp b/src/widgets.cpp index 4aa8018..46c7b8e 100644 --- a/src/widgets.cpp +++ b/src/widgets.cpp @@ -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); @@ -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(buf->data + y * buf->header.stride); - for (int x = 0; x < w; x++) { + for (int ty = 0; ty < th; ty++) { + auto *row = reinterpret_cast(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; @@ -183,7 +188,7 @@ 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; @@ -191,27 +196,32 @@ lv_draw_buf_t *make_tile(const ConsoleStyle &s) { } // 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); @@ -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) {