From f23eaec9e3971dfb5c4100f575a866267b6c48cb Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Mon, 21 Nov 2022 21:07:10 +0100 Subject: [PATCH] Rect drawing now working --- inputHandler.c | 31 ++++++++++++++++++++++++------- inputHandler.h | 2 ++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/inputHandler.c b/inputHandler.c index 7ad9785..1012804 100644 --- a/inputHandler.c +++ b/inputHandler.c @@ -20,13 +20,7 @@ void mouseInput(InputHandler *inputHandler, Sprite *sprites, int *spriteAmount, } if(inputHandler->pressed){ - float width = GetMousePosition().x - inputHandler->rectStart.x; - float height = GetMousePosition().y - inputHandler->rectStart.y; - if(width + height > 1){ - //TODO: Fallunterscheidung für negative width / height? - // Auslagern in eigene Funktion - DrawRectangleLines(inputHandler->rectStart.x, inputHandler->rectStart.y, width, height, GREEN); - } + DrawRect(inputHandler->rectStart, &(inputHandler->cursorPos)); } if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ @@ -61,4 +55,27 @@ void keyboardInput(InputHandler *inputHandler, Camera2D *camera){ if(IsKeyDown(KEY_A)){ (*camera).target.x -= 100.0f * GetFrameTime(); } +} + +void DrawRect(Vector2 rectStart, Vector2 *mousePosition){ + + float width = GetMousePosition().x - rectStart.x; + float height = GetMousePosition().y - rectStart.y; + + if(width < 0 && height >= 0){ + width *= -1; + rectStart.x -= width; + } + else if(height < 0 && width >= 0){ + height *= -1; + rectStart.y -= height; + } + else if(height < 0 && width < 0){ + height *= -1; + width *= -1; + rectStart.x -= width; + rectStart.y -= height; + } + + DrawRectangleLines(rectStart.x, rectStart.y, width, height, GREEN); } \ No newline at end of file diff --git a/inputHandler.h b/inputHandler.h index 5cda884..a13c63d 100644 --- a/inputHandler.h +++ b/inputHandler.h @@ -13,4 +13,6 @@ void mouseInput(InputHandler *inputHandler, Sprite *sprites, int *spriteAmount, void keyboardInput(InputHandler *inputHandler, Camera2D *camera); +void DrawRect(Vector2 rectStart, Vector2 *mousePosition); + #endif \ No newline at end of file