From a5b8ea449bbb6fe30a6bf27843eab1df6a21ccef Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 4 Jul 2025 08:06:42 -0600 Subject: Started getting walking on heightmap working --- src/assets.h | 4 ++-- src/game.c | 4 ++++ src/game.h | 8 +++++--- src/player.c | 22 ++++++++++++++++++++++ src/player.h | 2 +- src/settings.h | 3 ++- src/utils.c | 1 + src/utils.h | 4 ++++ 8 files changed, 41 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/assets.h b/src/assets.h index c5fb7ac..43c4b43 100644 --- a/src/assets.h +++ b/src/assets.h @@ -9,7 +9,7 @@ extern const char imageAssetPaths[IMAGE_ASSET_COUNT][FT_NAMEMAX]; extern const char textureAssetPaths[TEXTURE_ASSET_COUNT][FT_NAMEMAX]; -typedef int8_t Assetid; +typedef int8_t AssetId; // Image asset ids. enum { @@ -21,7 +21,7 @@ enum { HEIGHT_MAP_TEXTURE }; -typedef struct Assets { +typedef struct { Image images[IMAGE_ASSET_COUNT]; Texture textures[TEXTURE_ASSET_COUNT]; } Assets; diff --git a/src/game.c b/src/game.c index 778d05e..15e54e5 100644 --- a/src/game.c +++ b/src/game.c @@ -27,6 +27,9 @@ void initGame(Game* game) game->heightmap.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = game->assets.textures[HEIGHT_MAP_TEXTURE]; + game->heightmapColors = LoadImageColors( + game->assets.images[HEIGHT_MAP_IMAGE]); + DisableCursor(); } @@ -73,5 +76,6 @@ void closeGame(Game* game) { closeAssets(&game->assets); UnloadModel(game->heightmap); + UnloadImageColors(game->heightmapColors); CloseWindow(); } diff --git a/src/game.h b/src/game.h index f25c97f..8c9969a 100644 --- a/src/game.h +++ b/src/game.h @@ -6,18 +6,20 @@ #ifndef GAME_H #define GAME_H -typedef enum SceneID { +typedef enum { MAIN_MENU_SCENE, GAME_SCENE } SceneID; -typedef struct Game { +struct Game { SceneID sceneId; Settings settings; Assets assets; Player player; + Model heightmap; -} Game; + Color* heightmapColors; +}; void initGame(Game* game); void updateGame(Game* game); diff --git a/src/player.c b/src/player.c index ec6eaf2..880aa34 100644 --- a/src/player.c +++ b/src/player.c @@ -18,6 +18,26 @@ Player createPlayer() }; } +// TODO: subpixel hackery +void readoutMapHeight(Player* player, Game* game) +{ + Vector3 position = player->position; + Image* map = &game->assets.images[HEIGHT_MAP_IMAGE]; + + // Map player position to height map pixel. + int pixelX = (int)roundf(position.x) * (map->width / 100); + int pixelY = (int)roundf(position.z) * (map->height / 100); + pixelX = Clamp(pixelX, 0, map->width); + pixelY = Clamp(pixelY, 0, map->height); + + Color color = game->heightmapColors[pixelY * map->width + pixelX]; + float height = Vector3Length((Vector3){color.r, color.g, color.b}) + * (30.0 / 255.0) - 15.0; + + printf("%f\n", height); + player->position.y = height + 1.0; +} + // TODO: move magic numbers to settings void updatePlayer(Player* player, Game* game) { @@ -72,4 +92,6 @@ void updatePlayer(Player* player, Game* game) // Apply camera. camera->position = player->position; camera->target = Vector3Add(player->position, player->direction); + + readoutMapHeight(player, game); } diff --git a/src/player.h b/src/player.h index 4983752..bc061f8 100644 --- a/src/player.h +++ b/src/player.h @@ -3,7 +3,7 @@ #ifndef PLAYER_H #define PLAYER_H -typedef struct Player { +typedef struct { Vector3 position; Vector3 direction; Vector3 velocity; diff --git a/src/settings.h b/src/settings.h index 3576955..da67539 100644 --- a/src/settings.h +++ b/src/settings.h @@ -3,7 +3,7 @@ #ifndef SETTINGS_H #define SETTINGS_H -typedef struct Settings { +typedef struct { // Window. int windowWidth; int windowHeight; @@ -17,5 +17,6 @@ typedef struct Settings { } Settings; Settings defaultSettings(); +// Bite my shiny metal ass! #endif diff --git a/src/utils.c b/src/utils.c index 2916c30..635a1f7 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,2 +1,3 @@ #include "utils.h" +// Why does the universe feel strange to exist in? diff --git a/src/utils.h b/src/utils.h index e289189..db6f7a8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -38,4 +38,8 @@ typedef enum FTError { #define TOGGLE_BIT(b, n) (b ^ (0x1 << n)) #define HAS_FLAG(v, f) ((v & f) == f) +// Helpful for debugging +#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) + #endif -- cgit v1.2.3