aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.c
blob: 33e57bcfa475790a74d2d57f24be46ff10bd74cf (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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "map.h"
#include "game.h"
#include "world.h"
#include "player.h"

void repositionMap(Map* map)
{
  map->rect.x = GetRenderWidth() - map->rect.width;
}

void initMapHeightmap(Map* map, const World* world)
{
  Image image = LoadImageFromTexture(world->heightmapTexture);
  Color* colors = LoadImageColors(image);
  UnloadImage(image);

  for (int index = 0; index < WORLD_IMAGE_WIDTH * WORLD_IMAGE_HEIGHT; ++index)
  {
    unsigned char grayValue = GRAY_VALUE(colors[index]);
    unsigned char alpha = colors[index].a;

    if (grayValue >= 170)
    {
      colors[index] = (Color){grayValue, 0, 0, alpha};
    }
    else if (grayValue >= 85)
    {
      colors[index] = (Color){0, grayValue * 2, grayValue, alpha};
    }
    else
    {
      colors[index] = (Color){grayValue, 0, grayValue, alpha};
    }
  }

  image = (Image){
    .data = colors,
    .width = WORLD_IMAGE_WIDTH,
    .height = WORLD_IMAGE_HEIGHT,
    .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
    .mipmaps = 1
  };

  ImageColorContrast(&image, 25.0);
  
  map->heightmap = LoadTextureFromImage(image);
  UnloadImage(image);
}

void initMap(Map* map, const World* world, const Settings* settings)
{
  *map = (Map){
    .rect = (Rectangle){0.0, 0.0, settings->mapPreviewWidth,
                        settings->mapPreviewHeight},
    .render = LoadRenderTexture(settings->mapPreviewWidth,
                                settings->mapPreviewHeight),
    .camera = (Camera2D){
      .offset = (Vector2){settings->mapPreviewWidth / 2.0,
                          settings->mapPreviewHeight / 2.0},
      .target = Vector2Zero(),
      .rotation = 0.0,
      .zoom = settings->mapPreviewZoomDefault
    },
    .playerPosition = Vector2Zero(),
    .isEnabled = settings->isMapPreviewEnabledDefault,
    .isFullSize = false
  };

  initMapHeightmap(map, world);
  repositionMap(map);
}

void drawMapPlayer(Map* map, Player* player, Vector2 position,
                   float width, float height)
{
  Vector2 playerPreview[3] = {
    (Vector2){0.0, height},
    (Vector2){width, -height},
    (Vector2){-width, -height}
  };

  // Move player to position.
  for (int index = 0; index < 3; ++index)
  {
    playerPreview[index] = Vector2Rotate(playerPreview[index],
                                         player->cameraAngle.x);
    playerPreview[index] = Vector2Add(playerPreview[index], position);
  }

  // Draw triangles.
  DrawTriangle(playerPreview[0], playerPreview[1], playerPreview[2], BLACK);
  DrawTriangleLines(playerPreview[0], playerPreview[1], playerPreview[2],
                    WHITE);
}

void updateMapPreview(Map* map, Game* game)
{
  const Settings* settings = &game->settings;
  const World* world = &game->world;

  // Player position.
  map->playerPosition = (Vector2){
    WORLD_IMAGE_WIDTH / world->size.x * game->player.position.x,
    WORLD_IMAGE_HEIGHT / world->size.z * game->player.position.z
  };

  // Zoom.
  float mouseScroll = GetMouseWheelMove();

  if (mouseScroll != 0.0)
  {
    map->camera.zoom = expf(logf(map->camera.zoom) +
                            ((float)GetMouseWheelMove() * 0.1f));
    map->camera.zoom = Clamp(map->camera.zoom, MAP_ZOOM_MIN, MAP_ZOOM_MAX);
  }

  // Camera follow player.
  map->camera.target = map->playerPosition;

  // Draw map scene.
  BeginTextureMode(map->render);
  BeginMode2D(map->camera);
  ClearBackground(BLANK);

  // Draw height map.
  DrawTexture(map->heightmap, 0.0, 0.0,
              (Color){255, 255, 255, settings->mapAlpha});

  EndMode2D();
  EndTextureMode();

  // Draw render texture.
  DrawTexturePro(map->render.texture,
                 (Rectangle){0.0, 0.0, map->render.texture.width,
                             -map->render.texture.height},
                 map->rect,
                 (Vector2){0.0, 0.0},
                 0.0,
                 WHITE);

  // Draw player.
  Vector2 mapCenter = (Vector2){map->rect.x + (map->rect.width / 2.0),
                                map->rect.y + (map->rect.height / 2.0)};
  drawMapPlayer(map, &game->player, mapCenter, 3.0, 6.0);
  
  DrawRectangleLinesEx(map->rect, 3.0, BLUE);
}

void updateMap(Map* map, Game* game)
{
  const Settings* settings = &game->settings;
  
  // Handle window resize.
  if (IsWindowResized())
  {
    repositionMap(map);
  }

  // Toggle preview.
  if (IsKeyPressed(settings->toggleMapPreviewKey))
  {
    map->isEnabled = !map->isEnabled;
  }
  
  if (!map->isEnabled)
  {
    return;
  }

  updateMapPreview(map, game);
}

void closeMap(Map* map)
{
  UnloadRenderTexture(map->render);
  UnloadTexture(map->heightmap);
}