#include <AfxWin.h>
#include <AfxExt.h>
#define INITGUID
#include <ddraw.h>
#include <d3d.h>
#include <d3drm.h>
#include <d3drmwin.h>
#include "ShowRoomWin.h"
#include "resource.h"
#include "HelpDialog.h"

#pragma comment (lib,"ddraw.lib")
#pragma comment (lib,"d3drm.lib")
#pragma comment (lib,"winmm.lib")


BEGIN_MESSAGE_MAP(ShowRoomWin, RMWin)
	//{{AFX_MSG_MAP(ShowRoomWin)
	ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


LPDIRECT3DRMMESH ShowRoomWin::mesh;
LPDIRECT3DRMTEXTURE ShowRoomWin::texture[15];

BOOL ShowRoomWin::CreateScene()
{
	//-------- TEXTURES ---------
	HRSRC texture_id;
	int t=0;
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE01), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE02), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE03), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE04), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE05), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE06), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE07), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE08), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE09), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE10), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE11), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE12), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE13), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE14), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);
	texture_id=FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE15), "TEXTURE" );
	d3drm->LoadTextureFromResource( texture_id, &texture[t++]);

	// ------- MESH BUILDER --------
	D3DRMLOADRESOURCE resinfo;
	resinfo.hModule=NULL;
	resinfo.lpName=MAKEINTRESOURCE( IDR_BOXMESH );
	resinfo.lpType="MESH";
	LPDIRECT3DRMMESHBUILDER meshbuilder;
	d3drm->CreateMeshBuilder( &meshbuilder );
	meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL );
	meshbuilder->Scale( D3DVALUE(1), D3DVALUE(1), D3DVALUE(.5) );
	ScaleMesh( meshbuilder, D3DVALUE(20) );
	meshbuilder->SetPerspective( TRUE );
	meshbuilder->SetQuality(D3DRMRENDER_UNLITFLAT);
	meshbuilder->SetTexture( texture[0] );

	//--------- WRAP ---------
	D3DRMBOX box;
	meshbuilder->GetBox(&box);
	D3DVALUE width=box.max.x-box.min.x;
	D3DVALUE height=box.max.y-box.min.y;

	LPDIRECT3DRMWRAP wrap;
	d3drm->CreateWrap( D3DRMWRAP_FLAT, NULL,
			D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0),  // wrap origin
			D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0),  // z axis of wrap
			D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0),  // y axis of wrap
			D3DVALUE(0.5), D3DVALUE(0.5),				// texture origin
			D3DDivide(1,width),D3DDivide(1,height),		// texture scale
			&wrap );
	wrap->Apply( meshbuilder );
	wrap->Release();
	wrap=0;

	//------- MESH ------
	meshbuilder->CreateMesh( &mesh );
	meshbuilder->Release();
	meshbuilder=0;

	//------- FRAME --------
	LPDIRECT3DRMFRAME meshframe;	
	d3drm->CreateFrame( scene, &meshframe );
	meshframe->AddVisual( mesh );
	meshframe->SetRotation( scene,
			D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
			D3DVALUE(0.05) );
	meshframe->AddMoveCallback( UpdateTexture, NULL );
	meshframe->Release();
	meshframe=0;

	// -------- LIGHTS --------
	LPDIRECT3DRMLIGHT dlight, alight;

	d3drm->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL,
			D3DVALUE(1.0), D3DVALUE(0), D3DVALUE(0), 
			&dlight);
	d3drm->CreateLightRGB(D3DRMLIGHT_AMBIENT,
			D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0), 
			&alight);

	LPDIRECT3DRMFRAME lightframe;
	d3drm->CreateFrame( scene, &lightframe );
	lightframe->SetOrientation( scene, 
			D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
			D3DVALUE(0), D3DVALUE(1), D3DVALUE(0));
	lightframe->AddLight( dlight );
	lightframe->AddLight( alight );
	lightframe->Release();
	lightframe=0;
	dlight->Release();
	dlight=0;
	alight->Release();
	alight=0;

	//------ CAMERA ----------
	d3drm->CreateFrame( scene, &camera );
	camera->SetPosition( scene, D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50));
	d3drm->CreateViewport( device, camera, 0, 0,	
			device->GetWidth(), device->GetHeight(), 
			&viewport);

	return TRUE;
}

void ShowRoomWin::UpdateTexture(LPDIRECT3DRMFRAME frame, void*, D3DVALUE)
{
	static UINT count;
	int curtex=count%15;
	frame->DeleteVisual( mesh );
	mesh->SetGroupTexture( 0, texture[curtex] );
	frame->AddVisual( mesh );
	count++;
}


void ShowRoomWin::OnHelpAbout() 
{
	HelpDialog dialog;
	dialog.DoModal();
}
