Implemented gradient sky

This commit is contained in:
Maximilian Giller 2023-06-20 21:54:59 +02:00
parent 06a6b1b029
commit 4a44a41c80
6 changed files with 90 additions and 24 deletions

View file

@ -13,6 +13,7 @@
// World // World
#define WORLD_GRAVITY b2Vec2(0.f, 9.8f) #define WORLD_GRAVITY b2Vec2(0.f, 9.8f)
#define SKY_HEIGHT_SCALE 2.f
// FPS // FPS
#define FRAME_RATE 60 #define FRAME_RATE 60

View file

@ -17,14 +17,17 @@ struct LevelConfig
TileMapConfig tileMapConfig = {}; TileMapConfig tileMapConfig = {};
std::vector<GridCoordinates> playerSpawnPoints = {}; std::vector<GridCoordinates> playerSpawnPoints = {};
std::vector<CollectableInLevel> collectables = {}; std::vector<CollectableInLevel> collectables = {};
std::vector<sf::Color> skyColors = {};
LevelConfig(std::string name, LevelConfig(std::string name,
const std::vector<GridCoordinates> &playerSpawnPoints, const std::vector<GridCoordinates> &playerSpawnPoints,
const std::vector<CollectableInLevel> &collectables, const std::vector<CollectableInLevel> &collectables,
std::vector<sf::Color> skyColors,
const TileMapConfig &tileMapConfig) const TileMapConfig &tileMapConfig)
: name(std::move(name)), : name(std::move(name)),
playerSpawnPoints(playerSpawnPoints), playerSpawnPoints(playerSpawnPoints),
tileMapConfig(tileMapConfig) tileMapConfig(tileMapConfig),
skyColors(std::move(skyColors))
{ {
worldMapSize = tileMapConfig.getSize(); worldMapSize = tileMapConfig.getSize();

View file

@ -25,7 +25,7 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig)
std::shared_ptr<LevelRenderer> levelRenderer = std::make_shared<LevelRenderer>(); std::shared_ptr<LevelRenderer> levelRenderer = std::make_shared<LevelRenderer>();
game->addGameObject(levelRenderer); game->addGameObject(levelRenderer);
levelRenderer->addChild(std::make_shared<Skymap>()); levelRenderer->addChild(std::make_shared<Skymap>(levelConfig.skyColors, levelConfig.worldMapSize));
levelRenderer->addChild(SpriteFactory::createTileMap(levelConfig.tileMapConfig)); levelRenderer->addChild(SpriteFactory::createTileMap(levelConfig.tileMapConfig));
if (DB_WORLD_GRID_RENDER) if (DB_WORLD_GRID_RENDER)

View file

@ -9,7 +9,24 @@
std::map<std::string, LevelConfig> const all_levels = { std::map<std::string, LevelConfig> const all_levels = {
{"default", LevelConfig("Default", {{0, 0}}, { {"default", LevelConfig("Default", {{0, 0}}, {
CollectableInLevel("box", {5, 5})}, CollectableInLevel("box", {5, 5})
},
{
// Blues
sf::Color(2, 100, 234),
sf::Color(2, 100, 234),
sf::Color(2, 100, 234),
sf::Color(2, 195, 234),
// Neutral
sf::Color::White,
// Browns
sf::Color(163, 128, 68),
sf::Color(100, 80, 40),
sf::Color(20, 18, 11),
sf::Color::Black
},
TileMapConfig("iso-tiles", {{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, TileMapConfig("iso-tiles", {{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},

View file

@ -1,28 +1,68 @@
#include <SFML/Graphics/VertexArray.hpp> #include <SFML/Graphics/VertexArray.hpp>
#include "skymap.hpp" #include "skymap.hpp"
#include "../../logging/easylogging++.h"
#include "../../config.h"
Skymap::Skymap(std::vector<sf::Color> skyColors, sf::Vector2<int> mapSize)
{
// Sizes
float mapWidth = 2 * TranslatedCoordinates(GridCoordinates(mapSize.x, 0)).isometric().x;
float mapHeight = TranslatedCoordinates(GridCoordinates(mapSize.x, mapSize.y)).isometric().y;
float halfSkyWidth = mapWidth * 1.5f;
float skyHeight = mapHeight * SKY_HEIGHT_SCALE;
// If only one color is provided, use it for both the top and bottom
if (skyColors.empty())
{
LOG(ERROR) << "No sky colors provided. Using defaults instead.";
skyColors = {
// Blues
sf::Color(2, 100, 234),
sf::Color(2, 100, 234),
sf::Color(2, 100, 234),
sf::Color(2, 195, 234),
// Neutral
sf::Color::White,
// Browns
sf::Color(163, 128, 68),
sf::Color(100, 80, 40),
sf::Color(20, 18, 11),
sf::Color::Black
};
} else if (skyColors.size() == 1)
{
skyColors.push_back(skyColors[0]);
}
// Create the background gradient
int colorCount = skyColors.size();
int vertexCount = colorCount * 2;
backgroundGradient = sf::VertexArray(sf::TriangleStrip, vertexCount);
// Starting top from top, going down
// Always alternating between left and right
for (int i = 0; i < vertexCount; i += 2)
{
int colorIndex = i / 2;
auto color = skyColors[colorIndex];
float heightProgress = (float) colorIndex / (float) (colorCount - 1);
// Set position
float y = mapHeight / 2.f + skyHeight / 2.f - skyHeight * heightProgress;
// Left
backgroundGradient[i].position = sf::Vector2f(-halfSkyWidth, y);
backgroundGradient[i].color = color;
// Right
backgroundGradient[i + 1].position = sf::Vector2f(halfSkyWidth, y);
backgroundGradient[i + 1].color = color;
}
}
void Skymap::draw(sf::RenderWindow *window) void Skymap::draw(sf::RenderWindow *window)
{ {
float mapHeight = 1000;
sf::VertexArray backgroundGradient(sf::Quads, 4);
// Defining the coordinates clockwise
// Top Left
backgroundGradient[0].position = sf::Vector2f(-2 * mapHeight, -2 * mapHeight);
backgroundGradient[0].color = sf::Color(150, 200, 255);
// Top Right
backgroundGradient[1].position = sf::Vector2f(2 * mapHeight, -2 * mapHeight);
backgroundGradient[1].color = sf::Color(150, 200, 255);
// Bottom Right
backgroundGradient[2].position = sf::Vector2f(2 * mapHeight, mapHeight);
backgroundGradient[2].color = sf::Color::White;
// Bottom Left
backgroundGradient[3].position = sf::Vector2f(-2 * mapHeight, mapHeight);
backgroundGradient[3].color = sf::Color::White;
window->draw(backgroundGradient); window->draw(backgroundGradient);
} }

View file

@ -7,7 +7,12 @@
class Skymap : public GameObject class Skymap : public GameObject
{ {
public: public:
explicit Skymap(std::vector<sf::Color> skyColors, sf::Vector2<int> mapSize);
void draw(sf::RenderWindow *window) override; void draw(sf::RenderWindow *window) override;
private:
sf::VertexArray backgroundGradient;
}; };