From f880b9687352640906c0156bb27160f7cbb96840 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Tue, 22 Nov 2022 01:32:46 +0100 Subject: [PATCH] =?UTF-8?q?Auswahl=20von=20Objekten=20m=C3=B6glich?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inputHandler.c | 107 ++++++++++++++++++++++++++++++++++++------------- main.c | 11 +---- sprite.c | 9 ++++- sprite.h | 1 + 4 files changed, 90 insertions(+), 38 deletions(-) diff --git a/inputHandler.c b/inputHandler.c index 1012804..8f0669e 100644 --- a/inputHandler.c +++ b/inputHandler.c @@ -2,9 +2,54 @@ #include "raylib.h" #include "sprite.h" #include +#include +#include //TODO: Macht es Sinn ein einzelnes "Game" struct zu haben, das alle möglichen Pointer hat zu allen arrays, camera, textures etc? // Man hat einen Übergabeparameter mit dem man dann alles verändern kann, man muss nicht alles was man verändern will einzeln übergeben +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); +} + +Vector2 GetRectangle(Vector2 rectStart){ + 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; + } + return rectStart; +} void mouseInput(InputHandler *inputHandler, Sprite *sprites, int *spriteAmount, Texture2D *texture, Camera2D *camera){ @@ -27,18 +72,49 @@ void mouseInput(InputHandler *inputHandler, Sprite *sprites, int *spriteAmount, inputHandler->pressed = 0; float width = GetMousePosition().x - inputHandler->rectStart.x; float height = GetMousePosition().y - inputHandler->rectStart.y; + + // Add Sprite if(width + height <= 1){ //printf("Klick\n"); SpriteAdd(sprites, spriteAmount, texture, inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2, inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2); } + + // Berechnung, welche Sprites ausgewählt wurden + Vector2 rect = GetRectangle(inputHandler->rectStart); + width = abs(width); + height = abs(height); + + printf("Auswahl: x: %f, y: %f, w: %f, h: %f\n", rect.x, rect.y, width, height); + + int k; + float deltaX; + float deltaY; + for(k = 0; k < *spriteAmount; k++){ + deltaX = (sprites+k)->x - (rect.x + camera->target.x); + deltaY = (sprites+k)->y - (rect.y + camera->target.y); + + printf("deltaX: %f, deltaY: %f\n", deltaX, deltaY); + + if(deltaX > 0 && deltaX < width && deltaY > 0 && deltaY < height){ + (sprites + k)->selected = 1; + printf("%d selected\n", k); + } + else{ + (sprites + k)->selected = 0; + } + } } if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){ - sprites->hasDestination = 1; - sprites->destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2; - sprites->destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2; + int i; + for(i=0;i<*spriteAmount;i++){ + if((sprites+i)->selected){ + (sprites+i)->hasDestination = 1; + (sprites+i)->destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2; + (sprites+i)->destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2; + } + } } - } @@ -55,27 +131,4 @@ 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/main.c b/main.c index e883a07..1623734 100644 --- a/main.c +++ b/main.c @@ -37,7 +37,7 @@ int main(){ int i; //int length = sizeof(sprites)/sizeof(sprites[0]); for(i=0; i < spriteAmount; i++){ - DrawTexture(*sprites[i].texture, sprites[i].x, sprites[i].y, WHITE); + DrawSprite(sprites + i); } EndMode2D(); @@ -50,13 +50,6 @@ int main(){ mouseInput(&inputHandler, sprites, &spriteAmount, &texture, &camera); keyboardInput(&inputHandler, &camera); -/* - if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ - printf("Klick\n"); - SpriteAdd(sprites, &j, &texture, cursor.x + camera.target.x, cursor.y + camera.target.y); - } - */ - // Sprites move towards their destination float movementSpeed = 10.0f; for(i=0; i < spriteAmount; i++){ @@ -77,8 +70,6 @@ int main(){ } } - - EndDrawing(); } diff --git a/sprite.c b/sprite.c index 3414573..4872b71 100644 --- a/sprite.c +++ b/sprite.c @@ -10,6 +10,7 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in (sprites + *spriteAmount) -> destX = x; (sprites + *spriteAmount) -> destY = y; (sprites + *spriteAmount) -> hasDestination = 0; + (sprites + *spriteAmount) -> selected = 0; (*spriteAmount)++; } else{ @@ -18,5 +19,11 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in } void DrawSprite(Sprite *sprite){ - DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); + + if(sprite->selected){ + DrawTexture(*sprite->texture, sprite->x, sprite->y, BLACK); + } + else{ + DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE); + } } diff --git a/sprite.h b/sprite.h index 1365b4b..b7e3ab8 100644 --- a/sprite.h +++ b/sprite.h @@ -9,6 +9,7 @@ typedef struct Sprite { float destX; float destY; int hasDestination; + int selected; } Sprite; void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y);