Jump to content
  • Advertisement
Sign in to follow this  
shoez

Reducing Coupling help

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

The following is my code :)


// Singleton? Since only 1 board
Class CBoard
{
private:
DWORD* m_pdwBoardAddress;
DWORD* m_pdwHeight;
DWORD* m_pdwWidth;
struct Cell
{
private:
POINT m_ptCoord;
BYTE* m_pbStatus;
public:
Cell();
};
CELL* pcllArray;
protected:
InitCells();
public:
CBoard();
INT GetCellCount() { (*m_pdwHeight ) * (*m_pdwWidth); }

};

CBoard::CBoard( DWORD* pdwBoardAddress, DWORD* pdwBoardHeight, DWORD* pdwBoardWidth)
: m_pdwBoardAddress( pdwBoardAddress ), m_pdwHeight( pdwBoardHeight ), m_pdwWidth( pdwBoardWidth ), pcllArray( NULL )
{
// Make sure passed in parameters aren’t NULL
if(m_pdwBoardAddress && m_pdwHeight && m_pdwWidth)
{
// SetErrorCodeInvalidParameter?
}
// Allocate Cells depending on board size
INT nCellCount = GetCellCount();
m_pcllArray = (CELL*) malloc( nCellCount * sizeof( CELL ) );
if(m_pcllArray == NULL)
{
// Unable To allocate memory
}
// Initialize Cells
InitCells();
}

CBoard::InitCells()
{
RECT
BOOL bOk;
// Orgiinal virtual? Not needed?
// Obtain Window Handle to game
HWND hWindow = FindWindow( 0, TEXT(“Minesweeper”) );
if ( hWindow == NULL ) // ERROR
// Obtain Client area coordinates of game window
bOk = GetClientRect( hWindow, &rctClient );
if( bOk == FALSE )
..
......
....
...
}


Pretty much does anyone have any recommendations on how i can REMOVE the dependency of GetClientRect() and FindWindow() API function from my source code? I would like to use these data structures for other games or even in other projects where i wouldn't need/use these API functions!

Share this post


Link to post
Share on other sites
Advertisement
You could have the caller decide how to get the RECT struct (perhaps using a class that implements an ClientInfo interface).

Something roughly like this:


class ClientInfo
{
public:
virtual RECT GetWindowRect( ) = 0;
// Other client info methods
}
---
class WindowsClientInfo : public ClientInfo
{
public:
RECT GetWindowRect( );
// Other client info methods
}

WindowsClientInfo::GetWindowRect( string windowName )
{
RECT rctClient;

HWND hWindow = FindWindow( 0, TEXT(windowName.c_str( )) );

if( hwindow == NULL )
//Handle Error

if( !GetClientRect( hWindow, &rctClient ) )
//Handle Error

return rctClient;
}
---
int main( )
{

WindowsClientInfo clientInfo;
board.InitCells( &clientInfo );

delete clientInfo;
}
---
CBoard::InitCells(ClientInfo* clientInfo)
{
RECT rctClient = clientInfo.GetWindowRect(“Minesweeper”);
...
}

Share this post


Link to post
Share on other sites

Why not just pass the client rect in through the constructor?


I was originally going to suggest that, but in his example he only uses the rect in InitCells so it might not be appropriate to store it as a member variable. Even then I was being excessive, he could pass just the rect into InitCells.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!