Thread: [W32API/OpenGL] How do you make a fullscreen opengl window?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    [W32API/OpenGL] How do you make a fullscreen opengl window?

    I'm using windows api to create a fullscreen window, but this code just changes my screen resolution but fails to create the window:

    (it's 99% example code so don't blame me for the ugly mess, I'm gonna clean it up when I get it working)

    Code:
    #include <windows.h>
    #include <gl/gl.h>
    
    #define WIDTH 1024
    #define HEIGHT 768
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
    WPARAM wParam, LPARAM lParam);
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
    
    int WINAPI WinMain (HINSTANCE inst, HINSTANCE pinst, char *cmd, int cs)
    {
        WNDCLASS wc;
        HWND hwnd;
        HDC hdc;
        HGLRC hrc;        
        MSG msg;
        DEVMODE dmScreenSettings;
    
    
        // register window class 
        wc.style = CS_OWNDC;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = inst;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = NULL;
        wc.lpszMenuName = NULL;
        wc.lpszClassName = "GLSample";
        RegisterClass (&wc);
    
        // create main window
        hwnd = CreateWindowEx(WS_EX_APPWINDOW, 
          "GLSample", "OpenGL Sample", 
          WS_POPUP | WS_CLIPSIBLINGS,
          0, 0, WIDTH, HEIGHT,
          NULL, NULL, inst, NULL);
          
        memset(&dmScreenSettings,0,sizeof(dmScreenSettings));			
    	dmScreenSettings.dmSize		= sizeof(dmScreenSettings);					
    	dmScreenSettings.dmPelsWidth	= WIDTH;						
    	dmScreenSettings.dmPelsHeight	= HEIGHT;					
    	dmScreenSettings.dmBitsPerPel	= 16;							
    	dmScreenSettings.dmFields	= DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    	
    	ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN);
        /* enable OpenGL for the window */
        EnableOpenGL(hwnd, &hdc, &hrc);
    
        /* program main loop */
        while (1)
        {
            /* check for messages */
            if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
            {
                /* handle or dispatch messages */
                if (msg.message == WM_QUIT)
                {
                    break;
                }
                else
                {
                    TranslateMessage (&msg);
                    DispatchMessage (&msg);
                }
            }
            else
            {
                /* OpenGL animation code goes here */
    
                glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
                glClear (GL_COLOR_BUFFER_BIT);
    
                glPushMatrix ();
                glRotatef (0, 0.0f, 0.0f, 1.0f);
                glBegin (GL_TRIANGLES);
                glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
                glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
                glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
                glEnd ();
                glPopMatrix ();
    
                SwapBuffers (hdc);
    
             
                Sleep (1);
            }
        }
        
        ChangeDisplaySettings(NULL,0);	
    
        /* shutdown OpenGL */
        DisableOpenGL (hwnd, hdc, hrc);
    
        /* destroy the window explicitly */
        DestroyWindow (hwnd);
    
        return msg.wParam;
    }
    
    
    /* window procedure */
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                              WPARAM wParam, LPARAM lParam)
    {
        switch (message) {
            case WM_CREATE:
                return 0;
            case WM_CLOSE:
                PostQuitMessage (0);
                return 0;
            case WM_DESTROY:
                return 0;
            case WM_KEYDOWN:
                switch (wParam) {
                    case VK_ESCAPE: PostQuitMessage(0); return 0;
                }
                return 0;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    
    
    /*******************
     * Enable OpenGL
     *
     *******************/
    
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
    {
     	PIXELFORMATDESCRIPTOR pfd;
    	int format;
    	*hDC=GetDC(hWnd);											
    
    	//Set the pixel format for the DC
    	memset(&pfd,0,sizeof(pfd));
    	pfd.nSize = sizeof(pfd);									
    	pfd.nVersion = 1;										
    	pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER; 
    	pfd.iPixelType = PFD_TYPE_RGBA;
    	pfd.cColorBits = 24;										
    	pfd.cDepthBits = 16;							
    
    	format = ChoosePixelFormat(*hDC,&pfd);
    	SetPixelFormat(*hDC,format,&pfd);								
     
    	*hRC=wglCreateContext(*hDC);									
    	wglMakeCurrent(*hDC,*hRC);
    }
    
    
    // Disable OpenGL
    
    
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
    {
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(hRC);
        ReleaseDC(hWnd, hDC);
    }
    what's wrong with it?

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    I dont' have time to look through your code atm (and I've more or less forgot the tiny little bit of win32 api that I used to know) but I used something like this when I programmed on windows.
    http://cboard.cprogramming.com/showthread.php?t=35720

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by Perspective
    I dont' have time to look through your code atm (and I've more or less forgot the tiny little bit of win32 api that I used to know) but I used something like this when I programmed on windows.
    http://cboard.cprogramming.com/showthread.php?t=35720
    Looks promising, I'll look through that. Thanks.

  4. #4

    Join Date
    May 2005
    Posts
    1,042
    You also may want to look into the GLUT library, it handles creating basic windows on virtually any platform.
    I'm not immature, I'm refined in the opposite direction.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by BobMcGee123
    You also may want to look into the GLUT library, it handles creating basic windows on virtually any platform.
    I second that, and you could look here for some starters
    http://cboard.cprogramming.com/showt...ight=EmptyGlut

  6. #6

    Join Date
    May 2005
    Posts
    1,042
    nice, way ahead of me per
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM
  5. how to make 2 text window
    By bluexrogue in forum C Programming
    Replies: 2
    Last Post: 09-15-2001, 08:51 PM