diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2022-12-16 15:44:52 +0100 |
---|---|---|
committer | Fábio Maciel <6810827+fabiomaciel@users.noreply.github.com> | 2022-12-17 11:40:05 -0300 |
commit | dd5d9a63287896269c01da88db9f50506f06c292 (patch) | |
tree | 272d01f16a31f314ddb5b3240d2a2c49664cc529 | |
parent | 71631138740ffef02c587b49835feb56a1ff5edc (diff) |
styling: Reformat the entire code base based on clang-format
The following command has been executed:
clang-format -i src/*.c src/*.h test/*.c
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
-rw-r--r-- | src/controller.c | 83 | ||||
-rw-r--r-- | src/controller.h | 49 | ||||
-rw-r--r-- | src/main.c | 80 | ||||
-rw-r--r-- | src/player.c | 97 | ||||
-rw-r--r-- | src/player.h | 75 | ||||
-rw-r--r-- | src/screen.c | 89 | ||||
-rw-r--r-- | src/screen.h | 71 | ||||
-rw-r--r-- | test/controller-test.c | 138 |
8 files changed, 360 insertions, 322 deletions
diff --git a/src/controller.c b/src/controller.c index 4e488e9..6135060 100644 --- a/src/controller.c +++ b/src/controller.c @@ -5,8 +5,8 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation @@ -18,79 +18,78 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ -#include <SDL.h> #include "controller.h" - +#include <SDL.h> void -controller_update(controller_t *self, - SDL_Event *event) +controller_update(controller_t* self, SDL_Event* event) { if (event->type == SDL_KEYDOWN) { switch (event->key.keysym.sym) { - case SDLK_UP: - self->pressed_buttons |= BTN_UP; - break; - case SDLK_DOWN: - self->pressed_buttons |= BTN_DOWN; - break; - case SDLK_LEFT: - self->pressed_buttons |= BTN_LEFT; - break; - case SDLK_RIGHT: - self->pressed_buttons |= BTN_RIGHT; - break; + case SDLK_UP: + self->pressed_buttons |= BTN_UP; + break; + case SDLK_DOWN: + self->pressed_buttons |= BTN_DOWN; + break; + case SDLK_LEFT: + self->pressed_buttons |= BTN_LEFT; + break; + case SDLK_RIGHT: + self->pressed_buttons |= BTN_RIGHT; + break; } } if (event->type == SDL_KEYUP) { switch (event->key.keysym.sym) { - case SDLK_UP: - self->pressed_buttons ^= BTN_UP; - break; - case SDLK_DOWN: - self->pressed_buttons ^= BTN_DOWN; - break; - case SDLK_LEFT: - self->pressed_buttons ^= BTN_LEFT; - break; - case SDLK_RIGHT: - self->pressed_buttons ^= BTN_RIGHT; - break; + case SDLK_UP: + self->pressed_buttons ^= BTN_UP; + break; + case SDLK_DOWN: + self->pressed_buttons ^= BTN_DOWN; + break; + case SDLK_LEFT: + self->pressed_buttons ^= BTN_LEFT; + break; + case SDLK_RIGHT: + self->pressed_buttons ^= BTN_RIGHT; + break; } } } bool -controller_is_up_pressed(controller_t *self) +controller_is_up_pressed(controller_t* self) { return self->pressed_buttons & BTN_UP; } bool -controller_is_down_pressed(controller_t *self) +controller_is_down_pressed(controller_t* self) { return self->pressed_buttons & BTN_DOWN; } bool -controller_is_right_pressed(controller_t *self) +controller_is_right_pressed(controller_t* self) { return self->pressed_buttons & BTN_RIGHT; } bool -controller_is_left_pressed(controller_t *self) +controller_is_left_pressed(controller_t* self) { return self->pressed_buttons & BTN_LEFT; } diff --git a/src/controller.h b/src/controller.h index ecc0389..59fcc26 100644 --- a/src/controller.h +++ b/src/controller.h @@ -5,8 +5,8 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation @@ -18,14 +18,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ #ifndef CONTROLLER_H @@ -35,23 +36,29 @@ #include <stdbool.h> #include <stdint.h> -enum btn_t { - BTN_UP = 1 << 0, - BTN_DOWN = 1 << 1, +enum btn_t +{ + BTN_UP = 1 << 0, + BTN_DOWN = 1 << 1, BTN_RIGHT = 1 << 2, - BTN_LEFT = 1 << 3 + BTN_LEFT = 1 << 3 }; -typedef struct controller_t { +typedef struct controller_t +{ uint8_t pressed_buttons; } controller_t; -void controller_update(controller_t *self, - SDL_Event *event); +void +controller_update(controller_t* self, SDL_Event* event); -bool controller_is_up_pressed(controller_t *self); -bool controller_is_down_pressed(controller_t *self); -bool controller_is_right_pressed(controller_t *self); -bool controller_is_left_pressed(controller_t *self); +bool +controller_is_up_pressed(controller_t* self); +bool +controller_is_down_pressed(controller_t* self); +bool +controller_is_right_pressed(controller_t* self); +bool +controller_is_left_pressed(controller_t* self); #endif /* CONTROLLER_H */ @@ -1,72 +1,69 @@ /* -* Copyright (c) 2021, Johnny Richard -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* 3. Neither the name of the copyright holder nor the names of its -* contributors may be used to endorse or promote products derived from -* this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * Copyright (c) 2021, Johnny Richard + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <SDL.h> +#include <math.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <stdbool.h> -#include <math.h> -#include <SDL.h> #include "controller.h" #include "player.h" #include "screen.h" int -main (int argc, - char *args[]) +main(int argc, char* args[]) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf( - stderr, - "SDL could not be initialized! SDL_Error: %s\n", - SDL_GetError() - ); + stderr, "SDL could not be initialized! SDL_Error: %s\n", SDL_GetError()); return EXIT_FAILURE; } screen_t screen; - if(screen_init(&screen) == false){ + if (screen_init(&screen) == false) { return EXIT_FAILURE; } SDL_Rect bg_rect = { .x = 0, .y = 0, .w = screen.width, .h = screen.height }; bool quit = false; - controller_t ctrl = {0}; + controller_t ctrl = { 0 }; player_t player; player_init(&player); - SDL_Renderer *renderer = screen.renderer; + SDL_Renderer* renderer = screen.renderer; while (!quit) { screen_reset(&screen); @@ -90,7 +87,8 @@ main (int argc, uint64_t end = SDL_GetPerformanceCounter(); - float elapsedMS = (end - start) / (float)SDL_GetPerformanceFrequency() * 1000.0f; + float elapsedMS = + (end - start) / (float)SDL_GetPerformanceFrequency() * 1000.0f; SDL_Delay(floor(16.666f - elapsedMS)); } diff --git a/src/player.c b/src/player.c index 000eb76..bebf5a1 100644 --- a/src/player.c +++ b/src/player.c @@ -1,40 +1,41 @@ /* -* Copyright (c) 2021, Johnny Richard -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* 3. Neither the name of the copyright holder nor the names of its -* contributors may be used to endorse or promote products derived from -* this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * Copyright (c) 2021, Johnny Richard + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ #include "player.h" -#include <assert.h> #include <SDL.h> +#include <assert.h> void -player_init(player_t *player) +player_init(player_t* player) { assert(player && "player is required"); player->x = 0; @@ -44,10 +45,11 @@ player_init(player_t *player) player->speed = 8; } -void player_update(player_t *player, - controller_t *ctrl, - int screen_width, - int screen_height) +void +player_update(player_t* player, + controller_t* ctrl, + int screen_width, + int screen_height) { if (controller_is_up_pressed(ctrl) && player->y > 0) { player->y -= player->speed; @@ -56,34 +58,29 @@ void player_update(player_t *player, if (controller_is_down_pressed(ctrl) && player->y < screen_height) { player->y += player->speed; - player->y = player->y + player->height > screen_height - ? screen_height - player->height - : player->y; + player->y = player->y + player->height > screen_height + ? screen_height - player->height + : player->y; } - if (controller_is_left_pressed(ctrl) && player->x > 0) { + if (controller_is_left_pressed(ctrl) && player->x > 0) { player->x -= player->speed; - player->x = player->x < 0 - ? 0 - : player->x; + player->x = player->x < 0 ? 0 : player->x; } if (controller_is_right_pressed(ctrl) && player->x < screen_width) { player->x += player->speed; - player->x = player->x + player->width > screen_width - ? screen_width - player->width - : player->x; + player->x = player->x + player->width > screen_width + ? screen_width - player->width + : player->x; } } void -player_draw(player_t *player, SDL_Renderer *renderer) +player_draw(player_t* player, SDL_Renderer* renderer) { - SDL_Rect rect = { - .x = player->x, - .y = player->y, - .w = player->width, - .h = player->height + SDL_Rect rect = { + .x = player->x, .y = player->y, .w = player->width, .h = player->height }; SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 1); diff --git a/src/player.h b/src/player.h index 320c1bb..b7ee6da 100644 --- a/src/player.h +++ b/src/player.h @@ -1,32 +1,33 @@ /* -* Copyright (c) 2021, Johnny Richard -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* 3. Neither the name of the copyright holder nor the names of its -* contributors may be used to endorse or promote products derived from -* this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * Copyright (c) 2021, Johnny Richard + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ #ifndef PLAYER_H #define PLAYER_H @@ -34,7 +35,8 @@ #include <stdint.h> -typedef struct player { +typedef struct player +{ int16_t x; int16_t y; uint16_t width; @@ -42,14 +44,17 @@ typedef struct player { uint16_t speed; } player_t; -void player_init(player_t *player); +void +player_init(player_t* player); // FIXME: Find out a better way to provide screen w and h properties -void player_update(player_t *player, - controller_t *ctrl, - int screen_width, - int screen_height); +void +player_update(player_t* player, + controller_t* ctrl, + int screen_width, + int screen_height); -void player_draw(player_t *player, SDL_Renderer *renderer); +void +player_draw(player_t* player, SDL_Renderer* renderer); #endif /* PLAYER_H */ diff --git a/src/screen.c b/src/screen.c index 52dec55..18a2768 100644 --- a/src/screen.c +++ b/src/screen.c @@ -1,40 +1,41 @@ /* -* Copyright (c) 2021, Fabio Maciel -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* 3. Neither the name of the copyright holder nor the names of its -* contributors may be used to endorse or promote products derived from -* this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * Copyright (c) 2021, Fabio Maciel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ #include "screen.h" -#include <stdint.h> -#include <assert.h> #include <SDL.h> +#include <assert.h> +#include <stdint.h> -const int SCREEN_WIDTH = 640; +const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; bool @@ -45,26 +46,28 @@ screen_init(screen_t* screen) screen->width = SCREEN_WIDTH; screen->height = SCREEN_HEIGHT; - screen->window = SDL_CreateWindow( - "Blast Attack", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - screen->width, screen->height, - SDL_WINDOW_RESIZABLE - ); + screen->window = SDL_CreateWindow("Blast Attack", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + screen->width, + screen->height, + SDL_WINDOW_RESIZABLE); if (screen->window == NULL) { - fprintf(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); + fprintf( + stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); return false; } - screen->renderer = SDL_CreateRenderer(screen->window, -1, SDL_RENDERER_ACCELERATED); + screen->renderer = + SDL_CreateRenderer(screen->window, -1, SDL_RENDERER_ACCELERATED); if (screen->renderer == NULL) { fprintf(stderr, "Could not create renderer: %s\n", SDL_GetError()); return false; } - if (SDL_RenderSetLogicalSize(screen->renderer, screen->width, screen->height) < 0) { + if (SDL_RenderSetLogicalSize( + screen->renderer, screen->width, screen->height) < 0) { fprintf(stderr, "Could not set logical size: %s\n", SDL_GetError()); return false; } @@ -73,14 +76,14 @@ screen_init(screen_t* screen) } void -screen_reset(screen_t *screen) +screen_reset(screen_t* screen) { SDL_SetRenderDrawColor(screen->renderer, 0, 0, 0, 1); SDL_RenderClear(screen->renderer); } void -screen_destroy(screen_t *screen) +screen_destroy(screen_t* screen) { SDL_DestroyRenderer(screen->renderer); SDL_DestroyWindow(screen->window); diff --git a/src/screen.h b/src/screen.h index d9bb9ee..56544b6 100644 --- a/src/screen.h +++ b/src/screen.h @@ -1,51 +1,56 @@ /* -* Copyright (c) 2021, Fabio Maciel -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this -* list of conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, -* this list of conditions and the following disclaimer in the documentation -* and/or other materials provided with the distribution. -* -* 3. Neither the name of the copyright holder nor the names of its -* contributors may be used to endorse or promote products derived from -* this software without specific prior written permission. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * Copyright (c) 2021, Fabio Maciel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ #ifndef SCREEN_H #define SCREEN_H -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> #include <SDL.h> -typedef struct screen { +typedef struct screen +{ uint16_t width; uint16_t height; SDL_Renderer* renderer; SDL_Window* window; } screen_t; -bool screen_init(screen_t *screen); +bool +screen_init(screen_t* screen); -void screen_reset(screen_t *screen); +void +screen_reset(screen_t* screen); -void screen_destroy(screen_t *screen); +void +screen_destroy(screen_t* screen); #endif /* SCREEN_H */ diff --git a/test/controller-test.c b/test/controller-test.c index c271756..dfaa203 100644 --- a/test/controller-test.c +++ b/test/controller-test.c @@ -5,8 +5,8 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation @@ -18,14 +18,15 @@ * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ #define MUNIT_ENABLE_ASSERT_ALIASES @@ -33,87 +34,110 @@ #include <munit/munit.h> static SDL_Event -an_event(SDL_EventType type, SDL_KeyCode keycode) { - SDL_Event event; - event.type = type; - event.key.keysym.sym = keycode; - return event; +an_event(SDL_EventType type, SDL_KeyCode keycode) +{ + SDL_Event event; + event.type = type; + event.key.keysym.sym = keycode; + return event; } static MunitResult press_and_release_up(const MunitParameter params[], void* fixture) { - controller_t ctrl = {0}; + controller_t ctrl = { 0 }; - SDL_Event event = an_event(SDL_KEYDOWN, SDLK_UP); - controller_update(&ctrl, &event); - assert_true(controller_is_up_pressed(&ctrl)); + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_UP); + controller_update(&ctrl, &event); + assert_true(controller_is_up_pressed(&ctrl)); - event = an_event(SDL_KEYUP, SDLK_UP); - controller_update(&ctrl, &event); - assert_false(controller_is_up_pressed(&ctrl)); - return MUNIT_OK; + event = an_event(SDL_KEYUP, SDLK_UP); + controller_update(&ctrl, &event); + assert_false(controller_is_up_pressed(&ctrl)); + return MUNIT_OK; } static MunitResult press_and_release_down(const MunitParameter params[], void* fixture) { - controller_t ctrl = {0}; + controller_t ctrl = { 0 }; - SDL_Event event = an_event(SDL_KEYDOWN, SDLK_DOWN); - controller_update(&ctrl, &event); - assert_true(controller_is_down_pressed(&ctrl)); + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_DOWN); + controller_update(&ctrl, &event); + assert_true(controller_is_down_pressed(&ctrl)); - event = an_event(SDL_KEYUP, SDLK_DOWN); - controller_update(&ctrl, &event); - assert_false(controller_is_down_pressed(&ctrl)); - return MUNIT_OK; + event = an_event(SDL_KEYUP, SDLK_DOWN); + controller_update(&ctrl, &event); + assert_false(controller_is_down_pressed(&ctrl)); + return MUNIT_OK; } static MunitResult press_and_release_right(const MunitParameter params[], void* fixture) { - controller_t ctrl = {0}; + controller_t ctrl = { 0 }; - SDL_Event event = an_event(SDL_KEYDOWN, SDLK_RIGHT); - controller_update(&ctrl, &event); - assert_true(controller_is_right_pressed(&ctrl)); + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_RIGHT); + controller_update(&ctrl, &event); + assert_true(controller_is_right_pressed(&ctrl)); - event = an_event(SDL_KEYUP, SDLK_RIGHT); - controller_update(&ctrl, &event); - assert_false(controller_is_right_pressed(&ctrl)); - return MUNIT_OK; + event = an_event(SDL_KEYUP, SDLK_RIGHT); + controller_update(&ctrl, &event); + assert_false(controller_is_right_pressed(&ctrl)); + return MUNIT_OK; } static MunitResult press_and_release_left(const MunitParameter params[], void* fixture) { - controller_t ctrl = {0}; + controller_t ctrl = { 0 }; - SDL_Event event = an_event(SDL_KEYDOWN, SDLK_LEFT); - controller_update(&ctrl, &event); - assert_true(controller_is_left_pressed(&ctrl)); + SDL_Event event = an_event(SDL_KEYDOWN, SDLK_LEFT); + controller_update(&ctrl, &event); + assert_true(controller_is_left_pressed(&ctrl)); - event = an_event(SDL_KEYUP, SDLK_LEFT); - controller_update(&ctrl, &event); - assert_false(controller_is_left_pressed(&ctrl)); - return MUNIT_OK; + event = an_event(SDL_KEYUP, SDLK_LEFT); + controller_update(&ctrl, &event); + assert_false(controller_is_left_pressed(&ctrl)); + return MUNIT_OK; } MunitTest tests[] = { - { "/press_and_release_up", press_and_release_up, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/press_and_release_down", press_and_release_down, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/press_and_release_right", press_and_release_right, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { "/press_and_release_left", press_and_release_left, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, - { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } + { "/press_and_release_up", + press_and_release_up, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/press_and_release_down", + press_and_release_down, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/press_and_release_right", + press_and_release_right, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { "/press_and_release_left", + press_and_release_left, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; -static const MunitSuite suite = { - "/controller", tests, NULL, 1, MUNIT_TEST_OPTION_NONE -}; +static const MunitSuite suite = { "/controller", + tests, + NULL, + 1, + MUNIT_TEST_OPTION_NONE }; int -main(int argc, char *argv[]) +main(int argc, char* argv[]) { - return munit_suite_main(&suite, NULL, argc, argv); + return munit_suite_main(&suite, NULL, argc, argv); } |