summaryrefslogtreecommitdiff
path: root/examples/gravity.c
blob: 246aab144870f9b8b8a931d72aa98c055426d73f (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#if 0 /*
#/  ================================================================
#/
#/    gravity.c
#/
#/  ================================================================
#/
#/    Self-compilation shell script
#/
SRC=${0##*./}
BIN=${SRC%.*}
gcc                                         \
  -Wall -Wextra -Werror -pedantic           \
  -Wno-old-style-declaration                \
  -Wno-missing-braces                       \
  -Wno-unused-variable                      \
  -Wno-unused-but-set-variable              \
  -Wno-unused-parameter                     \
  -Wno-overlength-strings                   \
  -O3                                       \
  -fsanitize=undefined,address,leak -mshstk \
  -lX11 -lm                                 \
  -o $BIN $SRC &&                           \
  ./$BIN $@ && rm $BIN
exit $? # */
#endif

#include "../graphics.c"

typedef struct {
  f64 x;
  f64 y;
  f64 vx;
  f64 vy;
} Entity;

typedef struct {
  i64    time;
  i64    num_entities;
  Entity entities[10 * 1024 * 1024];
} World;

i32 main(i32 argc, c8 **argv) {
  (void) argc;
  (void) argv;

  u32 background = u32_from_rgb(.0f, .0f, .08f);

  platform = (Platform) {
    .title        = "Gravity",
    .frame_width  = 960,
    .frame_height = 720,
  };

  p_init();

  srand(time(0));

  static World world  = {0};
  i64          time_0 = p_time();

  while (!platform.done) {
    p_handle_events();

    i64 time_elapsed  = p_time() - time_0;
    time_0           += time_elapsed;

    if (platform.key_pressed[BUTTON_LEFT]) {
      i64 n = world.num_entities++;

      world.entities[n] = (Entity) {
        .x =   (f64) platform.cursor_x - (f64) platform.frame_width  / 2,
        .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2),
      };
    }

    if (time_elapsed > 0) {
      if (platform.key_down[BUTTON_RIGHT]) {
        for (i64 n = 0; n < time_elapsed; ++n)
          world.entities[world.num_entities + n] = (Entity) {
            .x =   (f64) platform.cursor_x - (f64) platform.frame_width  / 2,
            .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2),
          };

        world.num_entities += time_elapsed;
      }

      for (i64 n = 0; n < world.num_entities; ++n) {
        Entity *e = &world.entities[n];

        for (i64 k = 0; k < n; ++k) {
          Entity *u = &world.entities[k];

          f64 dx = e->x - u->x;
          f64 dy = e->y - u->y;
          f64 d = dx * dx + dy * dy;
          if (d < 1000.) continue;

          f64 r = sqrt(d);
          dx /= r;
          dy /= r;

          f64 a = (1. / d) * time_elapsed;

          if (d < 5000.)
            a = -a;
          
          e->vx -= dx * a;
          e->vy -= dy * a;
          u->vx += dx * a;
          u->vy += dy * a;
        }
      }

      for (i64 n = 0; n < world.num_entities; ++n) {
        Entity *e = &world.entities[n];

        e->x += e->vx * time_elapsed;
        e->y += e->vy * time_elapsed;

        f64 z = (M_PI * 2.0) * (rand() % 10000) * .0001;
        e->vx += .0001 * cos(z);
        e->vy += .0001 * sin(z);
      }

      world.time += time_elapsed;
    }

    for (i32 j = 0; j < platform.frame_height; ++j)
      for (i32 i = 0; i < platform.frame_width; ++i)
        platform.pixels[j * platform.frame_width + i] = background;

    for (i64 n = 0; n < world.num_entities; ++n) {
      Entity *e = &world.entities[n];

      i32 x = platform.frame_width  / 2 + (i32) floor(e->x + .5);
      i32 y = platform.frame_height / 2 - (i32) floor(e->y + .5);

      for (i32 j = y - 10; j <= y + 10; ++j) {
        if (j < 0 || j >= platform.frame_height) continue;
        for (i32 i = x - 10; i <= x + 10; ++i) {
          if (i < 0 || i >= platform.frame_width) continue;
          if ((i - x) * (i - x) + (j - y) * (j - y) > 100) continue;
          f64 v = (e->vx * e->vx + e->vy * e->vy) * 8.;
          platform.pixels[j * platform.frame_width + i] = u32_from_rgb(-.2 + v * 2., .1 + v * .7,  1. - v);
        }
      }
    }

    p_render_frame();
  }

  p_cleanup();
  return 0;
}