1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/DEMOLib
3 File: DEMOInit.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:: 2009-01-13#$
14 $Rev: 9815 $
15 $Author: nakasima $
16 *---------------------------------------------------------------------------*/
17
18 #ifdef SDK_TWL
19 #include <twl.h>
20 #else
21 #include <nitro.h>
22 #endif
23 #include "DEMOInit.h"
24 #include "DEMOKey.h"
25
DEMOInitCommon(void)26 void DEMOInitCommon(void)
27 {
28 OS_Init();
29 FX_Init();
30
31 // GX_SetPower(GX_POWER_ALL);
32 GX_Init();
33
34 GX_DispOff();
35 GXS_DispOff();
36
37 OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
38 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
39 (void)OS_EnableIrq();
40
41 DEMOReadKey(); // Dummy read to save previous key input
42
43 (void)GX_VBlankIntr(TRUE); // to generate VBlank interrupt request
44 }
45
DEMOInitVRAM(void)46 void DEMOInitVRAM(void)
47 {
48 //---------------------------------------------------------------------------
49 // All VRAM banks to LCDC
50 //---------------------------------------------------------------------------
51 GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
52
53 //---------------------------------------------------------------------------
54 // Clear all LCDC space
55 //---------------------------------------------------------------------------
56 MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
57
58 //---------------------------------------------------------------------------
59 // Disable the banks on LCDC
60 //---------------------------------------------------------------------------
61 (void)GX_DisableBankForLCDC();
62
63 MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE); // clear OAM
64 MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE); // clear the standard palette
65
66 MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE); // clear OAM
67 MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE); // clear the standard palette
68 }
69
70
DEMOInitDisplayBG0Only(void)71 void DEMOInitDisplayBG0Only(void)
72 {
73
74 GX_SetBankForBG(GX_VRAM_BG_128_A); // VRAM-A for BGs
75
76 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, // graphics mode
77 GX_BGMODE_0, // BGMODE is 0
78 GX_BG0_AS_2D); // BG #0 is for 2D
79
80 GX_SetVisiblePlane(GX_PLANEMASK_BG0); // display only BG #0
81
82 G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, // 256pix x 256pix text
83 GX_BG_COLORMODE_256, // use 256 colors mode
84 GX_BG_SCRBASE_0x0000, // screen base offset + 0x0000 is the address for BG #0 screen
85 GX_BG_CHARBASE_0x04000, // character base offset + 0x04000 is the address for BG #0 characters
86 GX_BG_EXTPLTT_01 // use BGExtPltt slot #0 if BGExtPltt is enabled
87 );
88
89 G2_SetBG0Priority(0);
90
91 G2_BG0Mosaic(FALSE);
92 }
93
DEMOInitDisplaySubBG0Only(void)94 void DEMOInitDisplaySubBG0Only(void)
95 {
96
97 GX_SetBankForSubBG(GX_VRAM_SUB_BG_128_C); // VRAM-C for BGs
98
99 GXS_SetGraphicsMode(GX_BGMODE_0); // BGMODE is 0
100
101 GXS_SetVisiblePlane(GX_PLANEMASK_BG0); // display only BG #0
102
103 G2S_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, // 256pix x 256pix text
104 GX_BG_COLORMODE_256, // use 256 colors mode
105 GX_BG_SCRBASE_0x0000, // screen base offset + 0x0000 is the address for BG #0 screen
106 GX_BG_CHARBASE_0x04000, // character base offset + 0x04000 is the address for BG #0 characters
107 GX_BG_EXTPLTT_01 // use BGExtPltt slot #0 if BGExtPltt is enabled
108 );
109
110 G2S_SetBG0Priority(0);
111
112 G2S_BG0Mosaic(FALSE);
113 }
114
115
116
DEMOInitDisplayBG2Only(void)117 void DEMOInitDisplayBG2Only(void)
118 {
119 GX_SetBankForBG(GX_VRAM_BG_128_A); // VRAM-A for BGs
120
121 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, // graphics mode
122 GX_BGMODE_2, // BGMODE is 2
123 GX_BG0_AS_2D // BG #0 is for 2D
124 );
125
126 GX_SetVisiblePlane(GX_PLANEMASK_BG2); // display only BG #2
127
128 G2_SetBG2ControlAffine(GX_BG_SCRSIZE_AFFINE_128x128, // 128pix x 128pix affine
129 GX_BG_AREAOVER_XLU, // no repeat(transparent)
130 GX_BG_SCRBASE_0x0000, // screen base offset + 0x0000 is the address for BG #2 screen
131 GX_BG_CHARBASE_0x04000 // character base offset + 0x04000 is the address for BG #2 characters
132 );
133
134 G2_SetBG2Priority(0);
135
136 G2_BG2Mosaic(FALSE);
137 }
138
139
DEMOInitDisplaySubBG2Only(void)140 void DEMOInitDisplaySubBG2Only(void)
141 {
142 GX_SetBankForSubBG(GX_VRAM_SUB_BG_128_C); // VRAM-C for BGs
143
144 GXS_SetGraphicsMode(GX_BGMODE_2); // BGMODE is 2
145
146 GXS_SetVisiblePlane(GX_PLANEMASK_BG2); // display only BG #2
147
148 G2S_SetBG2ControlAffine(GX_BG_SCRSIZE_AFFINE_128x128, // 128pix x 128pix affine
149 GX_BG_AREAOVER_XLU, // no repeat(transparent)
150 GX_BG_SCRBASE_0x0000, // screen base offset + 0x0000 is the address for BG #2 screen
151 GX_BG_CHARBASE_0x04000 // character base offset + 0x04000 is the address for BG #2 characters
152 );
153
154 G2S_SetBG2Priority(0);
155
156 G2S_BG2Mosaic(FALSE);
157 }
158
159
160
DEMOInitDisplayOBJOnly(void)161 void DEMOInitDisplayOBJOnly(void)
162 {
163 GX_SetBankForOBJ(GX_VRAM_OBJ_128_A); // VRAM-A for OBJ
164
165 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, // graphics mode
166 GX_BGMODE_0, // BGMODE is 0
167 GX_BG0_AS_2D); // BG #0 is for 2D
168
169 GX_SetVisiblePlane(GX_PLANEMASK_OBJ); // display only OBJ
170
171 GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D); // 2D mapping mode
172 }
173
174
DEMOInitDisplaySubOBJOnly(void)175 void DEMOInitDisplaySubOBJOnly(void)
176 {
177 GX_SetBankForSubOBJ(GX_VRAM_SUB_OBJ_128_D); // VRAM-D for OBJ
178
179 GXS_SetGraphicsMode(GX_BGMODE_0); // BGMODE is 0
180
181 GXS_SetVisiblePlane(GX_PLANEMASK_OBJ); // display only OBJ
182
183 GXS_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D); // 2D mapping mode
184 }
185
DEMOInitDisplay3D(void)186 void DEMOInitDisplay3D(void)
187 {
188 G3X_Init(); // initialize the 3D graphics states
189 // G3X_InitTable(); // initialize tables
190
191 G3X_InitMtxStack(); // initialize the matrix stack
192
193 GX_SetBankForTex(GX_VRAM_TEX_0_A); // VRAM-A for texture images
194
195 GX_SetBankForBG(GX_VRAM_BG_128_D); // VRAM-D for BGs
196
197 GX_SetBankForTexPltt(GX_VRAM_TEXPLTT_0123_E); // VRAM-E for texture palettes
198
199 GX_SetBankForOBJ(GX_VRAM_OBJ_32_FG); // VRAM-FG for OBJ
200
201 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, // graphics mode
202 GX_BGMODE_4, // BGMODE is 4
203 GX_BG0_AS_3D); // BG #0 is for 3D
204
205 GX_SetVisiblePlane(GX_PLANEMASK_BG0 | // BG #0 for 3D
206 GX_PLANEMASK_BG1 | // BG #1 for text
207 GX_PLANEMASK_OBJ // OBJ
208 );
209
210 G2_SetBG0Priority(1);
211
212 G2_SetBG1Control(GX_BG_SCRSIZE_TEXT_256x256, // 256pix x 256pix text
213 GX_BG_COLORMODE_16, // use 16 colors mode
214 GX_BG_SCRBASE_0x3800, // screen base offset + 0x3800 is the address for BG #1 screen
215 GX_BG_CHARBASE_0x00000, // character base offset + 0x00000 is the address for BG #1 characters
216 GX_BG_EXTPLTT_01 // use BGExtPltt slot #1 if BGExtPltt is enabled
217 );
218
219 G2_SetBG1Priority(0);
220
221 G2_BG1Mosaic(FALSE);
222
223 G3X_SetShading(GX_SHADING_TOON); // shading mode is toon
224
225 G3X_AntiAlias(TRUE); // enable antialias(without additional computing costs)
226
227 G2_SetBlendAlpha(GX_BLEND_PLANEMASK_BG0|GX_BLEND_PLANEMASK_BG1|GX_BLEND_PLANEMASK_BG2|GX_BLEND_PLANEMASK_BG3|GX_BLEND_PLANEMASK_OBJ,
228 GX_BLEND_PLANEMASK_BG0|GX_BLEND_PLANEMASK_BG1|GX_BLEND_PLANEMASK_BG2|GX_BLEND_PLANEMASK_BG3|GX_BLEND_PLANEMASK_OBJ|GX_BLEND_PLANEMASK_BD,
229 16,
230 0
231 );
232
233 //---------------------------------------------------------------------------
234 // After you start the geometry engine and the rendering one
235 // (by GX_SetPower etc.), G3_SwapBuffers must be called once before use
236 //---------------------------------------------------------------------------
237 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
238
239 //---------------------------------------------------------------------------
240 // Set clear color, depth and polygon ID.
241 //---------------------------------------------------------------------------
242 G3X_SetClearColor(GX_RGB(0, 0, 0), // clear color
243 31, // clear alpha
244 0x7fff, // clear depth
245 63, // clear polygon ID
246 FALSE // fog
247 );
248
249 G3_ViewPort(0, 0, 255, 191); // Viewport
250
251 //---------------------------------------------------------------------------
252 // Set up the projection matrix
253 //---------------------------------------------------------------------------
254 {
255 fx32 right = FX32_ONE;
256 fx32 top = FX32_ONE * 3 / 4;
257 fx32 near = FX32_ONE;
258 fx32 far = FX32_ONE * 400;
259
260 //---------------------------------------------------------------------------
261 // Switch MTXMODE to GX_MTXMODE_PROJECTION, and
262 // set a projection matrix onto the current projection matrix on the matrix stack
263 //---------------------------------------------------------------------------
264 G3_Perspective(FX32_SIN30, FX32_COS30, // sine and cosine of FOVY
265 FX32_ONE * 4 / 3, // aspect
266 near, // near
267 far, // far
268 NULL // a pointer to a matrix if you use it
269 );
270
271 G3_StoreMtx(0);
272 }
273 }
274
DEMOInit(void)275 void DEMOInit(void)
276 {
277 DEMOInitCommon();
278 DEMOInitVRAM();
279 DEMOInitDisplay3D();
280 }
281
DEMOStartDisplay(void)282 void DEMOStartDisplay(void)
283 {
284 OS_WaitVBlankIntr(); // Waiting the end of VBlank interrupt
285 GX_DispOn();
286 GXS_DispOn();
287 }
288