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

} }
} }


SDL_Color DefaultWorldGen::backgroundColor(Swan::Vec2 pos) {
Cygnet::Color DefaultWorldGen::backgroundColor(Swan::Vec2 pos) {
float y = pos.y; float y = pos.y;
return Swan::Draw::linearGradient(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) { Swan::Tile::ID DefaultWorldGen::genTile(Swan::TilePos pos) {

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



void drawBackground( void drawBackground(
const Swan::Context &ctx, Cygnet::Renderer &rnd, Swan::Vec2 pos) override; 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; void genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) override;
Swan::EntityRef spawnPlayer(const Swan::Context &ctx) override; Swan::EntityRef spawnPlayer(const Swan::Context &ctx) override;



+ 3
- 1
libcygnet/include/cygnet/Window.h View File

#include <swan-common/Vector2.h> #include <swan-common/Vector2.h>
#include <memory> #include <memory>


#include "util.h"

struct SDL_Window; struct SDL_Window;


namespace Cygnet { namespace Cygnet {
~Window(); ~Window();


void makeCurrent(); void makeCurrent();
void clear();
void clear(Color color = {});
void flip(); void flip();
void onResize(int w, int h); void onResize(int w, int h);
SwanCommon::Vec2i size() { return { w_, h_ }; } SwanCommon::Vec2i size() { return { w_, h_ }; }

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



using Mat3gf = SwanCommon::Matrix3<GLfloat>; 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 { struct SDLError: public std::exception {
SDLError(std::string msg): message(std::move(msg)) {} SDLError(std::string msg): message(std::move(msg)) {}
const char *what() const noexcept override { return message.c_str(); } const char *what() const noexcept override { return message.c_str(); }

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

glCheck(); glCheck();
} }


void Window::clear() {
void Window::clear(Color color) {
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glCheck(); glCheck();
} }

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

#include <optional> #include <optional>
#include <SDL.h> #include <SDL.h>
#include <cygnet/Renderer.h> #include <cygnet/Renderer.h>
#include <cygnet/util.h>


#include "common.h" #include "common.h"
#include "Mod.h" #include "Mod.h"


TilePos getMouseTile(); TilePos getMouseTile();


SDL_Color backgroundColor();
Cygnet::Color backgroundColor();
void draw(); void draw();
void update(float dt); void update(float dt);
void tick(float dt); void tick(float dt);

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

#include <SDL.h> #include <SDL.h>
#include <cygnet/Renderer.h> #include <cygnet/Renderer.h>
#include <cygnet/ResourceManager.h> #include <cygnet/ResourceManager.h>
#include <cygnet/util.h>


#include "common.h" #include "common.h"
#include "Item.h" #include "Item.h"
Item &getItem(const std::string &name); Item &getItem(const std::string &name);
Cygnet::RenderSprite &getSprite(const std::string &name); Cygnet::RenderSprite &getSprite(const std::string &name);


SDL_Color backgroundColor();
Cygnet::Color backgroundColor();
void draw(Cygnet::Renderer &rnd); void draw(Cygnet::Renderer &rnd);
void update(float dt); void update(float dt);
void tick(float dt); void tick(float dt);

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



#include <memory> #include <memory>
#include <SDL.h> #include <SDL.h>
#include <cygnet/util.h>


#include "common.h" #include "common.h"
#include "Chunk.h" #include "Chunk.h"
virtual ~WorldGen() = default; virtual ~WorldGen() = default;


virtual void drawBackground(const Context &ctx, Cygnet::Renderer &rnd, Vec2 pos) = 0; 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 void genChunk(WorldPlane &plane, Chunk &chunk) = 0;
virtual EntityRef spawnPlayer(const Context &ctx) = 0; virtual EntityRef spawnPlayer(const Context &ctx) = 0;

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

EntityRef spawnPlayer(); EntityRef spawnPlayer();
void breakTile(TilePos pos); void breakTile(TilePos pos);


SDL_Color backgroundColor();
Cygnet::Color backgroundColor();
void draw(Cygnet::Renderer &rnd); void draw(Cygnet::Renderer &rnd);
void update(float dt); void update(float dt);
void tick(float dt); void tick(float dt);

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

#include <optional> #include <optional>
#include <initializer_list> #include <initializer_list>
#include <utility> #include <utility>
#include <cygnet/util.h>


namespace Swan { namespace Swan {
namespace Draw { 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( void parallaxBackground(

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

return TilePos{(int)floor(pos.x), (int)floor(pos.y)}; return TilePos{(int)floor(pos.x), (int)floor(pos.y)};
} }


SDL_Color Game::backgroundColor() {
Cygnet::Color Game::backgroundColor() {
return world_->backgroundColor(); return world_->backgroundColor();
} }



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

return iter->second; return iter->second;
} }


SDL_Color World::backgroundColor() {
Cygnet::Color World::backgroundColor() {
return planes_[currentPlane_]->backgroundColor(); return planes_[currentPlane_]->backgroundColor();
} }



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

world_->evtTileBreak_.emit(getContext(), pos, world_->getTileByID(id)); world_->evtTileBreak_.emit(getContext(), pos, world_->getTileByID(id));
} }


SDL_Color WorldPlane::backgroundColor() {
Cygnet::Color WorldPlane::backgroundColor() {
return gen_->backgroundColor(world_->player_->pos); return gen_->backgroundColor(world_->player_->pos);
} }



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

namespace Swan { namespace Swan {
namespace Draw { 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 { 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(
Cygnet::Color linearGradient(
float val, 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(); size_t size = colors.size();


if (val < arr[0].first) if (val < arr[0].first)

+ 1
- 3
src/main.cc View File

} }


{ {
//auto [r, g, b, a] = game.backgroundColor();
// TODO: Set clear color
window.clear();
window.clear(game.backgroundColor());
} }


// ImGUI // ImGUI

Loading…
Cancel
Save