Object Oriented NeHe

Started by
5 comments, last by Caste 16 years, 9 months ago
Hi all, i've a question... NeHe had not implemented a Code like this? http://www.c-unit.com/tutorials/directx/?t=04 i wan't to use the most OO possible programming p.s. sorry for my english, but i'm italian, and i'm not good to speak/write in english :P
Advertisement
No, NeHe doesn't have any tutorials discussing how to construct a good object-oriented framework (at least, none to my knowledge). But, even though the lesson given at the link you've provided is geared towards DirectX, many of the principles discussed therein are applicable to OpenGL and to programming in general. The concept of object-oriented programming is in fact a very broad aspect that numerous languages promote, and shouldn't be viewed as a "feature" of any language. Instead, look at object-oriented programming as an idea, and examine the benefits and/or drawbacks that it may present for a project.

For a more general overview of object-oriented framework development, I'd recommend reading the lesson provided here. If you are iffy on object-oriented programming in general, see the information given at this link and this link.
It's a shame that most OO examples online do not follow the dependancy inversion principle.
Quote:HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS.

ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS.

Look here for more on OOP.
Programming since 1995.
Well, and also, if you look at this post, you will see the work the new NeHe team has been doing, wicth is actually OpenGL with C++. Personaly, I was really amazed by how Object Oriented really worked, it was when I realised how bad my own code has been all this time.

Anyway, it's OpenGL and it's Object Oriented, so maybe you should take a look.
Ok, I'm trying to Convert NeHe tutorial to OOP, without the Chad code...
but i have a problem...

when i try to register the class, the Program say me "Impossible to register Window Class"

now, i've copied all the functions in a class, and i use this Constructor:

CGame::CGame( HINSTANCE HInstance );

when in the WinMain i use :

CGame* game = new CGame( hInstance );

if( ! game->Initialize( /* */ ) ) return 0;

game->Run();


i've the upper problem...
any solution?
EDIT:
How i make Spoilers?!?

all done, i'm near the perfection
there is only a problem...

This stupid don't register the class...

here the code:

main.cpp
[spoiler]
#include "stdafx.h"#include "CFramework.h"int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){	bool fullscreen = false;	CFramework* framework = new CFramework( hInstance );	if( MessageBox( NULL, "Would you like to run in Fullscreen Mode?", "Fullscreen?", MB_YESNO | MB_ICONQUESTION ) == IDYES )	{		fullscreen = true;	}	if( !framework->Initialize( "OGL", 800, 600, 32, fullscreen ) )	{		return 0;	}	WPARAM param = framework->Run();	return param;}

[/spoiler]

CFramework.h

[spoiler]
#ifndef CFRAMEWORK_H#define CFRAMEWORK_H#include "stdafx.h"#include "CGraphics.h"class CFramework{public:	CFramework( HINSTANCE hInstance );	~CFramework() { Release(); }	bool Initialize( char* title, int width, int height, int bpp, bool fullscreen );	WPARAM Run();	void Release();	void Resize( GLsizei width, GLsizei height );	static LRESULT CALLBACK StaticWndProc( HWND hWnd, UINT mag, WPARAM wParam, LPARAM lParam );private:	HDC				m_hDC;	HWND			m_hWnd;	HGLRC			m_hRC;	HINSTANCE		m_hInstance;	bool			m_keys[256];	bool			m_loop;	bool			m_active;	bool			m_fullscreen;	char*			m_title;		int				m_windowWidth;	int				m_windowHeight;	LRESULT CALLBACK WndProc( HWND hWnd, UINT mag, WPARAM wParam, LPARAM lParam );	CGraphics*		graphic;};#endif

[/spoiler]

CFramework.cpp

[spoiler]
#include "stdafx.h"#include "CFramework.h"CFramework::CFramework( HINSTANCE hInstance ){	this->m_hInstance = hInstance;	this->m_loop = true;}void CFramework::Release(){	if( this->m_fullscreen )	{		ChangeDisplaySettings( NULL, 0 );		ShowCursor( true );	}	if( this->m_hRC )	{		if( !wglMakeCurrent( NULL, NULL ) ) MessageBox( NULL, "Release of HDC and HRC failed!", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );		if( !wglDeleteContext( this->m_hRC ) ) MessageBox( NULL, "Release of HRC failed!", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );		this->m_hRC = NULL;	}	if( this->m_hDC && !ReleaseDC( this->m_hWnd, this->m_hDC ) )	{		MessageBox( NULL, "Release of DC Failed", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );		this->m_hDC = NULL;	}	if( this->m_hWnd && !DestroyWindow( this->m_hWnd ) )	{		MessageBox( NULL, "Could not Release hWnd", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );		this->m_hWnd = NULL;	}	if( !UnregisterClass( CLASSNAME, this->m_hInstance ) )	{		MessageBox( NULL, "Could not Unregister Class", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );		this->m_hInstance = NULL;	}}bool CFramework::Initialize( char *title, int width, int height, int bpp, bool fullscreen ){	this->m_windowWidth = width;	this->m_windowHeight = height;	this->m_fullscreen = fullscreen;	this->m_title = title;	GLuint PixelFormat;	WNDCLASSEX		wc;	DWORD			dwExstyle;	DWORD			dwstyle;	RECT wr;	wr.left		= ( long )0;	wr.right	= ( long )width;	wr.top		= ( long )0;	wr.bottom	= ( long )height;		wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	wc.lpfnWndProc		= ( WNDPROC ) this->StaticWndProc;	wc.cbClsExtra		= 0;	wc.cbWndExtra		= 0;	wc.hInstance		= this->m_hInstance;	wc.hIcon			= LoadIcon( NULL, IDI_WINLOGO );	wc.hCursor			= LoadCursor( NULL, IDC_ARROW );	wc.hbrBackground	= NULL;	wc.lpszMenuName		= NULL;	wc.lpszClassName	= CLASSNAME;	if( !RegisterClassEx( &wc ) )	{		MessageBox( NULL, "Failed to Register Window Class", "ERROR", MB_OK | MB_ICONINFORMATION );		return false;	}	if( this->m_fullscreen )	{		DEVMODE		dmSS;		memset( &dmSS, 0, sizeof( dmSS ) );		dmSS.dmSize			= sizeof( dmSS );		dmSS.dmPelsWidth	= width;		dmSS.dmPelsHeight	= height;		dmSS.dmBitsPerPel	= bpp;		dmSS.dmFields		= DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT ;		if( ChangeDisplaySettings( &dmSS, CDS_FULLSCREEN ) != DISP_CHANGE_SUCCESSFUL )		{			if( MessageBox( NULL, "The requested Fullscreen mode is not supported by\nyour video Card! Use Windowed Mode Instead?", "OGL", MB_YESNO ) == IDYES )			{				this->m_fullscreen = false;			}			else			{				MessageBox( NULL, "Program will now close!", "ERROR", MB_OK | MB_ICONSTOP );				return false;			}		}	}	if( this->m_fullscreen )	{		dwExstyle	= WS_EX_APPWINDOW;		dwstyle		= WS_POPUP;		ShowCursor( false );	}	else	{		dwExstyle	= WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;		dwstyle		= WS_OVERLAPPEDWINDOW;	}	AdjustWindowRectEx( &wr, dwstyle, false, dwExstyle );	if( !( this->m_hWnd = CreateWindowEx(											dwExstyle,											CLASSNAME,											title,											WS_CLIPSIBLINGS |											WS_CLIPCHILDREN |											dwstyle,											0, 0,											wr.right - wr.left,											wr.bottom - wr.top,											NULL,											NULL,											this->m_hInstance,											NULL ) ) )	{		this->Release();		MessageBox( NULL, "Window Creation Error", "ERROR", MB_OK | MB_ICONEXCLAMATION );		return false;	}	static PIXELFORMATDESCRIPTOR pfd =	{		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	};	if( !( this->m_hDC = GetDC(this->m_hWnd ) ) )	{		this->Release();		MessageBox( NULL, "Can't create a GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return false;	}	if( !(PixelFormat = ChoosePixelFormat( this->m_hDC, &pfd ) ) )	{		this->Release();		MessageBox( NULL, "Can't find a Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return false;	}	if( !SetPixelFormat( this->m_hDC, PixelFormat, &pfd ) )	{		this->Release();		MessageBox( NULL, "Can't set the PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return false;	}	if( !( this->m_hRC = wglCreateContext( this->m_hDC ) ) )	{		this->Release();		MessageBox( NULL, "Can't create a GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return false;	}	if( !wglMakeCurrent( this->m_hDC, this->m_hRC ) )	{		this->Release();		MessageBox( NULL, "Can't activate the GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return false;	}	ShowWindow( this->m_hWnd, SW_SHOW );	SetForegroundWindow( this->m_hWnd );	SetFocus( this->m_hWnd );	this->Resize( width, height );	if( !graphic->Initialize() )	{		this->Release();		MessageBox( NULL, "Initializzation Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return false;	}	return true;}void CFramework::Resize( GLsizei width, GLsizei height ){	if( height == 0 ) height = 1;	glViewport( 0, 0, width, height );	glMatrixMode( GL_PROJECTION );	glLoadIdentity();	gluPerspective( 45.0f, ( GLfloat )width /( GLfloat )height, 0.1f, 100.0f );	glMatrixMode( GL_MODELVIEW );	glLoadIdentity();}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->WndProc( 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_ACTIVATE:		if( !HIWORD( wParam ) )	this->m_active = true;		else					this->m_active = false;		return 0;	case WM_SYSCOMMAND:		switch( wParam )		{		case SC_SCREENSAVE:		case SC_MONITORPOWER:			return 0;		}		break;	case WM_CLOSE:		PostQuitMessage( 0 );		return 0;	case WM_KEYDOWN:		this->m_keys[wParam] = true;		return 0;	case WM_KEYUP:		this->m_keys[wParam] = false;		return 0;	case WM_SIZE:		this->Resize( LOWORD( lParam ), HIWORD( lParam ) );		return 0;	}	return DefWindowProc( hWnd, msg, wParam, lParam );}WPARAM CFramework::Run(){	MSG msg;	while( ! this->m_loop )	{		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )		{			if( msg.message = WM_QUIT )			{				this->m_loop = true;			}			else			{				TranslateMessage( &msg );				DispatchMessage( &msg );			}		}		else		{			if( this->m_active )			{				if( this->m_keys[VK_ESCAPE] )				{					this->m_loop = true;				}				else				{					graphic->Draw();					SwapBuffers( this->m_hDC );				}			}			if( this->m_keys[VK_F1] )			{				this->m_keys[VK_F1] = false;				this->Release();				this->m_fullscreen = !this->m_fullscreen;				if( !Initialize( this->m_title, 800, 600, 32, this->m_fullscreen ) )				{					return 0;				}			}		}	}	this->Release();	return ( msg.wParam );}

[/spoiler]

CGraphics.h

[spoiler]
#ifndef CGRAPHICS_h#define CGRAPHICS_H#include "stdafx.h"class CGraphics{public:	CGraphics();	~CGraphics() { Release(); }	bool Initialize();	void Release();	bool Draw();private:};#endif


CGraphics.cpp

[spoiler]
#include "stdafx.h"#include "CGraphics.h"CGraphics::CGraphics(){}bool CGraphics::Initialize(){	glShadeModel( GL_SMOOTH );	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );	glClearDepth( 1.0f );	glEnable( GL_DEPTH_TEST );	glDepthFunc( GL_LEQUAL );	glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );	return true;}bool CGraphics::Draw(){	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	glLoadIdentity();	return true;}

[/spoiler]

stdafx.h

[spoiler]
#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <gl/glaux.h>#pragma comment( lib, "opengl32.lib" )#pragma comment( lib, "glu32.lib" )#pragma comment( lib, "glaux.lib")#define CLASSNAME "OpenGL"

[/spoiler]
why don't work?
thx ^^
Take a look at this thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=437836
We're currentyl working on a new set of OOP lessons for NeHe, although developement unfortunately has been rather slow as we're all spending our free time on this and then we needed to find a compromise of our different ideas how to manage stuff. In the given thread you can already download a beta version, unfortunately it's overworked completely up to now again. I dunno when we'll publish the code with the new lessons as the texts are not finished yet.

This topic is closed to new replies.

Advertisement