@@ -30,28 +30,14 @@ void WGDefault::drawBackground(const Swan::Context &ctx, Swan::Win &win, Swan::V | |||
SDL_Color WGDefault::backgroundColor(Swan::Vec2 pos) { | |||
float y = pos.y; | |||
float cave_start = 20; | |||
float cave_end = cave_start + 100; | |||
float deep_start = cave_end + 200; | |||
float deep_end = deep_start + 300; | |||
if (y < cave_start) { | |||
return Swan::Draw::linearColor( | |||
{ 128, 220, 250, 255 }, | |||
{ 107, 87, 5, 255 }, | |||
y / cave_start); | |||
} else if (y < deep_start) { | |||
return Swan::Draw::linearColor( | |||
{ 107, 87, 5, 255 }, | |||
{ 15, 7, 7, 255 }, | |||
(y - cave_start) / (cave_end - cave_start)); | |||
} else if (y < deep_end) { | |||
return Swan::Draw::linearColor( | |||
{ 15, 7, 7, 255 }, | |||
{ 35, 0, 0, 255 }, | |||
(y - deep_start) / (deep_end - deep_start)); | |||
} else { | |||
return { 35, 0, 0, 255 }; | |||
} | |||
return Swan::Draw::linearGradient(y, { | |||
{ 0, { 128, 220, 250, 255 } }, | |||
{ 70, { 107, 87, 5, 255 } }, | |||
{ 100, { 107, 87, 5, 255 } }, | |||
{ 200, { 20, 20, 23, 255 } }, | |||
{ 300, { 20, 20, 23, 255 } }, | |||
{ 500, { 25, 10, 10, 255 } }, | |||
{ 1000, { 65, 10, 10, 255 } } }); | |||
} | |||
Swan::Tile::ID WGDefault::genTile(Swan::TilePos pos) { |
@@ -15,6 +15,13 @@ class World; | |||
class WorldPlane; | |||
class ImageResource; | |||
class WorldGenStructure { | |||
public: | |||
virtual ~WorldGenStructure() = 0; | |||
virtual bool isBase(TilePos pos); | |||
}; | |||
class WorldGen { | |||
public: | |||
class Factory { | |||
@@ -33,11 +40,4 @@ public: | |||
virtual BodyTrait::HasBody *spawnPlayer(WorldPlane &plane) = 0; | |||
}; | |||
class WorldGenStructure { | |||
public: | |||
virtual ~WorldGenStructure() = 0; | |||
virtual bool isBase(TilePos pos); | |||
}; | |||
} |
@@ -2,13 +2,16 @@ | |||
#include <SDL.h> | |||
#include <optional> | |||
#include <initializer_list> | |||
#include <utility> | |||
#include "Win.h" | |||
namespace Swan { | |||
namespace Draw { | |||
SDL_Color linearColor(SDL_Color from, SDL_Color to, float frac); | |||
SDL_Color linearGradient( | |||
float val, std::initializer_list<std::pair<float, SDL_Color>> colors); | |||
void parallaxBackground( | |||
Win &win, SDL_Texture *tex, |
@@ -12,7 +12,7 @@ static Uint8 linearLine(float from, float to, float frac) { | |||
return (Uint8)std::clamp(to * frac + from * (1 - frac), 0.0f, 255.0f); | |||
} | |||
SDL_Color linearColor(SDL_Color from, SDL_Color to, float frac) { | |||
static SDL_Color linearColor(SDL_Color from, SDL_Color to, float frac) { | |||
return { | |||
.r = linearLine(from.r, to.r, frac), | |||
.g = linearLine(from.g, to.g, frac), | |||
@@ -21,6 +21,29 @@ SDL_Color linearColor(SDL_Color from, SDL_Color to, float frac) { | |||
}; | |||
} | |||
SDL_Color linearGradient( | |||
float val, | |||
std::initializer_list<std::pair<float, SDL_Color>> colors) { | |||
const std::pair<float, SDL_Color> *arr = colors.begin(); | |||
size_t size = colors.size(); | |||
if (val < arr[0].first) | |||
return arr[0].second; | |||
for (size_t i = 1; i < size; ++i) { | |||
if (arr[i].first < val) | |||
continue; | |||
auto [fromv, fromc] = arr[i - 1]; | |||
auto [tov, toc] = arr[i]; | |||
float frac = (val - fromv) / (tov - fromv); | |||
return linearColor(fromc, toc, frac); | |||
} | |||
return arr[size - 1].second; | |||
} | |||
void parallaxBackground( | |||
Win &win, SDL_Texture *tex, | |||
std::optional<SDL_Rect> srcrect, std::optional<SDL_Rect> destrect, |