Files
radiantwave/src/license_blocked_page.cpp
T
2026-08-07 14:51:41 -05:00

117 lines
3.3 KiB
C++

#include "license_blocked_page.h"
#include "app_state.h"
#include "home.h"
#include <SDL3/SDL.h>
void LicenseBlockedPage::onPush(AppState& state)
{
state_ = &state;
TTF_Init();
createFonts();
}
void LicenseBlockedPage::onResume(AppState& state)
{
state_ = &state;
createFonts();
}
void LicenseBlockedPage::onPause(AppState& state)
{
destroyFonts();
destroyTextures();
}
void LicenseBlockedPage::onPop(AppState& state)
{
destroyFonts();
destroyTextures();
TTF_Quit();
}
void LicenseBlockedPage::createFonts()
{
destroyFonts();
fontMessage_ = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 40);
fontPrompt_ = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 24);
}
void LicenseBlockedPage::destroyFonts()
{
if (fontMessage_) { TTF_CloseFont(fontMessage_); fontMessage_ = nullptr; }
if (fontPrompt_) { TTF_CloseFont(fontPrompt_); fontPrompt_ = nullptr; }
}
void LicenseBlockedPage::destroyTextures()
{
messageText_.destroy();
infoText_.destroy();
promptText_.destroy();
}
void LicenseBlockedPage::handleEvent(const SDL_Event& event)
{
if (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE) {
// Return to the main screen regardless of where the block was raised
// (Play on Home, or Proclamations inside Settings). Snapshot the
// state pointer first: the first pop() destroys this page, so reading
// the state_ member afterwards would be a use-after-free.
AppState* state = state_;
while (state->pages.depth() > 0) {
state->pages.pop(*state);
}
state->pages.push(*state, std::make_unique<Home>());
}
}
void LicenseBlockedPage::update(float dt)
{
}
void LicenseBlockedPage::render(SDL_Renderer* renderer)
{
int winW, winH;
SDL_GetWindowSize(SDL_GetRenderWindow(renderer), &winW, &winH);
float fw = static_cast<float>(winW);
float fh = static_cast<float>(winH);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 15, 18, 25, 255);
SDL_FRect bgRect{0, 0, fw, fh};
SDL_RenderFillRect(renderer, &bgRect);
if (!fontMessage_ || !fontPrompt_) return;
SDL_Color white = {255, 255, 255, 255};
SDL_Color gray = {170, 170, 170, 255};
const std::string message = "License could not be verified";
const std::string info =
"Please check that you are connected to the internet, and double check "
"your license key and email address.";
const std::string prompt = "Press ESC to return.";
messageText_.ensure(renderer, fontMessage_, message, white);
infoText_.ensure(renderer, fontPrompt_, info, gray);
promptText_.ensure(renderer, fontPrompt_, prompt, gray);
constexpr float gap = 24.0f;
float totalH = 0;
float maxW = 0;
for (const CachedText* t : {&messageText_, &infoText_, &promptText_}) {
maxW = std::max(maxW, t->width);
totalH += (totalH > 0 ? gap : 0.0f) + t->height;
}
float x = (fw - maxW) / 2.0f;
float y = (fh - totalH) / 2.0f;
for (const CachedText* t : {&messageText_, &infoText_, &promptText_}) {
if (t->texture) {
SDL_FRect dst{x + (maxW - t->width) / 2.0f, y, t->width, t->height};
SDL_RenderTexture(renderer, t->texture, nullptr, &dst);
y += t->height + gap;
}
}
}