project(swan) | project(swan) | ||||
find_package(SDL2 REQUIRED) | find_package(SDL2 REQUIRED) | ||||
find_package(PNG) | |||||
option(USE_CLANG_TIDY "Use clang-tidy for additional checks" ON) | option(USE_CLANG_TIDY "Use clang-tidy for additional checks" ON) | ||||
if(USE_CLANG_TIDY) | if(USE_CLANG_TIDY) | ||||
elseif(CMAKE_BUILD_TYPE STREQUAL Debug) | elseif(CMAKE_BUILD_TYPE STREQUAL Debug) | ||||
message(STATUS "Build mode: Debug") | message(STATUS "Build mode: Debug") | ||||
add_compile_options(-g -O0) | add_compile_options(-g -O0) | ||||
elseif(CMAKE_BUILD_TYPE STREQUAL Profile) | |||||
message(STATUS "Build mode: Profile") | |||||
add_compile_options(-g -Og -pg) | |||||
add_link_options(-pg) | |||||
elseif(CMAKE_BUILD_TYPE STREQUAL Release) | elseif(CMAKE_BUILD_TYPE STREQUAL Release) | ||||
message(STATUS "Build mode: Release") | message(STATUS "Build mode: Release") | ||||
add_compile_options(-O3 -flto) | add_compile_options(-O3 -flto) | ||||
src/main.cc) | src/main.cc) | ||||
target_link_libraries(swan libswan ${libraries}) | target_link_libraries(swan libswan ${libraries}) | ||||
add_executable(perlin-test | |||||
src/perlin-test.cc) | |||||
target_link_libraries(perlin-test libswan PNG::PNG ${libraries}) | |||||
set(assets | set(assets | ||||
assets/icon.png | assets/icon.png | ||||
assets/music/happy-1.wav) | assets/music/happy-1.wav) |
assets/tiles/air.png | assets/tiles/air.png | ||||
assets/tiles/stone.png | assets/tiles/stone.png | ||||
assets/tiles/dirt.png | assets/tiles/dirt.png | ||||
assets/tiles/leaves.png | |||||
assets/tiles/tree-trunk.png) | assets/tiles/tree-trunk.png) | ||||
foreach(a ${assets}) | foreach(a ${assets}) | ||||
configure_file("${a}" "${a}" COPYONLY) | configure_file("${a}" "${a}" COPYONLY) |
int grass_level = grassLevel(perlin_, pos.x); | int grass_level = grassLevel(perlin_, pos.x); | ||||
int stone_level = stoneLevel(perlin_, pos.x); | int stone_level = stoneLevel(perlin_, pos.x); | ||||
// Caves | |||||
if (pos.y > grass_level + 7 && perlin_.noise(pos.x / 43.37, pos.y / 16.37) > 0.2) | |||||
return tAir_; | |||||
if (pos.y > stone_level) | if (pos.y > stone_level) | ||||
return tStone_; | return tStone_; | ||||
else if (pos.y > grass_level) | else if (pos.y > grass_level) |
#include <swan/log.h> | |||||
#include <PerlinNoise/PerlinNoise.hpp> | |||||
#include <png++/png.hpp> | |||||
static int grassLevel(const siv::PerlinNoise &perlin, int x) { | |||||
return (int)(perlin.noise(x / 50.0, 0) * 13); | |||||
} | |||||
static int stoneLevel(const siv::PerlinNoise &perlin, int x) { | |||||
return (int)(perlin.noise(x / 50.0, 10) * 10) + 10; | |||||
} | |||||
int main() { | |||||
siv::PerlinNoise perlin = siv::PerlinNoise(100); | |||||
int x1 = -1600; | |||||
int x2 = 1600; | |||||
int y1 = -800; | |||||
int y2 = 800; | |||||
png::image<png::gray_pixel> image(x2 - x1 + 1, y2 - y1 + 1); | |||||
Swan::info << "perlin-test.png: " << (x2 - x1 + 1) << "x" << (y2 - y1 + 1); | |||||
for (int x = x1; x <= x2; ++x) { | |||||
int px = x - x1; | |||||
int grass_level = grassLevel(perlin, x); | |||||
int stone_level = stoneLevel(perlin, x); | |||||
for (int y = y1; y <= y2; ++y) { | |||||
int py = y - y1; | |||||
if (y > grass_level + 10) { | |||||
double l = perlin.noise(x / 41.37, y / 16.37); | |||||
if (l > 0.2) | |||||
image[py][px] = 255; | |||||
else | |||||
image[py][px] = 0; | |||||
} else if (y >= grass_level) { | |||||
image[py][px] = 0; | |||||
} else { | |||||
image[py][px] = 255; | |||||
} | |||||
if (y == grass_level) { | |||||
image[py][px] = 128; | |||||
} | |||||
} | |||||
} | |||||
image.write("perlin-test.png"); | |||||
} |