mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
feat: add text class
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/shader.hpp>
|
||||
#include <Cubed/ui/text.hpp>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
@@ -45,6 +46,13 @@ private:
|
||||
std::unordered_map<std::size_t, Shader> m_shaders;
|
||||
std::vector<GLuint> m_vao;
|
||||
std::vector<Vertex2D> m_ui;
|
||||
|
||||
Text m_version_text;
|
||||
Text m_fps_text;
|
||||
Text m_player_pos_text;
|
||||
|
||||
void init_text();
|
||||
|
||||
void render_outline();
|
||||
void render_sky();
|
||||
void render_text();
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
|
||||
struct Character {
|
||||
glm::vec2 uv_min;
|
||||
glm::vec2 uv_max;
|
||||
@@ -23,8 +25,8 @@ public:
|
||||
Font();
|
||||
~Font();
|
||||
|
||||
static void render_text(const Shader& shader, const std::string& text, float x, float y, float scale, const glm::vec3& color);
|
||||
|
||||
static std::vector<Vertex2D> vertices(const std::string& text, float x = 0.0f, float y = 0.0f, float scale = 1.0f);
|
||||
static GLuint text_texture();
|
||||
private:
|
||||
FT_Library m_ft;
|
||||
FT_Face m_face;
|
||||
@@ -32,7 +34,7 @@ private:
|
||||
float m_texture_width = 64;
|
||||
float m_texture_height = 64;
|
||||
|
||||
GLuint m_text_texture;
|
||||
static inline GLuint m_text_texture;
|
||||
std::unordered_map<char8_t, Character> m_characters;
|
||||
|
||||
void load_character(char8_t c);
|
||||
|
||||
65
include/Cubed/ui/color.hpp
Normal file
65
include/Cubed/ui/color.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
enum class Color {
|
||||
BLACK,
|
||||
WHITE,
|
||||
RED,
|
||||
GREEN,
|
||||
BLUE,
|
||||
YELLOW,
|
||||
CYAN,
|
||||
MAGENTA,
|
||||
GRAY,
|
||||
ORANGE,
|
||||
PURPLE,
|
||||
PINK,
|
||||
BROWN
|
||||
};
|
||||
|
||||
inline constexpr glm::vec4 color_value(Color color) {
|
||||
using glm::vec4;
|
||||
|
||||
switch (color) {
|
||||
case Color::BLACK:
|
||||
return vec4{0.0f, 0.0f, 0.0f, 1.0f};
|
||||
case Color::WHITE:
|
||||
return vec4{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
case Color::RED:
|
||||
return vec4{1.0f, 0.0f, 0.0f, 1.0f};
|
||||
case Color::GREEN:
|
||||
return vec4{0.0f, 1.0f, 0.0f, 1.0f};
|
||||
case Color::BLUE:
|
||||
return vec4{0.0f, 0.0f, 1.0f, 1.0f};
|
||||
case Color::YELLOW:
|
||||
return vec4{1.0f, 1.0f, 0.0f, 1.0f};
|
||||
case Color::CYAN:
|
||||
return vec4{0.0f, 1.0f, 1.0f, 1.0f};
|
||||
case Color::MAGENTA:
|
||||
return vec4{1.0f, 0.0f, 1.0f, 1.0f};
|
||||
case Color::GRAY:
|
||||
return vec4{0.5f, 0.5f, 0.5f, 1.0f};
|
||||
case Color::ORANGE:
|
||||
return vec4{1.0f, 0.647f, 0.0f, 1.0f};
|
||||
case Color::PURPLE:
|
||||
return vec4{0.502f, 0.0f, 0.502f, 1.0f};
|
||||
case Color::PINK:
|
||||
return vec4{1.0f, 0.753f, 0.769f, 1.0f};
|
||||
case Color::BROWN:
|
||||
return vec4{0.647f, 0.165f, 0.165f, 1.0f};
|
||||
default:
|
||||
CUBED_ASSERT_MSG(false, "Unknown Color");
|
||||
return vec4{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline glm::vec4 rgb255_to_float(int r, int g, int b, int a) {
|
||||
float nr = static_cast<float>(r) / 255.0f;
|
||||
float ng = static_cast<float>(g) / 255.0f;
|
||||
float nb = static_cast<float>(b) / 255.0f;
|
||||
float na = static_cast<float>(a) / 255.0f;
|
||||
|
||||
return glm::vec4{nr, ng, nb, na};
|
||||
}
|
||||
46
include/Cubed/ui/text.hpp
Normal file
46
include/Cubed/ui/text.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/ui/color.hpp>
|
||||
|
||||
class Shader;
|
||||
|
||||
class Text {
|
||||
public:
|
||||
Text();
|
||||
Text(std::string_view str, glm::vec2 pos = glm::vec2{0.0f, 0.0f}, Color color = Color::BLACK);
|
||||
~Text();
|
||||
|
||||
Text& color(Color color);
|
||||
//Text& color(const glm::vec4& color, int pos);
|
||||
Text& position(float x, float y);
|
||||
Text& scale(float s);
|
||||
static void set_loc(const Shader& shader);
|
||||
Text& text(std::string_view str);
|
||||
|
||||
|
||||
void render();
|
||||
|
||||
private:
|
||||
float m_scale = 1.0f;
|
||||
glm::vec2 m_pos{0.0f, 0.0f};
|
||||
|
||||
std::string m_name;
|
||||
std::string m_text;
|
||||
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
|
||||
glm::mat4 m_model_matrix;
|
||||
|
||||
std::vector<Vertex2D> m_vertices;
|
||||
GLuint m_vbo = 0;
|
||||
static inline GLuint m_color_loc = 0;
|
||||
static inline GLuint m_mv_loc = 0;
|
||||
|
||||
void update_vertices();
|
||||
void upload_to_gpu();
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user