1 /*---------------------------------------------------------------------------*
2 Project: Dolphin/Revolution gx demo
3 File: G2D-testEditor.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 "G2D-test.h"
15
16 #ifdef _EDITOR
17 #include <demo.h>
18 #include <math.h>
19 #include <string.h>
20
21 /*---------------------------------------------------------------------------*
22 Defines
23 *---------------------------------------------------------------------------*/
24
25
26 /*---------------------------------------------------------------------------*
27 Global Variables
28 *---------------------------------------------------------------------------*/
29
30 s32 nMapX, nMapY;
31 s32 nStampLeft, nStampWidth = 1;
32 s32 nStampTop, nStampHeight = 1;
33 s32 nStampStartX, nStampStartY;
34 s32 nStampOffsetX, nStampOffsetY;
35 u8 aEditStamp[8192];
36 u8 aEditBack[8192];
37
38 /*---------------------------------------------------------------------------*
39 Local Function Declarations
40 *---------------------------------------------------------------------------*/
41
42 static void DrawCursor ( void );
43
44 #endif // ifdef _EDITOR
45
46 /*---------------------------------------------------------------------------*
47 Name: MapEditor
48
49 Description: Logic for map editor mode
50 (uses ship position variables for cursor position)
51
52 Arguments: none
53
54 Returns: none
55 *---------------------------------------------------------------------------*/
MapEditor(G2DLayer * layer)56 void MapEditor( G2DLayer *layer )
57 {
58 #ifdef _EDITOR
59
60 s8 nDX, nDY;
61 static s8 nOldDX = 0;
62 static s8 nOldDY = 0;
63 s32 nMapIdx, nIdx;
64 u8 *buf = aEditStamp;
65 s32 nX, nY, nZ;
66 s32 nOldMapX, nOldMapY;
67
68 nOldMapX= nMapX;
69 nOldMapY= nMapY;
70 nDX = 0;
71 if (DEMOPadGetDirsNew(0) & DEMO_STICK_RIGHT)
72 {
73 nDX = 1;
74 }
75 else if (DEMOPadGetDirsNew(0) & DEMO_STICK_LEFT)
76 {
77 nDX = -1;
78 }
79
80 nDY = 0;
81 if (DEMOPadGetDirsNew(0) & DEMO_STICK_DOWN)
82 {
83 nDY = 1;
84 }
85 else if (DEMOPadGetDirsNew(0) & DEMO_STICK_UP)
86 {
87 nDY = -1;
88 }
89
90 if (nButtons & PAD_BUTTON_B)
91 {
92 poShip.rPosX += (f32)(nDX * layer->nTileWidth);
93 poShip.rPosY += (f32)(nDY * layer->nTileHeight);
94 }
95 else
96 {
97 if ((nDX) && (nOldDX == 0))
98 {
99 poShip.rPosX += (f32)(nDX * layer->nTileWidth);
100 }
101
102 if ((nDY) && (nOldDY == 0))
103 {
104 poShip.rPosY += (f32)(nDY * layer->nTileHeight);
105 }
106 }
107
108 nOldDX = nDX;
109 nOldDY = nDY;
110
111 if (poShip.rPosX >= rWorldWidth) { poShip.rPosX -= rWorldWidth; }
112 else if (poShip.rPosX < 0) { poShip.rPosX += rWorldWidth; }
113 if (poShip.rPosY >= rWorldHeight) { poShip.rPosY -= rWorldHeight; }
114 else if (poShip.rPosY < 0) { poShip.rPosY += rWorldHeight; }
115
116 nMapX = (s32)(poShip.rPosX / layer->nTileWidth);
117 nMapY = (s32)(poShip.rPosY / layer->nTileHeight);
118 nMapIdx = (nMapY<<layer->nHS) + nMapX;
119
120 if (nButtons & PAD_BUTTON_X)
121 {
122 if ((~nOldButtons) & PAD_BUTTON_X)
123 {
124 nStampStartX = nMapX;
125 nStampStartY = nMapY;
126 nStampOffsetX = 0;
127 nStampOffsetY = 0;
128 }
129 else
130 {
131 /* If there was a change in map coordinates, we should add
132 * that change to the stamp offset
133 */
134 if (nMapX != nOldMapX)
135 {
136 nStampOffsetX += nDX;
137 }
138 if (nMapY != nOldMapY)
139 {
140 nStampOffsetY += nDY;
141 }
142 }
143
144 if (nStampOffsetX < 0)
145 {
146 nStampLeft = nStampStartX + nStampOffsetX;
147 nStampWidth = 1 - nStampOffsetX;
148 }
149 else
150 {
151 nStampLeft = nStampStartX;
152 nStampWidth = 1 + nStampOffsetX;
153 }
154
155 if (nStampOffsetY < 0)
156 {
157 nStampTop = nStampStartY + nStampOffsetY;
158 nStampHeight = 1 - nStampOffsetY;
159 }
160 else
161 {
162 nStampTop = nStampStartY;
163 nStampHeight = 1 + nStampOffsetY;
164 }
165
166 if (nStampWidth > nMapWidth)
167 {
168 nStampWidth = nMapWidth;
169 }
170
171 if (nStampHeight > nMapHeight)
172 {
173 nStampHeight = nMapHeight;
174 }
175
176 /* Copy block of tiles for use as a stamp */
177 buf = aEditStamp;
178 for(nY = 0; nY<nStampHeight; nY++)
179 {
180 nZ = ((nY + nStampTop) & (nMapHeight-1)) * nMapWidth;
181 for(nX = 0; nX<nStampWidth; nX++)
182 {
183 *buf++ = map[nZ + ((nX + nStampLeft) & (nMapWidth-1))];
184 }
185 }
186 }
187
188 if ((nStampWidth == 1) && (nStampHeight == 1))
189 {
190 nIdx = (s32)(strchr(sUsedTiles, aEditStamp[0] + ' ') - sUsedTiles);
191
192 if (((~nOldButtons & PAD_TRIGGER_L) | (nButtons & PAD_BUTTON_B)) &&
193 (nButtons & PAD_TRIGGER_L))
194 {
195 if (nIdx == 0)
196 {
197 nIdx = USED_TILES;
198 }
199 nIdx--;
200
201 aEditStamp[0] = (u8)(sUsedTiles[nIdx] - ' ');
202 }
203
204 if (((~nOldButtons & PAD_TRIGGER_R) | (nButtons & PAD_BUTTON_B)) &&
205 (nButtons & PAD_TRIGGER_R))
206 {
207 nIdx++;
208 if (nIdx == USED_TILES)
209 {
210 nIdx = 0;
211 }
212
213 aEditStamp[0] = (u8)(sUsedTiles[nIdx] - ' ');
214 }
215 }
216
217 if (nButtons & PAD_BUTTON_A)
218 {
219 s32 nStaticX = nStampLeft + nMapX - nStampStartX - nStampOffsetX;
220 s32 nStaticY = nStampTop + nMapY - nStampStartY - nStampOffsetY;
221
222 buf = aEditStamp;
223
224 /* Copy stamp into map */
225
226 for(nY = 0; nY<nStampHeight; nY++)
227 {
228 nZ = ((nY + nStaticY) & (nMapHeight-1)) * nMapWidth;
229 for(nX = 0; nX<nStampWidth; nX++)
230 {
231 map[nZ + ((nX + nStaticX) & (nMapWidth-1))] = *buf++;
232 }
233 }
234 }
235
236 #else
237 #pragma unused (layer)
238 #endif // ifdef _EDITOR
239 }
240
241
242 /*---------------------------------------------------------------------------*
243 Name: SaveMap
244
245 Description: Saves map data to a file
246
247 Arguments: none
248
249 Returns: none
250 *---------------------------------------------------------------------------*/
SaveMap(G2DLayer * layer)251 void SaveMap( G2DLayer *layer )
252 {
253 #ifdef _EDITOR
254
255 FILE *fpMap = fopen("map.c", "wb");
256 s32 nI, nJ;
257 s32 nMapIdx;
258
259 nMapIdx = (nMapY<<layer->nHS) + nMapX;
260 map[nMapIdx] = (u8)('*'-' '); // "*' indicates the start position of the ship
261
262 for (nJ=0; nJ<nMapHeight; nJ++)
263 {
264 fprintf(fpMap, "\"");
265 for (nI=0; nI<nMapWidth; nI++)
266 {
267 fprintf(fpMap, "%c", map[(nJ<<layer->nHS)+nI] + ' ');
268 }
269 fprintf(fpMap, "\"\n");
270 }
271
272 map[nMapIdx] = '.'-' ';
273
274 fclose(fpMap);
275
276 #else
277 #pragma unused (layer)
278 #endif // ifdef _EDITOR
279 }
280
281
282 /*---------------------------------------------------------------------------*
283 Name: DrawCursor
284
285 Description: Draw the map-editor cursor
286
287 Arguments: none
288 Returns: none
289 *---------------------------------------------------------------------------*/
DrawCursor(void)290 static void DrawCursor( void )
291 {
292 #ifdef _EDITOR
293
294 s32 nX, nY, nW, nH;
295
296 nX = lyrBack.nTileWidth * (nStampLeft + nMapX - nStampStartX - nStampOffsetX);
297 nY = lyrBack.nTileHeight * (nStampTop + nMapY - nStampStartY - nStampOffsetY);
298 nW = lyrBack.nTileWidth * nStampWidth;
299 nH = lyrBack.nTileHeight * nStampHeight;
300
301 /* Draw a white box around the position of the cursor */
302
303 // Set Position Params
304 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XY, GX_F32, 0); //PositionShift);
305 GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
306
307 // Set Color for Vertex Format 0
308 GXSetNumTexGens(0);
309 GXSetNumChans(1);
310 GXSetVtxDesc(GX_VA_TEX0, GX_NONE);
311 GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
312 GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
313 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGB, GX_RGB8, 0);
314 GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT);
315
316
317 GXBegin(GX_LINESTRIP, GX_VTXFMT0, 3);
318 GXPosition2f32( (f32)(nX + 4), (f32)nY );
319 GXColor3u8( 255, 255, 255 );
320 GXPosition2f32( (f32)nX, (f32)nY );
321 GXColor3u8( 255, 255, 255 );
322 GXPosition2f32( (f32)nX, (f32)(nY + 4) );
323 GXColor3u8( 255, 255, 255 );
324 GXEnd();
325
326 GXBegin(GX_LINESTRIP, GX_VTXFMT0, 3);
327 GXPosition2f32( (f32)(nX + nW - 4), (f32)nY );
328 GXColor3u8( 255, 255, 255 );
329 GXPosition2f32( (f32)(nX + nW), (f32)nY );
330 GXColor3u8( 255, 255, 255 );
331 GXPosition2f32( (f32)(nX + nW), (f32)(nY + 4) );
332 GXColor3u8( 255, 255, 255 );
333 GXEnd();
334
335 GXBegin(GX_LINESTRIP, GX_VTXFMT0, 3);
336 GXPosition2f32( (f32)(nX + 4), (f32)(nY + nH) );
337 GXColor3u8( 255, 255, 255 );
338 GXPosition2f32( (f32)nX, (f32)(nY + nH) );
339 GXColor3u8( 255, 255, 255 );
340 GXPosition2f32( (f32)nX, (f32)(nY + nH - 4) );
341 GXColor3u8( 255, 255, 255 );
342 GXEnd();
343
344 GXBegin(GX_LINESTRIP, GX_VTXFMT0, 3);
345 GXPosition2f32( (f32)(nX + nW - 4), (f32)(nY + nH) );
346 GXColor3u8( 255, 255, 255 );
347 GXPosition2f32( (f32)(nX + nW), (f32)(nY + nH) );
348 GXColor3u8( 255, 255, 255 );
349 GXPosition2f32( (f32)(nX + nW), (f32)(nY + nH - 4) );
350 GXColor3u8( 255, 255, 255 );
351 GXEnd();
352 #endif // ifdef _EDITOR
353 }
354
355
356 /*---------------------------------------------------------------------------*
357 Name: RenderEditorMode
358
359 Description: Renders the
360
361 Arguments: none
362
363 Returns: none
364 *---------------------------------------------------------------------------*/
RenderEditorMode(s8 * aSortBuffer)365 void RenderEditorMode( s8 *aSortBuffer )
366 {
367 #ifdef _EDITOR
368
369 s32 nStaticX = nStampLeft + nMapX - nStampStartX - nStampOffsetX;
370 s32 nStaticY = nStampTop + nMapY - nStampStartY - nStampOffsetY;
371 s32 nMapWidth = 1<<lyrBack.nHS;
372 s32 nMapHeight = 1<<lyrBack.nVS;
373 s32 nX, nY, nZ;
374 u8 *buf;
375
376 /* Copy from map to back buffer */
377 buf = aEditBack;
378 for(nY = 0; nY<nStampHeight; nY++)
379 {
380 nZ = ((nY + nStaticY) & (nMapHeight-1)) * nMapWidth;
381 for(nX = 0; nX<nStampWidth; nX++)
382 {
383 *buf++ = map[nZ + ((nX + nStaticX) & (nMapWidth-1))];
384 }
385 }
386
387 /* Copy stamp into map */
388 buf = aEditStamp;
389 for(nY = 0; nY<nStampHeight; nY++)
390 {
391 nZ = ((nY + nStaticY) & (nMapHeight-1)) * nMapWidth;
392 for(nX = 0; nX<nStampWidth; nX++)
393 {
394 map[nZ + ((nX + nStaticX) & (nMapWidth-1))] = *buf++;
395 }
396 }
397
398 G2DDrawLayer( &lyrBack, aSortBuffer );
399
400 /* Copy from back buffer into map */
401 buf = aEditBack;
402 for(nY = 0; nY<nStampHeight; nY++)
403 {
404 nZ = ((nY + nStaticY) & (nMapHeight-1)) * nMapWidth;
405 for(nX = 0; nX<nStampWidth; nX++)
406 {
407 map[nZ + ((nX + nStaticX) & (nMapWidth-1))] = *buf++;
408 }
409 }
410
411 DrawCursor();
412
413 #else
414 #pragma unused (aSortBuffer)
415 #endif // ifdef _EDITOR
416 }
417