// Run-time sprites of the new UI: the original GBA sprites that depend on the species or the item // (Pokémon icons and front sprites, item icons, footprints), decoded from the game's own tables // into 64x64 cells of the UI texture (below the fixed atlas, UI_DYN_Y). // // Threads (docs/THREADS.md): // - Core 1 owns the cache (which key is in which cell, when each cell was last drawn). On a miss // it decodes the sprite into a staging slot of a single-producer / single-consumer ring and // publishes it with a release store, before the draw list that uses it is published. // - Core 0 (Ui_Render) consumes the ring with the texture bound and writes each staging slot // straight into its cell (vglGetTexDataPointer, as the voxel billboards do), then draws. // A cell is only reused when no list drawn in the last UI_SPR_IDLE_TICKS ticks used it, so the // GPU never reads a cell while it is rewritten, and vitaGL never copies the texture. #include #include "ui_internal.h" #include "global.h" #include "data.h" #include "decompress.h" #include "item_icon.h" #include "pokemon_icon.h" #include "constants/species.h" #include "constants/trainers.h" #ifdef PLATFORM_VITA #include #else #include #endif extern const u8 *const gMonFootprintTable[]; extern void PokeStorage_WallpaperForUi(u8 wallpaperId, const u32 **tiles, const u32 **tilemap, u16 *pal32, const u32 **iconTiles); extern void Mail_StationeryForUi(u8 mailType, const u32 **tiles, const u32 **tilemap, u16 *palette); extern bool8 Decoration_PictureForUi(u8 decor, u8 *tiles, u16 *palette, u8 *w, u8 *h); extern void MapNamePopUp_GfxForUi(u8 theme, bool8 underwater, const u8 **inner, const u8 **outline, const u16 **palette); static inline uint16_t To5551(uint16_t bgr); #define LZ77_MAGIC 0x10u #define UI_WALL_ICON_OFFSET 0x800u #define UI_MON_PIC_MAX_FRAMES 4 // Castform and 6 other front sheets in gMonStillFrontPicTable store 4 64x64 frames (8192 B) // Decompresses an LZ77 block only when its header is valid and its uncompressed size fits in dstCap. static bool UnpackLz(const u32 *src, void *dst, uint32_t dstCap) { if (src == NULL || dst == NULL) return false; const uint32_t header = src[0]; if ((header & 0xFFu) != LZ77_MAGIC) return false; const uint32_t size = header >> 8; if (size == 0u || size > dstCap) return false; LZ77UnCompWram(src, dst); return true; } // PC box wallpaper (160 x 144), composed once as DrawWallpaper puts it on BG2 and cut into 3 x 3 // cells. Tilemap entries use palette 1 (frame) and 2 (background); colour 0 is transparent. #define WALL_W 160 #define WALL_H 144 static uint16_t sWall[WALL_W * WALL_H]; static int32_t sWallId = -1; static void ComposeWallpaper(int32_t id) { static uint8_t tiles[256 * 32]; static uint16_t tmap[32 * 32]; static uint16_t pal32[32]; const u32 *tilesLz = NULL, *mapLz = NULL, *iconLz = NULL; memset(tiles, 0, sizeof(tiles)); memset(tmap, 0, sizeof(tmap)); PokeStorage_WallpaperForUi((u8)id, &tilesLz, &mapLz, pal32, &iconLz); if (!UnpackLz(tilesLz, tiles, sizeof(tiles)) || !UnpackLz(mapLz, tmap, sizeof(tmap))) return; if (iconLz != NULL && !UnpackLz(iconLz, tiles + UI_WALL_ICON_OFFSET, sizeof(tiles) - UI_WALL_ICON_OFFSET)) return; uint16_t pal[2][16]; for (int32_t p = 0; p < 2; p++) for (int32_t i = 0; i < 16; i++) pal[p][i] = i ? To5551(pal32[p * 16 + i]) : 0; memset(sWall, 0, sizeof(sWall)); for (int32_t t = 0; t < 20 * 18; t++) { const uint16_t e = tmap[t]; const int32_t p = (e >> 12) - 1; if (e == 0 || p < 0 || p > 1) continue; const uint8_t *tile = &tiles[(e & 0x3FF) * 32]; const bool hf = (e >> 10) & 1, vf = (e >> 11) & 1; for (int32_t y = 0; y < 8; y++) for (int32_t x = 0; x < 8; x++) { const int32_t sx = hf ? 7 - x : x, sy = vf ? 7 - y : y; const uint8_t b = tile[sy * 4 + sx / 2]; const uint8_t v = (sx & 1) ? (b >> 4) : (b & 15); if (v) sWall[((t / 20) * 8 + y) * WALL_W + (t % 20) * 8 + x] = pal[p][v]; } } sWallId = id; } // Mail stationery (240 x 160), composed as ReadMail draws it: BG2 is tile 1 everywhere, BG1 the // mail's tilemap over it (32 entries a row), palette 0 with the player's colours (Mail_StationeryForUi) #define MAIL_W 240 #define MAIL_H 160 static uint16_t sMail[MAIL_W * MAIL_H]; static int32_t sMailKey = -1; static void PutMailTile(const uint8_t *tiles, uint16_t tileCount, uint16_t entry, int32_t tx, int32_t ty, const uint16_t *pal) { const uint16_t t = entry & 0x3FF; if (t >= tileCount) return; const bool hf = (entry >> 10) & 1, vf = (entry >> 11) & 1; const uint8_t *tile = &tiles[t * 32]; for (int32_t y = 0; y < 8; y++) for (int32_t x = 0; x < 8; x++) { const int32_t sx = hf ? 7 - x : x, sy = vf ? 7 - y : y; const uint8_t b = tile[sy * 4 + sx / 2], v = (sx & 1) ? (b >> 4) : (b & 15); if (v) sMail[(ty * 8 + y) * MAIL_W + tx * 8 + x] = pal[v]; } } static void ComposeMail(int32_t mailType) { static uint8_t tiles[0x2000]; static uint16_t tmap[32 * 32]; const u32 *tilesLz = NULL, *mapLz = NULL; uint16_t bgr[16], pal[16]; Mail_StationeryForUi((u8)mailType, &tilesLz, &mapLz, bgr); if (!UnpackLz(tilesLz, tiles, sizeof(tiles)) || !UnpackLz(mapLz, tmap, sizeof(tmap))) return; const uint32_t tilesSize = tilesLz[0] >> 8; for (int32_t i = 0; i < 16; i++) pal[i] = To5551(bgr[i]); const uint16_t backdrop = pal[0]; for (int32_t i = 0; i < MAIL_W * MAIL_H; i++) sMail[i] = backdrop; const uint16_t count = (uint16_t)(tilesSize / 32); for (int32_t ty = 0; ty < MAIL_H / 8; ty++) for (int32_t tx = 0; tx < MAIL_W / 8; tx++) PutMailTile(tiles, count, 1, tx, ty, pal); // BG2 for (int32_t ty = 0; ty < MAIL_H / 8; ty++) for (int32_t tx = 0; tx < MAIL_W / 8; tx++) PutMailTile(tiles, count, tmap[ty * 32 + tx], tx, ty, pal); // BG1 sMailKey = mailType; } // Decoration pictures: their size, known once decoded (Core 1) static uint8_t sDecorW[256], sDecorH[256]; // Zone sign plate: the GBA map name popup (96 x 40), composed as DrawMapNamePopUpFrame lays it // out: the outline's 30 tiles round the 10 x 3 tile inner picture, colour 0 transparent. #define POPUP_W 96 #define POPUP_H 40 static uint16_t sPopup[POPUP_W * POPUP_H]; static int32_t sPopupKey = -1; static void ComposePopup(int32_t key) { const u8 *inner, *outline; const u16 *bgr; MapNamePopUp_GfxForUi((u8)(key >> 1), (bool8)(key & 1), &inner, &outline, &bgr); uint16_t pal[16]; pal[0] = 0; for (int32_t i = 1; i < 16; i++) pal[i] = To5551(bgr[i]); memset(sPopup, 0, sizeof(sPopup)); // Where each outline tile goes (tile units): 12 along the top, the sides, 12 along the bottom for (int32_t n = 0; n < 30; n++) { int32_t tx, ty; if (n < 12) { tx = n; ty = 0; } else if (n < 18) { tx = (n & 1) ? 11 : 0; ty = 1 + (n - 12) / 2; } else { tx = n - 18; ty = 4; } const uint8_t *tile = &outline[n * 32]; for (int32_t y = 0; y < 8; y++) for (int32_t x = 0; x < 8; x++) { const uint8_t b = tile[y * 4 + x / 2], v = (x & 1) ? (b >> 4) : (b & 15); if (v) sPopup[(ty * 8 + y) * POPUP_W + tx * 8 + x] = pal[v]; } } for (int32_t n = 0; n < 30; n++) { const uint8_t *tile = &inner[n * 32]; const int32_t tx = 1 + n % 10, ty = 1 + n / 10; for (int32_t y = 0; y < 8; y++) for (int32_t x = 0; x < 8; x++) { const uint8_t b = tile[y * 4 + x / 2], v = (x & 1) ? (b >> 4) : (b & 15); if (v) sPopup[(ty * 8 + y) * POPUP_W + tx * 8 + x] = pal[v]; } } sPopupKey = key; } #define UI_SPR_RING 64 // staging slots (64x64 RGBA5551 = 8 KB each) #define UI_SPR_DECODES_PER_TICK 16 // bounds the Core 1 cost of opening a full screen (allows full 9-cell wallpaper + party in 1 tick) #define UI_SPR_IDLE_TICKS 4 // a cell is reusable after this many ticks without being drawn (triple-buffered draw lists) enum { KIND_MON_ICON = 1, KIND_MON_FRONT, KIND_MON_FRONT_SHINY, KIND_ITEM, KIND_FOOTPRINT, KIND_TRAINER, KIND_WALLPAPER, KIND_POPUP, KIND_DECOR, KIND_MAIL }; #define KEY(kind, id) (((uint32_t)(kind) << 16) | (uint32_t)(id)) // ---------- Core 1: cache ---------- static uint32_t sCellKey[UI_CELL_COUNT]; // 0 = free static uint32_t sCellUsed[UI_CELL_COUNT]; // tick of the last list that drew it static uint32_t sCellUpload[UI_CELL_COUNT]; // ring head index (sHead + 1) of the cell's latest upload static uint32_t sTick; static uint32_t sDecodes, sDecodesLast; // ---------- Ring (Core 1 writes head, Core 0 writes tail) ---------- typedef struct { uint16_t cell; uint16_t pixels[UI_CELL * UI_CELL]; } UiSprUpload; static UiSprUpload sRing[UI_SPR_RING]; static uint32_t sHead, sTail; // free-running counters // GBA BGR555 palette entry -> RGBA5551 (index 0 of a sprite palette is transparent) static inline uint16_t To5551(uint16_t bgr) { const uint16_t r = bgr & 31, g = (bgr >> 5) & 31, b = (bgr >> 10) & 31; return (uint16_t)((r << 11) | (g << 6) | (b << 1) | 1); } // 4bpp GBA tiles (row-major, 32 bytes each) -> pixels of a staging cell at (x0, y0) static void Tiles4bpp(uint16_t *dst, int32_t x0, int32_t y0, const uint8_t *tiles, int32_t tilesW, int32_t tilesH, const uint16_t *bgrPal) { if (x0 < 0 || y0 < 0 || tilesW <= 0 || tilesH <= 0 || x0 + tilesW * 8 > UI_CELL || y0 + tilesH * 8 > UI_CELL) return; uint16_t pal[16]; pal[0] = 0; for (int32_t i = 1; i < 16; i++) pal[i] = To5551(bgrPal[i]); for (int32_t ty = 0; ty < tilesH; ty++) for (int32_t tx = 0; tx < tilesW; tx++) { const uint8_t *tile = &tiles[(ty * tilesW + tx) * 32]; for (int32_t py = 0; py < 8; py++) for (int32_t px = 0; px < 8; px += 2) { const uint8_t b = tile[py * 4 + px / 2]; uint16_t *row = &dst[(y0 + ty * 8 + py) * UI_CELL + x0 + tx * 8 + px]; row[0] = pal[b & 15]; row[1] = pal[b >> 4]; } } } // Decodes a sprite into a staging cell. Runs on Core 1 (the game thread), reading ROM tables. static bool Decode(uint32_t key, uint16_t *dst) { const uint32_t kind = key >> 16; const uint16_t id = (uint16_t)key; memset(dst, 0, UI_CELL * UI_CELL * sizeof(uint16_t)); switch (kind) { case KIND_MON_ICON: { if (id == SPECIES_NONE || id > SPECIES_EGG) return false; // 32x64: frame 0 at the top, frame 1 below (the two-frame party bounce) const uint8_t *tiles = gMonIconTable[id]; const uint16_t *pal = GetValidMonIconPalettePtr(id); if (tiles == NULL || pal == NULL) return false; Tiles4bpp(dst, 0, 0, tiles, 4, 4, pal); Tiles4bpp(dst, 0, 32, tiles + 512, 4, 4, pal); return true; } case KIND_MON_FRONT: case KIND_MON_FRONT_SHINY: { if (id == SPECIES_NONE || id > SPECIES_EGG) return false; static uint8_t tiles[(UI_CELL * UI_CELL / 2) * UI_MON_PIC_MAX_FRAMES]; static uint16_t pal[16 * 16]; const u32 *picLz = gMonStillFrontPicTable[id].data; const u32 *palLz = (kind == KIND_MON_FRONT_SHINY ? gMonShinyPaletteTable : gMonPaletteTable)[id].data; if (!UnpackLz(picLz, tiles, sizeof(tiles)) || !UnpackLz(palLz, pal, sizeof(pal))) return false; Tiles4bpp(dst, 0, 0, tiles, 8, 8, pal); return true; } case KIND_ITEM: { static uint8_t tiles[512]; static uint16_t pal[16]; const u32 *pic = (const u32 *)GetItemIconPicOrPalette(id, 0); const u32 *palGfx = (const u32 *)GetItemIconPicOrPalette(id, 1); if (!UnpackLz(pic, tiles, sizeof(tiles)) || !UnpackLz(palGfx, pal, sizeof(pal))) return false; Tiles4bpp(dst, 0, 0, tiles, 3, 3, pal); return true; } case KIND_TRAINER: { static uint8_t tiles[64 * 64 / 2]; static uint16_t pal[16]; if (id > TRAINER_PIC_RS_MAY || !UnpackLz(gTrainerFrontPicTable[id].data, tiles, sizeof(tiles)) || !UnpackLz(gTrainerFrontPicPaletteTable[id].data, pal, sizeof(pal))) return false; Tiles4bpp(dst, 0, 0, tiles, 8, 8, pal); return true; } case KIND_FOOTPRINT: { if (id == SPECIES_NONE || id >= NUM_SPECIES) return false; // 16x16, 1bpp in 2x2 tiles of 8 bytes (bit 0 = leftmost pixel); white, tinted when drawn const uint8_t *fp = gMonFootprintTable[id]; if (fp == NULL) return false; for (int32_t t = 0; t < 4; t++) for (int32_t py = 0; py < 8; py++) for (int32_t px = 0; px < 8; px++) if ((fp[t * 8 + py] >> px) & 1) dst[((t / 2) * 8 + py) * UI_CELL + (t % 2) * 8 + px] = 0xFFFF; return true; } case KIND_WALLPAPER: { // id = wallpaper * 16 + part (3 x 3 parts of 64 x 64) const int32_t wid = id >> 4, part = id & 15; if (wid != sWallId) ComposeWallpaper(wid); if (wid != sWallId) return false; const int32_t x0 = (part % 3) * UI_CELL, y0 = (part / 3) * UI_CELL; for (int32_t y = 0; y < UI_CELL && y0 + y < WALL_H; y++) for (int32_t x = 0; x < UI_CELL && x0 + x < WALL_W; x++) dst[y * UI_CELL + x] = sWall[(y0 + y) * WALL_W + x0 + x]; return true; } case KIND_DECOR: { // A decoration's list picture (Decoration_PictureForUi), up to 64 x 64, in 1D tile order static uint8_t tiles[0x800]; static uint16_t pal[16]; uint8_t w = 0, h = 0; if (!Decoration_PictureForUi((u8)id, tiles, pal, &w, &h) || w == 0 || h == 0 || w > UI_CELL || h > UI_CELL) return false; Tiles4bpp(dst, 0, 0, tiles, w / 8, h / 8, pal); sDecorW[(uint8_t)id] = w; sDecorH[(uint8_t)id] = h; return true; } case KIND_MAIL: { // id = mail type * 16 + part (4 x 3 parts of 64 x 64) const int32_t mt = id >> 4, part = id & 15; if (mt != sMailKey) ComposeMail(mt); if (mt != sMailKey) return false; const int32_t x0 = (part % 4) * UI_CELL, y0 = (part / 4) * UI_CELL; for (int32_t y = 0; y < UI_CELL && y0 + y < MAIL_H; y++) for (int32_t x = 0; x < UI_CELL && x0 + x < MAIL_W; x++) dst[y * UI_CELL + x] = sMail[(y0 + y) * MAIL_W + x0 + x]; return true; } case KIND_POPUP: { // id = popup key (theme * 2 + underwater palette) * 2 + part (x 0-63, x 64-95) const int32_t pk = id >> 1, part = id & 1; if (pk != sPopupKey) ComposePopup(pk); const int32_t x0 = part * UI_CELL; for (int32_t y = 0; y < POPUP_H; y++) for (int32_t x = 0; x < UI_CELL && x0 + x < POPUP_W; x++) dst[y * UI_CELL + x] = sPopup[y * POPUP_W + x0 + x]; return true; } default: return false; } } // Cell holding `key`, decoding it on a miss; -1 when it cannot be shown this tick. static int32_t Lookup(uint32_t key) { const uint32_t tail = __atomic_load_n(&sTail, __ATOMIC_ACQUIRE); int32_t freeCell = -1, oldest = -1; for (int32_t c = 0; c < UI_CELL_COUNT; c++) { if (sCellKey[c] == key) { sCellUsed[c] = sTick; return c; } if (sCellKey[c] == 0) { if (freeCell < 0) freeCell = c; } else if ((int32_t)(tail - sCellUpload[c]) >= 0 && (oldest < 0 || sCellUsed[c] < sCellUsed[oldest])) oldest = c; } // A free cell, else the least recently drawn one once Core 0 has flushed it and no recent list uses it const int32_t victim = freeCell >= 0 ? freeCell : (oldest >= 0 && sTick - sCellUsed[oldest] >= UI_SPR_IDLE_TICKS) ? oldest : -1; if (victim < 0 || sDecodes >= UI_SPR_DECODES_PER_TICK) return -1; if (sHead - tail >= UI_SPR_RING) return -1; // Core 0 has not drawn for a while: wait UiSprUpload *up = &sRing[sHead % UI_SPR_RING]; sDecodes++; if (!Decode(key, up->pixels)) return -1; up->cell = (uint16_t)victim; const uint32_t nextHead = sHead + 1; __atomic_store_n(&sHead, nextHead, __ATOMIC_RELEASE); sCellKey[victim] = key; sCellUsed[victim] = sTick; sCellUpload[victim] = nextHead; return victim; } static bool DrawCell(uint32_t key, int32_t sx, int32_t sy, int32_t sw, int32_t sh, int32_t x, int32_t y, int32_t scale, uint32_t rgba) { const int32_t c = Lookup(key); if (c < 0) return false; if (scale < 1) scale = 1; const uint32_t u = (uint32_t)(c % UI_CELL_COLS) * UI_CELL + sx, v = UI_DYN_Y + (uint32_t)(c / UI_CELL_COLS) * UI_CELL + sy; UiDraw_Quad(x, y, sw * scale, sh * scale, u, v, u + sw, v + sh, rgba); return true; } bool UiSpr_MonIcon(uint16_t species, uint8_t frame, int32_t x, int32_t y, int32_t scale) { if (species == SPECIES_NONE || species > SPECIES_EGG) return false; return DrawCell(KEY(KIND_MON_ICON, species), 0, (frame & 1) ? 32 : 0, 32, 32, x, y, scale, 0xFFFFFFFFu); } bool UiSpr_MonFront(uint16_t species, bool shiny, int32_t x, int32_t y, int32_t scale) { if (species == SPECIES_NONE || species > SPECIES_EGG) return false; return DrawCell(KEY(shiny ? KIND_MON_FRONT_SHINY : KIND_MON_FRONT, species), 0, 0, 64, 64, x, y, scale, 0xFFFFFFFFu); } bool UiSpr_ItemIcon(uint16_t itemId, int32_t x, int32_t y, int32_t scale) { if (itemId == 0) return false; return DrawCell(KEY(KIND_ITEM, itemId), 0, 0, 24, 24, x, y, scale, 0xFFFFFFFFu); } bool UiSpr_Trainer(uint16_t trainerPic, int32_t x, int32_t y, int32_t scale) { return DrawCell(KEY(KIND_TRAINER, trainerPic), 0, 0, 64, 64, x, y, scale, 0xFFFFFFFFu); } // Sprite drawn with a colour multiply (silhouettes, unearned badges) bool UiSpr_MonFrontTint(uint16_t species, int32_t x, int32_t y, int32_t scale, uint32_t rgba) { if (species == SPECIES_NONE || species > SPECIES_EGG) return false; return DrawCell(KEY(KIND_MON_FRONT, species), 0, 0, 64, 64, x, y, scale, rgba); } bool UiSpr_MonFrontSized(uint16_t species, int32_t x, int32_t y, int32_t w, int32_t h) { if (species == SPECIES_NONE || species > SPECIES_EGG) return false; const int32_t c = Lookup(KEY(KIND_MON_FRONT, species)); if (c < 0) return false; const uint32_t u = (uint32_t)(c % UI_CELL_COLS) * UI_CELL, v = UI_DYN_Y + (uint32_t)(c / UI_CELL_COLS) * UI_CELL; UiDraw_Quad(x, y, w, h, u, v, u + 64, v + 64, 0xFFFFFFFFu); return true; } bool UiSpr_TrainerSized(uint16_t pic, int32_t x, int32_t y, int32_t w, int32_t h) { const int32_t c = Lookup(KEY(KIND_TRAINER, pic)); if (c < 0) return false; const uint32_t u = (uint32_t)(c % UI_CELL_COLS) * UI_CELL, v = UI_DYN_Y + (uint32_t)(c / UI_CELL_COLS) * UI_CELL; UiDraw_Quad(x, y, w, h, u, v, u + 64, v + 64, 0xFFFFFFFFu); return true; } // PC box wallpaper at (x, y), 160 x 144 times scale; false while a part is still on its way bool UiSpr_Wallpaper(uint8_t wallpaperId, int32_t x, int32_t y, int32_t scale) { bool all = true; for (int32_t part = 0; part < 9; part++) { const int32_t px = (part % 3) * UI_CELL, py = (part / 3) * UI_CELL; const int32_t w = WALL_W - px < UI_CELL ? WALL_W - px : UI_CELL, h = WALL_H - py < UI_CELL ? WALL_H - py : UI_CELL; all &= DrawCell(KEY(KIND_WALLPAPER, ((uint32_t)wallpaperId << 4) | (uint32_t)part), 0, 0, w, h, x + px * scale, y + py * scale, scale, 0xFFFFFFFFu); } return all; } // Columns sx .. sx + sw of the popup picture (never across x = 64) at x2, full height static bool PopupColumns(uint32_t pk, int32_t sx, int32_t sw, int32_t x, int32_t y) { const int32_t part = sx >= UI_CELL ? 1 : 0; return DrawCell(KEY(KIND_POPUP, (pk << 1) | (uint32_t)part), sx - part * UI_CELL, 0, sw, POPUP_H, x, y, 2, 0xFFFFFFFFu); } // Zone sign plate: the GBA map name popup of `theme` at x2, w wide (>= 64) and 80 tall. Its // 8 px ends as they are, its middle (80 px) repeated to fill, the last copy cut. bool UiSpr_ZonePlate(uint8_t theme, bool underwater, int32_t x, int32_t y, int32_t w) { const uint32_t pk = (uint32_t)theme * 2u + (underwater ? 1u : 0u); bool all = PopupColumns(pk, 0, 8, x, y); all &= PopupColumns(pk, POPUP_W - 8, 8, x + w - 16, y); UiDraw_PushClip(x + 16, y, w - 32, POPUP_H * 2); for (int32_t mx = x + 16; mx < x + w - 16; mx += 160) { all &= PopupColumns(pk, 8, UI_CELL - 8, mx, y); all &= PopupColumns(pk, UI_CELL, POPUP_W - 8 - UI_CELL, mx + (UI_CELL - 8) * 2, y); } UiDraw_PopClip(); return all; } // A decoration's picture centred in the box (bx, by, box x box): at 2x if it fits, else 1x, else // half size (the 64 px ones in a small box); false while it is on its way bool UiSpr_Decoration(uint16_t decor, int32_t bx, int32_t by, int32_t box) { const uint32_t key = KEY(KIND_DECOR, decor); const int32_t c = Lookup(key); if (c < 0 || decor > 255 || sDecorW[decor] == 0) return false; const int32_t w = sDecorW[decor], h = sDecorH[decor]; const int32_t big = w > h ? w : h; int32_t dw = w, dh = h; if (big * 2 <= box) { dw = w * 2; dh = h * 2; } else if (big > box) { dw = w / 2; dh = h / 2; } const uint32_t u = (uint32_t)(c % UI_CELL_COLS) * UI_CELL, v = UI_DYN_Y + (uint32_t)(c / UI_CELL_COLS) * UI_CELL; UiDraw_Quad(bx + (box - dw) / 2, by + (box - dh) / 2, dw, dh, u, v, u + w, v + h, 0xFFFFFFFFu); return true; } // A mail's stationery at (x, y), 240 x 160 times scale; false while a part is on its way bool UiSpr_MailStationery(uint8_t mailType, int32_t x, int32_t y, int32_t scale) { bool all = true; for (int32_t part = 0; part < 12; part++) { const int32_t px = (part % 4) * UI_CELL, py = (part / 4) * UI_CELL; const int32_t w = MAIL_W - px < UI_CELL ? MAIL_W - px : UI_CELL, h = MAIL_H - py < UI_CELL ? MAIL_H - py : UI_CELL; all &= DrawCell(KEY(KIND_MAIL, ((uint32_t)mailType << 4) | (uint32_t)part), 0, 0, w, h, x + px * scale, y + py * scale, scale, 0xFFFFFFFFu); } return all; } bool UiSpr_Footprint(uint16_t species, int32_t x, int32_t y, int32_t scale, uint32_t rgba) { if (species == SPECIES_NONE || species >= NUM_SPECIES) return false; return DrawCell(KEY(KIND_FOOTPRINT, species), 0, 0, 16, 16, x, y, scale, rgba); } void UiSpr_BeginTick(void) { sTick++; sDecodesLast = sDecodes; sDecodes = 0; } uint32_t UiSpr_DecodesLastTick(void) { return sDecodesLast; } // ---------- Core 0 ---------- void UiSpr_FlushUploads(void) { const uint32_t head = __atomic_load_n(&sHead, __ATOMIC_ACQUIRE); uint32_t tail = sTail; if (tail == head) return; #ifdef PLATFORM_VITA uint32_t *const texels = (uint32_t *)vglGetTexDataPointer(GL_TEXTURE_2D); #else uint32_t *const texels = NULL; #endif for (; tail != head; tail++) { const UiSprUpload *up = &sRing[tail % UI_SPR_RING]; if (up->cell >= UI_CELL_COUNT) continue; const uint32_t cx = (uint32_t)(up->cell % UI_CELL_COLS) * UI_CELL; const uint32_t cy = UI_DYN_Y + (uint32_t)(up->cell / UI_CELL_COLS) * UI_CELL; if (texels != NULL) { for (uint32_t r = 0; r < UI_CELL; r++) { uint32_t *dst = &texels[(cy + r) * UI_TEX_W + cx]; const uint16_t *src = &up->pixels[r * UI_CELL]; for (uint32_t c = 0; c < UI_CELL; c++) dst[c] = Ui_UnpackTexel(src[c]); } } else { static uint32_t cellRgba[UI_CELL * UI_CELL]; for (uint32_t i = 0; i < UI_CELL * UI_CELL; i++) cellRgba[i] = Ui_UnpackTexel(up->pixels[i]); glTexSubImage2D(GL_TEXTURE_2D, 0, (GLint)cx, (GLint)cy, UI_CELL, UI_CELL, GL_RGBA, GL_UNSIGNED_BYTE, cellRgba); } } __atomic_store_n(&sTail, head, __ATOMIC_RELEASE); }