Files
modular-radar/include/02_text_description.h

108 lines
4.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* MIT License
*
* Copyright (c) 2026 Mark Allyn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* 02_text_description.h
* Author: Mark Allyn
* Component: 02_text_description
*
* FreeType-based text renderer used by all three screen panels
* (left description panel, scope panel, status bar).
*
* Usage:
* 1. Call g_text_renderer.initialize_shaders() after GLAD is loaded.
* 2. Call g_text_renderer.initialize_font(FONT_PATH, FONT_SIZE_NORMAL).
* 3. Before rendering into a viewport, call set_projection(w, h).
* 4. Call render_text() with viewport-local pixel coordinates.
*/
#pragma once
#include <string>
#include <map>
#include <glad/glad.h>
#include "data_structure_and_constants.h"
#include "settings.h"
/* Cached metrics for one loaded glyph. */
struct glyph_character {
GLuint texture_id; // GL_RED single-channel texture
int size_x; // bitmap width in pixels
int size_y; // bitmap height in pixels
int bearing_x; // pixel offset from cursor to left of glyph
int bearing_y; // pixel offset from cursor baseline to top of glyph
unsigned int advance; // horizontal advance in 1/64-pixel units
};
class TextRenderer {
public:
TextRenderer() : vao_(0), vbo_(0), shader_program_(0) {}
/* Compile and link 02_text_description_{vert,frag}.glsl.
* Must be called once, after GLAD is initialised. */
bool initialize_shaders();
/* Load glyphs for printable ASCII (32126) from the given font file.
* font_size is the desired pixel height. */
bool initialize_font(const std::string& font_path, unsigned int font_size);
/* Render text at (x, y) in viewport-local pixel coordinates.
* y=0 is the bottom of the current viewport.
* scale multiplies the loaded glyph size.
* color_r/g/b are 0.01.0. */
void render_text(const std::string& text,
float x, float y, float scale,
float color_r, float color_g, float color_b);
/* Return the rendered pixel width of text at the given scale. */
float get_text_width(const std::string& text, float scale) const;
/* Render text at (x, y), wrapping on word boundaries when a line would
* exceed max_width pixels. y is decremented by line_height * scale after
* each rendered line, including the last. Never breaks within a word. */
void render_text_wrapped(const std::string& text,
float x, float& y, float scale,
float max_width, float line_height,
float color_r, float color_g, float color_b);
/* Set the orthographic projection for a viewport of w × h pixels.
* Call this each time glViewport changes before rendering text. */
void set_projection(int w, int h);
/* Release all GL resources and glyph textures. */
void cleanup();
private:
std::map<char, glyph_character> characters_;
GLuint vao_;
GLuint vbo_;
GLuint shader_program_;
};
/* Global instances — shared by all panels and radar stubs.
* g_text_renderer : FONT_SIZE_NORMAL (body text, controls, status bar)
* g_text_renderer_large: FONT_SIZE_TITLE (panel titles, scope headings) */
extern TextRenderer g_text_renderer;
extern TextRenderer g_text_renderer_large;