[Core] Add option to hash text contents to text config (#238)

This commit is contained in:
Nic Barker 2025-01-30 09:59:42 +13:00 committed by GitHub
parent 5fae7a6249
commit a1e692b72a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 20 additions and 9 deletions

View File

@ -111,12 +111,13 @@ TextWrapMode :: enum EnumBackingType {
}
TextElementConfig :: struct {
textColor: Color,
fontId: u16,
fontSize: u16,
letterSpacing: u16,
lineHeight: u16,
wrapMode: TextWrapMode,
textColor: Color,
fontId: u16,
fontSize: u16,
letterSpacing: u16,
lineHeight: u16,
wrapMode: TextWrapMode,
hashStringContents: bool,
}
ImageElementConfig :: struct {

Binary file not shown.

Binary file not shown.

Binary file not shown.

16
clay.h
View File

@ -324,6 +324,7 @@ typedef struct {
uint16_t letterSpacing;
uint16_t lineHeight;
Clay_TextElementConfigWrapMode wrapMode;
bool hashStringContents;
#ifdef CLAY_EXTEND_CONFIG_TEXT
CLAY_EXTEND_CONFIG_TEXT
#endif
@ -987,9 +988,18 @@ uint32_t Clay__HashTextWithConfig(Clay_String *text, Clay_TextElementConfig *con
uint32_t hash = 0;
uintptr_t pointerAsNumber = (uintptr_t)text->chars;
hash += pointerAsNumber;
hash += (hash << 10);
hash ^= (hash >> 6);
if (config->hashStringContents) {
uint32_t maxLengthToHash = CLAY__MIN(text->length, 256);
for (int i = 0; i < maxLengthToHash; i++) {
hash += text->chars[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
} else {
hash += pointerAsNumber;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += text->length;
hash += (hash << 10);