GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Oct-2007, 14:50
lucamarcus lucamarcus is offline
New Member
 
Join Date: Oct 2007
Posts: 3
lucamarcus is on a distinguished road

RE:" THE BOUNCING BALL in C++"


Having looked at the code for the "bouncing ball" which was said to be "hello world" of Win 32.

I have been trying to do the following:

1. Make the this ball move in a predefined rectangle (one ALREADY drawn) even before the ball starts moving

2. I also would like to make this ball to change color temporarily as it hits either ends of the rectangle for a few seconds and then change back to the original color. As it hits the other end of the rectangle again the ball should change color temporarily and then back again to the original color and keep doing this.

Please help the new programmer.

Thank you!

Below is what i have done so far


CPP / C++ / C Code:
#include<windows.h>

#define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW); 

#define AppName "Moving Ball"
#define Caption "Moving Ball ..."

char BCX_STR [1024*1024];

static int     BCX_GetDiaUnit;
static int     BCX_cxBaseUnit;
static int     BCX_cyBaseUnit;
static int     BCX_ScaleX;
static int     BCX_ScaleY;
static		   HANDLE  Form1;
double		   MIN (double,double);
double		   MAX (double,double);

int     WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
void    FormLoad (HANDLE);LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); 
double MAX (double a, double b)
{  
	if (a > b)  
		{   
			return a;
		  } 
			return b;
}

double MIN (double a, double b)
{  
	if (a < b)
	{
		return a;
	
	  }  
	
		return b;
} 

// standard main for Windows GUI
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int CmdShow)
{  
	static  WNDCLASS  Wc; 
	memset(&Wc,0,sizeof(Wc)); 
	static  MSG  Msg; 
	memset(&Msg,0,sizeof(Msg));
	Wc.style=CS_HREDRAW | CS_VREDRAW;
	Wc.lpfnWndProc=WndProc; 
	Wc.cbClsExtra=0; 
	Wc.cbWndExtra=0;  
	Wc.hInstance=hInst; 
	Wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
	Wc.hCursor=LoadCursor(NULL,IDC_ARROW); 
	Wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); 
	Wc.lpszMenuName=NULL; 
	Wc.lpszClassName=AppName; 
	RegisterClass(&Wc); 
	FormLoad(hInst);
	// 50ms here, lower value gives higher speed
	SetTimer((HWND)Form1,1,50,NULL);  
	// ye olde event message loop
while(GetMessage(&Msg,NULL,0,0)) 
 {
    if (!IsWindow((HWND)Form1)||!IsDialogMessage((HWND)Form1,&Msg))
		{
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
	    } 
 }  return Msg.wParam;
}

 // create the form and show it (somewhat older style)

void FormLoad (HANDLE hInst)
{
	// get the scale factors  
	BCX_GetDiaUnit = GetDialogBaseUnits();
	BCX_cxBaseUnit = LOWORD(BCX_GetDiaUnit);
	BCX_cyBaseUnit = HIWORD(BCX_GetDiaUnit);
	BCX_ScaleX = BCX_cxBaseUnit/4;
	BCX_ScaleY = BCX_cyBaseUnit/8;
	// now the form
	Form1=CreateWindow(AppName,Caption,
		DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU,
	10*BCX_ScaleX,20*BCX_ScaleY,250*BCX_ScaleX,180*BCX_ScaleY,NULL,
		(HMENU)NULL,(HINSTANCE)hInst,NULL);  Show((HWND)Form1);
}

// event message handler

LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	static  HANDLE  hBitmap;
	static  HBRUSH  hBrush;
	static  HBRUSH  rBrush;
	static  HDC  hdc; 
	static  HDC  hdcMem; 
	static int cxClient; 
	static int cyClient;
	static int xCenter;
	static int yCenter;
	static int cxTotal;
	static int cyTotal; 
	static int cxRadius;  
	static int cyRadius;  
	static int cxMove;
	static int cyMove;
	static int xPixel;
	static int yPixel;
	static int nScale;
	
while(1) 
 {
    if (Msg == WM_CREATE)
    {      
		hdc = GetDC(hWnd);
		xPixel = GetDeviceCaps(hdc,ASPECTX);
		yPixel = GetDeviceCaps(hdc,ASPECTY);
		ReleaseDC(hWnd,hdc);
		return 0;
		break;
    }
    // draw the ball
    if (Msg == WM_SIZE)
    {
		xCenter = (cxClient=LOWORD(lParam))/2;
		yCenter = (cyClient=HIWORD(lParam))/2;
		nScale = (int)MIN(cxClient*xPixel,cyClient*yPixel)/8;
		cxRadius = nScale/xPixel;
		cyRadius = nScale/yPixel;
		cxMove = (int)MAX(1,cxRadius/4);
		cyMove = (int)MAX(1,cyRadius/4);
		cxTotal = 2*(cxRadius+cxMove);
		cyTotal = 2*(cyRadius+cyMove);
	
		if (hBitmap)
		{
			DeleteObject(hBitmap); 
		}
		hdc = GetDC(hWnd);
		hdcMem = CreateCompatibleDC(hdc);
		hBitmap = CreateCompatibleBitmap(hdc,cxTotal,cyTotal);
		ReleaseDC(hWnd,hdc);
		SelectObject(hdcMem,hBitmap);

		rBrush = CreateSolidBrush(RGB(95,158,160));
		SelectObject(hdcMem,rBrush);


		Rectangle (hdcMem, -100, -100, 250, 250);
		DeleteObject(rBrush);
		
		hBrush = CreateSolidBrush(RGB(255, 255, 0));
		SelectObject(hdcMem,hBrush);
		SetBkColor(hdcMem,RGB(255,255,255));
		Ellipse(hdcMem,cxMove,cyMove,cxTotal-cxMove,cyTotal-cyMove);
		DeleteDC(hdcMem);
		DeleteObject(hBrush);
		return 0;
		break;
    }
    // move the ball
    if (Msg == WM_TIMER)
    {
		if (!hBitmap)
		{
			break;
		}
		hdc = GetDC(hWnd);
		hdcMem = CreateCompatibleDC(hdc);
		SelectObject(hdcMem,hBitmap);
		BitBlt(hdc,xCenter-cxTotal/2,yCenter-cyTotal/2,cxTotal,cyTotal,hdcMem,0,0,SRCCOPY);
		ReleaseDC(hWnd,hdc);
		DeleteDC(hdcMem);
		xCenter += cxMove;
	
		if (xCenter+cxRadius>=cxClient||xCenter-cxRadius<=0)
		{
			cxMove = -cxMove;
		}
		
		return 0;
		break;
    }
  // clean up and exit program

if (Msg == WM_DESTROY)
    {
	if (hBitmap)
	{
        DeleteObject(hBitmap);
      }
	KillTimer((HWND)Form1,1);
	PostQuitMessage(0);
	return 0;
    }
    break;
  }
  return DefWindowProc(hWnd, Msg, wParam, lParam);
}
Last edited by LuciWiz : 21-Oct-2007 at 14:55. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 21-Oct-2007, 21:12
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,006
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: " THE BOUNCING BALL in C++"


I haven't got around to changing the ball color [yet], but the 'y' axis of code is missing in the code. Where the ball is moved, make sure this is in there too:
CPP / C++ / C Code:
	xCenter += cxMove;
	yCenter += cyMove; // this was missing
			
	if (xCenter+cxRadius>=cxClient||xCenter-cxRadius<=0)
	{
		cxMove = -cxMove;
	}
	
        // missing condition.
        if (yCenter+cyRadius >= cyClient || yCenter-cyRadius <= 0)
	{
		cyMove = -cyMove;
	}
Found that comparing your code with other code found here.
(someone must have updated along the way)
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 22-Oct-2007, 06:09
lucamarcus lucamarcus is offline
New Member
 
Join Date: Oct 2007
Posts: 3
lucamarcus is on a distinguished road

Re: " THE BOUNCING BALL in C++"


I tried putting your code it made the ball move funny.

I need it to move along a recatangle and change colors temporally as it hits either ends and change back to original color.
  #4  
Old 22-Oct-2007, 08:54
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,006
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: " THE BOUNCING BALL in C++"


I don't know what you mean by "the ball moves funny", as I only added the missing code parts from my previous post and it bounces around within the window like it should. I did, however, make the ball just a bit smaller. I'll put the original size back and see if makes any difference.

I have NOT started on the ball-color change yet (like I said in the last post), that'll be next, but don't wait for me -- I may not get to it over the next couple of days.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 22-Oct-2007, 10:31
lucamarcus lucamarcus is offline
New Member
 
Join Date: Oct 2007
Posts: 3
lucamarcus is on a distinguished road

Re: " THE BOUNCING BALL in C++"


The main aim is this:

Consider a circle which is moving within a rectangle as shown below:
The circle maintains its yellow colour when moving horizontally within the rectangle. Once the
circle hits the side of the rectangle at point B, it temporarily changes colour to red and reverses its
direction towards A. As it is moving toward A, it retains its yellow colour. Once it hits the rectangle
side at point A, it temporarily changes its colour to red again and starts moving towards B. Once it
is moving towards B, it maintains its yellow colour again. The circle remains in perpetual motion until the user exits.

That is What i need.

Hence i need:

1. The RECTANGLE to be DRAWN ALREADY before the ball even starts moving.

2. The issue of the color changing also. THANKS!!

PLEASE HELP i have been struggling with this for Days now!
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java bouncing blue ball candykane69 Java Forum 4 01-Dec-2006 08:22
cricket hostbreak Sports Forum 21 10-Dec-2005 08:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:11.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.