Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

o_gfx.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/18 14:12:16 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: o_gfx.cpp,v 1.6 2002/12/18 14:12:16 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: o_gfx.cpp,v $
00030   Revision 1.6  2002/12/18 14:12:16  kolrabi
00031   *** empty log message ***
00032 
00033   Revision 1.5  2002/12/13 12:29:26  kolrabi
00034   fixed some bugs
00035 
00036   Revision 1.4  2002/12/12 13:13:39  kolrabi
00037   made the fullscreen work under X11
00038 
00039   Revision 1.3  2002/12/11 16:35:43  kolrabi
00040   sound is now part of the engine
00041 
00042   Revision 1.2  2002/12/08 20:08:15  kolrabi
00043   Now really runs under X11
00044 
00045   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00046   initial release
00047 
00048 
00049 */
00050 
00060 #include            "omicron/internal.h"
00061 #include            <string.h>
00062 
00063 
00064 #if !defined(WIN32)
00065 
00066 #include            <X11/extensions/xf86vmode.h>
00067 
00068 static XF86VidModeModeInfo oldmode;
00069 
00070 #endif
00071 
00072 /**************************************************************************** 
00073  **************************************************************************** 
00074  * GFX ROUTINES ************************************************************* 
00075  **************************************************************************** 
00076  ****************************************************************************/ 
00077 
00078 /****************************************************************************
00079  * _gfx_setmode                                           set graphics mode *
00080  ****************************************************************************/
00081 int _gfx_setmode() 
00082 { 
00083 #if defined(WIN32) 
00084     int bpp = gv.init.bpp;
00085 
00086     _log_printf(MSG_NORMAL, "setting full screen"); 
00087     DEVMODE devmode; 
00088  
00089     ZeroMemory(&devmode, sizeof(DEVMODE)); 
00090  
00091     devmode.dmSize      =   sizeof(DEVMODE); 
00092     devmode.dmPelsWidth =   gv.init.hres;   // Selected Screen Width 
00093     devmode.dmPelsHeight=   gv.init.vres;         // Selected Screen Height 
00094     devmode.dmBitsPerPel=   bpp;            // Selected Bits Per Pixel 
00095     devmode.dmFields    =   DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; 
00096 
00097     // try this for color depth 32/24/16/8 bits 
00098     while((signed)bpp>0) 
00099     { 
00100         _log_printf(MSG_DEBUG, "trying %u bpp",bpp); 
00101         devmode.dmBitsPerPel= bpp; 
00102         if ( ChangeDisplaySettings(&devmode, CDS_FULLSCREEN) == 
00103             DISP_CHANGE_SUCCESSFUL ) 
00104             break; 
00105  
00106         bpp -= 8; 
00107     } 
00108  
00109     // could not set display mode
00110     if (!bpp) 
00111     { 
00112         ChangeDisplaySettings(NULL, 0); 
00113         _log_printf(MSG_ERROR, 
00114         "failed to set full screen. try command line option -w."); 
00115         return 0; 
00116     } 
00117 #else  // not WIN32
00118 
00119     XF86VidModeModeInfo **modes;
00120     int modeNum;
00121     int bestMode;
00122     Atom wmDelete;
00123     Window winDummy;
00124     unsigned int borderDummy;
00125 
00126     XF86VidModeGetAllModeLines(gv.init.dpy, 0, &modeNum, &modes);
00127     /* save desktop-resolution before switching modes */
00128 //    XF86VidModeModeLine curmode;
00129 //    XF86VidModeGetModeLine(gv.init.dpy, 0, NULL, &curmode);
00130     oldmode = *modes[modeNum-1];
00131     /* look for mode with requested resolution */
00132     for (int i = 0; i < modeNum; i++)
00133     {
00134         if ((modes[i]->hdisplay == gv.init.hres) && 
00135             (modes[i]->vdisplay == gv.init.vres))
00136         {
00137             bestMode = i;
00138         }
00139 /*
00140         if ((modes[i]->hdisplay == curmode.hdisplay) &&
00141             (modes[i]->vdisplay == curmode.vdisplay))
00142         {
00143             oldmode = *modes[i];
00144         }*/
00145     }
00146 
00147     XF86VidModeSwitchToMode(gv.init.dpy, 0, modes[bestMode]);
00148     XF86VidModeSetViewPort(gv.init.dpy, 0, 0, 0);
00149     gv.init.hres = modes[bestMode]->hdisplay;
00150     gv.init.vres = modes[bestMode]->vdisplay;
00151     XFree(modes);
00152     
00153     XWarpPointer(gv.init.dpy, None, gv.init.wnd, 0, 0, 0, 0, 0, 0);
00154     XWarpPointer(gv.init.dpy, None, gv.init.wnd, 0, 0, 0, 0, gv.init.hres, gv.init.vres);
00155     XWarpPointer(gv.init.dpy, None, gv.init.wnd, 0, 0, 0, 0, gv.init.hres/2, gv.init.vres/2);
00156 
00157 
00158     /* vars to make blank cursor */
00159     Pixmap blank;
00160     XColor dummy;
00161     char data[1] = {0};
00162     Cursor cursor;
00163     
00164     /* make a blank cursor */
00165     blank = XCreateBitmapFromData (gv.init.dpy, gv.init.wnd, data, 1, 1);
00166     cursor = XCreatePixmapCursor(gv.init.dpy, blank, blank, &dummy, &dummy, 0, 0);
00167     XFreePixmap (gv.init.dpy, blank);
00168     
00169     XGrabPointer(gv.init.dpy, gv.init.wnd, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, gv.init.wnd, cursor, CurrentTime);
00170     XMapRaised(gv.init.dpy, gv.init.wnd);
00171         
00172 #endif // not WIN32
00173     return 1; 
00174 } 
00175  
00176  
00177 /**************************************************************************** 
00178  * _gfx_init                                           initializes graphics * 
00179  ****************************************************************************/ 
00180 ulong _gfx_init                       // returns a nonzero value if successful 
00181 ( 
00182     ushort          /* nWidth  */,      // desired width 
00183     ushort          /* nHeight */,      // and height of screen 
00184     ulong           bFullscreen         // set to 1 for fullscreen 
00185 ) 
00186 { 
00187     _log_printf(MSG_DEBUG, "- _gfx_init() --------------------------"); 
00188  
00189 #if defined(WIN32)
00190 
00191     int                     pixel_format;
00192     PIXELFORMATDESCRIPTOR   pfd=                // pfd Tells Windows How We Want Things To Be
00193     {
00194         sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
00195         1,                                          // Version Number
00196         PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
00197         PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
00198         PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
00199         PFD_TYPE_RGBA,                              // Request An RGBA Format
00200         gv.init.bpp,                                // Select Our Color Depth
00201         0, 0, 0, 0, 0, 0,                           // Color Bits Ignored
00202         0,                                          // No Alpha Buffer
00203         0,                                          // Shift Bit Ignored
00204         0,                                          // No Accumulation Buffer
00205         0, 0, 0, 0,                                 // Accumulation Bits Ignored
00206         gv.init.zbuffer,                            // Z-Buffer (Depth Buffer)  
00207         0,                                          // No Stencil Buffer
00208         0,                                          // No Auxiliary Buffer
00209         PFD_MAIN_PLANE,                             // Main Drawing Layer
00210         0,                                          // Reserved
00211         0, 0, 0                                     // Layer Masks Ignored
00212     };
00213 
00214     _log_printf(MSG_DEBUG, "getting device context for window"); 
00215     gv.hdc = GetDC((HWND)gv.init.wnd); 
00216     if (!gv.hdc)
00217     {
00218         if (bFullscreen) 
00219             ChangeDisplaySettings(NULL, 0); 
00220  
00221         _log_printf(MSG_ERROR,  
00222         "could not get device context for window.."); 
00223         return 0; 
00224     }
00225 
00226     pixel_format = ChoosePixelFormat(gv.hdc, &pfd);
00227  
00228     if (!pixel_format) 
00229     { 
00230         if (bFullscreen) 
00231             ChangeDisplaySettings(NULL, 0); 
00232  
00233         _log_printf(MSG_ERROR,  
00234         "could not find valid, accellerated pixel format.."); 
00235         ReleaseDC((HWND)gv.init.wnd, gv.hdc); 
00236         return 0; 
00237     } 
00238 
00239     _log_printf(MSG_DEBUG, "setting pixel format %u", pixel_format); 
00240     if(!SetPixelFormat(gv.hdc, pixel_format, &pfd)) 
00241     { 
00242         if (bFullscreen) 
00243             ChangeDisplaySettings(NULL, 0); 
00244  
00245         _log_printf(MSG_ERROR, "could not set pixel format..");
00246         ReleaseDC((HWND)gv.init.wnd, gv.hdc); 
00247         return 0; 
00248     } 
00249  
00250     _log_printf(MSG_DEBUG, "creating rendering context..."); 
00251     
00252     if (!(gv.hrc = wglCreateContext(gv.hdc))) 
00253     { 
00254         _log_printf(MSG_ERROR, "couldn't create rendering context...");
00255         ReleaseDC((HWND)gv.init.wnd, gv.hdc);
00256         return 0; 
00257     } 
00258  
00259     _log_printf(MSG_DEBUG, "making rendering context current..."); 
00260  
00261     if (!wglMakeCurrent(gv.hdc, gv.hrc)) 
00262     { 
00263         if (bFullscreen) 
00264             ChangeDisplaySettings(NULL, 0); 
00265  
00266         wglDeleteContext(gv.hrc); 
00267         ReleaseDC((HWND)gv.init.wnd, gv.hdc); 
00268         _log_printf(MSG_ERROR, "could not make rendering context current.."); 
00269         return 0; 
00270     } 
00271 #endif  
00272  
00273     _log_printf(MSG_NORMAL, "OpenGL version     : %s",  
00274                            glGetString(GL_VERSION)); 
00275     _log_printf(MSG_NORMAL, "OpenGL vendor      : %s",  
00276                            glGetString(GL_VENDOR)); 
00277     _log_printf(MSG_NORMAL, "OpenGL renderer    : %s",  
00278                            glGetString(GL_RENDERER)); 
00279 
00280     _log_printf(MSG_DEBUG, "OpenGL extensions  : %s",  
00281                            glGetString(GL_EXTENSIONS));
00282  
00283     // initialize extensions 
00284 
00285 #if defined(WIN32)
00286 
00287     extgl_Initialize();
00288     
00289     RECT r; 
00290 
00291     // bring window to top 
00292     GetClientRect((HWND)gv.init.wnd, &r); 
00293  
00294     SetForegroundWindow((HWND)gv.init.wnd); 
00295     SetFocus((HWND)gv.init.wnd); 
00296     
00297 #else
00298 #endif 
00299  
00300     // set fullscreen 
00301     if (gv.init.fullscreen = bFullscreen) 
00302         _gfx_setmode(); 
00303  
00304     gv.init.nogfx = 0;
00305  
00306 #if defined(WIN32) 
00307     _gfx_resize((ushort)r.right, (ushort)r.bottom, bFullscreen);
00308 #else
00309     _gfx_resize(gv.init.hres,gv.init.vres, bFullscreen);
00310 #endif
00311  
00312     _log_printf(MSG_DEBUG, "----------------------------------------"); 
00313  
00314     return 1; 
00315 } // _gfx_init 
00316  
00317 /**************************************************************************** 
00318  * _gfx_resize                                called when window is resized * 
00319  ****************************************************************************/ 
00320 ulong           _gfx_resize 
00321 ( 
00322     ushort          width,              // new width 
00323     ushort          height,             // and height of window 
00324     ulong           fullscreen          // useless 
00325 ) 
00326 { 
00327     // only if gfx is already initialized 
00328     if (gv.init.nogfx) 
00329         return 0; 
00330  
00331     gv.init.hres = width; 
00332     gv.init.vres = height; 
00333  
00334     // recalculate aspect ratio 
00335     gv.aspect = 1.0f*width / (height?height*1.0f:1.0f); 
00336 
00337     if (!fullscreen) 
00338         if (gv.aspect<1) gv.aspect = 1; 
00339  
00340     glViewport(0, 0, width, height); 
00341 
00342     return 1; 
00343 } // _gfx_resize 
00344  
00345  
00346  
00347 /**************************************************************************** 
00348  * _gfx_deinit                                       deinitializes graphics * 
00349  ****************************************************************************/ 
00350 ulong             _gfx_deinit() 
00351 { 
00352     _log_printf(MSG_DEBUG, "- _gfx_deinit() ------------------------"); 
00353     _log_printf(MSG_DEBUG, "deinitializing opengl.."); 
00354     
00355 #if defined(WIN32) 
00356     if (gv.init.fullscreen) 
00357     { 
00358         _log_printf(MSG_DEBUG, "resetting display mode.."); 
00359         ChangeDisplaySettings(NULL, 0); 
00360     } 
00361  
00362     wglMakeCurrent(gv.hdc,0); 
00363     wglDeleteContext(gv.hrc); 
00364     ReleaseDC((HWND)gv.init.wnd, gv.hdc); 
00365 #else
00366 
00367     glXMakeCurrent(gv.init.dpy, 0, 0);
00368     glXDestroyContext(gv.init.dpy, gv.glxc);
00369 
00370     XF86VidModeSwitchToMode(gv.init.dpy, 0, &oldmode);
00371     XF86VidModeSetViewPort(gv.init.dpy, 0, 0, 0);
00372 
00373     XUngrabPointer(gv.init.dpy, CurrentTime);
00374     
00375 #endif 
00376  
00377     _log_printf(MSG_DEBUG, "----------------------------------------"); 
00378     return 1; 
00379 } // _gfx_deinit 

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18