Jump to content
  • Advertisement
Sign in to follow this  
Gumgo

OpenGL Initialize opengl before window

This topic is 3108 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

I seem to be unable to call any OpenGL functions until I have created a window. This makes some things awkward, for example, I initialize most systems, create the window, and then go back and initialize OpenGL related systems. Is there a way to initialize OpenGL first, then later create the window, so I can initialize everything at once? I don't really understand what all of the window setup code actually does, it was just sort of procedure to write it (I know, probably not a good thing), so I'm not really sure what to do. Here is the code I'm using:
bool Window::makeWindow( const std::string & text, int w, int h, int bits, bool full ) {
	if (created)
		return false;

	UINT PixelFormat;
	WNDCLASS wc;
	DWORD dwExStyle, dwStyle;

	width = w;
	height = h;
	numbofbits = bits;
	title = text;

	RECT WindowRect;
	WindowRect.left = (long)0;
	WindowRect.right = (long)w;
	WindowRect.top = (long)0;
	WindowRect.bottom = (long)h;

	fullscreen = full;

	// some window settings - mostly just standard procedure
	hInstance = GetModuleHandle( NULL );
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 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";

	if (!RegisterClass( &wc ))
		return false;

	if (fullscreen) {
		DEVMODE dmScreenSettings;
		memset( &dmScreenSettings, 0, sizeof( dmScreenSettings ) );
		dmScreenSettings.dmSize = sizeof( dmScreenSettings );
		dmScreenSettings.dmPelsWidth = w;
		dmScreenSettings.dmPelsHeight = h;
		dmScreenSettings.dmBitsPerPel = bits;
		dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN ) != DISP_CHANGE_SUCCESSFUL)
			fullscreen = false;
	}

//	ShowCursor( false );

	// window frame settings here
	if (fullscreen) {
		dwExStyle = WS_EX_APPWINDOW;
		dwStyle = WS_POPUP;
	} else {
		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		//dwStyle = WS_OVERLAPPEDWINDOW;
		dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU/* | WS_THICKFRAME*/ | WS_MINIMIZEBOX/* | WS_MAXIMIZEBOX*/;
	}

	AdjustWindowRectEx( &WindowRect, dwStyle, false, dwExStyle );

	if (!(hWnd = CreateWindowEx( dwExStyle,
								 "OpenGL",
								 title.c_str(),
								 WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
								 0, // x
								 0, // y
								 WindowRect.right - WindowRect.left,
								 WindowRect.bottom - WindowRect.top,
								 NULL,
								 NULL,
								 hInstance,
								 NULL ))) {
		killWindow();
		return false;
	}

	// center the window
	if (!fullscreen) {
		RECT WinPos;
		ULONG NewX, NewY, NewW, NewH, ScreenWidth, ScreenHeight;

		GetWindowRect( hWnd, &WinPos );

		ScreenWidth = GetSystemMetrics( SM_CXSCREEN );
		ScreenHeight = GetSystemMetrics( SM_CYSCREEN );

		NewX = ScreenWidth/2 - (WinPos.right - WinPos.left)/2;
		NewY = ScreenHeight/2 - (WinPos.bottom - WinPos.top)/2;
		NewW = WinPos.right - WinPos.left;
		NewH = WinPos.bottom - WinPos.top;

		MoveWindow( hWnd, NewX, NewY, NewW, NewH, true );
	}

	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof( PIXELFORMATDESCRIPTOR ),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 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
	};

	if (!(hDC = GetDC( hWnd ))) {
		killWindow();
		return false;
	}

	if (!(PixelFormat = ChoosePixelFormat( hDC, &pfd ))) {
		killWindow();
		return false;
	}

	if (!SetPixelFormat( hDC, PixelFormat, &pfd )) {
		killWindow();
		return false;
	}

	if (!(hRC = wglCreateContext( hDC ))) {
		killWindow();
		return false;
	}

	if (!wglMakeCurrent( hDC, hRC )) {
		killWindow();
		return false;
	}

	SetWindowLongPtr( hWnd, GWLP_USERDATA, (long)this ); // for use in wndProc
	ShowWindow( hWnd, SW_SHOW );
	SetForegroundWindow( hWnd );
	SetFocus( hWnd );

	if (!initOpenGL()) {
		killWindow();
		return false;
	}

	created = true;
	return true;
}

bool Window::initOpenGL() {
	glViewport( 0, 0, width, height );
	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
	glClearDepth( 1.0f );
	return true;
};

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Is there a way to initialize OpenGL first, then later create the window, so I can initialize everything at once?

Not to my knowledge, because an OpenGL rendering context must be attached to an existing window.

Create window -> get device context of window -> create OpenGL context

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!