From 10baa49bf3be2ad03b3adf5be5cb0fcf5f10fcfb Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Thu, 21 Aug 2008 19:06:56 -0400 Subject: [PATCH] cleaned how balls are added --- src/entityCreator.cpp | 83 ++++++++++++++++++++++++++++++++++++---------- src/entityCreator.h | 2 +- src/game.cpp | 5 ++- src/game.h | 2 +- src/input/inputManager.cpp | 17 ++++++++-- src/input/inputManager.h | 3 ++ src/main.cpp | 2 +- 7 files changed, 89 insertions(+), 25 deletions(-) diff --git a/src/entityCreator.cpp b/src/entityCreator.cpp index eddfb3d..b0c034c 100644 --- a/src/entityCreator.cpp +++ b/src/entityCreator.cpp @@ -20,55 +20,102 @@ #include #include "entityManager.h" +#include "input/inputManager.h" + #include "Entities/Ball.h" #include "graphics/colors.h" + /// ***** Private Variables ***** typedef std::queue queBall; -queBall Balls; + +queBall startBalls; +queBall mouseBalls; + +/// ***** Private Method Headers ***** + +Ball* addBall(const Vector2& pos, + float radius, + const float* color, + queBall& que); + +void removeBall(queBall& que); +void removeAllBalls(queBall& que); /// ***** Initializers/Cleaners ***** void creator::init() { - addBall(Vector2(50, 50), 20, cWhite); - addBall(Vector2(150, 50), 20, cGrey); - addBall(Vector2(50, 100), 20, cRed); - addBall(Vector2(100, 100), 20, cGreen); - addBall(Vector2(150, 100), 20, cBlue); - addBall(Vector2(50, 150), 20, cYellow); - addBall(Vector2(100, 150), 20, cMagenta); - addBall(Vector2(150, 150), 20, cCyan); + addBall(Vector2(50, 50), 20, cWhite, startBalls); + addBall(Vector2(150, 50), 20, cGrey, startBalls); + addBall(Vector2(50, 100), 20, cRed, startBalls); + addBall(Vector2(100, 100), 20, cGreen, startBalls); + addBall(Vector2(150, 100), 20, cBlue, startBalls); + addBall(Vector2(50, 150), 20, cYellow, startBalls); + addBall(Vector2(100, 150), 20, cMagenta, startBalls); + addBall(Vector2(150, 150), 20, cCyan, startBalls); } void creator::clean() { - removeAllBalls(); + removeAllBalls(startBalls); + removeAllBalls(mouseBalls); } /// ***** Public Methods ***** void creator::addBall(const Vector2& pos, float radius, const float* color) { + addBall(pos, radius, color, mouseBalls); +} +void creator::removeBall() +{ + removeBall(mouseBalls); +} +void creator::removeAllBalls() +{ + removeAllBalls(mouseBalls); +} + +void creator::handleInput() +{ + if(input::mouseLeft()) + addBall(input::mousePosition(), 10, cCyan); + + if(input::mouseRight() && ! mouseBalls.empty()) + removeBall(); +} + +/// ***** Private Methods ***** + +Ball* addBall(const Vector2& pos, + float radius, + const float* color, + queBall& que) +{ Ball* ball = new Ball(pos, radius, color); - Balls.push(ball); + que.push(ball); manager::add(ball); + + return ball; } -void creator::removeBall() + +void removeBall(queBall& que) { - Ball* ball = Balls.front(); + Ball* ball = que.front(); - Balls.pop(); + manager::remove(ball); + que.pop(); delete ball; } -void creator::removeAllBalls() +void removeAllBalls(queBall& que) { - while(! Balls.empty() ) + while(! que.empty() ) { - Ball* ball = Balls.front(); - Balls.pop(); + Ball* ball = que.front(); + que.pop(); manager::remove(ball); delete ball; diff --git a/src/entityCreator.h b/src/entityCreator.h index 6987c34..4c1b4c1 100644 --- a/src/entityCreator.h +++ b/src/entityCreator.h @@ -30,6 +30,6 @@ namespace creator void removeBall(); void removeAllBalls(); - void input(); + void handleInput(); } #endif // ENTITYCREATOR_H diff --git a/src/game.cpp b/src/game.cpp index 3b7a34b..929f6b5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -81,8 +81,11 @@ void game::clean() delete running; } -void game::input() +void game::handleInput() { + creator::handleInput(); + + if(running->pushMe()) push_State = running; diff --git a/src/game.h b/src/game.h index 83d2f51..272cd83 100644 --- a/src/game.h +++ b/src/game.h @@ -26,7 +26,7 @@ namespace game void init(); void clean(); - void input(); + void handleInput(); void update(float); void draw(); } diff --git a/src/input/inputManager.cpp b/src/input/inputManager.cpp index 0df4baa..ebbd72e 100644 --- a/src/input/inputManager.cpp +++ b/src/input/inputManager.cpp @@ -74,14 +74,25 @@ void input::update() Vector2 input::mousePosition() { - int x; - int y; - + int x,y; SDL_GetMouseState(&x, &y); return Vector2(x,y); } +bool input::mouseLeft() +{ + Uint8 state = SDL_GetMouseState(NULL, NULL); + + return state & SDL_BUTTON(1); +} +bool input::mouseRight() +{ + Uint8 state = SDL_GetMouseState(NULL, NULL); + + return state & SDL_BUTTON(3); +} + bool input::isPressed(Uint8 key) { return keyState[key] == isP || keyState[key] == wasP; diff --git a/src/input/inputManager.h b/src/input/inputManager.h index 3e0e45d..13b6583 100644 --- a/src/input/inputManager.h +++ b/src/input/inputManager.h @@ -33,6 +33,9 @@ namespace input Vector2 mousePosition(); + bool mouseLeft(); + bool mouseRight(); + bool isPressed(Uint8); bool isReleased(Uint8); diff --git a/src/main.cpp b/src/main.cpp index 5381a86..d8ca3e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -181,7 +181,7 @@ void handleInput() { input::update(); - game::input(); + game::handleInput(); if(cfg::endGame()) is_Running = false; -- 2.10.2