From fbaf15337f9ee1cd96a8cf80c47d212d6cc96828 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 18 Jun 2014 01:00:31 +1000 Subject: [PATCH] Working on a new generator class, nearly there --- makefile | 1 + mapgen/makefile | 37 +++++++++++++++++ mapgen/map_generator.cpp | 90 ++++++++++++++++++++++++++++++++++++++++ mapgen/map_generator.hpp | 39 +++++++++++++++++ pseudocode.txt | 5 +-- src/makefile | 2 +- src/shell_scene.cpp | 6 +-- src/shell_scene.hpp | 4 +- {src => utils}/image.cpp | 0 {src => utils}/image.hpp | 0 utils/maths.cpp | 9 ++++ utils/maths.hpp | 1 + utils/simple_rng.hpp | 10 ++--- 13 files changed, 190 insertions(+), 14 deletions(-) create mode 100644 mapgen/makefile create mode 100644 mapgen/map_generator.cpp create mode 100644 mapgen/map_generator.hpp rename {src => utils}/image.cpp (100%) rename {src => utils}/image.hpp (100%) diff --git a/makefile b/makefile index f17f0b7..84bd6cb 100644 --- a/makefile +++ b/makefile @@ -11,6 +11,7 @@ OUTDIR=out all: $(OUTDIR) $(MAKE) -C utils + $(MAKE) -C mapgen $(MAKE) -C src $(OUTDIR): diff --git a/mapgen/makefile b/mapgen/makefile new file mode 100644 index 0000000..43d6507 --- /dev/null +++ b/mapgen/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../utils +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/mapgen/map_generator.cpp b/mapgen/map_generator.cpp new file mode 100644 index 0000000..31ced51 --- /dev/null +++ b/mapgen/map_generator.cpp @@ -0,0 +1,90 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "map_generator.hpp" + +#include "maths.hpp" + +#include + +//it's either this, or dealing with floating points +struct Coord { + int x, y; +}; + +//std::abs() only uses floating points +int absolute(int i) { + return i > 0 ? i : -i; +} + +//calculate how far away a query point is from a grid point +Coord calcDist(Coord gridPoint, Coord queryPoint) { + int i = absolute(queryPoint.x - gridPoint.x); + int j = absolute(queryPoint.y - gridPoint.y); + return {i, j}; +} + +//calculate the amount of influence a distance will have within a certain sized area +int calcInfluence(Coord dist, int width, int height) { + //inverted distance + Coord i = {width - dist.x, height - dist.y}; + //essentially calculating the length + return sqrt(i.x*i.x + i.y*i.y); +} + +//determine the scaled value from the grid point +int calcScale(int influence, int gridValue, int pointValue) { + /* TODO + * (influence / {width, height}.Length()) //percentage to interpolate + * poingValue - gridValue //difference + * return percentage * difference //scale + */ + return gridValue; +} + +int MapGenerator::Noise(int x, int y, int width, int height) { + Coord queryPoint = {x, y}; + + //the "grid points" + Coord tl = {snap(x, width), snap(y, height)}; + Coord tr = {tl.x + width, tl.y}; + Coord bl = {tl.x, tl.y + height}; + Coord br = {tl.x + width, tl.y + height}; + + //get the distance from the query, and subsequently the influence + int tlInfluence = calcInfluence(calcDist(tl, queryPoint), width, height); + int trInfluence = calcInfluence(calcDist(tr, queryPoint), width, height); + int blInfluence = calcInfluence(calcDist(bl, queryPoint), width, height); + int brInfluence = calcInfluence(calcDist(br, queryPoint), width, height); + + //now combine the grid's values with the influence values + int tlScale = calcScale(tlInfluence, RawNoise(tl.x, tl.y), RawNoise(queryPoint.x, queryPoint.y)); + int trScale = calcScale(trInfluence, RawNoise(tr.x, tr.y), RawNoise(queryPoint.x, queryPoint.y)); + int blScale = calcScale(blInfluence, RawNoise(bl.x, bl.y), RawNoise(queryPoint.x, queryPoint.y)); + int brScale = calcScale(brInfluence, RawNoise(br.x, br.y), RawNoise(queryPoint.x, queryPoint.y)); + + //Finally, apply each scale to the raw value, resulting in the height map + return tlScale + trScale + blScale + brScale + RawNoise(queryPoint.x, queryPoint.y); +} + +int MapGenerator::RawNoise(int x, int y) { + return (x * 11235 + y * 81321 + 3455) % 256; +} \ No newline at end of file diff --git a/mapgen/map_generator.hpp b/mapgen/map_generator.hpp new file mode 100644 index 0000000..49cdb72 --- /dev/null +++ b/mapgen/map_generator.hpp @@ -0,0 +1,39 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef MAPGENERATOR_HPP_ +#define MAPGENERATOR_HPP_ + +#include "simple_rng.hpp" + +class MapGenerator { +public: + MapGenerator() = default; + ~MapGenerator() = default; + + int Noise(int x, int y, int width, int height); + int RawNoise(int x, int y); + +private: + SimpleRNG rng; +}; + +#endif \ No newline at end of file diff --git a/pseudocode.txt b/pseudocode.txt index 3e24bdf..25391a9 100644 --- a/pseudocode.txt +++ b/pseudocode.txt @@ -1,4 +1 @@ -All terrain is grass by default - -produce a hightmap for the terrain - +All coordinates have a random value assigned to them \ No newline at end of file diff --git a/src/makefile b/src/makefile index 81bd1cb..690b944 100644 --- a/src/makefile +++ b/src/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../utils +INCLUDES+=. ../utils ../mapgen LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/src/shell_scene.cpp b/src/shell_scene.cpp index 9ab2235..c8a62ac 100644 --- a/src/shell_scene.cpp +++ b/src/shell_scene.cpp @@ -37,13 +37,13 @@ ShellScene::ShellScene() { int height = 80; image.CreateSurface(GetScreen()->w, GetScreen()->h); - double mod = 0.0; + int value = 0; int colour = 0; std::cout << "Beggining generation" << std::endl; for (int i = 0; i < image.GetSurface()->w; i++) { for (int j = 0; j < image.GetSurface()->h; j++) { -// mod = generator.Noise(i, j, width, height); - colour = SDL_MapRGB(image.GetSurface()->format, 255*mod, 255*mod, 255*mod); + value = generator.Noise(i, j, width, height); + colour = SDL_MapRGB(image.GetSurface()->format, value, value, value); setPixel(image.GetSurface(), i, j, colour); } } diff --git a/src/shell_scene.hpp b/src/shell_scene.hpp index 5689ed7..b3882ed 100644 --- a/src/shell_scene.hpp +++ b/src/shell_scene.hpp @@ -24,6 +24,8 @@ #include "base_scene.hpp" +#include "map_generator.hpp" + #include "image.hpp" class ShellScene : public BaseScene { @@ -47,7 +49,7 @@ protected: void KeyUp(SDL_KeyboardEvent const&); //members -// PerlinNoise generator; + MapGenerator generator; Image image; }; diff --git a/src/image.cpp b/utils/image.cpp similarity index 100% rename from src/image.cpp rename to utils/image.cpp diff --git a/src/image.hpp b/utils/image.hpp similarity index 100% rename from src/image.hpp rename to utils/image.hpp diff --git a/utils/maths.cpp b/utils/maths.cpp index a5952a7..0b14504 100644 --- a/utils/maths.cpp +++ b/utils/maths.cpp @@ -30,6 +30,15 @@ double snap(double x, double base) { return floor(x / base) * base; } +int snap(int x, int base) { + //snap to a grid (integer devision version) + if (x < 0) { + ++x; + return x / base * base - base; + } + return x / base * base; +} + double scalarProduct(Vector2 lhs, Vector2 rhs) { //the dot product return lhs.x * rhs.x + lhs.y * rhs.y; diff --git a/utils/maths.hpp b/utils/maths.hpp index c0dea17..b55d115 100644 --- a/utils/maths.hpp +++ b/utils/maths.hpp @@ -29,6 +29,7 @@ double curve(double); //snap x to a grid of base double snap(double x, double base); +int snap(int x, int base); //vector dot product double scalarProduct(Vector2, Vector2); diff --git a/utils/simple_rng.hpp b/utils/simple_rng.hpp index db3a6af..35cf08f 100644 --- a/utils/simple_rng.hpp +++ b/utils/simple_rng.hpp @@ -24,18 +24,18 @@ class SimpleRNG { public: - SimpleRNG() = default; + SimpleRNG() { SetSeed(8891); } SimpleRNG(int x) { SetSeed(x); } - int SetSeed(int s) { return seed = s; } + int SetSeed(int s) { return seed = s & 0xFFFF; } int GetSeed() { return seed; } - int SimpleRNG::operator()(int x) { - return (x + seed) * 345589144 + 1123581321 + seed; + int operator()(int x) { + return SetSeed((x + seed) * 11235 + 81321); }; private: - int seed = 8891; + int seed; }; #endif \ No newline at end of file