/******************************************************************************
*******************************************************************************
	Easy GL2D 
	Sprites example
	Shows a some sprite capabilities of easy GL2D
	
	Relminator (Richard Eric M. Lope BSN RN)
	Http://Rel.Phatcode.Net
	
	Thanks to:
	Patater (Jaeden Amero) for the shuttle sprites

*******************************************************************************
******************************************************************************/ 

#include <nds.h>
#include <stdio.h>
#include <gl2d.h>


// GRIT auto-generated header
// can be found inside the /build directory
#include "shuttle.h"


int main( void )
{

	// Instantiate a glImage object
	glImage  Shuttle[1];  // only a single sprite for now
	
	
    // Set it to my favorite mode (Any mode that supports 3D should work)
    videoSetMode( MODE_5_3D );
   

	// Init console so that we could
	// print some stuff
	consoleDemoInit();
	
	// Set up enough texture memory for our textures
	// Bank A is 128kb. (enough for our needs)
	// since the sprite we're using is just
	// 2 kb 
	vramSetBankA( VRAM_A_TEXTURE );     
    
	// Very important or you'll get black textures
	vramSetBankE(VRAM_E_TEX_PALETTE);  // Allocate VRAM bank for all the palettes
	
	// Initialize GL2D
    glScreen2D();
   
	
	// load our single image
	// sprite.
	// Note that glLoadTileset can also 
	// load single images.
	glLoadTileSet( Shuttle,			// pointer to glImage array
				   64,				// sprite width
				   64,				// sprite height
				   64,				// bitmap image width
				   64,				// bitmap image height
				   GL_RGB16,		// texture type for glTexImage2D() in videoGL.h 16 colors
				   TEXTURE_SIZE_64,	// sizeX for glTexImage2D() in videoGL.h
				   TEXTURE_SIZE_64,	// sizeY for glTexImage2D() in videoGL.h
									// Set texture params setting color 0 as transparent
				   GL_TEXTURE_WRAP_S|GL_TEXTURE_WRAP_T|TEXGEN_OFF|GL_TEXTURE_COLOR0_TRANSPARENT,
				   256,					// Length of the palette to use (256 colors)
				   (u16*)shuttlePal,	// Load our 16 color shuttle palette
			 	   (u8*)shuttleBitmap  // image data generated by GRIT
				 );

	
	// print some stuff
	
	
	iprintf("\x1b[1;1HEasy GL2D Sprites Demo");
	iprintf("\x1b[2;1HRelminator");
	iprintf("\x1b[4;1HHttp://Rel.Phatcode.Net");
	
	iprintf("\x1b[6;1HA demo showing some sprite");
	iprintf("\x1b[7;1Hcapabilities of Easy GL2D");
	
	iprintf("\x1b[ 9;1HSprites by Patater");
	
	
	// calculate the amount of 
	// memory uploaded to VRAM in KB
	int TextureSize = shuttleBitmapLen;
					  
					  
	iprintf("\x1b[15;1HTotal Texture size= %i kb", TextureSize / 1024);
	
	int frame = 0;
	
    while( 1 )
    {
	
		frame++;
		
        // set up GL2D for 2d mode
        glBegin2D();

		
			// call Easy GL2D's 2D rendering functions here

			// Draw some primitives
			
			// fill the whole screen with a gradient box
			glBoxFilledGradient( 0, 0, 255, 191,
								 RGB15(  0, 31,  0 ),
								 RGB15( 31,  0,  0 ),
								 RGB15(  0,  0, 31 ),
								 RGB15( 31, 31, 31 )
							   );
			
			// draw a row of sprites in different flipping
			// modes
			glSprite( 64* 0, 0, GL_FLIP_NONE, Shuttle );
			glSprite( 64* 1, 0, GL_FLIP_V, Shuttle );
			glSpriteScale( 64* 2, 0, floattof32(0.6),GL_FLIP_H, Shuttle );
			glSpriteScaleXY( 64* 3, 0, floattof32(1.2), floattof32(1.6), GL_FLIP_V | GL_FLIP_H, Shuttle );
			
			// draw a row of sprites in colored modes
			glColor( RGB15(31,0,0) );
			glSprite( 64* 0, 64, GL_FLIP_NONE, Shuttle );
			
			glColor( RGB15(0,31,0) );
			glSprite( 64* 1, 64, GL_FLIP_V, Shuttle );
			
			// make the last two sprites translucent
			// giving them a unique Polygon ID for translucency
			// to work.
			glPolyFmt(POLY_ALPHA(16) | POLY_CULL_NONE | POLY_ID(1));
			glColor( RGB15(16,31,0) );
			glSprite( 64* 2, 64, GL_FLIP_H, Shuttle );
			
			glColor( RGB15(0,0,0) );
			glSprite( 64* 3, 64, GL_FLIP_V | GL_FLIP_H, Shuttle );

			// Go back to normal mode
			// POLY_ALPHA(31) is none translucent
			// Again, give it another poly ID
			glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE | POLY_ID(2));
			
			// Also restore color to normal
			glColor( RGB15(31,31,31) );
			glSpriteRotate( 64 * 0 + 32, 128 + 32, frame * 40, GL_FLIP_NONE, Shuttle );
			
			// Scale by Sinlerp
			int scale = sinLerp(frame*60);
			glSpriteRotateScale( 64 * 1 + 32, 128 + 32, -frame * 40, scale, GL_FLIP_NONE, Shuttle );

			// Independently scale the 3rd image
			int scaleX = sinLerp(frame*90);
			int scaleY = sinLerp(frame*30);
			glSpriteRotateScaleXY( 64 * 2 + 32, 128 + 32, frame * 100, scaleX, scaleY,
								   GL_FLIP_NONE, Shuttle
								 );

			// reverse scale the 4th image,
			// make it translucent and
			// do some coloring
			glColor( RGB15(0,31,0) );
			glPolyFmt(POLY_ALPHA(16) | POLY_CULL_NONE | POLY_ID(3));
			glSpriteRotateScaleXY( 64 * 3 + 32, 128 + 32, -frame * 200, scaleX, scaleY,
								   GL_FLIP_NONE, Shuttle
								 );

        // end 2D rendering mode
        glEnd2D();
   
        glFlush(0);                    
        swiWaitForVBlank();
   
    }
   
   
}

