From 8f39d5810737a4ee5d56422fe9f3fe07abd47846 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 21 Jun 2014 04:05:16 +1000 Subject: [PATCH] A few tweaks --- mapgen/map_generator.cpp | 30 ++++++++++++++++++++++++++++-- mapgen/map_generator.hpp | 3 ++- src/shell_scene.cpp | 27 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/mapgen/map_generator.cpp b/mapgen/map_generator.cpp index 49ca22e..f0169dd 100644 --- a/mapgen/map_generator.cpp +++ b/mapgen/map_generator.cpp @@ -45,6 +45,27 @@ static double curve(double x) { return 3.0 * pow(x, 2.0) - 2.0 * pow(x, 3.0); } +//fix the overflow +static double curl(double x) { + if (x > 1.0) { + return -curl(x-1) +1; + } + if (x < 0.0) { + return curl(-x); + } + return x; +} + +static double cut(double x) { + if (x > 1.0) { + return 1.0; + } + if (x < 0.0) { + return 0.0; + } + return x; +} + //------------------------- //Public methods //------------------------- @@ -85,10 +106,15 @@ double MapGenerator::ScaledNoise(double x, double y, double width, double height return curve((b - a) / height); } -double MapGenerator::ScaleOctave(double x, double y, double width, double height, double octave) { +double MapGenerator::ScaledOctave(double x, double y, double width, double height, double octave) { double ret = 0; if (octave > 1) { - ret += ScaleOctave(x, y, width/2, height/2, octave-1); + ret += ScaledOctave(x, y, width/2, height/2, octave-1); } return ret / octave + ScaledNoise(x, y, width, height); +} + +double MapGenerator::GetPixel(double x, double y, double width, double height, double octave) { + //use this as a decorator function + return curl(ScaledOctave(x, y, width, height, octave)); } \ No newline at end of file diff --git a/mapgen/map_generator.hpp b/mapgen/map_generator.hpp index faf429b..3fab00e 100644 --- a/mapgen/map_generator.hpp +++ b/mapgen/map_generator.hpp @@ -34,7 +34,8 @@ public: Vector2 RawNoise(Vector2 const&); double Influence(Vector2 const& gridPoint, Vector2 const& queryPoint, double width, double height); double ScaledNoise(double x, double y, double width, double height); - double ScaleOctave(double x, double y, double width, double height, double octave); + double ScaledOctave(double x, double y, double width, double height, double octave); + double GetPixel(double x, double y, double width, double height, double octave); private: SimpleRNG rng; diff --git a/src/shell_scene.cpp b/src/shell_scene.cpp index 7cf9468..eb00fca 100644 --- a/src/shell_scene.cpp +++ b/src/shell_scene.cpp @@ -54,6 +54,31 @@ int convertToColour(SDL_PixelFormat* format, double x) { return SDL_MapRGB(format, 255*x, 255*x, 255*x); } +int convertToTerrain(SDL_PixelFormat* format, double x) { + //water + if (x < 0.4) { + return SDL_MapRGB(format, 0, 0, 255); + } + //sand + if (x < 0.5) { + return SDL_MapRGB(format, 255, 255, 0); + } + //grass + if (x < 0.6) { + return SDL_MapRGB(format, 0, 255, 0); + } + //forest + if (x < 0.8) { + return SDL_MapRGB(format, 0, 128, 0); + } + //snow + if (x < 0.8) { + return SDL_MapRGB(format, 255, 255, 255); + } + //dark snow + return SDL_MapRGB(format, 128, 128, 128); +} + ShellScene::ShellScene() { //test the generator int width = 256; @@ -66,7 +91,7 @@ ShellScene::ShellScene() { std::cout << "Beggining generation" << std::endl; for (int i = 0; i < image.GetSurface()->w; i++) { for (int j = 0; j < image.GetSurface()->h; j++) { - value = generator.ScaleOctave(i, j, width, height, 8); + value = generator.GetPixel(i, j, width, height, 8); colour = convertToColour(image.GetSurface()->format, value); setPixel(image.GetSurface(), i, j, colour); }