#include extreme\_ex_drm;

init()
{
	level.ex_mylogo_showimage = [[level.ex_drm]]("ex_mylogo_showimage", 0, 0, 1, "int");
	level.ex_mylogo_image_posx = [[level.ex_drm]]("ex_mylogo_image_posx", 590, 0, 640, "int");
	level.ex_mylogo_image_posy = [[level.ex_drm]]("ex_mylogo_image_posy", 118, 0, 480, "int");
	level.ex_mylogo_image_sizex = [[level.ex_drm]]("ex_mylogo_image_sizex", 80, 0, 512, "int");
	level.ex_mylogo_image_sizey = [[level.ex_drm]]("ex_mylogo_image_sizey", 80, 0, 512, "int");
	level.ex_mylogo_image_transp = [[level.ex_drm]]("ex_mylogo_image_transp", 0, 0, 9, "int");
	level.ex_mylogo_showtext = [[level.ex_drm]]("ex_mylogo_showtext", 0, 0, 1, "int");
	level.ex_mylogo_text_posx = [[level.ex_drm]]("ex_mylogo_text_posx", 590, 0, 640, "int");
	level.ex_mylogo_text_posy = [[level.ex_drm]]("ex_mylogo_text_posy", 75, 0, 480, "int");
	level.ex_mylogo_text_transp = [[level.ex_drm]]("ex_mylogo_text_transp", 0, 0, 9, "int");
	level.ex_mylogo_looptime = [[level.ex_drm]]("ex_mylogo_looptime", 120, 0, 3600, "int");
	level.ex_mylogo_fadewait = [[level.ex_drm]]("ex_mylogo_fadewait", 10, 0, 3600, "int");

	if(level.ex_mylogo_showimage) [[level.ex_PrecacheShader]]("logo");
	if(level.ex_mylogo_showtext) [[level.ex_PrecacheString]](&"CUSTOM_LOGOTEXT");
}

start()
{
	if(level.ex_mylogo_showimage || level.ex_mylogo_showtext)
	{
		level.ex_mylogo_active = true;
		level thread myLogoMonitor();
		level thread myLogoShow();
	}
}

myLogoMonitor()
{
	level waittill("ex_gameover");
	level.ex_mylogo_active = false;
}

myLogoShow()
{
	if(level.ex_mylogo_showtext && !isdefined(level.mylogo_txt))
	{
		level.mylogo_txt = newHudElem();
		level.mylogo_txt.x = level.ex_mylogo_text_posx;
		level.mylogo_txt.y = level.ex_mylogo_text_posy;
		level.mylogo_txt.alignX = "center";
		level.mylogo_txt.alignY = "middle";
		level.mylogo_txt.archived = false;
		level.mylogo_txt.fontScale = 1;
		level.mylogo_txt.sort = 3;
		level.mylogo_txt.alpha = 0;
		level.mylogo_txt setText(&"CUSTOM_LOGOTEXT");
	}

	if(level.ex_mylogo_showimage && !isdefined(level.mylogo_img))
	{
		level.mylogo_img = newHudElem();
		level.mylogo_img.x = level.ex_mylogo_image_posx;
		level.mylogo_img.y = level.ex_mylogo_image_posy;
		level.mylogo_img.alignX = "center";
		level.mylogo_img.alignY = "middle";
		level.mylogo_img.archived = false;
		level.mylogo_img.sort = 3;
		level.mylogo_img.alpha = 0;
		level.mylogo_img setShader("logo", level.ex_mylogo_image_sizex, level.ex_mylogo_image_sizey);
	}

	// Initial fixed pause
	myLogoWait(20);

	if(!level.ex_mylogo_looptime)
	{
		if(isdefined(level.mylogo_txt))
			level.mylogo_txt.alpha = 1 - (level.ex_mylogo_text_transp / 10);
		if(isdefined(level.mylogo_img))
			level.mylogo_img.alpha = 1 - (level.ex_mylogo_image_transp / 10);
	}

	// Loop until the myLogoMonitor signals it to end (regardless of fading effects)
	while(level.ex_mylogo_active)
	{
		wait(1);
		
		if(level.ex_mylogo_looptime)
		{

			// Fade in
			if(isdefined(level.mylogo_txt))
			{
				level.mylogo_txt fadeOverTime(2);
				level.mylogo_txt.alpha = 1 - (level.ex_mylogo_text_transp / 10);
			}

			if(isdefined(level.mylogo_img))
			{
				level.mylogo_img fadeOverTime(2);
				level.mylogo_img.alpha = 1 - (level.ex_mylogo_image_transp / 10);
			}

			// Wait before fading out again
			myLogoWait(level.ex_mylogo_fadewait);

			// Fade out
			if(isdefined(level.mylogo_txt))
			{
				level.mylogo_txt fadeOverTime(2);
				level.mylogo_txt.alpha = 0;
			}

			if(isdefined(level.mylogo_img))
			{
				level.mylogo_img fadeOverTime(2);
				level.mylogo_img.alpha = 0;
			}

			// Wait (before fading in again)
			myLogoWait(level.ex_mylogo_looptime);
		}
	}

	if(isdefined(level.mylogo_txt)) level.mylogo_txt destroy();
	if(isdefined(level.mylogo_img)) level.mylogo_img destroy();
}

myLogoWait(seconds)
{
	for(i = 0; i < seconds; i++)
	{
		if(level.ex_mylogo_active) wait(1);
			else break;
	}
}
