Archived

This topic is now archived and is closed to further replies.

RegaRIP

Colors not working on brush and pen....

Recommended Posts

working on a game, as you''ll see its in VERY early stages lol this is my actual function called from my main file, its directly from the MSDN (except hdc is now hDC) the colors do not show, it just draws a white box
  
#include <windows.h>
#include <Wingdi.h>
#include <cstdlib>
#include <stdlib.h>



void DrawBoard(HWND g_hWindow, HDC hDC)
{
	HPEN hpen, hpenOld;
    HBRUSH hbrush, hbrushOld;

    // Create a green pen.

    hpen = CreatePen(PS_SOLID, 10, RGB(0, 255, 0));
    // Create a red brush.

    hbrush = CreateSolidBrush(RGB(255, 0, 0));

    // Select the new pen and brush, and then draw.

    hpenOld = (HPEN)SelectObject(hDC, hpen);
    hbrushOld = (HBRUSH)SelectObject(hDC, hbrush);
    Rectangle(hDC, 100,100, 200,200);

    // Do not forget to clean up.

    SelectObject(hDC, hpenOld);
    DeleteObject(hpen);
    SelectObject(hDC, hbrushOld);
    DeleteObject(hbrush);

}
  
here is the main file from which the function is called
  
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <cstdlib>
#include <stdlib.h>
#include "Board.h"

//*****************************PROTOTYPES****************************************

int GameInit();
int GameLoop();
int GameShutdown();

void DrawPacMan(HDC hDC);
void DrawDots();
void DrawGhost(HDC hDC);
void MoveGhost();
void DrawBoard(HWND g_hWindow, HDC hDC);
void Pause(int MilliSecs);
void DrawScore();
bool Touched(RECT object1, RECT object2);
//*****************************PROTOTYPES****************************************


const int PAC_SPEED = 3;
const int PAC_RADIUS = 15;

const int GHOST_SPEED = 2;
const int GHOST_WIDTH = 20;
const int GHOST_HEIGHT = 20;

int g_GhostSpeed = GHOST_SPEED;

BOOL g_bRedrawPac = TRUE;
BOOL g_bRedrawGhost = TRUE;

int g_Score = 0;
int g_Deaths = 0;

typedef struct
{
	int x, y;
	int Radius;
}PAC, *PPAC;

typedef struct
{
	int x, y;
	int Width, Height;
}GHOST, *PGHOST;

PAC g_PacMan;
GHOST g_Ghost;


HDC g_hDC;
HWND g_hWndMain;



// The window procedure to handle events

long CALLBACK WndProc( HWND hWnd, UINT uMessage, 
                                WPARAM wParam, LPARAM lParam )
{
	PAINTSTRUCT PaintStruct;	// Structure used during windows painting

	HDC hDC;			// Handle to device context for painting


	// Switch the windows message to figure out what it is

	switch( uMessage )
	{
		case WM_CREATE:	// The CreateWindow() function was just called

		{
			// One Time Initialization

			return 0;
		}

		case WM_PAINT:	// The window needs to be redrawn

		{
			// Tell windows we want to start updating the window

			hDC = BeginPaint( hWnd, &PaintStruct );

			// Tell windows we have finished updating the window

			EndPaint( hWnd, &PaintStruct );

			g_bRedrawPac = FALSE;
			g_bRedrawGhost = FALSE;

			return 0;
		}
		
		case WM_KEYDOWN:
		{
			switch( wParam )
			{
				case VK_ESCAPE:
					{
						if( MessageBox( g_hWndMain, "Do you really want to quit the game of the great ones?", 
												"Bye Bye", MB_YESNO ) == IDYES )					
							PostMessage( g_hWndMain, WM_DESTROY, 0, 0 );
					
						break;
					}

			}
			
			return 0;
		}



		case WM_DESTROY:	// The window is about to be closed

		{
			// Our main window is closing.  This means we want our app to exit.

			// Tell windows to put a WM_QUIT message in our message queue

			PostQuitMessage( 0 );
			return 0;
		}

		default:			// Some other message

		{
			// Let windows handle this message

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

// The windows entry point. The application will start executing here

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                     PSTR pstrCmdLine, int iCmdShow )
{
	HWND hWnd;		// The handle to our main window

	MSG msg;		// The message windows is sending us

	WNDCLASSEX wc;	// The window class used to create our window


	// The name of our class and also the title to our window

	static char strAppName[] = "Muncher!!!";

	// Fill in the window class with the attributes for our main window


	// The size of this struture in bytes

	wc.cbSize			= sizeof( WNDCLASSEX );
	
	//  The style of the window.

	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	// Useless information.  Just set to zero.

	wc.cbClsExtra		= 0;
	// Useless information.  Just set to zero.

	wc.cbWndExtra		= 0;
	// The name of our event handler

	wc.lpfnWndProc		= WndProc;
	// A handle to the applications instance

	wc.hInstance		= hInstance;
	// The handle to the brush to use for the window background

	wc.hbrBackground	= (HBRUSH)GetStockObject( BLACK_BRUSH );
	// A handle to the icon to use for the window

	wc.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	// A handle to a smaller version of the apps icon

	wc.hIconSm			= LoadIcon( NULL, IDI_APPLICATION );
	// A handle to the cursor to use while the mouse is over our window

	wc.hCursor			= LoadCursor( NULL, IDC_CROSS );
	// A handle to the resource to use as our menu

	wc.lpszMenuName		= NULL;
	// The human readable name for this class

	wc.lpszClassName	= strAppName;

	// Register the class with windows

	RegisterClassEx( &wc );

	// Create the window based on the previous class

	hWnd = CreateWindowEx( NULL,	// Advanced style settings

	            strAppName,			// The name of the class

				strAppName,			// The window caption

				WS_OVERLAPPEDWINDOW,// The window style

			   	0,		// The initial x position 

                0,		// The initial y position

			   	1024, 740,			// The intiial width / height

				NULL,				// Handle to parent window						

				NULL,				// Handle to the menu

				hInstance,			// Handle to the apps instance

				NULL );				// Advanced context


	g_hWndMain = hWnd;

	GameInit();

	// Display the window we just created

	ShowWindow( hWnd, iCmdShow );
	// Draw the window contents for the first time

	UpdateWindow( hWnd );

	
	// Start the message loop

	while( TRUE )
	{
		// Check if a message is waiting for processing

		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			// Check if the message is to quit the application

			if( msg.message == WM_QUIT )
				// Exit the message loop

				break;
			
			// Change the format of certain messages

			TranslateMessage( &msg );
			// Pass the message to WndProc() for processing

			DispatchMessage( &msg );
		}
		else
		{
			GameLoop();
		}
	}


	GameShutdown();

	// Return control to windows with the exit code

	return msg.wParam;
}


int GameInit()
{
	// Get a DC for this window

	g_hDC = GetDC( g_hWndMain );

	ZeroMemory( &g_PacMan, sizeof( PAC) );
	ZeroMemory( &g_Ghost, sizeof( GHOST) );

	g_Ghost.Height = GHOST_HEIGHT;
	g_Ghost.Width = GHOST_WIDTH;
	g_Ghost.x = 100;
	g_Ghost.y = 100;

	g_PacMan.Radius = PAC_RADIUS;
	g_PacMan.x = 100;
	g_PacMan.y = 100;

	return 0;
}

int GameLoop()
{
	static GhostTime = 0;
	static PacTime = 0;

	HRESULT Res;

	RECT ClientRect;
	GetClientRect( g_hWndMain, &ClientRect);


	if( g_Deaths >= 5)
	{
		Res = MessageBox(NULL, "Game Over, Wanna try again?", "Results", MB_YESNO);
		
		if( Res == IDYES)
		{
			g_Deaths = 0;
			g_Score = 0;
			g_GhostSpeed = GHOST_SPEED;
			g_PacMan.x = 30;
			g_PacMan.y = 30;
		}
		else
			PostMessage( g_hWndMain, WM_DESTROY, 0, 0);

		return 0;
		
	}

	else if(g_Score == 10)
	{
		Res = MessageBox(NULL, "You Won!   Would you like to play again?", "Results", MB_YESNO);
		
		if(Res == IDYES)
		{
			g_Deaths = 0;
			g_Score = 0;
			g_GhostSpeed = GHOST_SPEED;
			g_PacMan.x = 10;
			g_PacMan.y = 10;
		}
		else
			PostMessage(g_hWndMain, WM_DESTROY, 0, 0);
		
		return 0;
	}
	
	//first run

	if( PacTime == 0)
	{
		DrawPacMan(g_hDC);
		DrawGhost(g_hDC);
		DrawScore();
		DrawBoard(g_hWndMain, g_hDC);
		PacTime = 1;
	}

	if(GetAsyncKeyState(VK_UP))
	{
		if((GetTickCount() - PacTime) > 1)
		{
			PacTime = GetTickCount();
			
			if(g_PacMan.y > ClientRect.top + 20)
				g_PacMan.y -= PAC_SPEED;

			DrawPacMan(g_hDC);
		}
	}

	if(GetAsyncKeyState(VK_DOWN))
	{
		if((GetTickCount() - PacTime) > 1)
		{
			PacTime = GetTickCount();
			
			if(g_PacMan.y < ClientRect.bottom - 20)
				g_PacMan.y += PAC_SPEED;

			DrawPacMan(g_hDC);
		}
	}

	if(GetAsyncKeyState(VK_LEFT))
	{
		if((GetTickCount() - PacTime) > 1)
		{
			PacTime = GetTickCount();

			if(g_PacMan.x > ClientRect.left + 20)
				g_PacMan.x -= PAC_SPEED;

			DrawPacMan(g_hDC);
		}
	}

	if(GetAsyncKeyState(VK_RIGHT))
	{
		if((GetTickCount() - PacTime) > 1)
		{
			PacTime = GetTickCount();

			if(g_PacMan.x < ClientRect.right - 20)
				g_PacMan.x += PAC_SPEED;

			DrawPacMan(g_hDC);
		}
	}

	if((GetTickCount() - GhostTime) > 10)
	{
		GhostTime = GetTickCount();

		MoveGhost();
		
		DrawGhost(g_hDC);
		
	}

	static int OldScore = g_Score;

	if(g_Score != OldScore)
	{
		DrawScore();

		OldScore = g_Score;
	}

	if(GetAsyncKeyState(VK_PAUSE))
	{
		while(1)
		{
			Pause(250);
			if(GetAsyncKeyState(VK_PAUSE))
				break;
		}
	}

	return 0;
}


//********************************************************************************

//	Function used to release any devices and memory that the game was using

//

//********************************************************************************

int GameShutdown()
{
	ReleaseDC(g_hWndMain, g_hDC);

	return 0;
}


//********************************************************************************

//	Function used for drawing the score of the game. 

//

//********************************************************************************

void DrawScore()
{
	RECT ClientRect;

	GetClientRect(g_hWndMain, &ClientRect);

	char TextScore[5];

	itoa(g_Score, TextScore, 10);

	char OutputText[30] = "Score:  ";

	strcat(OutputText, TextScore);

	SetTextColor(g_hDC, RGB(255,255,255));

	SetBkColor(g_hDC, 0);

	TextOut(g_hDC, ClientRect.right - 100, 5, OutputText, strlen(OutputText));

}


//********************************************************************************

//	Function used for drawing the ghost.  An inverted drawing mode is used to

//	draw over the old position of the ghost.

//

//	Param(g_hDC) - A handle to the device window

//********************************************************************************

void DrawGhost(HDC g_hDC)
{
	static RECT OldRect;

	RECT Rect = {g_Ghost.x, g_Ghost.y, g_Ghost.Width + g_Ghost.x, g_Ghost.Height + g_Ghost.y};

	RECT ClientRect;

	GetClientRect(g_hWndMain, &ClientRect);
	
	SelectObject(g_hDC, GetStockObject(NULL_BRUSH));

	SetROP2(g_hDC, R2_NOT);

	if(g_bRedrawGhost)
	{
		Rectangle(g_hDC, OldRect.left, OldRect.top, OldRect.right, OldRect.bottom);
	}
	else
	{
		g_bRedrawGhost = TRUE;
	}

	Rectangle(g_hDC, Rect.left, Rect.top, Rect.right, Rect.bottom);

	OldRect = Rect;

}


//********************************************************************************

//	Function used for drawing the pacman.  An inverted drawing mode is used to

//	draw over the old position of the pacman.

//

//	Param(hDC) - A handle to the device window

//********************************************************************************

void DrawPacMan(HDC hDC)
{
	// Holds the previous dimensions of the ball

	static RECT OldRect;
	// Holds the current dimensions of the ball

	RECT PacRect;

	// Fill in a RECT structure to describe the ball

	PacRect.left	= g_PacMan.x - g_PacMan.Radius;
	PacRect.top	    = g_PacMan.y - g_PacMan.Radius;
	PacRect.right	= g_PacMan.x + g_PacMan.Radius;
	PacRect.bottom	= g_PacMan.y + g_PacMan.Radius;

	// Select a hollow brush

	SelectObject( hDC, GetStockObject( NULL_BRUSH ) );
	
	// Set the drawing mode to inverted

	SetROP2( hDC, R2_NOT );	

	// Draw the last ball again to invert it.

	if( g_bRedrawPac )
		Ellipse( hDC, OldRect.left, OldRect.top, OldRect.right, OldRect.bottom );	
	else
		g_bRedrawPac = TRUE;
	// Draw the new ball

	Ellipse( hDC, PacRect.left,  PacRect.top, 
				  PacRect.right, PacRect.bottom );
	
	// Save the position of the current ball.

	OldRect = PacRect;

}


//**********************************************************************************

//	Function that moves the ghost around the screen, if it hits the edge of window

//	its direction reverses.

//

//**********************************************************************************

void MoveGhost()
{
	
	static int TouchTime = 10;

	RECT ClientRect;

	// Controls the direction the ghost moves

	static int MultiplierX = 1;
	static int MultiplierY = 1;

	GetClientRect( g_hWndMain, &ClientRect );

	
	// If the ghost is beyond the right of the window

	// then reverse its direction

	if( g_Ghost.x + g_Ghost.Width > ClientRect.right )
		MultiplierX = -1;

	// If the ghost is beyond the top of the window

	// then reverse its direction

	if( g_Ghost.y < ClientRect.top )
		MultiplierY = 1;

	// If the ghost is beyond the bottom of the window

	// then reverse its direction

	if( g_Ghost.y + g_Ghost.Height> ClientRect.bottom )
		MultiplierY = -1;
	
	// If the ghost is beyond the left of the window

	// then reverse its direction

	if(g_Ghost.x < ClientRect.left)
		MultiplierX = 1;


	// Fill RECT structures with the current position

	// of the pac and ghost 

	RECT PacRect = { g_PacMan.x, 
					 g_PacMan.y, 
					 ((g_PacMan.Radius) + g_PacMan.x), 
					 ((g_PacMan.Radius) + g_PacMan.y )};

	RECT GhostRect = { g_Ghost.x, g_Ghost.y, g_Ghost.Height, g_Ghost.Width};
	
	// Check if the Ghost is touching the Pac

	if( Touched(GhostRect, PacRect ) )
	{
		
		if((GetTickCount() - TouchTime) > 900)
		{
			TouchTime = GetTickCount();
			
			// Increase the score

			g_Deaths++;

			//take out when working

			g_Score++;
		}
		
	}

	// Update the position of the ball

	g_Ghost.x += ( MultiplierX * g_GhostSpeed );
	g_Ghost.y += ( MultiplierY * g_GhostSpeed );

}


//*********************************************************************************

//	Function used for pausing the game untill the users continues

//

//	Param(MilliSecs) - time between checking presses of pause button

//*********************************************************************************

void Pause( int MilliSecs )
{
	// Get the current time

	int Time = GetTickCount();

	// Wait until the specifed time has elapsed.

	while( (UINT)GetTickCount() - (UINT)Time < (UINT)MilliSecs ){}
}



//*********************************************************************************

//	Collision detection function used to determine if the Pacman is touching a ghost

//	

//	Param1(object1) - RECT type used to define the ghosts parameters

//	Param2(object2) - RECT type used to define the pacmans parameters

//	Return - True if touching, else false

//*********************************************************************************

bool Touched(RECT object1, RECT object2)
{
	int left1, left2;
    int right1, right2;
    int top1, top2;
    int bottom1, bottom2;

    left1 = object1.left;
    left2 = object2.left;
    right1 = object1.left + 20;  
    right2 = object2.left + 40; 
    top1 = object1.top;
    top2 = object2.top;
    bottom1 = object1.top + 20; 
    bottom2 = object2.top + 40;   

    if((top2 >= top1 && top2 <= bottom1) && (left2 >= left1 && left2 <= right1))  
		return(1);
		
	if(bottom1 <= bottom2 && bottom1 >= top2 && right1 <= right2 && right1 >= left2)
		return(1);
	
    return(0);

};
  
anyone have any ideas as to what i''m doing wrong?

Share this post


Link to post
Share on other sites
void Draw()
{
//u may want to make your brushes and pens static because your
//brushed and pens are local variables and they get destroyed
//when the functions returns control
static HBRUSH hBrush, old_brush;
HDC hdc;
.
.
.
//old_brush captures the resources that are originally in
// selectobject. u should return the resources so no
// memory link
old_brush = (HBRUSH) SelectObject(hdc, hBrush);

.
.
.
DeleteObject(SelectObject(hdc, hBrush);

}


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites