1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/Master_Bright
3   File:     main.c
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 //---------------------------------------------------------------------------
18 //  A sample to adjust screen master brightness.
19 //
20 //  One cube is displayed.
21 //  And all screen bright along the setting of brightness.
22 //
23 //  HOWTO:
24 //  1.  Call "GX_SetMasterBrightness(brightness)" to change brightness.
25 //
26 //  OPERATION:
27 //  1.  Push UP button to put up brightness.
28 //  2.  Push DOWN button to put down brightness.
29 //  3.  Push start button to reset brightness default.
30 //
31 //---------------------------------------------------------------------------
32 #ifdef SDK_TWL
33 #include <twl.h>
34 #else
35 #include <nitro.h>
36 #endif
37 #include "DEMO.h"
38 #include "tex_32768.h"
39 
40 //---------------------------------------------------------------------------
41 //  Cube model data
42 //---------------------------------------------------------------------------
43 // Vertex data
44 static s16 s_Vertex[3 * 8] = {
45     FX16_ONE, FX16_ONE, FX16_ONE,
46     FX16_ONE, FX16_ONE, -FX16_ONE,
47     FX16_ONE, -FX16_ONE, FX16_ONE,
48     FX16_ONE, -FX16_ONE, -FX16_ONE,
49     -FX16_ONE, FX16_ONE, FX16_ONE,
50     -FX16_ONE, FX16_ONE, -FX16_ONE,
51     -FX16_ONE, -FX16_ONE, FX16_ONE,
52     -FX16_ONE, -FX16_ONE, -FX16_ONE
53 };
54 // Normal data
55 static VecFx10 s_Normal[6] = {
56     GX_VECFX10(0, 0, FX32_ONE - 1),
57     GX_VECFX10(0, FX32_ONE - 1, 0),
58     GX_VECFX10(FX32_ONE - 1, 0, 0),
59     GX_VECFX10(0, 0, -FX32_ONE + 1),
60     GX_VECFX10(0, -FX32_ONE + 1, 0),
61     GX_VECFX10(-FX32_ONE + 1, 0, 0)
62 };
63 // Texture coordinate data
64 static GXSt s_TextureCoord[] = {
65     GX_ST(0, 0),
66     GX_ST(0, 64 * FX32_ONE),
67     GX_ST(64 * FX32_ONE, 0),
68     GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
69 };
70 
71 //---------------------------------------------------------------------------
72 //  Set vertex coordinate.
73 //  input:
74 //      idx:  ID of vertex data
75 //---------------------------------------------------------------------------
Vertex(int idx)76 inline void Vertex(int idx)
77 {
78     G3_Vtx(s_Vertex[idx * 3], s_Vertex[idx * 3 + 1], s_Vertex[idx * 3 + 2]);
79 }
80 
81 //---------------------------------------------------------------------------
82 //  Set normal setting.
83 //  input:
84 //      idx:  ID of normal data
85 //---------------------------------------------------------------------------
Normal(int idx)86 inline void Normal(int idx)
87 {
88     G3_Direct1(G3OP_NORMAL, s_Normal[idx]);
89 }
90 
91 //---------------------------------------------------------------------------
92 //  Set texture coordinate.
93 //  input:
94 //      idx:  ID of texture data
95 //---------------------------------------------------------------------------
TextureCoord(int idx)96 inline void TextureCoord(int idx)
97 {
98     G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[idx]);
99 }
100 
101 //---------------------------------------------------------------------------
102 //  Draw a cube and set texture.
103 //---------------------------------------------------------------------------
DrawCube(void)104 static void DrawCube(void)
105 {
106     G3_Begin(GX_BEGIN_QUADS);          // start to set vertex.(use square polygon)
107     {
108         TextureCoord(1);
109         Normal(0);
110         Vertex(2);
111         TextureCoord(0);
112         Normal(0);
113         Vertex(0);
114         TextureCoord(2);
115         Normal(0);
116         Vertex(4);
117         TextureCoord(3);
118         Normal(0);
119         Vertex(6);
120 
121         TextureCoord(1);
122         Normal(3);
123         Vertex(7);
124         TextureCoord(0);
125         Normal(3);
126         Vertex(5);
127         TextureCoord(2);
128         Normal(3);
129         Vertex(1);
130         TextureCoord(3);
131         Normal(3);
132         Vertex(3);
133 
134         TextureCoord(1);
135         Normal(5);
136         Vertex(6);
137         TextureCoord(0);
138         Normal(5);
139         Vertex(4);
140         TextureCoord(2);
141         Normal(5);
142         Vertex(5);
143         TextureCoord(3);
144         Normal(5);
145         Vertex(7);
146 
147         TextureCoord(1);
148         Normal(2);
149         Vertex(3);
150         TextureCoord(0);
151         Normal(2);
152         Vertex(1);
153         TextureCoord(2);
154         Normal(2);
155         Vertex(0);
156         TextureCoord(3);
157         Normal(2);
158         Vertex(2);
159 
160         TextureCoord(1);
161         Normal(1);
162         Vertex(5);
163         TextureCoord(0);
164         Normal(1);
165         Vertex(4);
166         TextureCoord(2);
167         Normal(1);
168         Vertex(0);
169         TextureCoord(3);
170         Normal(1);
171         Vertex(1);
172 
173         TextureCoord(1);
174         Normal(4);
175         Vertex(6);
176         TextureCoord(0);
177         Normal(4);
178         Vertex(7);
179         TextureCoord(2);
180         Normal(4);
181         Vertex(3);
182         TextureCoord(3);
183         Normal(4);
184         Vertex(2);
185     }
186     G3_End();                          // end
187 }
188 
189 
190 //---------------------------------------------------------------------------
191 // VBlank interrupt function:
192 //
193 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
194 // OS_EnableIrqMask selects IRQ interrupts to enable, and
195 // OS_EnableIrq enables IRQ interrupts.
196 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
197 //---------------------------------------------------------------------------
VBlankIntr(void)198 void VBlankIntr(void)
199 {
200     // Set flag which checks VBlank interrupt.
201     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
202 }
203 
204 //---------------------------------------------------------------------------
205 //  main
206 //---------------------------------------------------------------------------
207 #ifdef  SDK_CW_WA_CONSTPOOLS
208 #pragma optimization_level 1
209 #endif
210 #ifdef SDK_TWL
TwlMain(void)211 void TwlMain(void)
212 #else
213 void NitroMain(void)
214 #endif
215 {
216     unsigned int count = 0;
217     int     brightness = 8;
218     u32     myTexAddr = 0x01000;       // Address of texture image slot
219     u16     Rotate = 0;
220 
221     // Initialize
222     DEMOInitCommon();
223     DEMOInitVRAM();
224     DEMOInitDisplay3D();
225 
226     // Load texture image into texture image slot.
227     GX_BeginLoadTex();
228     {
229         GX_LoadTex((void *)&tex_32768_64x64[0], // Src address
230                    myTexAddr,          // Destination address
231                    8192);              // Size to load
232     }
233     GX_EndLoadTex();
234 
235     DEMOStartDisplay();
236 
237     // main loop
238     while (1)
239     {
240         G3X_Reset();
241         Rotate += 256;
242 
243         // Read input
244         DEMOReadKey();
245 #ifdef SDK_AUTOTEST                    // code for auto test
246         {
247             const EXTKeys keys[8] = { {PAD_KEY_UP, 4}, {0, 0} };
248             EXT_AutoKeys(keys, &gKeyWork.press, &gKeyWork.trigger);
249         }
250 #endif
251 
252         // Put up brightness
253         if (DEMO_IS_PRESS(PAD_KEY_UP))
254         {
255             if (++brightness > 16)
256                 brightness = 16;
257             OS_Printf("mssg%d:Brightness=%d\n", count++, brightness);
258         }
259         // Put down brightness
260         else if (DEMO_IS_PRESS(PAD_KEY_DOWN))
261         {
262             if (--brightness < -16)
263                 brightness = -16;
264             OS_Printf("mssg%d:Brightness=%d\n", count++, brightness);
265         }
266         // Reset brightness default
267         if (DEMO_IS_TRIG(PAD_BUTTON_START))
268         {
269             brightness = 8;
270             OS_Printf("mssg%d:Brightness=%d\n", count++, brightness);
271         }
272 
273         //---------------------------------------------------------------------------
274         // Change brightness toward all screen
275         GX_SetMasterBrightness(brightness);
276         //---------------------------------------------------------------------------
277 
278         // Camera setting
279         {
280             VecFx32 Eye = { 0, 0, FX32_ONE };   // Sight position
281             VecFx32 at = { 0, 0, 0 };  // Viewpoint
282             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
283             G3_LookAt(&Eye, &vUp, &at, NULL);   // Sight setting
284         }
285 
286         // Light setting
287         G3_LightVector(GX_LIGHTID_0, 0, -FX32_ONE + 1, 0);
288         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
289 
290         // Matrix setting
291         G3_MtxMode(GX_MTXMODE_TEXTURE);
292         G3_Identity();
293         G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
294         G3_PushMtx();
295 
296         // Rotate and translate cube
297         {
298             fx16    s = FX_SinIdx(Rotate);
299             fx16    c = FX_CosIdx(Rotate);
300 
301             G3_Translate(0, 0, -5 * FX32_ONE);
302 
303             G3_RotX(s, c);
304             G3_RotY(s, c);
305             G3_RotZ(s, c);
306         }
307 
308         // Draw setting
309         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // Diffuse
310                                 GX_RGB(16, 16, 16),     // Ambient
311                                 TRUE); // Color
312         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // Specular
313                                 GX_RGB(0, 0, 0),        // Emission
314                                 FALSE); // Shininess
315         G3_TexImageParam(GX_TEXFMT_DIRECT,      // Texture format
316                          GX_TEXGEN_TEXCOORD,    // Texture generation
317                          GX_TEXSIZE_S64,        // Texture width
318                          GX_TEXSIZE_T64,        // Texture height
319                          GX_TEXREPEAT_NONE,     // Texture repeat
320                          GX_TEXFLIP_NONE,       // Texture flip
321                          GX_TEXPLTTCOLOR0_USE,  // Palette color
322                          myTexAddr);   // Texture address
323         G3_PolygonAttr(GX_LIGHTMASK_0, // Light
324                        GX_POLYGONMODE_MODULATE, // Polygon mode
325                        GX_CULL_NONE,   // Culling
326                        0,              // Polygon ID
327                        31,             // Alpha
328                        GX_POLYGON_ATTR_MISC_NONE);      // Misc
329 
330         // Draw cube
331         DrawCube();
332 
333         G3_PopMtx(1);
334 
335         // swapping the polygon list RAM, the vertex RAM, etc.
336         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
337 
338         // Wait VBlank
339         OS_WaitVBlankIntr();
340 
341 #ifdef SDK_AUTOTEST                    // code for auto test
342         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
343         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
344         EXT_TestScreenShot(100, 0x8C9F5E42);
345         EXT_TestTickCounter();
346 #endif //SDK_AUTOTEST
347     }
348 }
349