// Utility.h
#ifndef UTILITY_H_
#define UTILITY_H_
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#endif
#ifndef OPENGL_TOOLS_
#define OPENGL_TOOLS_
#include <glew.h>
#pragma comment(lib,"glew32.lib")
#include <GLUT.H>
#pragma comment(lib,"glut.lib")
#pragma comment(lib,"opengl.lib")
#pragma comment(lib,"glaux.lib")
#endif
// GameEngine.h
#ifndef GAMEENGINE_H_
#define GAMEENGINE_H_
#include "Utility.h"
#pragma once
class CGameEngine
{
public:
CGameEngine();
~CGameEngine();
static GLvoid ReSizeGLScene(GLsizei width, GLsizei height);// 重置OpenGL窗口大小
BOOL InitGL(GLvoid);
VOID onLogic();
BOOL DrawGLScene(GLvoid);
VOID onCleanup();
};
#endif
// GameEngine.cpp
#include "GameEngine.h"
#include "MyWinFrame.h"
CGameEngine::CGameEngine()
{
}
CGameEngine::~CGameEngine()
{
}
VOID CGameEngine::ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height == 0) // 防止被零除
{
height = 1; // 将Height设为1
}
glViewport(0, 0, width, height); // 重置当前的视口
glMatrixMode(GL_PROJECTION); // 选择投影矩阵
glLoadIdentity(); // 重置投影矩阵
// 设置视口的大小
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW); // 选择模型观察矩阵
glLoadIdentity(); // 重置模型观察矩阵
}
BOOL CGameEngine::InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH); // 启用阴影平滑
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景
glClearDepth(1.0f); // 设置深度缓存
glEnable(GL_DEPTH_TEST); // 启用深度测试
glDepthFunc(GL_LEQUAL); // 所作深度测试的类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 告诉系统对透视进行修正
return TRUE; // 初始化 OK
}
BOOL CGameEngine::DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
glLoadIdentity(); // 重置当前的模型观察矩阵
return TRUE; // 一切 OK
}
VOID CGameEngine::onLogic()
{
}
VOID CGameEngine::onCleanup()
{
}
//MyWinFrame.h
#ifndef MYWINFRAME_H_
#define MYWINFRAME_H_
#pragma once
#include "Utility.h"
#include "GameEngine.h"
class CMyWinFrame
{
public:
static CGameEngine m_game;
public:
CMyWinFrame();
~CMyWinFrame();
BOOL RegisterGLWindow(char *title);
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag);
GLvoid KillGLWindow(GLvoid);
static LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
};
#endif
//MyWinFrame.cpp
#include "MyWinFrame.h"
HGLRC hRC = NULL; // 窗口着色描述表句柄
HDC hDC = NULL; // OpenGL渲染描述表句柄
HWND hWnd = NULL; // 保存我们的窗口句柄
HINSTANCE hInstance; // 保存程序的实例
bool keys[256]; // 保存键盘按键的数组
bool active = TRUE; // 窗口的活动标志,缺省为TRUE
bool fullscreen = TRUE; // 全屏标志缺省,缺省设定成全屏模式
CGameEngine CMyWinFrame::m_game = CGameEngine();
CMyWinFrame::CMyWinFrame()
{
}
CMyWinFrame::~CMyWinFrame()
{
}
GLvoid CMyWinFrame::KillGLWindow(GLvoid)
{
if (fullscreen) // 我们处于全屏模式吗?
{
ChangeDisplaySettings(NULL, 0); // 是的话,切换回桌面