summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-12-10 19:50:26 +0500
committerilotterytea <iltsu@alright.party>2024-12-10 19:50:26 +0500
commitc664ddaf47e078219b848b9e2d9c3c4241420261 (patch)
tree0c30825531ecb5f4763d5fdc468a6128fafbe75e /src
parent0a50b20f43c2abec41ba35c01f26ed5fa650e9f9 (diff)
feat: render stars!
Diffstat (limited to 'src')
-rw-r--r--src/constants.h2
-rw-r--r--src/main.c23
-rw-r--r--src/star.c43
-rw-r--r--src/star.h7
4 files changed, 66 insertions, 9 deletions
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;
+ 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 <math.h>
+#include <stdlib.h>
+#include <time.h>
+
+#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();