From 22569a224177161feeb4da4276319ebd600aac3e Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sat, 1 May 2021 02:23:11 +0200 Subject: [PATCH] clang-format config added and applied --- .clang-format | 9 ++++ include/conf.h | 24 +++++------ include/input.h | 8 ++-- include/level.h | 6 +-- include/player.h | 34 +++++++-------- include/vec2.h | 4 +- src/input.c | 35 ++++++++++------ src/level.c | 34 +++++++++------ src/main.c | 33 +++++++++------ src/player.c | 105 ++++++++++++++++++++++++++++------------------- src/tiles.c | 36 +++++++++++----- 11 files changed, 201 insertions(+), 127 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..57fd90a --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: LLVM +IndentWidth: 8 +UseTab: AlignWithSpaces +BreakBeforeBraces: Linux +AllowShortIfStatementsOnASingleLine: false +IndentCaseLabels: false +ColumnLimit: 80 +AlignConsecutiveMacros: true +AlwaysBreakAfterReturnType: TopLevelDefinitions diff --git a/include/conf.h b/include/conf.h index ae88f1c..575e603 100644 --- a/include/conf.h +++ b/include/conf.h @@ -6,16 +6,16 @@ * included in all copies and substantial portions of the software. */ #pragma once -#define TARGET_UPS 60 -#define TARGET_FPS 30 -#define TILE_SIZE 16 -#define LEVEL_WIDTH 16 -#define LEVEL_HEIGHT 16 -#define LEVEL_SIZE (LEVEL_WIDTH * LEVEL_HEIGHT) -#define LEVEL_WIDTH_PX (LEVEL_WIDTH * TILE_SIZE) -#define LEVEL_HEIGHT_PX (LEVEL_HEIGHT * TILE_SIZE) -#define DRAW_OFFSET_X 70 -#define DRAW_OFFSET_Y_MIN -32 -#define DRAW_OFFSET_Y_MAX 0 -#define DRAW_OFFSET_Y_STEP 8 +#define TARGET_UPS 60 +#define TARGET_FPS 30 +#define TILE_SIZE 16 +#define LEVEL_WIDTH 16 +#define LEVEL_HEIGHT 16 +#define LEVEL_SIZE (LEVEL_WIDTH * LEVEL_HEIGHT) +#define LEVEL_WIDTH_PX (LEVEL_WIDTH * TILE_SIZE) +#define LEVEL_HEIGHT_PX (LEVEL_HEIGHT * TILE_SIZE) +#define DRAW_OFFSET_X 70 +#define DRAW_OFFSET_Y_MIN -32 +#define DRAW_OFFSET_Y_MAX 0 +#define DRAW_OFFSET_Y_STEP 8 #define DRAW_OFFSET_Y_DEFAULT -16 diff --git a/include/input.h b/include/input.h index cd48473..86517ee 100644 --- a/include/input.h +++ b/include/input.h @@ -5,8 +5,8 @@ * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ #pragma once -#include #include "lazyint.h" +#include #define KEYS_COUNT 8 enum { @@ -20,10 +20,10 @@ enum { K_EXIT }; -#define S_PRESSED 0 -#define S_DOWN 1 +#define S_PRESSED 0 +#define S_DOWN 1 #define S_RELEASED 2 -#define S_UP 3 +#define S_UP 3 typedef struct Input { u8 keys[KEYS_COUNT]; diff --git a/include/level.h b/include/level.h index 023b55c..32c4595 100644 --- a/include/level.h +++ b/include/level.h @@ -6,11 +6,11 @@ * included in all copies and substantial portions of the software. */ #pragma once #include "conf.h" -#include "vec2.h" -#include "tiles.h" #include "lazyint.h" +#include "tiles.h" +#include "vec2.h" -typedef struct Level{ +typedef struct Level { tile_t content[LEVEL_WIDTH * LEVEL_HEIGHT]; Vec2 start_pos; u8 keys; diff --git a/include/player.h b/include/player.h index f67b270..367a122 100644 --- a/include/player.h +++ b/include/player.h @@ -5,28 +5,28 @@ * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ #pragma once -#include +#include "input.h" #include "lazyint.h" #include "vec2.h" -#include "input.h" +#include -#define PLAYER_WIDTH 12 -#define PLAYER_HEIGHT 12 -#define GRAVITY 0.2 -#define JUMP_SPD -5.8 -#define JUMP_BUFFER 6 -#define COYOT 6 -#define FALL_RESISTANCE 0.04 -#define FASTFALL_RESISTANCE 0.01 +#define PLAYER_WIDTH 12 +#define PLAYER_HEIGHT 12 +#define GRAVITY 0.2 +#define JUMP_SPD -5.8 +#define JUMP_BUFFER 6 +#define COYOT 6 +#define FALL_RESISTANCE 0.04 +#define FASTFALL_RESISTANCE 0.01 #define MAX_HORIZONTAL_SPEED 2.0 -#define GROUND_ACCELERATION 0.4 -#define AIR_ACCELERATION 1.0 -#define GROUND_FRICTION (GROUND_ACCELERATION / MAX_HORIZONTAL_SPEED) -#define AIR_FRICTION (AIR_ACCELERATION / MAX_HORIZONTAL_SPEED) -#define KNOCKBACK_Y (-(JUMP_SPD) + 2.0) -#define KNOCKBACK_X MAX_HORIZONTAL_SPEED +#define GROUND_ACCELERATION 0.4 +#define AIR_ACCELERATION 1.0 +#define GROUND_FRICTION (GROUND_ACCELERATION / MAX_HORIZONTAL_SPEED) +#define AIR_FRICTION (AIR_ACCELERATION / MAX_HORIZONTAL_SPEED) +#define KNOCKBACK_Y (-(JUMP_SPD) + 2.0) +#define KNOCKBACK_X MAX_HORIZONTAL_SPEED -typedef struct Player{ +typedef struct Player { Vec2 pos; f32 spd_x; f32 spd_y; diff --git a/include/vec2.h b/include/vec2.h index 35074d4..16d77ae 100644 --- a/include/vec2.h +++ b/include/vec2.h @@ -5,11 +5,11 @@ * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ #pragma once -#include #include "lazyint.h" +#include typedef i16 vec2_int_t; -typedef struct Vec2{ +typedef struct Vec2 { vec2_int_t x; vec2_int_t y; } Vec2; diff --git a/src/input.c b/src/input.c index 993240a..d6a8218 100644 --- a/src/input.c +++ b/src/input.c @@ -4,11 +4,13 @@ * [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg), * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ -#include -#include "lazyint.h" #include "input.h" +#include "lazyint.h" +#include -void input_update(Input *input) { +void +input_update(Input *input) +{ /* Read full input stream. */ clearevents(); /* For each key, update state. */ @@ -23,8 +25,7 @@ void input_update(Input *input) { *state = S_PRESSED; else *state = S_DOWN; - } - else { + } else { if (*state == S_PRESSED || *state == S_DOWN) *state = S_RELEASED; else @@ -33,7 +34,9 @@ void input_update(Input *input) { } } -void input_init(Input *input) { +void +input_init(Input *input) +{ /* initialize all values to S_UP, avoid random bugs */ input->keys[K_LEFT] = KEY_LEFT; input->keys[K_RIGHT] = KEY_RIGHT; @@ -47,20 +50,28 @@ void input_init(Input *input) { input->states[i] = S_UP; } -bool input_is_pressed(Input input, u8 key) { +bool +input_is_pressed(Input input, u8 key) +{ return input.states[key] == S_PRESSED; } -bool input_is_down(Input input, u8 key) { +bool +input_is_down(Input input, u8 key) +{ return ((input.states[key] == S_DOWN) || - (input.states[key] == S_PRESSED)); + (input.states[key] == S_PRESSED)); } -bool input_is_released(Input input, u8 key) { +bool +input_is_released(Input input, u8 key) +{ return input.states[key] == S_RELEASED; } -bool input_is_up(Input input, u8 key) { +bool +input_is_up(Input input, u8 key) +{ return ((input.states[key] == S_UP) || - (input.states[key] == S_RELEASED)); + (input.states[key] == S_RELEASED)); } diff --git a/src/level.c b/src/level.c index b4e826a..706803a 100644 --- a/src/level.c +++ b/src/level.c @@ -4,16 +4,18 @@ * [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg), * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ +#include "level.h" +#include "conf.h" +#include "lazyint.h" +#include "player.h" +#include "tiles.h" +#include "vec2.h" #include #include -#include "lazyint.h" -#include "level.h" -#include "tiles.h" -#include "conf.h" -#include "player.h" -#include "vec2.h" -void level_draw(Level level, i16 y_offset) { +void +level_draw(Level level, i16 y_offset) +{ /* Pixel position (where we draw). */ i32 x = DRAW_OFFSET_X; i16 y = y_offset; @@ -22,9 +24,11 @@ void level_draw(Level level, i16 y_offset) { while (cx < LEVEL_WIDTH) { u8 cy = 0; while (cy < LEVEL_HEIGHT) { - const tile_t tile = level.content[cy * LEVEL_WIDTH + cx]; + const tile_t tile = + level.content[cy * LEVEL_WIDTH + cx]; const u32 color = tile_color(tile); - drect(x, y, x + TILE_SIZE - 1, y + TILE_SIZE - 1, color); + drect(x, y, x + TILE_SIZE - 1, y + TILE_SIZE - 1, + color); y += TILE_SIZE; cy += 1; } @@ -34,7 +38,9 @@ void level_draw(Level level, i16 y_offset) { } } -void level_load(Level *level, Player *player, u8 id) { +void +level_load(Level *level, Player *player, u8 id) +{ extern Level levels[LEVEL_SIZE]; memcpy(level->content, levels[id].content, LEVEL_SIZE); level->start_pos = levels[id].start_pos; @@ -44,7 +50,9 @@ void level_load(Level *level, Player *player, u8 id) { player->keys_left = levels[id].keys; } -tile_t level_get_tile_at_px(Level level, Vec2 pos) { +tile_t +level_get_tile_at_px(Level level, Vec2 pos) +{ /* Out of bounds check. */ if (pos.x < 0 || pos.y < 0) return OUT_OF_BOUNDS; @@ -58,7 +66,9 @@ tile_t level_get_tile_at_px(Level level, Vec2 pos) { return level.content[x + y * LEVEL_WIDTH]; } -void level_set_tile_at_px(Level *level, Vec2 pos, tile_t tile) { +void +level_set_tile_at_px(Level *level, Vec2 pos, tile_t tile) +{ /* Convert to unsigned integers. */ u8 x = pos.x / TILE_SIZE; u8 y = pos.y / TILE_SIZE; diff --git a/src/main.c b/src/main.c index eedf8e5..d3e19ce 100644 --- a/src/main.c +++ b/src/main.c @@ -4,23 +4,27 @@ * [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg), * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ -#include -#include -#include -#include -#include "lazyint.h" #include "conf.h" #include "input.h" +#include "lazyint.h" #include "level.h" #include "player.h" +#include +#include +#include +#include -int callback(volatile void *arg) { +int +callback(volatile void *arg) +{ volatile bool *has_ticked = arg; *has_ticked = true; return 0; } -int main(void) { +int +main(void) +{ /* Initialize player. */ Player player; player_init(&player); @@ -36,7 +40,8 @@ int main(void) { /* UPS control. */ volatile bool has_ticked = true; - int timer = timer_setup(TIMER_ANY, 1000000/TARGET_UPS, callback, &has_ticked); + int timer = + timer_setup(TIMER_ANY, 1000000 / TARGET_UPS, callback, &has_ticked); timer_start(timer); /* Core loop. */ @@ -44,15 +49,18 @@ int main(void) { /* Repeat step event so the UPS is constant. */ for (u8 i = 0; i < TARGET_UPS / TARGET_FPS; i += 1) { /* UPS control. */ - while (!has_ticked) sleep(); + while (!has_ticked) + sleep(); has_ticked = false; /* Update. */ input_update(&input); player_update(&player, &level, input, &level_id); /* Y offset. */ - if (input_is_pressed(input, K_CAM_UP) && y_offset < DRAW_OFFSET_Y_MAX) + if (input_is_pressed(input, K_CAM_UP) && + y_offset < DRAW_OFFSET_Y_MAX) y_offset += DRAW_OFFSET_Y_STEP; - if (input_is_pressed(input, K_CAM_DOWN) && y_offset > DRAW_OFFSET_Y_MIN) + if (input_is_pressed(input, K_CAM_DOWN) && + y_offset > DRAW_OFFSET_Y_MIN) y_offset -= DRAW_OFFSET_Y_STEP; } /* Draw. */ @@ -61,7 +69,8 @@ int main(void) { player_draw(player, y_offset); /* Black borders. */ drect(0, 0, DRAW_OFFSET_X - 1, DHEIGHT, C_BLACK); - drect(DRAW_OFFSET_X + LEVEL_WIDTH_PX, 0, DWIDTH, DHEIGHT, C_BLACK); + drect(DRAW_OFFSET_X + LEVEL_WIDTH_PX, 0, DWIDTH, DHEIGHT, + C_BLACK); dupdate(); } diff --git a/src/player.c b/src/player.c index 483ca3e..3a77b76 100644 --- a/src/player.c +++ b/src/player.c @@ -4,17 +4,19 @@ * [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg), * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ -#include -#include -#include "lazyint.h" #include "player.h" #include "conf.h" -#include "vec2.h" -#include "level.h" #include "input.h" +#include "lazyint.h" +#include "level.h" #include "tiles.h" +#include "vec2.h" +#include +#include -void player_init(Player *player) { +void +player_init(Player *player) +{ player->pos = (Vec2){0, 0}; player->spd_x = 0.0; player->spd_y = 0.0; @@ -25,16 +27,17 @@ void player_init(Player *player) { player->coyot = 0; } -void player_update(Player *player, Level *level, Input input, u8 *level_id) { +void +player_update(Player *player, Level *level, Input input, u8 *level_id) +{ const Vec2 one_px_down = (Vec2){player->pos.x, player->pos.y + 1}; const bool on_ground = - player_collide(*level, one_px_down, SOLID_TILE, 0) || - player_collide(*level, one_px_down, SEMI_SOLID_TILE, 0); + player_collide(*level, one_px_down, SOLID_TILE, 0) || + player_collide(*level, one_px_down, SEMI_SOLID_TILE, 0); if (!player->stun) { /* Get input. */ - const i8 move_x = sign( - input_is_down(input, K_RIGHT) - - input_is_down(input, K_LEFT)); + const i8 move_x = sign(input_is_down(input, K_RIGHT) - + input_is_down(input, K_LEFT)); const bool fastfall = input_is_down(input, K_DOWN); const bool jump_pressed = input_is_pressed(input, K_JUMP); const bool jump_held = input_is_down(input, K_JUMP); @@ -75,18 +78,19 @@ void player_update(Player *player, Level *level, Input input, u8 *level_id) { player->jump_buffer = 0; player->coyot = 0; } - } - else { + } else { /* Gravity. */ player->spd_y += GRAVITY; /* Vertical friction. */ player->spd_y *= 1 - FALL_RESISTANCE; } /* Semi-solid platform stop fall. */ - if (player->spd_y > 0 && player_collide(*level, player->pos, SEMI_SOLID_TILE, 0)) + if (player->spd_y > 0 && + player_collide(*level, player->pos, SEMI_SOLID_TILE, 0)) player->spd_y = 0; /* Apply movement. */ - const bool hit_wall_x = player_move(player, *level, round(player->spd_x), 0); + const bool hit_wall_x = + player_move(player, *level, round(player->spd_x), 0); player_move(player, *level, 0, round(player->spd_y)); /* If player hit a wall in stun mode, turn back. */ if (hit_wall_x && player->stun) { @@ -101,8 +105,7 @@ void player_update(Player *player, Level *level, Input input, u8 *level_id) { player->knocked = true; } player->stun = true; - } - else { + } else { player->knocked = false; if (on_ground) player->stun = false; @@ -111,29 +114,30 @@ void player_update(Player *player, Level *level, Input input, u8 *level_id) { if ((player->pos.x <= -PLAYER_WIDTH) || (player->pos.y <= -PLAYER_HEIGHT) || (player->pos.x >= LEVEL_WIDTH_PX) || - (player->pos.y >= LEVEL_HEIGHT_PX)) - { + (player->pos.y >= LEVEL_HEIGHT_PX)) { player_init(player); player->pos = level->start_pos; } /* Get keys. */ player_collide_keys(player, level); /* Exit, victory! */ - if (player->keys_left == 0 && player_collide(*level, player->pos, EXIT_TILE, 0)) { + if (player->keys_left == 0 && + player_collide(*level, player->pos, EXIT_TILE, 0)) { *level_id += 1; level_load(level, player, *level_id); - } - else if (input_is_pressed(input, K_RESTART)) /* Manual restart. */ + } else if (input_is_pressed(input, K_RESTART)) /* Manual restart. */ level_load(level, player, *level_id); } -void player_draw(Player player, i16 y_offset) { +void +player_draw(Player player, i16 y_offset) +{ /* Draw colored rectangle depending on player state. */ u32 color = 0; if (player.stun) - color = C_RGB(230/8, 41/8, 55/8); /* Red. */ + color = C_RGB(230 / 8, 41 / 8, 55 / 8); /* Red. */ else - color = C_RGB(0/8, 121/8, 241/8); /* Blue. */ + color = C_RGB(0 / 8, 121 / 8, 241 / 8); /* Blue. */ const vec2_int_t x = player.pos.x + DRAW_OFFSET_X; const vec2_int_t y = player.pos.y + y_offset; drect(x, y, x + PLAYER_WIDTH - 1, y + PLAYER_HEIGHT - 1, color); @@ -141,7 +145,9 @@ void player_draw(Player player, i16 y_offset) { /* Helper functions */ -bool player_collide(Level level, Vec2 pos, tile_t tile, u8 margin) { +bool +player_collide(Level level, Vec2 pos, tile_t tile, u8 margin) +{ /* Note: these `* 2` are here to completely replicate the behavior of * the original Painfull Success. If you want to reuse this in your * game, remove them. They are a bug! */ @@ -150,20 +156,24 @@ bool player_collide(Level level, Vec2 pos, tile_t tile, u8 margin) { const vec2_int_t yt = pos.y + margin; const vec2_int_t yb = pos.y + PLAYER_HEIGHT - 1 - margin * 2; return ((tile == level_get_tile_at_px(level, (Vec2){xl, yt})) || - (tile == level_get_tile_at_px(level, (Vec2){xr, yt})) || - (tile == level_get_tile_at_px(level, (Vec2){xl, yb})) || - (tile == level_get_tile_at_px(level, (Vec2){xr, yb}))); + (tile == level_get_tile_at_px(level, (Vec2){xr, yt})) || + (tile == level_get_tile_at_px(level, (Vec2){xl, yb})) || + (tile == level_get_tile_at_px(level, (Vec2){xr, yb}))); } -bool player_move(Player *player, Level level, i8 spd_x, i8 spd_y) { +bool +player_move(Player *player, Level level, i8 spd_x, i8 spd_y) +{ player->pos.x += spd_x; player->pos.y += spd_y; /* If player is in solid, move back until it ain't. */ if (player_collide(level, player->pos, SOLID_TILE, 0)) { const i8 sign_spd_x = sign(spd_x); const i8 sign_spd_y = sign(spd_y); - if (sign_spd_x != 0) player->spd_x = 0; - if (sign_spd_y != 0) player->spd_y = 0; + if (sign_spd_x != 0) + player->spd_x = 0; + if (sign_spd_y != 0) + player->spd_y = 0; while (player_collide(level, player->pos, SOLID_TILE, 0)) { player->pos.x -= sign_spd_x; player->pos.y -= sign_spd_y; @@ -175,7 +185,9 @@ bool player_move(Player *player, Level level, i8 spd_x, i8 spd_y) { /* Seek for a key tile at given position, if found destroy it and return 1. * Otherwise, return 0. */ -u8 player_collide_key(Level *level, vec2_int_t x, vec2_int_t y) { +u8 +player_collide_key(Level *level, vec2_int_t x, vec2_int_t y) +{ if (level_get_tile_at_px(*level, (Vec2){x, y}) == KEY_TILE) { level_set_tile_at_px(level, (Vec2){x, y}, AIR_TILE); return 1; @@ -184,20 +196,23 @@ u8 player_collide_key(Level *level, vec2_int_t x, vec2_int_t y) { } /* Destroy keys the player is touching and deduce them from keys left. */ -void player_collide_keys(Player *player, Level *level) { +void +player_collide_keys(Player *player, Level *level) +{ const vec2_int_t xl = player->pos.x; const vec2_int_t xr = xl + PLAYER_WIDTH - 1; const vec2_int_t yt = player->pos.y; const vec2_int_t yb = yt + PLAYER_HEIGHT - 1; - player->keys_left -= - player_collide_key(level, xl, yt) + - player_collide_key(level, xr, yt) + - player_collide_key(level, xl, yb) + - player_collide_key(level, xr, yb); + player->keys_left -= player_collide_key(level, xl, yt) + + player_collide_key(level, xr, yt) + + player_collide_key(level, xl, yb) + + player_collide_key(level, xr, yb); } /* Used by `collide_keys()`. Don't call this one directly. */ -i8 sign(i8 value) { +i8 +sign(i8 value) +{ if (value > 0) return 1; else if (value < 0) @@ -206,7 +221,9 @@ i8 sign(i8 value) { return 0; } -f32 signf(f32 value) { +f32 +signf(f32 value) +{ if (value > 0.0) return 1.0; else if (value < 0.0) @@ -215,7 +232,9 @@ f32 signf(f32 value) { return 0.0; } -i8 round(f32 value) { +i8 +round(f32 value) +{ if (value > 0) return (i8)(value + 0.5); else diff --git a/src/tiles.c b/src/tiles.c index edffbab..d5f4d24 100644 --- a/src/tiles.c +++ b/src/tiles.c @@ -4,20 +4,36 @@ * [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg), * which is MIT licensed. The MIT license requires this copyright notice to be * included in all copies and substantial portions of the software. */ -#include -#include "lazyint.h" #include "tiles.h" +#include "lazyint.h" +#include -u32 tile_color(tile_t tile) { +u32 +tile_color(tile_t tile) +{ int color = 0; switch (tile) { - case AIR_TILE: color = C_RGB(0/8, 0/8, 0/8); break; - case SOLID_TILE: color = C_RGB(10/8, 210/8, 180/8); break; - case PAIN_TILE: color = C_RGB(210/8, 10/8, 180/8); break; - case SPAWN_TILE: color = C_RGB(20/8, 220/8, 20/8); break; - case EXIT_TILE: color = C_RGB(250/8, 220/8, 10/8); break; - case KEY_TILE: color = C_RGB(210/8, 210/8, 210/8); break; - case SEMI_SOLID_TILE: color = C_RGB(5/8, 105/8, 90/8); break; + case AIR_TILE: + color = C_RGB(0 / 8, 0 / 8, 0 / 8); + break; + case SOLID_TILE: + color = C_RGB(10 / 8, 210 / 8, 180 / 8); + break; + case PAIN_TILE: + color = C_RGB(210 / 8, 10 / 8, 180 / 8); + break; + case SPAWN_TILE: + color = C_RGB(20 / 8, 220 / 8, 20 / 8); + break; + case EXIT_TILE: + color = C_RGB(250 / 8, 220 / 8, 10 / 8); + break; + case KEY_TILE: + color = C_RGB(210 / 8, 210 / 8, 210 / 8); + break; + case SEMI_SOLID_TILE: + color = C_RGB(5 / 8, 105 / 8, 90 / 8); + break; } return color; }