[Refactor] Givin' the client some luv!

Refactored client-side rendering code to use a Color struct rather than
raw float arrays.
This commit is contained in:
Ritchie Cunningham 2025-09-27 22:58:04 +01:00
parent c052cf3cbf
commit cf48324516
8 changed files with 26 additions and 13 deletions

View File

@ -54,9 +54,9 @@ ShapeRenderer::~ShapeRenderer(void) {
delete _shape_shader; delete _shape_shader;
} }
void ShapeRenderer::draw_rect(int x, int y, int width, int height, float color[3]) { void ShapeRenderer::draw_rect(int x, int y, int width, int height, const Color& color) {
_shape_shader->use(); _shape_shader->use();
_shape_shader->set_vec3("objectColor", color[0], color[1], color[2]); _shape_shader->set_vec3("objectColor", color.r, color.g, color.b);
float x_f = (float)x; float x_f = (float)x;
float y_f = (float)y; float y_f = (float)y;

View File

@ -1,13 +1,14 @@
#pragma once #pragma once
#include "shader.h" #include "shader.h"
#include "types.h"
class ShapeRenderer { class ShapeRenderer {
public: public:
ShapeRenderer(unsigned int screen_width, unsigned int screen_height); ShapeRenderer(unsigned int screen_width, unsigned int screen_height);
~ShapeRenderer(void); ~ShapeRenderer(void);
void draw_rect(int x, int y, int width, int height, float color[3]); void draw_rect(int x, int y, int width, int height, const Color& color);
private: private:
Shader* _shape_shader; Shader* _shape_shader;

View File

@ -1,4 +1,5 @@
#include <cstdio> #include <cstdio>
#include "gfx/types.h"
#include <GL/glew.h> #include <GL/glew.h>
#include <SDL3/SDL_render.h> #include <SDL3/SDL_render.h>
#include <freetype/freetype.h> #include <freetype/freetype.h>
@ -106,9 +107,9 @@ void TextRenderer::load_font(const char* font_path, unsigned int font_size) {
} }
void TextRenderer::render_text(const char* text, float x, float y, float scale, void TextRenderer::render_text(const char* text, float x, float y, float scale,
float color[3]) { const Color& color) {
_txt_shader->use(); _txt_shader->use();
_txt_shader->set_vec3("textColor", color[0], color[1], color[2]); _txt_shader->set_vec3("textColor", color.r, color.g, color.b);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(_vao); glBindVertexArray(_vao);

View File

@ -5,6 +5,7 @@
#include <map> #include <map>
#include "shader.h" #include "shader.h"
#include "types.h"
/* State of a single charactrer glyph. */ /* State of a single charactrer glyph. */
struct character { struct character {
@ -20,7 +21,7 @@ public:
~TextRenderer(void); ~TextRenderer(void);
void load_font(const char* font_path, unsigned int font_size); void load_font(const char* font_path, unsigned int font_size);
void render_text(const char* text, float x, float y, float scale, float color[3]); void render_text(const char* text, float x, float y, float scale, const Color& color);
private: private:
Shader* _txt_shader; Shader* _txt_shader;

9
client/src/gfx/types.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
struct Color {
float r, g, b;
};
struct Rect {
int x, y, w, h;
};

View File

@ -6,6 +6,7 @@
#include "terminal.h" #include "terminal.h"
#include "client_network.h" #include "client_network.h"
#include "gfx/txt_renderer.h" #include "gfx/txt_renderer.h"
#include "gfx/types.h"
Terminal::Terminal(ClientNetwork* network) : _network(network) { Terminal::Terminal(ClientNetwork* network) : _network(network) {
/* Placeholder welcome message to history. */ /* Placeholder welcome message to history. */
@ -103,8 +104,8 @@ void Terminal::scroll(int amount, int win_content_height) {
void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height, void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height,
bool show_cursor) { bool show_cursor) {
float white[] = { 1.0f, 1.0f, 1.0f }; const Color white = { 1.0f, 1.0f, 1.0f };
float green[] = { 0.2f, 1.0f, 0.2f }; const Color green = { 0.2f, 1.0f, 0.2f };
float line_height = 20.0f; float line_height = 20.0f;
float padding = 5.0f; float padding = 5.0f;

View File

@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include "gfx/shape_renderer.h" #include "gfx/shape_renderer.h"
#include "gfx/txt_renderer.h" #include "gfx/txt_renderer.h"
#include "gfx/types.h"
UIWindow::UIWindow(const char* title, int x, int y, int width, int height) { UIWindow::UIWindow(const char* title, int x, int y, int width, int height) {
_title = title; _title = title;
@ -40,10 +41,10 @@ void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
int title_bar_height = 30; int title_bar_height = 30;
/* Define colours. */ /* Define colours. */
float frame_color[] = { 0.2f, 0.2f, 0.25f }; const Color frame_color = { 0.2f, 0.2f, 0.25f };
float title_bar_color[] = { 0.15f, 0.15f, 0.2f }; const Color title_bar_color = { 0.15f, 0.15f, 0.2f };
float focused_title_bar_color[] = { 0.3f, 0.3f, 0.4f }; const Color focused_title_bar_color = { 0.3f, 0.3f, 0.4f };
float title_text_color[] = { 0.9f, 0.9f, 0.9f }; const Color title_text_color = { 0.9f, 0.9f, 0.9f };
/* Convert top-left coords to bottom-left for OpenGL. */ /* Convert top-left coords to bottom-left for OpenGL. */
int y_gl = screen_height - _y - _height; int y_gl = screen_height - _y - _height;

View File

@ -5,7 +5,6 @@
#include "network_manager.h" #include "network_manager.h"
/* TODO: Re-implement. */
#include "asio/error_code.hpp" #include "asio/error_code.hpp"
#include "asio/ip/tcp.hpp" #include "asio/ip/tcp.hpp"
#include "command_processor.h" #include "command_processor.h"