Jump to content
  • Advertisement
Sign in to follow this  
miminawewe

OpenGL OpenGL hangs in Visual C++ Express

This topic is 3820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I try and run an OpenGL app in C++ Express it hangs most of the time. What's up with that? [EDIT]I've added more code
//---------------------------------------------------------------------------

#include "File1.h"
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#pragma hdrstop

//---------------------------------------------------------------------------
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL				InitInstance(HINSTANCE, int);

HINSTANCE hInst;
char szTitle[10] = "test";					// The title bar text
char  szWindowClass[10] = "test1";

const unsigned short SCREEN_WIDTH = 800, SCREEN_HEIGHT = 600;
BYTE SCREEN_COLORBITS = 32, SCREEN_DEPTH = 16;
COGL ogl;
#pragma argsused
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;

	WNDCLASSEX wndclassex = {0};

	// Fill the fields we care about
	wndclassex.cbSize = sizeof(WNDCLASSEX);
	wndclassex.style = CS_HREDRAW | CS_VREDRAW;
	wndclassex.lpfnWndProc = WndProc;
	wndclassex.hInstance = hInstance;
	wndclassex.lpszClassName = szWindowClass;
	wndclassex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	//wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_CROSS), IMAGE_CURSOR, 0, 0, LR_SHARED);
    //wndclassex.hCursor = (HCURSOR)LoadImage(NULL, "images/Cursor1.cur", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
    wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);

	RegisterClassEx(&wndclassex);

	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	GLuint texture_id;
	glGenTextures(1, &texture_id);

	// Bind the texture to the texture arrays index and init the texture
	glBindTexture(GL_TEXTURE_2D, texture_id);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	GLint internal_format = GL_RGB8;    
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	const unsigned int image_width = 512;
	const unsigned int image_height = 512;
	const unsigned int cui_tmp = image_width*image_height*3; 
	GLubyte *image_data_tmp = new GLubyte[cui_tmp];
	for(int i=0; i<cui_tmp; ++i)
		image_data_tmp = 0;

	glTexImage2D(GL_TEXTURE_2D, 0, internal_format, image_width,
            image_height, 0, GL_RGB, GL_UNSIGNED_BYTE,
            image_data_tmp);	

     delete[] image_data_tmp;

	while(!GetAsyncKeyState(VK_ESCAPE))
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}//end if		
        else
		{
				//ref_playingobj.ogg_mmplayer.playMM(b_endofplayback, ref_playingobj.quad_coords);	
				//ogl.MainLoop(playmediafile);	

				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);				
				glLoadIdentity();		
				glBindTexture(GL_TEXTURE_2D, texture_id);	

				glColor4f(1.0f, 1.0f, 1.0f, 1.0f);		
				glBegin(GL_QUADS);

				glTexCoord2f(0.0, 0.0); glVertex2s(0, 0);
				glTexCoord2f(0.0, 0.9375f); glVertex2s(0, 600);
				glTexCoord2f(0.6875f, 0.9375f); glVertex2s(800, 600);
				glTexCoord2f(0.6875f, 0.0); glVertex2s(800, 0);
				glEnd();

				glFlush();
				SwapBuffers(ogl.hdc);
         }//end else		
	} // end of while


	ReleaseDC(ogl.hwnd, ogl.hdc); // Be sure to free up the window's HDC
    wglMakeCurrent(NULL, NULL); // Set render context back
    wglDeleteContext(ogl.hglrc); // Delete render context
    ogl.hglrc = NULL;
	ogl.hdc = NULL;
	UnregisterClass(szWindowClass, hInstance); // Free up the WNDCLASSEX

	return 0;
}
//---------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   RECT rect = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; // Desired window client rect

	DWORD winStyleEx = WS_EX_APPWINDOW;
    DWORD winStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
	//DWORD winStyle = WS_CAPTION | WS_SYSMENU; // Window style
        //DWORD winStyle = WS_MAXIMIZE;

	// Adjust window rect so it gives us our desired client rect when we
	AdjustWindowRectEx(&rect, winStyle, false, winStyleEx);
   
   hWnd = /*CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);*/
	  CreateWindowEx(winStyleEx,
                                szWindowClass,
                                szTitle,
                                winStyle,
                                0,
                                0,
                                rect.right - rect.left,
                                rect.bottom - rect.top,
                                NULL,
                                NULL,
                                hInstance,
                                NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ogl.hwnd = hWnd;
   ogl.hdc = GetDC(hWnd);

   //ChangeToFullScreen();
   if(!ogl.initOGL((BYTE)SCREEN_COLORBITS, (BYTE)SCREEN_DEPTH))
   {
		UnregisterClass(szWindowClass, hInstance); // Free up the WNDCLASSEX
	   	return EXIT_FAILURE; // Something bad happened
   }//end if

   ogl.setupOGL();

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}
//------------------------------------------------------------------------------------
//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
//------------------------------------------------------------------------------------
// Message handler for about box.

//------------------------------------------------------------------------------------
bool COGL::initOGL(const BYTE &colorBits,
				   const BYTE &depthBits)
{
        // Set the pixel format
	if(!setPixelFormat(colorBits, depthBits))
		return false;

	// Set the render context
	if(!setRenderContext())
		return false;

     return true;
}
//------------------------------------------------------------------------------------
bool COGL::setPixelFormat(const BYTE &colorBits, const BYTE &depthBits)
{
        // Declare a pixel format descriptor
        PIXELFORMATDESCRIPTOR pfd = {
	        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
	        1,                     // version number
	        PFD_DRAW_TO_WINDOW |   // support window
	        PFD_SUPPORT_OPENGL |   // support OpenGL
	        PFD_DOUBLEBUFFER,      // double buffered
	        PFD_TYPE_RGBA,         // RGBA type
	        24,                    // 24-bit color depth
	        8, 0, 8, 0, 8, 0,      // color bits ignored
	        8,                     // no alpha buffer
	        0,                     // shift bit ignored
            0,                     // no accumulation buffer
	        0, 0, 0, 0,            // accum bits ignored
	        16,                    // 24-bit z-buffer
	        0,                     // no stencil buffer
	        0,                     // no auxiliary buffer
	        PFD_MAIN_PLANE,        // main layer
	        0,                     // reserved
	        0, 0, 0                // layer masks ignored
	        };	


	// Choose a compatible pixel format (storing the pixel format index in "result")
	int result = ChoosePixelFormat(hdc, &pfd);
        // Error Check
        if(result == 0)
                return false;

	// Attempt to set the pixel format
	if(!SetPixelFormat(hdc, result, &pfd))
		return false;

	return true; // Success
}
//------------------------------------------------------------------------------------
bool COGL::setRenderContext()
{
        // Create the OpenGL render context
	hglrc = wglCreateContext(hdc);

        // Error Check
        if(hglrc == NULL)
                return false;

	// Make it the current render context
	if(!wglMakeCurrent(hdc, hglrc))
	{
		DWORD ii = GetLastError();
		return false;
	}//end if

        //set view port
        glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glOrtho(0.0, (GLdouble) SCREEN_WIDTH, 0.0, (GLdouble) SCREEN_HEIGHT, -1.0, 1.0);
	//gluPerspective(60.0f,(GLfloat)SCREEN_WIDTH/(GLfloat)SCREEN_HEIGHT, CAMNEAR, CAMFAR);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	return true; // Success
}
//------------------------------------------------------------------------------------
void COGL::setupOGL()
{
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_CULL_FACE);        
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClearDepth(1.0f);
        glFrontFace(GL_CW);
        glCullFace(GL_BACK);
        glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

        //glPixelStorei ( GL_UNPACK_ALIGNMENT, 4 );
        //glPixelStorei ( GL_PACK_ALIGNMENT, 4 );        

        //let mouse be in the middle of the window
        SetCursorPos(int(SCREEN_WIDTH  >> 1), int(SCREEN_HEIGHT  >> 1));        
}


[Edited by - miminawewe on January 30, 2008 6:03:48 AM]

Share this post


Link to post
Share on other sites
Advertisement
Yeah try pressing the 'pause' button when you're debugging to see where you are, and you can use the call stack view to see where your code has been.

Just a minor note here .. Using try / catch on OpenGL calls wont catch any errors because it's a C-style API. Try / catch are C++ style structured exception handling, the use of which in your main game loop may incur a serious performance penalty. Try to keep try / catch outside of your main loop.

Share this post


Link to post
Share on other sites
The loop runs a couple of times then it hangs. The hanging is irregular so breaking might not work.
The try / catch was for some c++ code which I have commented out. I thought it only slows down an application if an exception occurs but if none occurs then it's OK.

Share this post


Link to post
Share on other sites
Which is why you wait for it to hang before hitting pause to break into the app.

with regards to the exceptions, the issues is that when you enter a 'try' block the compiler has to do some setup in order to unwind the stack properly when an exception occurs; if you move the try block outside of the main loop this setup is only done once instead of every time.

That said, for one entry per-loop this may not be a problem (certainly unlikely to be a bottle neck with your current app) so I wouldn't worry too much about it right now, just keep it in mind for the future.

Share this post


Link to post
Share on other sites
The problem is also in borland turbo C++. I think the problem is the graphics adapter. It's an S3 prosavage/twister v1.1 2.40.103 It seems like if I set GL_CLAMP, Windows hangs, and if I don't set it or set GL_REPEAT it works.
Is there a place where I can see issues that various cards and adapters have? I think this will be a very important resource for anyone developing graphics applications especially OpenGL/DirectX.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!