Colors: Introduce color type abstraction
SFML and Dear ImGui have incompatible color types, abstract them through a new Color class which has conversions. Centralize color constants in a namespace as well.
This commit is contained in:
parent
b492def96e
commit
e234dda860
3 changed files with 47 additions and 9 deletions
37
Color.h
Normal file
37
Color.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// Created by trotfunky on 26/01/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef RAYCASTING_COLOR_H
|
||||||
|
#define RAYCASTING_COLOR_H
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#ifdef IMGUI
|
||||||
|
#include <imgui.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
class Color {
|
||||||
|
public:
|
||||||
|
uint8_t r;
|
||||||
|
uint8_t g;
|
||||||
|
uint8_t b;
|
||||||
|
uint8_t alpha;
|
||||||
|
|
||||||
|
constexpr Color(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) :
|
||||||
|
r(r), g(g), b(b), alpha(alpha) {};
|
||||||
|
|
||||||
|
operator sf::Color() const { return {r, g, b, alpha}; }
|
||||||
|
#ifdef IMGUI
|
||||||
|
operator ImColor() const { return {r, g, b, alpha}; }
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace Colors {
|
||||||
|
static constexpr Color Ground{67, 137, 39};
|
||||||
|
static constexpr Color Ceiling{39, 69, 137};
|
||||||
|
static constexpr Color Wall{84, 56, 34};
|
||||||
|
static constexpr Color Door{186, 152, 107};
|
||||||
|
static constexpr Color Window{104, 123, 165};
|
||||||
|
}
|
||||||
|
#endif //RAYCASTING_COLOR_H
|
12
World.cpp
12
World.cpp
|
@ -287,13 +287,13 @@ void World::step(const float& stepTime) {
|
||||||
ImVec4 hoverColor;
|
ImVec4 hoverColor;
|
||||||
switch ((BlockType)blockToPlace) {
|
switch ((BlockType)blockToPlace) {
|
||||||
case BlockType::WALL:
|
case BlockType::WALL:
|
||||||
hoverColor = (ImVec4)ImColor(84, 56, 34);
|
hoverColor = (ImVec4)Colors::Wall;
|
||||||
break;
|
break;
|
||||||
case BlockType::DOOR:
|
case BlockType::DOOR:
|
||||||
hoverColor = (ImVec4)ImColor(186, 152, 107);
|
hoverColor = (ImVec4)Colors::Door;
|
||||||
break;
|
break;
|
||||||
case BlockType::WINDOW:
|
case BlockType::WINDOW:
|
||||||
hoverColor = (ImVec4)ImColor(104, 123, 165);
|
hoverColor = (ImVec4)Colors::Window;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* Default header color, it seems ? */
|
/* Default header color, it seems ? */
|
||||||
|
@ -305,13 +305,13 @@ void World::step(const float& stepTime) {
|
||||||
ImVec4 blockColor;
|
ImVec4 blockColor;
|
||||||
switch (currentBlock) {
|
switch (currentBlock) {
|
||||||
case BlockType::WALL:
|
case BlockType::WALL:
|
||||||
blockColor = (ImVec4)ImColor(84, 56, 34);
|
blockColor = (ImVec4)Colors::Wall;
|
||||||
break;
|
break;
|
||||||
case BlockType::DOOR:
|
case BlockType::DOOR:
|
||||||
blockColor = (ImVec4)ImColor(186, 152, 107);
|
blockColor = (ImVec4)Colors::Door;
|
||||||
break;
|
break;
|
||||||
case BlockType::WINDOW:
|
case BlockType::WINDOW:
|
||||||
blockColor = (ImVec4)ImColor(104, 123, 165);
|
blockColor = (ImVec4)Colors::Window;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
blockColor = (ImVec4)ImColor(188, 120, 32);
|
blockColor = (ImVec4)ImColor(188, 120, 32);
|
||||||
|
|
7
World.h
7
World.h
|
@ -10,6 +10,7 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Color.h"
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
|
|
||||||
enum class BlockType {
|
enum class BlockType {
|
||||||
|
@ -24,8 +25,8 @@ public:
|
||||||
Player player;
|
Player player;
|
||||||
|
|
||||||
World(int w, int h,
|
World(int w, int h,
|
||||||
sf::Color groundColor = sf::Color{67, 137, 39},
|
sf::Color groundColor = Colors::Ground,
|
||||||
sf::Color ceilingColor = sf::Color{39, 69, 137},
|
sf::Color ceilingColor = Colors::Ceiling,
|
||||||
std::vector<BlockType> worldMap = {});
|
std::vector<BlockType> worldMap = {});
|
||||||
int getW() const;
|
int getW() const;
|
||||||
int getH() const;
|
int getH() const;
|
||||||
|
@ -52,7 +53,7 @@ private:
|
||||||
sf::Color ceilingColor;
|
sf::Color ceilingColor;
|
||||||
|
|
||||||
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
|
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
|
||||||
sf::Color fillColor = sf::Color(84,56,34)) const;
|
sf::Color fillColor = Colors::Wall) const;
|
||||||
/**
|
/**
|
||||||
* Cast a ray from a given position and return its distance to the origin.
|
* Cast a ray from a given position and return its distance to the origin.
|
||||||
* @param originX Ray X origin, strictly positive
|
* @param originX Ray X origin, strictly positive
|
||||||
|
|
Loading…
Add table
Reference in a new issue