#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <tchar.h>
#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <GL/glew.h>
#include <GL/wglew.h> //定义程序链接时所需要调用的OpenGL程序库,简化工程配置
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "glew32.lib" )
#pragma comment( lib, "glew32s.lib" )
#define WINDOW_TITLE "Hello"
#define WINDOW_CLASS_NAME "This is function!"
#define WIDTH 800
#define HEIGHT 600
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define FRAME_PER_SECOND (60)
#define TIME_IN_FRAME (1000/FRAME_PER_SECOND)
HWND ghWnd; // 窗口句柄
HINSTANCE ghInstance; // 程序实例句柄
HDC ghDC; // GDI设备环境句柄
HGLRC ghRC; // 渲染器环境句柄
// 激活创建窗口
void EnableOpengl()
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
ghDC = GetDC(ghWnd);
ZeroMemory(&pfd,sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat(ghDC, &pfd);
SetPixelFormat(ghDC, iFormat, &pfd);
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
}
void SceneInit(int w, int h)
{
GLenum err = glewInit();
if(err != GLEW_OK)
{
MessageBox(NULL, _T("error"), _T("Glew init failed"), MB_OK);
exit(-1);
}
// 前三列是颜色数组,后三列是顶点数组数据,
static GLfloat fVertices[] = { 1.0, 0.0, 0.0, -0.5, -0.5, 0.0,
0.0, 1.0, 0.0, 0.5, -0.5, 0.0,
0.0, 0.0, 1.0, 0.5, 0.5, 0.0,
0.0, 0.0, 0.0, -0.5, 0.5, 0.0,
1.0, 1.0, 1.0, 0.0, 0.0, 0.0
};
glInterleavedArrays(GL_C3F_V3F, 6 * sizeof(GL_FLOAT), fVertices);
}
//这里进行所有的绘图工作
void SceneShow(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT); // 清空颜色缓冲区
glBegin(GL_QUADS);
glArrayElement(0);
glArrayElement(1);
glArrayElement(2);
glArrayElement(3);
glEnd();
glFlush();
}
// 取消OpenGL 在程序结束前调用, 释放渲染环境, 设备环境
void DisableOpengl( )
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(ghRC);
ReleaseDC(ghWnd, ghDC);
}
int Game_Main(void *parms = NULL, int num_parms = 0)
{
DWORD dwStartTime = GetTickCount();
if(KEYDOWN(VK_ESCAPE))
SendMessage(ghWnd, WM_CLOSE, 0,0);
SceneShow();
while(GetTickCount() - dwStartTime < TIME_IN_FRAME)
{
Sleep(1);
}
return 1;
}
int Game_Init(void *parms = NULL, int num_parms = 0)
{
ghDC = GetDC(ghWnd);
EnableOpengl();
SceneInit(WIDTH, HEIGHT);
return 1;
}
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{ // this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;
case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// end painting
EndPaint(hwnd,&ps);
// return success
return(0);
} break;
case WM_DESTROY:
{
// kill the application, this sends a WM_QUIT message
PostQuitMessage(0); // return success
return(0);
} break;
default:break;
} // end switch // process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
}
int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{ // this is called after the game is exited and the main event
// loop while is exited, do all you cleanup and shutdown here
DisableOpengl(); // return success or failure or your own return code here
return(1);
}
int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// save hinstance in global
ghInstance = hinstance;
if (!(hwnd = CreateWindowEx(NULL,
// extended style
WINDOW_CLASS_NAME, // class
WINDOW_TITLE, // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
WIDTH,HEIGHT, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);
ghWnd = hwnd;
Game_Init();
while(TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Game_Main();
}
Game_Shutdown();
return (msg.wParam);
}