Jump to content
  • Advertisement
Sign in to follow this  
prgcrs

Setting up classes

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

Hello programmers! I am able to program somewhat but I have never really had a good design when it comes to creating classes. I have seen various ways people use. I would like to know if I am on the right track or if I am making things too complicated. Here is what I have thought of: 2 global classes CApp g_CApp; CGame g_CGame; (inside CApp is an instance of a seperate class "CDirect3d", which initialises d3d and holds the pointer to the d3d device) WinMain { create window, pass the HWND handle to g_CApp.SetWindowHandle() call g_CApp.Init() to initialise the direct3d class pass pointers over to the game class: g_CGame.SetDirect3DDevice(g_CApp.CDirect3d.GetDirect3DDevice() ) while g_CApp.IsRunning() { // do windows stuff etc g_CGame.MainLoop() .. now this class can access pDirect3DDevice->Clear(..) etc.. } } Does that seem like the correct way to split my functions up in to classes? Thanks for looking :)

Share this post


Link to post
Share on other sites
Advertisement
The last game i worked on did something similar to what i have done in my own engine which in turn is pretty similar to what you have done. The main difference being that i instantiate an Engine class and set everything up through that.

Here is the main.cpp of an example Win32 application i hacked together the other day to test my engine out:


#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
#include <Lugru.h>

Lugru::Engine* g_pEngine( NULL );

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
g_pEngine->Stop();
Sleep(100);
g_pEngine->CleanUp();
PostQuitMessage( 0 );
return 0;
}

// We don't want the engine to handle messages until it exists!
if ( g_pEngine != NULL )
{
Lugru::Engine::AppWndProc( hWnd, msg, wParam, lParam );
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR CommandLine, INT Count)
{
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"LugruTestApplication", NULL
};
RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( L"LugruTestApplication", L"D3D Tutorial 06: Meshes",
WS_OVERLAPPEDWINDOW, 100, 100, 1280, 1024,
NULL, NULL, wc.hInstance, NULL );

DWORD a = GetLastError();


// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

g_pEngine = new Lugru::Engine();

g_pEngine->SetWindowHandle( hWnd );

SetCurrentDirectoryA("F:/Dev/Lugru/Resources");
Lugru::Path::AddSymbolSubstitution("$ENGINE_EFFECTS", "Effects");
Lugru::Path::AddSymbolSubstitution("$ENGINE_MODELS", "Models");
Lugru::Path::AddSymbolSubstitution("$ENGINE_TEXTURES", "Textures");
Lugru::Path::AddSymbolSubstitution("$EFFECTS", "Effects");
Lugru::Path::AddSymbolSubstitution("$MODELS", "Models");
Lugru::Path::AddSymbolSubstitution("$TEXTURES", "Textures");
Lugru::Path::AddSymbolSubstitution("$SCENES", "Scenes");

g_pEngine->Initialise();
g_pEngine->SetStandaloneInput(true);
if ( Count == 1 )
{
std::wstring Path(CommandLine);
std::string narrow( Path.begin(), Path.end() );

if ( narrow.length() > 0 )
{
Lugru::SceneLoader( Lugru::Path(narrow), *g_pEngine->GetSceneGraph(), *g_pEngine->GetRenderer(), *g_pEngine->GetGame() );

Sleep(1000);
g_pEngine->GetGame()->StartGame();
}
}

// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
g_pEngine->Frame();
}
}

UnregisterClass( L"LugruTestApplication", wc.hInstance );
return 0;
}


Hope that is useful,

Share this post


Link to post
Share on other sites
Thanks for your reply Dave, thats reassuring.

I guess I thought that globals in C++ was frowned upon :)

I'll keep at it in the direction I'm going.

Cheers!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!