aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game.c3
-rw-r--r--src/map.c172
-rw-r--r--src/map.h12
-rw-r--r--src/player.h2
-rw-r--r--src/settings.c12
-rw-r--r--src/settings.h6
-rw-r--r--src/utils.h2
7 files changed, 130 insertions, 79 deletions
diff --git a/src/game.c b/src/game.c
index e2cf7e8..9d7686f 100644
--- a/src/game.c
+++ b/src/game.c
@@ -83,7 +83,7 @@ void initGame(Game* game)
game->player.position = Vector3Scale(game->world.size, 0.5);
// Map.
- initMap(&game->map, &game->settings);
+ initMap(&game->map, &game->world, &game->settings);
disableGameCursor(game);
}
@@ -239,6 +239,7 @@ void closeGame(Game* game)
UnloadTexture(game->skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
UnloadModel(game->skybox);
freeWorld(game->world);
+ closeMap(&game->map);
CloseWindow();
}
diff --git a/src/map.c b/src/map.c
index 3b763f0..0ff87ea 100644
--- a/src/map.c
+++ b/src/map.c
@@ -1,112 +1,146 @@
#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, const Settings* settings)
{
- 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)
+ {
+ float height = (float)GRAY_VALUE(colors[index]) / 255.0;
+
+ // Limit color acount.
+ height *= settings->mapColorCount;
+ height = floorf(height);
+ height /= settings->mapColorCount;
+
+ Color lowColor = settings->mapLowColor;
+ Color highColor = settings->mapHighColor;
+
+ // Lerp between low and high colors.
+ colors[index].r = Lerp(lowColor.r, highColor.r, height);
+ colors[index].g = Lerp(lowColor.g, highColor.g, height);
+ colors[index].b = Lerp(lowColor.b, highColor.b, height);
+ }
+
+ image = (Image){
+ .data = colors,
+ .width = WORLD_IMAGE_WIDTH,
+ .height = WORLD_IMAGE_HEIGHT,
+ .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
+ .mipmaps = 1
+ };
+
+ 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, settings);
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});
-
- map->heightmapRect.x = map->playerPosition.x -
- (map->heightmapRect.width / 2.0);
- map->heightmapRect.y = map->playerPosition.y -
- (map->heightmapRect.height / 2.0);
+ // Zoom.
+ float mouseScroll = GetMouseWheelMove();
- // 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)
+ if (mouseScroll != 0.0)
{
- map->heightmapRect.x = WORLD_IMAGE_WIDTH - map->heightmapRect.width;
- outOfX = true;
- }
- else if (map->heightmapRect.x < 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() * settings->mapZoomSpeed));
+ 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 +167,9 @@ void updateMap(Map* map, Game* game)
updateMapPreview(map, game);
}
+
+void closeMap(Map* map)
+{
+ UnloadRenderTexture(map->render);
+ UnloadTexture(map->heightmap);
+}
diff --git a/src/map.h b/src/map.h
index 23faebe..bd581ba 100644
--- a/src/map.h
+++ b/src/map.h
@@ -4,21 +4,21 @@
#ifndef MAP_H
#define MAP_H
-#define MAP_ZOOM_MIN 1.0
+#define MAP_ZOOM_MIN 0.5
#define MAP_ZOOM_MAX 30.0
typedef struct {
Rectangle rect;
- Rectangle heightmapRect;
- float zoom;
+ RenderTexture render;
+ Camera2D camera;
+ Texture heightmap;
Vector2 playerPosition;
- float playerRotation;
-
bool isEnabled;
bool isFullSize;
} Map;
-void initMap(Map* map, const Settings* settings);
+void initMap(Map* map, const World* world, const Settings* settings);
void updateMap(Map* map, Game* game);
+void closeMap(Map* map);
#endif
diff --git a/src/player.h b/src/player.h
index acd9d7d..03b7141 100644
--- a/src/player.h
+++ b/src/player.h
@@ -4,7 +4,7 @@
#define PLAYER_H
#define PLAYER_HEIGHT 2.0
-#define PLAYER_SPEED 80.0
+#define PLAYER_SPEED 80.0 // 8.0
#define PLAYER_MAX_SELECT_DISTANCE 10.0
typedef struct {
diff --git a/src/settings.c b/src/settings.c
index 8baaa80..0d65432 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -19,10 +19,14 @@ Settings defaultSettings()
.crossHairThickness = 3.0,
.crossHairColor = BLUE,
.isMapPreviewEnabledDefault = true,
- .mapPreviewWidth = 700.0,
- .mapPreviewHeight = 700.0,
- .mapColor = (Color){255, 0.0, 0.0, (unsigned char)255.0 * 0.80},
- .mapPreviewZoomDefault = 10.0,
+ .mapPreviewWidth = 300.0,
+ .mapPreviewHeight = 300.0,
+ .mapAlpha = (unsigned char)255.0 * 0.80,
+ .mapPreviewZoomDefault = 3.0,
+ .mapLowColor = (Color){255, 255, 0, 255},
+ .mapHighColor = (Color){0, 0, 255, 255},
+ .mapColorCount = 7.0,
+ .mapZoomSpeed = 0.2,
.mouseSpeed = 0.1,
.forwardKey = KEY_W,
.backwardKey = KEY_S,
diff --git a/src/settings.h b/src/settings.h
index a76b0ab..d6416ba 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -37,8 +37,12 @@ typedef struct {
bool isMapPreviewEnabledDefault;
float mapPreviewWidth;
float mapPreviewHeight;
- Color mapColor;
+ unsigned char mapAlpha;
float mapPreviewZoomDefault;
+ Color mapLowColor;
+ Color mapHighColor;
+ float mapColorCount;
+ float mapZoomSpeed;
// Controls.
float mouseSpeed;
diff --git a/src/utils.h b/src/utils.h
index 75e27fd..fddfa9d 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -54,6 +54,8 @@
#define PRINT_VECTOR2(v) printf("%f %f\n", v.x, v.y)
#define PRINT_VECTOR3(v) printf("%f %f %f\n", v.x, v.y, v.z)
+#define GRAY_VALUE(color) ((float)(color.r + color.g + color.b) / 3.0)
+
#define RANDOM_DIRECTION_UNITS 4096
// Typedef hackery.