
#include "stdafx.h"
#include "BaseFunc.h"
#include "../GLDef.h"
#include "../Math/math.h"

TRGBf GetRGBf(float r, float g, float b)
{
	TRGBf color = {r, g, b};
	return color;
}

TPoint GetTPoint(float x,float y)
{
	TPoint ret = {x, y};
	return ret;
}

TVel W_GetVel(float w, float h)
{
	TVel ret = {w, h};
	return ret;
}

void W_Ortho()
{
	glDisable(GL_DEPTH_TEST);
	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(viewport[0],viewport[2],viewport[3],viewport[1]);
		
	glMatrixMode(GL_MODELVIEW);// Výběr matice
	glPushMatrix();// Uložení matice
	glLoadIdentity();// Reset matice	
}
void W_Proj()
{
	glMatrixMode(GL_PROJECTION);// Výběr projekční matice
	glPopMatrix();// Obnovení uložené projekční matice
	glMatrixMode(GL_MODELVIEW);// Výběr matice modelview
	glPopMatrix();// Obnovení uložené modelview matice
	glEnable(GL_DEPTH_TEST);
}

std::string W_GetPripona(std::string name, char znak)
{
	int delka = name.size();
	char koncovka[255];
	int num_kon=0;

	for(int i=delka-1;i >= 0;i--)
	{
		//koncovka[num_kon] = name[i];
		if(name[i] == znak) 
		{
			register k=i+1;
			while(name[k] != '\0')
			{
				koncovka[num_kon] = name[k];
				num_kon++;
				k++;
			}
			break;
		}
	}
	koncovka[num_kon] = '\0';
	return koncovka;
}

//////////////////////////////////////////////////////////////////////////
// _PrevedRectNaScreen - 
//////////////////////////////////////////////////////////////////////////
TRect W_PrevedRectNaScreen(TRect rect, int screenX, int screenY)
{
	if(screenX == 640 && screenY == 480) return rect;
	float delitel_x = (float)screenX/640.0f;
	float delitel_y = (float)screenY/480.0f;

	rect.x		*=delitel_x; 
	rect.width	*=delitel_x; 
	rect.y		*=delitel_y; 
	rect.height *=delitel_y; 
	return rect;
}

int W_PrevedIntXNaScreen(int x, int screenX)
{
	if(screenX == 640) return x;
	float delitel_x = (float)screenX/640.0f;
	x *= delitel_x; 
	return x;
}

int W_PrevedIntYNaScreen(int y, int screenY)
{
	if(screenY == 480) return y;
	float delitel_y = (float)screenY/480.0f;
	y *= delitel_y; 
	return y;
}

TAshlar W_PrevedBBoxNaAshlar(TBBox b)
{
	TAshlar ret;
	ret.x = b.xMin;
	ret.y = b.yMin;
	ret.z = b.zMin;

	ret.width = (b.xMax+ MATH_abs( b.xMin) );
	ret.height = (b.yMax+MATH_abs( b.yMin ) );
	ret.size_z = (b.zMax+MATH_abs( b.zMin ) );

	return ret;
}

std::string W_GetStringPlusInt(std::string t, int v)
{
	std::string ret = t;
	char string[255];
	sprintf( string, "%d", v );
	ret += string;
	return ret;
}

int W_SpecRand(int i)
{
	if(i >= 0) return (rand()%i);
	return ((rand()%i)+i);
}

int W_Spec2Rand(int A,int B)
{
	int r = W_SpecRand(B-A);
	return (A+r);
}

void W_SetColor( TRGBf &c )
{
	glColor3f( c.r, c.g, c.b );
}

std::string W_GetString(const char * fmt, ...) 
{
	char text[256];
	va_list ap;
	if (fmt == NULL) return std::string("");

	va_start(ap, fmt);
	vsprintf(text, fmt, ap);
	va_end(ap);

	return text;
} 

void W_NaplnPole( float *cil, const float *zdroj, int kol )
{
	for(register i=0; i<kol; i++)
	{
		cil[i] = zdroj[i];
	}
}