Browse Source

background colors

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
15f83881b1

+ 9
- 8
core.mod/src/DefaultWorldGen.cc View File

@@ -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) {

+ 1
- 1
core.mod/src/DefaultWorldGen.h View File

@@ -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
- 1
libcygnet/include/cygnet/Window.h View File

@@ -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_ }; }

+ 10
- 0
libcygnet/include/cygnet/util.h View File

@@ -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(); }

+ 2
- 1
libcygnet/src/Window.cc View File

@@ -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();
}

+ 2
- 1
libswan/include/swan/Game.h View File

@@ -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);

+ 2
- 1
libswan/include/swan/World.h View File

@@ -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
- 1
libswan/include/swan/WorldGen.h View File

@@ -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;

+ 1
- 1
libswan/include/swan/WorldPlane.h View File

@@ -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);

+ 3
- 2
libswan/include/swan/drawutil.h View File

@@ -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(

+ 1
- 1
libswan/src/Game.cc View File

@@ -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();
}


+ 1
- 1
libswan/src/World.cc View File

@@ -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();
}


+ 1
- 1
libswan/src/WorldPlane.cc View File

@@ -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);
}


+ 6
- 6
libswan/src/drawutil.cc View File

@@ -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)

+ 1
- 3
src/main.cc View File

@@ -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

Loading…
Cancel
Save