Alchemy
Loading...
Searching...
No Matches

fade.c

Go to the documentation of this file.
1/// @file
2/// Screen fades through the blend registers.
3/// Part of runtime.c, the runtime object, compiled only through it.
4
6
7enum {
8 /// The layers the screen fades take: all but BG0.
13 FADE_LEVEL_MAX = 16 ///< BLDY's strongest level: all white or all black.
14};
15
16/// Steps the screen fade once a frame, called by System_VBlankHandler: sets
17/// BLDCNT to brighten or darken the chosen layers, moves BLDY one step from
18/// the start level to the target level and counts gBlendFramesLeft down; at
19/// the end it clears gBlendDuration.
20void Fade_Update(void) {
21 if (gBlendDuration != 0) {
22 {
23 volatile u16 *blendControl;
24 u32 control;
25
26 if (gBlendBrighten != 0) {
27 control = gBlendLayers | FADE_BRIGHTEN;
28 blendControl = &REG_BLDCNT;
29 } else {
30 control = gBlendLayers | FADE_DARKEN;
31 blendControl = &REG_BLDCNT;
32 }
33 *blendControl = control;
34 }
35 {
36 u8 *remaining = &gBlendFramesLeft;
37 s32 delta;
38 s32 level;
39 s32 step;
40
41 (*remaining)--;
42 level = gBlendTargetLevel;
44 step = *remaining;
45 /* FAKEMATCH: an explicit libgcc call clobbers memory, so the zero test
46 * reloads *remaining after it as the native does; with the / operator,
47 * 37 instructions differ in every edition. */
48 level += __divsi3(delta * step, gBlendDuration);
49 REG_BLDY = level;
50 if (*remaining == 0)
52 }
53 }
54}
55
56// Screen fades. Fade_Update moves the fade one step each VBlank.
57// The brightness (BLDY) runs from 0, the picture as drawn, to 16, all white
58// or all black.
59// gBlendBrighten : 0 fades toward black, 1 toward white
60// gBlendLayers : the BLDCNT layers the fade covers
61// gBlendFramesLeft : frames until the fade ends
62
63/// Fades BG1 to BG3, the sprites and the backdrop (0x3e) from the current
64/// target brightness to `level` over `frames` frames.
65#define FADE_DEFINE_BLEND(name, brighten, level) \
66 void name(s32 frames) { \
67 gBlendBrighten = brighten; \
68 gBlendLayers = FADE_GAME_LAYERS; \
69 gBlendStartLevel = gBlendTargetLevel; \
70 gBlendTargetLevel = level; \
71 gBlendDuration = frames; \
72 gBlendFramesLeft = gBlendDuration; \
73 }
74
75/// Fades out to black.
77
78/// Fades in from black.
80
81/// Fades out to white.
83
84/// Fades in from white.
86
87/// Starts a fade of any layers by setting the gBlend globals Fade_Update
88/// steps. Reached through the far-call table.
89/// bright : 0 toward black, 1 toward white
90/// layer : the BLDCNT layers to fade (low six bits)
91/// start : the starting brightness; above 16, the current target
92/// target : the brightness to end at
93/// frames : how long the fade takes
94void Fade_Start(s8 bright, s32 layer, u32 start, s32 target, s32 frames) {
95 gBlendBrighten = bright;
97 if (start > FADE_LEVEL_MAX) {
99 } else {
100 gBlendStartLevel = start;
101 }
102 gBlendTargetLevel = target;
103 gBlendFramesLeft = (gBlendDuration = frames);
104}
105
106/// Waits a frame at a time until the running fade has finished. Called by the
107/// title screen and the battle, and through the far-call table.
108void Fade_Wait(void) {
109 if (gBlendDuration != 0) {
110 do {
111 WaitFrames(1);
112 } while (gBlendDuration != 0);
113 }
114}
115
116#undef FADE_DEFINE_BLEND
s32 __divsi3(s32, s32)
void Fade_ToBlack(s32 frames)
Fades out to black.
Definition fade.c:76
#define FADE_DEFINE_BLEND(name, brighten, level)
Fades BG1 to BG3, the sprites and the backdrop (0x3e) from the current target brightness to level ove...
Definition fade.c:65
void Fade_ToWhite(s32 frames)
Fades out to white.
Definition fade.c:82
void Fade_FromWhite(s32 frames)
Fades in from white.
Definition fade.c:85
void Fade_Wait(void)
Waits a frame at a time until the running fade has finished.
Definition fade.c:108
void Fade_FromBlack(s32 frames)
Fades in from black.
Definition fade.c:79
@ FADE_LEVEL_MAX
BLDY's strongest level: all white or all black.
Definition fade.c:13
@ FADE_BRIGHTEN
Definition fade.c:11
@ FADE_LAYER_MASK
Definition fade.c:10
@ FADE_DARKEN
Definition fade.c:12
@ FADE_GAME_LAYERS
The layers the screen fades take: all but BG0.
Definition fade.c:9
void Fade_Update(void)
Steps the screen fade once a frame, called by System_VBlankHandler: sets BLDCNT to brighten or darken...
Definition fade.c:20
void Fade_Start(s8 bright, s32 layer, u32 start, s32 target, s32 frames)
Starts a fade of any layers by setting the gBlend globals Fade_Update steps.
Definition fade.c:94
void WaitFrames(s32 frames)
The game's frame loop: runs frames frames and returns.
Definition frame.c:97
u16 gBlendLayers
u8 gBlendBrighten
volatile u8 gBlendTargetLevel
u8 gBlendFramesLeft
volatile u8 gBlendDuration
u8 gBlendStartLevel
#define BLDCNT_EFFECT_DARKEN
towards black by REG_BLDY
Definition io_reg.h:133
#define REG_BLDY
Definition io_reg.h:146
#define BLDCNT_EFFECT_LIGHTEN
towards white by REG_BLDY
Definition io_reg.h:132
#define BLDCNT_TGT1_BG0
Definition io_reg.h:123
#define BLDCNT_TGT1_ALL
Definition io_reg.h:129
#define REG_BLDCNT
Definition io_reg.h:119
What every part of the runtime object shares: its includes and the library call two parts make.
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
signed char s8
Definition types.h:10
unsigned short u16
Definition types.h:13
unsigned char u8
Definition types.h:11