1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     G2D-test.c (Test of 2D API by Paul Donnelly, Nov. 1999)
4 
5   Copyright 1998 - 2006 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 
14 #include <demo.h>
15 #include <math.h>
16 #include <stdio.h>
17 
18 #include "G2D-test.h"
19 
20 /*---------------------------------------------------------------------------*
21   Defines
22  *---------------------------------------------------------------------------*/
23 
24 // Size of sort buffer is based on 1200 16x16 blocks fitting on a
25 // 640x480 screen , plus some extra for when we are at an angle,
26 // and for lots of switching between materials
27 #define SORT_BUFFER         12000
28 
29 /*---------------------------------------------------------------------------*
30    Forward references
31  *---------------------------------------------------------------------------*/
32 
33 void        main                ( void );
34 
35 static void CameraInit          ( void );
36 static void CameraUpdate        ( void );
37 static void DrawInit            ( void );
38 static void DrawTick            ( void );
39 
40 static void MoveClouds          ( void );
41 
42 static void SetupTransforms     ( void );
43 static void DrawSprites         ( void );
44 
45 static void SendParticlePoint   ( Vec *vPoint, u8 colorIndex );
46 static void SendParticleLine    ( Vec *vPoint1, Vec *vDelta, u8 colorIndex );
47 
48 static void PrintIntro          ( void );
49 
50  /*---------------------------------------------------------------------------*
51    Global variables
52  *---------------------------------------------------------------------------*/
53 
54 static u8 *map;
55 
56 static GXTexObj toSpritesRGBA8_1;
57 
58 static G2DSprite sprShip =
59 {
60     0, 0,
61     32, 32,
62     &toSpritesRGBA8_1,
63 };
64 static G2DSprite sprShadow =
65 {
66     64, 0,
67     32, 32,
68     &toSpritesRGBA8_1,
69 };
70 
71 static TPLPalettePtr tpl = NULL;
72 
73 
74 /*---------------------------------------------------------------------------*
75    Application main loop
76  *---------------------------------------------------------------------------*/
main(void)77 void main ( void )
78 {
79     DEMOInit(NULL);
80 
81     DrawInit();         // Define my vertex formats and set array pointers.
82     AnimInit();
83 
84     PrintIntro();
85     DEMOPadRead();      // Read the joystick for this frame
86 
87     // While the quit button is not pressed
88     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
89     {
90         DEMOPadRead();  // Read the joystick for this frame
91 
92         AnimTick();     // Do animation based on input
93 
94         DEMOBeforeRender();
95 
96         DrawTick();     // Draw the model.
97 
98         DEMODoneRender();
99     }
100 
101     OSHalt("End of demo");
102 }
103 
104 /*---------------------------------------------------------------------------*
105    Functions
106  *---------------------------------------------------------------------------*/
107 /*---------------------------------------------------------------------------*
108     Name:           DrawInit
109 
110     Description:    Calls the correct initialization function for the current
111                     model.
112 
113     Arguments:      none
114 
115     Returns:        none
116  *---------------------------------------------------------------------------*/
DrawInit(void)117 static void DrawInit( void )
118 {
119     TPLGetPalette(&tpl, "gxTests/G2D-00.tpl");
120 
121     // Load Sprite
122     TPLGetGXTexObjFromPalette(tpl, &toSpritesRGBA8_1, 2);
123     // Override LOD information
124     GXInitTexObjLOD( &toSpritesRGBA8_1,
125         GX_LINEAR, GX_LINEAR, 0.0F, 0.0F, 0.0F,
126         GX_FALSE, GX_FALSE, GX_ANISO_1 );
127 
128     G2DInitSprite( &sprShip );
129     G2DInitSprite( &sprShadow );
130 
131     // Set gamma to 1.7 since this database has been assuming PC displays
132     GXSetDispCopyGamma( GX_GM_1_7 );
133 
134     InitLevel1( &tpl );
135 }
136 
137 
138 /*---------------------------------------------------------------------------*
139     Name:           MoveClouds
140 
141     Description:    Moves cloud layer
142 
143     Arguments:      none
144     Returns:        none
145  *---------------------------------------------------------------------------*/
MoveClouds(void)146 static void MoveClouds( void )
147 {
148     f32 rWorldWidth, rWorldHeight;
149     static f32 rCloudX = 0.0F;
150     static f32 rCloudY = 0.0F;
151     G2DPosOri poCloudCam;
152 
153     poCloudCam = poCam;
154 
155     poCloudCam.rPosX += rCloudX;
156     poCloudCam.rPosY += rCloudY;
157 
158     rCloudX += 0.5F;
159     rCloudY += 0.25F;
160 
161     rWorldWidth = (f32)(lyrBack.nTileWidth << lyrBack.nHS);
162     rWorldHeight = (f32)(lyrBack.nTileHeight << lyrBack.nVS);
163 
164     if (poCloudCam.rPosX >= rWorldWidth)  { poCloudCam.rPosX -= rWorldWidth; }
165     else if (poCloudCam.rPosX < 0)        { poCloudCam.rPosX += rWorldWidth; }
166     if (poCloudCam.rPosY >= rWorldHeight) { poCloudCam.rPosY -= rWorldHeight; }
167     else if (poCloudCam.rPosY < 0)        { poCloudCam.rPosY += rWorldHeight; }
168 
169     G2DSetCamera( &poCloudCam );
170 }
171 
172 #define SIXTEEN_OVER_PI 5.0929581789406507446042804279205F
173 G2DPosOri poShadow;
174 
175 /*---------------------------------------------------------------------------*
176     Name:           DrawTick
177 
178     Description:    Draw the current model once.
179 
180     Arguments:      none
181     Returns:        none
182  *---------------------------------------------------------------------------*/
DrawTick(void)183 static void DrawTick( void )
184 {
185     s8 aSortBuffer[SORT_BUFFER];
186     map = lyrBack.map;
187 
188     if (nMode == 2) // Map editor mode
189     {
190         RenderEditorMode( aSortBuffer );
191     }
192     else
193     {
194         s32 nFrame = (32 - (s32)(rAng * SIXTEEN_OVER_PI)) & 0x1f;
195 
196         G2DDrawLayer( &lyrBack, aSortBuffer );
197 
198         poShadow = poShip;
199         poShadow.rPosX += 9.0F;
200         poShadow.rPosY += 9.0F;
201         G2DDrawSprite( &sprShadow, &poShadow );
202 
203         sprShip.nTlcT = (u16)((nFrame & 0xf)<<5);
204         sprShip.nTlcS = (u16)((nFrame & 0x10)<<1);
205         G2DInitSprite( &sprShip );
206         G2DDrawSprite( &sprShip,   &poShip );
207         MoveClouds();
208         G2DDrawLayer( &lyrFront, aSortBuffer );
209     }
210 }
211 
212 /*---------------------------------------------------------------------------*
213     Name:           PrintIntro
214 
215     Description:    Prints the directions on how to use this demo.
216 
217     Arguments:      none
218 
219     Returns:        none
220  *---------------------------------------------------------------------------*/
PrintIntro(void)221 static void PrintIntro( void )
222 {
223     OSReport("\n\n");
224     OSReport("************************************************\n");
225     OSReport("G2D-test: test game that uses 2D library\n");
226     OSReport("************************************************\n");
227     OSReport("to quit hit the start button\n");
228     OSReport("\n");
229     OSReport("  Stick X/Y    : move forward/back\n");
230     OSReport("  L/R triggers : move left/right\n");
231     OSReport("  B button     : brake\n");
232     OSReport("  Y button     : move screen\n");
233     OSReport("************************************************\n\n");
234 }
235 
236