Working on a new generator class, nearly there
This commit is contained in:
@@ -11,6 +11,7 @@ OUTDIR=out
|
||||
|
||||
all: $(OUTDIR)
|
||||
$(MAKE) -C utils
|
||||
$(MAKE) -C mapgen
|
||||
$(MAKE) -C src
|
||||
|
||||
$(OUTDIR):
|
||||
|
||||
@@ -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
|
||||
@@ -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 <cmath>
|
||||
|
||||
//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;
|
||||
}
|
||||
@@ -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
|
||||
+1
-4
@@ -1,4 +1 @@
|
||||
All terrain is grass by default
|
||||
|
||||
produce a hightmap for the terrain
|
||||
|
||||
All coordinates have a random value assigned to them
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#config
|
||||
INCLUDES+=. ../utils
|
||||
INCLUDES+=. ../utils ../mapgen
|
||||
LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
|
||||
+3
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user