浏览代码

chore: add clang-format

Douglas A 1 年之前
父节点
当前提交
68987ca209
共有 6 个文件被更改,包括 243 次插入141 次删除
  1. 67 0
      .clang-format
  2. 44 0
      .clang-tidy
  3. 10 10
      bullet.h
  4. 7 9
      game.h
  5. 107 112
      main.c
  6. 8 10
      ship.h

+ 67 - 0
.clang-format

@@ -0,0 +1,67 @@
+BasedOnStyle:  Google
+Language: Cpp
+ColumnLimit: 100
+IndentWidth: 2
+TabWidth: 8
+UseTab: Never
+IndentCaseLabels: false
+BreakBeforeBraces: Custom
+AlignEscapedNewlinesLeft: false
+AllowShortFunctionsOnASingleLine: false
+AlignTrailingComments: true
+SpacesBeforeTrailingComments: 2
+PenaltyReturnTypeOnItsOwnLine: 200
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+BinPackParameters: true
+BreakBeforeBinaryOperators: true
+BreakBeforeTernaryOperators: true
+ContinuationIndentWidth: 2
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: No
+AlwaysBreakTemplateDeclarations: No
+AlignEscapedNewlines: DontAlign
+BinPackArguments: true
+BraceWrapping:
+  AfterClass:      false
+  AfterControlStatement: false
+  AfterEnum:       false
+  AfterFunction:   true
+  AfterObjCDeclaration: false
+  AfterStruct:     false
+  AfterUnion:      false
+  BeforeCatch:     false
+  BeforeElse:      false
+  IndentBraces:    false
+PointerAlignment: Right
+SortIncludes: true
+Cpp11BracedListStyle: false
+IncludeCategories:
+  - Regex:           '<[/[:alnum:].]+>'
+    Priority:        0
+  - Regex:           '^"(nvim|vim)/'
+    Priority:        1
+    SortPriority:    1
+    CaseSensitive:   false
+AlignConsecutiveMacros: AcrossEmptyLines
+IndentPPDirectives: AfterHash
+SpaceBeforeParens: ControlStatementsExceptControlMacros
+PPIndentWidth: 1
+ForEachMacros:
+  - FOR_ALL_AUEVENTS
+  - FOR_ALL_AUPATS_IN_EVENT
+  - FOR_ALL_BUFFERS
+  - FOR_ALL_BUFFERS_BACKWARDS
+  - FOR_ALL_FRAMES
+  - FOR_ALL_QFL_ITEMS
+  - FOR_ALL_SIGNS_IN_BUF
+  - FOR_ALL_TABS
+  - FOR_ALL_TAB_WINDOWS
+  - FOR_ALL_WINDOWS_IN_TAB
+  - RBUFFER_EACH
+  - RBUFFER_EACH_REVERSE
+  - RBUFFER_UNTIL_EMPTY
+  - RBUFFER_UNTIL_FULL
+  - kl_iter

+ 44 - 0
.clang-tidy

@@ -0,0 +1,44 @@
+WarningsAsErrors: '*'
+Checks: >
+  -*,
+
+  bugprone-*,
+  google-*,
+  misc-*,
+  modernize-*,
+  performance-*,
+  portability-*,
+  readability-*,
+
+  -bugprone-assignment-in-if-condition,
+  -bugprone-branch-clone,
+  -bugprone-easily-swappable-parameters,
+  -bugprone-implicit-widening-of-multiplication-result,
+  -bugprone-macro-parentheses,
+  -bugprone-narrowing-conversions,
+  -bugprone-not-null-terminated-result,
+  -bugprone-reserved-identifier,
+  -bugprone-sizeof-expression,
+  -bugprone-suspicious-include,
+  -bugprone-suspicious-memory-comparison,
+  -bugprone-unused-return-value,
+  -google-readability-braces-around-statements,
+  -google-readability-function-size,
+  -misc-misplaced-const,
+  -misc-no-recursion,
+  -misc-unused-parameters,
+  -modernize-macro-to-enum,
+  -performance-no-int-to-ptr,
+  -readability-avoid-const-params-in-decls,
+  -readability-braces-around-statements,
+  -readability-else-after-return,
+  -readability-function-cognitive-complexity,
+  -readability-function-size,
+  -readability-identifier-length,
+  -readability-isolate-declaration,
+  -readability-magic-numbers,
+  -readability-misleading-indentation,
+  -readability-redundant-declaration,
+  -readability-redundant-function-ptr-dereference,
+  -readability-suspicious-call-argument,
+  -readability-non-const-parameter,

+ 10 - 10
bullet.h

@@ -1,18 +1,18 @@
 #ifndef _BULLET_H
 #define _BULLET_H
 
-#include "raymath.h"
 #include <stdbool.h>
-typedef struct bullet_t {
-    Vector2 position;
-    Vector2 velocity;
-    Vector2 accel;
-    Vector2 direction;
-    float radius;
-    bool active;
 
-} bullet;
+#include "raymath.h"
 
+typedef struct bullet_t {
+  Vector2 position;
+  Vector2 velocity;
+  Vector2 accel;
+  Vector2 direction;
+  float radius;
+  bool active;
 
+} bullet;
 
-#endif // _BULLET_H
+#endif  // _BULLET_H

+ 7 - 9
game.h

@@ -7,14 +7,12 @@
 #define MAX_BULLETS 1024
 
 typedef struct game_g {
-    ship s;
-    bullet bullets[MAX_BULLETS];
-    size_t bullets_fired;
-    size_t active_bullets;
-    float last_fire;
-    float dt;
+  ship s;
+  bullet bullets[MAX_BULLETS];
+  size_t bullets_fired;
+  size_t active_bullets;
+  float last_fire;
+  float dt;
 } game;
 
-
-
-#endif // _GAME_H
+#endif  // _GAME_H

+ 107 - 112
main.c

@@ -1,190 +1,185 @@
-#include "raylib.h"
-#include "raymath.h"
-#include <stdbool.h>
 #include <math.h>
+#include <raylib.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 
-#include "ship.h"
 #include "bullet.h"
 #include "game.h"
+#include "ship.h"
 
 #define DRAG -0.001f
 
-void ship_draw(const ship* s) { DrawRectangleV(s->position, s->size, BLACK); }
-
-void ship_up(ship* s) {
+void ship_draw(const ship* s)
+{
+  DrawRectangleV(s->position, s->size, BLACK);
+}
 
-    Vector2 min = { .x = -1000.0f, .y = -1000.0f };
-    Vector2 max = { .x = 1000.0f, .y = 1000.0f };
+void ship_up(ship* s)
+{
+  Vector2 min = { .x = -1000.0F, .y = -1000.0F };
+  Vector2 max = { .x = 1000.0F, .y = 1000.0F };
 
-    Vector2 diff = Vector2Subtract(s->direction, s->position);
+  Vector2 diff = Vector2Subtract(s->direction, s->position);
 
-    Vector2 accellv = CLITERAL(Vector2) { .x = diff.x, .y = diff.y };
-    Vector2Normalize(accellv);
-    s->accel = Vector2Add(accellv, s->accel);
+  Vector2 accellv = CLITERAL(Vector2){ .x = diff.x, .y = diff.y };
+  s->accel = Vector2Add(accellv, s->accel);
 
-    s->accel = Vector2Clamp(s->accel, min, max);
+  s->accel = Vector2Clamp(s->accel, min, max);
 }
 
-void ship_stop(ship* s) {
-
-    Vector2 min = { .x = 1.0, .y = 1.0 };
-    Vector2 max = { .x = 0.0, .y = 0.0 };
-    s->accel.y -= 0.0001f;
-    s->accel.x -= 0.0001f;
-    s->accel = Vector2Clamp(s->accel, min, max);
+void ship_stop(ship* s)
+{
+  Vector2 min = { .x = 1.0F, .y = 1.0F };
+  Vector2 max = { .x = 0.0F, .y = 0.0F };
+  s->accel.y -= 0.0001F;
+  s->accel.x -= 0.0001F;
+  s->accel = Vector2Clamp(s->accel, min, max);
 }
 
-void ship_update(game *g, ship* s) {
-    Vector2 drag = { .x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y };
+void ship_update(game* g, ship* s)
+{
+  Vector2 drag = { .x = DRAG * s->velocity.x, .y = DRAG * s->velocity.y };
 
-    float x = cosf(s->angle);
-    float y = sinf(s->angle);
+  float x = cosf(s->angle);
+  float y = sinf(s->angle);
 
-    Vector2 polar = CLITERAL(Vector2) { .x = x, .y = y };
-    Vector2 pos = Vector2Add(s->position, polar);
+  Vector2 polar = CLITERAL(Vector2){ .x = x, .y = y };
+  Vector2 pos = Vector2Add(s->position, polar);
 
-    float x1 = 100 * cosf(s->angle);
-    float y1 = 100 * sinf(s->angle);
+  float x1 = 100 * cosf(s->angle);
+  float y1 = 100 * sinf(s->angle);
 
-    Vector2 polar1 = CLITERAL(Vector2) { .x = x1, .y = y1 };
-    Vector2 pos1 = Vector2Add(s->position, polar1);
+  Vector2 polar1 = CLITERAL(Vector2){ .x = x1, .y = y1 };
+  Vector2 pos1 = Vector2Add(s->position, polar1);
 
-    s->direction.x = pos.x;
-    s->direction.y = pos.y;
+  s->direction.x = pos.x;
+  s->direction.y = pos.y;
 
-    s->velocity.x += s->accel.x * g->dt;
-    s->velocity.y += s->accel.y * g->dt;
-    s->velocity = Vector2Add(s->velocity, drag);
+  s->velocity.x += s->accel.x * g->dt;
+  s->velocity.y += s->accel.y * g->dt;
+  s->velocity = Vector2Add(s->velocity, drag);
 
-    s->position.x += s->velocity.x * g->dt;
-    s->position.y += s->velocity.y * g->dt;
+  s->position.x += s->velocity.x * g->dt;
+  s->position.y += s->velocity.y * g->dt;
 
-    DrawLine(s->position.x, s->position.y, pos1.x, pos1.y, RED);
+  DrawLine(s->position.x, s->position.y, pos1.x, pos1.y, RED);
 }
 
 void ship_fire(game* g, ship* s)
 {
+  // Vector2 min = { .x = -300.0f, .y = -300.0f };
+  // Vector2 max = { .x = 300.0f, .y = 300.0f };
+  if (g->bullets_fired >= MAX_BULLETS) {
+    g->bullets_fired = 0;
+    // g->active_bullets -= 1;
+  }
+  float current_fire = GetFrameTime();
+  float allowed_fire = 0.0002;
+  if (current_fire > allowed_fire) {
+    Vector2 diff = Vector2Subtract(s->direction, s->position);
 
-    //Vector2 min = { .x = -300.0f, .y = -300.0f };
-    //Vector2 max = { .x = 300.0f, .y = 300.0f };
-    if (g->bullets_fired >= MAX_BULLETS) {
-        g->bullets_fired = 0;
-        //g->active_bullets -= 1;
-    }
-    float current_fire = GetFrameTime();
-    float allowed_fire = 0.0002;
-    if (current_fire > allowed_fire) {
-
-        Vector2 diff = Vector2Subtract(s->direction, s->position);
-
-        Vector2 accellv = CLITERAL(Vector2) { .x = diff.x, .y = diff.y };
-        g->bullets[g->bullets_fired].accel = Vector2Zero();//Vector2Scale(diff,3000);//Vector2Add(accellv, s->accel);
-
-        g->bullets[g->bullets_fired].position = s->position;
-        g->bullets[g->bullets_fired].radius = 5.0f;
-        g->bullets[g->bullets_fired].velocity = Vector2Scale(diff, 3000);
-        g->bullets[g->bullets_fired].active = true;
+    Vector2 accellv = CLITERAL(Vector2){ .x = diff.x, .y = diff.y };
+    g->bullets[g->bullets_fired].accel
+      = Vector2Zero();  // Vector2Scale(diff,3000);//Vector2Add(accellv, s->accel);
 
-        g->bullets_fired++;
-        g->active_bullets += 1;
-        g->last_fire = GetFrameTime();
-    }
+    g->bullets[g->bullets_fired].position = s->position;
+    g->bullets[g->bullets_fired].radius = 5.0F;
+    g->bullets[g->bullets_fired].velocity = Vector2Scale(diff, 3000);
+    g->bullets[g->bullets_fired].active = true;
 
+    g->bullets_fired++;
+    g->active_bullets += 1;
+    g->last_fire = GetFrameTime();
+  }
 }
 
-void bullets_draw(game* g, bullet* b) {
-    
-    if (b->active) {
-        DrawCircle(b->position.x, b->position.y, b->radius, RED);
-    }
-    
+void bullets_draw(game* g, bullet* b)
+{
+  if (b->active) {
+    DrawCircle(b->position.x, b->position.y, b->radius, RED);
+  }
 }
 
-void bullets_update(game* g, bullet* b) {
-    
-        if (b->active) {
-           
-            b->velocity.x += b->accel.x * g->dt;
-            b->velocity.y += b->accel.y * g->dt;
+void bullets_update(game* g, bullet* b)
+{
+  if (b->active) {
+    b->velocity.x += b->accel.x * g->dt;
+    b->velocity.y += b->accel.y * g->dt;
 
-            b->position.x += b->velocity.x * g->dt;
-            b->position.y += b->velocity.y * g->dt;
+    b->position.x += b->velocity.x * g->dt;
+    b->position.y += b->velocity.y * g->dt;
 
-            b->velocity = Vector2ClampValue(b->velocity, -3000, 3000);
-        
-            if (b->position.x > (GetScreenWidth() - b->radius) || b->position.y > (GetScreenHeight() - b->radius)) {
-                b->active = false;
-                g->active_bullets -= 1;
-            }
+    b->velocity = Vector2ClampValue(b->velocity, -3000, 3000);
 
-            if (b->position.x < b->radius || b->position.y < b->radius) {
-                b->active = false;
-                g->active_bullets -= 1;
-            }
+    if (b->position.x > (GetScreenWidth() - b->radius)
+        || b->position.y > (GetScreenHeight() - b->radius)) {
+      b->active = false;
+      g->active_bullets -= 1;
+    }
 
-            //DrawLine(g->s.position.x, g->s.position.y, b->direction.x, b->direction.y, GREEN);
-        }
+    if (b->position.x < b->radius || b->position.y < b->radius) {
+      b->active = false;
+      g->active_bullets -= 1;
+    }
 
+    // DrawLine(g->s.position.x, g->s.position.y, b->direction.x, b->direction.y, GREEN);
+  }
 }
 
-
-
-void process_inputs(game *g) {
-
+void process_inputs(game* g)
+{
   if (IsKeyDown(KEY_W)) {
     ship_up(&g->s);
   } else {
     ship_stop(&g->s);
   }
   if (IsKeyDown(KEY_D)) {
-    g->s.angle += 1.0f * g->dt;
+    g->s.angle += 1.0F * g->dt;
   }
   if (IsKeyDown(KEY_A)) {
-    g->s.angle -= 1.0f * g->dt;
+    g->s.angle -= 1.0F * g->dt;
   }
 
   if (IsKeyDown(KEY_SPACE)) {
-      ship_fire(g, &g->s);
+    ship_fire(g, &g->s);
   }
-
 }
 
-void text(const game *g) {
-  char ind[256] = {0};
+void text(const game* g)
+{
+  char ind[256] = { 0 };
   sprintf(ind,
           "ship position %fx%f\nship speed %fx%f\nship accel %fx%f\nangle: "
           "%f\ndirection %fx%f\nbullets fired %zu\nactive bullets %zu",
-          g->s.position.x, g->s.position.y, g->s.velocity.x, g->s.velocity.y,
-          g->s.accel.x, g->s.accel.y, g->s.angle, g->s.direction.x,
-          g->s.direction.y, g->bullets_fired, g->active_bullets);
+          g->s.position.x, g->s.position.y, g->s.velocity.x, g->s.velocity.y, g->s.accel.x,
+          g->s.accel.y, g->s.angle, g->s.direction.x, g->s.direction.y, g->bullets_fired,
+          g->active_bullets);
   DrawText(ind, 10, 10, 20, BLACK);
 }
 
-int main(void) {
-
+int main(void)
+{
   InitWindow(1820, 900, "Hello World");
 
-  ship s1 = {0};
-  s1.position.x = 910.0f;
-  s1.position.y = 450.0f;
+  ship s1 = { 0 };
+  s1.position.x = 910.0F;
+  s1.position.y = 450.0F;
 
-  s1.size.x = 10.0f;
-  s1.size.y = 10.0f;
+  s1.size.x = 10.0F;
+  s1.size.y = 10.0F;
   s1.angle = 3 * PI / 2;
 
-
   game g;
-
+  g.s = s1;
 
   memset(g.bullets, 0, MAX_BULLETS);
 
   while (!WindowShouldClose()) {
     BeginDrawing();
     ClearBackground(WHITE);
-      
-    for (size_t i=0; i < MAX_BULLETS; i++) {
+
+    for (size_t i = 0; i < MAX_BULLETS; i++) {
       bullets_draw(&g, &g.bullets[i]);
       bullets_update(&g, &g.bullets[i]);
     }

+ 8 - 10
ship.h

@@ -1,17 +1,15 @@
 #ifndef _SHIP_H
 #define _SHIP_H
 
-#include "raymath.h"
+#include <raymath.h>
 
 typedef struct ship_t {
-    Vector2 position;
-    Vector2 velocity;
-    Vector2 accel;
-    Vector2 size;
-    Vector2 direction;
-    float angle;
+  Vector2 position;
+  Vector2 velocity;
+  Vector2 accel;
+  Vector2 size;
+  Vector2 direction;
+  float angle;
 } ship;
 
-
-
-#endif //_SHIP_H
+#endif  //_SHIP_H