diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk.cpp | 44 | ||||
-rw-r--r-- | src/chunk.hpp | 12 | ||||
-rwxr-xr-x | src/first_app.cpp | 6 |
3 files changed, 43 insertions, 19 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp index 2f7c969..0fc52fe 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -57,13 +57,12 @@ static std::map<uint8_t, Block> blocks{}; static std::map<std::string, uint32_t> texturesIds{}; static std::vector<xe::Image*> textures{}; -void loadTexture(const std::string& filePath) { - xe::Image* image = xe::Image::createImage(filePath, false); - texturesIds[filePath] = static_cast<uint32_t>(textures.size()); - textures.push_back(image); -} - uint32_t getTexture(const std::string& filePath) { + if(!texturesIds.count(filePath)) { + xe::Image* image = xe::Image::createImage(filePath, false); + texturesIds[filePath] = static_cast<uint32_t>(textures.size()); + textures.push_back(image); + } return texturesIds[filePath]; } @@ -72,11 +71,13 @@ std::vector<xe::Image*>& Chunk::getTextures() { } void Chunk::load() { - loadTexture(DIRT_TEXTURE); - loadTexture(GRASS_TEXTURE); - loadTexture(GRASS_TOP_TEXTURE); blocks[DIRT] = {{getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE)}}; blocks[GRASS] = {{getTexture(GRASS_TEXTURE), getTexture(GRASS_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(GRASS_TEXTURE), getTexture(GRASS_TEXTURE)}}; + blocks[GREEN] = {{getTexture(GRASS_TOP_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(GRASS_TOP_TEXTURE)}}; + blocks[STONE] = {{getTexture(STONE_TEXTURE), getTexture(STONE_TEXTURE), getTexture(STONE_TEXTURE), getTexture(STONE_TEXTURE), getTexture(STONE_TEXTURE), getTexture(STONE_TEXTURE)}}; + blocks[SNOW] = {{getTexture(SNOW_TEXTURE), getTexture(SNOW_TEXTURE), getTexture(SNOW_TEXTURE), getTexture(SNOW_TEXTURE), getTexture(SNOW_TEXTURE), getTexture(SNOW_TEXTURE)}}; + blocks[SAND] = {{getTexture(SAND_TEXTURE), getTexture(SAND_TEXTURE), getTexture(SAND_TEXTURE), getTexture(SAND_TEXTURE), getTexture(SAND_TEXTURE), getTexture(SAND_TEXTURE)}}; + blocks[WATER] = {{getTexture(WATER_TEXTURE), getTexture(WATER_TEXTURE), getTexture(WATER_TEXTURE), getTexture(WATER_TEXTURE), getTexture(WATER_TEXTURE), getTexture(WATER_TEXTURE)}}; } void Chunk::unload() { @@ -177,14 +178,25 @@ void Chunk::generate(Chunk* c) { for(int x = 0; x < 16; x++) { for(int z = 0; z < 16; z++) { - int height = perlin.octave2D_01((( x + c->gridX * 16) * 0.01), ((z + c->gridZ * 16) * 0.01), 4) * 20; - for(int y = 0; y < 256; y++) { - if(y == height){ - c->setBlock(x, y, z, GRASS); - } else if(y < height) + double noise = perlin.octave2D_01((( x + c->gridX * 16) * 0.01), ((z + c->gridZ * 16) * 0.01), 4); + int height = noise * 40; + for(int y = 0; y < std::max(height, WATER_LEVEL); y++) { + int difference = y - WATER_LEVEL; + if (difference < 0) { + c->setBlock(x, y, z, WATER); + } else if(difference < 3) { + c->setBlock(x, y, z, SAND); + } else if(difference < 5) { c->setBlock(x, y, z, DIRT); - else - c->setBlock(x, y, z, AIR); + } else if(difference < 6) { + c->setBlock(x, y, z, GRASS); + } else if(difference < 10) { + c->setBlock(x, y, z, GREEN); + } else if(difference < 16) { + c->setBlock(x, y, z, STONE); + } else if(difference < 18) { + c->setBlock(x, y, z, SNOW); + } } } } diff --git a/src/chunk.hpp b/src/chunk.hpp index 734c07b..8d1f03c 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -13,15 +13,27 @@ #include <memory> #include <string> #include <map> +#include <algorithm> #define INVALID -1 #define AIR 0 #define DIRT 1 #define GRASS 2 +#define GREEN 3 +#define STONE 4 +#define SNOW 5 +#define SAND 6 +#define WATER 7 #define DIRT_TEXTURE "res/image/dirt.png" #define GRASS_TEXTURE "res/image/grass.png" #define GRASS_TOP_TEXTURE "res/image/grass_top.png" +#define STONE_TEXTURE "res/image/stone.png" +#define SNOW_TEXTURE "res/image/snow.png" +#define SAND_TEXTURE "res/image/sand.png" +#define WATER_TEXTURE "res/image/water.png" + +static constexpr int WATER_LEVEL = 20; namespace app { diff --git a/src/first_app.cpp b/src/first_app.cpp index e677d06..a20dcb0 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -18,9 +18,9 @@ void FirstApp::run() { SimpleRenderer renderer{xeEngine, Chunk::getTextures()}; - xe::Sound sound{"res/sound/when_the_world_ends.wav"}; - sound.setLooping(true); - sound.play(); + // xe::Sound sound{"res/sound/when_the_world_ends.wav"}; + // sound.setLooping(true); + // sound.play(); KeyboardMovementController cameraController{xeEngine.getInput(), viewerObject}; |