// MainWnd.cpp: implementation of the CMainWnd class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MainWnd.h"
$$IF(opengl)
$$IF(fps)
#include <stdio.h>
$$ENDIF
$$ENDIF
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
$$IF(opengl)
CMainWnd::CMainWnd():
m_hWnd(NULL),
$$IF(sample)
m_fAngle(0.0f),
$$ENDIF
m_hRC(NULL)
{
}
$$ELSE
CMainWnd::CMainWnd():
m_hWnd(NULL)
{
}
$$ENDIF
CMainWnd::~CMainWnd()
{
}
$$IF(opengl)
$$IF(sample)
//
// GLCube()
// Renders a cube.
//
void GLCube(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
{
glBegin(GL_POLYGON);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(x2, y2, z2);
glVertex3f(x1, y2, z2);
glVertex3f(x1, y1, z2);
glVertex3f(x2, y1, z2);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(x2, y2, z1);
glVertex3f(x2, y1, z1);
glVertex3f(x1, y1, z1);
glVertex3f(x1, y2, z1);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(x1, y2, z2);
glVertex3f(x1, y2, z1);
glVertex3f(x1, y1, z1);
glVertex3f(x1, y1, z2);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(x2, y2, z2);
glVertex3f(x2, y1, z2);
glVertex3f(x2, y1, z1);
glVertex3f(x2, y2, z1);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(x1, y2, z1);
glVertex3f(x1, y2, z2);
glVertex3f(x2, y2, z2);
glVertex3f(x2, y2, z1);
glEnd();
glBegin(GL_POLYGON);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y1, z1);
glVertex3f(x2, y1, z2);
glVertex3f(x1, y1, z2);
glEnd();
}
$$ENDIF
//
// InitScene()
// Called when the OpenGL RC has been created. Sets the initial state for
// the OpenGL scene.
//
void CMainWnd::InitScene()
{
glClearColor($$back_r$$f, $$back_g$$f, $$back_b$$f, 1.0f); //Background color
$$IF(sample)
// TODO: Replace the following sample code with your initialization code.
// Activate lighting and a light source
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
// Define material parameters
static GLfloat glfMatAmbient[] = {0.000f, 0.450f, 1.000f, 1.0f};
static GLfloat glfMatDiffuse[] = {0.000f, 0.000f, 0.580f, 1.0f};
static GLfloat glfMatSpecular[]= {1.000f, 1.000f, 1.000f, 1.0f};
static GLfloat glfMatEmission[]= {0.000f, 0.000f, 0.000f, 1.0f};
static GLfloat fShininess = 128.000f;
// Set material parameters
glMaterialfv(GL_FRONT, GL_AMBIENT, glfMatAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, glfMatDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, glfMatSpecular);
glMaterialfv(GL_FRONT, GL_EMISSION, glfMatEmission);
glMaterialf(GL_FRONT, GL_SHININESS, fShininess);
$$ELSE
// TODO: Add scene initialization code
$$ENDIF
}
//
// KillScene()
// Called when the OpenGL RC is about to be destroyed.
//
void CMainWnd::KillScene()
{
// TODO: Use KillScene to free resources.
}
//
// DrawScene()
// Called each time the OpenGL scene has to be drawn.
//
void CMainWnd::DrawScene()
{
$$IF(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
// Add a light source
GLfloat glfLight[] = {-4.0f, 4.0f, 4.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_POSITION, glfLight);
// 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
}
//
// Tick()
// The applications "heartbeat" function. Called before another frame needs
// to be drawn. Use this function to calculate new positions for objects in
// the scene, for instance. Set bRedrawScene = FALSE if the scene should not be
// redrawn.
//
void CMainWnd::Tick(BOOL &bRedrawScene)
{
$$IF(sample)
m_fAngle+= 1; // Add some rotation to the cube
$$ENDIF
$$IF(fps)
$$IF(winfull)
#ifndef FULLSCREEN
UpdateFPS();
#endif
$$ELSE
UpdateFPS();
$$ENDIF
$$ENDIF
}
$$IF(fps)
//
// UpdateFPS()
// Calculates FPS and shows the result in the window caption on regular
// intervals. Needs to be called every frame update.
//
void CMainWnd::UpdateFPS()
{
const int FRAMEINTERVAL = 1000; // Show FPS every 1000th milliseconds
static DWORD nFrames = 0; // Number of frames since last update
static DWORD nLastTick = GetTickCount(); // Time of last update
DWORD nTick = GetTickCount(); // Current time
if(nTick-nLastTick>=FRAMEINTERVAL)
{
float fFPS = 1000.0f*(GLfloat)nFrames/(GLfloat)(nTick-nLastTick);
nLastTick = nTick;
nFrames = 0;
char szFPS[256];
sprintf(szFPS, "%.1f FPS", fFPS);
SetWindowText(m_hWnd, szFPS);
}
nFrames++;
}
$$ENDIF
$$ENDIF