Skip to content

Commit

Permalink
Revert "Lua D2D API cache IDWriteTextLayout"
Browse files Browse the repository at this point in the history
This reverts commit d6ed79f.
  • Loading branch information
Aurumaker72 committed Jun 30, 2023
1 parent d6ed79f commit da6c581
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 92 deletions.
65 changes: 20 additions & 45 deletions lua/LuaConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ namespace LuaEngine {
IDWriteFactory* dw_factory;
std::unordered_map<uint32_t, ID2D1SolidColorBrush*> d2d_brush_cache;
std::unordered_map<std::string, ID2D1Bitmap*> d2d_bitmap_cache;
std::unordered_map<std::tuple<std::wstring, int, int, float, float, float, float, float, int, int, int>, IDWriteTextLayout*> dw_text_layout_cache;
//improved debug print from stackoverflow, now shows function info
#ifdef _DEBUG
static void stackDump(lua_State* L) {
Expand Down Expand Up @@ -593,22 +592,16 @@ namespace LuaEngine {
d2d_factory->Release();
d2d_render_target->Release();

for (auto const& [_, val] : d2d_brush_cache) {
for (auto const& [key, val] : d2d_brush_cache) {
val->Release();
}
d2d_brush_cache.clear();

for (auto const& [_, val] : d2d_bitmap_cache) {
for (auto const& [key, val] : d2d_bitmap_cache) {
val->Release();
}
d2d_bitmap_cache.clear();

for (auto const& [_, val] : dw_text_layout_cache) {
val->Release();
}
dw_text_layout_cache.clear();


ReleaseDC(mainHWND, lua_dc);
lua_dc = NULL;
d2d_factory = NULL;
Expand Down Expand Up @@ -1890,52 +1883,34 @@ namespace LuaEngine {
int horizontal_alignment = luaL_checkinteger(L, 14);
int vertical_alignment = luaL_checkinteger(L, 15);
int options = luaL_checkinteger(L, 16);

IDWriteTextFormat* text_format;

auto tuple = std::make_tuple(
text,
font_weight,
font_style,
dw_factory->CreateTextFormat(
widen(font_name).c_str(),
NULL,
(DWRITE_FONT_WEIGHT)font_weight,
(DWRITE_FONT_STYLE)font_style,
DWRITE_FONT_STRETCH_NORMAL,
font_size,
rectangle.left,
rectangle.top,
rectangle.right,
rectangle.bottom,
horizontal_alignment,
vertical_alignment,
options
L"",
&text_format
);

if (!dw_text_layout_cache.contains(tuple)) {
IDWriteTextFormat* text_format;

dw_factory->CreateTextFormat(
widen(font_name).c_str(),
NULL,
(DWRITE_FONT_WEIGHT)font_weight,
(DWRITE_FONT_STYLE)font_style,
DWRITE_FONT_STRETCH_NORMAL,
font_size,
L"",
&text_format
);

text_format->SetTextAlignment((DWRITE_TEXT_ALIGNMENT)horizontal_alignment);
text_format->SetParagraphAlignment((DWRITE_PARAGRAPH_ALIGNMENT)vertical_alignment);
text_format->SetTextAlignment((DWRITE_TEXT_ALIGNMENT)horizontal_alignment);
text_format->SetParagraphAlignment((DWRITE_PARAGRAPH_ALIGNMENT)vertical_alignment);

IDWriteTextLayout* text_layout;

dw_factory->CreateTextLayout(text.c_str(), text.length(), text_format, rectangle.right - rectangle.left, rectangle.bottom - rectangle.top, &text_layout);
IDWriteTextLayout* text_layout;

text_format->Release();

dw_text_layout_cache[tuple] = text_layout;
}

dw_factory->CreateTextLayout(text.c_str(), text.length(), text_format, rectangle.right - rectangle.left, rectangle.bottom - rectangle.top, &text_layout);

d2d_render_target->DrawTextLayout({
.x = rectangle.left,
.y = rectangle.top,
}, dw_text_layout_cache[tuple], brush, (D2D1_DRAW_TEXT_OPTIONS)options);
}, text_layout, brush, (D2D1_DRAW_TEXT_OPTIONS)options);

text_format->Release();
text_layout->Release();

return 0;
}
Expand Down
47 changes: 0 additions & 47 deletions lua/LuaConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,53 +76,6 @@ inline static std::wstring widen(const std::string& str) {
return ws;
}

// goddamn core polluting with macros
#undef Index
// https://stackoverflow.com/a/7115547/14472122
#include <tuple>
namespace std {
namespace {

// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// http://stackoverflow.com/questions/4948780

template <class T>
inline void hash_combine(std::size_t& seed, T const& v) {
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
static void apply(size_t& seed, Tuple const& tuple) {
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};

template <class Tuple>
struct HashValueImpl<Tuple, 0> {
static void apply(size_t& seed, Tuple const& tuple) {
hash_combine(seed, std::get<0>(tuple));
}
};
}

template <typename ... TT>
struct hash<std::tuple<TT...>> {
size_t
operator()(std::tuple<TT...> const& tt) const {
size_t seed = 0;
HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
return seed;
}

};
}


#endif

Expand Down

0 comments on commit da6c581

Please sign in to comment.