#include "render/ui_renderer.hpp" #include "core/log.hpp" #include #include #include #include namespace top_octane::render { namespace { constexpr const char* kUiVS = R"glsl( #version 330 core layout(location = 0) in vec2 aPos; layout(location = 1) in vec2 aUv; layout(location = 2) in vec4 aColor; uniform mat4 uOrtho; out vec2 vUv; out vec4 vColor; void main() { vUv = aUv; vColor = aColor; gl_Position = uOrtho * vec4(aPos, 0.0, 1.0); } )glsl"; constexpr const char* kUiFS = R"glsl( #version 330 core in vec2 vUv; in vec4 vColor; uniform sampler2D uTex; out vec4 FragColor; void main() { vec4 t = texture(uTex, vUv); vec4 res = t * vColor; if (res.a < 0.01) discard; FragColor = res; } )glsl"; // Compact 5x7 bitmap font for ASCII 32..95 (7 rows x 5 bits per glyph) struct Glyph5x7 { uint8_t rows[7]; }; const Glyph5x7& get_glyph(char ch) { unsigned char uc = static_cast(std::toupper(static_cast(ch))); static constexpr Glyph5x7 kBlank{{0, 0, 0, 0, 0, 0, 0}}; static constexpr Glyph5x7 kTable[64] = { {{0x00,0x00,0x00,0x00,0x00,0x00,0x00}}, // ' ' 32 {{0x04,0x04,0x04,0x04,0x04,0x00,0x04}}, // '!' 33 {{0x0A,0x0A,0x00,0x00,0x00,0x00,0x00}}, // '"' 34 {{0x0A,0x1F,0x0A,0x0A,0x1F,0x0A,0x00}}, // '#' 35 {{0x04,0x0F,0x14,0x0E,0x05,0x1E,0x04}}, // '$' 36 {{0x18,0x19,0x02,0x04,0x08,0x13,0x03}}, // '%' 37 {{0x0C,0x12,0x14,0x08,0x15,0x12,0x0D}}, // '&' 38 {{0x06,0x04,0x08,0x00,0x00,0x00,0x00}}, // ''' 39 {{0x02,0x04,0x08,0x08,0x08,0x04,0x02}}, // '(' 40 {{0x08,0x04,0x02,0x02,0x02,0x04,0x08}}, // ')' 41 {{0x00,0x0A,0x04,0x1F,0x04,0x0A,0x00}}, // '*' 42 {{0x00,0x04,0x04,0x1F,0x04,0x04,0x00}}, // '+' 43 {{0x00,0x00,0x00,0x00,0x06,0x04,0x08}}, // ',' 44 {{0x00,0x00,0x00,0x1F,0x00,0x00,0x00}}, // '-' 45 {{0x00,0x00,0x00,0x00,0x00,0x06,0x06}}, // '.' 46 {{0x01,0x01,0x02,0x04,0x08,0x10,0x10}}, // '/' 47 {{0x0E,0x11,0x13,0x15,0x19,0x11,0x0E}}, // '0' 48 {{0x04,0x0C,0x04,0x04,0x04,0x04,0x0E}}, // '1' 49 {{0x0E,0x11,0x01,0x02,0x04,0x08,0x1F}}, // '2' 50 {{0x1F,0x02,0x04,0x02,0x01,0x11,0x0E}}, // '3' 51 {{0x02,0x06,0x0A,0x12,0x1F,0x02,0x02}}, // '4' 52 {{0x1F,0x10,0x1E,0x01,0x01,0x11,0x0E}}, // '5' 53 {{0x06,0x08,0x10,0x1E,0x11,0x11,0x0E}}, // '6' 54 {{0x1F,0x01,0x02,0x04,0x08,0x08,0x08}}, // '7' 55 {{0x0E,0x11,0x11,0x0E,0x11,0x11,0x0E}}, // '8' 56 {{0x0E,0x11,0x11,0x0F,0x01,0x02,0x0C}}, // '9' 57 {{0x00,0x06,0x06,0x00,0x06,0x06,0x00}}, // ':' 58 {{0x00,0x06,0x06,0x00,0x06,0x04,0x08}}, // ';' 59 {{0x02,0x04,0x08,0x10,0x08,0x04,0x02}}, // '<' 60 {{0x00,0x00,0x1F,0x00,0x1F,0x00,0x00}}, // '=' 61 {{0x08,0x04,0x02,0x01,0x02,0x04,0x08}}, // '>' 62 {{0x0E,0x11,0x01,0x02,0x04,0x00,0x04}}, // '?' 63 {{0x0E,0x11,0x17,0x15,0x17,0x10,0x0E}}, // '@' 64 {{0x0E,0x11,0x11,0x1F,0x11,0x11,0x11}}, // 'A' 65 {{0x1E,0x11,0x11,0x1E,0x11,0x11,0x1E}}, // 'B' 66 {{0x0E,0x11,0x10,0x10,0x10,0x11,0x0E}}, // 'C' 67 {{0x1E,0x11,0x11,0x11,0x11,0x11,0x1E}}, // 'D' 68 {{0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F}}, // 'E' 69 {{0x1F,0x10,0x10,0x1E,0x10,0x10,0x10}}, // 'F' 70 {{0x0E,0x11,0x10,0x17,0x11,0x11,0x0F}}, // 'G' 71 {{0x11,0x11,0x11,0x1F,0x11,0x11,0x11}}, // 'H' 72 {{0x0E,0x04,0x04,0x04,0x04,0x04,0x0E}}, // 'I' 73 {{0x07,0x02,0x02,0x02,0x02,0x12,0x0C}}, // 'J' 74 {{0x11,0x12,0x14,0x18,0x14,0x12,0x11}}, // 'K' 75 {{0x10,0x10,0x10,0x10,0x10,0x10,0x1F}}, // 'L' 76 {{0x11,0x1B,0x15,0x15,0x11,0x11,0x11}}, // 'M' 77 {{0x11,0x19,0x15,0x13,0x11,0x11,0x11}}, // 'N' 78 {{0x0E,0x11,0x11,0x11,0x11,0x11,0x0E}}, // 'O' 79 {{0x1E,0x11,0x11,0x1E,0x10,0x10,0x10}}, // 'P' 80 {{0x0E,0x11,0x11,0x11,0x15,0x12,0x0D}}, // 'Q' 81 {{0x1E,0x11,0x11,0x1E,0x14,0x12,0x11}}, // 'R' 82 {{0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E}}, // 'S' 83 {{0x1F,0x04,0x04,0x04,0x04,0x04,0x04}}, // 'T' 84 {{0x11,0x11,0x11,0x11,0x11,0x11,0x0E}}, // 'U' 85 {{0x11,0x11,0x11,0x11,0x11,0x0A,0x04}}, // 'V' 86 {{0x11,0x11,0x11,0x15,0x15,0x1B,0x11}}, // 'W' 87 {{0x11,0x11,0x0A,0x04,0x0A,0x11,0x11}}, // 'X' 88 {{0x11,0x11,0x0A,0x04,0x04,0x04,0x04}}, // 'Y' 89 {{0x1F,0x01,0x02,0x04,0x08,0x10,0x1F}}, // 'Z' 90 {{0x0E,0x08,0x08,0x08,0x08,0x08,0x0E}}, // '[' 91 {{0x10,0x10,0x08,0x04,0x02,0x01,0x01}}, // '\' 92 {{0x0E,0x02,0x02,0x02,0x02,0x02,0x0E}}, // ']' 93 {{0x04,0x0A,0x11,0x00,0x00,0x00,0x00}}, // '^' 94 {{0x00,0x00,0x00,0x00,0x00,0x00,0x1F}}, // '_' 95 }; if (uc >= 32 && uc < 96) return kTable[uc - 32]; return kBlank; } } // namespace UiRenderer::~UiRenderer() { shutdown(); } void UiRenderer::upload_rgba(GLuint& tex, int w, int h, const uint8_t* rgba, bool linear) { if (!tex) glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); GLint f = linear ? GL_LINEAR : GL_NEAREST; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, f); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, f); } void UiRenderer::load_bank(const Vfs& vfs, const assets::Palette& pal, const std::string& key, const std::string& stem) { assets::SpriteBank sb; if (!sb.load(vfs, "DATA/" + stem + ".DAT", "DATA/" + stem + ".TAB", pal)) { LOG_WARN("Optional sprite bank failed to load: %s", stem.c_str()); return; } auto& vec = banks_[key]; vec.resize(sb.sprites.size()); for (size_t i = 0; i < sb.sprites.size(); ++i) { const auto& sp = sb.sprites[i]; if (sp.width > 0 && sp.height > 0 && !sp.rgba.empty()) { vec[i].width = sp.width; vec[i].height = sp.height; upload_rgba(vec[i].tex, sp.width, sp.height, sp.rgba.data(), true); } } } bool UiRenderer::init(const Vfs& vfs, const assets::Palette& pal) { if (initialized_) return true; if (!ui_shader_.build(kUiVS, kUiFS, "ui")) return false; static constexpr uint8_t kWhite[4] = {255, 255, 255, 255}; upload_rgba(white_tex_, 1, 1, kWhite, false); gl::GenVertexArrays(1, &vao_); gl::GenBuffers(1, &vbo_); gl::BindVertexArray(vao_); gl::BindBuffer(GL_ARRAY_BUFFER, vbo_); gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW); gl::EnableVertexAttribArray(0); gl::VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(UiVertex), nullptr); gl::EnableVertexAttribArray(1); gl::VertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(UiVertex), reinterpret_cast(2 * sizeof(float))); gl::EnableVertexAttribArray(2); gl::VertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(UiVertex), reinterpret_cast(4 * sizeof(float))); gl::BindVertexArray(0); // Load key Hi-Res Bullfrog screens & banks auto load_scr = [&](const std::string& key, const std::string& path, int w, int h) { assets::SpriteImage img; if (assets::load_raw_screen(vfs, path, w, h, pal, img)) { GpuSpriteTex gt; gt.width = w; gt.height = h; upload_rgba(gt.tex, w, h, img.rgba.data(), true); screens_[key] = gt; } }; load_scr("title", "DATA/TITLE.DAT", 320, 200); load_scr("oscr", "DATA/OSCR0-1.DAT", 640, 480); load_scr("onet", "DATA/ONET0-1.DAT", 640, 480); load_bank(vfs, pal, "logo", "LOGO0-1"); load_bank(vfs, pal, "panel", "PANEL0-1"); load_bank(vfs, pal, "track", "TRACK0-1"); initialized_ = true; return true; } void UiRenderer::shutdown() { if (!initialized_) return; if (white_tex_) glDeleteTextures(1, &white_tex_); if (stream_tex_) glDeleteTextures(1, &stream_tex_); for (auto& [k, s] : screens_) { if (s.tex) glDeleteTextures(1, &s.tex); } for (auto& [k, vec] : banks_) { for (auto& s : vec) { if (s.tex) glDeleteTextures(1, &s.tex); } } screens_.clear(); banks_.clear(); if (gl::is_loaded()) { if (vao_) gl::DeleteVertexArrays(1, &vao_); if (vbo_) gl::DeleteBuffers(1, &vbo_); } white_tex_ = stream_tex_ = vao_ = vbo_ = 0; ui_shader_.destroy(); initialized_ = false; } void UiRenderer::begin(int screen_w, int screen_h) { screen_w_ = std::max(screen_w, 1); screen_h_ = std::max(screen_h, 1); verts_.clear(); batches_.clear(); } void UiRenderer::push_quad(GLuint tex, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, const Vec4& c) { uint32_t start = static_cast(verts_.size()); verts_.push_back({x0, y0, u0, v0, c.x, c.y, c.z, c.w}); verts_.push_back({x1, y0, u1, v0, c.x, c.y, c.z, c.w}); verts_.push_back({x1, y1, u1, v1, c.x, c.y, c.z, c.w}); verts_.push_back({x0, y0, u0, v0, c.x, c.y, c.z, c.w}); verts_.push_back({x1, y1, u1, v1, c.x, c.y, c.z, c.w}); verts_.push_back({x0, y1, u0, v1, c.x, c.y, c.z, c.w}); if (!batches_.empty() && batches_.back().tex == tex) { batches_.back().count += 6; } else { batches_.push_back({tex, start, 6}); } } void UiRenderer::end() { if (verts_.empty()) return; glViewport(0, 0, screen_w_, screen_h_); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Mat4 ortho = Mat4::ortho(0.0f, static_cast(screen_w_), static_cast(screen_h_), 0.0f, -1.0f, 1.0f); ui_shader_.use(); ui_shader_.set_mat4("uOrtho", ortho); ui_shader_.set_int("uTex", 0); gl::BindVertexArray(vao_); gl::BindBuffer(GL_ARRAY_BUFFER, vbo_); gl::BufferData(GL_ARRAY_BUFFER, static_cast(verts_.size() * sizeof(UiVertex)), verts_.data(), GL_DYNAMIC_DRAW); gl::ActiveTexture(GL_TEXTURE0); for (const auto& b : batches_) { glBindTexture(GL_TEXTURE_2D, b.tex); glDrawArrays(GL_TRIANGLES, static_cast(b.first), static_cast(b.count)); } gl::BindVertexArray(0); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); } void UiRenderer::draw_rect(float x, float y, float w, float h, const Vec4& color) { push_quad(white_tex_, x, y, x + w, y + h, 0, 0, 1, 1, color); } void UiRenderer::draw_rect_outline(float x, float y, float w, float h, float t, const Vec4& color) { draw_rect(x, y, w, t, color); draw_rect(x, y + h - t, w, t, color); draw_rect(x, y + t, t, std::max(0.0f, h - 2 * t), color); draw_rect(x + w - t, y + t, t, std::max(0.0f, h - 2 * t), color); } void UiRenderer::draw_line(float x0, float y0, float x1, float y1, float thickness, const Vec4& color) { float dx = x1 - x0; float dy = y1 - y0; float len = std::sqrt(dx * dx + dy * dy); if (len < 0.5f) return; float nx = (-dy / len) * (thickness * 0.5f); float ny = (dx / len) * (thickness * 0.5f); uint32_t start = static_cast(verts_.size()); verts_.push_back({x0 - nx, y0 - ny, 0, 0, color.x, color.y, color.z, color.w}); verts_.push_back({x1 - nx, y1 - ny, 1, 0, color.x, color.y, color.z, color.w}); verts_.push_back({x1 + nx, y1 + ny, 1, 1, color.x, color.y, color.z, color.w}); verts_.push_back({x0 - nx, y0 - ny, 0, 0, color.x, color.y, color.z, color.w}); verts_.push_back({x1 + nx, y1 + ny, 1, 1, color.x, color.y, color.z, color.w}); verts_.push_back({x0 + nx, y0 + ny, 0, 1, color.x, color.y, color.z, color.w}); if (!batches_.empty() && batches_.back().tex == white_tex_) { batches_.back().count += 6; } else { batches_.push_back({white_tex_, start, 6}); } } float UiRenderer::measure_text_w(const std::string& text, float scale) const { if (text.empty()) return 0.0f; return static_cast(text.size()) * 6.0f * scale - scale; } void UiRenderer::draw_text(float x, float y, float scale, const std::string& text, const Vec4& color, bool shadow) { if (shadow) { Vec4 sh(0.0f, 0.0f, 0.0f, color.w * 0.78f); draw_text(x + scale, y + scale, scale, text, sh, false); } float cx = x; for (char ch : text) { const Glyph5x7& g = get_glyph(ch); for (int r = 0; r < 7; ++r) { uint8_t row = g.rows[r]; if (row == 0) continue; for (int c = 0; c < 5; ++c) { if (row & (1u << (4 - c))) { draw_rect(cx + c * scale, y + r * scale, scale, scale, color); } } } cx += 6.0f * scale; } } void UiRenderer::draw_text_centered(float cx, float y, float scale, const std::string& text, const Vec4& color, bool shadow) { float w = measure_text_w(text, scale); draw_text(cx - w * 0.5f, y, scale, text, color, shadow); } void UiRenderer::draw_screen_cover(const std::string& screen_key, float alpha) { auto it = screens_.find(screen_key); if (it == screens_.end() || !it->second.tex) return; push_quad(it->second.tex, 0, 0, static_cast(screen_w_), static_cast(screen_h_), 0, 0, 1, 1, Vec4(1, 1, 1, alpha)); } void UiRenderer::draw_screen_4_3(const std::string& screen_key, float alpha) { auto it = screens_.find(screen_key); if (it == screens_.end() || !it->second.tex) return; float target_aspect = 4.0f / 3.0f; float win_aspect = static_cast(screen_w_) / static_cast(screen_h_); float w = static_cast(screen_w_); float h = static_cast(screen_h_); float x = 0.0f, y = 0.0f; if (win_aspect > target_aspect) { w = h * target_aspect; x = (static_cast(screen_w_) - w) * 0.5f; } else { h = w / target_aspect; y = (static_cast(screen_h_) - h) * 0.5f; } push_quad(it->second.tex, x, y, x + w, y + h, 0, 0, 1, 1, Vec4(1, 1, 1, alpha)); } void UiRenderer::draw_bank_sprite(const std::string& bank_key, int index, float x, float y, float scale, const Vec4& tint) { auto it = banks_.find(bank_key); if (it == banks_.end() || index < 0 || static_cast(index) >= it->second.size()) return; const auto& sp = it->second[index]; if (!sp.tex || sp.width <= 0 || sp.height <= 0) return; push_quad(sp.tex, x, y, x + sp.width * scale, y + sp.height * scale, 0, 0, 1, 1, tint); } bool UiRenderer::get_bank_sprite_size(const std::string& bank_key, int index, int& out_w, int& out_h) const { auto it = banks_.find(bank_key); if (it == banks_.end() || index < 0 || static_cast(index) >= it->second.size()) { return false; } out_w = it->second[index].width; out_h = it->second[index].height; return out_w > 0 && out_h > 0; } void UiRenderer::update_stream_rgba(int w, int h, const uint8_t* rgba) { if (w <= 0 || h <= 0 || !rgba) return; if (!stream_tex_ || stream_w_ != w || stream_h_ != h) { stream_w_ = w; stream_h_ = h; upload_rgba(stream_tex_, w, h, rgba, true); } else { glBindTexture(GL_TEXTURE_2D, stream_tex_); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, rgba); } } void UiRenderer::draw_stream_4_3() { if (!stream_tex_) return; float target_aspect = 4.0f / 3.0f; float win_aspect = static_cast(screen_w_) / static_cast(screen_h_); float w = static_cast(screen_w_); float h = static_cast(screen_h_); float x = 0.0f, y = 0.0f; if (win_aspect > target_aspect) { w = h * target_aspect; x = (static_cast(screen_w_) - w) * 0.5f; } else { h = w / target_aspect; y = (static_cast(screen_h_) - h) * 0.5f; } push_quad(stream_tex_, x, y, x + w, y + h, 0, 0, 1, 1, Vec4(1, 1, 1, 1)); } } // namespace top_octane::render