This commit is contained in:
2026-06-17 10:35:08 -07:00
parent e3b09170a4
commit 5355a9eded
9 changed files with 1836 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
/*
* 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;
/* 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 instance — shared by all panels and radar stubs. */
extern TextRenderer g_text_renderer;