Intermediate C++ lesson 6 -> the window size reference

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Intermediate C++ lesson 6 -> the window size reference

Post by NaturalDemon » October 31st, 2012, 9:13 am

in order to pas the Window size or client rect (in technical terms) to the D3DGraphics class,
this is what you have to change

// windows.cpp

Code: Select all

#define WINDOWSIZEX 800
#define WINDOWSIZEY 600

int winSizeX = WINDOWSIZEX;
int winSizeY = WINDOWSIZEY;

int WINAPI wWinMain( HINSTANCE hInst,HINSTANCE,LPWSTR,INT )
{
.
..
...
RECT wr;
	wr.left = 650;
	wr.right = winSizeX + wr.left;
	wr.top = 150;
	wr.bottom = winSizeY + wr.top;
.
..
...
Game theGame( hWnd,kServ,mServ, winSizeX, winSizeY);
.
..
...

// game.h

Code: Select all

class Game
{
public:
	Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer, int& winSizeX, int& WinSizeX );
.
..
...
/********************************/
	/*  User Variables              */

	int& windowSizeX; // references
	int& windowSizeY;

// game.cpp

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer, int& winSizeX, int& winSizeY )
:	gfx( hWnd, winSizeX /* pass the window size X reference */, winSizeY /* pass the window size Y reference */ ),
	audio( hWnd ),
	kbd( kServer ),
	mouse( mServer ),
	lastMouseX(0),
	lastMouseY(0),
	windowSizeX(winSizeX), // save the window rect for future use
	windowSizeY(winSizeY) // save the window rect for future use
.
..
...
// D3DGraphics.h

Code: Select all

class D3DGraphics
{
public:
	D3DGraphics( HWND hWnd, int& winSizeX, int& winSizeY );
.
..
...
        D3DCOLOR*			sysBuffer; // <-- remember this one

	int& windowSizeX;
	int& windowSizeY;

// D3DGraphics.cpp

Code: Select all

#include <assert.h>

D3DGraphics::D3DGraphics( HWND hWnd, int& winSizeX, int& winSizeY )
{
.
..
...
	sysBuffer = new D3DCOLOR[winSizeX * winSizeY]; // <--- problem solved!
}
.
..
...
void D3DGraphics::PutPixel( int x,int y,int r,int g,int b )
{	
	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < windowSizeX );
	assert( y < windowSizeY );
	pSysBuffer[ x + windowSizeX * y ] = D3DCOLOR_XRGB( r,g,b );
}

void D3DGraphics::PutPixel( int x,int y,D3DCOLOR c )
{	
	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < windowSizeX );
	assert( y < windowSizeY );
	pSysBuffer[ x + windowSizeX * y ] = c;
}

D3DCOLOR D3DGraphics::GetPixel( int x,int y)
{	
	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < windowSizeX );
	assert( y < windowSizeY );
	return pSysBuffer[ x + windowSizeX * y ];
}

void D3DGraphics::BeginFrame()
{
	//HRESULT result;
	memset(pSysBuffer, 0x00, sizeof(D3DCOLOR) * windowSizeX *  windowSizeY); 
}

void D3DGraphics::EndFrame()
{
	HRESULT result;

	result = pBackBuffer->LockRect( &backRect,NULL,NULL );
	assert( !FAILED( result ) );

	for( int y = 0; y < windowSizeY; y++)
	{
		memcpy( &((BYTE*)backRect.pBits)[backRect.Pitch * y], &pSysBuffer[windowSizeX * y], sizeof( D3DCOLOR ) * windowSizeX);
	}
	result = pBackBuffer->UnlockRect();
	assert( !FAILED( result ) );

	result = pDevice->Present( NULL,NULL,NULL,NULL );
	assert( !FAILED( result ) );
}
.
..
...	
works like a charm .... thanks to you Chili .... without you ... i never would have understood the use of pointer and stuff.
i'm a selftauched programmer ... with electrical backgroun ... electrician : )

Like mentioned before ... i have some fair amount of c# knowlegde.
i tried c++ ... before : (
Last edited by NaturalDemon on October 31st, 2012, 10:31 am, edited 1 time in total.

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Intermediate C++ lesson 6 -> the window size reference

Post by NaturalDemon » October 31st, 2012, 9:22 am

isn't it possible to redraw a scene wit new dimensions on a resize event in the frame buffer?

destroy the buffer and insert a resized one?

maybe it can't be done seamless.... but with a messagebox ..... usng th delay ... to reset the internal stuff ...

it's a bummer ... having a fixed screen size.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Intermediate C++ lesson 6 -> the window size reference

Post by chili » October 31st, 2012, 2:18 pm

You can do that fine. You'll need to reallocate the backbuffer and the system buffer if you have one. It's better to pause the game loop during resizing and then perform the reallocation at its conclusion instead of resizing while the user is still dragging.
Chili

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Intermediate C++ lesson 6 -> the window size reference

Post by NaturalDemon » October 31st, 2012, 2:43 pm

I assume i can pause the game by method ..... if i correctly intercept the resize event.
that should be a too big hasle ....

in C# you have calbacks and .... it's a simle to add event ... by the properties panel.

// example

Code: Select all

while( msg.message != WM_QUIT )
    {
        if( PeekMessage( &msg,NULL,0,0,PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
		{
			if(!WM_RESIZE)
			{
			theGame.Go();
			}
			else
			{
				theGame.Go(pause);
			}
		}
    }

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Intermediate C++ lesson 6 -> the window size reference

Post by chili » October 31st, 2012, 3:06 pm

You can do callbacks in c++ too, but you have the set up a lot of the infrastructure yourself.

I do like delegates and events in C#, it's a nice language. Too bad it's slow and bloated as balls. :lol:
Chili

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Intermediate C++ lesson 6 -> the window size reference

Post by NaturalDemon » October 31st, 2012, 3:12 pm

chili wrote: slow
that's the keyword

Post Reply