summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@jacobmcdonnell.com>2024-02-19 20:43:39 -0500
committerJacob McDonnell <jacob@jacobmcdonnell.com>2024-02-19 20:43:39 -0500
commit563d44c4a3cabeddc442bfe8377bc2cdfde4a6dd (patch)
tree484a7c958dfe782ec7800fa2a3b0b828af46bae2
parent9d4fbd735e60c8bfd8360a9cdcfc1c757a6cced1 (diff)
Fixed screen flicker and added ball speedup
-rw-r--r--README.md3
-rw-r--r--cmake-build-debug/.ninja_depsbin24260 -> 34508 bytes
-rw-r--r--cmake-build-debug/.ninja_log20
-rw-r--r--cmake-build-debug/CMakeFiles/pong.dir/main.c.obin57816 -> 58160 bytes
-rw-r--r--cmake-build-debug/Testing/Temporary/LastTest.log4
-rwxr-xr-xcmake-build-debug/pongbin64344 -> 64312 bytes
-rw-r--r--main.c40
-rw-r--r--pong.c1
-rw-r--r--screen.c7
-rw-r--r--screen.h1
10 files changed, 46 insertions, 30 deletions
diff --git a/README.md b/README.md
index 4f3ac90..719ffee 100644
--- a/README.md
+++ b/README.md
@@ -6,5 +6,4 @@ with SDL.
- Add score count to the screen
-- Fix screen flicker
- - Most likely due to the way sprites are rendered on the screen \ No newline at end of file
+- Fix collision glitch on ball and paddles \ No newline at end of file
diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps
index 13da4e7..e470b2a 100644
--- a/cmake-build-debug/.ninja_deps
+++ b/cmake-build-debug/.ninja_deps
Binary files differ
diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log
index cb8b03b..5112d74 100644
--- a/cmake-build-debug/.ninja_log
+++ b/cmake-build-debug/.ninja_log
@@ -21,3 +21,23 @@
394 414 1708368627183340023 pong 145d847d1d840665
1 395 1708368754184091622 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
396 416 1708368754207092301 pong 145d847d1d840665
+7 435 1708375527183484552 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+435 460 1708375527213486010 pong 145d847d1d840665
+5 460 1708392195102875536 CMakeFiles/pong.dir/screen.c.o f08710f44569f228
+5 463 1708392195105875614 CMakeFiles/pong.dir/pong.c.o 5450e2f51d587a40
+4 476 1708392195117875926 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+476 503 1708392195147876704 pong 145d847d1d840665
+2 392 1708392805857359387 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+392 412 1708392805880360090 pong 145d847d1d840665
+1 386 1708392900465193657 CMakeFiles/pong.dir/pong.c.o 5450e2f51d587a40
+386 407 1708392900488194336 pong 145d847d1d840665
+1 399 1708393077975348927 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+399 419 1708393077998349609 pong 145d847d1d840665
+1 393 1708393182150441612 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+393 414 1708393182173442299 pong 145d847d1d840665
+2 396 1708393194241802548 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+396 417 1708393194265803265 pong 145d847d1d840665
+1 391 1708393213990392053 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+392 412 1708393214014392770 pong 145d847d1d840665
+1 396 1708393308953216921 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb
+396 416 1708393308976217590 pong 145d847d1d840665
diff --git a/cmake-build-debug/CMakeFiles/pong.dir/main.c.o b/cmake-build-debug/CMakeFiles/pong.dir/main.c.o
index d3d77de..9212738 100644
--- a/cmake-build-debug/CMakeFiles/pong.dir/main.c.o
+++ b/cmake-build-debug/CMakeFiles/pong.dir/main.c.o
Binary files differ
diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log
index 64dd2ff..e95b3fe 100644
--- a/cmake-build-debug/Testing/Temporary/LastTest.log
+++ b/cmake-build-debug/Testing/Temporary/LastTest.log
@@ -1,3 +1,3 @@
-Start testing: Feb 19 13:52 EST
+Start testing: Feb 19 20:41 EST
----------------------------------------------------------
-End testing: Feb 19 13:52 EST
+End testing: Feb 19 20:41 EST
diff --git a/cmake-build-debug/pong b/cmake-build-debug/pong
index ef00f83..b19b205 100755
--- a/cmake-build-debug/pong
+++ b/cmake-build-debug/pong
Binary files differ
diff --git a/main.c b/main.c
index 87369a9..8ebbf8a 100644
--- a/main.c
+++ b/main.c
@@ -3,7 +3,7 @@
#include <time.h>
#include <math.h>
-static uint8_t delay = 10;
+static uint8_t delay = 5;
static uint8_t rightScore = 0;
static uint8_t leftScore = 0;
@@ -13,25 +13,13 @@ void Delay ( const uint8_t seconds ) {
;
}
-void PrintScore ( void ) {
- printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
-}
-
-void Score ( uint8_t * const score, ball_t * const ball ) {
- (*score)++;
- PrintScore();
- Reset( ball );
- if ( delay > 2 ) {
- delay += ( abs( rightScore - leftScore ) > 5 ) ? -1 : 0;
- }
-}
-
int main ( void ) {
paddle_t leftPaddle = { { 0, 0 }, { 0, 0 }, 16, 48 },
rightPaddle = { { SCREEN_WIDTH - 16, 0 }, { 0, 0 }, 16, 48 };
ball_t ball = { { SCREEN_WIDTH >> 1, SCREEN_HEIGHT >> 1 }, { 1, 1 }, 16, 16 };
bool quit = false;
SDL_Event e;
+ uint8_t volley = 0;
if ( !InitScreen() ) {
printf( "Failed to initalize!\n" );
@@ -47,16 +35,28 @@ int main ( void ) {
while ( quit == false ) {
UpdatePosition( &ball.position, ball.velocity );
- ClearScreen();
- Draw( ball, ballSurface );
if ( CheckGroundCollision( ball ) || CheckCeilingCollision( ball ) ) {
ball.velocity.y *= -1;
} else if ( CheckLeftWallCollision( ball ) ) {
- Score( &rightScore, &ball );
+ rightScore++;
+ volley = 0;
+ delay = 5;
+ printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
+ Reset( &ball );
} else if ( CheckRightWallCollision( ball ) ) {
- Score( &leftScore, &ball );
+ volley = 0;
+ leftScore++;
+ delay = 5;
+ printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
+ Reset( &ball );
}else if ( CheckMoveableCollision( leftPaddle, ball ) || CheckMoveableCollision( ball, rightPaddle ) ) {
- ball.velocity.x *= -1;
+ volley++;
+ int xSign = ( ball.velocity.x < 0 ) ? -1 : 1;
+ int ySign = ( ball.velocity.y < 0 ) ? -1 : 1;
+ int increase = ( volley % 4 == 0 ) ? 1 : 0;
+ ball.velocity.x = -1 * ( xSign * ( abs( ball.velocity.x ) + increase ) );
+ delay += increase << 2;
+ ball.velocity.y = ySign * ( abs( ball.velocity.x ) + increase );
}
while ( SDL_PollEvent( &e ) != 0 ) {
if ( e.type == SDL_QUIT ) {
@@ -117,8 +117,10 @@ int main ( void ) {
}
}
}
+ SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) );
Draw ( rightPaddle, rightPaddleSurface );
Draw ( leftPaddle, leftPaddleSurface );
+ Draw( ball, ballSurface );
SDL_UpdateWindowSurface( gWindow );
Delay( delay );
}
diff --git a/pong.c b/pong.c
index 6ffd452..cf29b5b 100644
--- a/pong.c
+++ b/pong.c
@@ -33,4 +33,5 @@ bool CheckCeilingCollision ( const struct moveable_t m ) {
void Reset ( ball_t * const ball ) {
ball->position = ( point_t ) { SCREEN_WIDTH >> 1, SCREEN_HEIGHT >> 1 };
+ ball->velocity = ( point_t ){ 1, 1 };
}
diff --git a/screen.c b/screen.c
index 7f8e767..b00c97e 100644
--- a/screen.c
+++ b/screen.c
@@ -35,7 +35,7 @@ bool InitScreen ( void ) {
}
gScreenSurface = SDL_GetWindowSurface( gWindow );
- ClearScreen();
+ SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) );
ballSurface = LoadSurface( "ball.bmp" );
leftPaddleSurface = LoadSurface( "paddle.bmp" );
@@ -65,8 +65,3 @@ void Draw ( const struct moveable_t m, SDL_Surface * const surface ) {
printf( "Error with draw! SDL Error: %s\n", SDL_GetError() );
}
}
-
-void ClearScreen( void ) {
- SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) );
- SDL_UpdateWindowSurface( gWindow );
-}
diff --git a/screen.h b/screen.h
index efadac9..e7b4fa4 100644
--- a/screen.h
+++ b/screen.h
@@ -15,6 +15,5 @@ extern SDL_Surface *rightPaddleSurface;
bool InitScreen ( void );
void Close ( void );
void Draw ( const struct moveable_t m, SDL_Surface * const surface );
-void ClearScreen( void );
#endif //SCREEN_H