1

I create System object on heap and inside the system class I create Game object on heap while KeyboardServer object on stack.

1) Do KeyboardServer object will behave like on heap as System object is part of it?

2) Does KeyboardServer object needs to be created on heap too?

3) Do any better solution to increase the performance?

////////////////////////////////////////////////////////////////////////////////
// Filename: main.cpp
////////////////////////////////////////////////////////////////////////////////
#include "SystemClass.h"

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow )
{
    SystemClass* System;
    bool result;

    // Create the system object
    System = new SystemClass;
    if ( !System )
    {
        return 0;
    }

    // Initialize and run the system object
    result = System->Initialize();
    if (result)
    {
        System->Run();
    }

    // Shutdown and release the system object
    System->Shutdown();
    delete System;
    System = 0;

    return 0;
}



////////////////////////////////////////////////////////////////////////////////
// Filename: SystemClass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _SYSTEMCLASS_H_
#define _SYSTEMCLASS_H_


///////////////////////////////
// PRE-PROCESSING DIRECTIVES //
///////////////////////////////
#define WIN32_LEAN_AND_MEAN


//////////////
// INCLUDES //
//////////////
#include <Windows.h>


///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "GameClass.h"
#include "KeyboardClass.h"


////////////////////////////////////////////////////////////////////////////////
// Class name: SystemClass
////////////////////////////////////////////////////////////////////////////////
class SystemClass
{
    public:
    SystemClass();
    ~SystemClass();

    bool Initialize();
    void Shutdown();
    void Run();

    LRESULT CALLBACK MessageHandler( HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam );

private:
    void InitializeWindows();
    void ShutdownWindows();

private:
    LPCSTR m_applicationName;
    HINSTANCE m_hinstance;
    HWND m_hwnd;

    GameClass*            m_Game;
    KeyboardServerClass  m_KeyboardServer;
};


/////////////////////////
// FUNCTION PROTOTYPES //
/////////////////////////
static LRESULT CALLBACK WndProc( HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam );


/////////////
// GLOBALS //
/////////////
static SystemClass* ApplicationHandle = 0;

#endif



////////////////////////////////////////////////////////////////////////////////
// Filename: KeyboardClass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _KEYBOARDCLASS_H_
#define _KEYBOARDCLASS_H_


////////////////////////////////////////////////////////////////////////////////
// Class prototype
////////////////////////////////////////////////////////////////////////////////
class KeyboardServerClass;


////////////////////////////////////////////////////////////////////////////////
// Class name: KeyboardClientClass
////////////////////////////////////////////////////////////////////////////////
class KeyboardClientClass
{
public:
    KeyboardClientClass( const KeyboardServerClass& KeyboardServer );
    ~KeyboardClientClass();

    bool KeyIsPressed( unsigned char keycode ) const;

private:
    const KeyboardServerClass& server;
};

class KeyboardServerClass
{
    friend KeyboardClientClass;

public:
    KeyboardServerClass();

    void OnKeyPressed( unsigned char keycode );
    void OnKeyReleased( unsigned char keycode );

private:
    static const int nKeys = 256;
    bool keystates[ nKeys ];
};

#endif
4

1 に答える 1