Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

displaycard_opengl_win32.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: displaycard_opengl_win32.cpp,v 1.7 2001/03/06 18:51:06 japj Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 #include <windows.h>
00017 #include "displaycard_opengl_win32.h"
00018 #include "Display/Display/DirectDraw/display_dx.h"
00019 #include "Core/System/Win32/init_win32.h"
00020 #include "API/Core/System/clanstring.h"
00021 #include "API/Core/System/error.h"
00022 
00023 extern LONG WINAPI MainMessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00024 
00025 HGLRC CL_DisplayCard_OpenGL_Win32::context = NULL;
00026 HDC CL_DisplayCard_OpenGL_Win32::hdc = NULL;
00027 
00028 void CL_DisplayCard_OpenGL_Win32::add_display()
00029 {
00030         CL_Display::cards.push_back(
00031                 new CL_DisplayCard_OpenGL_Win32(CL_Display::cards.size()));
00032 }
00033 
00034 CL_DisplayCard_OpenGL_Win32::CL_DisplayCard_OpenGL_Win32(int card_no)
00035 : CL_GL_DisplayCard_Generic(card_no)
00036 {
00037         initialized = false;
00038 }
00039 
00040 CL_DisplayCard_OpenGL_Win32::~CL_DisplayCard_OpenGL_Win32()
00041 {
00042         if (!initialized) return;
00043         wglDeleteContext(context);
00044         destroy_window();
00045 
00046         if (is_fullscreen()) ChangeDisplaySettings(NULL, 0);
00047 }
00048 
00049 void CL_DisplayCard_OpenGL_Win32::flip_display(bool sync)
00050 {
00051         if (!initialized) return;
00052 
00053         BOOL retval = SwapBuffers(hdc);
00054         cl_assert(retval == TRUE);
00055 }
00056 
00057 void CL_DisplayCard_OpenGL_Win32::put_display(const class CL_Rect &rect)
00058 {
00059         cl_assert(false); // hmm - how do you do this under opengl?
00060 }
00061 
00062 void CL_DisplayCard_OpenGL_Win32::set_videomode(
00063         int width,
00064         int height,
00065         int bpp,
00066         bool fullscreen,
00067         bool allow_resize,
00068         bool video_memory)
00069 {
00070         if (fullscreen)
00071         {
00072                 DEVMODE devmode;
00073                 memset(&devmode, 0, sizeof(DEVMODE));
00074 
00075                 devmode.dmSize = sizeof(DEVMODE);
00076                 devmode.dmPelsWidth = width;
00077                 devmode.dmPelsHeight = height;
00078                 devmode.dmBitsPerPel = bpp;
00079 
00080                 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
00081 
00082                 LONG err = ChangeDisplaySettings(&devmode, CDS_FULLSCREEN);
00083 
00084                 if (err != DISP_CHANGE_SUCCESSFUL)
00085                 {
00086                         switch (err)
00087                         {
00088                         case DISP_CHANGE_RESTART:
00089                                 throw CL_Error("This OS sucks! It wants you to restart your computer to change to fullscreen!!!");
00090 
00091                         case DISP_CHANGE_FAILED:
00092                                 throw CL_Error("The display driver failed the specified graphics mode.");
00093 
00094                         case DISP_CHANGE_BADMODE:
00095                                 throw CL_Error("The graphics mode is not supported.");
00096 
00097                         default:
00098                                 {
00099                                         CL_String errorstring;
00100                                         errorstring << "Could not change videomode, error " << (unsigned short)err;
00101                                         throw CL_Error(errorstring.get_string());
00102                                 }
00103 
00104                         }
00105                 }
00106         }
00107 
00108         if (!initialized)
00109         {
00110                 create_window(width, height, bpp, fullscreen, allow_resize);
00111 
00112                 hdc = GetDC(get_hwnd());
00113 
00114                 PIXELFORMATDESCRIPTOR pfd =
00115                 {
00116                         sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd 
00117                         1,                              // version number
00118                         PFD_DRAW_TO_WINDOW |            // support window
00119                         PFD_SUPPORT_OPENGL |            // support OpenGL
00120                         PFD_DOUBLEBUFFER |              // double buffered
00121                         PFD_DEPTH_DONTCARE,             // do you care?
00122                         PFD_TYPE_RGBA,                  // RGBA type
00123                         24,                             // 24-bit color depth
00124                         0, 0, 0, 0, 0, 0,               // color bits ignored
00125                         0,                              // no alpha buffer
00126                         0,                              // shift bit ignored
00127                         0,                              // no accumulation buffer
00128                         0, 0, 0, 0,                     // accum bits ignored
00129                         32,                             // 32-bit z-buffer
00130                         0,                              // no stencil buffer
00131                         0,                              // no auxiliary buffer
00132                         PFD_MAIN_PLANE,                 // main layer
00133                         0,                              // reserved
00134                         0, 0, 0                         // layer masks ignored
00135                 };
00136 
00137                 HDC hdc = GetDC(get_hwnd());
00138                 int pixelformat = ChoosePixelFormat(hdc, &pfd);
00139                 SetPixelFormat(hdc, pixelformat, &pfd);
00140 
00141                 context = wglCreateContext(hdc);
00142                 cl_assert(CL_DisplayCard_OpenGL_Win32::context != NULL);
00143 
00144                 BOOL retval = wglMakeCurrent(
00145                         hdc,
00146                         context);
00147 
00148                 cl_assert(retval == TRUE);
00149 
00150                 if (get_hwnd() == NULL || hdc == NULL)
00151                 {
00152                         cl_assert(false);
00153                 }
00154 
00155                 ShowWindow(get_hwnd(), SW_SHOW);
00156                 UpdateWindow(get_hwnd());
00157         }
00158 
00159         set_gfxmode(width, height, bpp, fullscreen, allow_resize);
00160 
00161         max_texture_size = 0;
00162         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
00163 
00164         m_uses_gl = true;
00165 
00166         initialized = true;
00167 }

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001