From e500db97b1c120f4b0e26962e68337db3178ebf6 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 8 Apr 2020 18:04:21 +0200 Subject: [PATCH] - v_text updated and moved to 'common'. # Conflicts: # source/CMakeLists.txt --- source/CMakeLists.txt | 2 +- source/{core => common}/fonts/v_text.cpp | 71 ++++++++++++++++++++++++ source/common/fonts/v_text.h | 52 +++++++++++++++++ source/core/menu/menu.cpp | 1 + source/core/textures/buildtiles.cpp | 3 +- 5 files changed, 127 insertions(+), 2 deletions(-) rename source/{core => common}/fonts/v_text.cpp (76%) create mode 100644 source/common/fonts/v_text.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 438a70267..4c0f37268 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -733,7 +733,6 @@ set (PCH_SOURCES core/fonts/hexfont.cpp core/fonts/singlelumpfont.cpp core/fonts/v_font.cpp - core/fonts/v_text.cpp core/console/c_console.cpp core/console/d_event.cpp @@ -750,6 +749,7 @@ set (PCH_SOURCES common/thirdparty/sfmt/SFMT.cpp common/textures/texture.cpp common/textures/animtexture.cpp + common/fonts/v_text.cpp common/textures/bitmap.cpp common/textures/m_png.cpp common/textures/image.cpp diff --git a/source/core/fonts/v_text.cpp b/source/common/fonts/v_text.cpp similarity index 76% rename from source/core/fonts/v_text.cpp rename to source/common/fonts/v_text.cpp index e6e3a2ede..f4c7ddc0c 100644 --- a/source/core/fonts/v_text.cpp +++ b/source/common/fonts/v_text.cpp @@ -41,6 +41,11 @@ #include "v_font.h" #include "utf8.h" +#include "filesystem.h" + +#include "gstrings.h" +#include "vm.h" +#include "serializer.h" //========================================================================== // @@ -169,3 +174,69 @@ TArray V_BreakLines (FFont *font, int maxwidth, const uint8_t *str } return Lines; } + +FSerializer &Serialize(FSerializer &arc, const char *key, FBrokenLines& g, FBrokenLines *def) +{ + if (arc.BeginObject(key)) + { + arc("text", g.Text) + ("width", g.Width) + .EndObject(); + } + return arc; +} + + + +class DBrokenLines : public DObject +{ + DECLARE_CLASS(DBrokenLines, DObject) + +public: + TArray mBroken; + + DBrokenLines() = default; + + DBrokenLines(TArray &broken) + { + mBroken = std::move(broken); + } + + void Serialize(FSerializer &arc) override + { + arc("lines", mBroken); + } +}; + +IMPLEMENT_CLASS(DBrokenLines, false, false); + +DEFINE_ACTION_FUNCTION(DBrokenLines, Count) +{ + PARAM_SELF_PROLOGUE(DBrokenLines); + ACTION_RETURN_INT(self->mBroken.Size()); +} + +DEFINE_ACTION_FUNCTION(DBrokenLines, StringWidth) +{ + PARAM_SELF_PROLOGUE(DBrokenLines); + PARAM_INT(index); + ACTION_RETURN_INT((unsigned)index >= self->mBroken.Size()? -1 : self->mBroken[index].Width); +} + +DEFINE_ACTION_FUNCTION(DBrokenLines, StringAt) +{ + + PARAM_SELF_PROLOGUE(DBrokenLines); + PARAM_INT(index); + ACTION_RETURN_STRING((unsigned)index >= self->mBroken.Size() ? -1 : self->mBroken[index].Text); +} + +DEFINE_ACTION_FUNCTION(FFont, BreakLines) +{ + PARAM_SELF_STRUCT_PROLOGUE(FFont); + PARAM_STRING(text); + PARAM_INT(maxwidth); + + auto broken = V_BreakLines(self, maxwidth, text, true); + ACTION_RETURN_OBJECT(Create(broken)); +} diff --git a/source/common/fonts/v_text.h b/source/common/fonts/v_text.h new file mode 100644 index 000000000..7dbc13cdf --- /dev/null +++ b/source/common/fonts/v_text.h @@ -0,0 +1,52 @@ +/* +** v_text.h +** +**--------------------------------------------------------------------------- +** Copyright 1998-2006 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +#ifndef __V_TEXT_H__ +#define __V_TEXT_H__ + +#include "v_font.h" +#include "printf.h" + +struct FBrokenLines +{ + unsigned Width; + FString Text; +}; + +TArray V_BreakLines (FFont *font, int maxwidth, const uint8_t *str, bool preservecolor = false); +inline TArray V_BreakLines (FFont *font, int maxwidth, const char *str, bool preservecolor = false) + { return V_BreakLines (font, maxwidth, (const uint8_t *)str, preservecolor); } +inline TArray V_BreakLines (FFont *font, int maxwidth, const FString &str, bool preservecolor = false) + { return V_BreakLines (font, maxwidth, (const uint8_t *)str.GetChars(), preservecolor); } + +#endif //__V_TEXT_H__ diff --git a/source/core/menu/menu.cpp b/source/core/menu/menu.cpp index ce9ad7f62..8a955bf01 100644 --- a/source/core/menu/menu.cpp +++ b/source/core/menu/menu.cpp @@ -322,6 +322,7 @@ void DMenu::Drawer () if (this == DMenu::CurrentMenu && BackbuttonAlpha > 0 && m_show_backbutton >= 0 && m_use_mouse) { FTexture* tex = TileFiles.GetTexture("engine/graphics/m_back.png"); + if (tex) { int w = tex->GetDisplayWidth() * CleanXfac; int h = tex->GetDisplayHeight() * CleanYfac; diff --git a/source/core/textures/buildtiles.cpp b/source/core/textures/buildtiles.cpp index 4af1aad32..f70bffca0 100644 --- a/source/core/textures/buildtiles.cpp +++ b/source/core/textures/buildtiles.cpp @@ -774,7 +774,8 @@ FTexture* BuildTiles::GetTexture(const char* path) // let this go away. auto res = textures.CheckKey(path); if (res) return *res; - auto tex = FTexture::CreateTexture(path, -1, ETextureType::Override); + auto lump = fileSystem.FindFile(path); + auto tex = FTexture::CreateTexture(path, lump, ETextureType::Override); if (tex) textures.Insert(path, tex); return tex; }