Main.cpp
上传用户:sz83729876
上传日期:2013-03-07
资源大小:4140k
文件大小:10k
- //***********************************************************************//
- // //
- // - "Talk to me like I'm a 3 year old!" Programming Lessons - //
- // //
- // $Author: Ben Humphrey digiben@gametutorilas.com //
- // //
- // $Program: Triangle //
- // //
- // $Description: Init OpenGL and Draw a triangle to the screen //
- // //
- // $Date: 3/3/01 //
- // //
- //***********************************************************************//
- #include "main.h"
- #include "core.h"
- HWND g_hWnd; // This is the handle for the window
- RECT g_rRect; // This holds the window dimensions
- HDC g_hDC; // General HDC - (handle to device context)
- HGLRC g_hRC; // General OpenGL_DC - Our Rendering Context for OpenGL
- HINSTANCE g_hInstance; // This holds the global hInstance for UnregisterClass() in DeInit()
- BOOL g_keys[256];
- UINT ScreenWidth=800;
- UINT ScreenHeight=600;
- UINT ScreenDeph=16;
- int fst=0;
- // The window proc which handles all of window's messages.
- LRESULT CALLBACK wmWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- ///////////////////////////////// CHANGE TO FULL SCREEN \\\\\\\\\\\\\\\\*
- /////
- ///// This changes the screen to FULL SCREEN
- /////
- ///////////////////////////////// CHANGE TO FULL SCREEN \\\\\\\\\\\\\\\\*
- void wmChangeToFullScreen()
- {
- DEVMODE dmSettings; // Device Mode variable
- memset(&dmSettings,0,sizeof(dmSettings)); // Makes Sure Memory's Cleared
- // Get current settings -- This function fills our the settings
- // This makes sure NT and Win98 machines change correctly
- if(!EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&dmSettings))
- {
- // Display error message if we couldn't get display settings
- MessageBox(NULL, "Could Not Enum Display Settings", "Error", MB_OK);
- return;
- }
- dmSettings.dmPelsWidth = ScreenWidth; // Selected Screen Width
- dmSettings.dmPelsHeight = ScreenHeight; // Selected Screen Height
-
- // This function actually changes the screen to full screen
- // CDS_FULLSCREEN Gets Rid Of Start Bar.
- // We always want to get a result from this function to check if we failed
- int result = ChangeDisplaySettings(&dmSettings,CDS_FULLSCREEN);
- // Check if we didn't recieved a good return message From the function
- if(result != DISP_CHANGE_SUCCESSFUL)
- {
- // Display the error message and quit the program
- MessageBox(NULL, "Display Mode Not Compatible", "Error", MB_OK);
- PostQuitMessage(0);
- }
- }
- ///////////////////////////////// CREATE MY WINDOW \\\\\\\\\\\\\\\\*
- /////
- ///// This function creates a window, but doesn't have a message loop
- /////
- ///////////////////////////////// CREATE MY WINDOW \\\\\\\\\\\\\\\\*
- HWND wmCreateMyWindow(LPSTR strWindowName, int width, int height, DWORD dwStyle, HINSTANCE hInstance)
- {
- HWND hWnd;
- WNDCLASS wndclass;
-
- memset(&wndclass, 0, sizeof(WNDCLASS)); // Init the size of the class
- wndclass.style = CS_HREDRAW | CS_VREDRAW; // Regular drawing capabilities
- wndclass.lpfnWndProc = wmWinProc; // Pass our function pointer as the window procedure
- wndclass.hInstance = hInstance; // Assign our hInstance
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // General icon
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // An arrow for the cursor
- wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); // A white window
- wndclass.lpszClassName = "--Matrix Saver--"; // Assign the class name
- RegisterClass(&wndclass); // Register the class
-
- if(!dwStyle) // Check if we wanted full screen mode
- { // Set the window properties for full screen mode
- dwStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
- wmChangeToFullScreen(); // Go to full screen
- ShowCursor(FALSE); // Hide the cursor
- }
-
- g_hInstance = hInstance; // Assign our global hInstance to the window's hInstance
- // Below, we need to adjust the window to it's true requested size. If we say we
- // want a window that is 800 by 600, that means we want the client rectangle to
- // be that big, not the entire window. If we go into window mode, it will cut off
- // some of the client rect and stretch the remaining which causes slow down. We fix this below.
- RECT rWindow;
- rWindow.left = 0; // Set Left Value To 0
- rWindow.right = width; // Set Right Value To Requested Width
- rWindow.top = 0; // Set Top Value To 0
- rWindow.bottom = height; // Set Bottom Value To Requested Height
- AdjustWindowRect( &rWindow, dwStyle, false); // Adjust Window To True Requested Size
- // Create the window
- hWnd = CreateWindow("--Matrix Saver--", strWindowName, dwStyle, 0, 0,
- rWindow.right - rWindow.left, rWindow.bottom - rWindow.top,
- NULL, NULL, hInstance, NULL);
- if(!hWnd) return NULL; // If we could get a handle, return NULL
- ShowWindow(hWnd, SW_SHOWNORMAL); // Show the window
- UpdateWindow(hWnd); // Draw the window
- SetFocus(hWnd); // Sets Keyboard Focus To The Window
- return hWnd;
- }
- ///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\*
- /////
- ///// This function Handles the main game loop
- /////
- ///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\*
- WPARAM wmMainLoop()
- {
- MSG msg;
- while(1) // Do our infinate loop
- { // Check if there was a message
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if(msg.message == WM_QUIT) // If the message wasnt to quit
- break;
- TranslateMessage(&msg); // Find out what the message does
- DispatchMessage(&msg); // Execute the message
- }
- else // if there wasn't a message
- {
- // Do computationally expensive things here. We want to render the scene
- // every frame, so we call our rendering function here. Even though the scene
- // doesn't change, it will bottle neck the message queue if we don't do something.
- // Usually WaitMessage() is used to make sure the app doesn't eat up the CPU.
- gcProcessLevel();
- }
- }
- return(msg.wParam); // Return from the program
- }
- ///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\*
- /////
- ///// This function initializes the game window.
- /////
- ///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\*
- void wmInit(HWND hWnd)
- {
- g_hWnd = hWnd; // Assign the window handle to a global window handle
- GetClientRect(g_hWnd, &g_rRect); // Assign the windows rectangle to a global RECT
- griInitializeOpenGL(g_rRect.right, g_rRect.bottom); // Init OpenGL with the global rect
- for(int i=0;i<256;i++)
- g_keys[i]=FALSE;
- // *Hint* We will put all our game init stuff here
- // Some things include loading models, textures and network initialization
- }
- ///////////////////////////////// DE INIT \\\\\\\\\\\\\\\\*
- /////
- ///// This function cleans up and then posts a quit message to the window
- /////
- ///////////////////////////////// DE INIT \\\\\\\\\\\\\\\\*
- void wmDeInit()
- {
- gcUnloadLevel();
- if (g_hRC)
- {
- wglMakeCurrent(NULL, NULL); // This frees our rendering memory and sets everything back to normal
- wglDeleteContext(g_hRC); // Delete our OpenGL Rendering Context
- }
-
- if (g_hDC)
- ReleaseDC(g_hWnd, g_hDC); // Release our HDC from memory
-
- ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
- ShowCursor(TRUE); // Show Mouse Pointer
- UnregisterClass("--Matrix Saver--", g_hInstance); // Free the window class
- PostQuitMessage (0); // Post a QUIT message to the window
- }
- ///////////////////////////////// WIN MAIN \\\\\\\\\\\\\\\\*
- /////
- ///// This function handles registering and creating the window.
- /////
- ///////////////////////////////// WIN MAIN \\\\\\\\\\\\\\\\*
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev, PSTR cmdline, int ishow)
- {
- HWND hWnd;
-
- int i=0;
- BOOL Status=FALSE;
- while(cmdline[i])
- {
- if((cmdline[i]=='/' || cmdline[i]=='-') && (cmdline[i+1]=='s'|| cmdline[i+1]=='S'))
- Status=TRUE;
- i++;
- }
- if(!Status)
- return TRUE;
- // Create our window with our function we create that passes in the:
- // Name, width, height, any flags for the window, if we want fullscreen of not, and the hInstance
- hWnd = wmCreateMyWindow("--Matrix Saver--", ScreenWidth, ScreenHeight, 0, hInstance);
- // If we never got a valid window handle, quit the program
- if(hWnd == NULL) return TRUE;
- // INIT OpenGL
- wmInit(hWnd);
- // Run our message loop and after it's done, return the result
- return wmMainLoop();
- }
- ///////////////////////////////// WIN PROC \\\\\\\\\\\\\\\\*
- /////
- ///// This function handles the window messages.
- /////
- ///////////////////////////////// WIN PROC \\\\\\\\\\\\\\\\*
- LRESULT CALLBACK wmWinProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- LONG lRet = 0;
- PAINTSTRUCT ps;
- switch (uMsg)
- {
- case WM_SYSCOMMAND:
- if ((wParam == SC_SCREENSAVE) || (wParam == SC_CLOSE)) {
- return FALSE;
- }
- break;
- case WM_PAINT: // If we need to repaint the scene
- BeginPaint(hWnd, &ps); // Init the paint struct
- EndPaint(hWnd, &ps); // EndPaint, Clean up
- break;
- case WM_KEYDOWN:
- // g_keys[wParam]=TRUE;
- // if(wParam == VK_ESCAPE)
- wmDeInit(); // Quit if we pressed ESCAPE
- break;
-
- case WM_MOUSEMOVE:
- if(fst<10)
- fst++;
- else
- wmDeInit(); // Quit if we pressed ESCAPE
- break;
-
- case WM_KEYUP:
- // g_keys[wParam]=FALSE;
- wmDeInit(); // Quit if we pressed ESCAPE
- break;
-
- case WM_DESTROY: // If the window is destroyed
- wmDeInit(); // Release memory and restore settings
- break;
-
- default: // Return by default
- lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
- break;
- }
-
- return lRet; // Return by default
- }