Jump to content

  • Log In with Google      Sign In   
  • Create Account

does this code look ok?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 15 May 2013 - 01:54 AM

I'm trying to create an openGL window in my engine and the following code runs.........just not good.....so I was wondering if there's anything wrong with this code:

 

// create OpenGLWindow;
	bool IOpenGLRenderWindow::createWindow(std::string wiName, 
		std::string title, 
		unsigned int width, 
		unsigned int height, 
		bool fullscreen, 
		bool VSync, 
		bool Stencil, 
		unsigned int bits, 
		unsigned int numSamples)
	{
		// set values from class to values in-coming
		m_windowWidth = width;	// set the width;
		m_windowHeight = height;  // set height;
		m_windowName = wiName;	// the window's name;
		m_windowTitle = title;	// the title of the window.
		m_fullScreen = fullscreen; // full screen
		m_VSync = VSync;		   // V-Sync.
		m_Stencil = Stencil;	   // stencil
		m_bits = bits;				// bits
		m_numSamples = numSamples;	// the sample quality;

		WNDCLASSEX winClass;
		ZeroMemory(&winClass,sizeof(winClass));

		winClass.cbSize = sizeof(WNDCLASSEX);
		winClass.style  = CS_HREDRAW | CS_VREDRAW;
		winClass.lpfnWndProc = IWindowUtility::getSingleton()->WinProc;
		winClass.hInstance =  GetModuleHandle(NULL);
		winClass.hCursor   = LoadCursor(NULL,IDC_ARROW);
		winClass.hIcon	   = LoadIcon(NULL,IDI_APPLICATION);
		winClass.lpszClassName = this->m_windowName.c_str();

		if(!RegisterClassEx(&winClass))
		{
			IDebuggingUtility::operatingSystemBug("OpenGLRenderWindow::CreateWindow");	
		}

		// styles needed.
		DWORD dwStyle;		// regular style
		DWORD dwExStyle;	// extra style.
		// set the window rect.
		RECT winRect;
		winRect.left = 0;					// left region
		winRect.top = 0;					// top
		winRect.right = m_windowWidth;// right
		winRect.bottom = m_windowHeight;    // bottom.

		// check if full screen.
		if(m_fullScreen)
		{
			DEVMODE devMode;
			ZeroMemory(&devMode, sizeof(DEVMODE));

			devMode.dmSize = sizeof(DEVMODE);
			devMode.dmBitsPerPel    = m_bits;
			devMode.dmPanningWidth  = m_windowWidth;
			devMode.dmPanningHeight = m_windowHeight;
			devMode.dmFields		= DM_BITSPERPEL | DM_PANNINGWIDTH | DM_PANNINGHEIGHT; 

			// check if full screen.
			if(ChangeDisplaySettings(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
			{
				m_fullScreen = false;
				IDebuggingUtility::operatingSystemBug("ChangeDisplayMode");
				
			}

			dwStyle = WS_POPUP;
			dwExStyle = WS_EX_APPWINDOW;
			ShowCursor(false);
		}
		else
		{
			dwStyle = WS_OVERLAPPEDWINDOW;
			dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
			ShowCursor(true);
		}

		AdjustWindowRectEx(&winRect, dwStyle,NULL,dwExStyle);

		// create the window
		m_windowHandle = CreateWindowEx(dwExStyle,
			this->m_windowName.c_str(),
			this->m_windowTitle.c_str(),
			dwStyle,
			0,
			0,
			winRect.right - winRect.left,
			winRect.bottom - winRect.top,
			NULL,
			NULL,
			GetModuleHandle(NULL),
			NULL);

		if(m_windowHandle)
		{
			ShowWindow(m_windowHandle,SW_SHOW);
			UpdateWindow(m_windowHandle);
		}
		else
		{
			
			IDebuggingUtility::operatingSystemBug("CreateWindowEx");
		}

		// get Device context
		this->m_deviceContext = GetDC(m_windowHandle);
		
		// check if device gotten
		if(m_deviceContext == NULL)
		{
			IDebuggingUtility::operatingSystemBug("GetDC");
			const char* error = (char*)gluErrorString(glGetError());
			string err = "The device Context is NULL.";
			err += error;
			throw IException(ET_APIERROR,err,"GetDC");
		}

		// set pixel information
		ZeroMemory(&this->m_pPixelDesc,sizeof(m_pPixelDesc));

		m_pPixelDesc.nSize = sizeof(m_pPixelDesc);
		m_pPixelDesc.nVersion = 1;
		m_pPixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER;
		m_pPixelDesc.iPixelType = PFD_TYPE_RGBA;
		m_pPixelDesc.cColorBits = m_bits;
		
		if(m_Stencil)
		{
			m_pPixelDesc.cStencilBits = 8;
			m_pPixelDesc.cDepthBits = 24;
		}
		else
		{
			m_pPixelDesc.cStencilBits = 0;
			m_pPixelDesc.cDepthBits = 24;
		}
		m_pPixelDesc.iLayerType = PFD_MAIN_PLANE;
		int pixelFormat = 0;

		pixelFormat = ChoosePixelFormat(m_deviceContext,&m_pPixelDesc);
		if(!pixelFormat)
		{
			IDebuggingUtility::operatingSystemBug("ChoosePixelFormat");
			const char* error = (char*)gluErrorString(glGetError());
			string err = "Pixel format not working....... ";
			err += error;
			throw IException(ET_APIERROR, err,"ChoosePixelFormat");
		}

		// set the pixel format
		if(!SetPixelFormat(m_deviceContext,pixelFormat,&m_pPixelDesc))
		{
			IDebuggingUtility::operatingSystemBug("SetPixelFormat");
			const char* error = (char*)gluErrorString(glGetError());
			string err = "Pixel Format cannot be set: ";
			err += error;
			string func = "SetPixelFormat";
			throw IException(ET_APIERROR, err,func);
		}

		// create the device context
		this->m_GLRC = ::wglCreateContext(m_deviceContext);

		if(m_GLRC == NULL)
		{
			IDebuggingUtility::operatingSystemBug("wglCreateContext");
			const char* error = (char*)gluErrorString(glGetError());
			string err = "Context creation failed: ";
			err += error;
			string func = "wlgCreateContext";
			throw IException(ET_APIERROR,err,func);
		}
		if(m_GLRC != GLEW_OK)
		{
			const char* error = (char*)gluErrorString(glGetError());
			
			throw IException(ET_APIERROR, string(error),"Device Context");
		}

		// set the current device context
		wglMakeCurrent(this->m_deviceContext, m_GLRC);

		GLenum glew = glewInit();

		if(glew != GLEW_OK)
		{
			const char* error = (char*)gluErrorString(glew);
			string err = "glew not initialized ";
			err += error;
			throw IException(ET_APIERROR, err,"Cannot initalize glew");
		}

		int attributes[] = 
		{
			WGL_CONTEXT_MINOR_VERSION_ARB, 4,
			WGL_CONTEXT_MAJOR_VERSION_ARB, 1,
			WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
			0
		};

		if(wglewIsSupported("WGL_ARB_Create_Context") == 1)
		{
			this->m_GLRC = ::wglCreateContextAttribsARB(this->m_deviceContext, NULL, attributes);
			wglMakeCurrent(NULL,NULL);
			wglDeleteContext(m_GLRC);
			wglMakeCurrent(m_deviceContext, m_GLRC);
		}
		else
		{
			const char* error = (char*)gluErrorString(glew);
			string err = "WGL_ARG_Create_Context is flawed. ";
			err += error;

			throw IException(ET_APIERROR,err,"Error in WGL_ARG");
		}
		return IRenderWindow::createWindow(this->m_windowName,
										   m_windowTitle,
										   m_windowWidth,
										   m_windowHeight,
										   this->m_fullScreen,
										   this->m_VSync,
										   this->m_Stencil,
										   this->m_bits,
										   this->m_numSamples);
		}

		void IOpenGLRenderWindow::_setClearColor(BYTE alpha, BYTE red, BYTE green, BYTE blue)
		{
			if(m_deviceContext)
			{
				glClearColor(red,green,blue,alpha);
			}
			IRenderWindow::_setClearColor(alpha, red, green, blue);
		}

		void IOpenGLRenderWindow::_renderStart()
		{
			glClear(GL_COLOR_BUFFER_BIT);
		}

		void IOpenGLRenderWindow::_renderEnd()
		{
			bool isOK = SwapBuffers(m_deviceContext);

			if(!isOK)
			{
				IDebuggingUtility::operatingSystemBug("IOpenGLRenderWindow::_renderEnd");
			}
		}

 

seems ok to me, maybe i didn't initialize the pixel format good?

 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8606

Like
0Likes
Like

Posted 15 May 2013 - 04:02 AM

What does "runs.........just not good" mean?



#3 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 15 May 2013 - 07:19 AM

What does "runs.........just not good" mean?

Right, should of posted a screenie........lol

 

http://puu.sh/2TWb2.jpg

basically, I don't know WHY its giving me that........I suppose It could be many many things using a console rather then a win32 project? Doubt it.



#4 Brother Bob   Moderators   -  Reputation: 8606

Like
1Likes
Like

Posted 15 May 2013 - 07:35 AM

Use the debugger to determine where the error message is generated and under what conditions you'll get there, then figure out why it got there.

 

But you may not even have to use a debugger in this case, but rather just read the message and locate the corresponding message in the code: there's only one place in the code you posted where any string from the error window is present. If there is an error window with the title "Device Context" and the message "invalid operation", then these strings must come from somewhere.



#5 Rhetorician   Members   -  Reputation: 119

Like
-1Likes
Like

Posted 16 May 2013 - 05:12 PM

This is hilarious. Troll?



#6 Rhetorician   Members   -  Reputation: 119

Like
-3Likes
Like

Posted 16 May 2013 - 05:23 PM

I love it how the screen shot has nothing to do with this topic, or even the code. hahaha the Window title says Direct11 and all they needed to tell us was "invalid operation"

You have the anti-virus update reminder shit overlapping the screenshot and ... both IE and Firefox in your task bar ...

 

Just hilarious.


Edited by Reflexus, 16 May 2013 - 05:26 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS