Refactor, big with movement

main
JanEhehalt 3 years ago
parent b5b08abb8a
commit d8e98471e1

@ -0,0 +1,5 @@
{
"files.associations": {
"inputhandling.h": "c"
}
}

@ -0,0 +1,72 @@
struct InputHandler{
int pressed;
Vector2 rectStart;
int clicked;
Vector2 cursorPos;
} InputHandler;
//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
/*
cursor.x = GetMousePosition().x - texture.width / 2;
cursor.y = GetMousePosition().y - texture.height / 2;
*/
void mouseInput(struct InputHandler *inputHandler, struct Sprite *sprites, int *spriteAmount, Texture2D *texture, Camera2D *camera){
inputHandler->cursorPos.x = GetMousePosition().x;
inputHandler->cursorPos.y = GetMousePosition().y;
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
if(inputHandler->pressed == 0){
inputHandler->rectStart.x = GetMousePosition().x;
inputHandler->rectStart.y = GetMousePosition().y;
inputHandler->pressed = 1;
}
}
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);
}
}
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
inputHandler->pressed = 0;
float width = GetMousePosition().x - inputHandler->rectStart.x;
float height = GetMousePosition().y - inputHandler->rectStart.y;
if(width + height <= 1){
printf("Klick\n");
addSprite(sprites, spriteAmount, texture, inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2, inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2);
}
}
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
inputHandler->clicked = 1;
sprites->clicked = 1;
sprites->destX = inputHandler->cursorPos.x + (*camera).target.x - (texture->width)/2;
sprites->destY = inputHandler->cursorPos.y + (*camera).target.y - (texture->height)/2;
}
}
void keyboardInput(struct InputHandler *inputHandler, Camera2D *camera){
if(IsKeyDown(KEY_W)){
(*camera).target.y -= 100.0f * GetFrameTime();
}
if(IsKeyDown(KEY_S)){
(*camera).target.y += 100.0f * GetFrameTime();
}
if(IsKeyDown(KEY_D)){
(*camera).target.x += 100.0f * GetFrameTime();
}
if(IsKeyDown(KEY_A)){
(*camera).target.x -= 100.0f * GetFrameTime();
}
}

112
main.c

@ -1,42 +1,33 @@
#include "raylib.h"
#include "stdio.h"
#include "sprite.h"
#include "inputHandling.h"
#include "raymath.h"
int main(){
InitWindow(800, 450, "basic window");
Texture2D texture;
struct Sprite sprites[100];
int destX = 0;
int destY = 0;
int clicked = 0;
int pressed = 0;
Vector2 rectStart = {0,0};
texture = LoadTexture("assets/amulet.png");
int j = 0;
struct Sprite cursor = {&texture, 450, 225};
int spriteAmount = 0;
struct Sprite cursorSprite = {&texture, 450, 225};
struct InputHandler inputHandler;
Camera2D camera = { 0 };
camera.target = (Vector2){400, 225};
camera.rotation = 0.0f;
camera.zoom = 1.0f;
addSprite(sprites, &j, &texture, cursor.x + camera.target.x, cursor.y + camera.target.y);
addSprite(sprites, &spriteAmount, &texture, 400, 225);
SetTargetFPS(60);
while(!WindowShouldClose()){
BeginDrawing();
ClearBackground(RAYWHITE);
@ -44,90 +35,41 @@ int main(){
BeginMode2D(camera);
int i;
int length = sizeof(sprites)/sizeof(sprites[0]);
for(i=0; i < j; i++){
DrawTexture(*sprites[i].texture, sprites[i].x, sprites[i].y, WHITE);
for(i=0; i < spriteAmount; i++){
DrawSprite(&sprites[i]);
}
EndMode2D();
DrawTexture(*cursor.texture, cursor.x, cursor.y, WHITE);
cursor.x = GetMousePosition().x - texture.width / 2;
cursor.y = GetMousePosition().y - texture.height / 2;
/*
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
printf("Klick\n");
addSprite(sprites, &j, &texture, cursor.x + camera.target.x, cursor.y + camera.target.y);
}
*/
// Moving cursor Sprite to Mouse Pos and drawing it
cursorSprite.x = inputHandler.cursorPos.x - texture.width / 2;
cursorSprite.y = inputHandler.cursorPos.y - texture.height / 2;
DrawSprite(&cursorSprite);
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
if(pressed == 0){
rectStart.x = GetMousePosition().x;
rectStart.y = GetMousePosition().y;
pressed = 1;
}
}
// User Input Handling
mouseInput(&inputHandler, sprites, &spriteAmount, &texture, &camera);
keyboardInput(&inputHandler, &camera);
if(pressed){
float width = GetMousePosition().x - rectStart.x;
float height = GetMousePosition().y - rectStart.y;
if(width + height > 1){
//TODO: Fallunterscheidung für negative width / height?
// Auslagern in eigene Funktion
DrawRectangleLines(rectStart.x, rectStart.y, width, height, GREEN);
}
}
// Sprites move towards their destination
for(i=0; i < spriteAmount; i++){
if(sprites[i].clicked){
Vector2 movement = {sprites[i].destX - sprites->x, sprites[i].destY - sprites->y};
if(Vector2Length(movement) < 10.0f){
inputHandler.clicked = false;
sprites->clicked = false;
}
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
pressed = 0;
float width = GetMousePosition().x - rectStart.x;
float height = GetMousePosition().y - rectStart.y;
if(width + height <= 1){
printf("Klick\n");
addSprite(sprites, &j, &texture, cursor.x + camera.target.x, cursor.y + camera.target.y);
movement = Vector2Normalize(movement);
movement = Vector2Scale(movement, 10);
sprites->x += movement.x;
sprites->y += movement.y;
}
}
if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)){
clicked = 1;
destX = cursor.x + camera.target.x;
destY = cursor.y + camera.target.y;
}
if(IsKeyDown(KEY_W)){
camera.target.y -= 100.0f * GetFrameTime();
}
if(IsKeyDown(KEY_S)){
camera.target.y += 100.0f * GetFrameTime();
}
if(IsKeyDown(KEY_D)){
camera.target.x += 100.0f * GetFrameTime();
}
if(IsKeyDown(KEY_A)){
camera.target.x -= 100.0f * GetFrameTime();
}
if(clicked){
Vector2 movement = {destX - sprites->x, destY - sprites->y};
if(Vector2Length(movement) < 10.0f){
clicked = false;
}
movement = Vector2Normalize(movement);
movement = Vector2Scale(movement, 10);
sprites->x += movement.x;
sprites->y += movement.y;
}
EndDrawing();
}
CloseWindow();

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -2,16 +2,24 @@ struct Sprite{
Texture2D *texture;
float x;
float y;
float destX;
float destY;
int clicked;
} Sprite;
void addSprite(struct Sprite *cursors, int *j, Texture2D *texture, int x, int y){
if(*j < 100){
(cursors + *j) -> texture = texture;
(cursors + *j) -> x = x;
(cursors + *j) -> y = y;
(*j)++;
void addSprite(struct Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){
if(*spriteAmount < 100){
(sprites + *spriteAmount) -> texture = texture;
(sprites + *spriteAmount) -> x = x;
(sprites + *spriteAmount) -> y = y;
(*spriteAmount)++;
printf("SPRITE\n");
}
else{
printf("Voll\n");
}
}
void DrawSprite(struct Sprite *sprite){
DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE);
}
Loading…
Cancel
Save