Alchemy
Loading...
Searching...
No Matches

fade_overlay.c

Go to the documentation of this file.
1/// @file
2/// The fade overlay drawn on the ground beside an actor.
3/// Part of object.c, the field object, compiled only through it.
4
6
7#ifndef UBFIX
8/// The map work in heap slot 8, seen as the halfwords of its view position:
9/// the high halves are the view's x and z in whole pixels.
10struct FadeView {
11 u8 unk00[228];
13};
14#endif
15
16/// Each frame of the fade overlay: on each of the two cells beside the actor
17/// (left and right of it), where the ground one or two cells south rises above
18/// the actor's floor, draws a semi-transparent 16-pixel-wide sprite of colour 1
19/// at the height of that ground. Registered as a scheduler callback by
20/// BattleFx_StartFadeOverlay; reads the work in heap slot 36 and queues the
21/// marks with Runtime_PushSlotEntry.
23 struct FadeOverlayWork *work = WORK_SLOT(36);
24#ifdef UBFIX
25 struct MapScrollWork *map = WORK_SLOT(8);
26 s32 cameraX = map->viewX >> 16;
27 s32 cameraZ = map->viewY >> 16;
28#else
29 // UB: the legacy halfword overlay aliases the map's s32 coordinates.
30 // Named-member high halves keep 0x1da bytes but change load scheduling
31 // from .text+0x6cce in English; the actor's named fields match plainly.
32 s16 *camera = ((struct FadeView *)WORK_SLOT(8))->camera;
33 s32 cameraX = camera[1];
34 s32 cameraZ = camera[3];
35#endif
36 struct AnimationSpritePart *mark = work->marks;
37 struct FieldActor *actor = work->actor;
38 s32 x;
39 s32 z;
40 s32 terrainFloor;
41 s32 terrainLayer;
42 u32 tile;
43 s32 height;
44 s32 secondHeight;
45 s32 screenRow;
46 /* FAKEMATCH(dead): four frame bytes nothing reads or writes, which make the
47 * native 16-byte frame; without them, 4 of 200 instructions differ in
48 * tbs-en. */
49 UNUSED s16 unused[2];
50
51 if (actor == 0)
52 return;
53 x = actor->x.fixed;
54 z = actor->z.fixed;
55 terrainFloor = actor->terrainHeight >> 16;
56 terrainLayer = actor->terrainId;
57 // The first tile of the solid block BattleFx_StartFadeOverlay loaded (VRAM
58 // cache block 94; tiles are 32 bytes).
59 tile = gVramBlockCache[94].offset >> 5;
60 // The left cell, 8 pixels left of the actor. The ground is read one and two
61 // cells (16 and 32 pixels) south, the further one counting 16 pixels lower,
62 // and the higher of the two is used.
63 x -= 0x80000;
64 height = Map_GetTerrainHeightFar(terrainLayer, x, z + 0x100000) >> 16;
65 secondHeight =
66 (Map_GetTerrainHeightFar(terrainLayer, x, z + 0x200000) >> 16) - 16;
67 if (secondHeight > height)
68 height = secondHeight;
69 if (height > 0 && height > terrainFloor) {
70 // Attributes 0 and 1 give a 16 by 16 square (size 1); the object mode
71 // and priority set next replace what the two words put there, so the
72 // left and right marks end up alike although their third words differ.
73 ((u32 *)mark)[1] = 0x40000800;
74 ((u32 *)mark)[2] = 0x400;
75 mark->priority = 0;
76 mark->tile = tile;
77 mark->objectMode = 1;
78 // Snapped to the cell's 16-pixel grid, then moved up by the ground's
79 // height.
80 mark->x = ((u32)(x >> 16) & 0xfff0) - cameraX;
81 screenRow = (z >> 16) & 0xf0;
82 mark->y = screenRow - cameraZ - height + 16;
83 Runtime_PushSlotEntry(mark, 0);
84 }
85 // The right cell, 8 pixels right of the actor, read the same way.
86 x += 0x100000;
87 height = Map_GetTerrainHeightFar(terrainLayer, x, z + 0x100000) >> 16;
88 mark = &work->marks[1];
89 secondHeight =
90 (Map_GetTerrainHeightFar(terrainLayer, x, z + 0x200000) >> 16) - 16;
91 if (secondHeight > height)
92 height = secondHeight;
93 if (height > 0 && height > terrainFloor) {
94 ((u32 *)mark)[1] = 0x40000800;
95 ((u32 *)mark)[2] = 0;
96 mark->priority = 0;
97 mark->tile = tile;
98 mark->objectMode = 1;
99 mark->x = ((u32)(x >> 16) & 0xfff0) - cameraX;
100 screenRow = (z >> 16) & 0xf0;
101 mark->y = screenRow - cameraZ - height + 16;
102 Runtime_PushSlotEntry(mark, 0);
103 }
104}
105
106/// Starts the fade overlay on object (the party leader when NULL): loads its
107/// solid tiles, sets the blending and draws it each frame. Allocates heap slot
108/// 36 for its work, borrows heap block 14 for the tiles (0x200 bytes of colour
109/// 1, VRAM cache block 94) and registers FadeOverlay_DrawMarks. Called by
110/// seven scene overlays (Venus and Mercury Lighthouses, Kalay, the tunnel
111/// ruins, Crossbone Isle, the ice room by Imil and Fuchin Falls); Crossbone
112/// Isle then switches it on and off by setting the work's actor.
113void BattleFx_StartFadeOverlay(void *object) {
114 struct FadeOverlayWork *work;
115 u8 *tiles;
116 volatile u32 fill;
117
118 work = Runtime_AllocateBlock(36, sizeof(*work));
119 // Upload the solid sprite tiles before enabling the drawing callback.
120 tiles = Runtime_AllocateHeapBlock(14, 0x400);
121 Dma_Fill32Cell(fill, 0x11111111, tiles, 0x200);
122 VramCache_Load(94, 0x200, tiles);
127 &REG_BLDCNT);
129 // 31 is above the register's useful maximum of 16, which it acts as.
130 Io_Write16(31, &REG_BLDY);
131 Dma_Fill32Cell(fill, 0, work, sizeof(*work));
132 if (object == NULL)
134 work->actor = object;
135}
136
137/// Stops drawing the fade overlay, keeping its work. Reached only through a
138/// bare far-call veneer; no ☀️ code names it.
141}
142
143/// Draws the fade overlay again after FadeOverlay_Suspend. Reached only
144/// through a bare far-call veneer.
void * ObjectTable_Get(u32 actorIndex)
Returns the actor in slot index (0 to 65), or NULL.
Definition actors.c:462
#define Dma_Fill32Cell(cell, value, destination, size)
Dma_Fill32 through the caller's own volatile word cell, so a function that fills several blocks keeps...
Definition dma.h:42
void FadeOverlay_Suspend(void)
Stops drawing the fade overlay, keeping its work.
void FadeOverlay_DrawMarks(void)
Each frame of the fade overlay: on each of the two cells beside the actor (left and right of it),...
void BattleFx_StartFadeOverlay(void *object)
Starts the fade overlay on object (the party leader when NULL): loads its solid tiles,...
void FadeOverlay_Resume(void)
Draws the fade overlay again after FadeOverlay_Suspend.
s32 Map_GetTerrainHeightFar(s32 layer, s32 x, s32 y)
struct GameState gGameState
void * Runtime_AllocateBlock(s32 kind, s32 size)
As Runtime_AllocateHeapBlock, but tries EWRAM first, for blocks that need room more than speed: the b...
Definition heap.c:80
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 WORK_SLOT(n)
Reads slots through their own addresses; adjacent slots are reached by offsets.
Definition heap_state.h:14
#define BLDCNT_TGT1_BG3
Definition io_reg.h:126
#define BLDCNT_TGT1_OBJ
Definition io_reg.h:127
#define BLDCNT_TGT1_BG2
Definition io_reg.h:125
#define REG_BLDY
Definition io_reg.h:146
#define BLDCNT_EFFECT_LIGHTEN
towards white by REG_BLDY
Definition io_reg.h:132
#define BLDCNT_TGT2_ALL
Definition io_reg.h:140
#define BLEND_ALPHA(top, bottom)
A REG_BLDALPHA value: top and bottom are the two targets' weights, out of 16.
Definition io_reg.h:145
#define BLDCNT_TGT1_BG1
Definition io_reg.h:124
#define REG_BLDALPHA
Definition io_reg.h:142
#define REG_BLDCNT
Definition io_reg.h:119
#define Io_Write16(v, reg)
Writes a value to a 16-bit register or memory location.
Definition iowrite.h:14
void Runtime_ReleaseHeapBlock(s32 slot)
What every part of object.c shares: its includes and the macros and types more than one part uses.
void Runtime_PushSlotEntry(void *slotEntry, s32 slot)
Links a sprite entry into depth slot slot (clamped to 0..255) for this frame.
Definition render.c:79
s32 Scheduler_ResumeTasks(SchedulerCallback callback)
Resumes the task running callback, or every task when callback is null, by clearing bit 0 of its orde...
Definition scheduler.c:243
s32 Scheduler_AddOrUpdateCallback(SchedulerCallback callback, s32 order)
Schedules callback with order, or gives a task already running it the new order, and re-sorts the tab...
Definition scheduler.c:102
s32 Scheduler_SuspendTasks(SchedulerCallback callback)
Suspends the task running callback, or every task when callback is null, by setting bit 0 of its orde...
Definition scheduler.c:172
#define NULL
Definition bilibin.h:6
The hardware sprite attributes one part of an animated sprite (its image or its shadow) is drawn with...
Definition animspr.h:84
u16 y
The OAM entry's attributes 0 to 2.
Definition animspr.h:90
u16 x
screen pixels
Definition animspr.h:96
u16 tile
first sprite tile in VRAM
Definition animspr.h:99
u16 priority
2 when created
Definition animspr.h:100
The fade overlay draws one mark on each side of its tracked actor.
struct FieldActor * actor
the actor tracked; null draws nothing
struct AnimationSpritePart marks[2]
The left and right marks: 16-pixel-wide semi-transparent sprites drawn where the ground beside the ac...
The map work in heap slot 8, seen as the halfwords of its view position: the high halves are the view...
s16 camera[4]
u8 unk00[228]
A field actor: a person or thing on the field with a position, motion toward a target,...
Definition field_obj.h:30
union FieldCoordinate x
16.16 fixed-point pixels
Definition field_obj.h:35
s32 terrainHeight
16.16 fixed-point pixels
Definition field_obj.h:38
union FieldCoordinate z
16.16 fixed-point pixels
Definition field_obj.h:37
u16 work
The side of the player's next footprint (2 after a stop), or scene scratch.
Definition field_obj.h:112
u8 terrainId
The map layer (0 to 3) its ground height is read from, which the scrolling camera updates from the ce...
Definition field_obj.h:47
s32 selectedActor
the leader: its unit and its actor
Definition game_state.h:59
The field map's work: the followed position, screen shake, tile animations, blend script,...
Definition map_scroll.h:27
s32 viewY
the view's top left, 16.16 fixed-point pixels
Definition map_scroll.h:40
u16 offset
bytes from the start of OBJ VRAM
Definition vram_tab.h:25
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
signed short s16
Definition types.h:12
#define UNUSED
Definition types.h:22
unsigned char u8
Definition types.h:11
s32 fixed
16.16 fixed-point pixels
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
struct VramBlockCacheEntry gVramBlockCache[]