Skip to main content
gamedev.net
🔒 Locked

CreatePixelFormat error second time around.

Started by Budoken01 May 20, 2003 at 12:07 AM 0 replies 450+ views
Original Post
Budoken01
Budoken01
When I added directAudio and loaded a 5mb wav file, it works fine the first time I run the app., but then it crashes when it gets to CreatePixelFormat on the second time I try running the application. It only happens whenever I have the music playing part uncommented. Any ideas?
    
bool cEngine::CreateGL(char *title, int width, int height, int bpp, bool fs)
{
	glMatrixMode(GL_PROJECTION);

	GLuint nPixelFormat;
 	WNDCLASSEX windowClass;    // The class that I am using

	DWORD dwWndExStyle, dwWndStyle;

	// This is so the window borders don't overlap OpenGL rendering area

	RECT winRect;
	winRect.top = (long)0;
	winRect.left = (long)0;
	winRect.bottom = (long)height;
	winRect.right = (long)width;

	fullscreen = fs;

	// windowClass filled out

	hInstance = GetModuleHandle(NULL);
	windowClass.cbSize        = sizeof(WNDCLASSEX);
	windowClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	windowClass.lpfnWndProc   = (WNDPROC)WndProc;
	windowClass.cbClsExtra    = 0;
	windowClass.cbWndExtra    = 0;
	windowClass.hInstance     = hInstance;
	windowClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	windowClass.hIconSm       = LoadIcon(NULL, IDI_WINLOGO);
	windowClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	windowClass.hbrBackground = NULL;
	windowClass.lpszMenuName  = NULL;
	windowClass.lpszClassName = "Flux";

	// Tell Windows to register the class and create the window

	if(!RegisterClassEx(&windowClass))
	{
		MessageBox(NULL, "Couldn't register the class with Windows.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	// Checking if fullscreen is selected, and change styles accordingly

	if(fullscreen)
	{
		// Fullscreen settings

		DEVMODE devModeScreen;
		memset(&devModeScreen, 0, sizeof(devModeScreen));
		devModeScreen.dmSize = sizeof(devModeScreen);
		devModeScreen.dmPelsWidth = width;
		devModeScreen.dmPelsHeight = height;
		devModeScreen.dmBitsPerPel = bpp;
		devModeScreen.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;

		// Attempting to change to fullscreen

		if(ChangeDisplaySettings(&devModeScreen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
			MessageBox(NULL, "Your video card sucks too bad to support\nfullscreen in the requested resolution. Using\nwindowed mode instead.", "Error", MB_OK);
			fullscreen = false;
		}
	}
	
	// Adjusting the styles and the extended styles depending on the mode

	if(fullscreen)
	{
		dwWndExStyle = WS_EX_APPWINDOW;
		dwWndStyle = WS_POPUP;
		ShowCursor(false);
	}
	else
	{
		dwWndExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwWndStyle = WS_OVERLAPPEDWINDOW;
	}

	// Again, a command called to ensure that the window doesn't overlap the OpenGl area

	AdjustWindowRectEx(&winRect, dwWndStyle, false, dwWndExStyle);

	if(!(hWnd = CreateWindowEx(dwWndExStyle, "Flux", title, dwWndStyle | WS_CLIPCHILDREN |
							   WS_CLIPSIBLINGS, 0, 0, winRect.right - winRect.left,
							   winRect.bottom - winRect.top, NULL, NULL, hInstance, NULL)))
	{
		NukeGL();
		MessageBox(NULL, "Couldn't create the window.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	// Necessary code to setup the pixel format

	static PIXELFORMATDESCRIPTOR pixform = {sizeof(PIXELFORMATDESCRIPTOR), 1,
		                                    PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
											PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, bpp,
											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)))
	{
		NukeGL();
		MessageBox(NULL, "Couldn't create a device context.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

//  Here it is...

	MessageBox(NULL, "Monkeys.", "Error", MB_OK | MB_ICONEXCLAMATION);

	if(!(nPixelFormat = ChoosePixelFormat(hDC, &pixform)))
	{
		NukeGL();
		MessageBox(NULL, "Couldn't choose a suitable pixel format.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	// Make sure everything is in proper order.

	if(!SetPixelFormat(hDC, nPixelFormat, &pixform))
	{
		NukeGL();
		MessageBox(NULL, "Couldn't setup the pixel format.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	// Finally, create the context

	if(!(hRC = wglCreateContext(hDC)))
	{
		NukeGL();
		MessageBox(NULL, "Couldn't create a device context.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	// And make it your b*tch

	if(!wglMakeCurrent(hDC, hRC))
	{
		NukeGL();
		MessageBox(NULL, "Couldn't active the rendering context.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	ShowWindow(hWnd, SW_SHOW);
	SetForegroundWindow(hWnd);
	SetFocus(hWnd);
	ResizeGL(width, height);

	if(!InitGL())
	{
		NukeGL();
		MessageBox(NULL, "Couldn't initialize OpenGL.", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	return true;
}
  

This is the winmain function
      
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;
	bool finished = false; // When finished == true, app is over.

	value[1] = "data/Med-Ship.ms3d";

	if(player.ship.LoadMs3d(value[1]) == false)
	{
		MessageBox(NULL, "Could not load medship.ms3d", "Error", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}

	if(MessageBox(NULL, "Fullscreen mode?", "Pregunta en ingles", MB_YESNO | MB_ICONQUESTION) == IDNO)
		fs = false;

	if(!Flux.CreateGL("Flux: The Ultimate Experience", 800, 600, 32, fs))
		return 0;

	// setup DirectX Audio

	if(FAILED(audio.InitAudio()))
		Bam("Couldn't setup DirectX Audio");

	if(FAILED(sound.Init(L"explo1.wav", audio.getPerform(), audio.getLoad())))
		Bam("Couldn't load 3d sound effect.");

	if(FAILED(music.Init(L"kenshin.wav", audio.getLoad(), Flux.getHWND())))
		Bam("Couldn't load music.");

	music.Play();

	// Main Loop

	while(!finished)
	{
		if(PeekMessage(&msg, Flux.getHWND(), NULL, NULL, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				finished = true;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			Flux.Render();
			SwapBuffers(Flux.getHDC());
		}
	}
	Flux.input.Shutdown();
	sound.Kill();
	music.Kill();
	audio.Kill();
	CoUninitialize();
	HUD.Kill();
	Flux.NukeGL();
	return (msg.wParam);
}
    
[edited by - budoken01 on May 20, 2003 1:09:46 AM]
Budoken01
Budoken01
Can anyone please help me out? Any type of advice would be appreciated.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.