diff options
| author | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2024-02-20 18:33:42 -0500 |
|---|---|---|
| committer | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2024-02-20 18:33:42 -0500 |
| commit | 9e4797d2fe599acfbf13ed626df46650a88ef891 (patch) | |
| tree | 7b8afaf71d08859c6d8f0c564f08d8f67b343eb0 | |
| parent | 563d44c4a3cabeddc442bfe8377bc2cdfde4a6dd (diff) | |
Changed loading of the sprite
| -rw-r--r-- | cmake-build-debug/.ninja_deps | bin | 34508 -> 52132 bytes | |||
| -rw-r--r-- | cmake-build-debug/.ninja_log | 33 | ||||
| -rw-r--r-- | cmake-build-debug/CMakeFiles/pong.dir/main.c.o | bin | 58160 -> 59192 bytes | |||
| -rw-r--r-- | cmake-build-debug/CMakeFiles/pong.dir/pong.c.o | bin | 36056 -> 36576 bytes | |||
| -rw-r--r-- | cmake-build-debug/CMakeFiles/pong.dir/screen.c.o | bin | 47400 -> 47736 bytes | |||
| -rw-r--r-- | cmake-build-debug/Testing/Temporary/LastTest.log | 4 | ||||
| -rw-r--r-- | cmake-build-debug/ball.bmp | bin | 906 -> 0 bytes | |||
| -rw-r--r-- | cmake-build-debug/paddle.bmp | bin | 3210 -> 0 bytes | |||
| -rwxr-xr-x | cmake-build-debug/pong | bin | 64312 -> 65208 bytes | |||
| -rw-r--r-- | main.c | 22 | ||||
| -rw-r--r-- | pong.c | 10 | ||||
| -rw-r--r-- | pong.h | 15 | ||||
| -rw-r--r-- | screen.c | 12 |
13 files changed, 70 insertions, 26 deletions
diff --git a/cmake-build-debug/.ninja_deps b/cmake-build-debug/.ninja_deps Binary files differindex e470b2a..916dfb7 100644 --- a/cmake-build-debug/.ninja_deps +++ b/cmake-build-debug/.ninja_deps diff --git a/cmake-build-debug/.ninja_log b/cmake-build-debug/.ninja_log index 5112d74..6238597 100644 --- a/cmake-build-debug/.ninja_log +++ b/cmake-build-debug/.ninja_log @@ -41,3 +41,36 @@ 392 412 1708393214014392770 pong 145d847d1d840665 1 396 1708393308953216921 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb 396 416 1708393308976217590 pong 145d847d1d840665 +4 481 1708469430797228636 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +481 507 1708469430828228894 pong 145d847d1d840665 +1 424 1708469469734940079 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +425 445 1708469469758942488 pong 145d847d1d840665 +1 401 1708469666722970055 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +1 405 1708470342230273880 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +405 426 1708470342254274648 pong 145d847d1d840665 +2 412 1708470606042235244 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +412 436 1708470606069236036 pong 145d847d1d840665 +1 393 1708470643664338640 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +393 416 1708470643689339373 pong 145d847d1d840665 +1 396 1708470885679442359 CMakeFiles/pong.dir/pong.c.o 5450e2f51d587a40 +1 397 1708470885680442388 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +1 401 1708470885685442538 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +401 422 1708470885708443226 pong 145d847d1d840665 +1 386 1708471008943154508 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +387 407 1708471008966155204 pong 145d847d1d840665 +1 398 1708471076348194521 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +398 419 1708471076372195247 pong 145d847d1d840665 +1 393 1708471208366179541 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +393 413 1708471208388180192 pong 145d847d1d840665 +1 398 1708471235850992557 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +398 418 1708471235872993207 pong 145d847d1d840665 +1 390 1708471265641873789 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +390 411 1708471265664874470 pong 145d847d1d840665 +1 388 1708471283674407202 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +388 409 1708471283697407882 pong 145d847d1d840665 +1 398 1708471441158065652 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +398 421 1708471441183066391 pong 145d847d1d840665 +1 390 1708471964877432969 CMakeFiles/pong.dir/pong.c.o 5450e2f51d587a40 +1 394 1708471964881433085 CMakeFiles/pong.dir/screen.c.o f08710f44569f228 +1 399 1708471964886433232 CMakeFiles/pong.dir/main.c.o 9a80167090eb49bb +399 420 1708471964909433902 pong 145d847d1d840665 diff --git a/cmake-build-debug/CMakeFiles/pong.dir/main.c.o b/cmake-build-debug/CMakeFiles/pong.dir/main.c.o Binary files differindex 9212738..7246b75 100644 --- a/cmake-build-debug/CMakeFiles/pong.dir/main.c.o +++ b/cmake-build-debug/CMakeFiles/pong.dir/main.c.o diff --git a/cmake-build-debug/CMakeFiles/pong.dir/pong.c.o b/cmake-build-debug/CMakeFiles/pong.dir/pong.c.o Binary files differindex 6eb6fbf..537be6f 100644 --- a/cmake-build-debug/CMakeFiles/pong.dir/pong.c.o +++ b/cmake-build-debug/CMakeFiles/pong.dir/pong.c.o diff --git a/cmake-build-debug/CMakeFiles/pong.dir/screen.c.o b/cmake-build-debug/CMakeFiles/pong.dir/screen.c.o Binary files differindex 698f29c..916c1c1 100644 --- a/cmake-build-debug/CMakeFiles/pong.dir/screen.c.o +++ b/cmake-build-debug/CMakeFiles/pong.dir/screen.c.o diff --git a/cmake-build-debug/Testing/Temporary/LastTest.log b/cmake-build-debug/Testing/Temporary/LastTest.log index e95b3fe..c5b8150 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 20:41 EST +Start testing: Feb 20 18:32 EST ---------------------------------------------------------- -End testing: Feb 19 20:41 EST +End testing: Feb 20 18:32 EST diff --git a/cmake-build-debug/ball.bmp b/cmake-build-debug/ball.bmp Binary files differdeleted file mode 100644 index 021e3c2..0000000 --- a/cmake-build-debug/ball.bmp +++ /dev/null diff --git a/cmake-build-debug/paddle.bmp b/cmake-build-debug/paddle.bmp Binary files differdeleted file mode 100644 index 07c416f..0000000 --- a/cmake-build-debug/paddle.bmp +++ /dev/null diff --git a/cmake-build-debug/pong b/cmake-build-debug/pong Binary files differindex b19b205..44db581 100755 --- a/cmake-build-debug/pong +++ b/cmake-build-debug/pong @@ -1,7 +1,6 @@ #include "pong.h" #include "screen.h" #include <time.h> -#include <math.h> static uint8_t delay = 5; static uint8_t rightScore = 0; @@ -13,10 +12,18 @@ void Delay ( const uint8_t seconds ) { ; } +static inline void Update ( const paddle_t rightPaddle, const paddle_t leftPaddle, const ball_t ball ) { + SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) ); + Draw ( rightPaddle, rightPaddleSurface ); + Draw ( leftPaddle, leftPaddleSurface ); + Draw( ball, ballSurface ); + SDL_UpdateWindowSurface( gWindow ); +} + 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 }; + paddle_t leftPaddle = { { 0, 0 }, { 0, 0 }, PADDLE_WIDTH, PADDLE_HEIGHT }, + rightPaddle = { { SCREEN_WIDTH - 16, 0 }, { 0, 0 }, PADDLE_WIDTH, PADDLE_HEIGHT }; + ball_t ball = { { SCREEN_WIDTH >> 1, SCREEN_HEIGHT >> 1 }, { 1, 1 }, BALL_WIDTH, BALL_HEIGHT }; bool quit = false; SDL_Event e; uint8_t volley = 0; @@ -116,12 +123,9 @@ int main ( void ) { break; } } + Update( rightPaddle, leftPaddle, ball ); } - SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) ); - Draw ( rightPaddle, rightPaddleSurface ); - Draw ( leftPaddle, leftPaddleSurface ); - Draw( ball, ballSurface ); - SDL_UpdateWindowSurface( gWindow ); + Update( rightPaddle, leftPaddle, ball ); Delay( delay ); } Close(); @@ -1,15 +1,15 @@ #include "pong.h" #include "screen.h" -inline void UpdatePosition ( point_t * const position, const point_t velocity ) { +inline void UpdatePosition ( vector_t * const position, const vector_t velocity ) { position->x += velocity.x ; position->y += velocity.y ; } /* CheckMoveableCollision: Implementation of AABB collision for moveable objects */ bool CheckMoveableCollision ( const struct moveable_t m1, const struct moveable_t m2 ) { - const point_t m1p = m1.position; - const point_t m2p = m2.position; + const vector_t m1p = m1.position; + const vector_t m2p = m2.position; const bool checkX = m1p.x + m1.width > m2p.x, checkY = m1p.y + m1.height > m2p.y && m1p.y < m2p.y + m2.height; return checkX && checkY; @@ -32,6 +32,6 @@ 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 }; + ball->position = ( vector_t ) { SCREEN_WIDTH >> 1, SCREEN_HEIGHT >> 1 }; + ball->velocity = ( vector_t ){ 1, 1 }; } @@ -3,15 +3,20 @@ #include <stdint.h> -typedef struct point_t { +constexpr int PADDLE_WIDTH = 16; +constexpr int PADDLE_HEIGHT = 128; +constexpr int BALL_WIDTH = 16; +constexpr int BALL_HEIGHT = 16; + +typedef struct vector_t { int32_t x; int32_t y; -} point_t; +} vector_t; /* paddles have a fixed x position and velocity */ struct moveable_t { - point_t position; - point_t velocity; + vector_t position; + vector_t velocity; uint8_t width; uint8_t height; }; @@ -19,7 +24,7 @@ struct moveable_t { typedef struct moveable_t ball_t; typedef struct moveable_t paddle_t; -void UpdatePosition ( point_t * const position, const point_t velocity ); +void UpdatePosition ( vector_t * const position, const vector_t velocity ); bool CheckMoveableCollision ( const struct moveable_t m1, const struct moveable_t m2 ); bool CheckLeftWallCollision ( const struct moveable_t m ); bool CheckRightWallCollision ( const struct moveable_t m ); @@ -6,7 +6,9 @@ SDL_Surface *ballSurface = NULL; SDL_Surface *leftPaddleSurface = NULL; SDL_Surface *rightPaddleSurface = NULL; -static SDL_Surface *LoadSurface( const char * const path ) { +static const char * const SPRITE_PATH = "sprite.bmp"; + +static SDL_Surface *LoadSurface ( const char * const path ) { SDL_Surface *optimizedSurface = NULL; SDL_Surface * loadedSurface = SDL_LoadBMP( path ); if ( loadedSurface == NULL ) { @@ -37,9 +39,9 @@ bool InitScreen ( void ) { SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) ); - ballSurface = LoadSurface( "ball.bmp" ); - leftPaddleSurface = LoadSurface( "paddle.bmp" ); - rightPaddleSurface = LoadSurface( "paddle.bmp" ); + ballSurface = LoadSurface( SPRITE_PATH ); + leftPaddleSurface = LoadSurface( SPRITE_PATH ); + rightPaddleSurface = LoadSurface( SPRITE_PATH ); return true; } @@ -60,7 +62,7 @@ void Close ( void ) { void Draw ( const struct moveable_t m, SDL_Surface * const surface ) { SDL_Rect drect = { m.position.x, m.position.y, m.width, m.height }; - const int status = SDL_BlitSurface( surface, NULL, gScreenSurface, &drect ); + const int status = SDL_BlitScaled( surface, NULL, gScreenSurface, &drect ); if ( status < 0 ) { printf( "Error with draw! SDL Error: %s\n", SDL_GetError() ); } |
