/********************************************
* Object Orientated NeHeGL Using Base Class *
* Author: Andreas Oberdorfer 2004 *
********************************************/
#include "NeHeGL.h" // Header File For The NeHeGL Basecode
#include <gl/gl.h> // Header File For The OpenGL32 Library
#include <gl/glu.h> // Header File For The GLu32 Library
// Program Entry (WinMain)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int ret = -1;
GL_Application * appl = GL_Application::Create("OpenGL"); // Create Application Class
if (appl != 0)
{
ret = appl->Main(hInstance, hPrevInstance, lpCmdLine, nCmdShow); // Execute Main Program
delete appl; // Delete Application Class
}
else
{ // Error Creating Application
MessageBox(HWND_DESKTOP, "Error Creating Application Class", "Error", MB_OK | MB_ICONEXCLAMATION);
}
return ret;
} // End Of WinMain()
// Process Window Message Callbacks
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LONG user_data = GetWindowLong(hWnd, GWL_USERDATA); // Load The Application Class Context
if (user_data == 0)
{ // Application Class Context Not Available
if (uMsg == WM_CREATE) // Evaluate Window Message
{ // Window Creation Message Received
// Get Window Structure Pointer Which Contains The Application Class Context
CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>(lParam);
// Get The Application Class Context
GL_Application * appl = reinterpret_cast<GL_Application *>(creation->lpCreateParams);
// Store The Application Class Context
SetWindowLong(hWnd, GWL_USERDATA, reinterpret_cast<LONG>(appl));
appl->m_IsVisible = true; // Set Visible To true
return 0; // Return
}
}
else
{ // Get The Application Class Context
GL_Application * appl = reinterpret_cast<GL_Application *>(user_data);
return appl->Message(hWnd, uMsg, wParam, lParam); // Call Class Message Handler
}
return DefWindowProc(hWnd, uMsg, wParam, lParam); // Pass Unhandled Messages To DefWindowProc
}
// Initialize Class Default Values
GL_Application::GL_Application(const char * class_name) // Class Constructor
{
m_ClassName = class_name; // Store Class Name
m_IsProgramLooping = true; // Program Looping Is Set To true
m_CreateFullScreen = true; // Enable FullScreen Mode
m_IsVisible = false;
m_ResizeDraw = false; // Disable Redraw During Window Resize
m_LastTickCount = 0;
}
void GL_Application::ToggleFullscreen() // Toggle Fullscreen / Windowed
{
PostMessage(m_Window, WM_TOGGLEFULLSCREEN, 0, 0); // Send A WM_TOGGLEFULLSCREEN Message
}
void GL_Application::TerminateApplication() // Terminate The Application
{
PostMessage(m_Window, WM_QUIT, 0, 0); // Send A WM_QUIT Message
m_IsProgramLooping = false; // Stop Looping Of The Program
}
// Message Handler
LRESULT GL_Application::Message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) // Evaluate Window Message
{
case WM_SYSCOMMAND: // Intercept System Commands
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
break;
}
break; // Exit
case WM_CLOSE: // Closing The Window
TerminateApplication(); // Terminate The Application
return 0; // Return
break;
case WM_EXITMENULOOP:
case WM_EXITSIZEMOVE:
m_LastTickCount = GetTickCount(); // Refresh Last Tick Count
return 0;
break;
case WM_MOVE:
m_Window.SetPosX(LOWORD(lParam)); // Set Window - LoWord = X Position
m_Window.SetPosY(HIWORD(lParam)); // Set Window - HiWord = Y Position
return 0;
break;
case WM_PAINT:
if (m_ResizeDraw == true) // Is Draw Option Enabled
{
m_Window.ReshapeGL(); // Reshape Window
Draw(); // Redraw Scene
m_Window.SwapBuffers(); // Swap Buffers
}
break;
case WM_SIZING:
{
RECT * rect = (RECT *)lParam;
m_Window.SetWidth(rect->right - rect->left); // Set Window - LoWord = Width
m_Window.SetHeight(rect->bottom - rect->top); // Set Window - HiWord = Height
return TRUE;
}
break;
case WM_SIZE: // Size Action Has Taken Place
switch (wParam) // Evaluate Size Action
{
case SIZE_MINIMIZED: // Was Window Minimized?
m_IsVisible = false; // Set Visible To false
return 0; // Return
break;
case SIZE_MAXIMIZED: // Was Window Maximized?
case SIZE_RESTORED: // Was Window Restored?
m_IsVisible = true; // Set Visible To true
m_Window.SetWidth(LOWORD(lParam)); // Set Window - LoWord = Width
m_Window.SetHeight(HIWORD(lParam)); // Set Window - HiWord = Height
m_Window.ReshapeGL(); // Reshape Window
m_LastTickCount = GetTickCount(); // Refresh Last Tick Count
return 0; // Return
break;
}
break; // Break
case WM_KEYDOWN: // Update Keyboard Buffers For Keys Pressed
m_Keys.SetPressed(wParam); // Set The Selected Key (wParam) To Pressed
return 0; // Return
break;
case WM_KEYUP: // Update Keyboard Buffers For Keys Released
m_Keys.SetReleased(wParam); // Set The Selected Key (wParam) To Released
return 0; // Return
break;
case WM_TOGGLEFULLSCREEN: // Toggle FullScreen Mode On / Off
m_CreateFullScreen = !m_CreateFullScreen;
PostMessage(hWnd, WM_QUIT, 0, 0);
break; // Break
}
return DefWindowProc(hWnd, uMsg, wParam, lParam); // Pass Unhandled Messages To DefWindowProc
}
// Program Main Loop
int GL_Application::Main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Register A Class For Our Window To Use
WNDCLASSEX windowClass; // Window Class
ZeroMemory(&windowClass, sizeof(WNDCLASSEX)); // Make Sure Memory Is Cleared
windowClass.cbSize = sizeof(WNDCLASSEX); // Size Of The windowClass Structure
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraws The Window For Any Movement / Resizing
windowClass.lpfnWndProc = (WNDPROC)(WindowProc); // WindowProc Handles Messages
windowClass.hInstance = hInstance; // Set The Instance
windowClass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE); // Class Background Brush Color
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
windowClass.lpszClassName = m_ClassName; // Sets The Applications Classname
if (RegisterClassEx(&windowClass) == 0) // Did Registering The Class Fail?
{ // NOTE: Failure, Should Never Happen
MessageBox(HWND_DESKTOP, "Error Registering Window Class!", "Error", MB_OK | MB_ICONEXCLAMATION);
return -1; // Terminate Application
}
// Ask The User If They Want To Start In FullScreen Mode?
if (MessageBox(HWND_DESKTOP, "Would You Like To Run In Fullscreen Mode ?", "Start FullScreen ?", MB_YESNO | MB_ICONQUESTION) == IDNO)
{
m_CreateFullScreen = false; // m_CreateFullScreen Is Set To User Default
}
while (m_IsProgramLooping) // Loop Until WM_QUIT Is Received
{ // Create A Window
if (m_Window.Create("GL Framework", m_CreateFullScreen, m_ClassName, hInstance, this) == true)
{ // At This Point We Should Have A Window That Is
NeHe的 OpenGL框架
4星 · 超过85%的资源 需积分: 3 104 浏览量
2009-03-17
16:52:23
上传
评论
收藏 32KB ZIP 举报
billow36
- 粉丝: 0
- 资源: 23