From 81302d524bfaed88601248ef8647331daf74e1de Mon Sep 17 00:00:00 2001 From: Ty3r0X Date: Fri, 24 Nov 2023 22:27:24 +0200 Subject: [PATCH] If you touch the square, coords are spewed to stdout --- .clang-format | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ .clang-tidy | 6 +++++ .gitignore | 2 ++ config.mk | 1 + src/main.c | 9 ++++---- src/rectangle.c | 40 ++++++++++++++++++++++++++++++++ src/window.h | 4 ++-- 7 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 100644 .gitignore create mode 100644 src/rectangle.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..4e18d0e --- /dev/null +++ b/.clang-format @@ -0,0 +1,61 @@ +--- +Standard: Auto +Language: Cpp +BasedOnStyle: LLVM +ColumnLimit: 0 +IndentWidth: 8 +TabWidth: 8 +UseTab: ForIndentation + +AlignConsecutiveAssignments: true +AlignConsecutiveBitFields: true +AlignConsecutiveDeclarations: true +AlignConsecutiveMacros: true +AlignEscapedNewlines: Left +AlignOperands: AlignAfterOperator +AlignTrailingComments: true + +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Always +AllowShortLoopsOnASingleLine: true + +AlwaysBreakAfterDefinitionReturnType: TopLevel +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakBeforeMultilineStrings: false + +BinPackArguments: false +BinPackParameters: false + +BreakBeforeBinaryOperators: All +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: true + +BreakStringLiterals: false +BreakConstructorInitializers: AfterColon + +IndentCaseLabels: true +IndentPPDirectives: None +IndentGotoLabels: true + +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: true +IncludeBlocks: Regroup +InsertTrailingCommas: Wrapped +PointerAlignment: Right +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: true +SpaceBeforeParens: Always +SpacesInConditionalStatement: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false + +... diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..d732db4 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,6 @@ +--- +Checks: 'performance-*,portability-*,readability-*,bugprone-*,concurrency-*,llvm-*,clang-analyzer-core.*,clang-analyzer-nullability.*,clang-analyzer-optin.portability.UnixAPI,clang-analyzer-security.*,clang-analyzer-unix.*,clang-analyzer-valist.*,clang-analyzer-apiModeling.*,cert-*-c,-readability-isolate-declaration,-readability-braces-around-statements,-readability-named-parameter' +WarningsAsErrors: '' +HeaderFilterRegex: '' +AnalyzeTemporaryDtors: false +FormatStyle: file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..311fdd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +project +src/*.o diff --git a/config.mk b/config.mk index df5f84f..64f44bc 100644 --- a/config.mk +++ b/config.mk @@ -13,6 +13,7 @@ CFLAGS :=\ # Sources / Results SRC :=\ + src/rectangle.c \ src/init.c \ src/main.c diff --git a/src/main.c b/src/main.c index e7991a7..c887a18 100644 --- a/src/main.c +++ b/src/main.c @@ -25,6 +25,7 @@ #include #include #include +#define HEX_POKE 0xFF int main (int argc, char *argv[]) { @@ -40,10 +41,10 @@ main (int argc, char *argv[]) { rectangle->y = rand () % 500; SDL_SetRenderTarget (main_render, background); - SDL_SetRenderDrawColor (main_render, 0x00, 0x00, 0x00, 0x00); + SDL_SetRenderDrawColor (main_render, 0x00, HEX_POKE, 0x00, 0x00); SDL_RenderClear (main_render); SDL_RenderDrawRect (main_render, rectangle); - SDL_SetRenderDrawColor (main_render, 0xFF, 0x00, 0x00, 0x00); + SDL_SetRenderDrawColor (main_render, HEX_POKE, 0x00, 0x00, 0x00); SDL_RenderFillRect (main_render, rectangle); SDL_SetRenderTarget (main_render, NULL); SDL_RenderCopy (main_render, background, NULL, NULL); @@ -56,8 +57,8 @@ main (int argc, char *argv[]) { while (SDL_PollEvent (main_event)) { switch (main_event->type) { case SDL_MOUSEMOTION: - printf ("Mouse motion detected!\n"); - printf ("Current mouse position: (%d,%d)\t", main_event->motion.x, main_event->motion.y); + if (check_interaction_in_rect (main_event->motion.x, main_event->motion.y, rectangle)) + printf ("You touched the square! X position: %d, Y position %d\n", main_event->motion.x, main_event->motion.y); break; /* Yes I acknowledge this is a very hacky way to exit the program, my apologies */ diff --git a/src/rectangle.c b/src/rectangle.c new file mode 100644 index 0000000..9db873d --- /dev/null +++ b/src/rectangle.c @@ -0,0 +1,40 @@ + +/* ---------------------------------------------------------- + * @@@@@@@ @@@ @@@ @@@@@@ @@@@@@@ @@@@@@@@ @@@ @@@ + * @@@@@@@ @@@ @@@ @@@@@@@ @@@@@@@@ @@@@@@@@@@ @@@ @@@ + * @@! @@! !@@ @@@ @@! @@@ @@! @@@@ @@! !@@ + * !@! !@! @!! @!@ !@! @!@ !@! @!@!@ !@! @!! + * @!! !@!@! @!@!!@ @!@!!@! @!@ @! !@! !@@!@! + * !!! @!!! !!@!@! !!@!@! !@!!! !!! @!!! + * !!: !!: !!: !!: :!! !!:! !!! !: :!! + * :!: :!: :!: :!: !:! :!: !:! :!: !:! + * :: :: :: :::: :: ::: ::::::: :: :: ::: + * ---------------------------------------------------------- + * + * rectangle.c - defines special functions for rectangles + * + * Copyright (C) MMXXIII Patrick D. Covaci + * + * The source is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * FITNESS FOR A PARTICULAR PURPOSE. + * + */ + +#include "window.h" + +/* Function to check if a coordinate (x, y) is inside a rectangle */ +int +check_interaction_in_rect (int x_cursor, int y_cursor, struct SDL_Rect *rect) { + /* Check X coordinate is within rectangle range */ + if (x_cursor >= rect->x && x_cursor < (rect->x + rect->w)) { + /* Check Y coordinate is within rectangle range */ + if (y_cursor >= rect->y && y_cursor < (rect->y + rect->h)) { + /* X and Y is inside the rectangle */ + return 1; + } + } + + /* X or Y is outside the rectangle */ + return 0; +} diff --git a/src/window.h b/src/window.h index 7c6ec8a..1501c92 100644 --- a/src/window.h +++ b/src/window.h @@ -44,7 +44,7 @@ extern SDL_Texture *background; extern SDL_Rect *rectangle; extern SDL_Event *main_event; -int init_program (void); -void define_rect (SDL_Rect *rectangle, int pos_x, int pos_y, int width, int height); +int init_program (void); +int check_interaction_in_rect (int x_cursor, int y_cursor, struct SDL_Rect *rect); #endif