X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Frender.cpp;h=747bfa2dbee75a01d88e14bf55255f15007289c5;hb=3f35e70a6b66daf2ffd59590e98e2dd11e6eaabb;hp=3d0bbd47c6cf2e00feb6efb10798c22469a694a8;hpb=282d731ea8f10342efa82012028de7043b3dd639;p=blank.git diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index 3d0bbd4..747bfa2 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -1,6 +1,11 @@ +#include "BlendedSprite.hpp" +#include "FixedText.hpp" #include "Font.hpp" #include "Format.hpp" +#include "MessageBox.hpp" +#include "Text.hpp" #include "Texture.hpp" +#include "Viewport.hpp" #include #include @@ -34,6 +39,31 @@ Font &Font::operator =(Font &&other) noexcept { } +int Font::Style() const noexcept { + return TTF_GetFontStyle(handle); +} + +void Font::Style(int s) const noexcept { + TTF_SetFontStyle(handle, s); +} + +int Font::Outline() const noexcept { + return TTF_GetFontOutline(handle); +} + +void Font::Outline(int px) noexcept { + TTF_SetFontOutline(handle, px); +} + + +int Font::Hinting() const noexcept { + return TTF_GetFontHinting(handle); +} + +void Font::Hinting(int h) const noexcept { + TTF_SetFontHinting(handle, h); +} + bool Font::Kerning() const noexcept { return TTF_GetFontKerning(handle); } @@ -60,6 +90,15 @@ int Font::LineSkip() const noexcept { } +const char *Font::FamilyName() const noexcept { + return TTF_FontFaceFamilyName(handle); +} + +const char *Font::StyleName() const noexcept { + return TTF_FontFaceStyleName(handle); +} + + bool Font::HasGlyph(Uint16 c) const noexcept { return TTF_GlyphIsProvided(handle, c); } @@ -73,17 +112,21 @@ glm::tvec2 Font::TextSize(const char *text) const { return size; } -Texture Font::Render(const char *text, SDL_Color color) const { - SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, color); +Texture Font::Render(const char *text) const { + Texture tex; + Render(text, tex); + return tex; +} + +void Font::Render(const char *text, Texture &tex) const { + SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF }); if (!srf) { throw std::runtime_error(TTF_GetError()); } - Texture tex; tex.Bind(); tex.Data(*srf, false); tex.FilterLinear(); SDL_FreeSurface(srf); - return tex; } @@ -117,6 +160,104 @@ void Format::ReadPixelFormat(const SDL_PixelFormat &fmt) { } +MessageBox::MessageBox(const Font &f) +: font(f) +, lines() +, max_lines(10) +, pos(0.0f) +, adv(0.0f, font.LineSkip(), 0.0f) +, bg(1.0f, 1.0f, 1.0f, 0.0f) +, fg(1.0f, 1.0f, 1.0f, 1.0f) +, grav(Gravity::NORTH_WEST) { + +} + +void MessageBox::Position(const glm::vec3 &p, Gravity g) noexcept { + pos = p; + grav = g; + if (get_y(g) == Align::END) { + adv.y = -font.LineSkip(); + } else { + adv.y = font.LineSkip(); + } + for (Text &txt : lines) { + txt.Pivot(g); + } +} + +void MessageBox::PushLine(const char *text) { + lines.emplace_front(); + Text &txt = lines.front(); + txt.Set(font, text); + txt.Pivot(grav); + + while (lines.size() > max_lines) { + lines.pop_back(); + } +} + +void MessageBox::Render(Viewport &viewport) noexcept { + BlendedSprite &prog = viewport.SpriteProgram(); + prog.SetBG(bg); + prog.SetFG(fg); + viewport.SetCursor(pos, grav); + for (Text &txt : lines) { + prog.SetM(viewport.Cursor()); + txt.Render(viewport); + viewport.MoveCursor(adv); + } +} + + +Text::Text() noexcept +: tex() +, sprite() +, size(0.0f) +, pivot(Gravity::NORTH_WEST) +, dirty(false) { + +} + +FixedText::FixedText() noexcept +: Text() +, bg(1.0f, 1.0f, 1.0f, 0.0f) +, fg(1.0f, 1.0f, 1.0f, 1.0f) +, pos(0.0f) +, grav(Gravity::NORTH_WEST) +, visible(false) { + +} + +void Text::Set(const Font &font, const char *text) { + font.Render(text, tex); + size = font.TextSize(text); + dirty = true; +} + +void Text::Update() { + sprite.LoadRect(size.x, size.y, align(pivot, size)); + dirty = false; +} + +void FixedText::Render(Viewport &viewport) noexcept { + BlendedSprite &prog = viewport.SpriteProgram(); + viewport.SetCursor(pos, grav); + prog.SetM(viewport.Cursor()); + prog.SetBG(bg); + prog.SetFG(fg); + Text::Render(viewport); +} + +void Text::Render(Viewport &viewport) noexcept { + if (dirty) { + Update(); + } + BlendedSprite &prog = viewport.SpriteProgram(); + prog.SetTexture(tex); + sprite.Draw(); +} + + Texture::Texture() : handle(0) , width(0)