#include "stdafx.h"

#if 0

struct testvideoproc : public RenderThing
{

	D3DTexture *testtex;
	SimpleBuffer buf[256];

	BSMFile *picdata;
	BSMFile *alphadata;
	Surface *surf;
	Surface squig;
	int frame;

	testvideoproc()
	{
		D3DXCreateTextureFromFile(d3ddev,"c:\\shadow.jpg",&testtex);

		frame=0;
		picdata=new BSMFile(0);
		alphadata=new BSMFile(1);
		picdata->Open("bluescreen2.mad");
		alphadata->Open("bluescreen2.mad.alpha");
		surf=new Surface();
		surf->SetSize(256,256);
		squig.LoadFromDisk("squiggle.png");

		
	}

	~testvideoproc()
	{
		RELEASE(testtex);
		delete surf;
		delete picdata;
		delete alphadata;
	}

	int Do(float time, float dt, stereo *audio, CThingDoc *doc)
	{
		
		backbuf->BeginFrame(true,true,-1);		

		picdata->ReadFrame(frame);
		alphadata->ReadFrame(frame);
		frame+=10;

		//d3ddev->SetTexture(0,testtex);
		d3ddev->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
		d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
		SetCam(v3(0,0,0.51f),v3(0,0,0));
		SetFOV(2);
		m44 mymat;
		D3DXMatrixIdentity((D3DXMATRIX*)&mymat);
		SetWorldMat(mymat);
		
		
		CPoint pp;
		GetCursorPos(&pp);
		
		pixel *p = surf->pix;
		
#define CUT 40
		for (int y=0;y<256;y++)
		{
			u8 *pici = picdata->framebuf + (255-y)*512*3;
			u8 *alphai = alphadata->framebuf + (255-y)*512;
			for (int x=0;x<256;x++)
			{
				pixel pp;
				p->r = *pici++;
				p->g = *pici++;
				p->b = *pici++;
				p->a = *alphai++;
				pp=*p;

				int grey=(p->r+p->g*2+p->b)/4;
				p->r+=(grey-p->r)/4;
				p->g+=(grey-p->g)/4;
				p->b+=(grey-p->b)/4;
				//p->r=p->g=p->b=p->g;

				
				{
					int br2 = (pp.g*2+pp.b+pp.r)*pp.a/(4);
					int br=br2/256;
					if (br>255) br=255;					
					
					float sz=1;
					if (br) if ((rand()&255)<br+50)
					{						
						if (br>=CUT) 
						{
							if ((rand()&3)<3)
							{
							
								pp.r=bound(0,pp.r-CUT,255);
								pp.g=bound(0,pp.g-CUT,255);
								pp.b=bound(0,pp.b-CUT,255);
								pp.r=br2/1600;
								pp.g=br2/1600;
								pp.b=br2/1600;
								pp.a/=4;
								sz*=0.0711f;
#define RRR 0.01f
#define TTT 4.f
								
								buf[br%CUT].AddRandomBlob(v3((x-128)/128.f+Random(-RRR,RRR),(y-128)/128.f+Random(-RRR,RRR),0),0x24000000,2.4f*sz,x/256.f-5.f/256.f,y/256.f-5.f/256.f,x/256.f+5.f/256.f,y/256.f+5.f/256.f);
								buf[br].AddBlob(v3((x-128)/128.f+Random(-RRR,RRR),(y-128)/128.f+Random(-RRR,RRR),0),pp.c,0.4f*sz,0,x/256.f-TTT/256.f,y/256.f-TTT/256.f,x/256.f+TTT/256.f,y/256.f+TTT/256.f);
								//if (pp.a>0 && (rand()&3)==0) buf[br].AddRandomBlob(v3((x-128)/128.f,(y-128)/128.f,0),pp.c,0.8f*sz);
							}

						}
						else 
						{
							pp.r/=2;
							pp.g/=2;
							pp.b/=2;
							
							sz*=1.2f;
							buf[br%CUT].AddRandomBlob(v3((x-128)/128.f,(y-128)/128.f,0),pp.c,0.04f*sz);
							pp.a/=2;
							buf[br%CUT].AddRandomBlob(v3((x-128)/128.f,(y-128)/128.f,0),pp.c,0.2f*sz);
							br/=2;
							pp.a/=4;
							buf[br%CUT].AddRandomBlob(v3((x-128)/128.f,(y-128)/128.f,0),pp.c,0.5f*sz);
						}
						
					}
				}
				p++;
			}
			pici+=256*3;
			alphai+=256;			
			
		}

		surf->UpdateToTexture();		

		
		//buf.AddBlob(v3(0,0,0),-1);

		d3ddev->SetTexture(0,squig.tex);
		d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
		d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
		for (int b=0;b<CUT;b++) buf[b].Draw();
		d3ddev->SetTexture(0,surf->tex);
		d3ddev->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
		d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
		for (;b<256;b++) buf[b].Draw();
		
		

		backbuf->EndFrame(false);

		//backbuf->Blt(OPER_SET,255,surf);
		//backbuf->UpdateToRendertarget();

		return 0;
	}

};


/*
UINT filmthreadproc( LPVOID pParam )
{
	
	surf mytex;
	mytex.LoadFromDisk("c:\\dot.png");
	mytex.UpdateFromTexture();
	while ((GetAsyncKeyState(VK_ESCAPE)&1)==0 && forcequit==0)
	{
		
		//backbuf->BeginFrame();
		//backbuf->EndFrame(false);
		backbuf->ClearPix(0x123456);
		backbuf->Blt(OPER_SET,255,&mytex);
		//backbuf->GaussBlur(OPER_SET,255,25,CHAN_ALL);
		backbuf->BoxBlur(25,25,CHAN_ALL);
		backbuf->UpdateToRendertarget();

		Flip();
		_sleep(10);
		curframe++;
		curtime+=DT;
	}
	filmthread=NULL;
	forcequit=2;
	return 0;
}*/

#endif