-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeartField.c
327 lines (233 loc) · 7.04 KB
/
HeartField.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "Tunnel.h"
#include "DS3D/DS3D.h"
#include "DS3D/Utils.h"
#include <math.h>
#include <nds.h>
#include <nds/registers_alt.h>
#define PartSize 1024
uint32_t heartlist[PartSize];
static inline float CalcNormalU(float u1,float v1,float u2,float v2)
{
float du=u2-u1;
float dv=v2-v1;
float r=sqrtf(du*du+dv*dv);
return dv/r;
}
static inline float CalcNormalV(float u1,float v1,float u2,float v2)
{
float du=u2-u1;
float dv=v2-v1;
float r=sqrtf(du*du+dv*dv);
return -du/r;
}
#define YOFFS 0.3
#define Y0 (-1+YOFFS)
#define X1 0.5
#define Y1 (-0.5+YOFFS)
#define Z1 0.2
#define X2 0.7
#define Y2 (0+YOFFS)
#define Z2 0.3
#define X3 0.5
#define Y3 (0.3+YOFFS)
#define X4 0.2
#define Y4 (0.35+YOFFS)
#define N1_0_X CalcNormalU(0,Y0,X2,Y2)
#define N1_0_Y CalcNormalV(0,Y0,X2,Y2)
#define N1_1_Y CalcNormalU(Y2,Z2,Y0,0)
#define N1_1_Z CalcNormalV(Y2,Z2,Y0,0)
#define N2_0_X CalcNormalU(X1,Y1,X3,Y3)
#define N2_0_Y CalcNormalV(X1,Y1,X3,Y3)
#define N2_1_Y CalcNormalU(Y2,-Z2,Y1,Z1)
#define N2_1_Z CalcNormalV(Y2,-Z2,Y1,Z1)
#define N3_X CalcNormalU(X2,Y2,X4,Y4)
#define N3_Y CalcNormalV(X2,Y2,X4,Y4)
#define N4_X CalcNormalU(X3,Y3,0,Y2)
#define N4_Y CalcNormalV(X3,Y3,0,Y2)
#define NumHearts 100
struct Heart
{
int32_t x,y,z;
} hearts[NumHearts];
static void MakeParts()
{
DSStartList(heartlist,PartSize);
DSListBegin(DS_TRIANGLE_STRIP);
DSListNormal3f(0,-1,0);
DSListVertex3f(0,Y0,0);
DSListNormal3f(-N1_0_X,N1_0_Y,0);
DSListVertex3f(-X1,Y1,0);
DSListNormal3f(0,N1_1_Y,N1_1_Z);
DSListVertex3f(0,Y1,Z1);
DSListNormal3f(-N2_0_X,N2_0_Y,0);
DSListVertex3f(-X2,Y2,0);
DSListNormal3f(0,N2_1_Y,N2_1_Z);
DSListVertex3f(0,Y2,Z2);
DSListNormal3f(-N3_X,N3_Y,0);
DSListVertex3f(-X3,Y3,0);
DSListNormal3f(-N4_X,N4_Y,0);
DSListVertex3f(-X4,Y4,0);
DSListEnd();
DSListBegin(DS_TRIANGLE_STRIP);
DSListNormal3f(N4_X,N4_Y,0);
DSListVertex3f(X4,Y4,0);
DSListNormal3f(N3_X,N3_Y,0);
DSListVertex3f(X3,Y3,0);
DSListNormal3f(0,N2_1_Y,N2_1_Z);
DSListVertex3f(0,Y2,Z2);
DSListNormal3f(N2_0_X,N2_0_Y,0);
DSListVertex3f(X2,Y2,0);
DSListNormal3f(0,N1_1_Y,N1_1_Z);
DSListVertex3f(0,Y1,Z1);
DSListNormal3f(N1_0_X,N1_0_Y,0);
DSListVertex3f(X1,Y1,0);
DSListNormal3f(0,-1,0);
DSListVertex3f(0,Y0,0);
DSListEnd();
DSListBegin(DS_TRIANGLE_STRIP);
DSListNormal3f(0,-1,0);
DSListVertex3f(0,Y0,0);
DSListNormal3f(N1_0_X,N1_0_Y,0);
DSListVertex3f(X1,Y1,0);
DSListNormal3f(0,N1_1_Y,-N1_1_Z);
DSListVertex3f(0,Y1,-Z1);
DSListNormal3f(N2_0_X,N2_0_Y,0);
DSListVertex3f(X2,Y2,0);
DSListNormal3f(0,N2_1_Y,-N2_1_Z);
DSListVertex3f(0,Y2,-Z2);
DSListNormal3f(N3_X,N3_Y,0);
DSListVertex3f(X3,Y3,0);
DSListNormal3f(N4_X,N4_Y,0);
DSListVertex3f(X4,Y4,0);
DSListEnd();
DSListBegin(DS_TRIANGLE_STRIP);
DSListNormal3f(-N4_X,N4_Y,0);
DSListVertex3f(-X4,Y4,0);
DSListNormal3f(-N3_X,N3_Y,0);
DSListVertex3f(-X3,Y3,0);
DSListNormal3f(0,N2_1_Y,-N2_1_Z);
DSListVertex3f(0,Y2,-Z2);
DSListNormal3f(-N2_0_X,N2_0_Y,0);
DSListVertex3f(-X2,Y2,0);
DSListNormal3f(0,N1_1_Y,-N1_1_Z);
DSListVertex3f(0,Y1,-Z1);
DSListNormal3f(-N1_0_X,N1_0_Y,0);
DSListVertex3f(-X1,Y1,0);
DSListNormal3f(0,-1,0);
DSListVertex3f(0,Y0,0);
DSListEnd();
DSListBegin(DS_TRIANGLE_STRIP);
DSListNormal3f(-N4_X,N4_Y,0);
DSListVertex3f(-X4,Y4,0);
DSListNormal3f(0,N2_1_Y,-N2_1_Z);
DSListVertex3f(0,Y2,-Z2);
DSListNormal3f(0,N2_1_Y,N2_1_Z);
DSListVertex3f(0,Y2,Z2);
DSListNormal3f(N4_X,N4_Y,0);
DSListVertex3f(X4,Y4,0);
DSListEnd();
if(DSFinishList()>PartSize) for(;;);
}
static int a=0;
static int b=0;
static void PlaceHeart(struct Heart *heart)
{
heart->x=isin(a)+7*isin(b*3);
heart->y=icos(a)+7*icos(b*3);
a+=b;
b+=0x2;
}
void InitHeartField()
{
DISPCNT_A=DISPCNT_MODE_5|DISPCNT_3D|DISPCNT_BG0_ON|DISPCNT_BG2_ON|DISPCNT_BG3_ON|DISPCNT_ON;
BLDCNT_A = BLDCNT_SRC_A_BG3|BLDCNT_SRC_B_BG0|BLDCNT_EFFECT_ALPHA;
BLDALPHA_A = BLDALPHA_EVA(8)|BLDALPHA_EVB(15);
// BG and frame
VRAMCNT_D = VRAMCNT_D_LCDC;
VRAMCNT_B = VRAMCNT_B_BG_VRAM_A_OFFS_0K;
VRAMCNT_A = VRAMCNT_A_BG_VRAM_A_OFFS_128K;
loadImageVRAMIndirectGreen( "nitro:/gfx/tunnel_frame.img.bin", VRAM_A_OFFS_0K,256*256*2);
loadImageVRAMIndirectGreen( "nitro:/gfx/stripe_bg_hori.img.bin", VRAM_A_OFFS_128K,256*256*2);
BG2CNT_A = BGxCNT_EXTENDED_BITMAP_16 | BGxCNT_OVERFLOW_WRAP | BGxCNT_BITMAP_SIZE_256x256 | BGxCNT_BITMAP_BASE_0K;
BG2CNT_A = (BG2CNT_A&~BGxCNT_PRIORITY_MASK)|BGxCNT_PRIORITY_0;
BG2_XDX = (1 << 8);
BG2_XDY = 0;
BG2_YDX = 0;
BG2_YDY = (1 << 8);
BG2_CX = 0;
BG2_CY = 0;
// Init BG0 priority as above BG3
BG0CNT_A = (BG0CNT_A&~BGxCNT_PRIORITY_MASK)|BGxCNT_PRIORITY_2;
BG3CNT_A = BGxCNT_EXTENDED_BITMAP_16 | BGxCNT_OVERFLOW_WRAP | BGxCNT_BITMAP_SIZE_256x256 | BGxCNT_BITMAP_BASE_128K;
BG3CNT_A = (BG3CNT_A&~BGxCNT_PRIORITY_MASK)|BGxCNT_PRIORITY_1;
BG3_XDX = (1 << 8);
BG3_XDY = 0;
BG3_YDX = 0;
BG3_YDY = (1 << 8);
BG3_CX = 0;
BG3_CY = 0;
DSInit3D();
DSViewport(0,0,255,191);
DSSetControl(DS_FOG|DS_TOON_SHADING|DS_OUTLINE);
DSClearParams(0,0,0,31,63);
for(int i=0;i<8;i++) DSSetOutlineColor3b(i,31,31,31,0);
DSSetToonTableRange3b(0,31,24,0,0,0);
DSSetToonTableRange3b(12,31,31,0,0,0);
//DSSetToonTableRange3b(24,31,31,4,4,0);
//DSSetToonTableRange3b(28,31,31,8,8,0);
DSMatrixMode(DS_PROJECTION);
DSLoadIdentity();
DSPerspectivef(65,256.0/192.0,0.1,16);
DSSetFogLinearf(0,0,0,31,6,10,0.1,16);
MakeParts();
for(int i=0;i<NumHearts;i++)
{
PlaceHeart(&hearts[i]);
hearts[i].z=-F(10)*i/NumHearts;
}
}
void RunHeartField(int t)
{
DSMatrixMode(DS_POSITION_AND_VECTOR);
DSLoadIdentity();
#define LX 0.5
#define LY -0.5
#define LZ -1
DSLight3f(0,0x7fff,LX/sqrtf(LX*LX+LY*LY+LZ*LZ),LY/sqrtf(LX*LX+LY*LY+LZ*LZ),LZ/sqrtf(LX*LX+LY*LY+LZ*LZ));
// DSMaterialDiffuseAndAmbient(0x3def,0x3def);
// DSMaterialDiffuseAndAmbient6b(15,15,15,16,16,16);
DSMaterialDiffuseAndAmbient6b(31,0,0,0,0,0);
DSMatrixMode(DS_PROJECTION);
DSPushMatrix();
int dx=7*isin(b*3-NumHearts*2*3);
int dy=7*icos(b*3-NumHearts*2*3);
ivec3_t forward=ivec3norm(ivec3(dy,-dx,F(6)));
ivec3_t side=ivec3norm(ivec3cross(ivec3(0,F(1),0),forward));
ivec3_t up=ivec3cross(forward,side);
DSMultMatrix3x3(imat3x3vec3(side,up,forward));
DSTranslatef32(-dx,-dy,0);
DSMatrixMode(DS_POSITION_AND_VECTOR);
DSLoadIdentity();
for(int i=0;i<NumHearts;i++)
{
DSLoadIdentity();
DSTranslatef32(hearts[i].x,hearts[i].y,hearts[i].z);
int32_t scale=isin(F(1)*i/NumHearts-t*24);
scale=scale/5+F(0.4);
DSScalef32(scale,scale,scale);
DSRotateYi(F(1)*i/NumHearts+t*0x10);
int id=((-hearts[i].z)>>10)&0x3f;
DSPolygonAttributes(DS_POLY_MODE_TOON|DS_POLY_CULL_FRONT|DS_POLY_LIGHT0|DS_POLY_FOG|
DS_POLY_ALPHA(31)|DS_POLY_ID(id));
DSCallList(heartlist);
hearts[i].z+=F(0.1);
if(hearts[i].z>=0)
{
PlaceHeart(&hearts[i]);
hearts[i].z-=F(10);
}
}
DSMatrixMode(DS_PROJECTION);
DSPopMatrix(0);
DSSwapBuffers(0);
}