1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/2D_CharBg_7
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 blends two text BG planes:
20 //
21 // USAGE:
22 // UP, DOWN, LEFT, RIGHT : Controls blending coefficients
23 //
24 // HOWTO:
25 // 1. Set up the character/palette/screen data same as 2D_CharBG_1.
26 // 2. Specify the planes blended and blend ratio by G2_SetBlendAlpha.
27 // 3. You can also control blend ratio by G2_ChangeBlendAlpha.
28 //---------------------------------------------------------------------------
29
30 #ifdef SDK_TWL
31 #include <twl.h>
32 #else
33 #include <nitro.h>
34 #endif
35 #include "DEMO.h"
36 #include "data.h"
37
38 static u16 sScrnBuf1[SCREEN_SIZE]; // Buffer for screen data (BG #0)
39 static u16 sScrnBuf2[SCREEN_SIZE]; // Buffer for screen data (BG #1)
40
41 #ifdef SDK_TWL
TwlMain(void)42 void TwlMain(void)
43 #else
44 void NitroMain(void)
45 #endif
46 {
47
48 int eva = 0, evb = 0;
49
50 //---------------------------------------------------------------------------
51 // Initialize:
52 // Enables IRQ interrupts, initializes VRAM, and sets BG #0 for text mode.
53 //---------------------------------------------------------------------------
54 DEMOInitCommon();
55 DEMOInitVRAM();
56 DEMOInitDisplayBG0Only();
57
58 //---------------------------------------------------------------------------
59 // Setting up BG #0 controls:
60 //---------------------------------------------------------------------------
61 G2_SetBG0Control(GX_BG_SCRSIZE_TEXT_256x256,
62 GX_BG_COLORMODE_256,
63 GX_BG_SCRBASE_0x0000, GX_BG_CHARBASE_0x04000, GX_BG_EXTPLTT_01);
64 G2_SetBG0Priority(0);
65 G2_BG0Mosaic(FALSE);
66
67 //---------------------------------------------------------------------------
68 // Setting up BG #1 controls:
69 //---------------------------------------------------------------------------
70 G2_SetBG1Control(GX_BG_SCRSIZE_TEXT_256x256,
71 GX_BG_COLORMODE_256,
72 GX_BG_SCRBASE_0x0800, GX_BG_CHARBASE_0x0c000, GX_BG_EXTPLTT_01);
73
74 G2_SetBG1Priority(1);
75 G2_BG1Mosaic(FALSE);
76
77 //---------------------------------------------------------------------------
78 // Transmitting the character data and the palette data
79 //---------------------------------------------------------------------------
80 GX_LoadBG0Char(d_al_1_schDT, 0, sizeof(d_al_1_schDT));
81 GX_LoadBG1Char(d_al_2_schDT, 0, sizeof(d_al_2_schDT));
82 GX_LoadBGPltt(d_al_1_sclDT, 0, sizeof(d_al_1_sclDT));
83
84 GX_SetVisiblePlane(GX_PLANEMASK_BG0 | GX_PLANEMASK_BG1); // Displays BG #0 and BG #1
85
86 {
87 int i, j;
88
89 for (i = 0; i < 18; i++)
90 {
91 for (j = 0; j < 12; j++)
92 {
93 sScrnBuf1[((i + 3) * 32) + (j + 11)] = (u16)((i * 0x10) + j);
94 }
95 }
96 // Store the data in the main memory, and invalidate the cache.
97 DC_FlushRange(sScrnBuf1, sizeof(sScrnBuf1));
98
99 MI_CpuFill16(sScrnBuf2, 0xffff, SCREEN_SIZE * sizeof(u16));
100
101 for (i = 0; i < 13; i++)
102 {
103 for (j = 0; j < 16; j++)
104 {
105 sScrnBuf2[((i + 6) * 32) + (j + 9)] = (u16)((i * 0x10) + j);
106 }
107 }
108 // Store the data in the main memory, and invalidate the cache.
109 DC_FlushRange(sScrnBuf2, sizeof(sScrnBuf2));
110 }
111
112 // DMA Transfer to BG0 and BG1 screen
113 /* I/O register is accessed using DMA operation, so cache wait is not needed */
114 // DC_WaitWriteBufferEmpty();
115 GX_LoadBG0Scr(sScrnBuf1, 0, sizeof(sScrnBuf1));
116 GX_LoadBG1Scr(sScrnBuf2, 0, sizeof(sScrnBuf2));
117
118 //---------------------------------------------------------------------------
119 // Setting up alpha blending:
120 //
121 // The first plane is BG #0 and the second one is BG #1.
122 // Alpha blending occurs if and only if BG #1 is just behind BG #0.
123 //---------------------------------------------------------------------------
124 G2_SetBlendAlpha(GX_BLEND_PLANEMASK_BG0, // specifies only BG #0
125 GX_BLEND_PLANEMASK_BG1, // specifies only BG #1
126 0, // eva
127 0 // evb
128 );
129
130 DEMOStartDisplay();
131 //---------------------------------------------------------------------------
132 // Main Loop
133 //---------------------------------------------------------------------------
134 while (1)
135 {
136 DEMOReadKey();
137
138 if (DEMO_IS_PRESS(PAD_KEY_UP))
139 eva++;
140 if (DEMO_IS_PRESS(PAD_KEY_DOWN))
141 eva--;
142 if (DEMO_IS_PRESS(PAD_KEY_RIGHT))
143 evb++;
144 if (DEMO_IS_PRESS(PAD_KEY_LEFT))
145 evb--;
146 if (eva < 0x00)
147 eva = 0x00;
148 if (eva > 0x10)
149 eva = 0x10;
150 if (evb < 0x00)
151 evb = 0x00;
152 if (evb > 0x10)
153 evb = 0x10;
154
155 #ifdef SDK_AUTOTEST
156 eva = 0x08;
157 evb = 0x09; // default params for testing.
158 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
159 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
160 EXT_TestScreenShot(100, 0x2A2878AE);
161 EXT_TestTickCounter();
162 #endif //SDK_AUTOTEST
163
164 OS_WaitVBlankIntr(); // Waiting for the end of the VBlank interrupt
165
166 //---------------------------------------------------------------------------
167 // Change the coefficients for alpha blending
168 //---------------------------------------------------------------------------
169 G2_ChangeBlendAlpha(eva, evb);
170 }
171
172 }
173
174 //---------------------------------------------------------------------------
175 // VBlank interrupt function:
176 //
177 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
178 // OS_EnableIrqMask selects IRQ interrupts to enable, and
179 // OS_EnableIrq enables IRQ interrupts.
180 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
181 //---------------------------------------------------------------------------
VBlankIntr(void)182 void VBlankIntr(void)
183 {
184 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Checking VBlank interrupt
185 }
186