Untitled

 avatar
unknown
plain_text
5 months ago
17 kB
9
Indexable
//TRANSFORMERS .H

// TransformersView.h : interface of the CTransformersView class
//

#pragma once

#include "DImage.h"


class CTransformersView : public CView
{
private:
	int arm1Rotation = 0;
	int arm2Rotation = 0;
	int leg1Rotation = 0;
	int leg2Rotation = 0;
	int bodyRotation = 0;
private:
	DImage images[6];
private:
	void Translate(CDC* pDC, float dX, float dY, bool rightMultiply = false);
	void Rotate(CDC* pDC, float angle, bool rightMultiply = false);
	void Scale(CDC* pDC, float sX, float sY, bool rightMultiply = false);
	void DrawBackground(CDC* pDC, CRect rc);
	void DrawImgTransparent(CDC* pDC, DImage* pImage);
	void DrawArm1(CDC* pDC);
	void DrawArm2(CDC* pDC);
	void DrawLeg1(CDC* pDC);
	void DrawLeg2(CDC* pDC);
	void DrawBody1(CDC* pDC);
	void DrawTransformer(CDC* pDC);
	void TransformHelper(UINT nChar);

protected: // create from serialization only
	CTransformersView() noexcept;
	DECLARE_DYNCREATE(CTransformersView)

// Attributes
public:
	CTransformersDoc* GetDocument() const;

// Operations
public:

// Overrides
public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// Implementation
public:
	virtual ~CTransformersView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};

#ifndef _DEBUG  // debug version in TransformersView.cpp
inline CTransformersDoc* CTransformersView::GetDocument() const
   { return reinterpret_cast<CTransformersDoc*>(m_pDocument); }
#endif

//TRANSFORMERS .CPP

// TransformersView.cpp : implementation of the CTransformersView class
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Transformers.h"
#endif

#include "TransformersDoc.h"
#include "TransformersView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

# define PI 3.14159265358979323846
#define toRad PI / 180.f


// CTransformersView

IMPLEMENT_DYNCREATE(CTransformersView, CView)

BEGIN_MESSAGE_MAP(CTransformersView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()

// CTransformersView construction/destruction

CTransformersView::CTransformersView() noexcept
{
	ASSERT(images[0].Load(CString("res/images/background.jpg")));
	ASSERT(images[1].Load(CString("res/images/leg1.png")));
	ASSERT(images[2].Load(CString("res/images/leg2.png")));
	ASSERT(images[3].Load(CString("res/images/body1.png")));
	ASSERT(images[4].Load(CString("res/images/arm1.png")));
	ASSERT(images[5].Load(CString("res/images/arm2.png")));
}

CTransformersView::~CTransformersView()
{
}

BOOL CTransformersView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CTransformersView drawing
void CTransformersView::Translate(CDC* pDC, float dX, float dY, bool rightMultiply)
{
	XFORM t;
	t.eDx = dX;
	t.eDy = dY;
	t.eM11 = 1.0f;
	t.eM12 = 0;
	t.eM21 = 0;
	t.eM22 = 1.0f;

	pDC->ModifyWorldTransform(&t, rightMultiply ? MWT_RIGHTMULTIPLY : MWT_LEFTMULTIPLY);
}

void CTransformersView::Rotate(CDC* pDC, float angle, bool rightMultiply)
{
	XFORM t;
	t.eM11 = cos(angle);
	t.eM12 = sin(angle);
	t.eM21 = -sin(angle);
	t.eM22 = cos(angle);
	t.eDx = 0.0f;
	t.eDy = 0.0f;

	pDC->ModifyWorldTransform(&t, rightMultiply ? MWT_RIGHTMULTIPLY : MWT_LEFTMULTIPLY);
}

void CTransformersView::Scale(CDC* pDC, float sX, float sY, bool rightMultiply)
{
	XFORM t;
	t.eDx = 0;
	t.eDy = 0;
	t.eM12 = 0;
	t.eM11 = sX;
	t.eM21 = 0;
	t.eM22 = sY;

	pDC->ModifyWorldTransform(&t, rightMultiply ? MWT_RIGHTMULTIPLY : MWT_LEFTMULTIPLY);
}

void CTransformersView::DrawBackground(CDC* pDC, CRect rc)
{
	images[0].Draw(pDC, rc, CRect(0, 0, rc.Width(), rc.Height()));
}

void CTransformersView::DrawImgTransparent(CDC* pDC, DImage* pImage)
{
	ASSERT(pImage->BPP() == 4);

	unsigned char* bits = pImage->GetDIBBits();
	COLORREF clr = RGB(bits[2], bits[1], bits[0]);

	CRect rect(0, 0, pImage->Width(), pImage->Height());
	pImage->DrawTransparent(pDC, rect, rect, clr);
}

void CTransformersView::DrawArm1(CDC* pDC)
{
	Rotate(pDC, arm1Rotation * toRad);
	Translate(pDC, -34, -31);
	DrawImgTransparent(pDC, &(images[4]));

	Translate(pDC, 34, 31);
	Translate(pDC, 210 - 34, 102 - 31);
}

void CTransformersView::DrawArm2(CDC* pDC)
{
	Rotate(pDC, PI - arm2Rotation * toRad);
	Translate(pDC, -23, -61);
	DrawImgTransparent(pDC, &(images[5]));
	Translate(pDC, 23, 61);
}

void CTransformersView::DrawLeg1(CDC* pDC)
{
	Rotate(pDC, leg1Rotation * toRad);
	Translate(pDC, -30, -125);
	DrawImgTransparent(pDC, &(images[1]));

	Translate(pDC, 237, 125);
}

void CTransformersView::DrawLeg2(CDC* pDC)
{
	XFORM old; pDC->GetWorldTransform(&old);

	Rotate(pDC, leg2Rotation * toRad);
	Translate(pDC, -35, -60);
	DrawImgTransparent(pDC, &(images[2]));

	pDC->SetWorldTransform(&old);
}

void CTransformersView::DrawBody1(CDC* pDC)
{
	Rotate(pDC, bodyRotation * toRad); // 2.
	Translate(pDC, -26, -133); // 1.
	DrawImgTransparent(pDC, &(images[3])); // .3

	Translate(pDC, 206, 83); // 4. 
}

void CTransformersView::DrawTransformer(CDC* pDC)
{
	DrawLeg2(pDC);

	DrawLeg1(pDC);

	DrawBody1(pDC);


	XFORM oldTransform; pDC->GetWorldTransform(&oldTransform);

	Rotate(pDC, arm1Rotation * toRad);
	Translate(pDC, 210 - 34, 102 - 31);

	DrawArm2(pDC);

	pDC->SetWorldTransform(&oldTransform);

	DrawArm1(pDC);
}


void CTransformersView::OnDraw(CDC* pDC)
{
	CTransformersDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CRect rect; GetClientRect(&rect);
	int w = rect.Width(), h = rect.Height();

	CBitmap buffer;
	buffer.CreateCompatibleBitmap(pDC, w, h);

	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&buffer);
	memDC.SetGraphicsMode(GM_ADVANCED);

	XFORM oldTransform; memDC.GetWorldTransform(&oldTransform);

	DrawBackground(&memDC, CRect(0, 0, 2000, 1000));
	Translate(&memDC, 400, 600, false);
	DrawTransformer(&memDC);

	memDC.SetWorldTransform(&oldTransform);
	pDC->BitBlt(0, 0, w, h, &memDC, 0, 0, SRCCOPY);
}


// CTransformersView printing

BOOL CTransformersView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTransformersView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTransformersView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CTransformersView diagnostics

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

void CTransformersView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTransformersDoc* CTransformersView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTransformersDoc)));
	return (CTransformersDoc*)m_pDocument;
}
#endif //_DEBUG


// CTransformersView message handlers


void CTransformersView::TransformHelper(UINT nChar)
{
	switch (nChar)
	{
	case 'A':
		arm1Rotation -= 8;
		leg1Rotation -= 3;
		break;
	case 'Q':
		arm1Rotation += 8;
		leg1Rotation += 3;
		break;

	case 'G':
		arm2Rotation -= 10;
		break;
	case 'T':
		arm2Rotation += 10;
		break;

	case 'F':
		leg2Rotation -= 10;
		break;
	case 'R':
		leg2Rotation += 10;
		break;

	case 'S':
		bodyRotation -= 10;
		break;
	case 'W':
		bodyRotation += 10;
		break;

	default:
		break;
	}
}


void CTransformersView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	switch (nChar)
	{
	case VK_RIGHT:
		TransformHelper('A');
		TransformHelper('G');
		TransformHelper('F');
		TransformHelper('S');
		break;
	case VK_LEFT:
		TransformHelper('Q');
		TransformHelper('T');
		TransformHelper('R');
		TransformHelper('W');
		break;
	default:
		TransformHelper(nChar);
		break;
	}

	Invalidate(false);
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
//LAMPA .H

// LampaView.h : interface of the CLampaView class
//
#include "DImage.h"

#pragma once

# define PI 3.14159265358979323846


class CLampaView : public CView
{
private:
	DImage images[9];
private:
	float arm1Rotation = 0.0f;
	float arm2Rotation = 0.0f;
	float headRotation = 0.0f;

private:
	void Translate(CDC* pDC, float dX, float dY, bool rightMultiply = false);
	void Rotate(CDC* pDC, float angle, bool rightMultiply = false);
	void Scale(CDC* pDC, float sX, float sY, bool rightMultiply = false);
	void DrawBackground(CDC* pDC);
	void DrawImgTransparent(CDC* pDC, DImage* pImage);
private:
	void DrawLamp(CDC* pDC, bool bIsShadow);
	void DrawLampShadow(CDC* pDC);;
	void DrawLampBase(CDC* pDC, bool bIsShadow);
	void DrawLampArm1(CDC* pDC, bool bIsShadow);
	void DrawLampArm2(CDC* pDC, bool bIsShadow);
	void DrawLampHead(CDC* pDC, bool bIsShadow);




protected: // create from serialization only
	CLampaView() noexcept;
	DECLARE_DYNCREATE(CLampaView)

// Attributes
public:
	CLampaDoc* GetDocument() const;

// Operations
public:

// Overrides
public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// Implementation
public:
	virtual ~CLampaView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
};

#ifndef _DEBUG  // debug version in LampaView.cpp
inline CLampaDoc* CLampaView::GetDocument() const
   { return reinterpret_cast<CLampaDoc*>(m_pDocument); }
#endif

//LAMPA .CPP

// LampaView.cpp : implementation of the CLampaView class
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Lampa.h"
#endif

#include "LampaDoc.h"
#include "LampaView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include <string>


// CLampaView

IMPLEMENT_DYNCREATE(CLampaView, CView)

BEGIN_MESSAGE_MAP(CLampaView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_KEYUP()
END_MESSAGE_MAP()

// CLampaView construction/destruction

void CLampaView::Translate(CDC* pDC, float dX, float dY, bool rightMultiply)
{
	XFORM t;
	t.eDx = dX;
	t.eDy = dY;
	t.eM11 = 1.0f;
	t.eM12 = 0;
	t.eM21 = 0;
	t.eM22 = 1.0f;

	pDC->ModifyWorldTransform(&t, rightMultiply ? MWT_RIGHTMULTIPLY : MWT_LEFTMULTIPLY);
}

void CLampaView::Rotate(CDC* pDC, float angle, bool rightMultiply)
{
	XFORM t;
	t.eM11 = cos(angle);
	t.eM12 = sin(angle);
	t.eM21 = -sin(angle);
	t.eM22 = cos(angle);
	t.eDx = 0.0f;
	t.eDy = 0.0f;

	pDC->ModifyWorldTransform(&t, rightMultiply ? MWT_RIGHTMULTIPLY : MWT_LEFTMULTIPLY);
}

void CLampaView::Scale(CDC* pDC, float sX, float sY, bool rightMultiply)
{
	XFORM t;
	t.eDx = 0;
	t.eDy = 0;
	t.eM12 = 0;
	t.eM11 = sX;
	t.eM21 = 0;
	t.eM22 = sY;

	pDC->ModifyWorldTransform(&t, rightMultiply ? MWT_RIGHTMULTIPLY : MWT_LEFTMULTIPLY);
}

void CLampaView::DrawBackground(CDC* pDC)
{
	CRect rect;
	GetClientRect(&rect);
	DImage& background = images[4];

	int w = background.Width();
	int h = background.Height();

	int x = rect.Width() / 2;
	int y = rect.Height();
	
	background.Draw(pDC, CRect(0, 0, w, h), CRect(x - w/2, y - h, x + w/2, y));
}

void CLampaView::DrawImgTransparent(CDC* pDC, DImage* pImage)
{
	DImage& image = *pImage;
	ASSERT(image.BPP() == 4);

	unsigned char* bits = image.GetDIBBits();
	COLORREF clr = RGB(bits[2], bits[1], bits[0]);

	//MessageBox(CString(std::to_string(clr).c_str()));

	int w = pImage->Width(), h = pImage->Height();
	pImage->DrawTransparent(pDC, CRect(0, 0, w, h), CRect(0, 0, w, h), clr);
}


void CLampaView::DrawLampBase(CDC* pDC, bool bIsShadow)
{
	if (bIsShadow) {
		DrawImgTransparent(pDC, images + 5);
	} else {
		DrawImgTransparent(pDC, images);
	}

	Translate(pDC, 170, 40);
	pDC->Ellipse(-10, -10, 10, 10);
}

void CLampaView::DrawLampArm1(CDC* pDC, bool bIsShadow)
{
	Rotate(pDC, arm1Rotation);

	Translate(pDC, -58, -61);
	if (bIsShadow) {
		DrawImgTransparent(pDC, images + 6);
	} else {
		DrawImgTransparent(pDC, images + 1);
	}

	Translate(pDC, 309, 61);
}

void CLampaView::DrawLampArm2(CDC* pDC, bool bIsShadow)
{
	if (bIsShadow) {
		DrawImgTransparent(pDC, images + 7);
	} else {
		DrawImgTransparent(pDC, images + 2);
	}
}

void CLampaView::DrawLampHead(CDC* pDC, bool bIsShadow)
{
	Rotate(pDC, PI + headRotation);
	Translate(pDC, -178, -100);
	if (bIsShadow) {
		DrawImgTransparent(pDC, images + 8);
	} else {
		DrawImgTransparent(pDC, images + 3);
	}
	Translate(pDC, 178, 100);
	Rotate(pDC, -PI - headRotation);
}

void CLampaView::DrawLamp(CDC* pDC, bool bIsShadow)
{
	XFORM oldTransform; pDC->GetWorldTransform(&oldTransform);

	DrawLampBase(pDC, bIsShadow);
	DrawLampArm1(pDC, bIsShadow);

	Rotate(pDC, arm2Rotation);
	Translate(pDC, -36, -40);

	Translate(pDC, 272, 40);
	DrawLampHead(pDC, bIsShadow);
	Translate(pDC, -272, -40);

	DrawLampArm2(pDC, bIsShadow);

	//pDC->Ellipse(-10, -10, 10, 10);



	pDC->SetWorldTransform(&oldTransform);
}

void CLampaView::DrawLampShadow(CDC* pDC)
{
	DrawLamp(pDC, true);
}

CLampaView::CLampaView() noexcept
{
	ASSERT(images[0].Load(_T("res/images/base.png")));
	ASSERT(images[1].Load(CString("res/images/arm1.png")));
	ASSERT(images[2].Load(CString("res/images/arm2.png")));
	ASSERT(images[3].Load(CString("res/images/head.png")));
	ASSERT(images[4].Load(CString("res/images/pozadina.jpg")));
	ASSERT(images[5].Load(CString("res/images/base_shadow.png")));
	ASSERT(images[6].Load(CString("res/images/arm1_shadow.png")));
	ASSERT(images[7].Load(CString("res/images/arm2_shadow.png")));
	ASSERT(images[8].Load(CString("res/images/head_shadow.png")));
}

CLampaView::~CLampaView()
{
}

BOOL CLampaView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CLampaView drawing

void CLampaView::OnDraw(CDC* pDC)
{
	CLampaDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;


	CRect rect; GetClientRect(&rect);
	int w = rect.Width();
	int h = rect.Height();

	CBitmap buffer;
	buffer.CreateCompatibleBitmap(pDC, w, h);

	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&buffer);

	memDC.SetGraphicsMode(GM_ADVANCED);

	XFORM oldTransform; memDC.GetWorldTransform(&oldTransform);

	DrawBackground(&memDC);

	Translate(&memDC, 800, h - 200, true);
	memDC.Ellipse(-10, -10, 10, 10);



	Translate(&memDC, 100, 50);
	Scale(&memDC, 1, 0.25f, false);
	Rotate(&memDC, -PI/2);
	Translate(&memDC, -120, -120);
	DrawLampShadow(&memDC);
	Translate(&memDC, 120, 120);
	memDC.Ellipse(-10, -10, 10, 10);
	Rotate(&memDC, PI/2);
	Scale(&memDC, 1, 4.f, false);
	Translate(&memDC, -100, -50);

	//Translate(&memDC, 100, 0, false);
	DrawLamp(&memDC, false);

	memDC.SetWorldTransform(&oldTransform);

	pDC->BitBlt(0, 0, w, h, &memDC, 0, 0, SRCCOPY);
}


// CLampaView printing

BOOL CLampaView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLampaView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLampaView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CLampaView diagnostics

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

void CLampaView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CLampaDoc* CLampaView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLampaDoc)));
	return (CLampaDoc*)m_pDocument;
}
#endif //_DEBUG


// CLampaView message handlers


void CLampaView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	int n = nChar - 48;
	const float deg10 = PI / 18;

	switch (n)
	{
	case 1:
		arm1Rotation += deg10;
		break;
	case 2:
		arm1Rotation -= deg10;
		break;
	case 3:
		arm2Rotation += deg10;
		break;
	case 4:
		arm2Rotation -= deg10;
		break;
	case 5:
		headRotation += deg10;
		break;
	case 6:
		headRotation -= deg10;
		break;
	default:
		break;
	}

	Invalidate(false);

	CView::OnKeyUp(nChar, nRepCnt, nFlags);
}
Editor is loading...
Leave a Comment