aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
blob: 6d09293101cd926dcdca0fa35aa4131822a09ae6 (plain) (blame)
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
#include "utils.h"

Vector2 randomDirection2(int seed, int* nextSeed)
{
  Vector2 direction;
  direction.x = FT_RANDOM16(seed) % RANDOM_DIRECTION_UNITS;
  direction.y = FT_RANDOM16(seed) % RANDOM_DIRECTION_UNITS;
  direction.x -= RANDOM_DIRECTION_UNITS / 2.0;
  direction.y -= RANDOM_DIRECTION_UNITS / 2.0;

  if (nextSeed != NULL)
  {
    *nextSeed = seed;
  }

  return Vector2Normalize(direction);
}

Vector3 randomDirection3(int seed, int* nextSeed)
{
  Vector3 direction;
  direction.x = FT_RANDOM16(seed) % RANDOM_DIRECTION_UNITS;
  direction.y = FT_RANDOM16(seed) % RANDOM_DIRECTION_UNITS;
  direction.z = FT_RANDOM16(seed) % RANDOM_DIRECTION_UNITS;
  direction.x -= RANDOM_DIRECTION_UNITS / 2.0;
  direction.y -= RANDOM_DIRECTION_UNITS / 2.0;
  direction.z -= RANDOM_DIRECTION_UNITS / 2.0;

  if (nextSeed != NULL)
  {
    *nextSeed = seed;
  }

  return Vector3Normalize(direction);
}

Image generateCubemapImage(bool** cubemap, int width, int height)
{
  // Allocate pixel data.
  Image image = (Image){
    .data = FT_CALLOC(width * height, sizeof(Color)),
    .width = width,
    .height = height,
    .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
    .mipmaps = 1
  };

  if (image.data == NULL)
  {
    ALLOCATION_ERROR;
    return image;
  }

  // Convert cubemap to image data.
  int index = 0;

  for (int y = 0; y < height; ++y)
  {
    for (int x = 0; x < width; ++x)
    {
      ((Color*)image.data)[index] = cubemap[y][x] ? BLACK : WHITE;
      ++index;
    }
  }

  return image;
}

// Why does the universe feel strange to exist in?