Skip to content

Commit aef8de4

Browse files
committed
AGS.Native: fix deletion of fonts not clearing loaded font data
1 parent 78792c9 commit aef8de4

File tree

9 files changed

+94
-9
lines changed

9 files changed

+94
-9
lines changed

Common/font/fonts.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ struct Font
5454
Bitmap OutlineStencil, OutlineStencilSub;
5555

5656
Font() = default;
57+
Font(const Font &font) = default;
5758
Font(Font &&font) = default;
59+
Font &operator =(const Font &font) = default;
5860
Font &operator =(Font &&font) = default;
5961
};
6062

@@ -547,6 +549,7 @@ bool load_font_size(size_t font_number, const FontInfo &font_info)
547549
fonts.resize(font_number + 1);
548550
else
549551
freefont(font_number);
552+
550553
FontRenderParams params;
551554
params.SizeMultiplier = font_info.SizeMultiplier;
552555
params.LoadMode = (font_info.Flags & FFLG_LOADMODEMASK);
@@ -651,21 +654,25 @@ void freefont(size_t font_number)
651654
if (font_number >= fonts.size())
652655
return;
653656

654-
fonts[font_number].TextStencilSub.Destroy();
655-
fonts[font_number].OutlineStencilSub.Destroy();
656-
fonts[font_number].TextStencil.Destroy();
657-
fonts[font_number].OutlineStencil.Destroy();
658-
if (fonts[font_number].Renderer != nullptr)
657+
if (fonts[font_number].Renderer)
659658
fonts[font_number].Renderer->FreeMemory(font_number);
659+
fonts[font_number] = Font();
660+
}
661+
662+
void movefont(size_t old_number, size_t new_number)
663+
{
664+
if (old_number == new_number)
665+
return; // same number
660666

661-
fonts[font_number].Renderer = nullptr;
667+
fonts[new_number] = std::move(fonts[old_number]);
668+
fonts[old_number] = Font();
662669
}
663670

664671
void free_all_fonts()
665672
{
666673
for (size_t i = 0; i < fonts.size(); ++i)
667674
{
668-
if (fonts[i].Renderer != nullptr)
675+
if (fonts[i].Renderer)
669676
fonts[i].Renderer->FreeMemory(i);
670677
}
671678
fonts.clear();

Common/font/fonts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ void alloc_font_outline_buffers(size_t font_number,
110110
void adjust_fonts_for_render_mode(bool aa_mode);
111111
// Free particular font's data
112112
void freefont(size_t font_number);
113+
// Moves font data from one index to another; previous index becomes empty
114+
void movefont(size_t old_number, size_t new_number);
113115
// Free all fonts data
114116
void free_all_fonts();
115117

Common/gfx/allegrobitmap.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Bitmap::Bitmap(BITMAP *al_bmp, bool shared_data)
4747
WrapAllegroBitmap(al_bmp, shared_data);
4848
}
4949

50+
Bitmap::Bitmap(const Bitmap &bmp)
51+
{
52+
CreateCopy(&bmp);
53+
}
54+
5055
Bitmap::Bitmap(Bitmap &&bmp)
5156
{
5257
_pixelData = std::move(bmp._pixelData);
@@ -61,6 +66,12 @@ Bitmap::~Bitmap()
6166
Destroy();
6267
}
6368

69+
Bitmap &Bitmap::operator =(const Bitmap &bmp)
70+
{
71+
CreateCopy(&bmp);
72+
return *this;
73+
}
74+
6475
//=============================================================================
6576
// Creation and destruction
6677
//=============================================================================
@@ -152,6 +163,13 @@ bool Bitmap::CreateCopy(const Bitmap *src, int color_depth)
152163
if (src == this || src->_alBitmap == _alBitmap)
153164
return false; // cannot create a copy of yourself
154165

166+
// Handle uninitialized bitmap case
167+
if (!src->_alBitmap)
168+
{
169+
Destroy();
170+
return true;
171+
}
172+
155173
if (Create(src->_alBitmap->w, src->_alBitmap->h, color_depth ? color_depth : bitmap_color_depth(src->_alBitmap)))
156174
{
157175
blit(src->_alBitmap, _alBitmap, 0, 0, 0, 0, _alBitmap->w, _alBitmap->h);

Common/gfx/allegrobitmap.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ class Bitmap
3939
Bitmap() = default;
4040
Bitmap(int width, int height, int color_depth = 0);
4141
Bitmap(PixelBuffer &&pxbuf);
42+
// Constructs a sub-bitmap, referencing parent
4243
Bitmap(Bitmap *src, const Rect &rc);
44+
// Wraps Allegro BITMAP object, optionally owning it
4345
Bitmap(BITMAP *al_bmp, bool shared_data);
46+
// Copy-constructor: constructs a full Bitmap copy
47+
Bitmap(const Bitmap &bmp);
48+
// Move-constructor: moves pixel data from another bitmap
4449
Bitmap(Bitmap &&bmp);
4550
~Bitmap();
4651

52+
Bitmap &operator =(const Bitmap &bmp);
53+
Bitmap &operator =(Bitmap &&bmp) = default;
54+
4755
// Allocate new bitmap.
4856
// NOTE: color_depth is in BITS per pixel (i.e. 8, 16, 24, 32...).
4957
// NOTE: in all of these color_depth may be passed as 0 in which case a default

Editor/AGS.Editor/Components/FontsComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override void CommandClick(string controlID)
4949
newItem.TTFMetricsFixup = _agsEditor.CurrentGame.Settings.TTFMetricsFixup; // use defaults
5050
items.Add(newItem);
5151
Utilities.CopyFont(0, newItem.ID);
52-
Factory.NativeProxy.GameSettingsChanged(_agsEditor.CurrentGame);
52+
Factory.NativeProxy.OnFontAdded(_agsEditor.CurrentGame, newItem.ID);
5353
_guiController.ProjectTree.StartFromNode(this, TOP_LEVEL_COMMAND_ID);
5454
_guiController.ProjectTree.AddTreeLeaf(this, GetNodeID(newItem), newItem.ID.ToString() + ": " + newItem.Name, "FontIcon");
5555
_guiController.ProjectTree.SelectNode(this, GetNodeID(newItem));
@@ -86,6 +86,7 @@ public override void CommandClick(string controlID)
8686
}
8787
_agsEditor.CurrentGame.Fonts.Remove(_itemRightClicked);
8888
_agsEditor.CurrentGame.FilesAddedOrRemoved = true;
89+
Factory.NativeProxy.OnFontDeleted(_agsEditor.CurrentGame, removingID);
8990
Factory.NativeProxy.GameSettingsChanged(_agsEditor.CurrentGame);
9091
RePopulateTreeView();
9192
FontTypeConverter.SetFontList(_agsEditor.CurrentGame.Fonts);

Editor/AGS.Editor/NativeProxy.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ public int FindTTFSizeForHeight(string fileName, int size)
230230
return _native.FindTTFSizeForHeight(fileName, size);
231231
}
232232

233+
public void OnFontAdded(Game game, int fontSlot)
234+
{
235+
_native.OnGameFontAdded(game, fontSlot);
236+
}
237+
238+
public void OnFontDeleted(Game game, int fontSlot)
239+
{
240+
_native.OnGameFontDeleted(game, fontSlot);
241+
}
242+
233243
public void OnFontUpdated(Game game, int fontSlot, bool forceUpdate)
234244
{
235245
_native.OnGameFontUpdated(game, fontSlot, forceUpdate);

Editor/AGS.Native/NativeMethods.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ extern HAGSError reset_sprite_file();
9191
extern void PaletteUpdated(cli::array<PaletteEntry^>^ newPalette);
9292
extern void GameDirChanged(String ^workingDir);
9393
extern void GameUpdated(Game ^game, bool forceUpdate);
94+
extern void GameFontAdded(Game ^game, int fontNumber);
95+
extern void GameFontDeleted(Game ^game, int fontNumber);
9496
extern void GameFontUpdated(Game ^game, int fontNumber, bool forceUpdate);
9597
extern void UpdateNativeSpritesToGame(Game ^game, CompileMessages ^errors);
9698
extern void draw_room_background(void *roomptr, HDC hdc, int x, int y, int bgnum, float scaleFactor, int maskType, int selectedArea, int maskTransparency);
@@ -372,9 +374,19 @@ namespace AGS
372374
return height;
373375
}
374376

377+
void NativeMethods::OnGameFontAdded(Game^ game, int fontSlot)
378+
{
379+
::GameFontAdded(game, fontSlot);
380+
}
381+
382+
void NativeMethods::OnGameFontDeleted(Game^ game, int fontSlot)
383+
{
384+
::GameFontDeleted(game, fontSlot);
385+
}
386+
375387
void NativeMethods::OnGameFontUpdated(Game^ game, int fontSlot, bool forceUpdate)
376388
{
377-
GameFontUpdated(game, fontSlot, forceUpdate);
389+
::GameFontUpdated(game, fontSlot, forceUpdate);
378390
}
379391

380392
AGS::Types::SpriteInfo^ NativeMethods::GetSpriteInfo(int spriteSlot)

Editor/AGS.Native/NativeMethods.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ namespace AGS
7676
// Measures the TTF font from the given file, and tries to find a point size corresponding
7777
// to the closest pixel height match, returns the found point size, or 0 in case of error.
7878
int FindTTFSizeForHeight(String ^fileName, int size);
79+
void OnGameFontAdded(Game^ game, int fontSlot);
80+
void OnGameFontDeleted(Game^ game, int fontSlot);
7981
void OnGameFontUpdated(Game^ game, int fontSlot, bool forceUpdate);
8082
Dictionary<int,Sprite^>^ LoadAllSpriteDimensions();
8183
void LoadNewSpriteFile();

Editor/AGS.Native/agsnative.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,8 +2057,33 @@ void GameUpdated(Game ^game, bool forceUpdate) {
20572057
}
20582058
}
20592059

2060+
void GameFontAdded(Game ^game, int fontNumber)
2061+
{
2062+
thisgame.numfonts = game->Fonts->Count;
2063+
thisgame.fonts.resize(thisgame.numfonts);
2064+
for (int i = fontNumber; i < thisgame.numfonts; i++)
2065+
{
2066+
GameFontUpdated(game, i, true);
2067+
}
2068+
}
2069+
2070+
void GameFontDeleted(Game ^game, int fontNumber)
2071+
{
2072+
thisgame.numfonts = game->Fonts->Count;
2073+
thisgame.fonts.resize(thisgame.numfonts);
2074+
freefont(fontNumber);
2075+
for (int i = fontNumber; i < thisgame.numfonts; i++)
2076+
{
2077+
GameFontUpdated(game, i, true);
2078+
}
2079+
}
2080+
20602081
void GameFontUpdated(Game ^game, int fontNumber, bool forceUpdate)
20612082
{
2083+
assert(fontNumber >= 0 && fontNumber < thisgame.numfonts);
2084+
if (fontNumber < 0 || fontNumber >= thisgame.numfonts)
2085+
return;
2086+
20622087
FontInfo &font_info = thisgame.fonts[fontNumber];
20632088
AGS::Types::Font ^font = game->Fonts[fontNumber];
20642089

0 commit comments

Comments
 (0)