Results 1 to 5 of 5

Thread: SetPixelFormat freezes

  1. #1
    Member Newbie
    Join Date
    Feb 2001
    Location
    Atlanta Georgia
    Posts
    32

    SetPixelFormat freezes

    running the following initialization code, the program will cause my computer to freeze when executing SetPixelFormat(...). I have not tested it on other system, and if anyone else could, that would be awesome. Here is .c file contents:

    Code :
    #include <stdarg.h>
    #include "mgl.h"
     
    /***** Static Prototypes ******/
    static boolean createWindow(context * nglc);
    static void destroyWindow(context * nglc);
    static boolean createGL(context * nglc);
    static void destroyGL(context * nglc);
     
    void createContext(context **t,int height,int width,int bits,char *name)
    {
      if( t!=NULL &amp;&amp; (*t = (context *)malloc(sizeof(context))) != NULL )
        {
          (*t)->height = height;
          (*t)->width = width;
          (*t)->bits = bits;
          (*t)->hInst = NULL;
          (*t)->hWnd = NULL;
          (*t)->hDC = NULL;
          (*t)->hRC = NULL;
          (*t)->name = (name!=NULL?strdup(name):strdup(""));
        }
    }
     
    void deleteContext(context **t)
    {
      if( (t != NULL) &amp;&amp; (*t != NULL) )
        {
          if( (*t)->name != NULL )
    	free((*t)->name);
          free(*t);
          *t = NULL;
        }
    }
     
    boolean initGL(context * nglc)
    {
      if(createWindow(nglc) &amp;&amp; createGL(nglc))
        return true;
      killGL(nglc);
      return false;
    }
     
    void killGL(context * nglc)
    {
      destroyGL(nglc);
      destroyWindow(nglc);
    }
     
    /* Creates a window for use by initGL */
    static boolean createWindow(context *nglc)
    {
      WNDCLASSEX wcx;
      DEVMODE devmode;
     
      memset(&amp;wcx,0,sizeof(WNDCLASSEX));
      wcx.cbSize = sizeof(WNDCLASSEX);
      wcx.style = CS_OWNDC;
      wcx.lpfnWndProc = msgHandler;
      if( (wcx.hInstance = (nglc->hInst = GetModuleHandle(NULL))) == NULL) return false;
      wcx.lpszClassName = "mglClass";  
     
      memset(&amp;devmode,0,sizeof(DEVMODE));
      devmode.dmSize = sizeof(devmode);
      devmode.dmPelsWidth = nglc->width;
      devmode.dmPelsHeight = nglc->height;
      devmode.dmBitsPerPel = nglc->bits;
      devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
     
      if( RegisterClassEx(&amp;wcx) == 0 ) return false;
     
      if( (nglc->hWnd = CreateWindowEx(WS_EX_APPWINDOW,"mglClass",nglc->name,CS_OWNDC,0,0,nglc->width,nglc->height,NULL,NULL,nglc->hInst,NULL)) == NULL ) return false;
     
      if( ChangeDisplaySettings(&amp;devmode,0) != DISP_CHANGE_SUCCESSFUL ) return false;
     
      ShowWindow(nglc->hWnd,SW_SHOWNORMAL);
      ShowCursor(false);
      SetCapture(nglc->hWnd);
     
      return true;
    }
     
    /* destroys a initGL window */
    static void destroyWindow(context * nglc)
    {
      ReleaseCapture();
      ShowCursor(true);
      ChangeDisplaySettings(NULL,0);
      if( nglc->hWnd )
        {
          DestroyWindow(nglc->hWnd);
          nglc->hWnd = NULL;
        }
      if( nglc->hInst )
        {
          UnregisterClass("mglClass",nglc->hInst);
          nglc->hInst = NULL;
        }
    }
     
    static boolean createGL(context * nglc)
    {
      PIXELFORMATDESCRIPTOR pfd;
     
      memset(&amp;pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
      pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
      pfd.nVersion = 1;
      pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL	| PFD_DOUBLEBUFFER;
      pfd.iPixelType = PFD_TYPE_RGBA;
      pfd.cColorBits = nglc->bits;
      /* pfd.cAccumBits = 0; */
      pfd.cDepthBits = nglc->bits;
      /* pfd.cStencilBits = 0; */ 
      pfd.iLayerType = PFD_MAIN_PLANE;
     
      if( !(nglc->hDC = GetDC(nglc->hWnd)) ) return false;
      if( !SetPixelFormat(nglc->hDC,ChoosePixelFormat(nglc->hDC,&amp;pfd),&amp;pfd) ) return false;
      if( !(nglc->hRC = wglCreateContext(nglc->hDC)) ) return false;
      if( !wglMakeCurrent(nglc->hDC,nglc->hRC) ) return false;
     
      return true;
    }
     
    static void destroyGL(context * nglc)
    {
      wglMakeCurrent(NULL,NULL);
      if( nglc->hRC )
        {
          wglDeleteContext(nglc->hRC);
          nglc->hRC = NULL;
        }
      if( nglc->hDC )
        {
          ReleaseDC(nglc->hWnd,nglc->hDC);
          nglc->hDC = NULL;
        }
    }
    and the accompaning header

    Code :
    #ifndef MGLH
    #define MGLH
     
    /***** include files ******/
    #include<windows.h>
    #include<stdio.h>
    #include<gl/gl.h>
    #include<gl/glu.h>
    #include<gl/glaux.h>
     
    /***** structures ******/
    typedef struct
    {
      int height;
      int width;
      int bits;
      HINSTANCE hInst;
      HWND hWnd;
      HGLRC hRC;
      HDC hDC;
      char *name;
    } context;
     
    #ifndef BOOLEAN
    #define BOOLEAN
    typedef enum{false,true} boolean;
    #endif
    /***** gl prototypes ******/
     
    /* window message handler-MUST BE DEFINED BY USER */
    LRESULT msgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
     
    /* creates a new pointer at t, with preset values: height, width, bits, and name */
    void createContext(context **t,int height,int width,int bits,char *name);
     
    /* deletes a existing context, and frees all associated memory */
    void deleteContext(context **t);
     
    /* PRECONDITION: nglc is not NULL
     * initializes and starts a rendering context */
    boolean initGL(context *nglc);
     
    /* PRECONDITION: nglc is not NULL
     * destroys and resets nglc */
    void killGL(context *nglc);
     
    #endif
    This is the main .c file:

    Code :
    /***** global includes ******/
    #include <stdio.h>
     
    /***** local includes ******/
    #include "mgl.h"
     
    /***** typedefs ******/
    #ifndef BOOLEAN
    #define BOOLEAN
    typedef enum{false,true} boolean;
    #endif
     
    /***** global static variables ******/
    static boolean loop = true;
     
    /***** code ******/
    int main(int argc,char *argv[])
    {
      context *nglc;
      MSG msg;
     
      createContext(&amp;nglc,768,1024,32,"default");
      initGL(nglc);
      while(loop)
        {
          if( PeekMessage(&amp;msg,nglc->hWnd,0,0,PM_REMOVE) ) 
    	DispatchMessage(&amp;msg);
        }
      killGL(nglc);
      deleteContext(&amp;nglc);
      return 0;
    }
     
    LRESULT msgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
      switch(uMsg)
        {
        case(WM_KEYDOWN): 
          loop = false;
        default:
          break;
        }
      return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    Give it a try, and see if it has the same results. Be aware the freeze requires a reboot.

    Thanks,
    Cardinal
    "Close before striking. Keep away from Children"--Strike on the box matches.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,595

    Re: SetPixelFormat freezes

    Hi !

    A good start would be to check what ChoosePixelFormat() returns, can you run other OpenGL apps without problems ?, just so you don't have a wacky driver.

    I can't test it, don't have mgl installed.

    Mikael

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Dec 2000
    Location
    Reutlingen, Germany
    Posts
    2,042

    Re: SetPixelFormat freezes

    The first thing you should do, is to go to nvidias site and download their pixelformat-demo program. If it crashes too, you have the wrong driver.

    Some time ago nVidia released a driver, which had a bug in the pixelformat extension. If you still have this driver, you should download the latest one, the bug is fixed there.

    Jan.
    GLIM - Immediate Mode Emulation for GL3

  4. #4
    Member Newbie
    Join Date
    Feb 2001
    Location
    Atlanta Georgia
    Posts
    32

    Re: SetPixelFormat freezes

    I downloaded a random lesson from nehe.gamedev.net and it ran just fine; i've also been running opengl games, and other programs without problems before-so I am fairly certain its a code/language dependant problem.

    I am compiling the code with lcc-win32, and this is first program I have done outside of Borland Builder.

    In the first version, which suffered from the same problem, i had a
    Code :
    if( ChoosePixelFormat(...) <= 0 ) return false;
    but always returned valid formats, and even an invalid format should only make SetPixelFormat return false.

    [This message has been edited by Cardinal (edited 12-26-2002).]
    "Close before striking. Keep away from Children"--Strike on the box matches.

  5. #5
    Member Newbie
    Join Date
    Feb 2001
    Location
    Atlanta Georgia
    Posts
    32

    Re: SetPixelFormat freezes

    I am not exactly sure why SetPixelFormat triggered the freezes, but after correctly declaring:
    Code :
    LRESULT CALLBACK msgHandler(...)
    the code worked gracefully. G'luck to any of you with similarly strange and painstaking bugs.
    "Close before striking. Keep away from Children"--Strike on the box matches.

Similar Threads

  1. SetPixelFormat with WGL_ARB_pixel_format
    By Acheum in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-28-2003, 05:36 AM
  2. SetPixelFormat
    By link19 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-23-2001, 12:45 PM
  3. SetPixelFormat()
    By skippyj777 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-13-2000, 04:31 PM
  4. SetPixelFormat
    By jgrashow in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 05-26-2000, 09:05 AM
  5. SetPixelFormat
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-11-2000, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean