From: Daniel Karbach Date: Fri, 1 Jun 2012 20:53:19 +0000 (+0000) Subject: added some comments X-Git-Url: http://git.localhorst.tv/?p=sdl-test7.git;a=commitdiff_plain;h=403973e00a69339b9b382473ac514a845fb70ed5 added some comments --- diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 5b1b359..e98249b 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -23,7 +23,7 @@ Application::Application(sdl::InitScreen *screen, State *initialState) } Application::~Application(void) { - PopAllStates(); + PopAllStates(); // calls virtual function -> segfaults } diff --git a/src/geometry/AABB.cpp b/src/geometry/AABB.cpp index e40c69f..9ea60b5 100644 --- a/src/geometry/AABB.cpp +++ b/src/geometry/AABB.cpp @@ -13,7 +13,11 @@ namespace geometry { bool AABB::Overlaps(const Shape &other, Vector &normal) const { + // This call reveals the real type of this shape by overloading and the + // other one via polymorphism. if (other.Overlaps(*this, normal)) { + // Since the other shape's Overlaps() returns its own surface normal, + // it has to be inverted here. normal *= -1; return true; } else { diff --git a/src/geometry/Shape.h b/src/geometry/Shape.h index 79c3485..9232a3a 100644 --- a/src/geometry/Shape.h +++ b/src/geometry/Shape.h @@ -19,6 +19,9 @@ class AABB; class Circle; class FakeLens; +/// Base class for all "shapes" (i.e. collision primitives). +/// This serves as an interface for game::Entity to double dispatch the +/// collision detection algorithms. class Shape { public: @@ -31,6 +34,11 @@ class Shape { virtual ~Shape(void) { }; public: + /// Check if this shape overlaps the given and, if it does, write the + /// surface normal into given vector. + /// All shapes must override this method to dispatch to the specialized + /// version of Overlaps(). + /// See AABB::Overlaps(const Shape &, Vector &normal) const for details. virtual bool Overlaps(const Shape &, Vector &normal) const = 0; virtual bool Overlaps(const AABB &, Vector &normal) const = 0; diff --git a/src/pong/Match.cpp b/src/pong/Match.cpp index 229a8fc..6de4b03 100644 --- a/src/pong/Match.cpp +++ b/src/pong/Match.cpp @@ -26,15 +26,21 @@ namespace pong { Match::Match(void) : ctrl(0) -, scoreFont(TTF_OpenFont("data/font/Magra-Regular.ttf", 30)) +, scoreFont(TTF_OpenFont("data/font/Magra-Regular.ttf", 24)) , leftScoreText(0) , rightScoreText(0) +, topScoreText(0) +, bottomScoreText(0) +, totalScoreText(0) , paddleSpeed(250) , worldWidth(800) , worldHeight(480) , ball(10) , secondBall(7) , thirdBall(7) +, fourthBall(8) +, fifthBall(9) +, sixthBall(11) , leftPaddle(Entity::Vector(10, 100)) , rightPaddle(Entity::Vector(10, 100)) , topWall(Entity::Vector(800, 20)) @@ -61,6 +67,18 @@ Match::Match(void) thirdBall.SetVelocity(Entity::Vector(100, 100)); thirdBall.SetMaxVelocity(600.0f); + fourthBall.SetPosition(Entity::Vector(550, 250)); + fourthBall.SetVelocity(Entity::Vector(100, 100)); + fourthBall.SetMaxVelocity(600.0f); + + fifthBall.SetPosition(Entity::Vector(340, 250)); + fifthBall.SetVelocity(Entity::Vector(100, 100)); + fifthBall.SetMaxVelocity(600.0f); + + sixthBall.SetPosition(Entity::Vector(550, 350)); + sixthBall.SetVelocity(Entity::Vector(100, 100)); + sixthBall.SetMaxVelocity(600.0f); + leftPaddle.SetPosition(Entity::Vector(0, 200)); rightPaddle.SetPosition(Entity::Vector(790, 280)); @@ -80,10 +98,13 @@ Match::Match(void) bottomWall.SetStatic(); leftWall.SetStatic(); - entities.reserve(9); + entities.reserve(12); entities.push_back(&ball); entities.push_back(&secondBall); entities.push_back(&thirdBall); +// entities.push_back(&fourthBall); +// entities.push_back(&fifthBall); +// entities.push_back(&sixthBall); entities.push_back(&leftPaddle); entities.push_back(&rightPaddle); entities.push_back(&topWall); @@ -210,12 +231,27 @@ void Match::RenderScore(SDL_Surface *screen) { dest.x = screen->w - rightScoreText->w - 2; SDL_BlitSurface(rightScoreText, 0, screen, &dest); + +// dest.x = 2; +// dest.y = screen->h - topScoreText->h - 2; +// SDL_BlitSurface(topScoreText, 0, screen, &dest); +// +// dest.x = screen->w - bottomScoreText->w - 2; +// dest.y = screen->h - bottomScoreText->h - 2; +// SDL_BlitSurface(bottomScoreText, 0, screen, &dest); +// +// dest.x = screen->w/2 - totalScoreText->w/2; +// dest.y = 2; +// SDL_BlitSurface(totalScoreText, 0, screen, &dest); } void Match::UpdateScore(SDL_Surface *screen) { if (!scoreFont) return; if (leftScoreText) SDL_FreeSurface(leftScoreText); if (rightScoreText) SDL_FreeSurface(rightScoreText); + if (topScoreText) SDL_FreeSurface(topScoreText); + if (bottomScoreText) SDL_FreeSurface(bottomScoreText); + if (totalScoreText) SDL_FreeSurface(totalScoreText); SDL_Color color; color.r = 0xFF; @@ -223,12 +259,24 @@ void Match::UpdateScore(SDL_Surface *screen) { color.b = 0xFF; stringstream s; - s << rightWall.HitCount(); + s << "right: " << rightWall.HitCount(); leftScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color); s.str(""); - s << leftWall.HitCount(); + s << "left: " << leftWall.HitCount(); rightScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color); + + s.str(""); + s << "top: " << topWall.HitCount(); + topScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color); + + s.str(""); + s << "bottom: " << bottomWall.HitCount(); + bottomScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color); + + s.str(""); + s << "total: " << rightWall.HitCount() + leftWall.HitCount() + topWall.HitCount() + bottomWall.HitCount(); + totalScoreText = TTF_RenderUTF8_Blended(scoreFont, s.str().c_str(), color); } } diff --git a/src/pong/Match.h b/src/pong/Match.h index 8d36a08..e3e227e 100644 --- a/src/pong/Match.h +++ b/src/pong/Match.h @@ -49,10 +49,10 @@ class Match private: app::Control *ctrl; TTF_Font *scoreFont; - SDL_Surface *leftScoreText, *rightScoreText; + SDL_Surface *leftScoreText, *rightScoreText, *topScoreText, *bottomScoreText, *totalScoreText; Sint32 paddleSpeed; Uint32 worldWidth, worldHeight; - Ball ball, secondBall, thirdBall; + Ball ball, secondBall, thirdBall, fourthBall, fifthBall, sixthBall; Paddle leftPaddle, rightPaddle; CountingWall topWall, bottomWall, leftWall, rightWall; std::vector entities;