breakout

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 6498358bb85900f5a443561d690b062cc6f9e307
parent e8a1ef116dc08bce468d05b076e35ccbe51006e6
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Sun, 25 Aug 2019 18:42:23 -0400

Added ball sprite. Build segfaults (bad pointer alloc?)

Diffstat:
MMakefile | 1+
Aassets/gfx/ball.aseprite | 0
Aassets/gfx/ball.bmp | 0
Msrc/main.c | 95++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
4 files changed, 91 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,6 +2,7 @@ CC = gcc CFLAGS = -g -Wall -pedantic `sdl2-config --cflags --libs` default: dirs ./src/main.c + cp -r assets ./bin/assets $(CC) $(CFLAGS) ./src/main.c -o ./bin/breakout diff --git a/assets/gfx/ball.aseprite b/assets/gfx/ball.aseprite Binary files differ. diff --git a/assets/gfx/ball.bmp b/assets/gfx/ball.bmp Binary files differ. diff --git a/src/main.c b/src/main.c @@ -3,9 +3,23 @@ #define WINDOW_W 320 #define WINDOW_H 240 +int CreateSprite(void); +void MoveSprite(void); +void Quit(int); + SDL_Window *window; SDL_Renderer *renderer; +char* data_path; + +struct sprite { + SDL_Texture *tex; + SDL_Rect position; + SDL_Point velocity; +}; + +struct sprite ball; + int main(int argc, char *argv[]) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { @@ -14,6 +28,10 @@ int main(int argc, char *argv[]) } SDL_Log("SDL initialized successfully\n"); + + data_path = SDL_GetBasePath(); + SDL_Log("Data path: %s\n", data_path); + SDL_version compiled, linked; SDL_VERSION(&compiled); SDL_GetVersion(&linked); @@ -39,13 +57,80 @@ int main(int argc, char *argv[]) return 1; } - SDL_SetRenderDrawColor(renderer, 128, 255, 192, 255); - SDL_RenderClear(renderer); - SDL_RenderPresent(renderer); - SDL_Delay(1000); + int temp; + temp = CreateSprite(); + if(temp != 0) { + Quit(temp); + } - SDL_DestroyWindow(window); + SDL_Event event; + int done = 1; + + while(!done) { + while(SDL_PollEvent(&event)) + if(event.type == SDL_QUIT) + done = 0; + MoveSprite(); + SDL_Delay(100); + } + Quit(0); + return 0; +} + +void Quit(int i) +{ + SDL_DestroyWindow(window); + SDL_free(data_path); SDL_Quit(); + exit(i); +} + +int CreateSprite() +{ + SDL_Surface *temp; + char* path = strcat(data_path, "assets/gfx/ball.bmp"); + temp = SDL_LoadBMP(path); + + if(temp == NULL) + { + SDL_Log("Failed to create surface from %s: %s\n", path, SDL_GetError()); + return 1; + } + + ball.tex = SDL_CreateTextureFromSurface(renderer, temp); + ball.position.x = 0; + ball.position.y = 0; + ball.position.w = temp->w; + ball.position.h = temp->h; + ball.velocity.x = 1; + ball.velocity.y = 0; + + SDL_FreeSurface(temp); + return 0; } + +void MoveSprite() +{ + /* Clear the renderer for drawing the sprite */ + SDL_SetRenderDrawColor(renderer, 128, 255, 192, 255); + SDL_RenderClear(renderer); + + /* move the sprite a bit */ + ball.position.x += ball.velocity.x; + if(ball.position.x < 0 || ball.position.x > WINDOW_W - ball.position.w) { + ball.velocity.x = -ball.velocity.x; + ball.position.x += ball.velocity.x; + } + + SDL_RenderCopy( + renderer, + ball.tex, + NULL, + &ball.position + ); + + + SDL_RenderPresent(renderer); +}