bettola/client/src/ui/boot_sequence.cpp
Ritchie Cunningham e7607e3fc0 [Refactor] Created UIRenderer for consistant coord system.
The UI codebase was suffering with complexity due to the need to
manually convert between two different coordinate systems:
- Top-down "screen coordinates" used by SDL for input and windowing.
- Bottom-up "GL coordinates" used by low-level renderers.

This was making layout calculations diffucult and is bug prone.

With this commit, I'm introducing a 'UIRenderer' abstraction layer that
wraps the low-level 'ShapeRenderer' and 'TextRenderer'. This is
responsible for centralising all coordinate system conversations.

All UI components has been refactored to use the 'UIRenderer' so the
entire UI code opeates exclusively in a single, top-down screen
coordinate system as one would expect.
2025-10-04 03:11:48 +01:00

55 lines
1.5 KiB
C++

#include <fstream>
#include <string>
#include <vector>
#include <cstdio>
#include "ui/ui_renderer.h"
#include "boot_sequence.h"
#include <SDL3/SDL_timer.h>
BootSequence::BootSequence(void) {
/* Load boot messages. */
std::ifstream boot_file("assets/boot_messages.txt");
if(!boot_file.is_open()) {
printf("ERROR: Failed to open assets/boot_messages.txt\n");
_messages.push_back("ERROR: boot_messages.txt not found.");
} else {
std::string line;
while(std::getline(boot_file, line)) {
if(!line.empty()) {
_messages.push_back(line);
}
}
}
/* Init timings. */
_start_time = SDL_GetTicks();
_line_interval_ms = 150; /* 150ms between each line. */
/* Total duration is time for all lines plus a 2-second pause at the end. */
_total_duration_ms = (_messages.size() * _line_interval_ms) + 2000;
}
BootSequence::~BootSequence(void) {}
bool BootSequence::is_finished(void) {
return (SDL_GetTicks() - _start_time) >= _total_duration_ms;
}
void BootSequence::render(UIRenderer* ui_renderer) {
const Color text_color = { 0.9f, 0.9f, 0.9f }; /* grey/white */
const float line_height = 18.0f;
const float padding = 15.0f;
Uint32 elapsed_time = SDL_GetTicks() - _start_time;
int lines_to_show = elapsed_time / _line_interval_ms;
if(lines_to_show > _messages.size()) {
lines_to_show = _messages.size();
}
for(int i = 0; i < lines_to_show; ++i) {
float y_pos = padding + (i*line_height);
ui_renderer->render_text(_messages[i].c_str(), padding, y_pos, text_color);
}
}