Alchemy
Loading...
Searching...
No Matches

icons.c

Go to the documentation of this file.
1/// @file
2/// Item, Psynergy and command icons.
3/// Part of window.c, the window object, compiled only through it.
4
6
9}
10
13}
14
15/// Builds a command icon (overlay overlayIcon over frame frameIcon) and loads
16/// it into a sprite tile slot: *slot, or a free one when reuse is 0. Returns
17/// the tile in *tileOut.
18/// Frame decode is opaque; the command layer is then composited with
19/// transparency.
20void Ui_BuildPairedPatternsToSlot(s32 overlayIcon, s32 frameIcon, s32 *slot,
21 s32 *tileOut, s32 reuse) {
22 GlyphTransfer *work;
23
24 work = Runtime_AllocateHeapBlock(17, sizeof(GlyphTransfer));
25 work->encoded = gUiIcon_FramePointerTable[frameIcon];
26 work->width = 2;
27 work->height = 2;
28 UiIcon_Decode(work, 0);
29 work->encoded = gUiIcon_OverlayPointerTable[overlayIcon];
30 work->width = 2;
31 work->height = 2;
32 UiIcon_Decode(work, 1);
33 if (reuse == 0) {
34 *slot = Resource_FindFreeEntry();
35 }
36 *tileOut = VramCache_Load(*slot, 0x80, work->tiles);
38}
39
40/// Builds the icon of a battle action's item (its first operand) into a slot,
41/// as UiIcon_BuildItemIconTiles.
42void Ability_RequestGlyph(s32 action, s32 withBase, s32 *slot, s32 *tile,
43 s32 reuse) {
44 UiIcon_BuildItemIconTiles(BattleActGet(action)->unk04[0], withBase, slot,
45 tile, reuse);
46}
47
48/// Builds the item icon glyph, over the icon frame when withBase is set, and
49/// loads it into *cacheSlot (a free slot unless reuse is set). Returns the tile
50/// in *tileOut.
51/// The optional frame is decoded first and selects masked icon expansion.
52void UiIcon_BuildItemIconTiles(u32 glyph, s32 withBase, s32 *cacheSlot,
53 s32 *tileOut, s32 reuse) {
54 GlyphTransfer *work;
55 s32 overlay;
56
57 overlay = 0;
58 work = Runtime_AllocateHeapBlock(17, sizeof(GlyphTransfer));
59
60 if (glyph >= UiIcon_CountItemIcons())
61 glyph = 0;
62
63 if (withBase != 0) {
65 work->width = 2;
66 work->height = 2;
67 UiIcon_Decode(work, 0);
68 overlay = 1;
69 }
70
71 work->encoded = gUiIcon_ItemIconPointers[glyph];
72 work->width = 2;
73 work->height = 2;
74 UiIcon_Decode(work, overlay);
75
76 if (reuse == 0)
77 *cacheSlot = Resource_FindFreeEntry();
78
79 *tileOut = VramCache_Load(*cacheSlot, 0x80, work->tiles);
81}
82
83#define ICON_FRAME 1
84#define ICON_COUNT_ABOVE_ONE 2
85#define ICON_COUNT 4
86#define ICON_EQUIPPED 8
87#define ICON_BROKEN 16
88#define ICON_ARTIFACT 32
89
90/// Draws an item's icon into the glyph work buffer from its inventory code
91/// (item id in the low 9 bits, broken 0x200, equipped 0x400, count less one in
92/// the top 5 bits) with the layers asked for (ICON_ flags): the frame, the
93/// equipped, broken and artifact marks, and the count. Returns 256, or -1 when
94/// there is no glyph work buffer.
95s32 ItemIcon_Compose(u32 code, u32 layers) {
96 s32 overlay;
97 struct ItemDefinition *item;
98 GlyphTransfer *work;
99 s32 count;
100
101 overlay = 0;
102 count = 0;
103 item = Item_GetDirectFar(code & 0x1ff);
104 work = gGlyphWork;
105 if (work == NULL)
106 return -1;
107 if (layers & ICON_FRAME) {
109 work->width = 2;
110 work->height = 2;
111 UiIcon_Decode(work, 0);
112 overlay = 1;
113 }
115 work->width = 2;
116 work->height = 2;
117 UiIcon_Decode(work, overlay);
118 if ((layers & ICON_EQUIPPED) && (code & 0x400)) {
119 work->encoded = gUiIcon_MarkPointers[1];
120 work->width = 2;
121 work->height = 2;
122 UiIcon_Decode(work, 1);
123 }
124 if ((layers & ICON_BROKEN) && (code & 0x200)) {
125 work->encoded = gUiIcon_MarkPointers[0];
126 work->width = 2;
127 work->height = 2;
128 UiIcon_Decode(work, 1);
129 }
130 if ((layers & ICON_ARTIFACT) && (code & 0x200) && (item->flags & 1) &&
131 (item->flags & 2)) {
132 work->encoded = gUiIcon_MarkPointers[2];
133 work->width = 2;
134 work->height = 2;
135 UiIcon_Decode(work, 1);
136 }
137 if (layers & ICON_COUNT_ABOVE_ONE) {
138 count = (code & 0xf800) >> 11;
139 count++;
140 if (count <= 1)
141 count = 0;
142 }
143 if (layers & ICON_COUNT) {
144 count = (code & 0xf800) >> 11;
145 count++;
146 }
147 if (count != 0 && count <= 30) {
148 s32 ones = count % 10;
149
150 work->encoded = gUiIcon_DigitPointers[ones];
151 work->width = 2;
152 work->height = 2;
153 UiIcon_Decode(work, 1);
154 if (count / 10 != 0) {
155 work->encoded = gUiIcon_DigitPointers[count / 10 + 9];
156 work->width = 2;
157 work->height = 2;
158 UiIcon_Decode(work, 1);
159 }
160 }
161 return 256;
162}
163
164/// Composes an inventory icon with the requested layers and loads it into
165/// cacheSlot. The temporary glyph block is released after the upload.
166s32 UiIcon_CopyResourceToSlot(s32 inventoryCode, s32 layers, s32 cacheSlot) {
167 GlyphTransfer *work;
168
169 work = Runtime_AllocateHeapBlock(17, sizeof(GlyphTransfer));
170 ItemIcon_Compose(inventoryCode, layers);
171 VramCache_Load(cacheSlot, 0x80, work->tiles);
173 return 1;
174}
175
176/// Builds one of the other icons and loads it into sprite tile slot cacheSlot.
178 GlyphTransfer *work;
179
180 work = Runtime_AllocateHeapBlock(17, sizeof(GlyphTransfer));
182 VramCache_Load(cacheSlot, 0x80, work->tiles);
184 return 1;
185}
186extern const u8 *gRenderResource_PairSourceTable[];
187
188/// Loads one of four small fixed images into the sprite tile slot named by
189/// slot.
191 /* FAKEMATCH: three more names of the one table keep each case's own literal
192 * slot; with the one name in every case the switch folds away, differ 21 on
193 * tbs-en (9 instructions for 24). */
194 extern const u8 *PairSource1[] __asm__("gRenderResource_PairSourceTable"),
195 *PairSource2[] __asm__("gRenderResource_PairSourceTable"),
196 *PairSource3[] __asm__("gRenderResource_PairSourceTable");
197 void *source;
198 switch (value) {
199 case 1:
200 source = PairSource1;
201 break;
202 case 2:
203 source = PairSource2;
204 break;
205 case 3:
206 source = PairSource3;
207 break;
208 case 0:
209 default:
211 break;
212 }
213 VramCache_Load(slot, 32, source);
214 return 1;
215}
216
217/// Draws an item's icon from its inventory code into the glyph work buffer.
219 GlyphTransfer *glyph = gGlyphWork;
220 struct ItemDefinition *item = Item_GetDirectFar(0x1ff & code);
221
222 if (code != 0) {
223 glyph->encoded = gUiIcon_ItemIconPointers[item->icon];
224 } else {
226 }
227 glyph->width = 2;
228 glyph->height = 2;
229 UiIcon_Decode(glyph, 0);
230}
231
232/// Builds the Psynergy icon of a battle action, as
233/// UiIcon_BuildAbilityIconTiles.
234void Ability_LoadGlyph(s32 action, s32 withBase, s32 *slot, s32 *tile,
235 s32 reuse) {
236 UiIcon_BuildAbilityIconTiles(BattleActGet(action)->unk04[0], withBase, slot,
237 tile, reuse);
238}
239
240#define ROUTINE_BLOCK 49
241
242/// Builds the Psynergy icon glyph, over the icon frame when withBase is set,
243/// and loads it into *cacheSlot (a free slot unless reuse is set).
244/// Returns the tile in *tileOut.
245/// The optional frame is decoded first and selects masked icon expansion.
246void UiIcon_BuildAbilityIconTiles(u32 glyph, s32 withBase, s32 *cacheSlot,
247 s32 *tileOut, s32 reuse) {
248 GlyphTransfer *work;
249 s32 overlay;
250
251 work = Runtime_AllocateHeapBlock(17, sizeof(GlyphTransfer));
252 overlay = 0;
253
254 if (glyph >= UiIcon_CountPsynergyIcons())
255 glyph = 0;
256
257 if (withBase != 0) {
259 work->width = 2;
260 work->height = 2;
261 UiIcon_Decode(work, 0);
262 overlay = 1;
263 }
264
266 work->width = 2;
267 work->height = 2;
268 UiIcon_Decode(work, overlay);
269
270 if (reuse == 0)
271 *cacheSlot = Resource_FindFreeEntry();
272
273 *tileOut = VramCache_Load(*cacheSlot, 0x80, work->tiles);
275}
276
277/// Draws one of the other icons into the glyph work buffer.
279 GlyphTransfer *glyph = gGlyphWork;
280
281 glyph->encoded = gUiIcon_MiscIconPointers[iconIndex];
282 glyph->width = 2;
283 glyph->height = 2;
284 UiIcon_Decode(glyph, 0);
285}
286
287/// Loads a 32x32 icon from the icon resource into sprite tile slot *slot (a
288/// free slot unless reuse is set) and its colours into the sprite palette
289/// named by palette. Returns the tile in *tile.
290/// This resource stores its palette before the encoded tile payload.
291void UiGlyph_LoadEntryWithPalette(u32 icon, s32 unused, s32 *slot, s32 *tile,
292 s32 palette, s32 reuse) {
293 GlyphTransfer *work;
294 u16 *offsets;
295 u8 *encodedEntry;
296 u32 index;
297
298 work = Runtime_AllocateHeapBlock(17, sizeof(GlyphTransfer));
300 if (icon <= 127)
301 index = icon;
302 else
303 index = icon - 112;
304 encodedEntry = (u8 *)offsets + offsets[index];
305 work->encoded = encodedEntry + 32;
306 work->width = 4;
307 work->height = 4;
308 UiIcon_Decode(work, 0);
309 if (reuse == 0)
310 *slot = Resource_FindFreeEntry();
311 *tile = VramCache_Load(*slot, 0x200, work->tiles);
313 Dma_Set(encodedEntry, (void *)&OBJ_PLTT_COLOR(palette, 0), DMA_COPY16(16),
314 REG_DMA3);
315}
316
318
319/// Decodes a compressed icon into the glyph buffer and expands it to tiles,
320/// opaque for the first layer or masked over what is there when outlined is
321/// set. Releases each copied decoder before loading the next routine into the
322/// same slot.
323void UiIcon_Decode(GlyphTransfer *glyph, s32 outlined) {
326 ((void (*)(const void *, u8 *))gWorkSlot[ROUTINE_BLOCK])(glyph->encoded,
327 glyph->input);
329 if (outlined)
332 else
335 ((void (*)(u8 *, u8 *, u32, u32))gWorkSlot[ROUTINE_BLOCK])(
336 glyph->input, glyph->tiles, (u16)glyph->width, (u16)glyph->height);
338}
static __inline__ void Dma_Set(const volatile void *source, volatile void *destination, u32 control, volatile u32 *channel)
Starts a DMA transfer: stores the source, destination and control words into channel's registers with...
Definition dma.h:62
struct ItemDefinition * Item_GetDirectFar(s32 item)
struct BattleAct * BattleActGet(s32 actionId)
Far call to BattleAction_GetDirect.
u8 * gUiIcon_OverlayPointerTable[]
u8 * gUiIcon_ItemIconPointersEnd[]
u8 * gUiIcon_PsynergyIconPointers[]
u8 * gUiIcon_MiscIconPointers[]
u8 * gUiIcon_MarkPointers[]
const u8 Tile_ExpandOpaque[]
const u8 Tile_Decompress4bpp[]
u8 * gUiIcon_FramePointerTable[]
u8 * gUiIcon_PsynergyIconPointersEnd[]
u8 Tile_Decompress4bppCodeSize[]
u8 * gUiIcon_DigitPointers[]
u8 * gUiIcon_ItemIconPointers[]
const u8 Tile_ExpandMasked[]
u8 Tile_ExpandMaskedCodeSize[]
u8 Tile_ExpandOpaqueCodeSize[]
void * Runtime_AllocateHeapBlock(s32 kind, s32 size)
Returns heap slot kind's block, first allocating size bytes for it (rounded up to words),...
Definition heap.c:47
#define OBJ_PLTT_COLOR(palette, color)
Definition io_reg.h:160
#define REG_DMA3
Definition io_reg.h:153
void Runtime_ReleaseHeapBlock(s32 slot)
const u8 * gRenderResource_PairSourceTable[]
#define RamCode_LoadSlot(slot, code, bytes)
Allocates a work-slot copy; release it with Runtime_ReleaseHeapBlock.
Definition ramcode.h:26
char ResourceId_Icons
#define NULL
Definition bilibin.h:6
void * gWorkSlot[]
u8 input[0x400]
Definition glyph.h:39
s16 width
Definition glyph.h:41
u8 tiles[0x200]
Definition glyph.h:40
s16 height
Definition glyph.h:42
u8 * encoded
Definition glyph.h:43
An item's definition: its price, type, who can equip it, its bonuses and unleash, its effects and the...
Definition item.h:104
u8 flags
enum ItemFlags
Definition item.h:107
u16 icon
indexes gUiIcon_ItemIconPointers
Definition item.h:109
void * Resource_GetTableEntry(s32 index)
Returns the ROM address of resource index from the resource directory.
Definition system.c:150
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11
#define DMA_COPY16(count)
The DMA control word that copies count halfwords.
Definition window.h:114
s32 Resource_FindFreeEntry(void)
Returns the number of the first free cache entry, or VRAM_CACHE_ENTRY_COUNT when all are taken.
Definition vram_cache.c:211
s32 VramCache_Load(u32 no, u32 size, const void *src)
Gives cache entry no size bytes of sprite VRAM and copies src there.
Definition vram_cache.c:143
void UiIcon_Decode(GlyphTransfer *glyph, s32 outlined)
Decodes a compressed icon into the glyph buffer and expands it to tiles, opaque for the first layer o...
Definition icons.c:323
#define ICON_COUNT
Definition icons.c:85
void UiIcon_BuildAbilityIconTiles(u32 glyph, s32 withBase, s32 *cacheSlot, s32 *tileOut, s32 reuse)
Builds the Psynergy icon glyph, over the icon frame when withBase is set, and loads it into *cacheSlo...
Definition icons.c:246
void UiGlyph_ReservedNoOp(void)
Definition icons.c:317
s32 Ui_BuildPatternToSlot(s32 icon, s32 unused, s32 cacheSlot)
Builds one of the other icons and loads it into sprite tile slot cacheSlot.
Definition icons.c:177
#define ICON_BROKEN
Definition icons.c:87
s32 UiIcon_CopyResourceToSlot(s32 inventoryCode, s32 layers, s32 cacheSlot)
Composes an inventory icon with the requested layers and loads it into cacheSlot.
Definition icons.c:166
s32 ItemIcon_Compose(u32 code, u32 layers)
Draws an item's icon into the glyph work buffer from its inventory code (item id in the low 9 bits,...
Definition icons.c:95
void Ability_RequestGlyph(s32 action, s32 withBase, s32 *slot, s32 *tile, s32 reuse)
Builds the icon of a battle action's item (its first operand) into a slot, as UiIcon_BuildItemIconTil...
Definition icons.c:42
s32 RenderResource_LoadTableEntry(u32 value, s32 unused, u32 slot)
Loads one of four small fixed images into the sprite tile slot named by slot.
Definition icons.c:190
s32 UiIcon_CountPsynergyIcons(void)
Definition icons.c:11
#define ICON_ARTIFACT
Definition icons.c:88
void Ui_PrepareTransferForItem(s32 code)
Draws an item's icon from its inventory code into the glyph work buffer.
Definition icons.c:218
void Ui_BuildPairedPatternsToSlot(s32 overlayIcon, s32 frameIcon, s32 *slot, s32 *tileOut, s32 reuse)
Builds a command icon (overlay overlayIcon over frame frameIcon) and loads it into a sprite tile slot...
Definition icons.c:20
void Ability_LoadGlyph(s32 action, s32 withBase, s32 *slot, s32 *tile, s32 reuse)
Builds the Psynergy icon of a battle action, as UiIcon_BuildAbilityIconTiles.
Definition icons.c:234
void Ui_PrepareTransferFromTableEntry(u32 iconIndex)
Draws one of the other icons into the glyph work buffer.
Definition icons.c:278
#define ICON_COUNT_ABOVE_ONE
Definition icons.c:84
void UiIcon_BuildItemIconTiles(u32 glyph, s32 withBase, s32 *cacheSlot, s32 *tileOut, s32 reuse)
Builds the item icon glyph, over the icon frame when withBase is set, and loads it into *cacheSlot (a...
Definition icons.c:52
#define ROUTINE_BLOCK
Definition icons.c:240
#define ICON_FRAME
Definition icons.c:83
void UiGlyph_LoadEntryWithPalette(u32 icon, s32 unused, s32 *slot, s32 *tile, s32 palette, s32 reuse)
Loads a 32x32 icon from the icon resource into sprite tile slot *slot (a free slot unless reuse is se...
Definition icons.c:291
#define ICON_EQUIPPED
Definition icons.c:86
s32 UiIcon_CountItemIcons(void)
Definition icons.c:7
What every part of the window object shares: its includes, edition values, macros and the codes of de...
GlyphTransfer * gGlyphWork
The glyph transfer work in IWRAM (system/iwram.s), which icons are built in.