From 0f823b007a4e810330f73eece9edfb4b6ffaf6a1 Mon Sep 17 00:00:00 2001 From: wutno Date: Sat, 20 Jan 2024 15:58:40 -0500 Subject: [PATCH] Spawn threads for long printer actions to maintain connection, custom glyph scaling --- Includes/Printer.cpp | 59 ++++++++++++++++++++------------------------ Includes/Printer.h | 24 ++++++++++++------ 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/Includes/Printer.cpp b/Includes/Printer.cpp index 9cc788a..70fd787 100644 --- a/Includes/Printer.cpp +++ b/Includes/Printer.cpp @@ -32,15 +32,8 @@ bool Printer::RegisterFont(std::vector& data) data.erase(data.begin()); std::vector bits = ConvertToBits(data); - constexpr uint8_t maxDimentions = 24; - // FIXME: Doesn't need to be a 32-bit surface - SDL_Surface* glyph = SDL_CreateRGBSurface( - 0, - maxDimentions, - maxDimentions, - 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 - ); - + constexpr uint8_t dimensions = 24; + SDL_Surface* glyph = QuickCreateSurface(dimensions, dimensions); if (glyph == nullptr) return false; @@ -59,13 +52,7 @@ bool Printer::RegisterFont(std::vector& data) SDL_UnlockSurface(glyph); // The default 25x25 glyph is just slighly too small to match up with our font, so resize it - constexpr uint8_t newDimentions = 30; - SDL_Surface* scaledGlyph = SDL_CreateRGBSurface( - 0, - newDimentions, - newDimentions, - 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 - ); + SDL_Surface* scaledGlyph = QuickCreateSurface(maxCustomGylphDimensions, maxCustomGylphDimensions); SDL_BlitScaled(glyph, NULL, scaledGlyph, NULL); SDL_FreeSurface(glyph); @@ -103,7 +90,7 @@ bool Printer::QueuePrintLine(std::vector& data) m_printQueue.push_back({ offset, temp }); if (static_cast(data[0]) == Mode::Now) - PrintLine(); + std::thread(&Printer::PrintLine, this).detach(); return true; } @@ -122,7 +109,7 @@ void Printer::PrintLine() } constexpr uint8_t defaultX = 95; // This is good for *most* cards - const uint8_t defaultY = m_horizontalCard ? 85 : 120; + const uint8_t defaultY = m_isHorizontalCard ? 85 : 120; constexpr SDL_Color color = { 0x64, 0x64, 0x96, 0xFF }; constexpr uint8_t verticalCardOffset = 4; @@ -153,15 +140,15 @@ void Printer::PrintLine() // We don't run this for a single line skip as the FontLineSkip isn't the same as our defaultY if (print.offset > 1) { - yPos += TTF_FontLineSkip(font) * (print.offset - 1); - if (!m_horizontalCard) - yPos += (print.offset - 1) * verticalCardOffset; + yPos += TTF_FontLineSkip(font) * (print.offset); + if (!m_isHorizontalCard) + yPos += (print.offset) * (verticalCardOffset - 2); // Don't ask } // TODO: Have converted be a custom type where we can just iterate with converted[n] for (auto i = utf8codepoint(converted, ¤tChar); currentChar != '\0'; i = utf8codepoint(i, ¤tChar)) { // We need to fix our yPos after resetting from a yScale on the same line - if (yScaleCompensate) { + if (yScaleCompensate && maxYSizeForLine) { yPos += maxYSizeForLine - TTF_FontLineSkip(font); yScaleCompensate = false; } @@ -170,7 +157,7 @@ void Printer::PrintLine() switch (static_cast(currentChar)) { case Return: TTF_SetFontSize(font, defaultFontSize * std::atoi(yScale.c_str())); - yPos += TTF_FontLineSkip(font) + (m_horizontalCard ? 0 : verticalCardOffset); + yPos += TTF_FontLineSkip(font) + (m_isHorizontalCard ? 0 : verticalCardOffset); TTF_SetFontSize(font, defaultFontSize); xPos = defaultX; yScale = '1'; @@ -206,20 +193,28 @@ void Printer::PrintLine() continue; } SDL_Rect location = { xPos, yPos, 0, 0 }; - SDL_BlitSurface(m_customGlyphs.at(currentChar), NULL, m_cardImage, &location); - xPos += m_customGlyphs.at(currentChar)->w; + + if (xScale[0] != '1' || yScale[0] != '1') { + auto scaledCustomGlyph = QuickCreateSurface(maxCustomGylphDimensions * std::atoi(xScale.c_str()), maxCustomGylphDimensions * std::atoi(yScale.c_str())); + SDL_BlitScaled(m_customGlyphs.at(currentChar), NULL, scaledCustomGlyph, NULL); + if (yScale[0] != '1') + location.y -= (scaledCustomGlyph->h / std::atoi(yScale.c_str())); + SDL_BlitSurface(scaledCustomGlyph, NULL, m_cardImage, &location); + xPos += scaledCustomGlyph->w; + SDL_FreeSurface(scaledCustomGlyph); + scaledCustomGlyph = nullptr; + } + else { + SDL_BlitSurface(m_customGlyphs.at(currentChar), NULL, m_cardImage, &location); + xPos += m_customGlyphs.at(currentChar)->w; + } } continue; } // TODO: Solid produces a better glyph but with non-transparent backgrounds SDL_Surface* glyph = TTF_RenderGlyph32_Blended(font, currentChar, color); - SDL_Surface* scaledGlyph = SDL_CreateRGBSurface( - 0, - glyph->clip_rect.w * std::atoi(xScale.c_str()), - glyph->clip_rect.h * std::atoi(yScale.c_str()), - 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 - ); + SDL_Surface* scaledGlyph = QuickCreateSurface(glyph->clip_rect.w * std::atoi(xScale.c_str()), glyph->clip_rect.h * std::atoi(yScale.c_str())); SDL_BlitScaled(glyph, NULL, scaledGlyph, NULL); // Final blit @@ -237,7 +232,7 @@ void Printer::PrintLine() xPos += 15 * std::atoi(xScale.c_str()); else #endif - xPos += advance * std::atoi(xScale.c_str()); + xPos += advance * std::atoi(xScale.c_str()); SDL_FreeSurface(glyph); SDL_FreeSurface(scaledGlyph); diff --git a/Includes/Printer.h b/Includes/Printer.h index 62b5955..c0a1098 100644 --- a/Includes/Printer.h +++ b/Includes/Printer.h @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -60,6 +61,11 @@ class Printer void RemoveCard(std::string& cardName) { m_localName = cardName; + std::thread(&Printer::DetachedRemove, this).detach(); + } + + void DetachedRemove() + { PrintLine(); if (m_cardImage != nullptr) { SaveCardImage(m_localName); @@ -82,8 +88,11 @@ class Printer std::string m_localName = {}; // Default state is a vertical card on the mechs - bool m_horizontalCard = false; + bool m_isHorizontalCard = false; + protected: + static constexpr uint8_t maxCustomGylphDimensions = 30; + struct PrintCommand { uint8_t offset = 0; std::vector data = {}; @@ -142,12 +151,7 @@ class Printer } // Everything else failed, we *need* a surface... So let's generate a transparent one - m_cardImage = SDL_CreateRGBSurface( - 0, - (m_horizontalCard ? 1019 : 640), - (m_horizontalCard ? 640 : 1019), - 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 - ); + m_cardImage = QuickCreateSurface((m_isHorizontalCard ? 1019 : 640), (m_isHorizontalCard ? 640 : 1019)); } void SaveCardImage(std::string& cardName) @@ -169,6 +173,12 @@ class Printer } return temp; } + + // FIXME: Doesn't need to be a 32-bit surface + SDL_Surface* QuickCreateSurface(int x, int y) + { + return SDL_CreateRGBSurface(0, x, y, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); + } }; #endif