Sign in to follow this  
Revan1985

OpenGL Object Oriented Programmation

Recommended Posts

So, i decide to follow the C-Uint Framework Tutorial http://www.c-unit.com/ for dx, and translate it in opengl... now, i've some problems... first of all, there is not a device, so i do the needed rendering without it... i've created the window, but i don't know how change the CGraphics class to implement OpenGL Initializzation, because the OpenGL need HGLRC and HDC, and i don't know where implement them... can i have some help? i'm trying, but i've only errors... here the "flushed" code how the hell i make sopilers? the code is so long ^^ CGraphics.h
#ifndef CGRAPHICS_H
#define CGRAPHICS_H

#include "stdafx.h"

class CGraphics
{
public:
	CGraphics();
	~CGraphics() { Release(); }
	bool Initialize( HWND HWnd, bool windowed );
	void Release();
	bool Reset();

	bool Windowed;

private:
	HWND hWnd;
	HINSTANCE hInstance;
};

#endif


CGraphics.cpp
#include "stdafx.h"
#include "CGraphics.h"

CGraphics::CGraphics()
{
}

void CGraphics::Release()
{
}

bool CGraphics::Initialize( HWND HWnd, bool windowed )
{
	return true;
}

bool CGraphics::Reset()
{
	return true;
}



CFramework.h
#ifndef CFRAMEWORK_H
#define CFRAMEWORK_H

#include "stdafx.h"
#include "CGraphics.h"

class CBaseApp
{
public:
	virtual ~CBaseApp() { }
	virtual void Release() = 0;
	virtual void OnCreateDevice() = 0;
	virtual void OnResetDevice() = 0;
	virtual void OnLostDevice() = 0;
	virtual void OnDestroyDevice() = 0;
	virtual void OnUpdateFrame( float elapsedTime ) = 0;
	virtual void OnRenderFrame( float elapsedTime ) = 0;
	virtual void OnKeyDown( WPARAM wParam ) = 0;

};

class CFramework
{
public:
	CFramework( CBaseApp* pGameApp );
	~CFramework() { Release(); }
	bool Initialize( char* title, HINSTANCE hInstance, int width, int height, bool windowed = true );
	void Run();
	void Release();
	void ToggleFullScreen();
	static LRESULT CALLBACK StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

private:
	LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
	void OnCreateDevice();
	void OnResetDevice();
	void OnLostDevice();
	void OnDestroyDevice();
	void OnUpdateFrame();
	void OnRenderFrame();

	HWND				m_hWnd;
	HINSTANCE			m_hInstance;
	bool				m_active;
	int					m_windowWidth;
	int					m_windowHeight;
	WINDOWPLACEMENT		m_wp;

	CGraphics*			m_pGraphics;
	CBaseApp*			m_pGameApp;
};

#endif

CFramework.cpp
#include "stdafx.h"
#include "CFramework.h"

CFramework::CFramework( CBaseApp* pGameApp )
{
	this->m_pGameApp = pGameApp;
	this->m_active = true;
	this->m_pGraphics = new CGraphics();
}

void CFramework::Release()
{
	SAFE_RELEASE( this->m_pGraphics );
	OnLostDevice();
	OnDestroyDevice();
}

bool CFramework::Initialize( char *title, HINSTANCE hInstance, int width, int height, bool windowed )
{
	this->m_hInstance = hInstance;
	this->m_windowWidth = width;
	this->m_windowHeight = height;

	WNDCLASSEX wcex;
	wcex.cbSize			= sizeof( WNDCLASSEX );
	wcex.style			= CS_DBLCLKS;
	wcex.lpfnWndProc	= ( WNDPROC ) StaticWndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON1 ) );
	wcex.hCursor		= LoadCursor( NULL, IDC_ARROW );
	wcex.hbrBackground	= ( HBRUSH ) GetStockObject( BLACK_BRUSH );
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= title;
	wcex.hIconSm		= LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON1 ) );

	RegisterClassEx( &wcex );

	this->m_hWnd = CreateWindow( title, title, windowed ? WS_OVERLAPPEDWINDOW : WS_EX_TOPMOST, CW_USEDEFAULT,
			0, width, height, NULL, NULL, hInstance, this );

	RECT rect = { 0, 0, width, height };

	AdjustWindowRect( &rect, GetWindowLong( this->m_hWnd, GWL_STYLE ), false );
	SetWindowPos( this->m_hWnd, HWND_TOP, 0, 0, rect.right - rect.left, rect.bottom - rect.top,
		SWP_NOZORDER | SWP_NOMOVE );

	ShowWindow( this->m_hWnd, SW_SHOW );
	UpdateWindow( this->m_hWnd );

	if( !this->m_pGraphics->Initialize( this->m_hWnd, windowed ) ) return false;

	OnCreateDevice();
	OnResetDevice();

	return true;
}

void CFramework::Run()
{
	MSG msg;
	while( 1 )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			if( msg.message == WM_QUIT )
			{
				break;
			}
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			if( this->m_active )
			{
				OnUpdateFrame();
				OnRenderFrame();
			}
		}
	}
}


void CFramework::OnCreateDevice()
{
	if( this->m_pGameApp != NULL && this->m_pGraphics != NULL )
		this->m_pGameApp->OnCreateDevice();
}

void CFramework::OnResetDevice()
{
	if( this->m_pGameApp != NULL && this->m_pGraphics != NULL )
		this->m_pGameApp->OnResetDevice();
}

void CFramework::OnLostDevice()
{
	if( this->m_pGameApp != NULL )
		this->m_pGameApp->OnLostDevice();
}

void CFramework::OnDestroyDevice()
{
	if( this->m_pGameApp != NULL )
		this->m_pGameApp->OnDestroyDevice();
}

void CFramework::OnUpdateFrame()
{
	if( this->m_pGameApp != NULL && this->m_pGraphics != NULL )
		this->m_pGameApp->OnUpdateFrame( 0.0f );
}

void CFramework::OnRenderFrame()
{
	if( !this->m_active ) return;

	if( this->m_pGameApp != NULL )
		this->m_pGameApp->OnRenderFrame( 0.0f );
}

void CFramework::ToggleFullScreen()
{
	if( this->m_pGraphics == NULL || !this->m_active ) return;

	this->m_pGraphics->Windowed = !this->m_pGraphics->Windowed;

	if( this->m_pGraphics->Windowed )
	{
		SetWindowLongPtr( this->m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW );
	}
	else
	{
		ZeroMemory( &this->m_wp, sizeof( WINDOWPLACEMENT ) );
		this->m_wp.length = sizeof( WINDOWPLACEMENT );

		GetWindowPlacement( this->m_hWnd, &this->m_wp );

		SetWindowLongPtr( this->m_hWnd, GWL_STYLE, WS_EX_TOPMOST );

		ShowWindow( this->m_hWnd, SW_HIDE );
	}
	
	OnLostDevice();
	this->m_pGraphics->Reset();
	OnResetDevice();

	if( this->m_pGraphics->Windowed )
	{
		SetWindowPlacement( this->m_hWnd, &this->m_wp );
	}

	if( !IsWindowVisible( this->m_hWnd ) )
	{
		ShowWindow( this->m_hWnd, SW_SHOW );
	}
}

LRESULT CALLBACK CFramework::StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	if( msg == WM_CREATE )
	{
		SetWindowLongPtr( hWnd, GWLP_USERDATA, ( LONG )( ( CREATESTRUCT * )lParam )->lpCreateParams );
	}

	CFramework* targetApp = ( CFramework* ) GetWindowLongPtr( hWnd, GWLP_USERDATA );

	if( targetApp )
	{
		return targetApp->StaticWndProc( hWnd, msg, wParam, lParam );
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

LRESULT CALLBACK CFramework::WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	case WM_PAINT:
		if( this->m_pGraphics != NULL )
		{
			OnUpdateFrame();
			OnRenderFrame();
		}
		ValidateRect( hWnd, 0 );
		return 0;
	case WM_SIZE:
		if( wParam == SIZE_MINIMIZED )
		{
			this->m_active = false;
		}
		else
		{
			this->m_active = true;
			this->m_windowWidth = LOWORD( lParam );
			this->m_windowHeight = HIWORD( lParam );

			if( this->m_pGraphics != NULL )
			{
				OnLostDevice();
				this->m_pGraphics->Reset();
				OnResetDevice();
				OnUpdateFrame();
				OnRenderFrame();
			}
		}
		return 0;
	case WM_KEYDOWN:
		if( this->m_pGameApp != NULL )
		{
			this->m_pGameApp->OnKeyDown( wParam );
		}
		return 0;
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}


CGameApp.h

#ifndef CGAMEAPP_H
#define CGAMEAPP_H

#include "stdafx.h"
#include "CFramework.h"

class CGameApp : public CBaseApp
{
public:
	CGameApp();
	~CGameApp() { Release(); }
	void SetFramework( CFramework* pFramework );
	bool Initialize();
	virtual void Release();
	virtual void OnCreateDevice();
	virtual void OnResetDevice();
	virtual void OnLostDevice();
	virtual void OnDestroyDevice();
	virtual void OnUpdateFrame( float elapsedTime );
	virtual void OnRenderFrame( float elapsedTime );
	virtual void OnKeyDown( WPARAM wParam );

private:
	CFramework*		m_pFramework;
};

#endif


CGameApp.cpp

#include "stdafx.h"
#include "CGameApp.h"

CGameApp::CGameApp()
{
	this->m_pFramework = NULL;
}

void CGameApp::Release()
{
	SAFE_RELEASE( this->m_pFramework );
}

void CGameApp::SetFramework( CFramework* pFramework )
{
	this->m_pFramework = pFramework;
}

bool CGameApp::Initialize()
{
	return true;
}

void CGameApp::OnCreateDevice()
{
}

void CGameApp::OnResetDevice()
{
}

void CGameApp::OnLostDevice()
{
}

void CGameApp::OnDestroyDevice()
{
}

void CGameApp::OnUpdateFrame( float elapsedTime )
{
}

void CGameApp::OnRenderFrame( float elapsedTime )
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glLoadIdentity();
}

void CGameApp::OnKeyDown( WPARAM wParam )
{
	switch( wParam )
	{
	case VK_ESCAPE:
		PostQuitMessage( 0 );
		break;
	case VK_F1:
		if( this->m_pFramework != NULL ) this->m_pFramework->ToggleFullScreen();
		break;
	}
}


main.cpp

#include "stdafx.h"
#include "CGameApp.h"
#include "CFramework.h"

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
	CGameApp*		pApplication	= new CGameApp();
	CFramework*		pFramework		= new CFramework( ( CBaseApp* )pApplication );

	pApplication->SetFramework( pFramework );

	if( !pApplication->Initialize() ) return 0;

	if( !pFramework->Initialize( "OpenGL", hInstance, 640, 480, true ) ) return 0;

	pFramework->Run();

	SAFE_RELEASE( pApplication );

	return 0;
}


how i implement the opengl initialization? thx ^^

Share this post


Link to post
Share on other sites
I'm not sure I understand you're question. Why can't you just put the HGLRC and HDC in the CGraphics class along with the window handle, and then write the standard initialization code in Initialize()?

Or are you asking a design question, i.e., where should they go for it to be a good design?

If that's the case, I think they should probably go in a seperate class, along with the hwnd and hinstance. You can call this class Win32GLContext or something and it will handle all the Windows related initialization (and some other things like swapping buffers), while the CGraphics class will handle only OpenGL and won't contain any Windows-related functionality.

Taking this a step further, you can create an abstract GLContext class that will have pure virtual functions like Init() and SwapBuffers(), and Win32GLContext will inherit from it. That way the CGraphics class only has to know about GLContext and so it will be completely OS-independent.

This has the advantage that it makes porting to another OS much easier, and it makes the CGraphics class easier to maintain because it has less to worry about (i.e., it more closely follows the Single-Responsibility Principle).

Share this post


Link to post
Share on other sites
my problem was not "i can't use hglrc and hdc in Cgraphics", but was "How i can use Hglrc and hdc in CGraphics?"

sorry, probably was my "unperfect" english who make this confusion :P

Share this post


Link to post
Share on other sites
I'm not sure if I understood your question correctly, but in case you're wondering how to initialize OpenGL using this design, here's a solution. Otherwise, you should provide us with more info and describe your problem in more detail.

CGraphics.h

class CGraphics
{
public:
CGraphics();
~CGraphics() { Release(); }
bool Initialize( HWND HWnd, bool windowed );
void Release();
bool Reset();

bool Windowed;

private:
HWND hWnd;
HINSTANCE hInstance;

HDC hDC;
HGLRC hRC;
};



CGraphics.cpp

bool CGraphics::Initialize( HWND HWnd, bool windowed )
{
PIXELFORMATDESCRIPTOR pdf;
ZeroMemory( &pfd, sizeof( PIXELFORMATDESCRIPTOR ) );
pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

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

GLuint pixelFormat = ChoosePixelFormat( hDC, &pfd );
if ( !pixelFormat ) {
return false;
}

DescribePixelFormat( hDC, pixelFormat, sizeof( PIXELFORMATDESCRIPTOR ), &pfd );

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

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

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

// set initial render states
}



Hope that helped

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this