// ImageView.cpp : implementation file
//

#include "stdafx.h"
#include "Megatron.h"
#include "ImageView.h"
#include "MegatronDoc.h"

#include <imagelib\image.h>
#include <GameOS\ToolOS.hpp>

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

/////////////////////////////////////////////////////////////////////////////
// CImageView

IMPLEMENT_DYNCREATE(CImageView, CView)

CImageView::CImageView()
{
}

CImageView::~CImageView()
{
}


BEGIN_MESSAGE_MAP(CImageView, CView)
	//{{AFX_MSG_MAP(CImageView)
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageView drawing

void CImageView::OnDraw(CDC* pDC)
{
	HBITMAP hBitmap = m_BackBuffer;
	if (hBitmap)
	{
		CDC  memDC;
		int res;
		
		BITMAP bmp;
		res=m_BackBuffer.GetBitmap(&bmp);
		Verify(res);
		
		BOOL bres;
		bres=memDC.CreateCompatibleDC(pDC);
		Verify(bres);
		memDC.SelectObject(&m_BackBuffer);
		CRect hfrct;
		GetClientRect(&hfrct);
		//pDC->SetStretchBltMode(COLORONCOLOR);
		bres=pDC->BitBlt(0,0,hfrct.Width(),hfrct.Height(),NULL,0,0,BLACKNESS);
		bres=pDC->BitBlt(0,0,hfrct.Width(),hfrct.Height(),&memDC,0,0,SRCCOPY);
		Verify(bres);
		memDC.DeleteDC();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CImageView diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CImageView message handlers

void CImageView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	switch (lHint)
	{
	case UPDATE_HINTFILES:
	case UPDATE_BUILDFILES:
		{
			m_BackBuffer.DeleteObject();
			Invalidate();
			break;
		}
	case UPDATE_TEXTUREHINT:
		{
			break;
		}
	case UPDATE_TEXTUREVIEWSELECT:
		{
			m_BackBuffer.DeleteObject();
			Invalidate();
			Image image;


			CPtrList *list = (CPtrList *)pHint;
			if (list->GetCount())
			if (pHint)
			{
				Stuff::Page *page = (Stuff::Page *)list->GetHead();
				if (page)
				{
					CString imagename = page->GetName();
					const char *entry;
					if (page->GetEntry("Alias",&entry))
					{
						imagename = entry;
					}
					CString path = "content\\textures\\"+imagename;

					image.Delete();

					CString fullname = path+".tga";
					if (gos_DoesFileExist(fullname))
					{
						image.Load((char *)(LPCTSTR)fullname);
					}
					else
					{
						fullname = path+".png";
						if (gos_DoesFileExist(fullname))
						{
							image.Load((char *)(LPCTSTR)fullname);
						}
					}
					
					if(image.GetBpp()==32)
						image.MaskTo(RGBMask(0x00ff0000,0x0000ff00,0x000000ff,0xff000000));
					else
						image.MaskTo(RGBMask(0x00ff0000,0x0000ff00,0x000000ff));
				
					CDC  memDC,*hfDC;
					hfDC=GetDC();

					BOOL bres;
					bres=m_BackBuffer.CreateCompatibleBitmap(hfDC,image.GetWidth(),image.GetHeight());
					Verify(bres);

					memDC.CreateCompatibleDC(hfDC);
					memDC.SelectObject(&m_BackBuffer);

					BITMAPINFOHEADER bhd;
					BITMAPINFO       binf;
					bhd.biSize=sizeof(BITMAPINFOHEADER);
					bhd.biWidth=image.GetWidth(); bhd.biHeight=-image.GetHeight();
					bhd.biPlanes=1;
					bhd.biBitCount=image.GetBpp();
					bhd.biCompression=BI_RGB;
					bhd.biSizeImage=0;
					bhd.biClrImportant=0;
					binf.bmiHeader=bhd;

					BYTE *imgdat=(BYTE *)(image.Lock());

					DWORD isize = image.GetWidth()*image.GetHeight()*(image.GetBpp()>>3);
					if (32==image.GetBpp())
					{
						m_BackBuffer.SetBitmapBits(isize,imgdat);
					}
					else
					{
						SetDIBitsToDevice(memDC,0,0,image.GetWidth(),
						image.GetHeight(),0,0,0,image.GetWidth(),imgdat,
						&binf,DIB_RGB_COLORS);  
					}
					image.UnLock();

					memDC.DeleteDC();
					ReleaseDC(hfDC);

					CDocument *pDoc = GetDocument();
					pDoc->UpdateAllViews(this,UPDATE_IMAGESELECT,(CObject *)&image);
				}
			}
			break;
		}
	default:
		{
		}
	}

	
}

BOOL CImageView::OnEraseBkgnd(CDC* pDC) 
{
	HBITMAP hBitmap = m_BackBuffer;
	if (hBitmap)
		return true;
	return CView::OnEraseBkgnd(pDC);
}
