diff options
| author | nathan <nathansmith@disroot.org> | 2025-11-24 15:47:25 +0000 |
|---|---|---|
| committer | nathan <nathansmith@disroot.org> | 2025-11-24 15:47:25 +0000 |
| commit | 05624b044e59e70d10b786538f55e77c19cc3c8c (patch) | |
| tree | b51187d58403bcb2c00fc24fbca7643f176d7961 | |
| parent | eff6d7308d69ece8b89fe22e529b815f67a893e2 (diff) | |
| download | FindThings-main.tar.gz FindThings-main.tar.bz2 FindThings-main.zip | |
| -rw-r--r-- | src/game.c | 1 | ||||
| -rw-r--r-- | src/map.c | 138 | ||||
| -rw-r--r-- | src/map.h | 9 | ||||
| -rw-r--r-- | src/player.h | 2 | ||||
| -rw-r--r-- | src/settings.c | 6 | ||||
| -rw-r--r-- | src/settings.h | 2 |
6 files changed, 80 insertions, 78 deletions
@@ -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(); } @@ -1,112 +1,109 @@ #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) -{ - map->zoom = zoom; - map->heightmapRect.width = WORLD_IMAGE_WIDTH / zoom; - map->heightmapRect.height = WORLD_IMAGE_HEIGHT / zoom; -} - void initMap(Map* map, 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); 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) + 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() * 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. + Color color = GREEN; + color.a = settings->mapAlpha; + DrawTexture(game->world.heightmapTexture, 0.0, 0.0, color); - // Draw map. - DrawTexturePro(world->heightmapTexture, - map->heightmapRect, + 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, - 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 +130,8 @@ void updateMap(Map* map, Game* game) updateMapPreview(map, game); } + +void closeMap(Map* map) +{ + UnloadRenderTexture(map->render); +} @@ -4,21 +4,20 @@ #ifndef MAP_H #define MAP_H -#define MAP_ZOOM_MIN 1.0 +#define MAP_ZOOM_MIN 2.0 #define MAP_ZOOM_MAX 30.0 typedef struct { Rectangle rect; - Rectangle heightmapRect; - float zoom; + RenderTexture render; + Camera2D camera; Vector2 playerPosition; - float playerRotation; - bool isEnabled; bool isFullSize; } Map; void initMap(Map* map, 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..4ca38c4 100644 --- a/src/settings.c +++ b/src/settings.c @@ -19,9 +19,9 @@ 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}, + .mapPreviewWidth = 300.0, + .mapPreviewHeight = 300.0, + .mapAlpha = (unsigned char)255.0 * 0.85, .mapPreviewZoomDefault = 10.0, .mouseSpeed = 0.1, .forwardKey = KEY_W, diff --git a/src/settings.h b/src/settings.h index a76b0ab..94e21ad 100644 --- a/src/settings.h +++ b/src/settings.h @@ -37,7 +37,7 @@ typedef struct { bool isMapPreviewEnabledDefault; float mapPreviewWidth; float mapPreviewHeight; - Color mapColor; + unsigned char mapAlpha; float mapPreviewZoomDefault; // Controls. |
