SDL_Color WGDefault::backgroundColor(Swan::Vec2 pos) { | SDL_Color WGDefault::backgroundColor(Swan::Vec2 pos) { | ||||
float y = pos.y; | 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) { | Swan::Tile::ID WGDefault::genTile(Swan::TilePos pos) { |
class WorldPlane; | class WorldPlane; | ||||
class ImageResource; | class ImageResource; | ||||
class WorldGenStructure { | |||||
public: | |||||
virtual ~WorldGenStructure() = 0; | |||||
virtual bool isBase(TilePos pos); | |||||
}; | |||||
class WorldGen { | class WorldGen { | ||||
public: | public: | ||||
class Factory { | class Factory { | ||||
virtual BodyTrait::HasBody *spawnPlayer(WorldPlane &plane) = 0; | virtual BodyTrait::HasBody *spawnPlayer(WorldPlane &plane) = 0; | ||||
}; | }; | ||||
class WorldGenStructure { | |||||
public: | |||||
virtual ~WorldGenStructure() = 0; | |||||
virtual bool isBase(TilePos pos); | |||||
}; | |||||
} | } |
#include <SDL.h> | #include <SDL.h> | ||||
#include <optional> | #include <optional> | ||||
#include <initializer_list> | |||||
#include <utility> | |||||
#include "Win.h" | #include "Win.h" | ||||
namespace Swan { | namespace Swan { | ||||
namespace Draw { | 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( | void parallaxBackground( | ||||
Win &win, SDL_Texture *tex, | Win &win, SDL_Texture *tex, |
return (Uint8)std::clamp(to * frac + from * (1 - frac), 0.0f, 255.0f); | 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 { | return { | ||||
.r = linearLine(from.r, to.r, frac), | .r = linearLine(from.r, to.r, frac), | ||||
.g = linearLine(from.g, to.g, frac), | .g = linearLine(from.g, to.g, 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( | void parallaxBackground( | ||||
Win &win, SDL_Texture *tex, | Win &win, SDL_Texture *tex, | ||||
std::optional<SDL_Rect> srcrect, std::optional<SDL_Rect> destrect, | std::optional<SDL_Rect> srcrect, std::optional<SDL_Rect> destrect, |