mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor(render): replace UI shaders with image shaders and Image widget
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "Cubed/render/vertex_buffer.hpp"
|
||||
#include "Cubed/render/world_renderer.hpp"
|
||||
#include "Cubed/shader.hpp"
|
||||
#include "Cubed/ui/image.hpp"
|
||||
#include "Cubed/ui/label.hpp"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
@@ -33,6 +34,7 @@ public:
|
||||
void render();
|
||||
|
||||
void render_lable(const Label& label);
|
||||
void render_image(const Image& image);
|
||||
|
||||
void update(float delta_time);
|
||||
void update_fov(float fov);
|
||||
@@ -99,8 +101,9 @@ private:
|
||||
std::unique_ptr<VertexBuffer> m_player_vbo;
|
||||
std::unique_ptr<VertexBuffer> m_quad_vbo;
|
||||
|
||||
std::unique_ptr<Image> m_crosshair_image;
|
||||
|
||||
glm::mat4 m_ui_proj_matrix;
|
||||
glm::mat4 m_ui_model_matrix;
|
||||
ShaderManager m_shaders;
|
||||
|
||||
/*
|
||||
|
||||
@@ -88,7 +88,6 @@ public:
|
||||
private:
|
||||
GLuint m_id = 0;
|
||||
const TextureType M_TYPE;
|
||||
|
||||
GLenum get_gl_texture_type() const;
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -3,9 +3,42 @@
|
||||
#include <SOIL2.h>
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
|
||||
#include <utility>
|
||||
namespace Cubed {
|
||||
|
||||
namespace Tools {
|
||||
void delete_image_data(unsigned char* data);
|
||||
}
|
||||
|
||||
struct ImageData {
|
||||
unsigned char* data = nullptr;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int channels = 0;
|
||||
ImageData(const ImageData&) = delete;
|
||||
ImageData(ImageData&& o) noexcept
|
||||
: data(std::exchange(o.data, nullptr)), width(o.width),
|
||||
height(o.height), channels(o.channels) {}
|
||||
ImageData& operator=(const ImageData&) = delete;
|
||||
ImageData& operator=(ImageData&& o) noexcept {
|
||||
if (this == &o) {
|
||||
return *this;
|
||||
}
|
||||
if (data) {
|
||||
Tools::delete_image_data(data);
|
||||
}
|
||||
data = std::exchange(o.data, nullptr);
|
||||
width = o.width;
|
||||
height = o.height;
|
||||
channels = o.channels;
|
||||
return *this;
|
||||
}
|
||||
ImageData(unsigned char* d, int w, int h, int c)
|
||||
: data(d), width(w), height(h), channels(c) {}
|
||||
ImageData() = default;
|
||||
~ImageData() { Tools::delete_image_data(data); }
|
||||
};
|
||||
|
||||
namespace Tools {
|
||||
GLuint create_shader_program(const std::string& v_shader_path,
|
||||
const std::string& f_shader_path);
|
||||
@@ -13,9 +46,9 @@ void print_shader_log(GLuint shader);
|
||||
void print_program_info(int prog);
|
||||
bool check_opengl_error();
|
||||
std::string read_shader_source(const std::string& file_path);
|
||||
void delete_image_data(unsigned char* data);
|
||||
unsigned char* load_image_data(const std::string& tex_image_path,
|
||||
bool check_exist = true);
|
||||
|
||||
ImageData load_image_data(const std::string& tex_image_path,
|
||||
bool check_exist = true);
|
||||
|
||||
} // namespace Tools
|
||||
|
||||
|
||||
28
include/Cubed/ui/image.hpp
Normal file
28
include/Cubed/ui/image.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/render/texture.hpp"
|
||||
#include "Cubed/ui/widget.hpp"
|
||||
#include "glm/ext/vector_float2.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace Cubed {
|
||||
class Image : public Widget {
|
||||
public:
|
||||
Image();
|
||||
void update(float dt) override;
|
||||
void render(Renderer& renderer) override;
|
||||
|
||||
Image& set_image(std::filesystem::path path);
|
||||
float width() const;
|
||||
float height() const;
|
||||
const Texture& texture() const;
|
||||
|
||||
private:
|
||||
Texture m_texture;
|
||||
int m_width = 0;
|
||||
int m_height = 0;
|
||||
glm::vec2 m_pos{0.0f, 0.0f};
|
||||
float m_scale = 1.0f;
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -14,18 +14,15 @@ public:
|
||||
Label(const std::string& id);
|
||||
virtual ~Label() = default;
|
||||
|
||||
Label& set_position(const glm::vec2& pos);
|
||||
Label& set_position(float x, float y);
|
||||
Label& set_text(std::string_view text);
|
||||
Label& set_scale(float scale);
|
||||
|
||||
Label& set_color(Color color);
|
||||
virtual void update(float dt) override;
|
||||
virtual void render(Renderer& renderer) override;
|
||||
|
||||
const UIVertexData& data() const;
|
||||
const glm::vec2& pos() const;
|
||||
|
||||
const TextStyle& text_style() const;
|
||||
float scale() const;
|
||||
|
||||
protected:
|
||||
virtual void on_update(float dt) override;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include "glm/ext/vector_float2.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
@@ -12,6 +14,11 @@ public:
|
||||
virtual void update(float dt);
|
||||
virtual void render(Renderer& renderer);
|
||||
virtual const std::string& id() const;
|
||||
virtual Widget& set_position(const glm::vec2& pos);
|
||||
virtual Widget& set_position(float x, float y);
|
||||
virtual Widget& set_scale(float scale);
|
||||
virtual const glm::vec2& pos() const;
|
||||
virtual float scale() const;
|
||||
template <typename T, typename... Args> T& add_child(Args&&... args) {
|
||||
auto widget = std::make_unique<T>(std::forward<Args>(args)...);
|
||||
T& ref = *widget;
|
||||
@@ -23,6 +30,8 @@ protected:
|
||||
virtual void on_update(float dt);
|
||||
virtual void on_render(Renderer& renderer);
|
||||
std::string m_id;
|
||||
float m_scale = 1.0f;
|
||||
glm::vec2 m_pos{0.0f, 0.0f};
|
||||
|
||||
private:
|
||||
Widget* m_parent = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user