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

	Space Impakto DS
	relminator
	http://rel.betterwebber.com


	Cglsprite class


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

#include <nds.h>

#include "Cglsprite.h"

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


******************************************************************************/
void Cglsprite::render(int x, int y)
{
	int x1 = x;
	int y1 = y;
	int x2 = x + width;
	int y2 = y + height;
	
	glBindTexture(0, textureID);	
	glBegin(GL_QUADS);
	
		glTexcoord2i(u_off,v_off);
		glVertex2i(x1,y1);
		
		glTexcoord2i(u_off,v_off + height);
		glVertex2i(x1,y2);
		
		glTexcoord2i(u_off + width,v_off + height);
		glVertex2i(x2,y2);
		
		glTexcoord2i(u_off + width,v_off);
		glVertex2i(x2,y1);
		
	glEnd();

}

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


******************************************************************************/
void Cglsprite::rotate(int x, int y, int angle)
{
	
	int s_half_x = width/2;
	int	s_half_y = height/2;
	
	int x1 =  -s_half_x;
	int y1 =  -s_half_y;
	
	int x2 =  s_half_x;
	int y2 =  s_half_y;
	
	
	glBindTexture(0, textureID);
	
	glPushMatrix();
	
		glTranslate3f32(x, y, 0);
	
		glRotateZi(angle);
		
		glBegin(GL_QUADS);
			glTexcoord2i(u_off,v_off);
			glVertex2i(x1,y1);
			
			glTexcoord2i(u_off,v_off + height);
			glVertex2i(x1,y2);
			
			glTexcoord2i(u_off + width,v_off + height);
			glVertex2i(x2,y2);
			
			glTexcoord2i(u_off + width,v_off);
			glVertex2i(x2,y1);			
		glEnd();

	glPopMatrix(1);
		

}


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


******************************************************************************/
void Cglsprite::stretch_h(int x, int y, int length_x)
{
	int x1 = x;
	int y1 = y;
	int x2 = x + length_x;
	int y2 = y + height;
	int su = (width/2)-1;
	
	glBindTexture(0, textureID);
	// left
	int x2l = x + su;
	glBegin(GL_QUADS);
	
		glTexcoord2i(u_off,v_off);
		glVertex2i(x1,y1);
		
		glTexcoord2i(u_off,v_off + height);
		glVertex2i(x1,y2);
		
		glTexcoord2i(u_off + su,v_off + height);
		glVertex2i(x2l,y2);
		
		glTexcoord2i(u_off + su,v_off);
		glVertex2i(x2l,y1);
		
	glEnd();
	
	// center
	int x1l = x + su;
	x2l = x2 - su-1; 
	glBegin(GL_QUADS);
	
		glTexcoord2i(u_off + su,v_off);
		glVertex2i(x1l,y1);
		
		glTexcoord2i(u_off + su,v_off + height);
		glVertex2i(x1l,y2);
		
		glTexcoord2i(u_off + su,v_off + height);
		glVertex2i(x2l,y2);
		
		glTexcoord2i(u_off + su,v_off);
		glVertex2i(x2l,y1);
		
	glEnd();
	
	// right
	x1l = x2 - su-1;
	glBegin(GL_QUADS);
	
		glTexcoord2i(u_off + su,v_off);
		glVertex2i(x1l,y1);
		
		glTexcoord2i(u_off + su,v_off + height);
		glVertex2i(x1l,y2);
		
		glTexcoord2i(u_off + width,v_off + height);
		glVertex2i(x2,y2);
		
		glTexcoord2i(u_off + width,v_off);
		glVertex2i(x2,y1);
		
	glEnd();

}

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


******************************************************************************/
void Cglsprite::color(int x, int y, int color)
{
	int x1 = x;
	int y1 = y;
	int x2 = x + width;
	int y2 = y + height;
	
	glBindTexture(0, textureID);
	glColor(color);	
	glBegin(GL_QUADS);
	
		glTexcoord2i(u_off,v_off);
		glVertex2i(x1,y1);
		
		glTexcoord2i(u_off,v_off + height);
		glVertex2i(x1,y2);
		
		glTexcoord2i(u_off + width,v_off + height);
		glVertex2i(x2,y2);
		
		glTexcoord2i(u_off + width,v_off);
		glVertex2i(x2,y1);
		
	glEnd();
	
	glColor(0x7FFF);	

}
