// TextureView.cpp : implementation file
//

#include "stdafx.h"
#include "Megatron.h"
#include "TextureView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTextureView

IMPLEMENT_DYNCREATE(CTextureView, CView)

CTextureView::CTextureView()
{
}

CTextureView::~CTextureView()
{
}


BEGIN_MESSAGE_MAP(CTextureView, CView)
	//{{AFX_MSG_MAP(CTextureView)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextureView drawing

void CTextureView::OnDraw(CDC* pDC)
{
//	CDocument* pDoc = GetDocument();
	if (m_image.IsData())
	{
		CBitmap bmp;
		BYTE *pData = m_image.Lock();
		if (pData)
		{
			bmp.CreateBitmap(m_image.GetWidth(),m_image.GetHeight(),1,m_image.GetBpp(),pData);
			// Get the size of the bitmap
			BITMAP bmpInfo;
			bmp.GetBitmap(&bmpInfo);

			// Create an in-memory DC compatible with the
			// display DC we're using to paint
			CDC dcMemory;
			dcMemory.CreateCompatibleDC(pDC);

			// Select the bitmap into the in-memory DC
			CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

			// Find a centerpoint for the bitmap in the client area
			CRect rect;
			GetClientRect(&rect);
			int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
			int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

			// Copy the bits from the in-memory DC into the on-
			// screen DC to actually do the painting. Use the centerpoint
			// we computed for the target offset.
			pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
			 0, 0, SRCCOPY);

			dcMemory.SelectObject(pOldBitmap);
		}
		m_image.UnLock();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CTextureView diagnostics

#ifdef _DEBUG
void CTextureView::AssertValid() const
{
	CView::AssertValid();
}

void CTextureView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTextureView message handlers

void CTextureView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	CRect rc;
	GetClientRect(&rc);
/*	m_edit.Create(WS_CHILD | WS_VISIBLE,rc,this,NULL);
	m_edit.ShowWindow(SW_SHOW);
	m_edit.SetWindowText("Hello texture view");*/
}

void CTextureView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (lHint)
	{
		Stuff::Page *page = (Stuff::Page *)lHint;

		CString imagename = page->GetName();

		// TODO:  How do I load an image from Gos?
		CMegatronApp *pApp = (CMegatronApp *)AfxGetApp();
		CString path = pApp->m_strRunDir+"\\content\\textures\\"+imagename;

		m_image.Delete();

		CString fullname = path+".tga";
		if (gos_DoesFileExist(fullname))
		{
			m_image.Load((char *)(LPCTSTR)fullname);
			Invalidate();
			return;
		}

		fullname = path+".png";
		if (gos_DoesFileExist(fullname))
		{
			m_image.Load((char *)(LPCTSTR)fullname);
			Invalidate();
			return;
		}
	}
}
