This topic is now archived and is closed to further replies.


Serious window creation problem

Recommended Posts

Merc22    122
i have yet another problem cuz of Dev C++ i wrote all the code to make a simple OpenGl window but when i try to compile it i get 31 errors!!! here is the code(dont get mad cuz its long ) //include all the needed header files #include #include #include #include //This is the OpenGl rendering context HGLRC hRC=NULL; //This is the windows device context HDC hDC=NULL; //This is the handle to the window HWND hWnd; //This is the instance of the window HINSTANCE hInstance; //An array to monitor key presses bool keys[256]; //Variable to tell if the window is minimized or not bool active=true; //Variable to check if the window is fullscreen or not bool fullscreen=true; //This is the prototype for the message handler LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); //This is the function that resizes the winddow GLvoid ReSizeGLScene( GLsizei width, GLsizei height) { //If the height is zero, make it equal 1 if (height==0) { height=1; } //Reset the current viewport glViewport( 0, 0, width, height); //Select the projection matrix glMatrixMode(GL_PROJECTION); //Reset the projection matrix glLoadIdentity(); //Calculate the aspect ratio of the window gluPerspective(45.0f, (GLfloat)width/(Glfloat)height, 0.1f, 100.0f); //Select the modelview matrix glMatrixMode(GL_MODELVIEW); //Reset the modelview matrix glLoadIdentity(); } //This is the function that sets up OpenGL int InitGL(GLvoid) { //Enable smooth shading glShadeModel(GL_SMOOTH); //Set the color of the screen when it clears glClearColor( 0.0f, 0.0f, 0.0f, 0.0f); //Setup the depth buffer glClearDepth( 1.0f); //Enable depth testing glEnable(GL_DEPTH_TEST); //The type of depth test to do glDepthFunc(GL_LEQUAL); //Get the best perspective correction done glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Return true return true; } //This is the function in which we draw everything int DrawGLScene( GLvoid) { //Clear the screen and the depth buffer glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Reset the current modelview matrix glLoadIdentity(); //Return true return true; } //This function kills the window Glvoid KillGLWindow( Glvoid) { //Check if we are in fullscreen mode if (fullscreen) { //Return us back to our original desktop ChangeDisplaySettings( NULL, 0); //Show the cursor ShowCursor(true); } //Check if we have a rendering context if (hRC) { //Try to release the RC and DC contexts if (!wglMakeCurrent( NULL, NULL)) { //Tell the user the RC and DC could not be releases MessageBox( NULL, "Release Of DC And RC Failed.", "Shutdown Error", MB_OK | MB_INFORMATION); } //Can we delete the context if (!wglDeleteContext(hRC)) { //Tell the user the RC could not be released MessageBox( NULL, "Release Rendering Context Failed.", "Shutdown Error", MB_OK |MB_ICONINFORMATION); } //Set hRC to NULL hRC=NULL; } //Can we release the DC if (hDC && !ReleaseDC(hWnd, hDC)) { //Tell the user we can''t release the DC MessageBox( NULL, "Relase Device Contxet Failed", "Shutdown Error", MB_OK | MB_INFORMATION); //Set the DC to NULL hDC=NULL; } //Can we delete the window if (hWnd && !DestroyWindow(hWnd)) { //Tell the user we can''t delete the window MessageBox( NULL, "Could not release hWnd", "Shutdown Error", MB_OK | MB_INFORMATION); //Set hWnd to NULL hWnd=NULL; } //Can we unregister the class if (!UnregisterClass("OpenGL", hInstance)) { //Tell the user we can''t unregister the class MessageBox( NULL, "Could Not Unregister Class", "Shutdown Error", MB_OK | MB_INFORMATION); //Set hInstance to NULL hInstance=NULL; } } //This function creates the OpenGL window bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { //Hold results for the pixel search in this variable GLuint PixelFormat; //The windows class structure WNDCLASS wc; //Window Extended Style DWORD dwExStyle; //Window Style DWORD dwStyle; //Create a RECT RECT WindowRect; //Set left value to 0 WindowRect.left=(long)0; //Set right value to requested width WindowRect.right=(long)width; //Set top value to 0; //Set bottom value to requested height WindowRect.bottom=(long)height; //Fullscreen has to equal fullscreenflag fullscreen=fullscreenflag; //Initiliaze some variables hInstance=GetModuleHandle(NULL); |CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc=(WNDPROC)WndProc; wc.cbclsExtra=0; wc.WndExtra=0; wc.hInstance=hInstance; wc.hIcon=LoadIcon( NULL, IDI_WINLOGO); wc.hCursor=LoadCursor( NULL, IDC_ARROW); wc.hbrbackground=NULL; wc.lpszMenuName=NULL; wc.lpszClassName="OpenGL"; //Register the class if (!RegisterClass(&wc)) { //Tell the user class could not be registered MessageBox( NULL, "Failed To Register The Windows Class", "Shutdown Error", MB_OK | MB_INFORMATION); //Return False return false; } //Check if we should run in fullscreen mode if (fullscreen) { //The device mode DEVMODE dmScreenSettings; //make sure memory is cleared memset(&dmScreenSettings, 0, NULL, sizeof(dmScreenSettings)); //Size of the devmode structure dmScreenSettings.dmSize=sizeof(dmScreenSettings); //Selected screen width dmScreenSettings.dmPelsWidth=width; //Selected screen height dmScreenSettings.dmPelsHeight=height; //Selected bits per pixec dmScreenSettings.dmBitsPerPel=bits; dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; //Try to set selected mode if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { //If we can''t get fullscreen, offer 2 choices if (MessageBox( NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) { //Fullscreen equals false fullscreen=false; } else { //Tell the user program will close MessageBox( NULL, "Proram Will Now Close", "Error", MB_OK |MB_ICONSTOP); //Return False return false; } } } //Are we still in fullscreen mode? if (fullscreen) { //Window extended style dwExStyle=WS_EX_APPWINDOW; //Windows style dwStyle=WS_POPUP; //Hide mouse pointer ShowCursor(false); } else { //Windows extended style dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; //Windows style dwStyle=WS_OVERLAPPEDWINDOW; } //Adjust window to true requested size AdjustWindowRectEx( &WindowRect, dwStyle, false, dwExStyle); //Create the window if (!(hWnd=CreateWindowEx( dwExStyle, "OpenGl", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN dwStyle, 0, 0, WindowRect.right-WindowRect.left,, NULL, NULL, hInstance, NULL))) { //Reset the display KillGLWindow(); //Tell the user window could not be created MessageBox( NULL, "Window Creation Error", "Error", MB_OK | MB_ICONEXCLAMATION); //Return false return false; } //Tell window how we want things to be static PIXELFORMATDESCRIPTOR pfd= { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAWTOWINDOW | PFD_SUPPRTOPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, bits, 0 , 0 , 0, 0, 0, 0 , 0, 0 , 0, 0 ,0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0 , 0 }; //Get a device context if (!(hDC=GetDC(hWnd)) { //Reset the display KillGLWindow(); //Tell the user we can''t create a device context MessagBox(NULL, "Can''t Create A GL Device Context", "Error", MB_OK | MB_ICONEXCLAMATION); ////Return false return false; } // Did Windows Find A Matching Pixel Format? if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) { //Reset the display KillGLWindow(); //Tell the user we can''t find suitable PixelFormat MessageBox(NULL,"Can''t Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); //Return false return false; } // Are We Able To Set The Pixel Format? if (!SetPixelFormat(hDC,PixelFormat,&pfd)) { //Reset the display KillGLWindow(); //Tell the user we can''t set the PixelFormat MessageBox(NULL,"Can''t Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); //Return false return false; } // Are We Able To Get A Rendering Context? if (!(hRC=wglCreateContext(hDC))) { //Reset the display KillGLWindow(); //Tell the user we can''t create a Gl rendering context MessageBox(NULL,"Can''t Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); //Return false return false; } // Try To Activate The Rendering Context if (!wglMakeCurrent(hDC,hRC)) { //Reset the display KillGLWindow(); //Tell the user we can''t activate the Gl rendering context MessageBox(NULL,"Can''t Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); //Return false return false; } //Show the window ShowWindow( hWnd, SW_SHOW); //Slightly higher priority SetForegroundWindow( hWnd); //Set the keyboard focus SetFocus( hWnd); //Set perspective Gl scene ReSizeGLScene( width, height); //Initialize our new Gl window if (!InitGL()) { //Reset the display KillGLWindow(); //Tell the user initialization failed MessageBox( NULL, "Initialization Failed", "Error", MB_OK | MB_ICONEXCLAMATION); //Return false return false; } //Return true return true; } //The message handler LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { //Check the windows message switch(uMsg) { //Watch for windows activation message case WM_ACTIVE: { //Check the minimization state if (!HIWORD( wParam)) { //Program is active active=true; } else { //Program is no longer active active=false; } //Return to the message loop return 0; } //Intercept system commands case WM_SYSCOMMAND: { //Check system calls switch( wParam) { //Screen saver is trying to start case SC_SCREENSAVE: //Monitor is trying to enter power save mode case SC_MONITORPOWER: //Prevent from happening return 0; } //Exit break; } //Did we get a close message case WM_CLOSE: { //Post a quit message PostQuitMessage(0); //Jump back return 0; } //Is a key being held case WM_KEYDOWN: { //Mark the key as true keys[wParam]=true; //Jump back return 0; } //A key has been released case WM_KEYUP: { //Mark the key as false keys[wParam]=false; //Jump back return 0; } //Resize the OpenGL window case WM_SIZE: { //LOWORD=width, HIWORD=height ReSizeScene(LOWORD(lParam), HIWORD(lParam)); //Jump back return 0; } } //The deafult message handler return DefWindowProc( hWnd, uMsg, wParam, lParam); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //Windows message structure MSG msg; //Bool variable to quit loop bool done=false; //Ask the user if they want to run in fullscreen mode if (MessageBox( NULL, "Would you like to run in fullscreen mode?", "Start fullscreen?", MB_YESNO | MB_ICONEXCLAMATION) == ID_NO) { //Make fullscreen false fullscreen=false; } //Create our OpenGl window if (!CreateGLWindow( "Nehe''s OpenGL framework", 640, 480, 16, fullscreen)) { //Quit if window was not created return 0; } //Loop until done=true while(!done) { //Is there a message on the queue if (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE)) { //Was it a quit message? if (msg.message==WM_QUIT) { //Done is true done=true; } else { //Translate the message TranslateMessage(&msg); //Dispatch the message DispatchMessage(&msg); } } else { //Draw the screen and watch for ESC key if (active && !DrawGLScene() || keys[VK_EX]) { //Done equals true done=true; } else { //Swap the buffer for double buffering SwapBuffers(hDC); } //Is F1 being pressed if (keys[VK_F1]) { //Make the key false keys[VK_F1]=false; //Kill our current window KillGLWindow(); //Toggle Fullscreen mode, window mode fullscreen=!fullscreen; //Make the OpenGl window again if (!CreateGLWindow( "Nehe''s OpenGl framework", 640, 480, 16, fullscreen)) { //Quit return 0; } } } } //Shutdown //Kill the window KillGLWindow(); //Exit the program return (msg.wParam); } wat did i do wrong this time i included all the needed .a files and everything

Share this post

Link to post
Share on other sites