@@ -33,16 +33,17 @@ void DefaultWorldGen::drawBackground( | |||
} | |||
} | |||
SDL_Color DefaultWorldGen::backgroundColor(Swan::Vec2 pos) { | |||
Cygnet::Color DefaultWorldGen::backgroundColor(Swan::Vec2 pos) { | |||
float y = pos.y; | |||
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 } } }); | |||
{ 0, Cygnet::ByteColor{128, 220, 250}}, | |||
{ 70, Cygnet::ByteColor{107, 87, 5}}, | |||
{ 100, Cygnet::ByteColor{107, 87, 5}}, | |||
{ 200, Cygnet::ByteColor{ 20, 20, 23}}, | |||
{ 300, Cygnet::ByteColor{ 20, 20, 23}}, | |||
{ 500, Cygnet::ByteColor{ 25, 10, 10}}, | |||
{1000, Cygnet::ByteColor{ 65, 10, 10}}, | |||
}); | |||
} | |||
Swan::Tile::ID DefaultWorldGen::genTile(Swan::TilePos pos) { |
@@ -17,7 +17,7 @@ public: | |||
void drawBackground( | |||
const Swan::Context &ctx, Cygnet::Renderer &rnd, Swan::Vec2 pos) override; | |||
SDL_Color backgroundColor(Swan::Vec2 pos) override; | |||
Cygnet::Color backgroundColor(Swan::Vec2 pos) override; | |||
void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) override; | |||
Swan::EntityRef spawnPlayer(const Swan::Context &ctx) override; | |||
@@ -3,6 +3,8 @@ | |||
#include <swan-common/Vector2.h> | |||
#include <memory> | |||
#include "util.h" | |||
struct SDL_Window; | |||
namespace Cygnet { | |||
@@ -15,7 +17,7 @@ public: | |||
~Window(); | |||
void makeCurrent(); | |||
void clear(); | |||
void clear(Color color = {}); | |||
void flip(); | |||
void onResize(int w, int h); | |||
SwanCommon::Vec2i size() { return { w_, h_ }; } |
@@ -16,6 +16,16 @@ using GLfloat = float; | |||
using Mat3gf = SwanCommon::Matrix3<GLfloat>; | |||
struct Color { | |||
float r = 0, g = 0, b = 0, a = 1; | |||
}; | |||
struct ByteColor { | |||
uint8_t r = 0, g = 0, b = 0, a = 0; | |||
operator Color() { return { r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }; } | |||
}; | |||
struct SDLError: public std::exception { | |||
SDLError(std::string msg): message(std::move(msg)) {} | |||
const char *what() const noexcept override { return message.c_str(); } |
@@ -50,7 +50,8 @@ void Window::makeCurrent() { | |||
glCheck(); | |||
} | |||
void Window::clear() { | |||
void Window::clear(Color color) { | |||
glClearColor(color.r, color.g, color.b, color.a); | |||
glClear(GL_COLOR_BUFFER_BIT); | |||
glCheck(); | |||
} |
@@ -6,6 +6,7 @@ | |||
#include <optional> | |||
#include <SDL.h> | |||
#include <cygnet/Renderer.h> | |||
#include <cygnet/util.h> | |||
#include "common.h" | |||
#include "Mod.h" | |||
@@ -58,7 +59,7 @@ public: | |||
TilePos getMouseTile(); | |||
SDL_Color backgroundColor(); | |||
Cygnet::Color backgroundColor(); | |||
void draw(); | |||
void update(float dt); | |||
void tick(float dt); |
@@ -7,6 +7,7 @@ | |||
#include <SDL.h> | |||
#include <cygnet/Renderer.h> | |||
#include <cygnet/ResourceManager.h> | |||
#include <cygnet/util.h> | |||
#include "common.h" | |||
#include "Item.h" | |||
@@ -44,7 +45,7 @@ public: | |||
Item &getItem(const std::string &name); | |||
Cygnet::RenderSprite &getSprite(const std::string &name); | |||
SDL_Color backgroundColor(); | |||
Cygnet::Color backgroundColor(); | |||
void draw(Cygnet::Renderer &rnd); | |||
void update(float dt); | |||
void tick(float dt); |
@@ -2,6 +2,7 @@ | |||
#include <memory> | |||
#include <SDL.h> | |||
#include <cygnet/util.h> | |||
#include "common.h" | |||
#include "Chunk.h" | |||
@@ -24,7 +25,7 @@ public: | |||
virtual ~WorldGen() = default; | |||
virtual void drawBackground(const Context &ctx, Cygnet::Renderer &rnd, Vec2 pos) = 0; | |||
virtual SDL_Color backgroundColor(Vec2 pos) = 0; | |||
virtual Cygnet::Color backgroundColor(Vec2 pos) = 0; | |||
virtual void genChunk(WorldPlane &plane, Chunk &chunk) = 0; | |||
virtual EntityRef spawnPlayer(const Context &ctx) = 0; |
@@ -69,7 +69,7 @@ public: | |||
EntityRef spawnPlayer(); | |||
void breakTile(TilePos pos); | |||
SDL_Color backgroundColor(); | |||
Cygnet::Color backgroundColor(); | |||
void draw(Cygnet::Renderer &rnd); | |||
void update(float dt); | |||
void tick(float dt); |
@@ -4,12 +4,13 @@ | |||
#include <optional> | |||
#include <initializer_list> | |||
#include <utility> | |||
#include <cygnet/util.h> | |||
namespace Swan { | |||
namespace Draw { | |||
SDL_Color linearGradient( | |||
float val, std::initializer_list<std::pair<float, SDL_Color>> colors); | |||
Cygnet::Color linearGradient( | |||
float val, std::initializer_list<std::pair<float, Cygnet::Color>> colors); | |||
/* | |||
void parallaxBackground( |
@@ -23,7 +23,7 @@ TilePos Game::getMouseTile() { | |||
return TilePos{(int)floor(pos.x), (int)floor(pos.y)}; | |||
} | |||
SDL_Color Game::backgroundColor() { | |||
Cygnet::Color Game::backgroundColor() { | |||
return world_->backgroundColor(); | |||
} | |||
@@ -294,7 +294,7 @@ Cygnet::RenderSprite &World::getSprite(const std::string &name) { | |||
return iter->second; | |||
} | |||
SDL_Color World::backgroundColor() { | |||
Cygnet::Color World::backgroundColor() { | |||
return planes_[currentPlane_]->backgroundColor(); | |||
} | |||
@@ -196,7 +196,7 @@ void WorldPlane::breakTile(TilePos pos) { | |||
world_->evtTileBreak_.emit(getContext(), pos, world_->getTileByID(id)); | |||
} | |||
SDL_Color WorldPlane::backgroundColor() { | |||
Cygnet::Color WorldPlane::backgroundColor() { | |||
return gen_->backgroundColor(world_->player_->pos); | |||
} | |||
@@ -8,11 +8,11 @@ | |||
namespace Swan { | |||
namespace Draw { | |||
static Uint8 linearLine(float from, float to, float frac) { | |||
return (Uint8)std::clamp(to * frac + from * (1 - frac), 0.0f, 255.0f); | |||
static float linearLine(float from, float to, float frac) { | |||
return std::clamp(to * frac + from * (1 - frac), 0.0f, 255.0f); | |||
} | |||
static SDL_Color linearColor(SDL_Color from, SDL_Color to, float frac) { | |||
static Cygnet::Color linearColor(Cygnet::Color from, Cygnet::Color to, float frac) { | |||
return { | |||
.r = linearLine(from.r, to.r, frac), | |||
.g = linearLine(from.g, to.g, frac), | |||
@@ -21,11 +21,11 @@ static SDL_Color linearColor(SDL_Color from, SDL_Color to, float frac) { | |||
}; | |||
} | |||
SDL_Color linearGradient( | |||
Cygnet::Color linearGradient( | |||
float val, | |||
std::initializer_list<std::pair<float, SDL_Color>> colors) { | |||
std::initializer_list<std::pair<float, Cygnet::Color>> colors) { | |||
const std::pair<float, SDL_Color> *arr = colors.begin(); | |||
const std::pair<float, Cygnet::Color> *arr = colors.begin(); | |||
size_t size = colors.size(); | |||
if (val < arr[0].first) |
@@ -239,9 +239,7 @@ int main(int argc, char **argv) { | |||
} | |||
{ | |||
//auto [r, g, b, a] = game.backgroundColor(); | |||
// TODO: Set clear color | |||
window.clear(); | |||
window.clear(game.backgroundColor()); | |||
} | |||
// ImGUI |