A
download View.cpp
Language: C++
LOC: 278
Project Info
TableHockey3D(tablehockey)
Server: SourceForge
Type: cvs
...ey\CVSROOT\tablehockey\src\
   Application.cpp
   Application.h
   Camera.cpp
   Camera.h
   Cube.cpp
   Cube.h
   Data.cpp
   Data.h
   Disk.cpp
   Disk.h
   FloatingText.cpp
   FloatingText.h
   Floor.cpp
   Floor.h
   Input.cpp
   Input.h
   Light.cpp
   Light.h
   log.cpp
   log.h
   main.cpp
   mmgr.cpp
   mmgr.h
   Model.cpp
   Model.h
   nommgr.h
   Puck.cpp
   Puck.h
   Pyramid.cpp
   Pyramid.h
   scene.cpp
   scene.h
   SmartPtr.h
   Sound.cpp
   Sound.h
   TableHockey.dsp
   TableHockey.dsw
   TestScene.cpp
   TestScene.h
   text.cpp
   text.h
   TextHelp.cpp
   TextHelp.h
   texture.cpp
   texture.h
   TexturedCube.cpp
   TexturedCube.h
   timer.cpp
   timer.h
   View.cpp
   View.h

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// View.cpp: implementation of the CView class.
//
//////////////////////////////////////////////////////////////////////

#include "View.h"
#include "application.h"

// the various scenes
#include "testscene.h"

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);	// Declaration For WndProc

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CView::CView()
: pScene(NULL),
  hDC(0),
  hRC(0),
  hWnd(0)
{

}

CView::~CView()
{
	delete pScene;
	OutputDebugString("CView destructor\n");
	pApp->pLog->Log("CView destructor called");
}

void CView::Update()
{
	pScene->ProcessInput();
	pScene->Update();
	SwapBuffers(hDC);
}

void CView::Resize()
{
	pApp->pLog->Log("CView::Resize");
	pScene->Resize();
}

bool CView::Init()
{
	// Create Our OpenGL Window
	pApp->pData->Window_Bits = 16;
	pApp->pData->SetDimension(640, 480);
	if (!CreateMainWindow())
	{
		pApp->pLog->Log("CreateWindow failed!");
		return false;
	}

	// Resize window if in fullscreen mode
	// In window mode we will get a WM_SIZE message right
	// after WM_CREATE
	if (bFullscreen)
		Resize();

	if (!ShowTestScene())
		return false;
	
	return true;
}

bool CView::CreateMainWindow()
{
	DWORD		dwExStyle;				// Window Extended Style
	DWORD		dwStyle;				// Window Style
	RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left=(long)0;			// Set Left Value To 0
	WindowRect.right=pApp->pData->Window_Width;	// Set Right Value To Requested Width
	WindowRect.top=(long)0;					// Set Top Value To 0
	WindowRect.bottom=pApp->pData->Window_Height;	// Set Bottom Value To Requested Height

	if (!_RegisterMyClass())
		return false;

	if (bFullscreen)												// Attempt Fullscreen Mode?
	{
		if (!SwitchToFullscreen())
			// couldn't set fullscreen mode and user didn't want to use
			// windowed mode
			return false;
	}

	// Are We Still In Fullscreen Mode?
	if (bFullscreen)
	{
		pApp->pLog->Log("    Entering Fullscreen mode @ %dx%dx%d bits", 
			pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
		dwExStyle=WS_EX_APPWINDOW;								// Window Extended Style
		dwStyle=WS_POPUP;										// Windows Style
		ShowCursor(FALSE);										// Hide Mouse Pointer
	}
	else
	{
		pApp->pLog->Log("    Entering windowed mode %dx%dx%d bits", 
			pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
		dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style
		dwStyle=WS_OVERLAPPEDWINDOW;							// Windows Style
	}

	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size

	// Create The Window
	pApp->pLog->Log("    Creating window...");
	if (!(hWnd=CreateWindowEx(	dwExStyle,							// Extended Style For The Window
								szClassName,						// Class Name
								szProgName,								// Window Title
								dwStyle |							// Defined Window Style
								WS_CLIPSIBLINGS |					// Required Window Style
								WS_CLIPCHILDREN,					// Required Window Style
								0, 0,								// Window Position
								pApp->pData->Window_Width,	// Calculate Window Width
								pApp->pData->Window_Height,	// Calculate Window Height
								NULL,								// No Parent Window
								NULL,								// No Menu
								pApp->hInst,						// Instance
								NULL)))								// Dont Pass Anything To WM_CREATE
	{
		KillWindow();								// Reset The Display
		pApp->pLog->Log("     Window Creation Error");
		MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if (!_SetPixelFormat())
	{
		KillWindow();
		return false;
	}

	if (!(hRC=wglCreateContext(hDC)))				// Are We Able To Get A Rendering Context?
	{
		KillWindow();								// Reset The Display
		pApp->pLog->Log("    Can't Create A GL Rendering Context");
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	pApp->pLog->Log("    GL Rendering Context Created");

	if(!wglMakeCurrent(hDC,hRC))					// Try To Activate The Rendering Context
	{
		KillWindow();								// Reset The Display
		pApp->pLog->Log("    Can't Activate The GL Rendering Context");
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	pApp->pLog->Log("    GL Rendering Context Activated");

	ShowWindow(hWnd,SW_SHOW);						// Show The Window
	pApp->pLog->Log("    Window Displayed");
	SetForegroundWindow(hWnd);						// Slightly Higher Priority
	SetFocus(hWnd);									// Sets Keyboard Focus To The Window

	pApp->pLog->Log("    Initialization of OpenGL Was a Success");
	pApp->pLog->Log("Window Initialization Completed");

	return TRUE;									// Success

}

void CView::KillWindow()
{
	pApp->pLog->Log("Killing window...");
	if (bFullscreen)										// Are We In Fullscreen Mode?
	{
		ChangeDisplaySettings(NULL,0);					// If So Switch Back To The Desktop
		ShowCursor(TRUE);								// Show Mouse Pointer
	}

	if (hRC)											// Do We Have A Rendering Context?
	{
		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
		{
			pApp->pLog->Log("    Release Of DC And RC Failed");
		}

		if (!wglDeleteContext(hRC))						// Are We Able To Delete The RC?
		{
			pApp->pLog->Log("    Release Rendering Context Failed");
		}
		hRC=NULL;										// Set RC To NULL
	}

	if (hDC && !ReleaseDC(hWnd,hDC))					// Are We Able To Release The DC
	{
		pApp->pLog->Log("    Release Device Context Failed");
		hDC=NULL;										// Set DC To NULL
	}

	if (hWnd && !DestroyWindow(hWnd))					// Are We Able To Destroy The Window?
	{
		pApp->pLog->Log("    Could Not Release hWnd");
		hWnd=NULL;										// Set hWnd To NULL
	}

	if (!UnregisterClass(szClassName, pApp->hInst))			// Are We Able To Unregister Class
	{
		pApp->pLog->Log("    Could Not Unregister Class");
	}
	pApp->pLog->Log("    Success");

}

void CView::Exit()
{
	KillWindow();
}

bool CView::_RegisterMyClass()
{
	WNDCLASS wc;

	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
	wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
	wc.cbClsExtra		= 0;									// No Extra Window Data
	wc.cbWndExtra		= 0;									// No Extra Window Data
	wc.hInstance		= pApp->hInst;							// Set The Instance
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
	wc.hbrBackground	= NULL;									// No Background Required For GL
	wc.lpszMenuName		= NULL;									// We Don't Want A Menu
	wc.lpszClassName	= szClassName;							// Set The Class Name

	if (!RegisterClass(&wc))									// Attempt To Register The Window Class
	{
		pApp->pLog->Log("Failed To Register The Window Class");
		return FALSE;											// Return FALSE
	}

	pApp->pLog->Log("    Window Class Registered");
	return true;
}

bool CView::SwitchToFullscreen()
{
	DEVMODE dmScreenSettings;								// Device Mode

	memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
	dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
	dmScreenSettings.dmPelsWidth	= pApp->pData->Window_Width;	// Selected Screen Width
	dmScreenSettings.dmPelsHeight	= pApp->pData->Window_Height;	// Selected Screen Height
	dmScreenSettings.dmBitsPerPel	= pApp->pData->Window_Bits;	// Selected Bits Per Pixel
	dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
	
	// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
	if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
	{
		// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
		if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
		{
			pApp->pLog->Log("    Fullscreen mode %dx%d %d bits not supported", 
				pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
			bFullscreen=FALSE;		// Windowed Mode Selected.  Fullscreen = FALSE
		}
		else
		{
			// Pop Up A Message Box Letting User Know The Program Is Closing.
			pApp->pLog->Log("    Fullscreen mode %dx%d %d bits not supported\n\tClosing program", 
				pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
			MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
			return FALSE;									// Return FALSE
		}
	}

	return true;
}

bool CView::_SetPixelFormat()
{
	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
		1,											// Version Number
		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,							// Must Support Double Buffering
		PFD_TYPE_RGBA,								// Request An RGBA Format
		pApp->pData->Window_Bits,					// Select Our Color Depth
		0, 0, 0, 0, 0, 0,							// Color Bits Ignored
		0,											// No Alpha Buffer
		0,											// Shift Bit Ignored
		0,											// No Accumulation Buffer
		0, 0, 0, 0,									// Accumulation Bits Ignored
		16,											// 16Bit Z-Buffer (Depth Buffer)  
		0,											// No Stencil Buffer
		0,											// No Auxiliary Buffer
		PFD_MAIN_PLANE,								// Main Drawing Layer
		0,											// Reserved
		0, 0, 0										// Layer Masks Ignored
	};
	
	if (!(hDC=GetDC(hWnd)))							// Did We Get A Device Context?
	{
		KillWindow();								// Reset The Display
		pApp->pLog->Log("    Can't Create A GL Device Context");
		MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	pApp->pLog->Log("    GL Device Context Created");

	// Holds The Results After Searching For A Match
	GLuint		PixelFormat;
	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
	{
		KillWindow();								// Reset The Display
		pApp->pLog->Log("    Can't Find A Suitable PixelFormat");
		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	pApp->pLog->Log("    Matching Pixel Format Found");

	if(!SetPixelFormat(hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
	{
		KillWindow();								// Reset The Display
		pApp->pLog->Log("    Can't Set The PixelFormat");
		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}
	pApp->pLog->Log("    Pixel Format Set");
	return true;

}

bool CView::ShowTestScene()
{
	// delete old scene
	if (pScene != NULL)
	{
		delete pScene;
	}

	pScene	= new CTestScene();
	if (!pScene->Init())
	{
		// fatal exit
		KillWindow();
		pApp->pLog->Log("    Initialization of TestScene failed");
		MessageBox(NULL,"Initialization of TestScene Failed.","Fatal Error",MB_OK|MB_ICONEXCLAMATION);
		return false;
	}

	return true;
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us