breakout

A simple breakout clone created with C and SDL2
git clone git://git.hellocld.com/breakout
Log | Files | Refs

commit f7efdf721725f9a30bd27152caa47205c94ea333
parent e16ec42ccca2937f04ab89eaa0bed07d497008b6
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Sun,  6 Oct 2019 16:15:52 -0400

Modified sprite so floating point movement is used

Allows for slower movement on locked framerate thing. Might not stick
with this.

Diffstat:
Msrc/main.c | 49++++++++++++++++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -8,18 +8,28 @@ int Initialize(void); int CreateSprite(void); void MoveSprite(void); void Shutdown(int); +void FrameDelay(int framerate); SDL_Window *window; SDL_Renderer *renderer; char* data_path; +int dt, lt; + +struct CLD_Point { + float x; + float y; +}; + struct sprite { SDL_Texture *tex; - SDL_Rect position; - SDL_Point velocity; + struct SDL_Rect rect; + struct CLD_Point position; + struct CLD_Point velocity; }; + struct sprite ball; int main(int argc, char *argv[]) @@ -37,12 +47,12 @@ int main(int argc, char *argv[]) SDL_Event event; int done = 0; - while(!done) { + while (!done) { while(SDL_PollEvent(&event)) if(event.type == SDL_QUIT) done = 1; MoveSprite(); - SDL_Delay(10); + FrameDelay(60); } Shutdown(0); @@ -80,10 +90,10 @@ int CreateSprite() } ball.position.x = 10; ball.position.y = 13; - ball.position.w = temp->w; - ball.position.h = temp->h; - ball.velocity.x = 1; - ball.velocity.y = 1; + ball.rect.w = temp->w; + ball.rect.h = temp->h; + ball.velocity.x = 0.5; + ball.velocity.y = 0.5; SDL_FreeSurface(temp); SDL_Log("Successfully created sprite!\n"); @@ -99,22 +109,24 @@ void MoveSprite() /* move the sprite a bit */ ball.position.x += ball.velocity.x; - if(ball.position.x < 0 || ball.position.x > WINDOW_W - ball.position.w) { + if(ball.position.x < 0 || ball.position.x > WINDOW_W - ball.rect.w) { ball.velocity.x = -ball.velocity.x; ball.position.x += ball.velocity.x; } ball.position.y += ball.velocity.y; - if(ball.position.y < 0 || ball.position.y > WINDOW_H - ball.position.h) { + if(ball.position.y < 0 || ball.position.y > WINDOW_H - ball.rect.h) { ball.velocity.y = -ball.velocity.y; ball.position.y += ball.velocity.y; } + ball.rect.x = (int)ball.position.x; + ball.rect.y = (int)ball.position.y; + SDL_RenderCopy( renderer, ball.tex, NULL, - &ball.position - ); + &ball.rect); SDL_RenderPresent(renderer); @@ -153,9 +165,9 @@ int Initialize() "BREAKOUT", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 0, - 0, - SDL_WINDOW_RESIZABLE); + WINDOW_W * 4, + WINDOW_H * 4, + 0); if(window == NULL) { SDL_Log("Failed to create window: %s\n", SDL_GetError()); @@ -173,3 +185,10 @@ int Initialize() /* Success! */ return 0; } + +Uint32 lastFrame; +void FrameDelay(int framerate) { + Uint32 t_now = SDL_GetTicks(); + SDL_Delay((1000 - (t_now - lastFrame))/framerate); + lastFrame = t_now; +}