Click here to Skip to main content
15,892,927 members
Articles / Multimedia / OpenGL

OpenGL Win32 AppWizard

Rate me:
Please Sign up or sign in to vote.
4.97/5 (45 votes)
19 May 20033 min read 490.6K   22.7K   115  
This Custom AppWizard for VC++ 6.0 or VC++.NET creates an OpenGL enabled Win32 application suitable for demos and simple games.
// [!output PROJECT_NAME].cpp
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "[!output PROJECT_NAME].h"
#include <stdio.h>


/////////////////////////////////////////////////////////////////////////////
// Global [!output APP_CLASS_NAME] object

[!output APP_CLASS_NAME] gApp;


/////////////////////////////////////////////////////////////////////////////
// [!output APP_CLASS_NAME] construction/destruction

[!output APP_CLASS_NAME]::[!output APP_CLASS_NAME]()
{
[!if CHK_OPENGL]
[!if CHK_SAMPLE]
	m_fAngle = 0;
[!endif]
[!endif]
}

[!output APP_CLASS_NAME]::~[!output APP_CLASS_NAME]()
{
}

//
// PreCreateWindow()
// Called before the main window is created. Override this function to change window creation params.
//
void [!output APP_CLASS_NAME]::PreCreateWindow(WNDCLASSEX &wcex, DWORD &dwStyle, DWORD &dwExStyle, int &x, int &y, int &cx, int &cy)
{
	CApp::PreCreateWindow(wcex, dwStyle, dwExStyle, x, y, cx, cy);

[!if APP_FULLSCREEN]
	// Create a fullscreen window
	dwStyle = WS_POPUP | WS_SYSMENU;
	dwExStyle = WS_EX_TOPMOST;
	cx = GetSystemMetrics(SM_CXSCREEN);
	cy = GetSystemMetrics(SM_CYSCREEN);

[!endif]
[!if APP_ASK_FULLSCREEN]
	// Ask if window should be fullscreen
	if(MessageBox(0, "Run in fullscreen window?", "[!output PROJECT_NAME]", MB_YESNO | MB_ICONQUESTION)==IDYES)
	{
		dwStyle = WS_POPUP | WS_SYSMENU;
		dwExStyle = WS_EX_TOPMOST;
		cx = GetSystemMetrics(SM_CXSCREEN);
		cy = GetSystemMetrics(SM_CYSCREEN);
	}

[!endif]
	// TODO : Modify parameters to change default window style and/or size
}

[!if CHK_OPENGL]
//
// PreCreateRC()
// Called before the OpenGL rendering context is created.
//
void [!output APP_CLASS_NAME]::PreCreateRC(PIXELFORMATDESCRIPTOR &pfd)
{
	CApp::PreCreateRC(pfd);

	// TODO : Modify pfd to change RC settings
}

[!endif]
//
// OnUpdateFPS()
// Called on regular intervals to indicate current frame rate
//
void [!output APP_CLASS_NAME]::OnUpdateFPS(float fFPS)
{	
[!if CHK_FPS]
	static char szFPS[256];
	sprintf(szFPS, "%.1f FPS", fFPS);
	SetWindowText(m_hWnd, szFPS); // Show FPS in caption
[!endif]
}

//
// OnCommand()
// Override this function to handle WM_COMMAND messages sent to the main window.
//
BOOL [!output APP_CLASS_NAME]::OnCommand(int nCmdID, int nEvent)
{
	switch (nCmdID)
	{
		case IDM_EXIT:
			SendMessage(m_hWnd, WM_CLOSE, 0, 0);
			break;

[!if CHK_ABOUT]
		case IDM_ABOUT:
			ShowAboutDialog();
			break;

[!endif]
		default:
			return CApp::OnCommand(nCmdID, nEvent); // Call default handler
	}
	return TRUE;	
}

//
// WindowProc()
// Override this function to handle messages sent to the main window.
//
LRESULT [!output APP_CLASS_NAME]::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	return CApp::WindowProc(hWnd, message, wParam, lParam); // Call default WindowProc
}

//
// Tick()
[!if CHK_OPENGL]
// The applications "heartbeat" function. Called before another frame needs to be drawn. 
// Override this function to calculate new positions for objects in the scene, for instance.
[!else]
// The applications "heartbeat" function. Called when the message queue is empty.
[!endif]
//
BOOL [!output APP_CLASS_NAME]::Tick()
{		
[!if CHK_OPENGL]
[!if CHK_SAMPLE]
	m_fAngle+= 1; // Add some rotation to the cube
[!endif]
	return TRUE; // Return FALSE if the scene should not be redrawn
[!else]
	return FALSE; // Return TRUE if the client area should be invalidated
[!endif]
}

[!if CHK_OPENGL]
//
// InitScene()
// Called when the OpenGL RC has been created. Sets initial state for the OpenGL scene.
//
void [!output APP_CLASS_NAME]::InitScene()
{
	glClearColor([!output TXT_BG_R], [!output TXT_BG_G], [!output TXT_BG_B], 1.0f); //Background color

[!if CHK_SAMPLE]
	// TODO: Replace the following sample code with your initialization code.

	// Activate depth test, lighting and a light source
  glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);

	// Light position
	GLfloat glfLight0[] = {-4.0f, 4.0f, 4.0f, 0.0f};
	glLightfv(GL_LIGHT0, GL_POSITION, glfLight0);

	// Define material parameters
	static GLfloat fSpec[]= {1.0f, 1.0f, 1.0f, 1.0f};
	static GLfloat fShin  = 128.0f;
	
	// Set material parameters
	glMaterialfv(GL_FRONT, GL_SPECULAR, fSpec);
	glMaterialf(GL_FRONT, GL_SHININESS, fShin);

	// Set color
	glEnable(GL_COLOR_MATERIAL);	
	glColor3f(0.0f, 0.1f, 0.6f);
[!else]
	// TODO: Add scene initialization code
[!endif]
}

//
// KillScene()
// Called when the OpenGL RC is about to be destroyed. 
//
void [!output APP_CLASS_NAME]::KillScene()
{
	// TODO: Use KillScene to free resources.
}

//
// DrawScene()
// Called each time the OpenGL scene has to be drawn.
//
void [!output APP_CLASS_NAME]::DrawScene()
{
[!if CHK_SAMPLE]
	// TODO: Replace the following sample code with your code to draw the scene.

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
  glLoadIdentity(); // Load identity matrix
 
	// Position and rotate the camera
  glTranslatef(0.0f, 0.0f, -5.0f);	
	glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
	glRotatef(m_fAngle, 0.0f, 1.0f, 0.0f);

	// Draw a cube
	static GLfloat r = .7f;
	GLCube(-r, -r, -r, r, r, r);

  glFlush();
[!else]
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
  glLoadIdentity(); // Load identity matrix

	// TODO: Render OpenGL scene

  glFlush();
[!endif]
}

[!else]
//
// OnPaint()
// Handles WM_PAINT messages.
//
void [!output APP_CLASS_NAME]::OnPaint(HDC hDC)
{
	// TODO: Draw client area
}
[!endif]

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions