From c664ddaf47e078219b848b9e2d9c3c4241420261 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 10 Dec 2024 19:50:26 +0500 Subject: feat: render stars! --- src/constants.h | 2 ++ src/main.c | 23 +++++++++++++++-------- src/star.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/star.h | 7 ++++++- 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 src/star.c (limited to 'src') diff --git a/src/constants.h b/src/constants.h index ecd5b1f..d1ca7f4 100644 --- a/src/constants.h +++ b/src/constants.h @@ -1 +1,3 @@ #define STAR_AMOUNT 1000 +#define STAR_START_POS_Z 20.0 +#define SPACE_SIZE 35.0 diff --git a/src/main.c b/src/main.c index 01678b9..3710ae1 100644 --- a/src/main.c +++ b/src/main.c @@ -3,17 +3,17 @@ #include "star.h" int main(int argc, char *argv[]) { + InitWindow(800, 600, "hyperspace (demo)"); + + SetTargetFPS(60); + Star *stars[STAR_AMOUNT] = {}; for (int i = 0; i < STAR_AMOUNT; i++) { - Star star = {{0, 0, 0}, {20, 20}, 1.0, BLACK}; - stars[i] = ☆ + Star s = StarCreate(); + stars[i] = &s; } - InitWindow(800, 600, "hyperspace (demo)"); - - SetTargetFPS(60); - while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(WHITE); @@ -22,9 +22,16 @@ int main(int argc, char *argv[]) { for (int i = 0; i < sizeof(stars) / sizeof(stars[0]); i++) { Star *star = stars[i]; + star->position.z -= star->velocity; + + if (star->position.z < 1.0) { + star->position = Generate3DPosition(); + } + + StarUpdate(star); - DrawRectangle(star->position.x, star->position.y, star->size.x, - star->size.y, star->color); + DrawRectangle(star->renderPosition.x, star->renderPosition.y, + star->size.x, star->size.y, star->color); } EndDrawing(); diff --git a/src/star.c b/src/star.c new file mode 100644 index 0000000..6251e03 --- /dev/null +++ b/src/star.c @@ -0,0 +1,43 @@ +#include "star.h" + +#include +#include +#include + +#include "constants.h" +#include "raylib.h" + +Vector3 Generate3DPosition() { + srand(time(0)); + double random = (double)rand() / RAND_MAX; + + double angle = random * 2.0 * M_PI; + double radius = + (GetScreenHeight() / SPACE_SIZE) + (random * GetScreenHeight()); + + float x = radius * sin(angle); + float y = radius * cos(angle); + + Vector3 v = {x, y, STAR_START_POS_Z}; + return v; +} + +Star StarCreate() { + return (Star){Generate3DPosition(), {1, 1}, {0, 0}, 0.25f, BLACK}; +} + +void StarUpdate(Star *star) { + float screen_center_x = GetScreenWidth() / 2.0; + float screen_center_y = GetScreenHeight() / 2.0; + + float x = star->position.x / star->position.z + screen_center_x; + float y = star->position.y / star->position.z + screen_center_y; + + star->renderPosition.x = x; + star->renderPosition.y = y; + + float size = (STAR_START_POS_Z - star->position.z) / (0.2 * star->position.z); + + star->size.x = size; + star->size.y = size; +} diff --git a/src/star.h b/src/star.h index bc215ee..db34db6 100644 --- a/src/star.h +++ b/src/star.h @@ -2,7 +2,12 @@ typedef struct Star { Vector3 position; - Vector2 size; + Vector2 size, renderPosition; float velocity; Color color; } Star; + +Star StarCreate(); +void StarUpdate(Star *star); + +Vector3 Generate3DPosition(); -- cgit v1.2.3