#include "ball.h" #include "playingfield.h" #include extern LPDIRECTDRAW lpDDraw; //the directdraw object extern LPDIRECTDRAWSURFACE lpPrimarySurface; //the primary surface extern LPDIRECTDRAWSURFACE lpOffScreenSurface; //an offscreen surface extern LPDIRECTDRAWSURFACE lpBackGroundSurface; //offsreen surface for the background extern LPDIRECTDRAWSURFACE lpBackBuffer; //Backbuffer for page-flipping CBall::CBall() { x = 0; y = 0; BallSprite = new CSprite( lpOffScreenSurface, "ball.bmp" ); SpriteXOffset = BallSprite->GetWidth() / 2; SpriteYOffset = BallSprite->GetHeight() / 2; } CBall::CBall( long Xin, long Yin, C2DVector Vin ) { x = Xin; y = Yin; Velocity = Vin; BallSprite = new CSprite( lpOffScreenSurface, "ball.bmp" ); SpriteXOffset = BallSprite->GetWidth() / 2; SpriteYOffset = BallSprite->GetHeight() / 2; } CBall::~CBall() { delete BallSprite; BallSprite = NULL; } void CBall::SetParent( CPlayingField* newparent ) { parent = newparent; } void CBall::draw( LPDIRECTDRAWSURFACE Surface ) { RECT rSource; rSource.bottom = BallSprite->GetY() + BallSprite->GetHeight(); rSource.left = BallSprite->GetX(); rSource.right = BallSprite->GetX() + BallSprite->GetWidth(); rSource.top = BallSprite->GetY(); Surface->BltFast( x - SpriteXOffset, y - SpriteYOffset, BallSprite->GetLocation(), &rSource, DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY ); } void CBall::SetVelocity( long speed, int angle ) { Velocity.SetLength( speed ); Velocity.SetAngle( angle ); } void CBall::move() { RECT Bounds; RECT PadBounds; x = x + Velocity.GetX(); y = y + Velocity.GetY(); C2DVector tnormal; tnormal.SetLength( 10 ); Bounds = parent->GetBoundaries(); PadBounds = parent->ThePad->GetPadArea(); if( (x > PadBounds.left) && (x < PadBounds.right) && (y > PadBounds.top) ) { tnormal.SetAngle( 90 ); HitBoundary( &tnormal ); } if( x > Bounds.right ) { tnormal.SetAngle( 180 ); HitBoundary( &tnormal ); } if( x < Bounds.left ) { tnormal.SetAngle( 0 ); HitBoundary( &tnormal ); } if( y > Bounds.bottom ) { tnormal.SetAngle( 90 ); HitBoundary( &tnormal ); } if( y < Bounds.top ) { tnormal.SetAngle( 270 ); HitBoundary( &tnormal ); } } void CBall::HitBoundary( C2DVector* BoundaryNormal ) { int angle; int OppAngle; int NormDiffAngle; angle = Velocity.GetAngle(); OppAngle = (angle + 180) % 360; if( BoundaryNormal->GetAngle() >= OppAngle ) { NormDiffAngle = BoundaryNormal->GetAngle() - OppAngle; angle = (BoundaryNormal->GetAngle() + NormDiffAngle) % 360; } if( BoundaryNormal->GetAngle() < OppAngle ) { NormDiffAngle = OppAngle - BoundaryNormal->GetAngle(); angle = BoundaryNormal->GetAngle() - NormDiffAngle; if( angle < 0 ) angle += 360; } Velocity.SetAngle( angle ); }