aboutsummaryrefslogtreecommitdiffstats
path: root/src/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/map.c')
-rw-r--r--src/map.c174
1 files changed, 108 insertions, 66 deletions
diff --git a/src/map.c b/src/map.c
index 3b763f0..33e57bc 100644
--- a/src/map.c
+++ b/src/map.c
@@ -1,112 +1,148 @@
#include "map.h"
#include "game.h"
#include "world.h"
+#include "player.h"
void repositionMap(Map* map)
{
map->rect.x = GetRenderWidth() - map->rect.width;
}
-void zoomMap(Map* map, float zoom)
+void initMapHeightmap(Map* map, const World* world)
{
- map->zoom = zoom;
- map->heightmapRect.width = WORLD_IMAGE_WIDTH / zoom;
- map->heightmapRect.height = WORLD_IMAGE_HEIGHT / zoom;
+ 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 Settings* settings)
+void initMap(Map* map, const World* world, const Settings* settings)
{
*map = (Map){
.rect = (Rectangle){0.0, 0.0, settings->mapPreviewWidth,
settings->mapPreviewHeight},
- .isEnabled = settings->isMapPreviewEnabledDefault,
- .isFullSize = false,
+ .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(),
- .playerRotation = 0.0
+ .isEnabled = settings->isMapPreviewEnabledDefault,
+ .isFullSize = false
};
- zoomMap(map, settings->mapPreviewZoomDefault);
+ initMapHeightmap(map, world);
repositionMap(map);
}
-void updateMapPreview(Map* map, Game* game)
+void drawMapPlayer(Map* map, Player* player, Vector2 position,
+ float width, float height)
{
- const Settings* settings = &game->settings;
- const World* world = &game->world;
-
- float mouseWheel = GetMouseWheelMove();
+ Vector2 playerPreview[3] = {
+ (Vector2){0.0, height},
+ (Vector2){width, -height},
+ (Vector2){-width, -height}
+ };
- if (mouseWheel != 0.0)
+ // Move player to position.
+ for (int index = 0; index < 3; ++index)
{
- zoomMap(map, Clamp(map->zoom + mouseWheel, MAP_ZOOM_MIN, MAP_ZOOM_MAX));
+ 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
};
- Vector2 playerPreviewPosition = Vector2Add(
- (Vector2){map->rect.x, map->rect.y},
- (Vector2){map->rect.width / 2.0, map->rect.height / 2.0});
+ // Zoom.
+ float mouseScroll = GetMouseWheelMove();
- map->heightmapRect.x = map->playerPosition.x -
- (map->heightmapRect.width / 2.0);
- map->heightmapRect.y = map->playerPosition.y -
- (map->heightmapRect.height / 2.0);
-
- // Preview out of bounds.
- bool outOfX = false;
- bool outOfY = false;
- float oldX = map->heightmapRect.x;
- float oldY = map->heightmapRect.y;
-
- if (map->heightmapRect.x + map->heightmapRect.width > WORLD_IMAGE_WIDTH)
- {
- map->heightmapRect.x = WORLD_IMAGE_WIDTH - map->heightmapRect.width;
- outOfX = true;
- }
- else if (map->heightmapRect.x < 0.0)
+ if (mouseScroll != 0.0)
{
- map->heightmapRect.x = 0.0;
- outOfX = true;
- }
-
- if (map->heightmapRect.y + map->heightmapRect.height > WORLD_IMAGE_HEIGHT)
- {
- map->heightmapRect.y = WORLD_IMAGE_HEIGHT - map->heightmapRect.height;
- outOfY = true;
- }
- else if (map->heightmapRect.y < 0.0)
- {
- map->heightmapRect.y = 0.0;
- outOfY = true;
+ 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);
}
- if (outOfX)
- {
- playerPreviewPosition.x += (oldX - map->heightmapRect.x) *
- (map->rect.width / map->heightmapRect.width);
- }
- if (outOfY)
- {
- playerPreviewPosition.y += (oldY - map->heightmapRect.y) *
- (map->rect.height / map->heightmapRect.height);
- }
+ // 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 map.
- DrawTexturePro(world->heightmapTexture,
- map->heightmapRect,
+ // 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,
- settings->mapColor);
+ WHITE);
// Draw player.
- DrawCircleV(playerPreviewPosition, 3.0, BLUE);
-
- // Outline.
+ 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);
}
@@ -133,3 +169,9 @@ void updateMap(Map* map, Game* game)
updateMapPreview(map, game);
}
+
+void closeMap(Map* map)
+{
+ UnloadRenderTexture(map->render);
+ UnloadTexture(map->heightmap);
+}