summaryrefslogtreecommitdiff
path: root/win/Mfwnd.cpp
blob: 753fc4b9b7d5e661b42d579017003b2422a7ce53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// GLWindow.cpp : implementation file
//

#include "stdafx.h"
#include "LeoCAD.h"
#include "MFWnd.h"
#include "Tools.h"
#include "minifig.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMinifigWnd

CMinifigWnd::CMinifigWnd()
{
	m_pDC = NULL;
}

CMinifigWnd::~CMinifigWnd()
{
}

BEGIN_MESSAGE_MAP(CMinifigWnd, CWnd)
	//{{AFX_MSG_MAP(CMinifigWnd)
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMinifigWnd message handlers

int CMinifigWnd::InitGL() 
{
	m_pDC = new CClientDC(this);
	ASSERT(m_pDC != NULL);

	// Fill in the Pixel Format Descriptor
  PIXELFORMATDESCRIPTOR pfd;
	memset(&pfd,0, sizeof(PIXELFORMATDESCRIPTOR));

  pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);  
  pfd.nVersion = 1;
	pfd.dwFlags  =  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;
	pfd.iPixelType = PFD_TYPE_RGBA;
  pfd.cColorBits = 24;
	pfd.cDepthBits = 24;
  pfd.iLayerType = PFD_MAIN_PLANE;

  int nPixelFormat = pfnwglChoosePixelFormat(m_pDC->m_hDC, &pfd);
	if (nPixelFormat == 0)
		return -1 ;

	if (!pfnwglSetPixelFormat(m_pDC->m_hDC, nPixelFormat, &pfd))
		return -1 ;

	m_pPal = new CPalette;

	if (CreateRGBPalette(m_pDC->m_hDC, &m_pPal))
	{
		m_pDC->SelectPalette(m_pPal, FALSE);
		m_pDC->RealizePalette();
	}
	else
	{
		delete m_pPal;
		m_pPal = NULL;
	}

    // Create a rendering context.
	m_hrc = pfnwglCreateContext(m_pDC->m_hDC);
	if (!m_hrc)
		return -1;

	HDC oldDC = pfnwglGetCurrentDC();
	HGLRC oldRC = pfnwglGetCurrentContext();
	pfnwglShareLists(oldRC, m_hrc);
	pfnwglMakeCurrent (m_pDC->m_hDC, m_hrc);

	pfnwglMakeCurrent (oldDC, oldRC);

	return 0;
}

BOOL CMinifigWnd::OnEraseBkgnd(CDC* /*pDC*/) 
{
	return TRUE;
}

void CMinifigWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	DrawScene(); 
}

void CMinifigWnd::OnDestroy() 
{
	if (m_pPal)
	{
    CPalette palDefault;
		palDefault.CreateStockObject(DEFAULT_PALETTE);
		m_pDC->SelectPalette(&palDefault, FALSE);
		delete m_pPal;
	}

	if (m_hrc)
		pfnwglDeleteContext(m_hrc);
	if (m_pDC)
		delete m_pDC;

	CWnd::OnDestroy();
}

void CMinifigWnd::DrawScene() 
{
	if (m_pPal)
	{
		m_pDC->SelectPalette(m_pPal, FALSE);
		m_pDC->RealizePalette();
	}

	HDC oldDC = pfnwglGetCurrentDC();
	HGLRC oldRC = pfnwglGetCurrentContext();
	pfnwglMakeCurrent (m_pDC->m_hDC, m_hrc);

	RECT rc;
	GetClientRect (&rc);

	m_pFig->Resize (rc.right, rc.bottom);
  m_pFig->Redraw ();

	pfnwglSwapBuffers (m_pDC->m_hDC);
	pfnwglMakeCurrent (oldDC, oldRC);
}