half workaround to fix corner glitch in draw_rect and minor changes

This commit is contained in:
Andrea Blankenstijn 2021-07-26 22:57:28 +02:00
parent 369e1c1f1b
commit 55fbd59c36
11 changed files with 101 additions and 7 deletions

View file

@ -13,7 +13,7 @@ int main()
std::cout << "button(" << x << ',' << y << "):click!" << std::endl;
};
w->font(f);
w->text("¡jello!");
w->text("+");
});
return 0;
}

View file

@ -0,0 +1,11 @@
#include "example_widget.hpp"
#include "run.hpp"
int main()
{
run_example<Example>([](Example* w, core::Font*, int x, int y) {
w->cycle_r = (x + 1) * 3000;
w->cycle_b = (y + 1) * 3000;
w->cycle_b = (1 + x + y) * (y + 1) * 400;
});
}

View file

@ -0,0 +1,55 @@
#ifndef BWIDGET_EXAMPLES_EXAMPLE_WIDGET_
#define BWIDGET_EXAMPLES_EXAMPLE_WIDGET_
#include <SDL2/SDL_timer.h>
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/renderer.hpp>
#include <basic_widgets/w/base/widget.hpp>
using bwidgets::core::rect_margin;
using bwidgets::core::Renderer;
using bwidgets::core::Size;
using bwidgets::widget::Widget;
class Example final : public Widget
{
private:
virtual void _handle_geometry_change(const SDL_Rect& vp) noexcept override
{
_widget_area = {2, 2, vp.w - 4, vp.h - 4};
}
virtual void _handle_rendering() override
{
auto now = SDL_GetTicks();
uint8_t r = 255 * (now % cycle_r / (float)cycle_r);
uint8_t g = 255 * (now % cycle_g / (float)cycle_g);
uint8_t b = 255 * (now % cycle_b / (float)cycle_b);
SDL_Color base_color {r, g, b, 255};
int border = 10;
for (auto i = 0; i < border; i+=3)
{
uint8_t alpha = 255 * i / border;
_renderer->draw_color({base_color.r, base_color.g, base_color.b, alpha})
->draw_rect(rect_margin(_widget_area, {i, i}));
}
_renderer->draw_color(base_color)
->draw_rect(nullptr)
->fill_rect(rect_margin(_widget_area, {border*2, border*2}));
}
public:
unsigned int cycle_r {3500};
unsigned int cycle_g {3500};
unsigned int cycle_b {3500};
virtual Size size() const noexcept
{
return {128, 64};
}
};
#endif

View file

@ -36,6 +36,7 @@ void run_example(std::function<void (W*, core::Font*, int, int)> setup, int w=3,
size_init.w, size_init.h,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_UTILITY)};
auto* renderer {new core::Renderer(win, -1, SDL_RENDERER_ACCELERATED)};
renderer->blend_mode(SDL_BLENDMODE_BLEND);
auto* layout {new widget::HorizontalLayout()};

View file

@ -39,6 +39,9 @@ namespace bwidgets::widget
return this;
}
virtual inline const SDL_Rect& viewport() const noexcept final {
return _viewport;
}
virtual inline Widget* viewport(const SDL_Rect& vp) noexcept final {
_handle_geometry_change(vp);
_viewport = vp;

View file

@ -21,7 +21,7 @@ namespace bwidgets::widget
RIGHT
};
private:
protected:
SDL_Color _color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
SDL_Color _color_bg {255, 255, 255, SDL_ALPHA_TRANSPARENT};
core::Font::RenderMode _render_mode {core::Font::RenderMode::SHADED};

View file

@ -7,7 +7,7 @@ project('sdl2_basic_widgets', 'cpp',
'optimization=g'],
license: 'EUPL-1.2')
add_project_arguments('-Winline', '-pedantic', language: 'cpp')
add_project_arguments('-pedantic', language: 'cpp')
sdl = [
dependency('sdl2', version: '>=2.0.5'),
@ -52,6 +52,12 @@ executable('caption_demo',
link_with : libbasic_widgets,
install : false)
executable('example_demo',
'examples/example_example.cpp',
include_directories : pub_api,
link_with : libbasic_widgets,
install : false)
executable('input_demo',
'examples/input_example.cpp',
include_directories : pub_api,

View file

@ -111,9 +111,22 @@ core::Renderer* core::Renderer::draw_points(const std::vector<SDL_Point> pts)
core::Renderer* core::Renderer::draw_rect(const SDL_Rect* r)
{
SDLError::success_or_throw(SDL_RenderDrawRect(c_pod, r),
auto vp = viewport();
// Has glitch at top-left and bottom-right corner.
// The first corner has an extra pixel at y-1 and surimpression.
// The second corner is missing a pixel.
if (r)
viewport({vp.x + r->x, vp.y + r->y, r->w - 1, r->h - 1}); // crop extra pixel
SDLError::success_or_throw(SDL_RenderDrawRect(c_pod, nullptr),
__FILE__, __FUNCTION__, __LINE__);
if (r)
draw_point({r->w - 1, r->h - 1}); // add missing pixel
else
draw_point({vp.w - 1, vp.h - 1});
viewport(vp);
return this;
}

View file

@ -48,7 +48,7 @@ widget::Widget* widget::Widget::render()
if (!_renderer)
return this;
#ifndef _NDEBUG
#ifdef _NDEBUG
_renderer->draw_color({0, 255, 0, SDL_ALPHA_TRANSPARENT})
->draw_rect(nullptr);
#endif

View file

@ -118,7 +118,8 @@ void widget::Button::_handle_rendering()
color_shade(c.g, max, biggest),
color_shade(c.b, max, biggest),
c.a
})->draw_rect(core::rect_margin(_widget_area, margin));
})
->draw_rect(core::rect_margin(_widget_area, margin));
if (x < border_size.w)
x++;
if (y < border_size.h)

View file

@ -88,7 +88,11 @@ void widget::Caption::_handle_renderer_change(core::Renderer*)
void widget::Caption::_handle_rendering()
{
_renderer->draw_color(_color_bg)->fill_rect({0, 0, _viewport.w, _viewport.h});
if (!_text_texture)
_handle_texture_update();
if (_render_mode == core::Font::RenderMode::SHADED)
_renderer->draw_color(_color_bg)->fill_rect({0, 0, _viewport.w, _viewport.h});
if (!_text_texture)
_handle_texture_update();