]> git.localhorst.tv Git - sdl-test7.git/commitdiff
added some comments
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 1 Jun 2012 20:53:19 +0000 (20:53 +0000)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 1 Jun 2012 20:53:19 +0000 (20:53 +0000)
src/app/Application.cpp
src/geometry/AABB.cpp
src/geometry/Shape.h
src/pong/Match.cpp
src/pong/Match.h

index 5b1b3590cd4cbff3b0055c38ac5377e049c29022..e98249bba3c2e1d098f101f422a5470f7acd1530 100644 (file)
@@ -23,7 +23,7 @@ Application::Application(sdl::InitScreen *screen, State *initialState)
 }
 
 Application::~Application(void) {
-       PopAllStates();
+       PopAllStates(); // calls virtual function -> segfaults
 }
 
 
index e40c69fc40daab3b0c5245606dd893eb4db98c9e..9ea60b59accbb08c6de4792b395e7a34e9695710 100644 (file)
 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 {
index 79c3485d59e00c86122119285bc1b2d87a6dfe9a..9232a3a671edbc1959bcda0e6c00c89e88a52ca6 100644 (file)
@@ -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;
index 229a8fce034fd127c9303956e613d9b33099bb8c..6de4b03423d5ce4414a2208dd65ef63bea930b91 100644 (file)
@@ -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);
 }
 
 }
index 8d36a0854ada6811655eb2862d830fcce63a47da..e3e227e2054bf5d3e97a0ac1ccb56759b9bd4063 100644 (file)
@@ -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<game::Entity *> entities;