Sign in to follow this  
vertex78

fullscreen problems

Recommended Posts

vertex78    122
I am trying to make my program switch to fullscreen but when I run the program it just switches the desktop display to the different resolution and never shows my program. Then I have to use task manager to end the program. I've looked at the nehe example on switching to fullscreen but I cannot figure out what I am doing wrong. Hopefully someone can point me in the right direction
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

void SetupPixelFormat(HDC hDC);    
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

HDC g_hDC;
bool fullScreen = true;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;
    bool done;
    DEVMODE devModeScreen;
    DWORD extendedWindowstyle;
    DWORD windowstyle;
    
    
    memset(&devModeScreen,0,sizeof(devModeScreen));
    devModeScreen.dmSize = sizeof(devModeScreen);
    devModeScreen.dmPelsWidth = 1024;
    devModeScreen.dmPelsHeight = 768;
    devModeScreen.dmBitsPerPel = 32;
    devModeScreen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | CDS_FULLSCREEN | DM_BITSPERPEL;
    
    
   	wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = NULL;//(HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "MyClass";
    wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
  	
    //register window class
    if(!RegisterClassEx(&wc)){
        MessageBox(NULL,"failure registering!","error",MB_ICONEXCLAMATION);
        return 0;
    }
    
    if(ChangeDisplaySettings(&devModeScreen,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        fullScreen = false;
        
    if(fullScreen){
        extendedWindowstyle = WS_EX_APPWINDOW;
        windowstyle = WS_POPUP;
        ShowCursor(false);
    }
    else {
        extendedWindowstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
        windowstyle = WS_OVERLAPPEDWINDOW;
    }
    
    //create instance of window
    hwnd = CreateWindowEx(extendedWindowstyle,
                          "MyClass",
                          "app1",
                          windowstyle, 
                          0,0,            //x,y
                          1024,768,            //width,height
                          NULL,               //handle to parent
                          NULL,               //handle to menu
                          hInstance,          //app instance
                          NULL);              //no extra params
                          
    if(!hwnd) return 0;
    
    done = false;
    
    while(!done) 
    { 
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        { 
            if(msg.message == WM_QUIT) break; 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } 

        //render here
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        glTranslatef(0.0f,0.0f,-5.0f); // move back 5 units 
        glColor3f(1.0f,0.0f,0.0f);
        glBegin(GL_TRIANGLES);
            glVertex3f(0.0f,0.0f,0.0f);
            glVertex3f(1.0f,0.0f,0.0f);
            glVertex3f(1.0f,1.0f,0.0f);
        glEnd();
            
        SwapBuffers(g_hDC);
            
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        
    }
    
    return msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
        PAINTSTRUCT paintStruct;
        static HDC hDC;     
        static HGLRC hRC;
        int width,height;
        
        switch(message)
        {
            case WM_CREATE:
                 hDC = GetDC(hwnd);
                 g_hDC = hDC;
                 SetupPixelFormat(hDC);
                 hRC = wglCreateContext(hDC);
                 wglMakeCurrent(hDC,hRC);
                 break;
                 
            case WM_CLOSE:
                 ChangeDisplaySettings(NULL,0);				
		         ShowCursor(TRUE);						
                 PostQuitMessage(0);
                 return 0;
                 break;
            
            case WM_SIZE:
                 height = HIWORD(lParam);
                 width = LOWORD(lParam);
                 
                 if(height ==0) height = 1;
                 
                 //reset glviewport to new dimensions of window
                 glViewport(0,0,width,height);
                 glMatrixMode(GL_PROJECTION);
                 glLoadIdentity();
                 gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
                 glMatrixMode(GL_MODELVIEW);
                 glLoadIdentity();
                 return 0;
                 break;
                     
            default:
                break;
        }
        
        return (DefWindowProc(hwnd,message,wParam,lParam));
}   

void SetupPixelFormat(HDC hDC){
     int nPixelFormat; //pixel format index
     
     static PIXELFORMATDESCRIPTOR pfd = {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW |
            PFD_SUPPORT_OPENGL |
            PFD_DOUBLEBUFFER |
            PFD_TYPE_RGBA,
            32,
            0,0,0,0,0,0,
            0,0,0,0,0,0,0,
            16,0,0,
            PFD_MAIN_PLANE,
            0,0,0,0};
     
     nPixelFormat = ChoosePixelFormat(hDC,&pfd); //returns best matching pixel format
     SetPixelFormat(hDC,nPixelFormat,&pfd); //set pixel format of device context hDC
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this