//Copyright (C) 2007 SiegeLord
//See ReadMe.txt and License.txt for copying information

#include "world.h"
#include "neonz.h"
#include "camera.h"

extern SCamera g_strCamera;

SWorld g_strWorld;

vector<vector2d> g_vecStarField;
vector<zColor> g_vecStarColors;

void InitWorld()
{
	g_strWorld.planet = FindBitmap("planet.jpg");
	g_strWorld.star = FindBitmap("star.tga");

	g_vecStarField.reserve(200);
	g_vecStarColors.reserve(200);

//	union
//	{
//		GLubyte bytes[4];
//		zColor color;
//	} color2;

	FOR1(600)
	{
		float rad = Rand(0, g_strWorld.planet_size * 4);
		//rad = g_strWorld.planet_size * 1.1f;
		float theta = Rand(0, 2 * AL_PI);
		//theta = 0;//Rand(0, 2 * AL_PI);
		//if(Rand() > 0.5)
		//	theta = AL_PI;
		g_vecStarField.push_back(vector2d(rad * cosf(theta), rad * sinf(theta)));
		int val = (int)Rand(0, 128.0f);
		g_vecStarColors.push_back(val | val << 8 | val << 16 | val << 24);
	}
}

void DrawWorld()
{
	DrawBitmapStretched(g_strWorld.planet, -g_strWorld.planet->w, -g_strWorld.planet->h, -10, 2048, 2048);
	DrawModeAdd();
	SetCameraMatrix(g_trIdentity);

	vector2d diff;
	g_strCamera.trans.TransformVector(&diff);

	FOR1(600)
	{
		vector2d pos = g_vecStarField[ii];

		float r = pos.GetLength();


		if(r > g_strWorld.planet_size * g_strCamera.zoom)
		{
			pos = g_vecStarField[ii];

			pos.Rotate(g_strCamera.theta);
			pos += diff;

			zColor color;
			if(r - g_strWorld.planet_size * g_strCamera.zoom < 100 * g_strCamera.zoom)
			{
				color = BlendColors(0x00000000, g_vecStarColors[ii], (r - g_strWorld.planet_size) / 10.0f);
			}
			else
				color = g_vecStarColors[ii];
			SetColor(color);
			DrawBitmap(g_strWorld.star, pos.x, pos.y, -10);
			SetColor(0xffffffff);
		}
		SetColor(0xffffffff);
	}
	DrawModeSolid();
	SetCameraMatrix(g_strCamera.trans);
}

float GetMinWorldRadius()
{
	return g_strWorld.planet_size * 1.25;
}

float GetMaxWorldRadius()
{
	return g_strWorld.planet_size * 2;
}
