2021-08-06 14:22:56 +02:00
|
|
|
#ifndef BWIDGETS_EXAMPLES_RUN_HPP
|
|
|
|
#define BWIDGETS_EXAMPLES_RUN_HPP
|
2021-07-20 12:12:29 +02:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
2021-08-06 14:22:56 +02:00
|
|
|
#include <stack>
|
2021-07-20 12:12:29 +02:00
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
|
|
|
|
#include <basic_widgets/core/font.hpp>
|
|
|
|
#include <basic_widgets/core/renderer.hpp>
|
2021-08-06 14:22:56 +02:00
|
|
|
#include <basic_widgets/w/aligned_layout.hpp>
|
2021-07-20 12:12:29 +02:00
|
|
|
#include <basic_widgets/w/base/widget.hpp>
|
|
|
|
|
|
|
|
template<typename T>
|
2021-08-06 14:22:56 +02:00
|
|
|
concept WidgetType = std::derived_from<T, bwidgets::Widget>;
|
2021-07-20 12:12:29 +02:00
|
|
|
|
2021-07-28 15:15:32 +02:00
|
|
|
template<WidgetType W>
|
2021-08-06 14:22:56 +02:00
|
|
|
void run_example(std::function<void(W*, bwidgets::Font*, int, int)> setup, int w = 3,
|
2021-07-29 16:06:03 +02:00
|
|
|
int h = 3)
|
2021-07-20 12:12:29 +02:00
|
|
|
{
|
2021-08-06 14:22:56 +02:00
|
|
|
std::stack<std::function<void()>> cleaners;
|
2021-07-29 16:06:03 +02:00
|
|
|
try {
|
2021-08-06 14:22:56 +02:00
|
|
|
bwidgets::SDLError::success_or_throw(SDL_Init(SDL_INIT_VIDEO), __FILE__,
|
|
|
|
__FUNCTION__, __LINE__);
|
|
|
|
cleaners.emplace([]() { SDL_Quit(); });
|
|
|
|
bwidgets::SDLError::success_or_throw(TTF_Init(), __FILE__, __FUNCTION__,
|
|
|
|
__LINE__);
|
|
|
|
cleaners.emplace([]() { TTF_Quit(); });
|
2021-07-20 12:12:29 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
const bwidgets::Size size_init {854, 480};
|
|
|
|
auto* font {new bwidgets::Font(bwidgets::Font::find("Monospace"),
|
|
|
|
16)}; // NOLINT(readability-magic-numbers)
|
|
|
|
cleaners.emplace([font]() { delete font; });
|
2021-07-29 16:06:03 +02:00
|
|
|
auto* win {SDL_CreateWindow("basic_widgets example", SDL_WINDOWPOS_CENTERED,
|
|
|
|
SDL_WINDOWPOS_CENTERED, size_init.w, size_init.h,
|
|
|
|
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
|
|
|
|
| SDL_WINDOW_UTILITY)};
|
2021-08-06 14:22:56 +02:00
|
|
|
cleaners.emplace([win]() { SDL_DestroyWindow(win); });
|
|
|
|
auto* renderer {new bwidgets::Renderer(win, -1, SDL_RENDERER_ACCELERATED)};
|
|
|
|
cleaners.emplace([renderer]() { delete renderer; });
|
2021-07-26 22:57:28 +02:00
|
|
|
renderer->blend_mode(SDL_BLENDMODE_BLEND);
|
2021-07-20 12:12:29 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
auto* layout {
|
|
|
|
new bwidgets::AlignedLayout(bwidgets::AlignedLayout::Alignment::HORIZONTAL)};
|
|
|
|
cleaners.emplace([layout]() { delete layout; });
|
2021-07-20 12:12:29 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
for (auto x = 0; x < w; x++) {
|
2021-08-06 14:22:56 +02:00
|
|
|
auto* col =
|
|
|
|
new bwidgets::AlignedLayout(bwidgets::AlignedLayout::Alignment::VERTICAL);
|
2021-07-29 16:06:03 +02:00
|
|
|
for (auto y = 0; y < h; y++) {
|
2021-07-20 12:12:29 +02:00
|
|
|
auto* widget = new W();
|
|
|
|
setup(widget, font, x, y);
|
|
|
|
col->add_widget(widget);
|
|
|
|
}
|
|
|
|
layout->add_widget(col);
|
|
|
|
}
|
|
|
|
|
|
|
|
layout->renderer(renderer);
|
|
|
|
layout->viewport({0, 0, size_init.w, size_init.h});
|
|
|
|
|
|
|
|
bool quit {false};
|
2021-07-29 16:06:03 +02:00
|
|
|
while (!quit) {
|
2021-07-20 12:12:29 +02:00
|
|
|
SDL_Event ev;
|
2021-07-29 16:06:03 +02:00
|
|
|
while (SDL_PollEvent(&ev) != 0) {
|
|
|
|
switch (ev.type) {
|
2021-07-20 12:12:29 +02:00
|
|
|
case SDL_QUIT:
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT:
|
2021-07-29 16:06:03 +02:00
|
|
|
if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
2021-07-20 12:12:29 +02:00
|
|
|
auto size = renderer->output_size();
|
|
|
|
layout->viewport({0, 0, size.w, size.h});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
layout->handle_event(ev);
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
// NOLINTNEXTLINE(readability-magic-numbers)
|
|
|
|
renderer->draw_color({50, 60, 70, SDL_ALPHA_OPAQUE});
|
2021-07-20 12:12:29 +02:00
|
|
|
renderer->clear();
|
|
|
|
|
|
|
|
layout->render();
|
|
|
|
renderer->present();
|
|
|
|
}
|
2021-08-06 14:22:56 +02:00
|
|
|
while (!cleaners.empty()) cleaners.top()(), cleaners.pop();
|
|
|
|
} catch (const bwidgets::SDLError& e) {
|
2021-07-29 16:06:03 +02:00
|
|
|
std::cerr << "Error: " << e.file << ":" << e.func << ":" << e.line << ": "
|
|
|
|
<< e.what << std::endl;
|
2021-08-06 14:22:56 +02:00
|
|
|
while (!cleaners.empty()) cleaners.top()(), cleaners.pop();
|
2021-07-20 12:12:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|