📄 nehegl.cpp
字号:
/***********************************************
* *
* Jeff Molofee's Revised OpenGL Basecode *
* Huge Thanks To Maxwell Sayles & Peter Puck *
* http://nehe.gamedev.net *
* 2001 *
* *
***********************************************/
#include <windows.h> // Header File For The Windows Library
#include <gl/gl.h> // Header File For The OpenGL32 Library
#include <gl/glu.h> // Header File For The GLu32 Library
#include "NeHeGL.h" // Header File For The NeHeGL Basecode
#define WM_TOGGLEFULLSCREEN (WM_USER+1) // Application Define Message For Toggling
static BOOL g_isProgramLooping; // Window Creation Loop, For FullScreen/Windowed Toggle // Between Fullscreen / Windowed Mode
static BOOL g_createFullScreen; // If TRUE, Then Create Fullscreen
void TerminateApplication (GL_Window* window) // Terminate The Application
{
PostMessage (window->hWnd, WM_QUIT, 0, 0); // Send A WM_QUIT Message
g_isProgramLooping = FALSE; // Stop Looping Of The Program
}
void ToggleFullscreen (GL_Window* window) // Toggle Fullscreen/Windowed
{
PostMessage (window->hWnd, WM_TOGGLEFULLSCREEN, 0, 0); // Send A WM_TOGGLEFULLSCREEN Message
}
void ReshapeGL (int width, int height) // Reshape The Window When It's Moved Or Resized
{
glViewport (0, 0, (GLsizei)(width), (GLsizei)(height)); // Reset The Current Viewport
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
gluPerspective(50, (float)width/(float)height, 5, 2000);
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity (); // Reset The Modelview Matrix
}
BOOL ChangeScreenResolution (int width, int height, int bitsPerPixel) // Change The Screen Resolution
{
DEVMODE dmScreenSettings; // Device Mode
ZeroMemory (&dmScreenSettings, sizeof (DEVMODE)); // Make Sure Memory Is Cleared
dmScreenSettings.dmSize = sizeof (DEVMODE); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Select Screen Width
dmScreenSettings.dmPelsHeight = height; // Select Screen Height
dmScreenSettings.dmBitsPerPel = bitsPerPixel; // Select Bits Per Pixel
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings (&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
return FALSE; // Display Change Failed, Return False
}
return TRUE; // Display Change Was Successful, Return True
}
BOOL CreateWindowGL (GL_Window* window) // This Code Creates Our OpenGL Window
{
DWORD windowStyle = WS_OVERLAPPEDWINDOW; // Define Our Window Style
DWORD windowExtendedStyle = WS_EX_APPWINDOW; // Define The Window's Extended Style
PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
{
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
window->init.bitsPerPixel, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
1, // 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
};
RECT windowRect = {0, 0, window->init.width, window->init.height}; // Define Our Window Coordinates
GLuint PixelFormat; // Will Hold The Selected Pixel Format
if (window->init.isFullScreen == TRUE) // Fullscreen Requested, Try Changing Video Modes
{
if (ChangeScreenResolution (window->init.width, window->init.height, window->init.bitsPerPixel) == FALSE)
{
// Fullscreen Mode Failed. Run In Windowed Mode Instead
MessageBox (HWND_DESKTOP, "Mode Switch Failed.\nRunning In Windowed Mode.", "Error", MB_OK | MB_ICONEXCLAMATION);
window->init.isFullScreen = FALSE; // Set isFullscreen To False (Windowed Mode)
}
else // Otherwise (If Fullscreen Mode Was Successful)
{
ShowCursor (FALSE); // Turn Off The Cursor
windowStyle = WS_POPUP; // Set The WindowStyle To WS_POPUP (Popup Window)
windowExtendedStyle |= WS_EX_TOPMOST; // Set The Extended Window Style To WS_EX_TOPMOST
} // (Top Window Covering Everything Else)
}
else // If Fullscreen Was Not Selected
{
// Adjust Window, Account For Window Borders
AdjustWindowRectEx (&windowRect, windowStyle, 0, windowExtendedStyle);
}
// Create The OpenGL Window
window->hWnd = CreateWindowEx (windowExtendedStyle, // Extended Style
window->init.application->className, // Class Name
window->init.title, // Window Title
windowStyle, // Window Style
0, 0, // Window X,Y Position
windowRect.right - windowRect.left, // Window Width
windowRect.bottom - windowRect.top, // Window Height
HWND_DESKTOP, // Desktop Is Window's Parent
0, // No Menu
window->init.application->hInstance, // Pass The Window Instance
window);
if (window->hWnd == 0) // Was Window Creation A Success?
{
return FALSE; // If Not Return False
}
window->hDC = GetDC (window->hWnd); // Grab A Device Context For This Window
if (window->hDC == 0) // Did We Get A Device Context?
{
// Failed
DestroyWindow (window->hWnd); // Destroy The Window
window->hWnd = 0; // Zero The Window Handle
return FALSE; // Return False
}
PixelFormat = ChoosePixelFormat (window->hDC, &pfd); // Find A Compatible Pixel Format
if (PixelFormat == 0) // Did We Find A Compatible Format?
{
// Failed
ReleaseDC (window->hWnd, window->hDC); // Release Our Device Context
window->hDC = 0; // Zero The Device Context
DestroyWindow (window->hWnd); // Destroy The Window
window->hWnd = 0; // Zero The Window Handle
return FALSE; // Return False
}
if (SetPixelFormat (window->hDC, PixelFormat, &pfd) == FALSE) // Try To Set The Pixel Format
{
// Failed
ReleaseDC (window->hWnd, window->hDC); // Release Our Device Context
window->hDC = 0; // Zero The Device Context
DestroyWindow (window->hWnd); // Destroy The Window
window->hWnd = 0; // Zero The Window Handle
return FALSE; // Return False
}
window->hRC = wglCreateContext (window->hDC); // Try To Get A Rendering Context
if (window->hRC == 0) // Did We Get A Rendering Context?
{
// Failed
ReleaseDC (window->hWnd, window->hDC); // Release Our Device Context
window->hDC = 0; // Zero The Device Context
DestroyWindow (window->hWnd); // Destroy The Window
window->hWnd = 0; // Zero The Window Handle
return FALSE; // Return False
}
// Make The Rendering Context Our Current Rendering Context
if (wglMakeCurrent (window->hDC, window->hRC) == FALSE)
{
// Failed
wglDeleteContext (window->hRC); // Delete The Rendering Context
window->hRC = 0; // Zero The Rendering Context
ReleaseDC (window->hWnd, window->hDC); // Release Our Device Context
window->hDC = 0; // Zero The Device Context
DestroyWindow (window->hWnd); // Destroy The Window
window->hWnd = 0; // Zero The Window Handle
return FALSE; // Return False
}
ShowWindow (window->hWnd, SW_NORMAL); // Make The Window Visible
window->isVisible = TRUE; // Set isVisible To True
ReshapeGL (window->init.width, window->init.height); // Reshape Our GL Window
ZeroMemory (window->keys, sizeof (Keys)); // Clear All Keys
window->lastTickCount = GetTickCount (); // Get Tick Count
return TRUE; // Window Creating Was A Success
// Initialization Will Be Done In WM_CREATE
}
BOOL DestroyWindowGL (GL_Window* window) // Destroy The OpenGL Window & Release Resources
{
if (window->hWnd != 0) // Does The Window Have A Handle?
{
if (window->hDC != 0) // Does The Window Have A Device Context?
{
wglMakeCurrent (window->hDC, 0); // Set The Current Active Rendering Context To Zero
if (window->hRC != 0) // Does The Window Have A Rendering Context?
{
wglDeleteContext (window->hRC); // Release The Rendering Context
window->hRC = 0; // Zero The Rendering Context
}
ReleaseDC (window->hWnd, window->hDC); // Release The Device Context
window->hDC = 0; // Zero The Device Context
}
DestroyWindow (window->hWnd); // Destroy The Window
window->hWnd = 0; // Zero The Window Handle
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -