📄 main.cpp
字号:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//Clean up::nehe.gamedev.net/
void KillGLWindow()
{
if(hRC)
{
if(!wglMakeCurrent(NULL,NULL))
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if(!wglDeleteContext(hRC))
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL;
}
if(hDC && !ReleaseDC(hwnd,hDC))
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL;
}
if(hwnd && !DestroyWindow(hwnd))
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hwnd=NULL;
}
if(!UnregisterClass(WinClass_name,hInstance))
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL;
}
}
//Initiate Timer Function::nehe.gamedev.net/
void TimerInit(void)
{
memset(&timer, 0, sizeof(timer));
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))
{
timer.performance_timer = FALSE;
timer.mm_timer_start = timeGetTime();
timer.resolution = 1.0f/1000.0f;
timer.frequency = 1000;
timer.mm_timer_elapsed = timer.mm_timer_start;
}
else
{
QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);
timer.performance_timer = TRUE;
timer.resolution = (float)(((double)1.0f)/((double)timer.frequency));
timer.performance_timer_elapsed = timer.performance_timer_start;
}
}
//Very Precise Get Time Function::nehe.gamedev.net/
float TimerGetTime()
{
__int64 time;
if(timer.performance_timer)
{
QueryPerformanceCounter((LARGE_INTEGER *) &time);
return((float)(time-timer.performance_timer_start)*timer.resolution)*1000.0f;
}
else
{
return((float)(timeGetTime()-timer.mm_timer_start)*timer.resolution)*1000.0f;
}
}
//Calculate the framerate funcion::www.gametutorials.com
void CalculateFrameRate()
{
static float framesPerSecond = 0.0f;
static float lastTime = 0.0f;
static char strFrameRate[50] = {0};
static float frameTime = 0.0f;
float currentTime = timeGetTime() * 0.001f;
g_FrameInterval = currentTime - frameTime;
frameTime = currentTime;
++framesPerSecond;
if( currentTime - lastTime > 1.0f )
{
lastTime = currentTime;
sprintf(strFrameRate, "Fire Demo [FPS: %d]", int(framesPerSecond));
SetWindowText(hwnd, strFrameRate);
framesPerSecond = 0;
}
}
/*==============================================================================
APPLICATION WINDOW CREATION
==============================================================================*/
//CREATE OPENGL WINDOW
bool CreateGLWindow(char* title, int width, int height, int bits)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)width;
WindowRect.top = (long)0;
WindowRect.bottom = (long)height;
hInstance=GetModuleHandle(NULL);
//Fill in Window Class Structure
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName = WinClass_name;
if (!RegisterClass(&wc))
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_OVERLAPPEDWINDOW;
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
//Create The Window
if (!(hwnd=CreateWindowEx( dwExStyle,
WinClass_name,
title,
dwStyle |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
0, 0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
NULL,
NULL,
hInstance,
NULL)))
{
KillGLWindow();
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
static PIXELFORMATDESCRIPTOR pfd=
{
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
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)))
{
KillGLWindow();
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
{
KillGLWindow();
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd))
{
KillGLWindow();
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(hRC=wglCreateContext(hDC)))
{
KillGLWindow();
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if(!wglMakeCurrent(hDC,hRC))
{
KillGLWindow();
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
ShowWindow(hwnd,SW_SHOW);
SetForegroundWindow(hwnd);
SetFocus(hwnd);
ReSizeGLScene(width, height);
if (!InitGL())
{
KillGLWindow();
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
/*==============================================================================
//APPLICATION MAIN ENTRY POINT
==============================================================================*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
//Check for more than 1 instance
HANDLE hMutex = CreateMutex(NULL, TRUE, WinClass_name);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBox(hwnd,"Program is already running","Warning",MB_OK | MB_ICONERROR);
return 0;
}
if(!done)
{
if (!CreateGLWindow("Fire Demo [FPS: ]",640,480,16))
{
return 0;
}
}
hMenu=GetMenu(hwnd);
TimerInit();
//Windows Message Loop
while(!done)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
done=TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
float start=TimerGetTime();
if (active && !DrawScene())
{
done=TRUE;
}
else
{
Sleep(0);
SwapBuffers(hDC);
CalculateFrameRate();
}
speed_mod=(TimerGetTime()-start)/1000.0f;
}
}
//Cleanup when loop exits
xia_CleanFire();
KillGLWindow();
return (msg.wParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -