Auswahl von Objekten möglich

main
Jonathan Hager 3 years ago
parent ee0179f812
commit f880b96873
Signed by: JonathanHager
GPG Key ID: 34881E488569708C

@ -2,9 +2,54 @@
#include "raylib.h" #include "raylib.h"
#include "sprite.h" #include "sprite.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <math.h>
//TODO: Macht es Sinn ein einzelnes "Game" struct zu haben, das alle möglichen Pointer hat zu allen arrays, camera, textures etc? //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 // 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){ 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; inputHandler->pressed = 0;
float width = GetMousePosition().x - inputHandler->rectStart.x; float width = GetMousePosition().x - inputHandler->rectStart.x;
float height = GetMousePosition().y - inputHandler->rectStart.y; float height = GetMousePosition().y - inputHandler->rectStart.y;
// Add Sprite
if(width + height <= 1){ if(width + height <= 1){
//printf("Klick\n"); //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); 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)){ if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
sprites->hasDestination = 1; int i;
sprites->destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2; for(i=0;i<*spriteAmount;i++){
sprites->destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2; 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;
}
}
} }
} }
@ -56,26 +132,3 @@ void keyboardInput(InputHandler *inputHandler, Camera2D *camera){
(*camera).target.x -= 100.0f * GetFrameTime(); (*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);
}

@ -37,7 +37,7 @@ int main(){
int i; int i;
//int length = sizeof(sprites)/sizeof(sprites[0]); //int length = sizeof(sprites)/sizeof(sprites[0]);
for(i=0; i < spriteAmount; i++){ for(i=0; i < spriteAmount; i++){
DrawTexture(*sprites[i].texture, sprites[i].x, sprites[i].y, WHITE); DrawSprite(sprites + i);
} }
EndMode2D(); EndMode2D();
@ -50,13 +50,6 @@ int main(){
mouseInput(&inputHandler, sprites, &spriteAmount, &texture, &camera); mouseInput(&inputHandler, sprites, &spriteAmount, &texture, &camera);
keyboardInput(&inputHandler, &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 // Sprites move towards their destination
float movementSpeed = 10.0f; float movementSpeed = 10.0f;
for(i=0; i < spriteAmount; i++){ for(i=0; i < spriteAmount; i++){
@ -77,8 +70,6 @@ int main(){
} }
} }
EndDrawing(); EndDrawing();
} }

@ -10,6 +10,7 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in
(sprites + *spriteAmount) -> destX = x; (sprites + *spriteAmount) -> destX = x;
(sprites + *spriteAmount) -> destY = y; (sprites + *spriteAmount) -> destY = y;
(sprites + *spriteAmount) -> hasDestination = 0; (sprites + *spriteAmount) -> hasDestination = 0;
(sprites + *spriteAmount) -> selected = 0;
(*spriteAmount)++; (*spriteAmount)++;
} }
else{ else{
@ -18,5 +19,11 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in
} }
void DrawSprite(Sprite *sprite){ 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);
}
} }

@ -9,6 +9,7 @@ typedef struct Sprite {
float destX; float destX;
float destY; float destY;
int hasDestination; int hasDestination;
int selected;
} Sprite; } Sprite;
void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y); void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y);

Loading…
Cancel
Save