1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Peter Nelson a6cba8cbeb
Merge 251422c529 into 7bb4940ebd 2025-07-19 13:29:56 +00:00
Peter Nelson 251422c529
Codechange: Decouple glyph map from SpriteFontCache instances.
This makes the map independent from the SpriteFontCache instances.
2025-07-19 14:29:41 +01:00
Peter Nelson 7bb4940ebd
Codechange: Use unique_ptr for all pointers in script instance. (#14339)
Removes manual memory management with new/delete.
2025-07-19 09:29:30 +01:00
9 changed files with 76 additions and 87 deletions

View File

@ -83,7 +83,7 @@ void AIInstance::Died()
void AIInstance::LoadDummyScript() void AIInstance::LoadDummyScript()
{ {
ScriptAllocatorScope alloc_scope(this->engine); ScriptAllocatorScope alloc_scope(this->engine.get());
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI"); Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
} }

View File

@ -70,16 +70,6 @@ public:
*/ */
virtual int GetFontSize() const { return this->height; } virtual int GetFontSize() const { return this->height; }
/**
* Map a SpriteID to the key
* @param key The key to map to.
* @param sprite The sprite that is being mapped.
*/
virtual void SetUnicodeGlyph(char32_t key, SpriteID sprite) = 0;
/** Initialize the glyph map */
virtual void InitializeUnicodeGlyphMap() = 0;
/** Clear the font cache. */ /** Clear the font cache. */
virtual void ClearFontCache() = 0; virtual void ClearFontCache() = 0;
@ -153,20 +143,6 @@ public:
virtual bool IsBuiltInFont() = 0; virtual bool IsBuiltInFont() = 0;
}; };
/** Map a SpriteID to the font size and key */
inline void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite)
{
FontCache::Get(size)->SetUnicodeGlyph(key, sprite);
}
/** Initialize the glyph map */
inline void InitializeUnicodeGlyphMap()
{
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
FontCache::Get(fs)->InitializeUnicodeGlyphMap();
}
}
inline void ClearFontCache(FontSizes fontsizes) inline void ClearFontCache(FontSizes fontsizes)
{ {
for (FontSize fs : fontsizes) { for (FontSize fs : fontsizes) {
@ -237,4 +213,8 @@ void UninitFontCache();
bool GetFontAAState(); bool GetFontAAState();
void SetFont(FontSize fontsize, const std::string &font, uint size); void SetFont(FontSize fontsize, const std::string &font, uint size);
/* Implemented in spritefontcache.cpp */
void InitializeUnicodeGlyphMap();
void SetUnicodeGlyph(FontSize size, char32_t key, SpriteID sprite);
#endif /* FONTCACHE_H */ #endif /* FONTCACHE_H */

View File

@ -10,6 +10,7 @@
#include "../stdafx.h" #include "../stdafx.h"
#include "../fontcache.h" #include "../fontcache.h"
#include "../gfx_layout.h" #include "../gfx_layout.h"
#include "../string_func.h"
#include "../zoom_func.h" #include "../zoom_func.h"
#include "spritefontcache.h" #include "spritefontcache.h"
@ -31,41 +32,43 @@ static int ScaleFontTrad(int value)
return UnScaleByZoom(value * ZOOM_BASE, _font_zoom); return UnScaleByZoom(value * ZOOM_BASE, _font_zoom);
} }
/** static std::array<std::unordered_map<char32_t, SpriteID>, FS_END> _char_maps{}; ///< Glyph map for each font size.
* Create a new sprite font cache.
* @param fs The font size to create the cache for.
*/
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
{
this->InitializeUnicodeGlyphMap();
this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
}
/** /**
* Get SpriteID associated with a character. * Get SpriteID associated with a character.
* @param key Character to find. * @param key Character to find.
* @return SpriteID for character, or 0 if not present. * @return SpriteID for character, or 0 if not present.
*/ */
SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key) static SpriteID GetUnicodeGlyph(FontSize fs, char32_t key)
{ {
const auto found = this->char_map.find(key); auto found = _char_maps[fs].find(key);
if (found == std::end(this->char_map)) return 0; if (found != std::end(_char_maps[fs])) return found->second;
return found->second; return 0;
} }
void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite) /**
* Set the SpriteID for a unicode character.
* @param fs Font size to set.
* @param key Unicode character to set.
* @param sprite SpriteID of character.
*/
void SetUnicodeGlyph(FontSize fs, char32_t key, SpriteID sprite)
{ {
this->char_map[key] = sprite; _char_maps[fs][key] = sprite;
} }
void SpriteFontCache::InitializeUnicodeGlyphMap() /**
* Initialize the glyph map for a font size.
* This populates the glyph map with the baseset font sprites.
* @param fs Font size to initialize.
*/
void InitializeUnicodeGlyphMap(FontSize fs)
{ {
/* Clear out existing glyph map if it exists */ /* Clear out existing glyph map if it exists */
this->char_map.clear(); _char_maps[fs].clear();
SpriteID base; SpriteID base;
switch (this->fs) { switch (fs) {
default: NOT_REACHED(); default: NOT_REACHED();
case FS_MONO: // Use normal as default for mono spaced font case FS_MONO: // Use normal as default for mono spaced font
case FS_NORMAL: base = SPR_ASCII_SPACE; break; case FS_NORMAL: base = SPR_ASCII_SPACE; break;
@ -76,24 +79,45 @@ void SpriteFontCache::InitializeUnicodeGlyphMap()
for (uint i = ASCII_LETTERSTART; i < 256; i++) { for (uint i = ASCII_LETTERSTART; i < 256; i++) {
SpriteID sprite = base + i - ASCII_LETTERSTART; SpriteID sprite = base + i - ASCII_LETTERSTART;
if (!SpriteExists(sprite)) continue; if (!SpriteExists(sprite)) continue;
this->SetUnicodeGlyph(i, sprite); SetUnicodeGlyph(fs, i, sprite);
this->SetUnicodeGlyph(i + SCC_SPRITE_START, sprite); SetUnicodeGlyph(fs, i + SCC_SPRITE_START, sprite);
} }
/* Modify map to move non-standard glyphs to a better unicode codepoint. */
for (const auto &unicode_map : _default_unicode_map) { for (const auto &unicode_map : _default_unicode_map) {
uint8_t key = unicode_map.key; uint8_t key = unicode_map.key;
if (key == CLRA) { if (key == CLRA) {
/* Clear the glyph. This happens if the glyph at this code point /* Clear the glyph. This happens if the glyph at this code point
* is non-standard and should be accessed by an SCC_xxx enum * is non-standard and should be accessed by an SCC_xxx enum
* entry only. */ * entry only. */
this->SetUnicodeGlyph(unicode_map.code, 0); SetUnicodeGlyph(fs, unicode_map.code, 0);
} else { } else {
SpriteID sprite = base + key - ASCII_LETTERSTART; SpriteID sprite = base + key - ASCII_LETTERSTART;
this->SetUnicodeGlyph(unicode_map.code, sprite); SetUnicodeGlyph(fs, unicode_map.code, sprite);
} }
} }
} }
/**
* Initialize the glyph map.
*/
void InitializeUnicodeGlyphMap()
{
for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
InitializeUnicodeGlyphMap(fs);
}
}
/**
* Create a new sprite font cache.
* @param fs The font size to create the cache for.
*/
SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
{
this->height = ScaleGUITrad(FontCache::GetDefaultFontHeight(this->fs));
this->ascender = (this->height - ScaleFontTrad(FontCache::GetDefaultFontHeight(this->fs))) / 2;
}
void SpriteFontCache::ClearFontCache() void SpriteFontCache::ClearFontCache()
{ {
Layouter::ResetFontCache(this->fs); Layouter::ResetFontCache(this->fs);
@ -104,21 +128,21 @@ void SpriteFontCache::ClearFontCache()
const Sprite *SpriteFontCache::GetGlyph(GlyphID key) const Sprite *SpriteFontCache::GetGlyph(GlyphID key)
{ {
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH); SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
if (sprite == 0) sprite = this->GetUnicodeGlyph('?'); if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
return GetSprite(sprite, SpriteType::Font); return GetSprite(sprite, SpriteType::Font);
} }
uint SpriteFontCache::GetGlyphWidth(GlyphID key) uint SpriteFontCache::GetGlyphWidth(GlyphID key)
{ {
SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH); SpriteID sprite = static_cast<SpriteID>(key & ~SPRITE_GLYPH);
if (sprite == 0) sprite = this->GetUnicodeGlyph('?'); if (sprite == 0) sprite = GetUnicodeGlyph(this->fs, '?');
return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0; return SpriteExists(sprite) ? GetSprite(sprite, SpriteType::Font)->width + ScaleFontTrad(this->fs != FS_NORMAL ? 1 : 0) : 0;
} }
GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback) GlyphID SpriteFontCache::MapCharToGlyph(char32_t key, [[maybe_unused]] bool allow_fallback)
{ {
assert(IsPrintable(key)); assert(IsPrintable(key));
SpriteID sprite = this->GetUnicodeGlyph(key); SpriteID sprite = GetUnicodeGlyph(this->fs, key);
if (sprite == 0) return 0; if (sprite == 0) return 0;
return SPRITE_GLYPH | sprite; return SPRITE_GLYPH | sprite;
} }

View File

@ -10,15 +10,12 @@
#ifndef SPRITEFONTCACHE_H #ifndef SPRITEFONTCACHE_H
#define SPRITEFONTCACHE_H #define SPRITEFONTCACHE_H
#include "../string_func.h"
#include "../fontcache.h" #include "../fontcache.h"
/** Font cache for fonts that are based on a freetype font. */ /** Font cache for fonts that are based on a freetype font. */
class SpriteFontCache : public FontCache { class SpriteFontCache : public FontCache {
public: public:
SpriteFontCache(FontSize fs); SpriteFontCache(FontSize fs);
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override;
void InitializeUnicodeGlyphMap() override;
void ClearFontCache() override; void ClearFontCache() override;
const Sprite *GetGlyph(GlyphID key) override; const Sprite *GetGlyph(GlyphID key) override;
uint GetGlyphWidth(GlyphID key) override; uint GetGlyphWidth(GlyphID key) override;
@ -26,10 +23,6 @@ public:
GlyphID MapCharToGlyph(char32_t key, bool allow_fallback = true) override; GlyphID MapCharToGlyph(char32_t key, bool allow_fallback = true) override;
std::string GetFontName() override { return "sprite"; } std::string GetFontName() override { return "sprite"; }
bool IsBuiltInFont() override { return true; } bool IsBuiltInFont() override { return true; }
private:
std::unordered_map<char32_t, SpriteID> char_map{}; ///< Mapping of characters to sprite IDs.
SpriteID GetUnicodeGlyph(char32_t key);
}; };
#endif /* SPRITEFONTCACHE_H */ #endif /* SPRITEFONTCACHE_H */

View File

@ -46,8 +46,6 @@ public:
TrueTypeFontCache(FontSize fs, int pixels); TrueTypeFontCache(FontSize fs, int pixels);
virtual ~TrueTypeFontCache(); virtual ~TrueTypeFontCache();
int GetFontSize() const override { return this->used_size; } int GetFontSize() const override { return this->used_size; }
void SetUnicodeGlyph(char32_t key, SpriteID sprite) override { this->parent->SetUnicodeGlyph(key, sprite); }
void InitializeUnicodeGlyphMap() override { this->parent->InitializeUnicodeGlyphMap(); }
const Sprite *GetGlyph(GlyphID key) override; const Sprite *GetGlyph(GlyphID key) override;
void ClearFontCache() override; void ClearFontCache() override;
uint GetGlyphWidth(GlyphID key) override; uint GetGlyphWidth(GlyphID key) override;

View File

@ -53,7 +53,7 @@ static ScriptStorage &GetStorage()
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr; /* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine) ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine.get())
{ {
this->last_active = ScriptObject::ActiveInstance::active; this->last_active = ScriptObject::ActiveInstance::active;
ScriptObject::ActiveInstance::active = &instance; ScriptObject::ActiveInstance::active = &instance;
@ -230,8 +230,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
/* static */ bool ScriptObject::CanSuspend() /* static */ bool ScriptObject::CanSuspend()
{ {
Squirrel *squirrel = ScriptObject::GetActiveInstance().engine; Squirrel &squirrel = *ScriptObject::GetActiveInstance().engine;
return GetStorage().allow_do_command && squirrel->CanSuspend(); return GetStorage().allow_do_command && squirrel.CanSuspend();
} }
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue() /* static */ ScriptEventQueue &ScriptObject::GetEventQueue()

View File

@ -50,8 +50,8 @@ static void PrintFunc(bool error_msg, std::string_view message)
ScriptInstance::ScriptInstance(std::string_view api_name) ScriptInstance::ScriptInstance(std::string_view api_name)
{ {
this->storage = new ScriptStorage(); this->storage = std::make_unique<ScriptStorage>();
this->engine = new Squirrel(api_name); this->engine = std::make_unique<Squirrel>(api_name);
this->engine->SetPrintFunction(&PrintFunc); this->engine->SetPrintFunction(&PrintFunc);
} }
@ -59,10 +59,10 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
{ {
ScriptObject::ActiveInstance active(*this); ScriptObject::ActiveInstance active(*this);
this->controller = new ScriptController(company); this->controller = std::make_unique<ScriptController>(company);
/* Register the API functions and classes */ /* Register the API functions and classes */
this->engine->SetGlobalPointer(this->engine); this->engine->SetGlobalPointer(this->engine.get());
this->RegisterAPI(); this->RegisterAPI();
if (this->IsDead()) { if (this->IsDead()) {
/* Failed to register API; a message has already been logged. */ /* Failed to register API; a message has already been logged. */
@ -81,12 +81,11 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
} }
/* Create the main-class */ /* Create the main-class */
this->instance = new SQObject(); this->instance = std::make_unique<SQObject>();
if (!this->engine->CreateClassInstance(instance_name, this->controller, this->instance)) { if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) {
/* If CreateClassInstance has returned false instance has not been /* If CreateClassInstance has returned false instance has not been
* registered with squirrel, so avoid trying to Release it by clearing it now */ * registered with squirrel, so avoid trying to Release it by clearing it now */
delete this->instance; this->instance.reset();
this->instance = nullptr;
this->Died(); this->Died();
return; return;
} }
@ -158,11 +157,10 @@ ScriptInstance::~ScriptInstance()
ScriptObject::ActiveInstance active(*this); ScriptObject::ActiveInstance active(*this);
this->in_shutdown = true; this->in_shutdown = true;
if (instance != nullptr) this->engine->ReleaseObject(this->instance); if (instance != nullptr) this->engine->ReleaseObject(this->instance.get());
if (engine != nullptr) delete this->engine;
delete this->storage; /* Engine must be reset explicitly in scope of the active instance. */
delete this->controller; this->engine.reset();
delete this->instance;
} }
void ScriptInstance::Continue() void ScriptInstance::Continue()
@ -179,11 +177,9 @@ void ScriptInstance::Died()
this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance); if (this->instance != nullptr) this->engine->ReleaseObject(this->instance.get());
delete this->instance; this->engine.reset();
delete this->engine; this->instance.reset();
this->instance = nullptr;
this->engine = nullptr;
} }
void ScriptInstance::GameLoop() void ScriptInstance::GameLoop()

View File

@ -256,7 +256,7 @@ public:
void ReleaseSQObject(HSQOBJECT *obj); void ReleaseSQObject(HSQOBJECT *obj);
protected: protected:
class Squirrel *engine = nullptr; ///< A wrapper around the squirrel vm. std::unique_ptr<class Squirrel> engine; ///< A wrapper around the squirrel vm.
std::string api_version{}; ///< Current API used by this script. std::string api_version{}; ///< Current API used by this script.
/** /**
@ -288,9 +288,9 @@ protected:
virtual void LoadDummyScript() = 0; virtual void LoadDummyScript() = 0;
private: private:
class ScriptController *controller = nullptr; ///< The script main class. std::unique_ptr<class ScriptStorage> storage; ///< Some global information for each running script.
class ScriptStorage *storage = nullptr; ///< Some global information for each running script. std::unique_ptr<class ScriptController> controller; ///< The script main class.
SQObject *instance = nullptr; ///< Squirrel-pointer to the script main class. std::unique_ptr<SQObject> instance; ///< Squirrel-pointer to the script main class.
bool is_started = false; ///< Is the scripts constructor executed? bool is_started = false; ///< Is the scripts constructor executed?
bool is_dead = false; ///< True if the script has been stopped. bool is_dead = false; ///< True if the script has been stopped.

View File

@ -21,8 +21,6 @@ public:
this->height = FontCache::GetDefaultFontHeight(this->fs); this->height = FontCache::GetDefaultFontHeight(this->fs);
} }
void SetUnicodeGlyph(char32_t, SpriteID) override {}
void InitializeUnicodeGlyphMap() override {}
void ClearFontCache() override {} void ClearFontCache() override {}
const Sprite *GetGlyph(GlyphID) override { return nullptr; } const Sprite *GetGlyph(GlyphID) override { return nullptr; }
uint GetGlyphWidth(GlyphID) override { return this->height / 2; } uint GetGlyphWidth(GlyphID) override { return this->height / 2; }