/
template.c
216 lines (165 loc) · 6.49 KB
/
template.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
//-------------------------------------------------------
//
// 3D on both screens
// Jason Rogers (aka Dovoto)
//
//-------------------------------------------------------
#include <nds.h>
//-------------------------------------------------------
void renderCube(int angle){
//-------------------------------------------------------
glPushMatrix();
glTranslatef(0, 0, -4);
glRotatef32i(degreesToAngle(angle), inttof32(1), inttof32(1), inttof32(1));
glBegin(GL_QUADS);
glColor3b(255,0,0); glVertex3f(-1.0f, 1.0f, 1.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3b(255,255,0); glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3b(255,0,0); glVertex3f(-1.0f, 1.0f, -1.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, 1.0f, -1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3b(255,255,0); glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3b(255,0,0); glVertex3f(-1.0f, 1.0f, 1.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, 1.0f, -1.0f);
glColor3b(255,255,0); glVertex3f(-1.0f, 1.0f, -1.0f);
glColor3b(255,0,0); glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3b(255,255,0); glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3b(255,0,0); glVertex3f( 1.0f, 1.0f, -1.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3b(255,255,0); glVertex3f( 1.0f,-1.0f, -1.0f);
glColor3b(255,0,0); glVertex3f(-1.0f, 1.0f, -1.0f);
glColor3b(0,255,0); glVertex3f(-1.0f, 1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3b(255,255,0); glVertex3f(-1.0f,-1.0f, -1.0f);
glEnd();
glPopMatrix(1);
}
//-------------------------------------------------------
void renderPyramid(int angle){
//-------------------------------------------------------
glPushMatrix();
glTranslatef(0, 0, -4);
glRotatef32i(degreesToAngle(angle), inttof32(1),inttof32(1),inttof32(1));
glBegin(GL_QUADS);
glColor3b(255,0,0); glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3b(255,255,0); glVertex3f(-1.0f, -1.0f, -1.0f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3b(255,0,0); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3b(0,255,0); glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3b(255,0,0); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3b(0,255,0); glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3b(255,0,0); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3b(0,255,0); glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3b(255,0,0); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3b(0,255,0); glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3b(0,0,255); glVertex3f( 1.0f, -1.0f, -1.0f);
glEnd();
glPopMatrix(1);
}
//-------------------------------------------------------
void renderScene(bool top){
//-------------------------------------------------------
static short angle = 0;
if(top)
renderCube(angle);
else
renderPyramid(angle);
angle++;
}
//-------------------------------------------------------
// set up a 2D layer construced of bitmap sprites
// this holds the image when rendering to the top screen
//-------------------------------------------------------
void initSubSprites(void){
//-------------------------------------------------------
oamInit(&oamSub, SpriteMapping_Bmp_2D_256, false);
int x = 0;
int y = 0;
int id = 0;
//set up a 4x3 grid of 64x64 sprites to cover the screen
for(y = 0; y < 3; y++)
for(x = 0; x < 4; x++)
{
/* u16 *offset = &SPRITE_GFX_SUB[(x * 64) + (y * 64 * 256)];
oamSet(&oamSub, x + y * 4, x * 64, y * 64, 0, 15, SpriteSize_64x64,
SpriteColorFormat_Bmp, offset, -1, false,false,false,false,false);
*/
oamSub.oamMemory[id].attribute[0] = ATTR0_BMP | ATTR0_SQUARE | (64 * y);
oamSub.oamMemory[id].attribute[1] = ATTR1_SIZE_64 | (64 * x);
oamSub.oamMemory[id].attribute[2] = ATTR2_ALPHA(1) | (8 * 32 * y) | (8 * x);
id++;
}
swiWaitForVBlank();
oamUpdate(&oamSub);
}
//-------------------------------------------------------
int main() {
//-------------------------------------------------------
videoSetMode(MODE_0_3D);
videoSetModeSub(MODE_5_2D);
glInit();
// sub sprites hold the bottom image when 3D directed to top
initSubSprites();
// sub background holds the top image when 3D directed to bottom
bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
//-------------------------------------------------------
// Setup gl
//-------------------------------------------------------
glEnable(GL_ANTIALIAS);
glClearColor(0,0,0,31);
glClearPolyID(63);
glClearDepth(0x7FFF);
glViewport(0,0,255,191);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 256.0 / 192.0, 0.1, 100);
glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
//-------------------------------------------------------
// main loop
//-------------------------------------------------------
bool top = true;
while (true)
{
// wait for capture unit to be ready
while(REG_DISPCAPCNT & DCAP_ENABLE);
scanKeys();
int keys = keysDown();
if (keys & KEY_START) break;
//-------------------------------------------------------
// Switch render targets
//-------------------------------------------------------
top = !top;
if(top)
{
lcdMainOnBottom();
vramSetBankC(VRAM_C_LCD);
vramSetBankD(VRAM_D_SUB_SPRITE);
REG_DISPCAPCNT = DCAP_BANK(2) | DCAP_ENABLE | DCAP_SIZE(3);
}
else
{
lcdMainOnTop();
vramSetBankD(VRAM_D_LCD);
vramSetBankC(VRAM_C_SUB_BG);
REG_DISPCAPCNT = DCAP_BANK(3) | DCAP_ENABLE | DCAP_SIZE(3);
}
//-------------------------------------------------------
// Render the scene
//-------------------------------------------------------
glMatrixMode(GL_MODELVIEW);
renderScene(top);
glFlush(0);
}
return 0;
}