Move rendering logic

This commit is contained in:
2025-11-02 16:55:11 +00:00
parent e1079ab82d
commit 93ba953738
2 changed files with 35 additions and 29 deletions

View File

@@ -11,9 +11,28 @@ namespace Quip {
namespace Widget {
TextLabel::TextLabel() = default;
TextLabel::TextLabel(std::string text, int xpos, int ypos) : text(std::move(text)), xpos(xpos), ypos(ypos) {}
void TextLabel::render(const Window &window) const {
constexpr SDL_Color color = {255, 255, 255, 255};
SDL_Surface* surface = TTF_RenderText_Blended(window.font, text.c_str(), 0, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(window.renderer, surface);
SDL_FRect destRect = {static_cast<float>(xpos), static_cast<float>(ypos), static_cast<float>(surface->w), static_cast<float>(surface->h)};
SDL_RenderTexture(window.renderer, texture, nullptr, &destRect);
SDL_DestroyTexture(texture);
SDL_DestroySurface(surface);
}
Button::Button(std::string text, std::function<void()> callback, int xpos, int ypos) : text(std::move(text)), callback(std::move(callback)), xpos(xpos), ypos(ypos) {
Button::Button(std::string text, std::function<void()> callback, int xpos, int ypos) : text(std::move(text)), callback(std::move(callback)), xpos(xpos), ypos(ypos) {}
void Button::render(const Window &window) const {
SDL_SetRenderDrawColor(window.renderer, 40, 60, 80, 255); // Dark gray background
SDL_FRect backgroundRect = {static_cast<float>(xpos), static_cast<float>(ypos), static_cast<float>(width), static_cast<float>(height)};
SDL_RenderFillRect(window.renderer, &backgroundRect);
constexpr SDL_Color color = {255, 255, 255, 255};
SDL_Surface* surface = TTF_RenderText_Blended(window.font, text.c_str(), 0, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(window.renderer, surface);
SDL_FRect destRect = {static_cast<float>(xpos), static_cast<float>(ypos), static_cast<float>(surface->w), static_cast<float>(surface->h)};
SDL_RenderTexture(window.renderer, texture, nullptr, &destRect);
SDL_DestroyTexture(texture);
SDL_DestroySurface(surface);
}
}
@@ -45,7 +64,7 @@ namespace Quip {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cerr << "SuperGUI::Window() error " << errCode << ": ";
std::cerr << "Quip::Window() error " << errCode << ": ";
switch (errCode) {
default:
case 0:
@@ -65,17 +84,18 @@ namespace Quip {
std::cerr << "Context: " << context << std::endl;
}
switch (errCode) {
default:
case 0:
throw std::runtime_error("SuperGUI::Window() Generic error");
throw std::runtime_error("Quip::Window() Generic error");
break;
case 1:
throw std::runtime_error("SuperGUI::Window() SDL error");
throw std::runtime_error("Quip::Window() SDL error");
break;
case 2:
throw std::runtime_error("SuperGUI::Window() Window size error");
throw std::runtime_error("Quip::Window() Window size error");
break;
case 3:
throw std::runtime_error("SuperGUI::Window() Widget name error");
throw std::runtime_error("Quip::Window() Widget name error");
break;
}
}
@@ -126,26 +146,9 @@ namespace Quip {
SDL_RenderClear(renderer);
for (auto const& [id, widget] : widgets) {
if (std::holds_alternative<Widget::TextLabel>(widget)) {
const auto& textLabel = std::get<Widget::TextLabel>(widget);
constexpr SDL_Color color = {255, 255, 255, 255};
SDL_Surface* surface = TTF_RenderText_Blended(font, textLabel.text.c_str(), 0, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FRect destRect = {static_cast<float>(textLabel.xpos), static_cast<float>(textLabel.ypos), static_cast<float>(surface->w), static_cast<float>(surface->h)};
SDL_RenderTexture(renderer, texture, nullptr, &destRect);
SDL_DestroyTexture(texture);
SDL_DestroySurface(surface);
std::get<Widget::TextLabel>(widget).render(*this);
} else if (std::holds_alternative<Widget::Button>(widget)) {
const auto& button = std::get<Widget::Button>(widget);
SDL_SetRenderDrawColor(renderer, 40, 60, 80, 255); // Dark gray background
SDL_FRect backgroundRect = {static_cast<float>(button.xpos), static_cast<float>(button.ypos), static_cast<float>(button.width), static_cast<float>(button.height)};
SDL_RenderFillRect(renderer, &backgroundRect);
constexpr SDL_Color color = {255, 255, 255, 255};
SDL_Surface* surface = TTF_RenderText_Blended(font, button.text.c_str(), 0, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FRect destRect = {static_cast<float>(button.xpos), static_cast<float>(button.ypos), static_cast<float>(surface->w), static_cast<float>(surface->h)};
SDL_RenderTexture(renderer, texture, nullptr, &destRect);
SDL_DestroyTexture(texture);
SDL_DestroySurface(surface);
std::get<Widget::Button>(widget).render(*this);
}
}
if (!SDL_RenderPresent(renderer)) {

View File

@@ -8,12 +8,14 @@
#include <map>
namespace Quip {
class Window;
extern int windowCount;
namespace Widget {
class TextLabel {
public:
std::string text;
int xpos, ypos = 0;
int xpos = 0, ypos = 0;
void render(const Window &window) const;
explicit TextLabel(std::string text, int xpos, int ypos);
TextLabel();
};
@@ -21,8 +23,9 @@ namespace Quip {
public:
std::string text;
std::function<void()> callback;
int xpos, ypos = 0;
int xpos = 0, ypos = 0;
int width = 100, height = 30;
void render(const Window &window) const;
explicit Button(std::string text, std::function<void()> callback, int xpos, int ypos);
};
@@ -30,6 +33,7 @@ namespace Quip {
}
class Window {
public:
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event event;
@@ -38,7 +42,6 @@ namespace Quip {
int width, height;
std::map<std::string, Widget::Widget> widgets;
void error(int errCode = 0, const std::string& context = "");
public:
int run();
void addChild(const std::string& id, const Widget::Widget& widget);
Widget::Widget* getChild(const std::string& id);