#pragma once

#include <vfw.h>

#ifndef SAFE_DELETE
#define SAFE_DELETE(p)          { if (p) { delete (p);     (p)=NULL; } }
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p)    { if (p) { delete[] (p);   (p)=NULL; } }
#endif
#ifndef SAFE_DELETE_OBJECT
#define SAFE_DELETE_OBJECT(p)   { if (p) { DeleteObject(p); (p)=NULL; } }
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p)         { if (p) { (p)->Release(); (p)=NULL; } }
#endif
#ifndef SAFE_RELEASE_C
#define SAFE_RELEASE_C(p)       { if (p) { (p).Release(); } }
#endif

class CSprScr : public CWnd
{
		DECLARE_DYNAMIC(CSprScr)
		static const DWORD  ScreenView_ColorPalettes[16][4];
		static const DWORD  MonochromePalette[2][4];
		static const DWORD  AdaptMonochromePalette[2][4];

		BYTE    *m_pBuffer;
		size_t  m_nBufSize;

		int     m_PaletteNum;
		int     m_nScale;
		int     m_cx, m_cy;
		int     m_orig_cx, m_orig_cy;

		BOOL    m_bColorMode;
		BOOL    m_bAdapt;
		BOOL    m_bBusy;
		BOOL    m_bChangeMode;

		DWORD   *m_colTable32;
		DWORD   *m_monTable32;
		DWORD   *m_bits;

		HWND    m_hwndScreen;  // Screen View window handle
		HDRAWDIB m_hdd;
		HBITMAP m_hbmp;
		BITMAPINFO m_bmpinfo;

	public:
		CSprScr();
		virtual ~CSprScr(void);
		void DrawScreen();
		void ReDrawScreen();
		void AdjustLayout(int cx, int cy, int ysp);

		void SetParameters(BYTE *buffer, size_t size, int scale)
		{
			m_pBuffer = buffer; m_nBufSize = size; SetScale(scale);
		}

		inline const size_t GetBufferSize()
		{
			return m_nBufSize;
		}

		void SetScale(int scale);

		inline const int GetScale()
		{
			return m_nScale;
		}

		void SetPalette(int palette);

		inline const int GetPalette()
		{
			return m_PaletteNum;
		}

		void SetAdaptMode(BOOL bFlag);

		inline const BOOL IsAdaptMode()
		{
			return m_bAdapt;
		}

		void SetColorMode(BOOL bColorMode = TRUE);

		inline const BOOL IsColorMode()
		{
			return m_bColorMode;
		}

	protected:
		void ScreenView_Init();
		void ScreenView_Done();
		void InitColorTables();
		void PrepareScreenRGB32();
		void CalcOrigSize();
		virtual BOOL PreCreateWindow(CREATESTRUCT &cs);
		afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
		afx_msg void OnDestroy();
		afx_msg BOOL OnEraseBkgnd(CDC *pDC);
		DECLARE_MESSAGE_MAP()
};

