-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpileptor.c
155 lines (117 loc) · 3.05 KB
/
Epileptor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "Epileptor.h"
#include "Global.h"
#include "VGA.h"
#include "LED.h"
#include "Button.h"
#include "Utils.h"
#include "Random.h"
#include "Graphics/Bitmap.h"
#include "Graphics/Drawing.h"
#include "Graphics/Font.h"
#include <stdint.h>
#include <string.h>
static uint32_t sqrti(uint32_t n);
extern Font OLFont;
void Epileptor(const char* message)
{
uint8_t *framebuffer1=(uint8_t *)0x20000000;
uint8_t *framebuffer2=(uint8_t *)0x20010000;
memset(framebuffer1,0,320*200);
memset(framebuffer2,0,320*200);
for(int i=0;i<256;i++)
{
int r=ExtractRawRed(i);
int g=ExtractRawGreen(i);
int b=ExtractRawBlue(i);
if(b && (r&1)==0 && (g&1)==0 ) b--;
if(r) r--;
if(g) g--;
data.epileptor.replacements[i]=RawRGB(r,g,b);
}
SetVGAScreenMode320x200(framebuffer1);
Bitmap frame1,frame2;
InitializeBitmap(&frame1,320,200,320,framebuffer1);
InitializeBitmap(&frame2,320,200,320,framebuffer2);
int frame=0;
int first=VGAFrameCounter();
for(int i=0;i<NumberOfScrollerStars;i++)
{
data.epileptor.stars[i].x=Fix(RandomInteger()%320);
data.epileptor.stars[i].y=RandomInteger()%200;
int z=sqrti((NumberOfScrollerStars-1-i)*NumberOfScrollerStars)*1000/NumberOfScrollerStars;
data.epileptor.stars[i].dx=6000*1200/(z+200);
int c=255*i/NumberOfScrollerStars;
switch(RandomInteger()%5)
{
case 0:
case 1:
data.epileptor.stars[i].c=RGB(0,c,c);
break;
case 2:
data.epileptor.stars[i].c=RGB(c,c,c);
break;
case 3:
case 4:
data.epileptor.stars[i].c=RGB(c,0,c);
break;
}
}
while(CurrentBitBinRow(&song) < 1280)
{
WaitVBL();
Bitmap *currframe;
if(frame)
{
currframe=&frame2;
SetFrameBuffer(framebuffer1);
for(int j=0;j<320*200;j++)
framebuffer2[j]=data.epileptor.replacements[framebuffer1[j]];
}
else
{
currframe=&frame1;
SetFrameBuffer(framebuffer2);
for(int j=0;j<320*200;j++)
framebuffer1[j]=data.epileptor.replacements[framebuffer2[j]];
}
int t=VGAFrameCounter()-first;
int x=320-(t*5)/2;
for(int i=0;i<NumberOfScrollerStars;i++)
{
int oldx=data.epileptor.stars[i].x;
int newx=oldx+data.epileptor.stars[i].dx;
data.epileptor.stars[i].x=newx;
DrawHorizontalLine(currframe,FixedToInt(oldx),data.epileptor.stars[i].y,
FixedToInt(newx)-FixedToInt(oldx),data.epileptor.stars[i].c);
if(newx>=Fix(320))
{
data.epileptor.stars[i].x=Fix(0);
data.epileptor.stars[i].y=RandomInteger()%200;
}
}
for(const char *ptr=message;*ptr;ptr++)
{
int c=*ptr;
int y=(isin(x*20+t*33)>>7)+100-8;
DrawCharacter(currframe,&OLFont,x,y,0,c);
x+=WidthOfCharacter(&OLFont,c)+2;
}
SetLEDs(1<<((t/3)&3));
frame^=1;
}
while(UserButtonState());
}
static uint32_t sqrti(uint32_t n)
{
uint32_t s,t;
#define sqrtBit(k) \
t = s+(1UL<<(k-1)); t <<= k+1; if (n >= t) { n -= t; s |= 1UL<<k; }
s=0;
if(n>=1<<30) { n-=1<<30; s=1<<15; }
sqrtBit(14); sqrtBit(13); sqrtBit(12); sqrtBit(11); sqrtBit(10);
sqrtBit(9); sqrtBit(8); sqrtBit(7); sqrtBit(6); sqrtBit(5);
sqrtBit(4); sqrtBit(3); sqrtBit(2); sqrtBit(1);
if(n>s<<1) s|=1;
#undef sqrtBit
return s;
}