diff --git a/main.c b/main.c index 904b0e1..98cae18 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ #include #include "card.h" +#include "move.h" char *text = "こんにちわ、 世界!"; @@ -16,7 +17,26 @@ int main(int argc, char** argv) { Card *c = malloc(sizeof(Card)); c->position = (Vector2) { 200, 200 }; + Move *m = malloc(sizeof(Move)); + m->origin = c->position; + m->destination = c->position; + m->curve = CURVE_EASE_IN_OUT; + + float delta; + Vector2 mouse_position; + while (!WindowShouldClose()) { + delta = GetFrameTime(); + if (IsMouseButtonPressed(0)) { + mouse_position = GetMousePosition(); + m->origin = c->position; + m->destination = mouse_position; + m->current_time = 0.; + m->end_time = 1.; + } + + c->position = move_position(m, delta); + BeginDrawing(); ClearBackground(RAYWHITE); draw_card(c); diff --git a/move.c b/move.c new file mode 100644 index 0000000..e0a5d2f --- /dev/null +++ b/move.c @@ -0,0 +1,23 @@ +#include +#include "move.h" + +Vector2 move_position(Move *m, float delta) { + m->current_time += delta; + float percentage = m->current_time / m->end_time; + if (percentage < 0.0) percentage = 0.0; + else if (percentage > 1.0) percentage = 1.0; + + switch (m->curve) { + case CURVE_LINEAR: + return (Vector2) { + ((m->destination.x - m->origin.x) * percentage) + m->origin.x, + ((m->destination.y - m->origin.y) * percentage) + m->origin.y + }; + case CURVE_EASE_IN_OUT: + percentage = -(cos(PI * percentage) - 1) / 2; + return (Vector2) { + ((m->destination.x - m->origin.x) * percentage) + m->origin.x, + ((m->destination.y - m->origin.y) * percentage) + m->origin.y + }; + } +} diff --git a/move.h b/move.h new file mode 100644 index 0000000..298efd8 --- /dev/null +++ b/move.h @@ -0,0 +1,23 @@ +#ifndef _HF_MOVE_ +#define _HF_MOVE_ + +#include + +typedef enum Curve { + CURVE_LINEAR, + CURVE_EASE_IN_OUT, +} Curve; + +typedef struct Move Move; + +struct Move { + Vector2 origin; + Vector2 destination; + Curve curve; + float current_time; + float end_time; +}; + +Vector2 move_position(Move *m, float delta); + +#endif