// BKScreenDIB.cpp: определяет экспортированные функции для приложения DLL.
//

#include "stdafx.h"
#include "BKScreenDIB.h"


#pragma comment (lib,"Vfw32.lib")

BKEXTERN_C
{
	BKSCRDLL_API BKSCREENHANDLE APIENTRY GetBKScreen()
	{
		return new CScreenDIB;
	}

	CScreenDIB::CScreenDIB(void) : CBKScreen_Shared()
		, m_hdd(nullptr)
		, m_hbmp(nullptr)
		, m_cx(0)
		, m_cy(0)
	{
	}

	CScreenDIB::~CScreenDIB(void)
	{
	}

	void CScreenDIB::BKSS_OnSize(int cx, int cy)
	{
		if ((cy > 0) && (cx > 0))
		{
			m_cx = cx;
			m_cy = cy;
		}
	}

	HRESULT CScreenDIB::BKSS_ScreenView_Init(ScrsParams & sspar, CWnd * pwndScr, CWnd * pwndFullScr)
	{
		CBKScreen_Shared::BKSS_ScreenView_Init(sspar, pwndScr, pwndFullScr);
		m_hdd = DrawDibOpen();
		ASSERT(m_hwndScreen != nullptr);
		SAFE_DELETE_OBJECT(m_hbmp);
		HDC hdc = ::GetDC(m_hwndScreen);
		ZeroMemory(&m_bmpinfo, sizeof(BITMAPINFO));
		m_bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		m_bmpinfo.bmiHeader.biWidth = m_nTextureWidth;
		m_bmpinfo.bmiHeader.biHeight = m_nTextureHeight;
		m_bmpinfo.bmiHeader.biPlanes = 1;
		m_bmpinfo.bmiHeader.biBitCount = 32;
		m_bmpinfo.bmiHeader.biCompression = BI_RGB;
		m_bmpinfo.bmiHeader.biSizeImage = 0;
		m_bmpinfo.bmiHeader.biXPelsPerMeter = 0;
		m_bmpinfo.bmiHeader.biYPelsPerMeter = 0;
		m_bmpinfo.bmiHeader.biClrUsed = 0;
		m_bmpinfo.bmiHeader.biClrImportant = 0;
		m_hbmp = CreateDIBSection(hdc, &m_bmpinfo, DIB_RGB_COLORS, (void **) &m_bits, nullptr, 0);
		::ReleaseDC(m_hwndScreen, hdc);
		sspar.pBits = m_bits;

		if (m_bFullScreen)
		{
			BKSS_SetFullScreenMode();
		}
		else
		{
			BKSS_SetWindowMode();
		}

		return S_OK;
	}

	void CScreenDIB::BKSS_ScreenView_Done()
	{
		SAFE_DELETE_OBJECT(m_hbmp); //удаляем объект
		m_bits = nullptr;
		DrawDibClose(m_hdd);
	}

	void CScreenDIB::BKSS_DrawScreen()
	{
		if (m_bFullScreen)
		{
			HDC hdc = ::GetDC(m_hwndFullScreen);
			DrawDibDraw(m_hdd, hdc,
			            m_rectFSWnd.left, m_rectFSWnd.top, m_rectFSWnd.right, m_rectFSWnd.bottom,
			            &m_bmpinfo.bmiHeader, m_bits,
			            0, 0, m_nTextureWidth, m_nTextureHeight,
			            DDF_FULLSCREEN | DDF_BUFFER | DDF_HALFTONE);
			::ReleaseDC(m_hwndFullScreen, hdc);
		}
		else
		{
			HDC hdc = ::GetDC(m_hwndScreen);
			DrawDibDraw(m_hdd, hdc,
			            0, 0, m_cx, m_cy,
			            &m_bmpinfo.bmiHeader, m_bits,
			            0, 0, m_nTextureWidth, m_nTextureHeight,
			            DDF_BUFFER | DDF_HALFTONE);
			::ReleaseDC(m_hwndScreen, hdc);
		}
	}

	bool CScreenDIB::BKSS_SetFullScreenMode()
	{
		bool bRet = false;

		// Show background window
		if (m_hwndFullScreen)
		{
			m_pwndMain->ShowWindow(SW_HIDE);
			m_bMWHdn = true;
			// Remove caption and border
			SetWindowLongPtr(m_hwndFullScreen, GWL_STYLE, GetWindowLongPtr(m_hwndFullScreen, GWL_STYLE) & (~(WS_CAPTION | WS_BORDER)));
			m_pwndFullScr->SetWindowPos(nullptr, m_rectFullScreen.left, m_rectFullScreen.top, m_rectFullScreen.right, m_rectFullScreen.bottom, SWP_SHOWWINDOW);
			m_bFullScreen = true;
			bRet = true;
		}

		return bRet;
	}

	bool CScreenDIB::BKSS_SetWindowMode()
	{
		//Hide Background window
		if (m_hwndFullScreen)
		{
			if (m_bMWHdn)
			{
				m_pwndMain->ShowWindow(SW_SHOW);
				m_bMWHdn = false;
			}

			m_pwndFullScr->SetWindowPos(nullptr, 0, 0, 0, 0, SWP_HIDEWINDOW);
		}

		m_bFullScreen = false;
		return true;
	}

};
