1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/Sub_CharBg_2
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 BG #2 as affine mode (rotate):
20 //
21 // USAGE:
22 // UP, DOWN, LEFT, RIGHT: Change the center of rotation
23 // L,R: Rotate BG #2
24 //
25 // HOWTO:
26 // 1. Set up the character/palette/screen data the same as Sub_CharBg_1
27 // 2. Set up a matrix and points with G2S_SetBGxAffine
28 //---------------------------------------------------------------------------
29
30 #include <nitro/code32.h> // Avoid byte access problems
31 #ifdef SDK_TWL
32 #include <twl.h>
33 #else
34 #include <nitro.h>
35 #endif
36 #include "DEMO.h"
37 #include "data.h"
38
39 static u8 sScrnBuf[SCREEN_SIZE]; // Buffer for screen data (BG #2)
40
41 #ifdef SDK_TWL
TwlMain(void)42 void TwlMain(void)
43 #else
44 void NitroMain(void)
45 #endif
46 {
47 s16 x_0 = 0, y_0 = 0;
48 s16 x_1 = 0, y_1 = 0;
49
50 u16 rotate = 0;
51
52 //---------------------------------------------------------------------------
53 // Initialize:
54 // Enables IRQ interrupts, initializes VRAM, and sets BG #2 for affine mode
55 //---------------------------------------------------------------------------
56 DEMOInitCommon();
57 DEMOInitVRAM();
58 DEMOInitDisplaySubBG2Only();
59
60 //---------------------------------------------------------------------------
61 // Transmitting the character data and the palette data
62 //---------------------------------------------------------------------------
63 GXS_LoadBG2Char(d_64_256_bg_schDT, 0, sizeof(d_64_256_bg_schDT));
64 GXS_LoadBGPltt(d_64_256_bg_sclDT, 0, sizeof(d_64_256_bg_sclDT));
65
66 {
67 int i, j;
68 for (i = 0; i < 8; i++)
69 {
70 for (j = 0; j < 8; j++)
71 {
72 sScrnBuf[(i * 16) + j] = (u8)((i * 0x10) + j);
73 }
74 }
75 }
76 // Store the data in main memory, and invalidate the cache
77 DC_FlushRange(sScrnBuf, sizeof(sScrnBuf));
78 /* I/O register is accessed using DMA operation, so cache wait is not needed */
79 // DC_WaitWriteBufferEmpty();
80
81 // DMA Transfer to BG #2 screen
82 GXS_LoadBG2Scr(sScrnBuf, 0, sizeof(sScrnBuf));
83
84 DEMOStartDisplay();
85 //---------------------------------------------------------------------------
86 // Main Loop
87 //---------------------------------------------------------------------------
88 while (1)
89 {
90 MtxFx22 mtx;
91 fx16 sinVal, cosVal;
92
93 DEMOReadKey();
94
95 if (DEMO_IS_PRESS(PAD_KEY_UP))
96 y_0 -= 2;
97 if (DEMO_IS_PRESS(PAD_KEY_DOWN))
98 y_0 += 2;
99 if (DEMO_IS_PRESS(PAD_KEY_RIGHT))
100 x_0 += 2;
101 if (DEMO_IS_PRESS(PAD_KEY_LEFT))
102 x_0 -= 2;
103 if (DEMO_IS_PRESS(PAD_BUTTON_L))
104 rotate -= 256;
105 if (DEMO_IS_PRESS(PAD_BUTTON_R))
106 rotate += 256;
107 if (DEMO_IS_TRIG(PAD_BUTTON_SELECT))
108 {
109 x_0 = 32;
110 y_0 = 32;
111 rotate = 0;
112 }
113
114 sinVal = FX_SinIdx(rotate);
115 cosVal = FX_CosIdx(rotate);
116
117 mtx._00 = (fx32)cosVal;
118 mtx._01 = (fx32)sinVal;
119 mtx._10 = -(fx32)sinVal;
120 mtx._11 = (fx32)cosVal;
121
122 OS_WaitVBlankIntr(); // Waiting for the end of the V-Blank interrupt
123
124 //---------------------------------------------------------------------------
125 // Set up affine transformation of BG #2
126 //---------------------------------------------------------------------------
127 G2S_SetBG2Affine(&mtx, // A matrix for rotation and scaling
128 x_0, y_0, // The center of rotation
129 x_1, y_1 // The reference point before rotation and scaling is applied
130 );
131 }
132 }
133
134 //---------------------------------------------------------------------------
135 // V-Blank interrupt function:
136 //
137 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
138 // OS_EnableIrqMask selects IRQ interrupts to enable, and
139 // OS_EnableIrq enables IRQ interrupts.
140 // Notice that you have to call OS_SetIrqCheckFlag to check a V-Blank interrupt.
141 //---------------------------------------------------------------------------
VBlankIntr(void)142 void VBlankIntr(void)
143 {
144 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Checking V-Blank interrupt
145 }
146