00001
00007 #include "OpenGLApp.h"
00008
00009
00010
00011
00012 bool OpenGLApp::createWindow(HINSTANCE hInstance) {
00013 DWORD windowStyle = WS_OVERLAPPEDWINDOW;
00014 DWORD windowExtendedStyle = WS_EX_APPWINDOW;
00015
00016
00017
00018 PIXELFORMATDESCRIPTOR pfd =
00019 {
00020 sizeof(PIXELFORMATDESCRIPTOR),
00021 1,
00022 PFD_DRAW_TO_WINDOW |
00023 PFD_SUPPORT_OPENGL |
00024 PFD_DOUBLEBUFFER,
00025 PFD_TYPE_RGBA,
00026 g_bitsPerPixel,
00027 0, 0, 0, 0, 0, 0,
00028 0,
00029 0,
00030 0,
00031 0, 0, 0, 0,
00032 16,
00033 1,
00034 0,
00035 PFD_MAIN_PLANE,
00036 0,
00037 0, 0, 0
00038 };
00039
00040 GLuint PixelFormat;
00041
00042 if (g_fullscreen) {
00043 if (changeScreenResolution(g_fullscreenWidth, g_fullscreenHeight, g_bitsPerPixel)) {
00044
00045
00046 }
00047 else {
00048
00049 MessageBox(HWND_DESKTOP, "Could not set display mode.\nRunning in windowed mode", "Error", MB_OK | MB_ICONEXCLAMATION);
00050 g_fullscreen = false;
00051 }
00052 }
00053
00054 int x, y, w, h;
00055 if (g_fullscreen) {
00056 x = y = 0;
00057 w = g_fullscreenWidth;
00058 h = g_fullscreenHeight;
00059 windowStyle = WS_POPUP;
00060 windowExtendedStyle |= WS_EX_TOPMOST;
00061 }
00062 else {
00063 x = g_windowLeft;
00064 y = g_windowTop;
00065 w = g_windowRight - g_windowLeft;
00066 h = g_windowBottom - g_windowTop;
00067 }
00068
00069 hWnd = CreateWindowEx(windowExtendedStyle,
00070 "3DGameClass",
00071 "DeFacto",
00072 windowStyle,
00073 x, y,
00074 w, h,
00075 HWND_DESKTOP,
00076 NULL,
00077 hInstance,
00078 NULL);
00079
00080 if (!hWnd)
00081 return false;
00082
00083 if (!(hDC = GetDC(hWnd)))
00084 return false;
00085
00086 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
00087 return false;
00088
00089 if (!(SetPixelFormat(hDC, PixelFormat, &pfd)))
00090 return false;
00091
00092 if (!(hRC = wglCreateContext(hDC)))
00093 return false;
00094
00095 if (!wglMakeCurrent(hDC, hRC))
00096 return false;
00097
00098
00099
00100 g_visible = true;
00101
00102 initExtensions(hDC);
00103
00104 renderer = new OpenGLRenderer();
00105
00106 ShowWindow(hWnd, SW_NORMAL);
00107 SetForegroundWindow(hWnd);
00108 SetFocus(hWnd);
00109
00110 setViewport(w, h);
00111
00112 return true;
00113 }
00114
00115
00116
00117
00118 bool OpenGLApp::changeScreenResolution(unsigned int width, unsigned int height, unsigned int bitsPerPixel) {
00119 DEVMODE dm;
00120 ZeroMemory(&dm, sizeof(DEVMODE));
00121 dm.dmSize = sizeof(DEVMODE);
00122 dm.dmPelsWidth = width;
00123 dm.dmPelsHeight = height;
00124 dm.dmBitsPerPel = bitsPerPixel;
00125 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
00126 if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
00127 return false;
00128 return true;
00129 }
00130
00131
00132
00133
00134 void OpenGLApp::destroyWindow() {
00135 delete renderer;
00136
00137 if (hRC)
00138 wglDeleteContext(hRC);
00139
00140 if (hDC)
00141 ReleaseDC(hWnd, hDC);
00142
00143 if (hWnd)
00144 DestroyWindow(hWnd);
00145
00146 hRC = 0;
00147 hDC = 0;
00148 hWnd = 0;
00149
00150 if (g_fullscreen) {
00151 resetDisplayMode();
00152 showCursor(true);
00153 }
00154
00155
00156
00157 }
00158
00159
00160
00161
00162 void OpenGLApp::setViewport(unsigned int width, unsigned int height) {
00163
00164
00165 glViewport(0, 0, (GLsizei)(width), (GLsizei)(height));
00166 glMatrixMode(GL_PROJECTION);
00167 glLoadIdentity();
00168 gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height), 1.0f, 100.0f);
00169 glMatrixMode(GL_MODELVIEW);
00170 glLoadIdentity();
00171 }