1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/2D_Oam_OBJWindow
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 //---------------------------------------------------------------------------
19 // A sample that uses an OBJ window
20 //
21 // This sample displays a sea view through a OBJ window
22 //
23 // USAGE:
24 // UP, DOWN, LEFT, RIGHT : Move the OBJ window
25 //
26 // HOWTO:
27 // 1. Set up OBJ window with GX_SetVisibleWnd(GX_WNDMASK_OW)
28 // 2. Set inside and outside of window with G2_SetWndOutsidePlane and
29 // G2_SetWndOBJInsidePlane
30 // 3. Transfer the OBJ data with GX_LoadOBJ
31 // 4. Set GX_OAM_MODE_OBJWND to OAM attribute to use the OBJ window
32 //---------------------------------------------------------------------------
33
34
35 #ifdef SDK_TWL
36 #include <twl.h>
37 #else
38 #include <nitro.h>
39 #endif
40 #include "DEMO.h"
41 #include "data.h"
42 //---------------------------------------------------------------------------
43 // Summary:
44 // OAM buffer region
45 //---------------------------------------------------------------------------
46 static GXOamAttr s_OAMBuffer[128];
47
48 //---------------------------------------------------------------------------
49 // Summary:
50 // V-Blank interrupt process
51 // Description:
52 // Enables a flag that confirms that a V-Blank interrupt has been performed
53 //
54 // The following steps will be performed during common initialization (DEMOInitCommon), causing this function to be called during V-Blanks.
55 // * Select IRQ interrupt (OS_SetIrqMask)
56 // * Register this function, which performs IRQ interrupts (OS_SetIRQFunction)
57 // * Enable IRQ interrupts (OS_EnableIrq)
58 //
59 //---------------------------------------------------------------------------
VBlankIntr(void)60 void VBlankIntr(void)
61 {
62 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Sets a V-Blank interrupt confirmation flag
63 }
64
65 //---------------------------------------------------------------------------
66 // Summary:
67 // Display OBJ window
68 // Description:
69 // Display a background BG through a circular OBJ window. Outside the OBJ window is displayed in black.
70 //
71 // It is possible to move the OBJ window by using the +Control Pad
72 // Controls:
73 // UP, DOWN : Moves the OBJ window
74 //---------------------------------------------------------------------------
75 #ifdef SDK_TWL
TwlMain(void)76 void TwlMain(void)
77 #else
78 void NitroMain(void)
79 #endif
80 {
81 int pos_x = 0, pos_y = 0;
82
83 //---------------------
84 // Initialization
85 //---------------------
86 DEMOInitCommon();
87 DEMOInitVRAM();
88
89 /* Select OAM and BG to be displayed */
90 GX_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_OBJ);
91
92 /* OAM configuration */
93 GX_SetBankForOBJ(GX_VRAM_OBJ_128_A); // Use VRAM-A
94 // Set character OBJ mapping mode
95 GX_SetOBJVRamModeChar(GX_OBJVRAMMODE_CHAR_2D); // 2D mapping
96
97 /* BG configuration */
98 GX_SetBankForBG(GX_VRAM_BG_128_B); // Use VRAM-B
99 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, // 2D/3D mode
100 GX_BGMODE_0, //
101 GX_BG0_AS_2D); // Display 2D
102 G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256, // Screen size 256x256
103 GX_BG_COLORMODE_256, // 256 colors
104 GX_BG_SCRBASE_0x0000, // Screen offset 0x0000
105 GX_BG_CHARBASE_0x04000, // Character offset 0x04000
106 GX_BG_EXTPLTT_01);
107 G2_SetBG0Priority(0); // BG 0 display priority to top
108 G2_BG0Mosaic(FALSE); // Do not use mosaic effect
109
110 /* Window configuration */
111 GX_SetVisibleWnd(GX_WNDMASK_OW); // Enable OBJ window
112 G2_SetWndOutsidePlane(GX_WND_PLANEMASK_NONE, // Disable outside window
113 FALSE);
114 G2_SetWndOBJInsidePlane(GX_WND_PLANEMASK_BG0, // Set inside of the window to BG0
115 FALSE);
116
117 /* load OBJ character data */
118 GX_LoadOBJ(d_64_256_obj_schDT, // Load OBJ character data (window shape)
119 0, sizeof(d_64_256_obj_schDT));
120 /* Load BG character data and palette data */
121 GX_LoadBG0Char(d_natsunoumi_schDT, // Load BG0 character data
122 0, sizeof(d_natsunoumi_schDT));
123 GX_LoadBGPltt(d_natsunoumi_sclDT, // Load BG palette data
124 0, sizeof(d_natsunoumi_sclDT));
125 GX_LoadBG0Scr(d_natsunoumi_sscDT, // Load BG0 screen data
126 0, sizeof(d_natsunoumi_sscDT));
127
128 DEMOStartDisplay();
129
130 //---------------------
131 // Main loop
132 //---------------------
133 while (1)
134 {
135 /* Load pad information */
136 DEMOReadKey();
137 #ifdef SDK_AUTOTEST // Code for auto-test
138 {
139 const EXTKeys keys[8] =
140 { {PAD_BUTTON_A, 10}, {PAD_BUTTON_L, 10}, {PAD_KEY_DOWN | PAD_KEY_RIGHT, 20}, {0,
141 0}
142 };
143 EXT_AutoKeys(keys, &gKeyWork.press, &gKeyWork.trigger);
144 }
145 #endif
146
147 if (DEMO_IS_PRESS(PAD_KEY_DOWN))
148 {
149 if (++pos_y > 191)
150 {
151 pos_y = 191;
152 }
153 }
154 else if (DEMO_IS_PRESS(PAD_KEY_UP))
155 {
156 if (--pos_y < 0)
157 {
158 pos_y = 0;
159 }
160 }
161 if (DEMO_IS_PRESS(PAD_KEY_RIGHT))
162 {
163 if (++pos_x > 255)
164 {
165 pos_x = 255;
166 }
167 }
168 else if (DEMO_IS_PRESS(PAD_KEY_LEFT))
169 {
170 if (--pos_x < 0)
171 {
172 pos_x = 0;
173 }
174 }
175
176 /* Configure the OAM attributes of the OBJ window */
177 G2_SetOBJAttr(&s_OAMBuffer[0], // Pointer to the OAM to use
178 pos_x, // x position
179 pos_y, // y position
180 0, // Priority order
181 GX_OAM_MODE_OBJWND, // Set to OBJ window mode
182 FALSE, // Disable mosaic
183 GX_OAM_EFFECT_NONE, // No special effects
184 GX_OAM_SHAPE_64x64, // OBJ size
185 GX_OAM_COLOR_256, // 256 colors
186 0, 0, 0);
187
188 #ifdef SDK_AUTOTEST // Code for auto-test
189 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
190 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
191 EXT_TestScreenShot(100, 0xAE1F20B9);
192 EXT_TestTickCounter();
193 #endif //SDK_AUTOTEST
194
195 /* Dump the cache to main memory, and invalidate it */
196 DC_FlushRange(s_OAMBuffer, sizeof(s_OAMBuffer));
197 /* I/O register is accessed using DMA operation, so cache wait is not needed */
198 // DC_WaitWriteBufferEmpty();
199
200 /* V-Blank wait */
201 OS_WaitVBlankIntr();
202
203 /* Transfer to OAM */
204 GX_LoadOAM(s_OAMBuffer, // Transfer OAM buffer to OAM
205 0, sizeof(s_OAMBuffer));
206 MI_DmaFill32(3, // Clear OAM buffer
207 s_OAMBuffer, 192, sizeof(s_OAMBuffer));
208 }
209 }
210