diff options
author | nathan <nathansmith@disroot.org> | 2025-07-06 07:54:28 +0000 |
---|---|---|
committer | nathan <nathansmith@disroot.org> | 2025-07-06 07:54:28 +0000 |
commit | 404d0f1bdf95d5951bf2a5e77dca2f9dc18f373e (patch) | |
tree | db78303dc890b589696770ad713e801e6e713abf | |
parent | 025655dcf21ec40f1730097d3614114c704f5a17 (diff) | |
download | FindThings-404d0f1bdf95d5951bf2a5e77dca2f9dc18f373e.tar.gz FindThings-404d0f1bdf95d5951bf2a5e77dca2f9dc18f373e.tar.bz2 FindThings-404d0f1bdf95d5951bf2a5e77dca2f9dc18f373e.zip |
Cleaned up shit
-rw-r--r-- | src/game.c | 16 | ||||
-rw-r--r-- | src/game.h | 5 | ||||
-rw-r--r-- | src/player.c | 34 | ||||
-rw-r--r-- | src/player.h | 2 | ||||
-rw-r--r-- | src/world.c | 56 | ||||
-rw-r--r-- | src/world.h | 24 |
6 files changed, 90 insertions, 47 deletions
@@ -20,15 +20,8 @@ void initGame(Game* game) game->player = createPlayer(); game->player.position = (Vector3){50.0, 30.0, 50.0}; - // Heightmap. - Mesh heightmapMesh = GenMeshHeightmap(game->assets.images[HEIGHT_MAP_IMAGE], - (Vector3){1000.0, 30.0, 1000.0}); - game->heightmap = LoadModelFromMesh(heightmapMesh); - game->heightmap.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = - game->assets.textures[HEIGHT_MAP_TEXTURE]; - - game->heightmapColors = LoadImageColors( - game->assets.images[HEIGHT_MAP_IMAGE]); + // World. + game->world = createWorld(&game->assets); DisableCursor(); } @@ -46,7 +39,7 @@ void updateGameScene(Game* game) updatePlayer(&game->player, game); - DrawModel(game->heightmap, Vector3Zero(), 1.0, WHITE); + updateWorld(&game->world, game); EndMode3D(); } @@ -75,7 +68,6 @@ void updateGame(Game* game) void closeGame(Game* game) { closeAssets(&game->assets); - UnloadModel(game->heightmap); - UnloadImageColors(game->heightmapColors); + freeWorld(game->world); CloseWindow(); } @@ -2,6 +2,7 @@ #include "settings.h" #include "assets.h" #include "player.h" +#include "world.h" #ifndef GAME_H #define GAME_H @@ -16,9 +17,7 @@ struct Game { Settings settings; Assets assets; Player player; - - Model heightmap; - Color* heightmapColors; + World world; }; void initGame(Game* game); diff --git a/src/player.c b/src/player.c index 18d18bc..5b68f0a 100644 --- a/src/player.c +++ b/src/player.c @@ -18,37 +18,6 @@ Player createPlayer() }; } -// TODO: clean this all up -float getHeightOnMap(int x, int y, Model* map) -{ - int v = (y * 1023 + x) * 18; - float height = 0.0; - float c = 0.0; - - for (int n = 1; n < 18; n += 3) - { - height += map->meshes[0].vertices[v + n]; - ++c; - } - - return height / c; -} - -// TODO: subpixel hackery -// This is only for testing ideas. -void idkkk(Player* player, Game* game) -{ - Vector3 position = player->position; - Image* map = &game->assets.images[HEIGHT_MAP_IMAGE]; - - float toMapX = map->width / 1000.0; - float toMapY = map->height / 1000.0; - int pixelX = roundf(position.x * toMapX); - int pixelY = roundf(position.z * toMapY); - - player->position.y = getHeightOnMap(pixelX, pixelY, &game->heightmap) + 2.0; -} - // TODO: move magic numbers to settings void updatePlayer(Player* player, Game* game) { @@ -104,5 +73,6 @@ void updatePlayer(Player* player, Game* game) camera->position = player->position; camera->target = Vector3Add(player->position, player->direction); - idkkk(player, game); + player->position.y = getWorldHeightAtLocation(game->world, player->position.x, + player->position.z) + PLAYER_HEIGHT; } diff --git a/src/player.h b/src/player.h index bc061f8..0bd664e 100644 --- a/src/player.h +++ b/src/player.h @@ -3,6 +3,8 @@ #ifndef PLAYER_H #define PLAYER_H +#define PLAYER_HEIGHT 2.0 + typedef struct { Vector3 position; Vector3 direction; diff --git a/src/world.c b/src/world.c new file mode 100644 index 0000000..0a6b544 --- /dev/null +++ b/src/world.c @@ -0,0 +1,56 @@ +#include "world.h" +#include "game.h" + +World createWorld(const Assets* assets) +{ + World world; + world.size = (Vector3){1000.0, 30.0, 1000.0}; + world.image = &assets->images[HEIGHT_MAP_IMAGE]; + + Mesh mesh = GenMeshHeightmap(*world.image, world.size); + world.heightmap = LoadModelFromMesh(mesh); + world.heightmap.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = + assets->textures[HEIGHT_MAP_TEXTURE]; + + world.heightmapColors = LoadImageColors(assets->images[HEIGHT_MAP_IMAGE]); + + return world; +} + +void updateWorld(World* world, Game* game) +{ + DrawModel(world->heightmap, Vector3Zero(), 1.0, WHITE); +} + +void freeWorld(World world) +{ + UnloadModel(world.heightmap); + UnloadImageColors(world.heightmapColors); +} + +float getWorldPixelHeight(World world, int x, int y) +{ + int verticeStart = (y * (world.image->width - 1) + x) * 18; + float height = 0.0; + int count = 0; + + for (int index = 1; index < 18; index += 3) + { + height += world.heightmap.meshes[0].vertices[verticeStart + index]; + ++count; + } + + return (float)height / count; +} + +float getWorldHeightAtLocation(World world, int x, int y) +{ + float toMapX = (float)world.image->width / world.size.x; + float toMapY = (float)world.image->height / world.size.z; + int pixelX = roundf((float)x * toMapX); + int pixelY = roundf((float)y * toMapY); + + return getWorldPixelHeight(world, pixelX, pixelY); +} + +// Abortions are good. Get more abortions. diff --git a/src/world.h b/src/world.h new file mode 100644 index 0000000..d843094 --- /dev/null +++ b/src/world.h @@ -0,0 +1,24 @@ +#include "utils.h" +#include "assets.h" + +// This file is likely completely change. + +#ifndef WORLD_H +#define WORLD_H + +typedef struct { + Vector3 size; + Model heightmap; + const Image* image; + Color* heightmapColors; +} World; + +World createWorld(const Assets* assets); +void updateWorld(World* world, Game* game); +void freeWorld(World world); +// Dam, I wanta live in a free world ): + +float getWorldPixelHeight(World world, int x, int y); +float getWorldHeightAtLocation(World world, int x, int y); + +#endif |