From f42e079b708e2648a7b59736b3385c51dd4b3fd6 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 24 Oct 2009 20:18:00 +0100 Subject: [PATCH] Use scissor test to crop widgets --- include/gui2/RenderContext.hpp | 4 ++++ src/gui/Font.cpp | 15 ++++++++++++- src/gui2/Button.cpp | 5 +++-- src/gui2/ContainerWidget.cpp | 4 +++- src/gui2/RenderContext.cpp | 41 ++++++++++++++++++++++++++++++---- src/gui2/Theme.cpp | 4 ++-- 6 files changed, 63 insertions(+), 10 deletions(-) diff --git a/include/gui2/RenderContext.hpp b/include/gui2/RenderContext.hpp index 47415c6..7343cf2 100644 --- a/include/gui2/RenderContext.hpp +++ b/include/gui2/RenderContext.hpp @@ -29,6 +29,8 @@ namespace gui { + class Widget; + class RenderContext { public: RenderContext(); @@ -37,6 +39,8 @@ namespace gui { void push_origin(int x, int y); void pop_origin(); + void scissor(Widget* w); + void rectangle(int x, int y, int w, int h, Colour c); void border(int x, int y, int w, int h, Colour c); diff --git a/src/gui/Font.cpp b/src/gui/Font.cpp index 01a1c6e..35f9f9f 100644 --- a/src/gui/Font.cpp +++ b/src/gui/Font.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "ft2build.h" @@ -335,5 +336,17 @@ int Font::stringWidth(const char* fmt, ...) const IFontPtr gui::loadFont(const string& aFile, int aHeight, bool shadow) { - return IFontPtr(new Font(aFile, aHeight, shadow)); + typedef tuple FontToken; + static map cache; + + FontToken t = make_tuple(aFile, aHeight, shadow); + map::iterator it = cache.find(t); + + if (it != cache.end()) + return (*it).second; + else { + IFontPtr p(new Font(aFile, aHeight, shadow)); + cache[t] = p; + return p; + } } diff --git a/src/gui2/Button.cpp b/src/gui2/Button.cpp index 4a195db..fbf2f17 100644 --- a/src/gui2/Button.cpp +++ b/src/gui2/Button.cpp @@ -29,9 +29,10 @@ void Button::render(RenderContext& rc) const { rc.rectangle(x(), y(), width(), height(), rc.theme().background()); - rc.border(x(), y(), width(), height(), - rc.theme().border()); rc.print(rc.theme().normal_font(), x() + 5, y() + 5, "Yahyahyah"); + + rc.border(x(), y(), width(), height(), + rc.theme().border()); } diff --git a/src/gui2/ContainerWidget.cpp b/src/gui2/ContainerWidget.cpp index a32e34a..44a8484 100644 --- a/src/gui2/ContainerWidget.cpp +++ b/src/gui2/ContainerWidget.cpp @@ -28,8 +28,10 @@ ContainerWidget::ContainerWidget(const AttributeSet& attrs) void ContainerWidget::render(RenderContext& rc) const { for (ChildList::const_iterator it = const_begin(); - it != const_end(); ++it) + it != const_end(); ++it) { + rc.scissor(*it); (*it)->render(rc); + } } void ContainerWidget::add_child(Widget* w) diff --git a/src/gui2/RenderContext.cpp b/src/gui2/RenderContext.cpp index 29a8a8e..c839ac3 100644 --- a/src/gui2/RenderContext.cpp +++ b/src/gui2/RenderContext.cpp @@ -16,11 +16,16 @@ // #include "gui2/RenderContext.hpp" +#include "gui2/Widget.hpp" +#include "ILogger.hpp" +#include "IWindow.hpp" #include using namespace gui; +IWindowPtr getGameWindow(); + namespace { inline void set_colour(Colour c) { @@ -31,12 +36,13 @@ namespace { RenderContext::RenderContext() : origin_x(0), origin_y(0) { - + glPushAttrib(GL_ENABLE_BIT); + glEnable(GL_SCISSOR_TEST); } RenderContext::~RenderContext() { - + glPopAttrib(); } void RenderContext::push_origin(int x, int y) @@ -75,12 +81,26 @@ void RenderContext::border(int x, int y, int w, int h, Colour c) { offset(x, y); set_colour(c); - - glBegin(GL_LINE_LOOP); + + x += 1; + y += 1; + w -= 1; + h -= 1; + + glBegin(GL_LINES); + glVertex2i(x, y); glVertex2i(x + w, y); + + glVertex2i(x + w, y); + glVertex2i(x + w, y + h); + glVertex2i(x + w, y + h); glVertex2i(x, y + h); + + glVertex2i(x, y + h); + glVertex2i(x, y - 1); + glEnd(); } @@ -89,3 +109,16 @@ void RenderContext::print(IFontPtr font, int x, int y, const string& s) offset(x, y); font->print(x, y, "%s", s.c_str()); } + +void RenderContext::scissor(Widget* w) +{ + static int wh = getGameWindow()->height(); + + int x = w->x() - 1; + int y = w->y() - 1; + offset(x, y); + + y = wh - y - w->height() - 1; + + glScissor(x, y, w->width() + 1, w->height() + 1); +} diff --git a/src/gui2/Theme.cpp b/src/gui2/Theme.cpp index 1cee39a..9cf7cb0 100644 --- a/src/gui2/Theme.cpp +++ b/src/gui2/Theme.cpp @@ -21,7 +21,7 @@ using namespace gui; Theme::Theme() { - normal_font_ = loadFont("data/fonts/Vera.ttf", 14); + normal_font_ = loadFont("data/fonts/Vera.ttf", 11, false); } Colour Theme::background() const @@ -31,5 +31,5 @@ Colour Theme::background() const Colour Theme::border() const { - return make_colour(1.0f, 0.0f, 0.0f, 0.5f); + return make_colour(1.0f, 0.0f, 0.0f, 1.0f); } -- 2.39.2