Archived

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

Promit

Weirdo error when my function returns...

Recommended Posts

  
BOOL CreateGLWindow( Window_t* WindowData, char* Name, int Width, int Height, int ColorDepth )
{
	WNDCLASS wc;
	DWORD dwStyle;
	DWORD dwExStyle;
	RECT WndRect;
	UINT PixelFormat;
	static PIXELFORMATDESCRIPTOR pfd;

	WndRect.left = 0; WndRect.top = 0;
	WndRect.right = Width; WndRect.bottom = Height;

	memset( &wc, 0, sizeof(WNDCLASSEX) );
	g_hInstance = GetModuleHandle( NULL );
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.cbClsExtra = wc.cbWndExtra = 0;
	wc.hInstance = g_hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_WINLOGO );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = 0;
	wc.lpszMenuName = NULL;
	wc.lpszClassName = Name;	strcpy( WindowData->Name, Name );
	wc.lpfnWndProc = MainWndProc;

	if( !RegisterClass( &wc ) )
	{		
		//ErrorDesc = "Failed to register window class.\0"

		strcpy( g_ErrorDesc, "Failed to register window class" );
		return FALSE;
	}

	dwStyle = WS_OVERLAPPEDWINDOW;
	dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
	
	/*Fix the window rect slightly to account for the window style*/
	AdjustWindowRectEx( &WndRect, dwStyle, FALSE, dwExStyle );
	
	/*Create our window*/
	WindowData->hWnd = CreateWindowEx( dwExStyle, "Pong2D", "Pong2D", dwStyle, CW_USEDEFAULT,
							CW_USEDEFAULT, WndRect.right - WndRect.left,
							WndRect.bottom - WndRect.top, NULL, NULL, g_hInstance, NULL );
	/*We did get a hWnd, right?*/
	if( WindowData->hWnd == NULL ) //We dont, but i dont care right now
	{
		strcpy( g_ErrorDesc, "Failed to create window" );
		return FALSE;
	}

	memset( &pfd, 0, sizeof(PIXELFORMATDESCRIPTOR) );
	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 = ColorDepth;
	pfd.cDepthBits = 16;
	pfd.iLayerType = PFD_MAIN_PLANE;

	/*Get our window''s DC*/
	WindowData->hDC = GetDC( WindowData->hWnd );
	/*Got DC, right?*/
	if( WindowData->hDC == NULL )
	{
		strcpy( g_ErrorDesc, "Failed to get window DC" );
		return FALSE;
	}
	
	/*Choose Pixel Format*/
	PixelFormat = ChoosePixelFormat( WindowData->hDC, &pfd );
	/*Did we find a pixel format that will work?*/
	if( PixelFormat == 0 )
	{
		strcpy( g_ErrorDesc, "Failed to choose a suitable pixel format" );
		return FALSE;
	}

	/*Set pixel format*/
	if( !SetPixelFormat( WindowData->hDC, PixelFormat, &pfd ) )
	{
		strcpy( g_ErrorDesc, "Could not set pixel format" );
		return FALSE;
	}

	/*Create and activate a rendering context*/
	WindowData->hRC = wglCreateContext( WindowData->hDC );
	if( WindowData->hRC == NULL )
	{
		strcpy( g_ErrorDesc, "Could not create rendering context" );
		return FALSE;
	}
	if( !wglMakeCurrent( WindowData->hDC, WindowData->hRC ) )
	{
		strcpy( g_ErrorDesc, "Could not activate rendering context" );
		return FALSE;
	}

	ShowWindow( WindowData->hWnd, SW_SHOW );
	SetForegroundWindow( WindowData->hWnd );
	SetFocus( WindowData->hWnd );
	

	return TRUE;
} //<--Return statement jumps to here. WHen I move past the brace, i get an access violation. The pointer WindowData is valid...

  
No compile errors at all. Its pure C, if that matters. I suspect theres something silly wrong in the window creation code too, but I havent bothered to look into it yet. If you see it, feel free to save me the trouble. WHat I dont get is why i get an access violation when the funtion returns. ----------------------------- The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence. Democracy is where you say what you want and do what you''re told.

Share this post


Link to post
Share on other sites
Figured out the window creation mistake, really dumb error by me. Canged WndClass name but didnt change the string in the CreateWindowEx...should make that a constant...

Still have the function exit problem, still have no clue why.

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Democracy is where you say what you want and do what you''re told.

Share this post


Link to post
Share on other sites
COME ON doesnt anyone have any clue at all why i get an access violation?

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Democracy is where you say what you want and do what you''re told.

Share this post


Link to post
Share on other sites
The whole damn thing is right there. It doesnt do anything else.

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Democracy is where you say what you want and do what you''re told.

Share this post


Link to post
Share on other sites