Fix paddle / fix ball
authorwatkinsr <ryanwatkins54@gmail.com>
Thu, 18 Jun 2020 10:46:19 +0000 (11:46 +0100)
committerwatkinsr <ryanwatkins54@gmail.com>
Thu, 18 Jun 2020 10:46:19 +0000 (11:46 +0100)
31_scrolling_backgrounds.cpp
dot.bmp

index 7d805a47c5773848d712bf5fb028cd77af2c9ff4..e0bcbeefc4cb6a876429ae4dc8fb5316a8f79286 100755 (executable)
@@ -1,4 +1,5 @@
 /*This source code copyrighted by Lazy Foo' Productions (2004-2020)
+
 and may not be redistributed without written permission.*/
 
 //Using SDL, SDL_image, standard IO, vectors, and strings
@@ -43,7 +44,6 @@ class LTexture
                
                //Renders texture at given point
                void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE );
-
                //Gets image dimensions
                int getWidth();
                int getHeight();
@@ -67,16 +67,11 @@ class Dot
 
                //Initializes the variables
                Dot();
-
-               //Takes key presses and adjusts the dot's velocity
-               void handleEvent( SDL_Event& e );
+    Dot(Uint16 x, Uint16 y, Uint8 velX, Uint8 velY, Uint8 w, Uint8 h);
 
                //Moves the dot
                void move();
 
-               // Change dimensions
-         void setDimensions(Uint8 w, Uint8 h);
-
                //Shows the dot on the screen
                void render();
 
@@ -92,6 +87,29 @@ class Dot
          int mHeight;
 };
 
+class Paddle
+{
+    public:
+
+               //Maximum axis velocity of the dot
+               static const Uint8 PADDLE_VEL = 10;
+
+               //Initializes the variables
+    Paddle(Uint16 x, Uint16 y, Uint8 velX, Uint8 velY, Uint8 w, Uint8 h);
+
+               //Takes key presses and adjusts the dot's velocity
+               void handleEvent( SDL_Event& e );
+
+               void move();
+               void render();
+
+    private:
+    
+         SDL_Rect p;
+         
+         int8_t mVelX;
+         int8_t mVelY;
+};
 //Starts up SDL and creates window
 bool init();
 
@@ -109,7 +127,6 @@ SDL_Renderer* gRenderer = NULL;
 
 //Scene textures
 LTexture gDotTexture;
-LTexture gBGTexture;
 
 LTexture::LTexture()
 {
@@ -218,7 +235,10 @@ void LTexture::free()
 void LTexture::setColor( Uint8 red, Uint8 green, Uint8 blue )
 {
        //Modulate texture rgb
-       SDL_SetTextureColorMod( mTexture, red, green, blue );
+       if(SDL_SetTextureColorMod( mTexture, red, green, blue ) < 0)
+       {
+         printf("Error setting dolor for texture: %s", SDL_GetError());
+       }
 }
 
 void LTexture::setBlendMode( SDL_BlendMode blending )
@@ -273,13 +293,63 @@ Dot::Dot()
     mHeight = 10;
 }
 
-void Dot::setDimensions(Uint8 w, Uint8 h)
+Dot::Dot(Uint16 x, Uint16 y, Uint8 velX, Uint8 velY, Uint8 w, Uint8 h)
 {
-  mWidth = w;
-  mHeight = h;
+    //Initialize the offsets
+    mPosX = x;
+    mPosY = y;
+
+    //Initialize the velocity
+    mVelX = velX;
+    mVelY = velY;
+
+    mWidth = w;
+    mHeight = h;
 }
 
-void Dot::handleEvent( SDL_Event& e )
+void Dot::move()
+{
+    //Move the dot left or right
+    mPosX += mVelX;
+
+    //If the dot went too far to the left or right
+    if( ( mPosX < 0 ) || ( mPosX + mWidth > SCREEN_WIDTH ) )
+    {
+        //Move back
+        mPosX -= mVelX;
+    }
+
+    //Move the dot up or down
+    mPosY += mVelY;
+
+    //If the dot went too far up or down
+    if( ( mPosY < 0 ) || ( mPosY + mHeight > SCREEN_HEIGHT ) )
+    {
+        //Move back
+        mPosY -= mVelY;
+    }
+}
+
+void Dot::render()
+{
+  //Show the dot
+  gDotTexture.render( mPosX, mPosY );
+}
+
+Paddle::Paddle(Uint16 x, Uint16 y, Uint8 velX, Uint8 velY, Uint8 w, Uint8 h)
+{
+    //Initialize the offsets
+    p.x = x;
+    p.y = y;
+    p.w = w;
+    p.h = h;
+
+    //Initialize the velocity
+    mVelX = velX;
+    mVelY = velY;
+}
+
+void Paddle::handleEvent( SDL_Event& e )
 {
     //If a key was pressed
        if( e.type == SDL_KEYDOWN && e.key.repeat == 0 )
@@ -287,10 +357,14 @@ void Dot::handleEvent( SDL_Event& e )
         //Adjust the velocity
         switch( e.key.keysym.sym )
         {
-            case SDLK_UP: mVelY -= DOT_VEL; break;
-            case SDLK_DOWN: mVelY += DOT_VEL; break;
-            // case SDLK_LEFT: mVelX -= DOT_VEL; break;
-            // case SDLK_RIGHT: mVelX += DOT_VEL; break;
+            case SDLK_UP: {
+              mVelY -= PADDLE_VEL;
+              break;
+            }
+            case SDLK_DOWN: {
+              mVelY += PADDLE_VEL; 
+              break;
+            }
         }
     }
     //If a key was released
@@ -299,31 +373,29 @@ void Dot::handleEvent( SDL_Event& e )
         //Adjust the velocity
         switch( e.key.keysym.sym )
         {
-            case SDLK_UP: mVelY += DOT_VEL; break;
-            case SDLK_DOWN: mVelY -= DOT_VEL; break;
-            // case SDLK_LEFT: mVelX += DOT_VEL; break;
-            // case SDLK_RIGHT: mVelX -= DOT_VEL; break;
+            case SDLK_UP: mVelY += PADDLE_VEL; break;
+            case SDLK_DOWN: mVelY -= PADDLE_VEL; break;
         }
     }
 }
 
-void Dot::move()
+void Paddle::move()
 {
     //Move the dot up or down
-    mPosY += mVelY;
+    p.y += mVelY;
 
     //If the dot went too far up or down
-    if( ( mPosY < 0 ) || ( mPosY + mHeight > SCREEN_HEIGHT ) )
+    if( ( p.y < 0 ) || ( p.y + p.h > SCREEN_HEIGHT ) )
     {
         //Move back
-        mPosY -= mVelY;
+        p.y -= mVelY;
     }
 }
 
-void Dot::render()
+void Paddle::render()
 {
     //Show the dot
-       gDotTexture.render( mPosX, mPosY );
+    SDL_RenderFillRect(gRenderer, &p);
 }
 
 bool init()
@@ -391,13 +463,6 @@ bool loadMedia()
                success = false;
        }
 
-       //Load background texture
-       if( !gBGTexture.loadFromFile( "bg.png" ) )
-       {
-               printf( "Failed to load background texture!\n" );
-               success = false;
-       }
-
        return success;
 }
 
@@ -405,7 +470,6 @@ void close()
 {
        //Free loaded images
        gDotTexture.free();
-       gBGTexture.free();
 
        //Destroy window        
        SDL_DestroyRenderer( gRenderer );
@@ -441,13 +505,10 @@ int main( int argc, char* args[] )
                        SDL_Event e;
 
                        //The dot that will be moving around on the screen
-                       Dot dot;
+                       Paddle paddle = Paddle(10, SCREEN_HEIGHT/2, 0, 0, 8, 40);
 
-                       Dot movingDot;
-                       movingDot.setDimensions(3, 3);
-
-                       //The background scrolling offset
-                       int scrollingOffset = 0;
+                       Dot ball = Dot(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 2, 1, 10, 10);
+                       gDotTexture.setColor(0xFF, 0xFF, 0xFF);
 
                        //While application is running
                        while( !quit )
@@ -462,33 +523,24 @@ int main( int argc, char* args[] )
                                        }
 
                                        //Handle input for the dot
-                                       dot.handleEvent( e );
+                                       paddle.handleEvent( e );
                                }
 
                                //Move the dot
-                               dot.move();
-
-                               //Scroll background
-                               --scrollingOffset;
-                               if( scrollingOffset < -gBGTexture.getWidth() )
-                               {
-                                       scrollingOffset = 0;
-                               }
+                               paddle.move();
+                               ball.move();
 
                                //Clear screen
-                               SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
+                               SDL_SetRenderDrawColor( gRenderer, 0x00, 0x00, 0x00, 0xFF );
                                SDL_RenderClear( gRenderer );
-
-                               //Render background
-                               gBGTexture.render( scrollingOffset, 0 );
-                               gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 );
-
+               
                                //Render middle line
-                               //SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
+                               SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
                                SDL_RenderDrawLine(gRenderer, SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT);
 
                                //Render objects
-                               dot.render();
+                               paddle.render();
+                               ball.render();
 
                                //Update screen
                                SDL_RenderPresent( gRenderer );
diff --git a/dot.bmp b/dot.bmp
index 16ed38da4395d821e003875a7561f4e230092396..b49a291a85da971040ea8ea2db1e875515c38256 100755 (executable)
Binary files a/dot.bmp and b/dot.bmp differ