diff options
Diffstat (limited to 'src/world.c')
-rw-r--r-- | src/world.c | 56 |
1 files changed, 56 insertions, 0 deletions
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. |