From 2ff8549c3482e4f331835bc49a66e0396ba4367d Mon Sep 17 00:00:00 2001 From: turanszkij Date: Tue, 13 Nov 2018 19:31:01 +0000 Subject: [PATCH] font and rectpacker updates --- Editor/Editor.cpp | 4 +- Tests/Tests.cpp | 12 +- WickedEngine/MainComponent.h | 2 +- WickedEngine/wiBackLog.cpp | 2 +- WickedEngine/wiFont.cpp | 199 +++++++++++++---------------- WickedEngine/wiFont.h | 29 ++--- WickedEngine/wiFont_BindLua.cpp | 7 +- WickedEngine/wiHelper.cpp | 9 +- WickedEngine/wiHelper.h | 2 +- WickedEngine/wiProfiler.cpp | 2 +- WickedEngine/wiRectPacker.cpp | 35 +++-- WickedEngine/wiRectPacker.h | 16 +-- WickedEngine/wiRenderer.cpp | 16 +-- WickedEngine/wiResourceManager.cpp | 30 ++--- WickedEngine/wiVersion.cpp | 2 +- WickedEngine/wiWidget.cpp | 24 ++-- 16 files changed, 177 insertions(+), 214 deletions(-) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index fe87c94a3..a7c410d72 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -1,4 +1,4 @@ -#include "stdafx.h" +#include "stdafx.h" #include "Editor.h" #include "wiRenderer.h" #include "MaterialWindow.h" @@ -70,7 +70,7 @@ void Editor::Initialize() void EditorLoadingScreen::Load() { font = wiFont("Loading...", wiFontProps((int)(wiRenderer::GetDevice()->GetScreenWidth()*0.5f), (int)(wiRenderer::GetDevice()->GetScreenHeight()*0.5f), 36, - WIFALIGN_MID, WIFALIGN_MID)); + WIFALIGN_CENTER, WIFALIGN_CENTER)); addFont(&font); sprite = wiSprite("../logo/logo_small.png"); diff --git a/Tests/Tests.cpp b/Tests/Tests.cpp index a5b7c4f77..a731dbf60 100644 --- a/Tests/Tests.cpp +++ b/Tests/Tests.cpp @@ -245,13 +245,15 @@ void TestsRenderer::RunFontTest() font.props.posX = wiRenderer::GetDevice()->GetScreenWidth() / 2; font.props.posY = wiRenderer::GetDevice()->GetScreenHeight() / 6; + font.props.size = 32; font_upscaled.props = font.props; font_upscaled.props.posY += font.textHeight() + 10; - font.style = wiFont::AddFontStyle(wiFont::GetFontPath() + "arial.ttf", 32); - font_upscaled.style = wiFont::AddFontStyle(wiFont::GetFontPath() + "arial.ttf", 14); - font_upscaled.props.size = 32; // upscale + font.style = wiFont::AddFontStyle(wiFont::GetFontPath() + "arial.ttf"); + font_upscaled.style = wiFont::AddFontStyle(wiFont::GetFontPath() + "arial.ttf"); + font_upscaled.props.size = 14; + font_upscaled.props.scaling = 32.0f / 14.0f; addFont(&font); addFont(&font_upscaled); @@ -287,10 +289,10 @@ void TestsRenderer::RunFontTest() static wiFont font_japanese; font_japanese = font_aligned2; font_japanese.props.posY += font_aligned2.textHeight(); - font_japanese.style = wiFont::AddFontStyle("yumin.ttf", 34); + font_japanese.style = wiFont::AddFontStyle("yumin.ttf"); font_japanese.props.shadowColor = wiColor::Transparent; font_japanese.props.h_align = WIFALIGN_CENTER; - font_japanese.props.size = -1; // no scaling, it will use 34 (that was specified with AddFontStyle second argument) + font_japanese.props.size = 34; font_japanese.SetText(ss.str()); addFont(&font_japanese); } diff --git a/WickedEngine/MainComponent.h b/WickedEngine/MainComponent.h index 73a9fb3b4..bcb338b8a 100644 --- a/WickedEngine/MainComponent.h +++ b/WickedEngine/MainComponent.h @@ -80,7 +80,7 @@ public: // display resolution info bool resolution = false; // text size - int size = -1; + int size = 16; }; // display all-time engine information text InfoDisplayer infoDisplay; diff --git a/WickedEngine/wiBackLog.cpp b/WickedEngine/wiBackLog.cpp index f1663052a..62885ce5a 100644 --- a/WickedEngine/wiBackLog.cpp +++ b/WickedEngine/wiBackLog.cpp @@ -99,7 +99,7 @@ namespace wiBackLog font.props.posX = 50; font.props.posY = (int)pos + (int)scroll; font.Draw(GRAPHICSTHREAD_IMMEDIATE); - wiFont(inputArea.str().c_str(), wiFontProps(10, wiRenderer::GetDevice()->GetScreenHeight() - 10, -1, WIFALIGN_LEFT, WIFALIGN_BOTTOM)).Draw(GRAPHICSTHREAD_IMMEDIATE); + wiFont(inputArea.str().c_str(), wiFontProps(10, wiRenderer::GetDevice()->GetScreenHeight() - 10, WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_BOTTOM)).Draw(GRAPHICSTHREAD_IMMEDIATE); } } diff --git a/WickedEngine/wiFont.cpp b/WickedEngine/wiFont.cpp index 1f2e8d5bc..0c794b062 100644 --- a/WickedEngine/wiFont.cpp +++ b/WickedEngine/wiFont.cpp @@ -46,40 +46,33 @@ namespace wiFont_Internal Texture2D* texture = nullptr; // These won't be thread safe by the way! (todo) - unordered_map rects; - constexpr int64_t glyphhash(int code, int style) { return (int64_t(code) << 32) | int64_t(style); } + struct Glyph + { + int16_t x; + int16_t y; + int16_t width; + int16_t height; + uint16_t tc_left; + uint16_t tc_right; + uint16_t tc_top; + uint16_t tc_bottom; + }; + unordered_map glyph_lookup; + unordered_map rect_lookup; + constexpr int64_t glyphhash(int code, int style, int height) { return (int64_t(code) << 32) | (int64_t(style) << 16) | int64_t(height); } constexpr int codefromhash(int64_t hash) { return int((hash >> 32) & 0xFFFFFFFF); } - constexpr int stylefromhash(int64_t hash) { return int(hash & 0xFFFFFFFF); } + constexpr int stylefromhash(int64_t hash) { return int((hash >> 16) & 0x0000FFFF); } + constexpr int heightfromhash(int64_t hash) { return int((hash >> 0) & 0x0000FFFF); } unordered_set pendingGlyphs; struct wiFontStyle { string name; - size_t fontBufferSize = 0; vector fontBuffer; stbtt_fontinfo fontInfo; - float fontScaling = 1; - int lineHeight = 16; - float ascent = 0; - float descent = 0; - float lineGap = 0; - - struct Glyph + wiFontStyle(const string& newName) : name(newName) { - int16_t x; - int16_t y; - int16_t width; - int16_t height; - uint16_t tc_left; - uint16_t tc_right; - uint16_t tc_top; - uint16_t tc_bottom; - }; - unordered_map lookup; - - wiFontStyle(const string& newName, int height = 16) : name(newName), lineHeight(height) - { - wiHelper::readByteData(newName, fontBuffer, fontBufferSize); + wiHelper::readByteData(newName, fontBuffer); int offset = stbtt_GetFontOffsetForIndex(fontBuffer.data(), 0); @@ -88,18 +81,7 @@ namespace wiFont_Internal stringstream ss(""); ss << "Failed to load font: " << name; wiHelper::messageBox(ss.str()); - return; } - - fontScaling = stbtt_ScaleForPixelHeight(&fontInfo, (float)lineHeight); - - int _a, _d, _l; - stbtt_GetFontVMetrics(&fontInfo, &_a, &_d, &_l); - - ascent = float(_a) * fontScaling; - descent = float(_d) * fontScaling; - lineGap = float(_l) * fontScaling; - } }; std::vector fontStyles; @@ -114,44 +96,43 @@ namespace wiFont_Internal { int quadCount = 0; - const wiFontStyle& fontStyle = *fontStyles[style]; - - const int16_t lineHeight = (props.size < 0 ? uint16_t(fontStyle.lineHeight) : uint16_t(props.size)); - const float relativeSize = (props.size < 0 ? 1 : (float)props.size / (float)fontStyle.lineHeight); + const int16_t lineHeight = props.size; + const float scaling = props.scaling; int16_t line = 0; int16_t pos = 0; for (size_t i = 0; i < text.length(); ++i) { const int code = text[i]; + const int64_t hash = glyphhash(code, style, props.size); - if (fontStyle.lookup.count(code) == 0) + if (glyph_lookup.count(hash) == 0) { // glyph not packed yet, so add to pending list: - pendingGlyphs.insert(glyphhash(code, style)); + pendingGlyphs.insert(hash); continue; } if (code == '\n') { - line += lineHeight + int(props.spacingY * relativeSize); + line += lineHeight + int(props.spacingY * scaling); pos = 0; } else if (code == ' ') { - pos += int((WHITESPACE_SIZE + props.spacingX) * relativeSize); + pos += int((WHITESPACE_SIZE + props.spacingX) * scaling); } else if (code == '\t') { - pos += int(((WHITESPACE_SIZE + props.spacingX) * TAB_WHITESPACECOUNT) * relativeSize); + pos += int(((WHITESPACE_SIZE + props.spacingX) * TAB_WHITESPACECOUNT) * scaling); } else { - const wiFontStyle::Glyph& glyph = fontStyle.lookup.at(code); - const int16_t glyphWidth = int16_t(glyph.width * relativeSize); - const int16_t glyphHeight = int16_t(glyph.height * relativeSize); - const int16_t glyphOffsetX = int16_t(glyph.x * relativeSize); - const int16_t glyphOffsetY = int16_t(glyph.y * relativeSize); + const Glyph& glyph = glyph_lookup.at(hash); + const int16_t glyphWidth = int16_t(glyph.width * scaling); + const int16_t glyphHeight = int16_t(glyph.height * scaling); + const int16_t glyphOffsetX = int16_t(glyph.x * scaling); + const int16_t glyphOffsetY = int16_t(glyph.y * scaling); const size_t vertexID = quadCount * 4; @@ -178,7 +159,7 @@ namespace wiFont_Internal vertexList[vertexID + 3].Tex.x = glyph.tc_right; vertexList[vertexID + 3].Tex.y = glyph.tc_bottom; - pos += glyphWidth + props.spacingX; + pos += glyphWidth + int16_t(props.spacingX * scaling); //// add kerning //if (i > 0 && i < text.length() - 1) @@ -226,7 +207,7 @@ void wiFont::Initialize() // add default font if there is none yet: if (fontStyles.empty()) { - AddFontStyle(FONTPATH + "arial.ttf", 16); + AddFontStyle(FONTPATH + "arial.ttf"); } GraphicsDevice* device = wiRenderer::GetDevice(); @@ -391,27 +372,37 @@ void wiFont::BindPersistentState(GRAPHICSTHREAD threadID) { const int code = codefromhash(hash); const int style = stylefromhash(hash); + const int height = heightfromhash(hash); wiFontStyle& fontStyle = *fontStyles[style]; + float fontScaling = stbtt_ScaleForPixelHeight(&fontStyle.fontInfo, float(height)); + + int ascent, descent, lineGap; + stbtt_GetFontVMetrics(&fontStyle.fontInfo, &ascent, &descent, &lineGap); + + ascent = int(float(ascent) * fontScaling); + descent = int(float(descent) * fontScaling); + lineGap = int(float(lineGap) * fontScaling); + // get bounding box for character (may be offset to account for chars that dip above or below the line int left, top, right, bottom; - stbtt_GetCodepointBitmapBox(&fontStyle.fontInfo, code, fontStyle.fontScaling, fontStyle.fontScaling, &left, &top, &right, &bottom); + stbtt_GetCodepointBitmapBox(&fontStyle.fontInfo, code, fontScaling, fontScaling, &left, &top, &right, &bottom); - wiFontStyle::Glyph& glyph = fontStyle.lookup[code]; + Glyph& glyph = glyph_lookup[hash]; glyph.x = left; - glyph.y = top + int(fontStyle.ascent); + glyph.y = top + ascent; glyph.width = right - left; glyph.height = bottom - top; - rects[hash] = rect_ltrb(left, top, right, bottom); + rect_lookup[hash] = rect_ltrb(left, top, right, bottom); } pendingGlyphs.clear(); - vector out_rects(rects.size()); + vector out_rects(rect_lookup.size()); { int i = 0; - for (auto& it : rects) + for (auto& it : rect_lookup) { out_rects[i] = &it.second; i++; @@ -419,7 +410,7 @@ void wiFont::BindPersistentState(GRAPHICSTHREAD threadID) } std::vector bins; - if (pack(out_rects.data(), (int)rects.size(), 1024, bins)) + if (pack(out_rects.data(), (int)out_rects.size(), 1024, bins)) { assert(bins.size() == 1 && "The regions won't fit into the texture!"); @@ -431,17 +422,20 @@ void wiFont::BindPersistentState(GRAPHICSTHREAD threadID) vector bitmap(bitmapWidth * bitmapHeight); std::fill(bitmap.begin(), bitmap.end(), 0); - for (auto it : rects) + for (auto it : rect_lookup) { const int64_t hash = it.first; const wchar_t code = codefromhash(hash); const int style = stylefromhash(hash); + const int height = heightfromhash(hash); wiFontStyle& fontStyle = *fontStyles[style]; - rect_xywhf& rect = it.second; + const rect_xywh& rect = it.second; + + float fontScaling = stbtt_ScaleForPixelHeight(&fontStyle.fontInfo, float(height)); // render character (stride and offset is important here) int byteOffset = rect.x + (rect.y * bitmapWidth); - stbtt_MakeCodepointBitmap(&fontStyle.fontInfo, bitmap.data() + byteOffset, rect.w, rect.h, bitmapWidth, fontStyle.fontScaling, fontStyle.fontScaling, code); + stbtt_MakeCodepointBitmap(&fontStyle.fontInfo, bitmap.data() + byteOffset, rect.w, rect.h, bitmapWidth, fontScaling, fontScaling, code); float tc_left = float(rect.x); float tc_right = tc_left + float(rect.w); @@ -453,7 +447,7 @@ void wiFont::BindPersistentState(GRAPHICSTHREAD threadID) tc_top *= inv_height; tc_bottom *= inv_height; - wiFontStyle::Glyph& glyph = fontStyle.lookup[code]; + Glyph& glyph = glyph_lookup[hash]; glyph.tc_left = XMConvertFloatToHalf(tc_left); glyph.tc_right = XMConvertFloatToHalf(tc_right); glyph.tc_top = XMConvertFloatToHalf(tc_top); @@ -473,6 +467,23 @@ Texture2D* wiFont::GetAtlas() { return texture; } +std::string& wiFont::GetFontPath() +{ + return FONTPATH; +} +int wiFont::AddFontStyle(const string& fontName) +{ + for (size_t i = 0; i < fontStyles.size(); i++) + { + const wiFontStyle& fontStyle = *fontStyles[i]; + if (!fontStyle.name.compare(fontName)) + { + return int(i); + } + } + fontStyles.push_back(new wiFontStyle(fontName)); + return int(fontStyles.size() - 1); +} void wiFont::Draw(GRAPHICSTHREAD threadID) @@ -484,11 +495,11 @@ void wiFont::Draw(GRAPHICSTHREAD threadID) wiFontProps newProps = props; - if (props.h_align == WIFALIGN_CENTER || props.h_align == WIFALIGN_MID) + if (props.h_align == WIFALIGN_CENTER) newProps.posX -= textWidth() / 2; else if (props.h_align == WIFALIGN_RIGHT) newProps.posX -= textWidth(); - if (props.v_align == WIFALIGN_CENTER || props.h_align == WIFALIGN_MID) + if (props.v_align == WIFALIGN_CENTER) newProps.posY -= textHeight() / 2; else if (props.v_align == WIFALIGN_BOTTOM) newProps.posY -= textHeight(); @@ -554,25 +565,26 @@ void wiFont::Draw(GRAPHICSTHREAD threadID) } - int wiFont::textWidth() { if (style >= fontStyles.size()) { return 0; } - const wiFontStyle& fontStyle = *fontStyles[style]; - const float relativeSize = (props.size < 0 ? 1.0f : (float)props.size / (float)fontStyle.lineHeight); + const int16_t lineHeight = props.size; + const float scaling = props.scaling; + int maxWidth = 0; int currentLineWidth = 0; for (size_t i = 0; i < text.length(); ++i) { const int code = text[i]; + const int64_t hash = glyphhash(code, style, lineHeight); - if (fontStyle.lookup.count(code) == 0) + if (glyph_lookup.count(hash) == 0) { // glyph not packed yet, so add to pending list: - pendingGlyphs.insert(glyphhash(code, style)); + pendingGlyphs.insert(hash); continue; } @@ -582,16 +594,16 @@ int wiFont::textWidth() } else if (code == ' ') { - currentLineWidth += int((WHITESPACE_SIZE + props.spacingX) * relativeSize); + currentLineWidth += int((WHITESPACE_SIZE + props.spacingX) * scaling); } else if (code == '\t') { - currentLineWidth += int(((WHITESPACE_SIZE + props.spacingX) * TAB_WHITESPACECOUNT) * relativeSize); + currentLineWidth += int(((WHITESPACE_SIZE + props.spacingX) * TAB_WHITESPACECOUNT) * scaling); } else { - int characterWidth = (int)(fontStyle.lookup.at(code).width * relativeSize); - currentLineWidth += characterWidth + int(props.spacingX * relativeSize); + int characterWidth = (int)(glyph_lookup.at(hash).width * scaling); + currentLineWidth += characterWidth + int(props.spacingX * scaling); } maxWidth = max(maxWidth, currentLineWidth); } @@ -604,8 +616,9 @@ int wiFont::textHeight() { return 0; } - const wiFontStyle& fontStyle = *fontStyles[style]; - const float relativeSize = (props.size < 0 ? 1.0f : (float)props.size / (float)fontStyle.lineHeight); + const int16_t lineHeight = props.size; + const float scaling = props.scaling; + int i = 0; int lines = 1; int len = (int)text.length(); @@ -618,8 +631,7 @@ int wiFont::textHeight() i++; } - const int lineHeight = (props.size < 0 ? fontStyle.lineHeight : props.size); - return lines * (lineHeight + int(props.spacingY * relativeSize)); + return lines * (lineHeight + int(props.spacingY * scaling)); } @@ -647,34 +659,3 @@ string wiFont::GetTextA() { return string(text.begin(),text.end()); } - -int wiFont::AddFontStyle(const string& fontName, int height) -{ - for (size_t i = 0; i < fontStyles.size(); i++) - { - const wiFontStyle& fontStyle = *fontStyles[i]; - if (!fontStyle.name.compare(fontName) && fontStyle.lineHeight == height) - { - return int(i); - } - } - fontStyles.push_back(new wiFontStyle(fontName, height)); - return int(fontStyles.size() - 1); -} -int wiFont::GetFontStyle(const string& fontName, int height) -{ - for (size_t i = 0; i < fontStyles.size(); i++) - { - const wiFontStyle& fontStyle = *fontStyles[i]; - if (!fontStyle.name.compare(fontName) && fontStyle.lineHeight == height) - { - return int(i); - } - } - return -1; // fontstyle not found -} - -std::string& wiFont::GetFontPath() -{ - return FONTPATH; -} diff --git a/WickedEngine/wiFont.h b/WickedEngine/wiFont.h index c0e3c9949..0430fc9d0 100644 --- a/WickedEngine/wiFont.h +++ b/WickedEngine/wiFont.h @@ -8,26 +8,25 @@ enum wiFontAlign { WIFALIGN_LEFT, - // same as mid WIFALIGN_CENTER, - // same as center - WIFALIGN_MID, WIFALIGN_RIGHT, WIFALIGN_TOP, - WIFALIGN_BOTTOM, - WIFALIGN_COUNT, + WIFALIGN_BOTTOM }; +static const int WIFONTSIZE_DEFAULT = 16; + struct wiFontProps { - int size; - int spacingX, spacingY; int posX, posY; + int size = WIFONTSIZE_DEFAULT; // line height in pixels + float scaling = 1; + int spacingX, spacingY; wiFontAlign h_align, v_align; wiColor color; wiColor shadowColor; - wiFontProps(int posX = 0, int posY = 0, int size = -1, wiFontAlign h_align = WIFALIGN_LEFT, wiFontAlign v_align = WIFALIGN_TOP + wiFontProps(int posX = 0, int posY = 0, int size = 16, wiFontAlign h_align = WIFALIGN_LEFT, wiFontAlign v_align = WIFALIGN_TOP , int spacingX = 0, int spacingY = 0, const wiColor& color = wiColor(255, 255, 255, 255), const wiColor& shadowColor = wiColor(0,0,0,0)) :posX(posX), posY(posY), size(size), h_align(h_align), v_align(v_align), spacingX(spacingX), spacingY(spacingY), color(color), shadowColor(shadowColor) {} @@ -43,6 +42,12 @@ public: static void BindPersistentState(GRAPHICSTHREAD threadID); static wiGraphicsTypes::Texture2D* GetAtlas(); + // Returns the font path that can be modified + static std::string& GetFontPath(); + + // Create a font. Returns fontStyleID that is reusable. If font already exists, just return its ID + static int AddFontStyle(const std::string& fontName); + std::wstring text; wiFontProps props; int style; @@ -50,23 +55,15 @@ public: wiFont(const std::string& text = "", wiFontProps props = wiFontProps(), int style = 0); wiFont(const std::wstring& text, wiFontProps props = wiFontProps(), int style = 0); ~wiFont(); - void Draw(GRAPHICSTHREAD threadID); - int textWidth(); int textHeight(); - // Create a font. Returns fontStyleID that is reusable. If font already exists, just return its ID - static int AddFontStyle(const std::string& fontName, int height = 16); - // Returns the style ID that is reusable. If font not found, returns -1 - static int GetFontStyle(const std::string& fontName, int height = 16); - void SetText(const std::string& text); void SetText(const std::wstring& text); std::wstring GetText(); std::string GetTextA(); - static std::string& GetFontPath(); }; diff --git a/WickedEngine/wiFont_BindLua.cpp b/WickedEngine/wiFont_BindLua.cpp index e336cff5d..18edf9675 100644 --- a/WickedEngine/wiFont_BindLua.cpp +++ b/WickedEngine/wiFont_BindLua.cpp @@ -186,9 +186,8 @@ void wiFont_BindLua::Bind() wiLua::GetGlobal()->RunText("WIFALIGN_LEFT = 0"); wiLua::GetGlobal()->RunText("WIFALIGN_CENTER = 1"); - wiLua::GetGlobal()->RunText("WIFALIGN_MID = 2"); - wiLua::GetGlobal()->RunText("WIFALIGN_RIGHT = 3"); - wiLua::GetGlobal()->RunText("WIFALIGN_TOP = 4"); - wiLua::GetGlobal()->RunText("WIFALIGN_BOTTOM = 5"); + wiLua::GetGlobal()->RunText("WIFALIGN_RIGHT = 2"); + wiLua::GetGlobal()->RunText("WIFALIGN_TOP = 3"); + wiLua::GetGlobal()->RunText("WIFALIGN_BOTTOM = 4"); } } diff --git a/WickedEngine/wiHelper.cpp b/WickedEngine/wiHelper.cpp index 43a2eb7e5..3feaa7490 100644 --- a/WickedEngine/wiHelper.cpp +++ b/WickedEngine/wiHelper.cpp @@ -47,11 +47,12 @@ namespace wiHelper return result; } - bool readByteData(const std::string& fileName, std::vector& data, size_t& dataSize){ + bool readByteData(const std::string& fileName, std::vector& data) + { ifstream file(fileName, ios::binary | ios::ate); - if (file.is_open()){ - - dataSize = (size_t)file.tellg(); + if (file.is_open()) + { + size_t dataSize = (size_t)file.tellg(); file.seekg(0, file.beg); data.resize(dataSize); file.read((char*)data.data(), dataSize); diff --git a/WickedEngine/wiHelper.h b/WickedEngine/wiHelper.h index ec438ccdd..9fa74739d 100644 --- a/WickedEngine/wiHelper.h +++ b/WickedEngine/wiHelper.h @@ -30,7 +30,7 @@ namespace wiHelper std::string toUpper(const std::string& s); - bool readByteData(const std::string& fileName, std::vector& data, size_t& dataSize); + bool readByteData(const std::string& fileName, std::vector& data); void messageBox(const std::string& msg, const std::string& caption = "Warning!"); diff --git a/WickedEngine/wiProfiler.cpp b/WickedEngine/wiProfiler.cpp index a1ce882f3..03cea0558 100644 --- a/WickedEngine/wiProfiler.cpp +++ b/WickedEngine/wiProfiler.cpp @@ -191,7 +191,7 @@ namespace wiProfiler ss << endl; } - wiFont(ss.str(), wiFontProps(x, y, -1, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, wiColor(255, 255, 255, 255), wiColor(0, 0, 0, 255))).Draw(threadID); + wiFont(ss.str(), wiFontProps(x, y, WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, wiColor(255, 255, 255, 255), wiColor(0, 0, 0, 255))).Draw(threadID); } void SetEnabled(bool value) diff --git a/WickedEngine/wiRectPacker.cpp b/WickedEngine/wiRectPacker.cpp index 45c204a76..4bad68a4c 100644 --- a/WickedEngine/wiRectPacker.cpp +++ b/WickedEngine/wiRectPacker.cpp @@ -8,23 +8,23 @@ using namespace std; namespace wiRectPacker { - bool area(rect_xywhf* a, rect_xywhf* b) { + bool area(rect_xywh* a, rect_xywh* b) { return a->area() > b->area(); } - bool perimeter(rect_xywhf* a, rect_xywhf* b) { + bool perimeter(rect_xywh* a, rect_xywh* b) { return a->perimeter() > b->perimeter(); } - bool max_side(rect_xywhf* a, rect_xywhf* b) { + bool max_side(rect_xywh* a, rect_xywh* b) { return std::max(a->w, a->h) > std::max(b->w, b->h); } - bool max_width(rect_xywhf* a, rect_xywhf* b) { + bool max_width(rect_xywh* a, rect_xywh* b) { return a->w > b->w; } - bool max_height(rect_xywhf* a, rect_xywhf* b) { + bool max_height(rect_xywh* a, rect_xywh* b) { return a->h > b->h; } @@ -32,7 +32,7 @@ namespace wiRectPacker // just add another comparing function name to cmpf to perform another packing attempt // more functions == slower but probably more efficient cases covered and hence less area wasted - bool(*cmpf[])(rect_xywhf*, rect_xywhf*) = { + bool(*cmpf[])(rect_xywh*, rect_xywh*) = { area, perimeter, max_side, @@ -89,7 +89,7 @@ namespace wiRectPacker delcheck(); } - node* insert(rect_xywhf& img) { + node* insert(rect_xywh& img) { if (c[0].pn && c[0].fill) { node* newn; if (newn = c[0].pn->insert(img)) return newn; @@ -130,16 +130,16 @@ namespace wiRectPacker } }; - rect_wh _rect2D(rect_xywhf* const * v, int n, int max_s, std::vector& succ, std::vector& unsucc) { + rect_wh _rect2D(rect_xywh* const * v, int n, int max_s, std::vector& succ, std::vector& unsucc) { node root; - const int funcs = (sizeof(cmpf) / sizeof(bool(*)(rect_xywhf*, rect_xywhf*))); + const int funcs = (sizeof(cmpf) / sizeof(bool(*)(rect_xywh*, rect_xywh*))); - rect_xywhf** order[funcs]; + rect_xywh** order[funcs]; for (int f = 0; f < funcs; ++f) { - order[f] = new rect_xywhf*[n]; - memcpy(order[f], v, sizeof(rect_xywhf*) * n); + order[f] = new rect_xywh*[n]; + memcpy(order[f], v, sizeof(rect_xywh*) * n); sort(order[f], order[f] + n, cmpf[f]); } @@ -226,16 +226,16 @@ namespace wiRectPacker } - bool pack(rect_xywhf* const * v, int n, int max_s, std::vector& bins) { + bool pack(rect_xywh* const * v, int n, int max_s, std::vector& bins) { rect_wh _rect(max_s, max_s); for (int i = 0; i < n; ++i) if (!v[i]->fits(_rect)) return false; - std::vector vec[2], *p[2] = { vec, vec + 1 }; + std::vector vec[2], *p[2] = { vec, vec + 1 }; vec[0].resize(n); vec[1].clear(); - memcpy(&vec[0][0], v, sizeof(rect_xywhf*)*n); + memcpy(&vec[0][0], v, sizeof(rect_xywh*)*n); bin* b = 0; @@ -327,9 +327,4 @@ namespace wiRectPacker return 2 * w + 2 * h; } - - rect_xywhf::rect_xywhf(const rect_ltrb& rr) : rect_xywh(rr) {} - rect_xywhf::rect_xywhf(int x, int y, int width, int height) : rect_xywh(x, y, width, height) {} - rect_xywhf::rect_xywhf() {} - } diff --git a/WickedEngine/wiRectPacker.h b/WickedEngine/wiRectPacker.h index a3cd24af1..6ce8dd185 100644 --- a/WickedEngine/wiRectPacker.h +++ b/WickedEngine/wiRectPacker.h @@ -6,12 +6,12 @@ /* of your interest: -1. rect_xywhf - structure representing your rectangle object +1. rect_xywh - structure representing your rectangle object members: int x, y, w, h; 2. bin - structure representing resultant bin object -3. bool pack(rect_xywhf* const * v, int n, int max_side, std::std::vector& bins) - actual packing function +3. bool pack(rect_xywh* const * v, int n, int max_side, std::std::vector& bins) - actual packing function Arguments: input/output: v - pointer to array of pointers to your rectangles (const here means that the pointers will point to the same rectangles after the call) input: n - rectangles count @@ -24,7 +24,7 @@ Every bin also keeps information about its width and height of course, none of t returns true on success, false if one of the rectangles' dimension was bigger than max_side You want to your rectangles representing your textures/glyph objects with GL_MAX_TEXTURE_SIZE as max_side, -then for each bin iterate through its rectangles, typecast each one to your own structure (or manually add userdata) and then memcpy its pixel contents (rotated by 90 degrees if "flipped" rect_xywhf's member is true) +then for each bin iterate through its rectangles, typecast each one to your own structure (or manually add userdata) and then memcpy its pixel contents (rotated by 90 degrees if "flipped" rect_xywh's member is true) to the array representing your texture atlas to the place specified by the rectangle, then finally upload it with glTexImage2D. Algorithm doesn't create any new rectangles. @@ -69,18 +69,12 @@ namespace wiRectPacker void r(int), b(int); }; - struct rect_xywhf : public rect_xywh { - rect_xywhf(const rect_ltrb&); - rect_xywhf(int x, int y, int width, int height); - rect_xywhf(); - }; - struct bin { rect_wh size; - std::vector rects; + std::vector rects; }; - bool pack(rect_xywhf* const * v, int n, int max_side, std::vector& bins); + bool pack(rect_xywh* const * v, int n, int max_side, std::vector& bins); } diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 1dc7f1eaa..57bc2e2dd 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -6892,7 +6892,7 @@ void DrawTracedScene(const CameraComponent& camera, Texture2D* result, GRAPHICST } bool repackAtlas = false; - static unordered_map storedTextures; + static unordered_map storedTextures; const int atlasWrapBorder = 1; for (Texture2D* tex : sceneTextures) { @@ -6904,7 +6904,7 @@ void DrawTracedScene(const CameraComponent& camera, Texture2D* result, GRAPHICST if (storedTextures.find(tex) == storedTextures.end()) { // we need to pack this texture into the atlas - rect_xywhf newRect = rect_xywhf(0, 0, tex->GetDesc().Width + atlasWrapBorder * 2, tex->GetDesc().Height + atlasWrapBorder * 2); + rect_xywh newRect = rect_xywh(0, 0, tex->GetDesc().Width + atlasWrapBorder * 2, tex->GetDesc().Height + atlasWrapBorder * 2); storedTextures[tex] = newRect; repackAtlas = true; @@ -6914,7 +6914,7 @@ void DrawTracedScene(const CameraComponent& camera, Texture2D* result, GRAPHICST if (repackAtlas) { - rect_xywhf** out_rects = new rect_xywhf*[storedTextures.size()]; + rect_xywh** out_rects = new rect_xywh*[storedTextures.size()]; int i = 0; for (auto& it : storedTextures) { @@ -7011,7 +7011,7 @@ void DrawTracedScene(const CameraComponent& camera, Texture2D* result, GRAPHICST // Add extended properties: const TextureDesc& desc = atlasTexture->GetDesc(); - rect_xywhf rect; + rect_xywh rect; if (material.GetBaseColorMap() != nullptr) @@ -7305,7 +7305,7 @@ void ManageDecalAtlas(GRAPHICSTHREAD threadID) const int atlasClampBorder = 1; using namespace wiRectPacker; - static unordered_map storedTextures; + static unordered_map storedTextures; Scene& scene = GetScene(); @@ -7319,7 +7319,7 @@ void ManageDecalAtlas(GRAPHICSTHREAD threadID) if (storedTextures.find(decal.texture) == storedTextures.end()) { // we need to pack this decal texture into the atlas - rect_xywhf newRect = rect_xywhf(0, 0, decal.texture->GetDesc().Width + atlasClampBorder * 2, decal.texture->GetDesc().Height + atlasClampBorder * 2); + rect_xywh newRect = rect_xywh(0, 0, decal.texture->GetDesc().Width + atlasClampBorder * 2, decal.texture->GetDesc().Height + atlasClampBorder * 2); storedTextures[decal.texture] = newRect; repackAtlas = true; @@ -7331,7 +7331,7 @@ void ManageDecalAtlas(GRAPHICSTHREAD threadID) // Update atlas texture if it is invalidated: if (repackAtlas) { - rect_xywhf** out_rects = new rect_xywhf*[storedTextures.size()]; + rect_xywh** out_rects = new rect_xywh*[storedTextures.size()]; int i = 0; for (auto& it : storedTextures) { @@ -7396,7 +7396,7 @@ void ManageDecalAtlas(GRAPHICSTHREAD threadID) { const TextureDesc& desc = atlasTexture->GetDesc(); - rect_xywhf rect = storedTextures[decal.texture]; + rect_xywh rect = storedTextures[decal.texture]; // eliminate border expansion: rect.x += atlasClampBorder; diff --git a/WickedEngine/wiResourceManager.cpp b/WickedEngine/wiResourceManager.cpp index 802780ef9..fa25d1f62 100644 --- a/WickedEngine/wiResourceManager.cpp +++ b/WickedEngine/wiResourceManager.cpp @@ -241,10 +241,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) case Data_Type::VERTEXSHADER: { vector buffer; - size_t bufferSize; - if (wiHelper::readByteData(nameStr, buffer, bufferSize)) { + if (wiHelper::readByteData(nameStr, buffer)) { VertexShader* shader = new VertexShader; - wiRenderer::GetDevice()->CreateVertexShader(buffer.data(), bufferSize, shader); + wiRenderer::GetDevice()->CreateVertexShader(buffer.data(), buffer.size(), shader); success = shader; } else{ @@ -255,10 +254,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) case Data_Type::PIXELSHADER: { vector buffer; - size_t bufferSize; - if (wiHelper::readByteData(nameStr, buffer, bufferSize)){ + if (wiHelper::readByteData(nameStr, buffer)){ PixelShader* shader = new PixelShader; - wiRenderer::GetDevice()->CreatePixelShader(buffer.data(), bufferSize, shader); + wiRenderer::GetDevice()->CreatePixelShader(buffer.data(), buffer.size(), shader); success = shader; } else{ @@ -269,10 +267,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) case Data_Type::GEOMETRYSHADER: { vector buffer; - size_t bufferSize; - if (wiHelper::readByteData(nameStr, buffer, bufferSize)){ + if (wiHelper::readByteData(nameStr, buffer)){ GeometryShader* shader = new GeometryShader; - wiRenderer::GetDevice()->CreateGeometryShader(buffer.data(), bufferSize, shader); + wiRenderer::GetDevice()->CreateGeometryShader(buffer.data(), buffer.size(), shader); success = shader; } else{ @@ -283,10 +280,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) case Data_Type::HULLSHADER: { vector buffer; - size_t bufferSize; - if (wiHelper::readByteData(nameStr, buffer, bufferSize)){ + if (wiHelper::readByteData(nameStr, buffer)){ HullShader* shader = new HullShader; - wiRenderer::GetDevice()->CreateHullShader(buffer.data(), bufferSize, shader); + wiRenderer::GetDevice()->CreateHullShader(buffer.data(), buffer.size(), shader); success = shader; } else{ @@ -297,10 +293,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) case Data_Type::DOMAINSHADER: { vector buffer; - size_t bufferSize; - if (wiHelper::readByteData(nameStr, buffer, bufferSize)){ + if (wiHelper::readByteData(nameStr, buffer)){ DomainShader* shader = new DomainShader; - wiRenderer::GetDevice()->CreateDomainShader(buffer.data(), bufferSize, shader); + wiRenderer::GetDevice()->CreateDomainShader(buffer.data(), buffer.size(), shader); success = shader; } else{ @@ -311,10 +306,9 @@ void* wiResourceManager::add(const wiHashString& name, Data_Type newType) case Data_Type::COMPUTESHADER: { vector buffer; - size_t bufferSize; - if (wiHelper::readByteData(nameStr, buffer, bufferSize)) { + if (wiHelper::readByteData(nameStr, buffer)) { ComputeShader* shader = new ComputeShader; - wiRenderer::GetDevice()->CreateComputeShader(buffer.data(), bufferSize, shader); + wiRenderer::GetDevice()->CreateComputeShader(buffer.data(), buffer.size(), shader); success = shader; } else { diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index ec65b71e1..721b6eba9 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 22; // minor bug fixes, alterations, refactors, updates - const int revision = 15; + const int revision = 16; long GetVersion() diff --git a/WickedEngine/wiWidget.cpp b/WickedEngine/wiWidget.cpp index 7c1bcc7ea..d76cb762d 100644 --- a/WickedEngine/wiWidget.cpp +++ b/WickedEngine/wiWidget.cpp @@ -103,7 +103,7 @@ void wiWidget::RenderTooltip(wiGUI* gui) { tooltipPos.y += 40; } - wiFontProps fontProps = wiFontProps((int)tooltipPos.x, (int)tooltipPos.y, -1, WIFALIGN_LEFT, WIFALIGN_TOP); + wiFontProps fontProps = wiFontProps((int)tooltipPos.x, (int)tooltipPos.y, WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_TOP); fontProps.color = wiColor(25, 25, 25, 255); wiFont tooltipFont = wiFont(tooltip, fontProps); if (!scriptTip.empty()) @@ -398,7 +398,7 @@ void wiButton::Render(wiGUI* gui) scissorRect.right = (LONG)(translation.x + scale.x); scissorRect.top = (LONG)(translation.y); wiRenderer::GetDevice()->BindScissorRects(1, &scissorRect, gui->GetGraphicsThread()); - wiFont(text, wiFontProps((int)(translation.x + scale.x*0.5f), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, + wiFont(text, wiFontProps((int)(translation.x + scale.x*0.5f), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); } @@ -468,7 +468,7 @@ void wiLabel::Render(wiGUI* gui) scissorRect.right = (LONG)(translation.x + scale.x); scissorRect.top = (LONG)(translation.y); wiRenderer::GetDevice()->BindScissorRects(1, &scissorRect, gui->GetGraphicsThread()); - wiFont(text, wiFontProps((int)translation.x + 2, (int)translation.y + 2, -1, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, + wiFont(text, wiFontProps((int)translation.x + 2, (int)translation.y + 2, WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); } @@ -635,7 +635,7 @@ void wiTextInputField::Render(wiGUI* gui) { activeText = value; } - wiFont(activeText, wiFontProps((int)(translation.x + 2), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_LEFT, WIFALIGN_CENTER, 0, 0, + wiFont(activeText, wiFontProps((int)(translation.x + 2), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); } @@ -822,7 +822,7 @@ void wiSlider::Render(wiGUI* gui) wiRenderer::GetDevice()->BindScissorRects(1, &scissorRect, gui->GetGraphicsThread()); } // text - wiFont(text, wiFontProps((int)(translation.x - headWidth * 0.5f), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_RIGHT, WIFALIGN_CENTER, 0, 0, + wiFont(text, wiFontProps((int)(translation.x - headWidth * 0.5f), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_RIGHT, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); //// value @@ -958,7 +958,7 @@ void wiCheckBox::Render(wiGUI* gui) { wiRenderer::GetDevice()->BindScissorRects(1, &scissorRect, gui->GetGraphicsThread()); } - wiFont(text, wiFontProps((int)(translation.x), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_RIGHT, WIFALIGN_CENTER, 0, 0, + wiFont(text, wiFontProps((int)(translation.x), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_RIGHT, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); } @@ -1138,7 +1138,7 @@ void wiComboBox::Render(wiGUI* gui) // control-arrow wiImage::Draw(wiTextureHelper::getColor(color) , wiImageEffects(translation.x + scale.x + 1, translation.y, scale.y, scale.y), gui->GetGraphicsThread()); - wiFont("V", wiFontProps((int)(translation.x + scale.x + scale.y*0.5f), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, + wiFont("V", wiFontProps((int)(translation.x + scale.x + scale.y*0.5f), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); @@ -1146,12 +1146,12 @@ void wiComboBox::Render(wiGUI* gui) { wiRenderer::GetDevice()->BindScissorRects(1, &scissorRect, gui->GetGraphicsThread()); } - wiFont(text, wiFontProps((int)(translation.x), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_RIGHT, WIFALIGN_CENTER, 0, 0, + wiFont(text, wiFontProps((int)(translation.x), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_RIGHT, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); if (selected >= 0) { - wiFont(items[selected], wiFontProps((int)(translation.x + scale.x*0.5f), (int)(translation.y + scale.y*0.5f), -1, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, + wiFont(items[selected], wiFontProps((int)(translation.x + scale.x*0.5f), (int)(translation.y + scale.y*0.5f), WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); } @@ -1184,7 +1184,7 @@ void wiComboBox::Render(wiGUI* gui) } wiImage::Draw(wiTextureHelper::getColor(col) , wiImageEffects(translation.x, translation.y + _GetItemOffset(i), scale.x, scale.y), gui->GetGraphicsThread()); - wiFont(x, wiFontProps((int)(translation.x + scale.x*0.5f), (int)(translation.y + scale.y*0.5f + _GetItemOffset(i)), -1, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, + wiFont(x, wiFontProps((int)(translation.x + scale.x*0.5f), (int)(translation.y + scale.y*0.5f + _GetItemOffset(i)), WIFONTSIZE_DEFAULT, WIFALIGN_CENTER, WIFALIGN_CENTER, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); i++; } @@ -1473,7 +1473,7 @@ void wiWindow::Render(wiGUI* gui) scissorRect.right = (LONG)(translation.x + scale.x); scissorRect.top = (LONG)(translation.y); wiRenderer::GetDevice()->BindScissorRects(1, &scissorRect, gui->GetGraphicsThread()); - wiFont(text, wiFontProps((int)(translation.x + resizeDragger_UpperLeft->scale.x + 2), (int)(translation.y), -1, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, + wiFont(text, wiFontProps((int)(translation.x + resizeDragger_UpperLeft->scale.x + 2), (int)(translation.y), WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, textColor, textShadowColor)).Draw(gui->GetGraphicsThread()); } @@ -1893,7 +1893,7 @@ void wiColorPicker::Render(wiGUI* gui) _rgb << "R: " << (int)(final_color.x * 255) << endl; _rgb << "G: " << (int)(final_color.y * 255) << endl; _rgb << "B: " << (int)(final_color.z * 255) << endl; - wiFont(_rgb.str(), wiFontProps((int)(translation.x + 200), (int)(translation.y + 200), -1, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, + wiFont(_rgb.str(), wiFontProps((int)(translation.x + 200), (int)(translation.y + 200), WIFONTSIZE_DEFAULT, WIFALIGN_LEFT, WIFALIGN_TOP, 0, 0, textColor, textShadowColor)).Draw(threadID); }