From 6fb4ff7e05c823d3f801d916e415723ebacbdd2c Mon Sep 17 00:00:00 2001 From: John Tringham Date: Tue, 5 Mar 2024 14:47:53 +0000 Subject: [PATCH 1/2] Added basic text rendering supersampling --- common.go | 2 +- gui.go | 14 +++----------- textrenderer.go | 15 ++++++++------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/common.go b/common.go index 30ef878..3fc2e62 100644 --- a/common.go +++ b/common.go @@ -360,7 +360,7 @@ func HandleFontReload() { // For silver.ttf, 21 is the ideal font size. Otherwise, 30 seems to be reasonable. // loadedFont, err := ttf.OpenFont(fontPath, int(globals.Settings.Get(SettingsFontSize).AsFloat())) - loadedFont, err := ttf.OpenFont(fontPath, 48) + loadedFont, err := ttf.OpenFont(fontPath, 96) if err != nil { panic(err) diff --git a/gui.go b/gui.go index ca3333d..75bc6a7 100644 --- a/gui.go +++ b/gui.go @@ -2139,24 +2139,16 @@ func (label *Label) Draw() { if label.RendererResult != nil && len(label.Text) > 0 { - baseline := float32(globals.Font.Ascent()) / 4 - w := int32(label.RendererResult.Image.Size.X) - if w > int32(label.Rect.W) { - w = int32(label.Rect.W) - } - h := int32(label.RendererResult.Image.Size.Y) - if h > int32(label.Rect.H+baseline) { - h = int32(label.Rect.H + baseline) - } - src := &sdl.Rect{0, 0, w, h} + scaleup := float32(4) + // Floor the rectangle to avoid aliasing artifacts when rendering with nearest neighbour - newRect := &sdl.FRect{float32(math.Floor(float64(label.Rect.X + label.Offset.X))), float32(math.Floor(float64(label.Rect.Y + label.Offset.Y))), float32(w), float32(h)} + newRect := &sdl.FRect{float32(math.Floor(float64(label.Rect.X + label.Offset.X))), float32(math.Floor(float64(label.Rect.Y + label.Offset.Y))), float32(w) / scaleup, float32(h) / scaleup} // newRect.Y -= baseline // Center it diff --git a/textrenderer.go b/textrenderer.go index 39a66c8..4c89240 100644 --- a/textrenderer.go +++ b/textrenderer.go @@ -251,6 +251,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "2") finalW := globals.GridSize + scaleup := 4 x := 0 y := 0 @@ -261,7 +262,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme type renderPair struct { Glyph *Glyph - Rect *sdl.Rect + Rect *sdl.FRect } toRender := []*renderPair{} @@ -326,7 +327,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme toRender = append(toRender, &renderPair{ Glyph: glyph, - Rect: &sdl.Rect{int32(x), int32(y), glyph.Width(), glyph.Height()}, + Rect: &sdl.FRect{float32(x * scaleup), float32(y * scaleup), float32(glyph.Width() * 4), float32(glyph.Height() * 4)}, }) x += int(glyph.Width()) @@ -353,7 +354,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme lineIndex := 0 - var lw int32 + var lw float32 for _, ch := range toRender { if ch == nil { @@ -362,9 +363,9 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme } if horizontalAlignment == AlignCenter { - lw = int32((finalW - result.LineSizes[lineIndex].X) / 2) + lw = float32((finalW - result.LineSizes[lineIndex].X) / 2) } else if horizontalAlignment == AlignRight { - lw = int32(finalW - result.LineSizes[lineIndex].X) + lw = float32(finalW - result.LineSizes[lineIndex].X) } ch.Rect.X += lw @@ -375,7 +376,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme // Now render - renderTexture.Recreate(int32(result.TextSize.X), int32(result.TextSize.Y)) + renderTexture.Recreate(int32(result.TextSize.X) * int32(scaleup), int32(result.TextSize.Y) * int32(scaleup)) renderTexture.Texture.SetBlendMode(sdl.BLENDMODE_BLEND) @@ -392,7 +393,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme if r == nil { continue } - globals.Renderer.Copy(r.Glyph.Texture(), nil, r.Rect) + globals.Renderer.CopyF(r.Glyph.Texture(), nil, r.Rect) } } From d17158bf634cf694910ac5ab1b6a3a38a8d8c3c1 Mon Sep 17 00:00:00 2001 From: John Tringham Date: Tue, 5 Mar 2024 15:02:02 +0000 Subject: [PATCH 2/2] Moving supersampling rate over to globals --- common.go | 2 +- globals.go | 1 + gui.go | 2 +- main.go | 1 + textrenderer.go | 4 ++-- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/common.go b/common.go index 3fc2e62..e13a944 100644 --- a/common.go +++ b/common.go @@ -360,7 +360,7 @@ func HandleFontReload() { // For silver.ttf, 21 is the ideal font size. Otherwise, 30 seems to be reasonable. // loadedFont, err := ttf.OpenFont(fontPath, int(globals.Settings.Get(SettingsFontSize).AsFloat())) - loadedFont, err := ttf.OpenFont(fontPath, 96) + loadedFont, err := ttf.OpenFont(fontPath, 128) if err != nil { panic(err) diff --git a/globals.go b/globals.go index c50a83e..cc58c1d 100644 --- a/globals.go +++ b/globals.go @@ -51,6 +51,7 @@ type Globals struct { DeltaTime float32 Frame int64 GridSize float32 + TextSupersampling int ScreenSize Point ScreenSizePrev Point ScreenSizeChanged bool diff --git a/gui.go b/gui.go index 75bc6a7..4b2d1fc 100644 --- a/gui.go +++ b/gui.go @@ -2145,7 +2145,7 @@ func (label *Label) Draw() { src := &sdl.Rect{0, 0, w, h} - scaleup := float32(4) + scaleup := float32(globals.TextSupersampling); // Floor the rectangle to avoid aliasing artifacts when rendering with nearest neighbour newRect := &sdl.FRect{float32(math.Floor(float64(label.Rect.X + label.Offset.X))), float32(math.Floor(float64(label.Rect.Y + label.Offset.Y))), float32(w) / scaleup, float32(h) / scaleup} diff --git a/main.go b/main.go index 66d569c..7fd9d4e 100644 --- a/main.go +++ b/main.go @@ -58,6 +58,7 @@ func init() { globals.Mouse.Dummy = &nm globals.Resources = NewResourceBank() globals.GridSize = 32 + globals.TextSupersampling = 8 globals.InputText = []rune{} globals.CopyBuffer = NewCopyBuffer() globals.State = StateNeutral diff --git a/textrenderer.go b/textrenderer.go index 4c89240..c3eaaf9 100644 --- a/textrenderer.go +++ b/textrenderer.go @@ -251,7 +251,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme sdl.SetHint(sdl.HINT_RENDER_SCALE_QUALITY, "2") finalW := globals.GridSize - scaleup := 4 + scaleup := globals.TextSupersampling; x := 0 y := 0 @@ -327,7 +327,7 @@ func (tr *TextRenderer) RenderText(text string, maxSize Point, horizontalAlignme toRender = append(toRender, &renderPair{ Glyph: glyph, - Rect: &sdl.FRect{float32(x * scaleup), float32(y * scaleup), float32(glyph.Width() * 4), float32(glyph.Height() * 4)}, + Rect: &sdl.FRect{float32(x * scaleup), float32(y * scaleup), float32(glyph.Width() * int32(scaleup)), float32(glyph.Height() * int32(scaleup))}, }) x += int(glyph.Width())