4 * Created on: Apr 9, 2012
10 #include "../app/Control.h"
19 using game::Collision;
21 using std::runtime_error;
22 using std::stringstream;
30 , scoreFont(TTF_OpenFont("data/font/Magra-Regular.ttf", 30))
40 , rightPaddle(10, 100)
50 throw runtime_error("failed to load score font");
53 ball.Translate(Entity::Vector(400, 240));
54 ball.Accelerate(Entity::Vector(180, 180));
55 // ball.SetMaxVelocity(500.0f);
57 secondBall.Translate(Entity::Vector(300, 240));
58 secondBall.Accelerate(Entity::Vector(-50, -50), 1);
59 // secondBall.SetMaxVelocity(600.0f);
61 thirdBall.Translate(Entity::Vector(200, 440));
62 thirdBall.Accelerate(Entity::Vector(60, 40));
64 leftPaddle.Translate(Entity::Vector(5, 200));
65 rightPaddle.Translate(Entity::Vector(795, 280));
67 leftPaddle.SetMovementSpeed(Entity::Vector(0, paddleSpeed));
68 rightPaddle.SetMovementSpeed(Entity::Vector(0, paddleSpeed));
70 topWall.Translate(Entity::Vector(400, -5));
71 rightWall.Translate(Entity::Vector(805, 240));
72 bottomWall.Translate(Entity::Vector(400, 485));
73 leftWall.Translate(Entity::Vector(-5, 240));
76 entities.push_back(&ball);
77 entities.push_back(&secondBall);
78 entities.push_back(&thirdBall);
79 entities.push_back(&leftPaddle);
80 entities.push_back(&rightPaddle);
81 entities.push_back(&topWall);
82 entities.push_back(&bottomWall);
83 entities.push_back(&leftWall);
84 entities.push_back(&rightWall);
86 collisions.reserve(2 * entities.size());
94 void Match::EnterState(Control *c, SDL_Surface *screen) {
98 void Match::ExitState(void) {
100 TTF_CloseFont(scoreFont);
105 void Match::HandleEvent(const SDL_Event &e) {
106 if (e.type == SDL_KEYDOWN) {
107 if (e.key.keysym.sym == SDLK_w) {
108 leftPaddle.StartMovingUp();
109 } else if (e.key.keysym.sym == SDLK_s) {
110 leftPaddle.StartMovingDown();
111 } else if (e.key.keysym.sym == SDLK_UP) {
112 rightPaddle.StartMovingUp();
113 } else if (e.key.keysym.sym == SDLK_DOWN) {
114 rightPaddle.StartMovingDown();
116 } else if (e.type == SDL_KEYUP) {
117 if (e.key.keysym.sym == SDLK_w) {
118 leftPaddle.StopMovingUp();
119 } else if (e.key.keysym.sym == SDLK_s) {
120 leftPaddle.StopMovingDown();
121 } else if (e.key.keysym.sym == SDLK_UP) {
122 rightPaddle.StopMovingUp();
123 } else if (e.key.keysym.sym == SDLK_DOWN) {
124 rightPaddle.StopMovingDown();
125 } else if (e.key.keysym.sym == SDLK_q) {
126 if (ctrl) ctrl->Quit();
131 void Match::UpdateWorld(const Timer &timer) {
132 UpdateEntities(timer);
133 CheckCollisions(timer);
134 HandleCollisions(timer);
137 void Match::UpdateEntities(const Timer &timer) {
138 for (vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
143 void Match::CheckCollisions(const Timer &timer) {
145 for (vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
146 for (vector<Entity *>::const_iterator j(i + 1); j != end; ++j) {
147 if ((*i)->CheckCollision(**j, normal)) {
148 collisions.push_back(Collision(*i, *j, normal));
154 void Match::HandleCollisions(const Timer &timer) {
155 for (vector<Collision>::const_iterator i(collisions.begin()), end(collisions.end()); i != end; ++i) {
156 Entity::CollisionResponse(*(i->left), i->normal, *(i->right));
158 // TODO: resolve collisions of static objects
159 for (vector<Collision>::const_iterator i(collisions.begin()), end(collisions.end()); i != end; ++i) {
160 i->left->Collide(*i->right, i->normal);
161 i->right->Collide(*i->left, i->normal);
163 if (!collisions.empty()) {
170 void Match::Render(SDL_Surface *screen) {
171 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
172 for (vector<Entity *>::const_iterator i(entities.begin()), end(entities.end()); i != end; ++i) {
173 (*i)->Render(screen);
178 void Match::RenderHUD(SDL_Surface *screen) {
186 void Match::RenderScore(SDL_Surface *screen) {
187 SDL_Rect dest = { 2, 2, 0, 0 };
188 SDL_BlitSurface(leftScoreText, 0, screen, &dest);
190 dest.x = screen->w - rightScoreText->w - 2;
191 SDL_BlitSurface(rightScoreText, 0, screen, &dest);
194 void Match::UpdateScore(SDL_Surface *screen) {
195 if (!scoreFont) return;
196 if (leftScoreText) SDL_FreeSurface(leftScoreText);
197 if (rightScoreText) SDL_FreeSurface(rightScoreText);
205 s << rightWall.HitCount();
206 leftScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color);
209 s << leftWall.HitCount();
210 rightScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color);