From 96fa0960d09a8f84639679ebfab9a9e45b45350e Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Thu, 18 Jan 2018 11:45:19 +0100 Subject: [PATCH] Added mouse control Mode --- breakout.c | 80 +++++++++++++++++++++++++++++++++++------------------- breakout.h | 5 ++++ main.c | 7 +++-- paddle.c | 0 4 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 paddle.c diff --git a/breakout.c b/breakout.c index 098097a..a38b0ff 100644 --- a/breakout.c +++ b/breakout.c @@ -168,34 +168,36 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){ right = (ballCenter.x) > (rect->x) + (rect->w); top = (ballCenter.y) < (rect->y); bottom = (ballCenter.y) > (rect->y) + (rect->h); - if (top) - corner.y = rect->y; - else if (bottom) - corner.y = (rect->y) + (rect->w); - else - yMid = true; - if (left) - corner.x = rect->x; - else if (right) - corner.x = (rect->x) + (rect->w); - else - xMid = true; + // if (top) + // corner.y = rect->y; + // else if (bottom) + // corner.y = (rect->y) + (rect->w); + // else + // yMid = true; + // if (left) + // corner.x = rect->x; + // else if (right) + // corner.x = (rect->x) + (rect->w); + // else + // xMid = true; + yMid = !(top || bottom); + xMid = !(left || right); if (yMid) (obj->Momentum).x = -(obj->Momentum).x; if (xMid) (obj->Momentum).y = -(obj->Momentum).y; - if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n"); - if (yMid || xMid) { // Ball collides with Edge - } else { // Ball collides with corner - /* - * perpendicular = vectorRotation(vectorSub(center, corner)); - * angle = fabs(perpendicular - oldMomentum); - * if (oldMomentum < perpendicular) - * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed)); - * else - * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed)); - */ - } + // if (yMid && xMid) printf("WARNING: Ball is completely inside block!\n"); + // if (yMid || xMid) { // Ball collides with Edge + // } else { // Ball collides with corner + // /* + // * perpendicular = vectorRotation(vectorSub(center, corner)); + // * angle = fabs(perpendicular - oldMomentum); + // * if (oldMomentum < perpendicular) + // * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), (obj->Speed)); + // * else + // * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), (obj->Speed)); + // */ + // } return true; } /* BALL_CollideWithRect */ @@ -242,7 +244,7 @@ void BALL_Update(Ball * obj, Paddle * paddle, Block * blocks, int BlockCount){ (obj->Rotation) += (obj->RotationValue); // No effect on physics (obj->Location) = vectorAdd((obj->Location), oldMomentum); - if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) { + if (RECT_Collide(&(obj->TargetRect), &(paddle->TargetRect))) { (obj->Location) = oldLocation; // Maybe remove this BALL_SteerMomentum(obj, paddle); // Sets it to unit vector! // Following assumes that the paddle position was udated before the ball was updated @@ -315,7 +317,8 @@ Paddle PADDLE_CreateDefault(){ .TargetRect = (SDL_Rect) {.x = (BREAKOUT_BoxWidth - defaultpaddlewidth) / 2, .y = BREAKOUT_BoxHeight - 100, .w = defaultpaddlewidth, .h = 30 }, .TextureIndex = 0, .Speed = 10, - .SteeringAngle = 40.0f + .SteeringAngle = 40.0f, + .Mode = MouseControl }; // Objekt für die Eigenschaften des Balls } @@ -348,7 +351,28 @@ void DOUBLE_Constrain(double * variable, double min, double max){ } void PADDLE_Update(Paddle * obj, const Uint8 * keystate){ - bool leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys), rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys); + bool leftKeyPressed = false, rightKeyPressed = false; + int paddleXMid = (obj->TargetRect).x + ((obj->TargetRect).w / 2); + int mouseX; + + switch (obj->Mode) { + case MouseControl: + SDL_GetMouseState(&mouseX, NULL); + if (abs(mouseX - paddleXMid) > (obj->Speed)) { + if (mouseX > paddleXMid) + rightKeyPressed = true; + else + leftKeyPressed = true; + } + break; + case KeyboardControl: + leftKeyPressed = KeyPressed(keystate, PADDLE_MoveLeftKeys); + rightKeyPressed = KeyPressed(keystate, PADDLE_MoveRightKeys); + break; + default: + printf("Unknown Paddle Control Mode: %d!\n", obj->Mode); + break; + } if (leftKeyPressed && (!rightKeyPressed)) { ((obj->TargetRect).x) -= (obj->Speed); @@ -356,7 +380,7 @@ void PADDLE_Update(Paddle * obj, const Uint8 * keystate){ ((obj->TargetRect).x) += (obj->Speed); } INT_Constrain(&((obj->TargetRect).x), 0, (BREAKOUT_BoxWidth - ((obj->TargetRect).w))); -} +} /* PADDLE_Update */ void PADDLE_DestroyObject(Paddle * obj){ } diff --git a/breakout.h b/breakout.h index c687f02..8a638d9 100644 --- a/breakout.h +++ b/breakout.h @@ -9,6 +9,10 @@ #include "vector.h" +// Enums +typedef enum controlModeEnum {KeyboardControl = 0, MouseControl = 1} ControlMode; +// End Enums + // Structs typedef struct ballStruct { Vector Location, Momentum; @@ -23,6 +27,7 @@ typedef struct paddleStruct { int TextureIndex; int Speed; double SteeringAngle; + ControlMode Mode; } Paddle; // Objekt für die Eigenschaften des Paddles typedef struct blockStruct { diff --git a/main.c b/main.c index 903e0ea..986dd9a 100644 --- a/main.c +++ b/main.c @@ -101,8 +101,9 @@ void HandleSDLEvents(){ running = false; break; case SDL_KEYDOWN: - if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false; - else keyPress(event.key); + // if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) running = false; + // else + keyPress(event.key); break; case SDL_MOUSEBUTTONDOWN: mousePress(event.button); @@ -112,7 +113,7 @@ void HandleSDLEvents(){ break; } } -} +} /* HandleSDLEvents */ void mousePress(SDL_MouseButtonEvent b){ // Debug prop if (b.button == SDL_BUTTON_LEFT) { diff --git a/paddle.c b/paddle.c new file mode 100644 index 0000000..e69de29