Skip to content

Commit 96dbeda

Browse files
vBlank optimization
1 parent ecd4d63 commit 96dbeda

5 files changed

Lines changed: 67 additions & 45 deletions

File tree

FSPDS.cbp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
</ExtraCommands>
3838
<Unit filename="Makefile" />
3939
<Unit filename="arm7/Makefile" />
40+
<Unit filename="arm7/include/sound.h" />
4041
<Unit filename="arm7/source/main.c">
4142
<Option compilerVar="CC" />
4243
</Unit>
@@ -53,9 +54,6 @@
5354
</Unit>
5455
<Extensions>
5556
<lib_finder disable_auto="1" />
56-
<code_completion />
57-
<envvars />
58-
<debugger />
5957
</Extensions>
6058
</Project>
6159
</CodeBlocks_project_file>

arm7/include/sound.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef FSPDS_SOUND_H_INCLUDED
2+
#define FSPDS_SOUND_H_INCLUDED
3+
4+
5+
6+
#endif // FSPDS_SOUND_H_INCLUDED

arm9/include/player.h

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,16 @@ const u16 FlipnoteColors[4] =
9393
0xFCE1 // Blue
9494
};
9595

96-
void playerNextFrame()
96+
void buildLayer(int *i, int *k, u8 *first_byte_header, s8 *tX, s8 *tY, bool *diffing)
9797
{
98-
int i=ppm_OffsetTable[PlayerFrameIndex];
99-
u8 first_byte_header=ppm_AnimationData[i++];
100-
s8 tX=0,tY=0; // translate parameters
101-
bool diffing=!(first_byte_header & 0b10000000);
102-
if(first_byte_header & 0b01100000)
103-
{
104-
tX=ppm_AnimationData[i++];
105-
tY=ppm_AnimationData[i++];
106-
}
107-
108-
layer=layer1;
109-
int k=i+0x60;
110-
111-
__LBL__BUILD_LAYER__:
112-
if(diffing)
98+
if((*diffing))
11399
{
114100
memcpy(layerA,layer,LAYER_SIZE);
115101
}
116102
for(u16 y=0;y<192;y++)
117103
{
118104
u16 yy=y<<5;
119-
switch((ppm_AnimationData[i+(y>>2)]>>((y&0x3)<<1)) & 0x3)
105+
switch((ppm_AnimationData[(*i)+(y>>2)]>>((y&0x3)<<1)) & 0x3)
120106
{
121107
case 0:
122108
{
@@ -128,7 +114,7 @@ void playerNextFrame()
128114
{
129115
for(u16 c=0;c<32;c++)
130116
{
131-
u8 chunk=ppm_AnimationData[k++];
117+
u8 chunk=ppm_AnimationData[(*k)++];
132118
for(u8 b=0;b<8;b++)
133119
layer[yy] ^= ((-(u8)((chunk>>b)&1) ^ layer[yy]) & (1<<b));
134120
yy++;
@@ -149,15 +135,15 @@ void playerNextFrame()
149135

150136
__DECODETYPE_1_2__: ;
151137

152-
u32 bytes=ppm_AnimationData[k++]<<24;
153-
bytes|=ppm_AnimationData[k++]<<16;
154-
bytes|=ppm_AnimationData[k++]<<8;
155-
bytes|=ppm_AnimationData[k++];
138+
u32 bytes=ppm_AnimationData[(*k)++]<<24;
139+
bytes|=ppm_AnimationData[(*k)++]<<16;
140+
bytes|=ppm_AnimationData[(*k)++]<<8;
141+
bytes|=ppm_AnimationData[(*k)++];
156142
for(;bytes;)
157143
{
158144
if(bytes & 0x80000000)
159145
{
160-
u8 chunk=ppm_AnimationData[k++];
146+
u8 chunk=ppm_AnimationData[(*k)++];
161147
for(int b=0;b<8;b++)
162148
layer[yy] ^= (-(u8)((chunk>>b)&1) ^ layer[yy]) & (1<<b);
163149
}
@@ -168,36 +154,60 @@ void playerNextFrame()
168154
}
169155
}
170156
}
171-
if(diffing)
157+
if((*diffing))
172158
{
173159
for(u16 y=0;y<192;y++)
174160
{
175-
if(y<tY) continue;
176-
if(y-tY>=192) break;
177-
u16 BL=y<<5,BA=(y-tY)<<5;
161+
if(y<(*tY)) continue;
162+
if(y-(*tY)>=192) break;
163+
u16 BL=y<<5,BA=(y-(*tY))<<5;
178164
for(u8 c=0;c<32;c++)
179165
for(u8 b=0;b<8;b++)
180166
{
181167
u8 x=8*c+b;
182-
if(x<tX) continue;
183-
if(x-tX>=256) break;
184-
u8 dx=(x-tX);
168+
if(x<(*tX)) continue;
169+
if(x-(*tX)>=256) break;
170+
u8 dx=(x-(*tX));
185171
layer[BL+c]^= ((bool)(layerA[BA+(dx>>3)]&(1<<(dx&0x7))))<<b;
186172
}
187173
}
188174
}
189-
if(layer==layer1)
175+
}
176+
177+
/* [ vBlank Op ] Because minimum flipnote frame rate is 30 fps (so a frame each 2 vBlanks)
178+
I decided render each of the two layers on a separate vBlank
179+
Hope this will minimize the chances of missing vBlanks, which could cause
180+
synchronization troubles
181+
*/
182+
183+
void playerNextFrameVBlank0(int *i, int *k, u8 *first_byte_header, s8 *tX, s8 *tY, bool *diffing)
184+
{
185+
(*i)=ppm_OffsetTable[PlayerFrameIndex];
186+
(*first_byte_header)=ppm_AnimationData[(*i)++];
187+
(*tX)=0,(*tY)=0; // translate parameters
188+
(*diffing)=!((*first_byte_header) & 0b10000000);
189+
if((*first_byte_header) & 0b01100000)
190190
{
191-
layer=layer2;
192-
i+=0x30;
193-
goto __LBL__BUILD_LAYER__;
191+
(*tX)=ppm_AnimationData[(*i)++];
192+
(*tY)=ppm_AnimationData[(*i)++];
194193
}
195194

196-
u16 paperColor = (first_byte_header & 1);
197-
u16 layer1Color = (first_byte_header>>1) & 0x3;
195+
layer=layer1;
196+
(*k)=(*i)+0x60;
197+
buildLayer(i, k, first_byte_header, tX, tY, diffing);
198+
}
199+
200+
void playerNextFrameVBlank1(int *i, int *k, u8 *first_byte_header, s8 *tX, s8 *tY, bool *diffing)
201+
{
202+
layer=layer2;
203+
(*i)+=0x30;
204+
buildLayer(i, k, first_byte_header, tX, tY, diffing);
205+
206+
u16 paperColor = ((*first_byte_header) & 1);
207+
u16 layer1Color = ((*first_byte_header)>>1) & 0x3;
198208
if(layer1Color<2)
199209
layer1Color = 1-paperColor;
200-
u16 layer2Color = (first_byte_header>>3) & 0x3;
210+
u16 layer2Color = ((*first_byte_header)>>3) & 0x3;
201211
if(layer2Color<2)
202212
layer2Color = 1-paperColor;
203213
paperColor=FlipnoteColors[paperColor];

arm9/include/vars.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ bool PlayerThumbnailNeedsRedraw = false;
4848

4949
u16 PlayerFrameIndex=0;
5050

51-
const u8 frameTime[9]=
52-
{
53-
0, 120, 60, 30, 15, 10, 5, 3, 2
54-
};
51+
const u8 frameTime[9]= { 0, 120, 60, 30, 15, 10, 5, 3, 2 };
5552

5653
#endif // FSPDS_VARS_H_INCLUDED

arm9/source/main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ int main(int argc, char ** argv)
2727

2828

2929
u16 counter=0;
30+
int iPos, kPos;
31+
u8 first_byte_header;
32+
s8 tX=0,tY=0; // translate parameters
33+
bool diffing;
3034
while(1)
3135
{
3236
scanKeys();
@@ -62,10 +66,17 @@ int main(int argc, char ** argv)
6266

6367
if(PlayerState==PLAYING)
6468
{
69+
if(counter==0) // vBlankOp
70+
{
71+
playerNextFrameVBlank0(&iPos,&kPos,&first_byte_header,&tX,&tY,&diffing);
72+
}
73+
else if(counter==1)
74+
{
75+
playerNextFrameVBlank1(&iPos,&kPos,&first_byte_header,&tX,&tY,&diffing);
76+
}
6577
counter++;
6678
if(counter==frameTime[ppm_FramePlaybackSpeed])
6779
{
68-
playerNextFrame();
6980
counter=0;
7081
}
7182
}

0 commit comments

Comments
 (0)