playground header, font scaling

This commit is contained in:
2023-07-08 10:59:46 +02:00
parent 09959a8932
commit 3947c34b4c
9 changed files with 78 additions and 32 deletions

View File

@@ -5,12 +5,30 @@ import (
"image/color"
)
const (
DefaultFontName konstructor.FontName = "ArcadeClassic"
)
const (
DefaultDPI = 72
)
func GetDefaultFontLayout() konstructor.FontLayout {
return konstructor.FontLayout{
Path: "assets/fonts/ArcadeClassic.ttf",
DPI: 72,
Path: konstructor.GetFontPath(DefaultFontName),
DPI: DefaultDPI,
Size: 24,
Color: color.White,
SelectedColor: color.RGBA{R: 255, G: 0, B: 0, A: 100},
}
}
func GetHeaderFontLayout() konstructor.FontLayout {
return konstructor.FontLayout{
Path: konstructor.GetFontPath(DefaultFontName),
DPI: DefaultDPI,
Size: 12,
Color: color.White,
SelectedColor: color.RGBA{R: 255, G: 0, B: 0, A: 100},
}
}

View File

@@ -1,5 +1,7 @@
package konstructor
import "image/color"
type KeyMap struct {
Up any
Down any
@@ -16,9 +18,15 @@ type ScreenConfig struct {
Height int
Scale int
}
type HeaderConfig struct {
BackgroundColor color.Color
Height int
FontLayout FontLayout
}
type Config struct {
Name string
Screen *ScreenConfig
Header *HeaderConfig
KeyMap KeyMap
}

View File

@@ -25,7 +25,7 @@ func (e *Engine) DialogUpdate() {
func (e *Engine) DialogDraw(screen *ebiten.Image) {
dialog := e.Domain.GetDialog(e.KContext.ActiveDialog)
face := dialog.Layout.ChoiceFont.GetFontFace()
face := dialog.Layout.ChoiceFont.GetFontFace(e.Config.Screen.Scale)
for i, choice := range dialog.Choices {
offset := int(dialog.Layout.ChoiceFont.Size) * (i + 1)

View File

@@ -25,7 +25,7 @@ func (e *Engine) MenuUpdate() {
func (e *Engine) MenuDraw(screen *ebiten.Image) {
menu := e.Domain.GetMenu(e.KContext.ActiveMenu)
face := menu.Layout.MenuItemFont.GetFontFace()
face := menu.Layout.MenuItemFont.GetFontFace(e.Config.Screen.Scale)
for i, menu_item := range menu.MenuItems {
color := menu.GetMenuItemColor(i)

View File

@@ -16,11 +16,11 @@ func (e *Engine) PlaygroundUpdate() {
func (e *Engine) PlaygroundDraw(screen *ebiten.Image) {
e.PlaygroundBackgroundDraw(screen)
e.PlaygroundHeaderDraw(screen)
e.PlaygroundPlatformsDraw(screen)
e.PlaygroundItemsDraw(screen)
e.PlaygroundNPCsDraw(screen)
e.PlaygroundDefaultPlayerDraw(screen)
e.PlaygroundHeaderDraw(screen)
}
func (e *Engine) PlaygroundBackgroundDraw(screen *ebiten.Image) {
@@ -37,7 +37,5 @@ func (e *Engine) PlaygroundAssetDraw(screen *ebiten.Image, render konstructor.Re
geoM := ebiten.GeoM{}
geoM.Translate(float64(position.X), float64(position.Y))
geoM.Scale(float64(e.Config.Screen.Scale), float64(e.Config.Screen.Scale))
screen.DrawImage(e.GetImage(render), &ebiten.DrawImageOptions{
GeoM: geoM,
})
screen.DrawImage(e.GetImage(render), &ebiten.DrawImageOptions{GeoM: geoM})
}

View File

@@ -1,6 +1,7 @@
package easy_ebitengine
import (
"fmt"
"game/konstructor"
"image/color"
@@ -9,27 +10,28 @@ import (
)
func (e *Engine) PlaygroundHeaderDraw(screen *ebiten.Image) {
header_bg, _ := ebiten.NewImage(e.Config.Screen.Width, 20, ebiten.FilterDefault)
header_bg.Fill(color.RGBA{
R: 0,
G: 255,
B: 0,
A: 200,
})
fl := konstructor.FontLayout{
Path: "assets/fonts/ArcadeClassic.ttf",
DPI: 72,
Size: 24,
}
face := fl.GetFontFace()
e.PlaygroundHeaderBackgroundDraw(screen)
e.PlaygroundHeaderTextDraw(screen)
}
func (e *Engine) PlaygroundHeaderBackgroundDraw(screen *ebiten.Image) {
background, _ := ebiten.NewImage(e.Config.Screen.Width, e.Config.Header.Height, ebiten.FilterDefault)
background.Fill(e.Config.Header.BackgroundColor)
geoM := ebiten.GeoM{}
geoM.Scale(float64(e.Config.Screen.Scale), float64(e.Config.Screen.Scale))
screen.DrawImage(header_bg, &ebiten.DrawImageOptions{
GeoM: geoM,
})
text.Draw(screen, e.Config.Name, face, 10, 20, color.Black)
screen.DrawImage(background, &ebiten.DrawImageOptions{GeoM: geoM})
}
func (e *Engine) PlaygroundHeaderTextDraw(screen *ebiten.Image) {
face := e.Config.Header.FontLayout.GetFontFace(e.Config.Screen.Scale)
header_text := fmt.Sprint(e.Config.Name, " LIVES ", e.KContext.LiveCount, " LEVEL ", e.KContext.CurrentLevel)
offset := getTopOffset(e.Config)
text.Draw(screen, header_text, face, 10, offset, color.Black)
}
func getTopOffset(config *konstructor.Config) int {
size := int(config.Header.FontLayout.Size) * config.Screen.Scale
height := config.Header.Height * config.Screen.Scale
return (height-size)/2 + size
}

View File

@@ -7,6 +7,7 @@ type KContext struct {
CurrentLevel int
CurrentPlayground int
Multiplayer bool
LiveCount int
}
func (c *KContext) ScreenTypeIs(name ScreenType) bool {

View File

@@ -8,6 +8,8 @@ import (
"golang.org/x/image/font/opentype"
)
type FontName string
type FontLayout struct {
Path string
DPI float64
@@ -17,17 +19,21 @@ type FontLayout struct {
cachedFontFace font.Face
}
func (fl *FontLayout) GetFontFace() font.Face {
func (fl *FontLayout) GetFontFace(scale int) font.Face {
if fl.cachedFontFace != nil {
return fl.cachedFontFace
}
file, _ := ioutil.ReadFile(fl.Path)
true_type, _ := opentype.Parse(file)
font_face, _ := opentype.NewFace(true_type, &opentype.FaceOptions{
Size: fl.DPI,
DPI: fl.Size,
Size: fl.Size * float64(scale),
DPI: fl.DPI,
Hinting: font.HintingVertical,
})
fl.cachedFontFace = font_face
return font_face
}
func GetFontPath(name FontName) string {
return "assets/fonts/" + string(name) + ".ttf"
}

15
main.go
View File

@@ -4,6 +4,7 @@ import (
"game/domain"
"game/konstructor"
"game/konstructor/engines/easy_ebitengine"
"image/color"
"github.com/hajimehoshi/ebiten"
)
@@ -18,14 +19,26 @@ func main() {
ActiveDialog: domain.DialogTest,
CurrentLevel: 0,
CurrentPlayground: 0,
Multiplayer: false,
LiveCount: 3,
},
Config: &konstructor.Config{
Name: "Game",
Name: "Teletype Adventure",
Screen: &konstructor.ScreenConfig{
Width: 640,
Height: 480,
Scale: 2,
},
Header: &konstructor.HeaderConfig{
BackgroundColor: color.RGBA{
R: 0,
G: 255,
B: 0,
A: 200,
},
Height: 20,
FontLayout: domain.GetHeaderFontLayout(),
},
KeyMap: konstructor.KeyMap{
Up: ebiten.KeyUp,
Down: ebiten.KeyDown,