E:/Eigene Dateien/Eigene Projekte/c0re/Window.cpp

Go to the documentation of this file.
00001 /*
00002         This file is part of c0re.
00003 
00004         c0re is a multiplayer RTS on a hexagonal map with an evolving unit concept.
00005     Copyright (C) 2007 Stephan Hofmann
00006 
00007     c0re is free software: you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation, either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "Window.h"
00022 
00023 #include <iostream>
00024 
00025 std::map<HWND, Window*> Window::openWindows;
00026 
00027 Window::Window()
00028 {
00029 
00030         scancodes[27] = "ESC";
00031         scancodes[33] = "PGUP";
00032         scancodes[34] = "PGDOWN";
00033         scancodes[37] = "LEFTARROW";
00034         scancodes[38] = "UPARROW";
00035         scancodes[39] = "RIGHTARROW";
00036         scancodes[40] = "DOWNARROW";
00037         scancodes[32] = "SPACE";
00038         scancodes[13] = "RETURN";
00039         scancodes[8]  = "BACKSPACE";
00040         for (int i = 65; i <= 90; i++)
00041                 scancodes[i] = (char)i;
00042 }
00043 
00044 Window::~Window()
00045 {
00046 }
00047 int Window::checkMessages()
00048 {
00049         int ret = 0;
00050         MSG msg;
00051         while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))                    // Is There A Message Waiting?
00052         {
00053                 {
00054                         TranslateMessage(&msg);                         // Translate The Message
00055                         DispatchMessage(&msg);                          // Dispatch The Message
00056                 }
00057                 if (msg.message == WM_QUIT)
00058                 {
00059                         ret = 1;
00060                 }
00061         }
00062         return ret;
00063 }
00064 
00065 int Window::clearCurrent()
00066 {
00067         int ret = 0;
00068 
00069         if (!wglMakeCurrent (0, 0))
00070         {
00071                 //std::cout << (DWORD)GetLastError() << std::endl;
00072                 ret = -1;
00073         }
00074 
00075         return ret; 
00076 }
00077 
00078 HWND Window::createWindow(std::string name, int p_height, int p_width, int depth, bool p_fullscreen)
00079 {
00080         HWND ret;
00081         fullscreen = p_fullscreen;
00082         height = p_height;
00083         width = p_width;
00084 
00085         DWORD           dwExStyle;                                              // Window Extended Style
00086         DWORD           dwStyle;
00087         RECT WindowRect;                                                        // Grabs Rectangle Upper Left / Lower Right Values
00088         WindowRect.left=(long)0;                                                // Set Left Value To 0
00089         WindowRect.right=(long)width;                                           // Set Right Value To Requested Width
00090         WindowRect.top=(long)0;                                                 // Set Top Value To 0
00091         WindowRect.bottom=(long)height; 
00092         hInstance               = GetModuleHandle(NULL);
00093 
00094 
00095         WNDCLASS        wc;
00096         wc.style                = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;           // Redraw On Move, And Own DC For Window
00097         wc.lpfnWndProc          = (WNDPROC) this->WndProc;                              // WndProc Handles Messages
00098         wc.cbClsExtra           = 0;                                            // No Extra Window Data
00099         wc.cbWndExtra           = 0;                                            // No Extra Window Data
00100         wc.hInstance            = hInstance;                                    // Set The Instance
00101         wc.hIcon                = LoadIcon(NULL, IDI_WINLOGO);                  // Load The Default Icon
00102         wc.hCursor              = LoadCursor(NULL, IDC_ARROW);                  // Load The Arrow Pointer
00103         wc.hbrBackground        = NULL;                                         // No Background Required For GL
00104         wc.lpszMenuName         = NULL;                                         // We Don't Want A Menu
00105         wc.lpszClassName        = "OpenGL";                                     // Set The Class Name
00106 
00107         if (!RegisterClass(&wc))                                                // Attempt To Register The Window Class
00108         {
00109                 //MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
00110                 return 0;                                                               // Exit And Return FALSE
00111         }
00112 
00113         if (fullscreen)                                                         // Attempt Fullscreen Mode?
00114         {
00115                 DEVMODE dmScreenSettings;                                       // Device Mode
00116                 memset(&dmScreenSettings,0,sizeof(dmScreenSettings));           // Makes Sure Memory's Cleared
00117                 dmScreenSettings.dmSize=sizeof(dmScreenSettings);               // Size Of The Devmode Structure
00118                 dmScreenSettings.dmPelsWidth    = width;                        // Selected Screen Width
00119                 dmScreenSettings.dmPelsHeight   = height;                       // Selected Screen Height
00120                 dmScreenSettings.dmBitsPerPel   = depth;                                // Selected Bits Per Pixel
00121                 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
00122 
00123                 // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
00124                 if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
00125                 {
00126                         // If The Mode Fails, Offer Two Options.  Quit Or Run In A Window.
00127                         if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
00128                         {
00129                                 fullscreen=FALSE;                               // Select Windowed Mode (Fullscreen=FALSE)
00130                                 
00131                         }
00132                         else
00133                         {
00134                                 // Pop Up A Message Box Letting User Know The Program Is Closing.
00135                                 //MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
00136                                 return 0;                                               // Exit And Return FALSE
00137                         }
00138                 }
00139         }
00140 
00141 
00142         if (fullscreen)                                                         // Are We Still In Fullscreen Mode?
00143         {
00144                 dwExStyle=WS_EX_APPWINDOW;                                      // Window Extended Style
00145                 dwStyle=WS_POPUP;                                               // Windows Style
00146                 //ShowCursor(FALSE);                                            // Hide Mouse Pointer
00147         }
00148         else
00149         {
00150                 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;                   // Window Extended Style
00151                 dwStyle=WS_OVERLAPPEDWINDOW;                                    // Windows Style
00152         }
00153 
00154         if (!(hWnd=CreateWindowEx(      
00155                                 dwExStyle,                              // Extended Style For The Window
00156                                 "OpenGL",                               // Class Name
00157                                 name.c_str(),                                   // Window Title
00158                                 WS_CLIPSIBLINGS |                       // Required Window Style
00159                                 WS_CLIPCHILDREN |                       // Required Window Style
00160                                 dwStyle,                                // Selected Window Style
00161                                 0, 0,                                   // Window Position
00162                                 WindowRect.right-WindowRect.left,       // Calculate Adjusted Window Width
00163                                 WindowRect.bottom-WindowRect.top,       // Calculate Adjusted Window Height
00164                                 NULL,                                   // No Parent Window
00165                                 NULL,                                   // No Menu
00166                                 hInstance,                              // Instance
00167                                 NULL)))                                 // Don't Pass Anything To WM_CREATE
00168         {
00169                 killWindow();                                                   // Reset The Display
00170                 //MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
00171                 return 0;                                               // Return FALSE
00172         }
00173         else
00174                 ret = hWnd;
00175 
00176         openWindows.insert(std::make_pair(hWnd, this));
00177 
00178         hDC = GetDC(hWnd);
00179 
00180         PIXELFORMATDESCRIPTOR pfd ;
00181         memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)) ;
00182         pfd.nSize      = sizeof(PIXELFORMATDESCRIPTOR); 
00183         pfd.nVersion   = 1 ; 
00184         pfd.dwFlags    = PFD_DOUBLEBUFFER |
00185                                         PFD_SUPPORT_OPENGL |
00186                                         PFD_DRAW_TO_WINDOW ;
00187         pfd.iPixelType = PFD_TYPE_RGBA ;
00188         pfd.cColorBits = depth ;
00189         pfd.cDepthBits = depth ;
00190         pfd.iLayerType = PFD_MAIN_PLANE ;
00191 
00192         int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
00193         if (nPixelFormat == 0)
00194         {
00195                 //std::cout <<"ChoosePixelFormat Failed \n";
00196                 return 0;
00197         }
00198         std::cout << "Pixel Format: "<<nPixelFormat << std::endl;
00199 
00200         BOOL bResult = SetPixelFormat (hDC, nPixelFormat, &pfd);
00201         if (!bResult)
00202         {
00203                 //std::cout << "SetPixelFormat Failed \n";
00204                 return 0;
00205         }
00206 
00207         hRC = wglCreateContext(hDC);
00208         if (!hRC)
00209         {
00210                 //std::cout << "wglCreateContext Failed\n";
00211                 return 0;
00212         }
00213 
00214         ShowWindow(hWnd,SW_SHOW);                                               // Show The Window
00215         SetForegroundWindow(hWnd);                                              // Slightly Higher Priority
00216         SetFocus(hWnd);
00217         return ret;
00218 
00219 }
00220 
00221 int Window::killWindow()
00222 {
00223         int ret = 0;
00224 
00225         if (fullscreen)                                                         // Are We In globals.int_fullscreen Mode?
00226         {
00227 
00228                 ChangeDisplaySettings(NULL,0);                                  // If So Switch Back To The Desktop
00229                 ShowCursor(TRUE);                                               // Show Mouse Pointer
00230         }
00231 
00232         if (hRC)                                                                // Do We Have A Rendering Context?
00233         {
00234                 if (!wglDeleteContext(hRC))                                     // Are We Able To Delete The RC?
00235                 {
00236                         //MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
00237                         return -1;
00238                 }
00239                 hRC=NULL;                                                       // Set RC To NULL
00240         }
00241 
00242         if (hDC && !ReleaseDC(hWnd,hDC))                                        // Are We Able To Release The DC
00243         {
00244                 //MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
00245                 hDC=NULL;                                                       // Set DC To NULL
00246                 return -1;
00247         }
00248 
00249         openWindows.erase(hWnd);
00250 
00251         if (hWnd && !DestroyWindow(hWnd))                                       // Are We Able To Destroy The Window?
00252         {
00253                 //MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
00254                 hWnd=NULL;                                                      // Set hWnd To NULL
00255                 return -1;
00256         }
00257 
00258         return ret;
00259 
00260 }
00261 
00262 int Window::makeCurrent()
00263 {
00264         int ret = 0;
00265 
00266         if (!wglMakeCurrent (hDC, hRC))
00267         {
00268                 //std::cout << (DWORD)GetLastError() << std::endl;
00269                 return -1;
00270         }
00271 
00272         return ret;
00273 }
00274 
00275 int Window::swapBuffers()
00276 {
00277         int ret = 0;
00278 
00279         if (!SwapBuffers(hDC))
00280                 return -1;
00281 
00282         return ret;
00283 }
00284 
00285 void Window::switchFullscreen()
00286 {
00287 }
00288 
00289 
00290 int Window::BuildFont()                                                         // Build Our Bitmap Font
00291 {
00292         HFONT   font;                                                                           // Windows Font ID
00293         HFONT   oldfont;                                                                        // Used For Good House Keeping
00294 
00295         int base = -100; //glGenLists(96);                                                              // Storage For 96 Characters
00296 
00297         font = CreateFont(      25,                                                             // Height Of Font
00298                                                 0,                                                              // Width Of Font
00299                                                 0,                                                              // Angle Of Escapement
00300                                                 0,                                                              // Orientation Angle
00301                                                 700,                                                    // Font Weight
00302                                                 FALSE,                                                  // Italic
00303                                                 FALSE,                                                  // Underline
00304                                                 FALSE,                                                  // Strikeout
00305                                                 ANSI_CHARSET,                                   // Character Set Identifier
00306                                                 OUT_TT_PRECIS,                                  // Output Precision
00307                                                 CLIP_DEFAULT_PRECIS,                    // Clipping Precision
00308                                                 ANTIALIASED_QUALITY,                    // Output Quality
00309                                                 FF_DONTCARE|DEFAULT_PITCH,              // Family And Pitch
00310                                                 "Courier New");                                 // Font Name
00311 
00312         oldfont = (HFONT)SelectObject(hDC, font);           // Selects The Font We Want
00313         wglUseFontBitmaps(hDC, 32, 96, base);                           // Builds 96 Characters Starting At Character 32
00314         SelectObject(hDC, oldfont);                                                     // Selects The Font We Want
00315         DeleteObject(font);                                                                     // Delete The Font
00316         return base;
00317 }
00318 
00319 /******************** END OF CLASS WindowWindow ***********************/
00320 
00321 LRESULT CALLBACK Window::WndProc(       HWND    hWnd,                                   // Handle For This Window
00322                                 UINT    uMsg,                                   // Message For This Window
00323                                 WPARAM  wParam,                                 // Additional Message Information
00324                                 LPARAM  lParam)                                 // Additional Message Information
00325 {
00326         switch (uMsg)                                                           // Check For Windows Messages
00327         {
00328                 case WM_SYSCOMMAND:                                             // Intercept System Commands
00329                 {
00330                         switch (wParam)                                         // Check System Calls
00331                         {
00332                                 case SC_SCREENSAVE:                             // Screensaver Trying To Start?
00333                                 case SC_MONITORPOWER:                           // Monitor Trying To Enter Powersave?
00334                                 return 0;                                       // Prevent From Happening
00335                         }
00336                         break;                                                  // Exit
00337                 }
00338                 case WM_CLOSE:                                                  // Did We Receive A Close Message?
00339                 {
00340                         PostQuitMessage(0);                                     // Send A Quit Message
00341                         return 0;                                               // Jump Back
00342                 }
00343                 case WM_KEYDOWN:                                                // Is A Key Being Held Down?
00344                 {
00345                         std::map<HWND,Window*>::iterator it;
00346                         it = openWindows.find(hWnd);
00347                         if (it != openWindows.end())
00348                         {
00349                                 Window* wnd = it->second;
00350                                 std::map<int, std::string>::iterator it = wnd->scancodes.find((int)wParam);
00351                                 if (it != wnd->scancodes.end())
00352                                 {
00353                                         wnd->pushMessage("+" + it->second);
00354                                 }
00355                         }
00356                         return 0;                                               // Jump Back
00357                 }
00358                 case WM_KEYUP:                                                  // Has A Key Been Released?
00359                 {
00360                         std::map<HWND,Window*>::iterator it;
00361                         it = openWindows.find(hWnd);
00362                         if (it != openWindows.end())
00363                         {
00364                                 Window* wnd = it->second;
00365                                 std::map<int, std::string>::iterator it = wnd->scancodes.find((int)wParam);
00366                                 if (it != wnd->scancodes.end())
00367                                 {
00368                                         wnd->pushMessage("-" + it->second);
00369                                 }
00370                         }
00371                         return 0;                                               // Jump Back
00372                 }
00373                 case WM_SIZE:                                                   // Resize The OpenGL Window
00374                 {
00375                         std::map<HWND,Window*>::iterator it;
00376                         it = openWindows.find(hWnd);
00377                         if (it != openWindows.end())
00378                         {
00379                                 Window* wnd = it->second;
00380                                 wnd->height = HIWORD(lParam);
00381                                 (wnd->height == 0)?wnd->height = 1 : 1;
00382                                 wnd->width = LOWORD(lParam);            // LoWord=Width, HiWord=Height
00383                         }
00384                         return 0;                                               // Jump Back
00385                 }
00386                 case WM_MOUSEMOVE:
00387                 {
00388                         std::map<HWND,Window*>::iterator it;
00389                         it = openWindows.find(hWnd);
00390                         if (it != openWindows.end())
00391                         {
00392                                 Window* wnd = it->second;
00393                                 wnd->mousePos = Vector3i(LOWORD(lParam), wnd->height - HIWORD(lParam), 0);
00394                         }
00395                         return 0;                                               // Jump Back
00396                 }
00397                 case WM_MOUSEWHEEL:
00398                 {
00399                         int zDelta;
00400                         std::map<HWND,Window*>::iterator it;
00401                         it = openWindows.find(hWnd);
00402                         if (it != openWindows.end())
00403                         {
00404                                 Window* wnd = it->second;
00405                                 zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
00406                                 if (zDelta > 0)
00407                                 {
00408                                         wnd->pushMessage("+WMouse");
00409                                 }
00410                                 else 
00411                                 {
00412                                         wnd->pushMessage("-WMouse");
00413                                 }
00414                         }
00415                         return 0;                                               // Jump Back
00416                 }
00417                 case WM_LBUTTONDOWN:
00418                 {
00419                         std::map<HWND,Window*>::iterator it;
00420                         it = openWindows.find(hWnd);
00421                         if (it != openWindows.end())
00422                         {
00423                                 Window* wnd = it->second;
00424                                 wnd->pushMessage("+LMOUSE");
00425                         }
00426                         return 0;                                               // Jump Back
00427                 }
00428                 case WM_LBUTTONUP:
00429                 {
00430                         std::map<HWND,Window*>::iterator it;
00431                         it = openWindows.find(hWnd);
00432                         if (it != openWindows.end())
00433                         {
00434                                 Window* wnd = it->second;
00435                                 wnd->pushMessage("-LMOUSE");
00436                         }
00437                         return 0;                                               // Jump Back
00438                 }
00439                 case WM_RBUTTONDOWN:
00440                 {
00441                         std::map<HWND,Window*>::iterator it;
00442                         it = openWindows.find(hWnd);
00443                         if (it != openWindows.end())
00444                         {
00445                                 TRACKMOUSEEVENT tme;
00446                                 tme.cbSize = sizeof(tme); 
00447                 tme.dwFlags = TME_LEAVE; 
00448                 tme.hwndTrack = hWnd; 
00449                 TrackMouseEvent(&tme);
00450                                 Window* wnd = it->second;
00451                                 wnd->pushMessage("+RMOUSE");
00452                         }
00453                         return 0;                                               // Jump Back
00454                 }
00455                 case WM_RBUTTONUP:
00456                 {
00457                         std::map<HWND,Window*>::iterator it;
00458                         it = openWindows.find(hWnd);
00459                         if (it != openWindows.end())
00460                         {
00461                                 Window* wnd = it->second;
00462                                 wnd->pushMessage("-RMOUSE");
00463                         }
00464                         return 0;                                               // Jump Back
00465                 }
00466                 case WM_MOUSELEAVE:
00467                 {
00468                         std::map<HWND,Window*>::iterator it;
00469                         it = openWindows.find(hWnd);
00470                         if (it != openWindows.end())
00471                         {
00472                                 Window* wnd = it->second;
00473                                 wnd->pushMessage("-RMOUSE");
00474                                 wnd->pushMessage("-LMOUSE");
00475                         }
00476                         return 0;                                               // Jump Back
00477                 }
00478         }
00479 
00480         // Pass All Unhandled Messages To DefWindowProc
00481         return DefWindowProc(hWnd,uMsg,wParam,lParam);
00482 }

Generated on Tue Jul 17 22:02:22 2007 for C0re by  doxygen 1.5.2