summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 37788de18f7c21ea06d8999d51a10df5f12f5451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdlib.h>

#include "constants.h"
#include "raylib.h"
#include "star.h"
#include "stdlib.h"
#include "time.h"

int CompareStarsByZPosition(const void* a, const void* b) {
  return ((struct Star*)b)->position.z - ((struct Star*)a)->position.z;
}

int main(int argc, char* argv[]) {
  bool mouse_control = false;
  srand(time(0));

  InitWindow(800, 600, "hyperspace (demo)");

  SetTargetFPS(60);

  Star stars[STAR_AMOUNT] = {};

  Texture2D* texture = NULL;

  if (FileExists("star.png")) {
    Image image = LoadImage("star.png");
    Texture2D loadedTexture = LoadTextureFromImage(image);
    texture = &loadedTexture;
    UnloadImage(image);
  }

  for (int i = 0; i < STAR_AMOUNT; i++) {
    stars[i] = StarCreate(texture);
  }

  while (!WindowShouldClose()) {
    BeginDrawing();
    ClearBackground(BLACK);
    DrawText("hi world!", GetScreenWidth() / 2.0 - 16 * 4,
             GetScreenHeight() / 2.0 - 16, 32, BLACK);

    qsort(stars, sizeof(stars) / sizeof(stars[0]), sizeof(struct Star),
          CompareStarsByZPosition);

    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();
      }

      float screen_center_x =
          mouse_control ? GetMouseX() : GetScreenWidth() / 2.0;
      float screen_center_y =
          mouse_control ? GetMouseY() : GetScreenHeight() / 2.0;

      StarUpdate(star, screen_center_x, screen_center_y);

      if (star->texture == NULL) {
        DrawRectangle(star->renderPosition.x, star->renderPosition.y,
                      star->size.x, star->size.y, star->color);
      } else {
        DrawTextureEx(*star->texture, star->renderPosition, 0.0,
                      star->size.x / 10.0, star->color);
      }
    }

    EndDrawing();

    // Listening for input
    if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
      mouse_control = !mouse_control;
      if (mouse_control)
        HideCursor();
      else
        ShowCursor();
    }
  }

  return 0;
}