00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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))
00052 {
00053 {
00054 TranslateMessage(&msg);
00055 DispatchMessage(&msg);
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
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;
00086 DWORD dwStyle;
00087 RECT WindowRect;
00088 WindowRect.left=(long)0;
00089 WindowRect.right=(long)width;
00090 WindowRect.top=(long)0;
00091 WindowRect.bottom=(long)height;
00092 hInstance = GetModuleHandle(NULL);
00093
00094
00095 WNDCLASS wc;
00096 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
00097 wc.lpfnWndProc = (WNDPROC) this->WndProc;
00098 wc.cbClsExtra = 0;
00099 wc.cbWndExtra = 0;
00100 wc.hInstance = hInstance;
00101 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
00102 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
00103 wc.hbrBackground = NULL;
00104 wc.lpszMenuName = NULL;
00105 wc.lpszClassName = "OpenGL";
00106
00107 if (!RegisterClass(&wc))
00108 {
00109
00110 return 0;
00111 }
00112
00113 if (fullscreen)
00114 {
00115 DEVMODE dmScreenSettings;
00116 memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
00117 dmScreenSettings.dmSize=sizeof(dmScreenSettings);
00118 dmScreenSettings.dmPelsWidth = width;
00119 dmScreenSettings.dmPelsHeight = height;
00120 dmScreenSettings.dmBitsPerPel = depth;
00121 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
00122
00123
00124 if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
00125 {
00126
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;
00130
00131 }
00132 else
00133 {
00134
00135
00136 return 0;
00137 }
00138 }
00139 }
00140
00141
00142 if (fullscreen)
00143 {
00144 dwExStyle=WS_EX_APPWINDOW;
00145 dwStyle=WS_POPUP;
00146
00147 }
00148 else
00149 {
00150 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
00151 dwStyle=WS_OVERLAPPEDWINDOW;
00152 }
00153
00154 if (!(hWnd=CreateWindowEx(
00155 dwExStyle,
00156 "OpenGL",
00157 name.c_str(),
00158 WS_CLIPSIBLINGS |
00159 WS_CLIPCHILDREN |
00160 dwStyle,
00161 0, 0,
00162 WindowRect.right-WindowRect.left,
00163 WindowRect.bottom-WindowRect.top,
00164 NULL,
00165 NULL,
00166 hInstance,
00167 NULL)))
00168 {
00169 killWindow();
00170
00171 return 0;
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
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
00204 return 0;
00205 }
00206
00207 hRC = wglCreateContext(hDC);
00208 if (!hRC)
00209 {
00210
00211 return 0;
00212 }
00213
00214 ShowWindow(hWnd,SW_SHOW);
00215 SetForegroundWindow(hWnd);
00216 SetFocus(hWnd);
00217 return ret;
00218
00219 }
00220
00221 int Window::killWindow()
00222 {
00223 int ret = 0;
00224
00225 if (fullscreen)
00226 {
00227
00228 ChangeDisplaySettings(NULL,0);
00229 ShowCursor(TRUE);
00230 }
00231
00232 if (hRC)
00233 {
00234 if (!wglDeleteContext(hRC))
00235 {
00236
00237 return -1;
00238 }
00239 hRC=NULL;
00240 }
00241
00242 if (hDC && !ReleaseDC(hWnd,hDC))
00243 {
00244
00245 hDC=NULL;
00246 return -1;
00247 }
00248
00249 openWindows.erase(hWnd);
00250
00251 if (hWnd && !DestroyWindow(hWnd))
00252 {
00253
00254 hWnd=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
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()
00291 {
00292 HFONT font;
00293 HFONT oldfont;
00294
00295 int base = -100;
00296
00297 font = CreateFont( 25,
00298 0,
00299 0,
00300 0,
00301 700,
00302 FALSE,
00303 FALSE,
00304 FALSE,
00305 ANSI_CHARSET,
00306 OUT_TT_PRECIS,
00307 CLIP_DEFAULT_PRECIS,
00308 ANTIALIASED_QUALITY,
00309 FF_DONTCARE|DEFAULT_PITCH,
00310 "Courier New");
00311
00312 oldfont = (HFONT)SelectObject(hDC, font);
00313 wglUseFontBitmaps(hDC, 32, 96, base);
00314 SelectObject(hDC, oldfont);
00315 DeleteObject(font);
00316 return base;
00317 }
00318
00319
00320
00321 LRESULT CALLBACK Window::WndProc( HWND hWnd,
00322 UINT uMsg,
00323 WPARAM wParam,
00324 LPARAM lParam)
00325 {
00326 switch (uMsg)
00327 {
00328 case WM_SYSCOMMAND:
00329 {
00330 switch (wParam)
00331 {
00332 case SC_SCREENSAVE:
00333 case SC_MONITORPOWER:
00334 return 0;
00335 }
00336 break;
00337 }
00338 case WM_CLOSE:
00339 {
00340 PostQuitMessage(0);
00341 return 0;
00342 }
00343 case WM_KEYDOWN:
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;
00357 }
00358 case WM_KEYUP:
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;
00372 }
00373 case WM_SIZE:
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);
00383 }
00384 return 0;
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;
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;
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;
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;
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;
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;
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;
00477 }
00478 }
00479
00480
00481 return DefWindowProc(hWnd,uMsg,wParam,lParam);
00482 }