diff --git a/.clang-tidy b/.clang-tidy index 7756ffa..815041e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -2,21 +2,77 @@ # Keep checks at tool defaults while treating all warnings as errors. # clang-analyzer-* : enable static analysis passes for deeper bug finding # clang-diagnostic-* : surface compiler-style warnings (unused variables, conversions, etc.) -# clang-diagnostic-unused-const-variable : warn on unused 'const' variables # google-build-using-namespace : reject global using-namespace directives -# modernize-deprecated-headers : prefer C++ standard headers over legacy C ones -# modernize-loop-convert : prefer range-based for loops over traditional loops -# modernize-use-stdarray : prefer 'std::array' over C-style arrays -# modernize-use-using : prefer 'using' type alias declarations over 'typedef's -# readability-magic-numbers : discourage use of magic numbers in code +# misc-include-cleaner : ensure proper #includes are present and unused ones are removed +# modernize-* : enforce modern C++ idioms (e.g., avoid C arrays, prefer std::numbers) +# performance-* : flag common performance pitfalls +# readability-identifier-naming : enforce consistent identifier style Checks: > clang-analyzer-*, clang-diagnostic-*, - clang-diagnostic-unused-const-variable, google-build-using-namespace, - modernize-deprecated-headers, - modernize-loop-convert, - modernize-use-stdarray, - modernize-use-using + misc-include-cleaner, + modernize-*, + performance-*, + readability-identifier-naming WarningsAsErrors: '*' + +CheckOptions: + # ===== Types ===== + - key: readability-identifier-naming.NamespaceCase + value: lower_case + - key: readability-identifier-naming.ClassCase + value: CamelCase + - key: readability-identifier-naming.StructCase + value: CamelCase + - key: readability-identifier-naming.UnionCase + value: CamelCase + - key: readability-identifier-naming.TypeAliasCase + value: CamelCase + - key: readability-identifier-naming.EnumCase + value: CamelCase + + # ===== Enum Values ===== + - key: readability-identifier-naming.EnumConstantCase + value: UPPER_CASE + + # ===== Functions & Methods ===== + # (matches typical C++ engine style; avoids collision with OpenGL's mixed conventions) + - key: readability-identifier-naming.FunctionCase + value: lower_case + - key: readability-identifier-naming.MethodCase + value: lower_case + + # ===== Variables ===== + - key: readability-identifier-naming.VariableCase + value: lower_case + - key: readability-identifier-naming.ParameterCase + value: lower_case + + # ===== Member Variables ===== + # Strongly recommended for rendering code for clarity + - key: readability-identifier-naming.MemberCase + value: lower_case + - key: readability-identifier-naming.PrivateMemberPrefix + value: m_ + + # ===== Constants ===== + - key: readability-identifier-naming.GlobalConstantCase + value: UPPER_CASE + - key: readability-identifier-naming.StaticConstantCase + value: UPPER_CASE + - key: readability-identifier-naming.ConstexprVariableCase + value: UPPER_CASE + + # ===== Modernize tuning ===== + - key: modernize-loop-convert.MinConfidence + value: reasonable + - key: modernize-use-override.UseOverride + value: '1' + - key: modernize-avoid-c-arrays.CheckCStrings + value: '1' + + # ===== Include cleaner ===== + - key: misc-include-cleaner.StrictMode + value: '0' diff --git a/Makefile b/Makefile index 03ac160..89592b2 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ SHELL := /bin/bash CC := gcc TARGET := main SRCS := main.cpp +# `-lstdc++` was added to resolve [this issue](https://stackoverflow.com/questions/33263288/libstdc-dso-missing-from-command-line) +# `-lm` was added to resolve [this issue](https://stackoverflow.com/questions/16006145/ld-undefined-reference-to-symbol-log2glibc-2-2-5) LIBS := -lGL -lGLU -lglut -lIL -lILU -lILUT -lstdc++ -lm all: $(TARGET) diff --git a/README.md b/README.md index ef3a157..142f841 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,9 @@ git clone https://github.com/robert-7/Go-Board-Game.git && cd Go-Board-Game ## Building and Running Building requires the OpenGL and DevIL libraries imported. -To build the binary, simply run `make`. -To run the built binary, simply run `./main`. +To build and run the binary, simply run `make && ./main`. To clean up, run `make clean`. -### Building Gotchas - -* `-lstdc++` was added to resolve [this issue](https://stackoverflow.com/questions/33263288/libstdc-dso-missing-from-command-line) -* `-lm` was added to resolve [this issue](https://stackoverflow.com/questions/16006145/ld-undefined-reference-to-symbol-log2glibc-2-2-5). - ## Linting The repository uses the same commands locally and in CI. After installing the packages above, run: @@ -50,7 +44,8 @@ The repository uses the same commands locally and in CI. After installing the pa ./scripts/run_lint.sh ``` -The script configures CMake, runs clang-tidy, and then runs cppcheck with the same settings used in CI. It can be removed along with the `build` directory when you finish linting. +The script configures CMake, runs clang-tidy, and then runs cppcheck with the same settings used in CI. +It can be removed along with the `build` directory when you finish linting. To run linting automatically before each commit, install [`pre-commit`](https://pre-commit.com/) (e.g., `pip install pre-commit` or `sudo apt install pre-commit`), then enable the hooks once: diff --git a/main.cpp b/main.cpp index d3886d9..95714da 100644 --- a/main.cpp +++ b/main.cpp @@ -1,58 +1,43 @@ -// OLD INCLUDES/USINGS -// #include -// #include -// #include -// Standard library -#include +// Standard libraries #include #include -#include - -// For Sleep in Windows -// #include -// For Sleep in Linux -#include -// #pragma comment(lib, "ILUT.lib") - -#include - -// For Sleep in Windows -// #include -// For Sleep in Linux -#include -#include +#include // For std::cout +#include +#if defined(__has_include) +#if __has_include() +#include +#endif +#endif +#include +#include // For usleep +#include // includes, graphics +#include #include #include -#include - +// DevIL includes #define ILUT_USE_OPENGL #include #include #include -ILuint il[3] = {0, 1, 2}; -GLuint texture[3] = {0, 1, 2}; - -// For the queue -#include -#include -#include +std::array image_ids{0, 1, 2}; +std::array textures{0, 1, 2}; -#define GL_CLAMP_TO_EDGE 0x812F +constexpr GLenum GL_CLAMP_TO_EDGE_VALUE = 0x812F; // Window settings -int windowID; // Glut window ID (for display) +int window_id; // Glut window ID (for display) // Camera methods void mouse(int button, int state, int x, int y); void motion(int x, int y); // Camera settings -bool updateCamZPos = false; -int lastX = 0; -int lastY = 0; +bool update_cam_z_pos = false; +int last_x = 0; +int last_y = 0; const float ZOOM_SCALE = 0.01; constexpr int BOARD_SIZE = 19; @@ -63,7 +48,11 @@ constexpr int BOARD_CENTER = BOARD_SIZE / 2; constexpr GLdouble INITIAL_CAM_X = 0.0; constexpr GLdouble INITIAL_CAM_Y = 0.0; constexpr GLdouble INITIAL_CAM_Z = -1.5; -constexpr float PI = 3.14159265358979323846F; // Pi constant for trigonometric helpers +#if defined(__cpp_lib_math_constants) +constexpr float PI = std::numbers::pi_v; // Pi constant for trigonometric helpers +#else +constexpr float PI = 3.14159265358979323846F; // NOLINT(modernize-use-std-numbers) +#endif struct Camera { GLdouble x; @@ -74,27 +63,27 @@ struct Camera { int wireframe = 0; int lighting = 1; int material = 1; -int pauseBoardRotation_y = 1; -int pauseBoardRotation_x = 1; -int pauseLighting = 0; -int depthTest = 1; -int cullFace = 1; -int smooth = 1; -int enabletexture = 0; +int pause_board_rotation_y = 1; +int pause_board_rotation_x = 1; +int pause_lighting = 0; +int depth_test = 1; +int cull_face = 1; +int smooth_shading = 1; +int enable_texture = 0; constexpr int INITIAL_BOARD_ANGLE_X = 45; constexpr int INITIAL_BOARD_ANGLE_Y = 0; float angle_x = INITIAL_BOARD_ANGLE_X; float angle_y = INITIAL_BOARD_ANGLE_Y; -float translatelight = 0; +float translate_light = 0; -Camera camera{INITIAL_CAM_X, INITIAL_CAM_Y, INITIAL_CAM_Z}; +Camera camera{.x = INITIAL_CAM_X, .y = INITIAL_CAM_Y, .z = INITIAL_CAM_Z}; // Behind the Scenes variables -int placex = 0; -int placey = 0; -int col = 1; -float t = 0; +int place_x = 0; +int place_y = 0; +int stone_color = 1; +float animation_time = 0; constexpr float TIME_INCREMENT = 0.002; // Depicts how fast time increments. constexpr int DEFAULT_SLEEP_TIME = 3000; // Default sleep time in microseconds. @@ -103,7 +92,7 @@ std::array, BOARD_SIZE> board_status{}; std::array, BOARD_SIZE> liberties_status{}; int restart_option = 0; std::queue rm_queue; -std::list> L; +std::list> captured_groups; // float rm_array[1083]; // Holds the objects that will be removed. // int p = 0; // Always points to the last index of rm_array. @@ -113,45 +102,45 @@ void display(); void keyboard(unsigned char, int, int); void init(); void idle(); -void SetImage(); -void DrawSphere(int); -void DrawUnitCube(int); -void ApplyTransformations(float indx, float indy, float z); -void ApplyTransformationGeneral(float indx, float indy, float z); +void set_images(); +void draw_sphere(int color); +void draw_unit_cube(int color); +void apply_transformations(float indx, float indy, float z); +void apply_transformation_general(float indx, float indy, float z); // Behind the Scenes void init_board(); void clear_liberties(); -int make_move(int x, int y, int piece); -int check_liberties(int x, int y, int originx, int originy, int piece); +auto make_move(int x, int y, int piece) -> int; +auto check_liberties(int x, int y, int originx, int originy, int piece) -> int; void remove_block(int x, int y, int piece); void jump_off(int x0, int y0, int color); //! Program entry point -int main(int argc, char **argv) { - const int DEFAULT_WINDOW_WIDTH = 512; - const int DEFAULT_WINDOW_HEIGHT = 512; - const int DEFAULT_WINDOW_X = 100; - const int DEFAULT_WINDOW_Y = 100; - const char *WINDOW_TITLE = "Go Board Game"; +auto main(int argc, char **argv) -> int { + const int default_window_width = 768; + const int default_window_height = 768; + const int default_window_x = 256; + const int default_window_y = 256; + const char *window_title = "Go Board Game"; // initialize GLUT glutInit(&argc, argv); // set visual glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); // set physical window properties - glutInitWindowSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT); + glutInitWindowSize(default_window_width, default_window_height); // position window on the screen - glutInitWindowPosition(DEFAULT_WINDOW_X, DEFAULT_WINDOW_Y); + glutInitWindowPosition(default_window_x, default_window_y); // create window - windowID = glutCreateWindow(WINDOW_TITLE); + window_id = glutCreateWindow(window_title); ilInit(); iluInit(); ilutInit(); ilutRenderer(ILUT_OPENGL); - ilGenImages(2, il); - glGenTextures(2, texture); + ilGenImages(2, image_ids.data()); + glGenTextures(2, textures.data()); // initialize OpenGL init(); @@ -218,9 +207,9 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) break; case '5': - depthTest = !depthTest; + depth_test = !depth_test; - if (depthTest) { + if (depth_test) { glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); } else { @@ -230,9 +219,9 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) break; case '6': - cullFace = !cullFace; + cull_face = !cull_face; - if (cullFace) { + if (cull_face) { glEnable(GL_CULL_FACE); } else { glDisable(GL_CULL_FACE); @@ -241,9 +230,9 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) break; case '7': - smooth = !smooth; + smooth_shading = !smooth_shading; - if (smooth) { + if (smooth_shading) { glShadeModel(GL_SMOOTH); } else { glShadeModel(GL_FLAT); @@ -252,9 +241,9 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) break; case '8': - enabletexture = !enabletexture; + enable_texture = !enable_texture; - if (enabletexture) { + if (enable_texture) { glEnable(GL_TEXTURE_2D); } else { glDisable(GL_TEXTURE_2D); @@ -264,58 +253,58 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) case 'o': case 'O': std::cout << "PRESSED O\n"; - pauseBoardRotation_y = !pauseBoardRotation_y; + pause_board_rotation_y = !pause_board_rotation_y; break; case 'p': case 'P': std::cout << "PRESSED P\n"; - pauseBoardRotation_x = !pauseBoardRotation_x; + pause_board_rotation_x = !pause_board_rotation_x; break; case 'a': case 'A': - if (placex > -BOARD_CENTER) { - placex -= 1; + if (place_x > -BOARD_CENTER) { + place_x -= 1; } break; case 'w': case 'W': - if (placey < BOARD_CENTER) { - placey += 1; + if (place_y < BOARD_CENTER) { + place_y += 1; } break; case 's': case 'S': - if (placey > -BOARD_CENTER) { - placey -= 1; + if (place_y > -BOARD_CENTER) { + place_y -= 1; } break; case 'd': case 'D': - if (placex < BOARD_CENTER) { - placex += 1; + if (place_x < BOARD_CENTER) { + place_x += 1; } break; case '\r': - if (board_status[placex + BOARD_CENTER][placey + BOARD_CENTER] != 0) { + if (board_status[place_x + BOARD_CENTER][place_y + BOARD_CENTER] != 0) { std::cout << "YOU CAN'T PLACE A PIECE HERE BECAUSE THERE ALREADY IS A " "PIECE HERE!!!\n"; } else { std::cout << "ENTER KEY PRESSED!!!\n"; - make_move(placex + BOARD_CENTER, placey + BOARD_CENTER, col); - if (col == 1) { - col = 2; + make_move(place_x + BOARD_CENTER, place_y + BOARD_CENTER, stone_color); + if (stone_color == 1) { + stone_color = 2; } else { - col = 1; + stone_color = 1; } } break; @@ -337,17 +326,17 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) if (restart_option) { std::cout << "You pressed 'y'. The game has been restarted.\n"; init_board(); - col = 1; + stone_color = 1; } break; case 'b': case 'B': - col = 2; + stone_color = 2; break; case 'v': case 'V': - col = 1; + stone_color = 1; break; } } @@ -355,15 +344,15 @@ void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) // Handles mouse motion events while a button is pressed void motion(int x, int y) { // If the RMB is pressed and dragged then zoom in / out - if (updateCamZPos) { + if (update_cam_z_pos) { // Update camera position while the mouse is dragged - camera.z += (y - lastY) * ZOOM_SCALE; - camera.x += (x - lastX) * ZOOM_SCALE; - lastX = x; - lastY = y; + camera.z += (y - last_y) * ZOOM_SCALE; + camera.x += (x - last_x) * ZOOM_SCALE; + last_x = x; + last_y = y; // Redraw the scene from updated camera position - glutSetWindow(windowID); + glutSetWindow(window_id); glutPostRedisplay(); } } @@ -373,38 +362,38 @@ void mouse(int button, int state, int x, int y) { // If the RMB is pressed and dragged then zoom in / out if (button == GLUT_RIGHT_BUTTON) { if (state == GLUT_DOWN) { - lastX = x; - lastY = y; - updateCamZPos = true; + last_x = x; + last_y = y; + update_cam_z_pos = true; } else { - updateCamZPos = false; + update_cam_z_pos = false; } } } void idle() { - const float BOARD_ROTATION_STEP = 0.5f; - const float ANGLE_LIMIT = 360.0f; - if (!pauseBoardRotation_y) { - angle_y += BOARD_ROTATION_STEP; - if (angle_y > ANGLE_LIMIT) { - angle_y -= ANGLE_LIMIT; + const float board_rotation_step = 0.5f; + const float angle_limit = 360.0f; + if (!pause_board_rotation_y) { + angle_y += board_rotation_step; + if (angle_y > angle_limit) { + angle_y -= angle_limit; } } - if (!pauseBoardRotation_x) { - angle_x += BOARD_ROTATION_STEP; - if (angle_x > ANGLE_LIMIT) { - angle_x -= ANGLE_LIMIT; + if (!pause_board_rotation_x) { + angle_x += board_rotation_step; + if (angle_x > angle_limit) { + angle_x -= angle_limit; } } /* - if (!pauseLighting) { - translatelight += BOARD_ROTATION_STEP; + if (!pause_lighting) { + translate_light += board_rotation_step; - if (translatelight > 360) { - translatelight -= 360; + if (translate_light > 360) { + translate_light -= 360; } } */ @@ -412,80 +401,80 @@ void idle() { glutPostRedisplay(); } -void lightingFunc() { +void lighting_func() { // Specify light position - GLfloat lightposition[4] = {0.0, 0.0, -0.5, 1.0}; - glLightfv(GL_LIGHT0, GL_POSITION, lightposition); + const std::array light_position{0.0F, 0.0F, -0.5F, 1.0F}; + glLightfv(GL_LIGHT0, GL_POSITION, light_position.data()); // Specify diffuse component. Diffuse component is white. // GLfloat lightdiffuse[4] = {0.0, 0.0, 0.0, 1.0}; - GLfloat lightstrength[4] = {2.0, 2.0, 2.0, 2.0}; - glLightfv(GL_LIGHT0, GL_DIFFUSE, lightstrength); + const std::array light_strength{2.0F, 2.0F, 2.0F, 2.0F}; + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_strength.data()); // Specify specular component. Specular component is green. // TODO: Decide on desired specular component -- IF NEEDED // GLfloat lightspecular[4] = {0.0, 0.0, 0.0, 1.0}; // GLfloat lightspecular[4] = { 0.0, 2.0, 0.0, 1.0 }; - glLightfv(GL_LIGHT0, GL_SPECULAR, lightstrength); + glLightfv(GL_LIGHT0, GL_SPECULAR, light_strength.data()); - GLfloat spread = 90 - 90 * t; + GLfloat spread = 90 - 90 * animation_time; glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, &spread); glEnable(GL_LIGHT0); } -void materialFunc() { +void material_func() { // Add a diffuse component to our board. The diffuse reflection constant // is white (white light source produces white reflection). - GLfloat materialdiffuse[4] = {0.5, 0.5, 1.0, 1.0}; - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materialdiffuse); + const std::array material_diffuse{0.5F, 0.5F, 1.0F, 1.0F}; + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse.data()); // Add a specular component to our board. The specular reflection constant // is white (white light source produces white reflection). - GLfloat materialspecular[4] = {1.0, 1.0, 1.0, 1.0}; - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialspecular); + const std::array material_specular{1.0F, 1.0F, 1.0F, 1.0F}; + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular.data()); // Defines the shinyness (the exponent to the phong model). - GLfloat materialshininess[1] = {100.0}; - glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, materialshininess); + const std::array material_shininess{100.0F}; + glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, material_shininess.data()); } -void DrawSphere(int color) { - float colorNone[4] = {0.0, 0.0, 0.0, 0.0}; +void draw_sphere(int color) { + const std::array color_none{0.0F, 0.0F, 0.0F, 0.0F}; if (color == 0) { - float colorRed[4] = {1.0, 0.0, 0.0, 0.0}; - glColor4fv(colorRed); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorRed); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_red{1.0F, 0.0F, 0.0F, 0.0F}; + glColor4fv(color_red.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_red.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } if (color == 1) { - float colorWhite[4] = {1.0, 1.0, 1.0, 1.0}; - glColor4fv(colorWhite); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorWhite); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_white{1.0F, 1.0F, 1.0F, 1.0F}; + glColor4fv(color_white.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_white.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } if (color == 2) { - float colorBlack[4] = {0.0, 0.0, 0.0, 0.0}; - glColor4fv(colorBlack); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlack); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_black{0.0F, 0.0F, 0.0F, 0.0F}; + glColor4fv(color_black.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_black.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } if (color == 3) { - float colorGreen[4] = {0.0, 0.0, 1.0, 0.0}; - glColor4fv(colorGreen); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorGreen); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_green{0.0F, 0.0F, 1.0F, 0.0F}; + glColor4fv(color_green.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_green.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } if (color == 4) { - float colorBlue[4] = {0.0, 1.0, 0.0, 0.0}; - glColor4fv(colorBlue); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_blue{0.0F, 1.0F, 0.0F, 0.0F}; + glColor4fv(color_blue.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_blue.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } glBegin(GL_TRIANGLES); @@ -498,21 +487,21 @@ void DrawSphere(int color) { glEnd(); } -void DrawUnitCube(int color) { +void draw_unit_cube(int color) { if (color == 0) { - glBindTexture(GL_TEXTURE_2D, texture[0]); + glBindTexture(GL_TEXTURE_2D, textures[0]); } else if (color == 3) { - float colorNone[4] = {0.0, 0.0, 0.0, 0.0}; - float colorGreen[4] = {0.0, 0.0, 1.0, 0.0}; - glColor4fv(colorGreen); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorGreen); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_none{0.0F, 0.0F, 0.0F, 0.0F}; + const std::array color_green{0.0F, 0.0F, 1.0F, 0.0F}; + glColor4fv(color_green.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_green.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } else if (color == 4) { - float colorNone[4] = {0.0, 0.0, 0.0, 0.0}; - float colorBlue[4] = {0.0, 1.0, 0.0, 0.0}; - glColor4fv(colorBlue); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + const std::array color_none{0.0F, 0.0F, 0.0F, 0.0F}; + const std::array color_blue{0.0F, 1.0F, 0.0F, 0.0F}; + glColor4fv(color_blue.data()); + glMaterialfv(GL_FRONT, GL_DIFFUSE, color_blue.data()); + glMaterialfv(GL_FRONT, GL_SPECULAR, color_none.data()); } glBegin(GL_QUADS); @@ -571,7 +560,7 @@ void DrawUnitCube(int color) { if (color == 0) { glEnd(); - glBindTexture(GL_TEXTURE_2D, texture[1]); + glBindTexture(GL_TEXTURE_2D, textures[1]); glBegin(GL_QUADS); } @@ -615,10 +604,10 @@ void display() { glPushMatrix(); //////////////////// MOVING LIGHT!!!!! //////////////////// - glTranslatef(2.0F * std::sin(translatelight * 2.0F * PI / 360.0F), 0.0F, 0.0F); + glTranslatef(2.0F * std::sin(translate_light * 2.0F * PI / 360.0F), 0.0F, 0.0F); if (lighting) { - lightingFunc(); + lighting_func(); } glPopMatrix(); @@ -633,10 +622,10 @@ void display() { // Specify material for the board we'll be drawing. if (material) { - materialFunc(); + material_func(); } - DrawUnitCube(0); + draw_unit_cube(0); glPopMatrix(); glPushMatrix(); @@ -648,31 +637,31 @@ void display() { glTranslatef(0.0, 1.0, 0.0); // The centre glPushMatrix(); - ApplyTransformations(placex, placey, 0); - DrawSphere(0); + apply_transformations(place_x, place_y, 0); + draw_sphere(0); glPopMatrix(); for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { if (board_status[i][j] != 0) { glPushMatrix(); - ApplyTransformations(i - 9, j - 9, 0); - DrawSphere(board_status[i][j]); + apply_transformations(i - 9, j - 9, 0); + draw_sphere(board_status[i][j]); glPopMatrix(); } } } // THE REMOVAL OF PIECE PORTION - if (!L.empty()) { - for (const auto &piece : L) { + if (!captured_groups.empty()) { + for (const auto &piece : captured_groups) { jump_off(piece[0], piece[1], piece[2]); } - t += (TIME_INCREMENT * L.size()); + animation_time += (TIME_INCREMENT * captured_groups.size()); } - if (t > 1) { - t = 0; - L.clear(); + if (animation_time > 1) { + animation_time = 0; + captured_groups.clear(); } glPopMatrix(); @@ -685,7 +674,7 @@ void display() { glutSwapBuffers(); } -void ApplyTransformations(float indx, float indy, [[maybe_unused]] float z) { +void apply_transformations(float indx, float indy, [[maybe_unused]] float z) { int i; if (indx < 0) { for (i = 0; i < std::abs(indx); i++) { @@ -712,35 +701,39 @@ We can probably get rid of the first method, but I don't feel like doing that. */ -void ApplyTransformationGeneral(float indx, float indy, float z) { +void apply_transformation_general(float indx, float indy, float z) { glTranslatef(2 * indx, 10 * indy, 2 * z); } -void SetImages() { +void set_images() { ///////////////////// LOAD WOODEN TEXTURE ///////////////////// - ilBindImage(il[0]); + ilBindImage(image_ids[0]); ilLoadImage("images/wooden.jpg"); ilConvertImage(IL_RGB, IL_UNSIGNED_SHORT); - glBindTexture(GL_TEXTURE_2D, texture[0]); + glBindTexture(GL_TEXTURE_2D, textures[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, + static_cast(GL_CLAMP_TO_EDGE_VALUE)); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, + static_cast(GL_CLAMP_TO_EDGE_VALUE)); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGB, GL_UNSIGNED_SHORT, ilGetData()); ///////////////////// LOAD CHECKERBOARD TEXTURE ///////////////// - ilBindImage(il[1]); + ilBindImage(image_ids[1]); ilLoadImage("images/checkerboard.png"); ilConvertImage(IL_RGB, IL_UNSIGNED_SHORT); - glBindTexture(GL_TEXTURE_2D, texture[1]); + glBindTexture(GL_TEXTURE_2D, textures[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, + static_cast(GL_CLAMP_TO_EDGE_VALUE)); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, + static_cast(GL_CLAMP_TO_EDGE_VALUE)); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGB, GL_UNSIGNED_SHORT, ilGetData()); @@ -764,7 +757,7 @@ void clear_liberties() { } } -int check_liberties(int x, int y, int originx, int originy, int piece) { +auto check_liberties(int x, int y, int originx, int originy, int piece) -> int { if (x < 0 || x > 18 || y < 0 || y > 18) { return 0; } @@ -794,53 +787,53 @@ void jump_off(int x0, int z0, int color) { // For now, let's just assume they're all jumping to point (-4,0,-4) int x1 = -4; - float xt = x0 * (1 - t) + x1 * t; + float xt = x0 * (1 - animation_time) + x1 * animation_time; int y0 = 9; - float yt = -y0 * std::pow(t, 2.0F) + y0 * t + y0; + float yt = -y0 * std::pow(animation_time, 2.0F) + y0 * animation_time + y0; int z1 = -4; - float zt = z0 * (1 - t) + z1 * t; + float zt = z0 * (1 - animation_time) + z1 * animation_time; glPushMatrix(); - ApplyTransformationGeneral((xt - 9), (yt - 9), -(zt - 9)); - DrawSphere(color); + apply_transformation_general((xt - 9), (yt - 9), -(zt - 9)); + draw_sphere(color); // Draw Thigh glPushMatrix(); - if (t < 0.1) { - glRotatef(-80 + 80 * std::sin(t * 10.0F * PI), 1.0, 0.0, 1.0); + if (animation_time < 0.1) { + glRotatef(-80 + 80 * std::sin(animation_time * 10.0F * PI), 1.0, 0.0, 1.0); } else { glRotatef(-80, 1.0, 0.0, 1.0); } glScalef(2.0 / 5.0, 1.0, 2.0 / 5.0); glTranslatef(0.0, -1.0, 0.0); - DrawUnitCube(3); + draw_unit_cube(3); glScalef(5.0 / 2.0, 1.0, 5.0 / 2.0); // Draw Shin glPushMatrix(); glTranslatef(0, -0.8, 0); - if (t < 0.1) { - glRotatef(90 - 90 * std::sin(t * 10.0F * PI), 1.0, 0.0, 1.0); + if (animation_time < 0.1) { + glRotatef(90 - 90 * std::sin(animation_time * 10.0F * PI), 1.0, 0.0, 1.0); } else { glRotatef(90, 1.0, 0.0, 1.0); } glScalef(1.0 / 5.0, 1.0, 1.0 / 5.0); glTranslatef(0, -1.0, 0); - DrawUnitCube(4); + draw_unit_cube(4); glScalef(5.0, 1.0, 5.0); // Draw Ankle glPushMatrix(); glTranslatef(0, -1.0, 0); - if (t < 0.1) { - glRotatef(-100 + 100 * std::sin(t * 10.0F * PI), 1.0, 0.0, 1.0); + if (animation_time < 0.1) { + glRotatef(-100 + 100 * std::sin(animation_time * 10.0F * PI), 1.0, 0.0, 1.0); } else { glRotatef(-100, 1.0, 0.0, 1.0); } glScalef(1.0 / 2.0, 2.0 / 3.0, 1.0 / 2.0); glTranslatef(0, -1.0, 0); - DrawSphere(0); + draw_sphere(0); glPopMatrix(); glPopMatrix(); glPopMatrix(); @@ -863,7 +856,7 @@ void remove_block(int x, int y, int piece) { rm_piece.push_back(x); rm_piece.push_back(y); rm_piece.push_back(piece); - L.push_back(rm_piece); + captured_groups.push_back(rm_piece); remove_block(x - 1, y, piece); remove_block(x + 1, y, piece); @@ -872,7 +865,7 @@ void remove_block(int x, int y, int piece) { } } -int make_move(int x, int y, int piece) { +auto make_move(int x, int y, int piece) -> int { board_status[x][y] = piece; clear_liberties(); int other; @@ -929,12 +922,15 @@ void init() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); - double fovY = 45, aspect = 1.0, zNear = 0.1, zFar = 100; + double fov_y = 45; + double aspect = 1.0; + double z_near = 0.1; + double z_far = 100; - gluPerspective(fovY, // Field of view + gluPerspective(fov_y, // Field of view aspect, // Aspect ratio (width / height) - zNear, // Near plane - zFar); // Far plane + z_near, // Near plane + z_far); // Far plane // initialize modelview matrix glMatrixMode(GL_MODELVIEW); @@ -945,7 +941,7 @@ void init() { glEnable(GL_DEPTH_TEST); // enable depth testing // glEnable(GL_TEXTURE_2D); // enable textures - SetImages(); + set_images(); init_board(); // Create the "Board" } \ No newline at end of file diff --git a/scripts/install_dependencies.sh b/scripts/install_dependencies.sh index 410fa68..acbb869 100755 --- a/scripts/install_dependencies.sh +++ b/scripts/install_dependencies.sh @@ -21,4 +21,5 @@ run_with_sudo apt-get install -y \ cmake \ clang-tidy \ clang-format \ - cppcheck + cppcheck \ + iwyu # enforces include hygiene diff --git a/scripts/run_lint.sh b/scripts/run_lint.sh index d4175f1..b13d09a 100755 --- a/scripts/run_lint.sh +++ b/scripts/run_lint.sh @@ -19,6 +19,9 @@ clang-format --dry-run --Werror "${REPO_ROOT}/main.cpp" # Static analysis with clang-tidy; uses the compile database for accurate diagnostics. clang-tidy "${REPO_ROOT}/main.cpp" -p "${BUILD_DIR}" +# Include-What-You-Use to enforce proper includes; uses the compile database via the helper tool. +iwyu_tool -p "${BUILD_DIR}" "${REPO_ROOT}/main.cpp" + # Cross-check the same sources with cppcheck for complementary warnings. # ignore normalCheckLevelMaxBranches to reduce noise cppcheck --project="${BUILD_DIR}/compile_commands.json" --enable=warning,style,performance --error-exitcode=1 --suppress=normalCheckLevelMaxBranches