1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/FlipRepeat
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 confirm drawing out of texture range.
19 //
20 //      One square which has double length side of texture is displayed.
21 //
22 //      HOWTO:
23 //      1.      Call "G3_TexImageParam" to set mode for out of texture range.
24 //
25 //      OPERATION:
26 //      1.      Push A button to change texture repeat mode.
27 //      2.      Push B button to change texture flip mode.
28 //
29 //---------------------------------------------------------------------------
30 #ifdef SDK_TWL
31 #include <twl.h>
32 #else
33 #include <nitro.h>
34 #endif
35 #include "DEMO.h"
36 #include "tex_32768.h"
37 
38 //---------------------------------------------------------------------------
39 //      Square model data
40 //---------------------------------------------------------------------------
41 // Vertex data
42 static s16 s_Vertex[3 * 4] = {
43     FX16_ONE, FX16_ONE, -FX16_ONE,
44     FX16_ONE, -FX16_ONE, -FX16_ONE,
45     -FX16_ONE, FX16_ONE, -FX16_ONE,
46     -FX16_ONE, -FX16_ONE, -FX16_ONE
47 };
48 // Normal data
49 static VecFx10 s_Normal = GX_VECFX10(0, 0, -FX32_ONE + 1);
50 // Texture coordinate data
51 #define FX32_TWO        (FX32_ONE * 2)
52 static GXSt s_TextureCoord[] = {
53     GX_ST(0, 0),
54     GX_ST(0, 64 * FX32_TWO),
55     GX_ST(64 * FX32_TWO, 0),
56     GX_ST(64 * FX32_TWO, 64 * FX32_TWO)
57 };
58 
59 //---------------------------------------------------------------------------
60 //      Set vertex coordinate.
61 //      input:
62 //              idx:  ID of vertex data
63 //---------------------------------------------------------------------------
Vertex(int idx)64 inline void Vertex(int idx)
65 {
66     G3_Vtx(s_Vertex[idx * 3], s_Vertex[idx * 3 + 1], s_Vertex[idx * 3 + 2]);
67 }
68 
69 //---------------------------------------------------------------------------
70 //      Set normal setting.
71 //---------------------------------------------------------------------------
Normal(void)72 inline void Normal(void)
73 {
74     G3_Direct1(G3OP_NORMAL, s_Normal);
75 }
76 
77 //---------------------------------------------------------------------------
78 //      Set texture coordinate.
79 //      input:
80 //              idx:  ID of texture data
81 //---------------------------------------------------------------------------
TextureCoord(int idx)82 inline void TextureCoord(int idx)
83 {
84     G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[idx]);
85 }
86 
87 //---------------------------------------------------------------------------
88 //      Draw a rectangle and set texture.
89 //---------------------------------------------------------------------------
DrawRectangle(void)90 static void DrawRectangle(void)
91 {
92     G3_Begin(GX_BEGIN_QUADS);          // start to set vertexes.(use square polygon)
93     {
94         TextureCoord(1);
95         Normal();
96         Vertex(3);
97         TextureCoord(0);
98         Normal();
99         Vertex(2);
100         TextureCoord(2);
101         Normal();
102         Vertex(0);
103         TextureCoord(3);
104         Normal();
105         Vertex(1);
106     }
107     G3_End();                          // end
108 }
109 
110 //---------------------------------------------------------------------------
111 // VBlank interrupt function:
112 //
113 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
114 // OS_EnableIrqMask selects IRQ interrupts to enable, and
115 // OS_EnableIrq enables IRQ interrupts.
116 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
117 //---------------------------------------------------------------------------
VBlankIntr(void)118 void VBlankIntr(void)
119 {
120     // Set flag which checks VBlank interrupt.
121     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
122 }
123 
124 //---------------------------------------------------------------------------
125 //      main
126 //---------------------------------------------------------------------------
127 #ifdef SDK_TWL
TwlMain(void)128 void TwlMain(void)
129 #else
130 void NitroMain(void)
131 #endif
132 {
133     unsigned int count = 0;
134     GXTexRepeat repeat = GX_TEXREPEAT_NONE;
135     GXTexFlip flip = GX_TEXFLIP_NONE;
136     u32     myTexAddr = 0x00000;       // Address of texture image slot
137 
138     // Initialize
139     DEMOInitCommon();
140     DEMOInitVRAM();
141     DEMOInitDisplay3D();
142 
143     // Load texture image into texture image slot.
144     GX_BeginLoadTex();
145     {
146         GX_LoadTex((void *)&tex_32768_64x64[0], // Src address
147                    myTexAddr,          // Destination address
148                    8192);              // Size to load
149     }
150     GX_EndLoadTex();
151 
152     // Print information
153     OS_Printf("mssg%d:Repeat=S:%s T:%s  Flip=S:%s T:%s\n", count++,
154               ((repeat & 0x01) ? "TRUE" : "FALSE"),
155               ((repeat & 0x02) ? "TRUE" : "FALSE"),
156               ((flip & 0x01) ? "TRUE" : "FALSE"), ((flip & 0x02) ? "TRUE" : "FALSE"));
157 
158     DEMOStartDisplay();
159 
160     // main loop
161     while (1)
162     {
163         G3X_Reset();
164 
165         // Read input
166         DEMOReadKey();
167 #ifdef SDK_AUTOTEST                    // code for auto test
168         {
169             const EXTKeys keys[8] = { {0, 10}, {PAD_BUTTON_A | PAD_BUTTON_B, 10},
170             {0, 10}, {PAD_BUTTON_A | PAD_BUTTON_B, 10}, {0, 0}
171             };
172             EXT_AutoKeys(keys, &gKeyWork.press, &gKeyWork.trigger);
173         }
174 #endif
175 
176         // Change repeat mode
177         if (DEMO_IS_TRIG(PAD_BUTTON_A))
178         {
179             repeat = (GXTexRepeat)(((int)repeat + 1) % 4);
180         }
181         // Change flip mode
182         if (DEMO_IS_TRIG(PAD_BUTTON_B))
183         {
184             flip = (GXTexFlip)(((int)flip + 1) % 4);
185         }
186         // Print information
187         if (DEMO_IS_TRIG((PAD_BUTTON_A | PAD_BUTTON_B)))
188         {
189             OS_Printf("mssg%d:Repeat=S:%s T:%s  Flip=S:%s T:%s\n", count++,
190                       ((repeat & 0x01) ? "TRUE" : "FALSE"),
191                       ((repeat & 0x02) ? "TRUE" : "FALSE"),
192                       ((flip & 0x01) ? "TRUE" : "FALSE"), ((flip & 0x02) ? "TRUE" : "FALSE"));
193         }
194 
195         // Camera setting
196         {
197             VecFx32 Eye = { 0, 0, FX32_ONE };   // Sight position
198             VecFx32 at = { 0, 0, 0 };  // Viewpoint
199             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
200             G3_LookAt(&Eye, &vUp, &at, NULL);   // Sight setting
201         }
202 
203         // Light setting
204         G3_LightVector(GX_LIGHTID_0, 0, -FX32_ONE + 1, 0);
205         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
206 
207         // Matrix setting
208         G3_MtxMode(GX_MTXMODE_TEXTURE);
209         G3_Identity();
210         G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
211         G3_PushMtx();
212 
213         // Draw setting
214         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // Diffuse
215                                 GX_RGB(16, 16, 16),     // Ambient
216                                 TRUE); // Color
217         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // Specular
218                                 GX_RGB(0, 0, 0),        // Emission
219                                 FALSE); // Shininess
220         //---------------------------------------------------------------------------
221         // Texture drawing setting
222         G3_TexImageParam(GX_TEXFMT_DIRECT,      // Texture format
223                          GX_TEXGEN_TEXCOORD,    // Texture generation
224                          GX_TEXSIZE_S64,        // Texture width
225                          GX_TEXSIZE_T64,        // Texture height
226                          repeat,       // --- Texture REPEAT mode ---
227                          flip,         // --- Texture FLIP mode ---
228                          GX_TEXPLTTCOLOR0_USE,  // Palette color
229                          myTexAddr);   // Texture address
230         //---------------------------------------------------------------------------
231         G3_PolygonAttr(GX_LIGHTMASK_0, // Light
232                        GX_POLYGONMODE_MODULATE, // Polygon mode
233                        GX_CULL_NONE,   // Culling
234                        0,              // Polygon ID
235                        31,             // Alpha
236                        GX_POLYGON_ATTR_MISC_NONE);      // Misc
237 
238         // Draw square
239         DrawRectangle();
240 
241         G3_PopMtx(1);
242 
243         // swapping the polygon list RAM, the vertex RAM, etc.
244         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
245 
246         // Wait VBlank
247         OS_WaitVBlankIntr();
248 
249 #ifdef SDK_AUTOTEST                    // code for auto test
250         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
251         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
252         EXT_TestScreenShot(100, 0xA7CD1F92);
253         EXT_TestTickCounter();
254 #endif //SDK_AUTOTEST
255     }
256 }
257