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" "image/color"
) )
const (
DefaultFontName konstructor.FontName = "ArcadeClassic"
)
const (
DefaultDPI = 72
)
func GetDefaultFontLayout() konstructor.FontLayout { func GetDefaultFontLayout() konstructor.FontLayout {
return konstructor.FontLayout{ return konstructor.FontLayout{
Path: "assets/fonts/ArcadeClassic.ttf", Path: konstructor.GetFontPath(DefaultFontName),
DPI: 72, DPI: DefaultDPI,
Size: 24, Size: 24,
Color: color.White, Color: color.White,
SelectedColor: color.RGBA{R: 255, G: 0, B: 0, A: 100}, 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 package konstructor
import "image/color"
type KeyMap struct { type KeyMap struct {
Up any Up any
Down any Down any
@@ -16,9 +18,15 @@ type ScreenConfig struct {
Height int Height int
Scale int Scale int
} }
type HeaderConfig struct {
BackgroundColor color.Color
Height int
FontLayout FontLayout
}
type Config struct { type Config struct {
Name string Name string
Screen *ScreenConfig Screen *ScreenConfig
Header *HeaderConfig
KeyMap KeyMap KeyMap KeyMap
} }

View File

@@ -25,7 +25,7 @@ func (e *Engine) DialogUpdate() {
func (e *Engine) DialogDraw(screen *ebiten.Image) { func (e *Engine) DialogDraw(screen *ebiten.Image) {
dialog := e.Domain.GetDialog(e.KContext.ActiveDialog) 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 { for i, choice := range dialog.Choices {
offset := int(dialog.Layout.ChoiceFont.Size) * (i + 1) 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) { func (e *Engine) MenuDraw(screen *ebiten.Image) {
menu := e.Domain.GetMenu(e.KContext.ActiveMenu) 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 { for i, menu_item := range menu.MenuItems {
color := menu.GetMenuItemColor(i) color := menu.GetMenuItemColor(i)

View File

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

View File

@@ -1,6 +1,7 @@
package easy_ebitengine package easy_ebitengine
import ( import (
"fmt"
"game/konstructor" "game/konstructor"
"image/color" "image/color"
@@ -9,27 +10,28 @@ import (
) )
func (e *Engine) PlaygroundHeaderDraw(screen *ebiten.Image) { func (e *Engine) PlaygroundHeaderDraw(screen *ebiten.Image) {
header_bg, _ := ebiten.NewImage(e.Config.Screen.Width, 20, ebiten.FilterDefault) e.PlaygroundHeaderBackgroundDraw(screen)
header_bg.Fill(color.RGBA{ e.PlaygroundHeaderTextDraw(screen)
R: 0,
G: 255,
B: 0,
A: 200,
})
fl := konstructor.FontLayout{
Path: "assets/fonts/ArcadeClassic.ttf",
DPI: 72,
Size: 24,
} }
face := fl.GetFontFace() 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 := ebiten.GeoM{}
geoM.Scale(float64(e.Config.Screen.Scale), float64(e.Config.Screen.Scale)) geoM.Scale(float64(e.Config.Screen.Scale), float64(e.Config.Screen.Scale))
screen.DrawImage(header_bg, &ebiten.DrawImageOptions{ screen.DrawImage(background, &ebiten.DrawImageOptions{GeoM: geoM})
GeoM: geoM, }
})
text.Draw(screen, e.Config.Name, face, 10, 20, color.Black) 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 CurrentLevel int
CurrentPlayground int CurrentPlayground int
Multiplayer bool Multiplayer bool
LiveCount int
} }
func (c *KContext) ScreenTypeIs(name ScreenType) bool { func (c *KContext) ScreenTypeIs(name ScreenType) bool {

View File

@@ -8,6 +8,8 @@ import (
"golang.org/x/image/font/opentype" "golang.org/x/image/font/opentype"
) )
type FontName string
type FontLayout struct { type FontLayout struct {
Path string Path string
DPI float64 DPI float64
@@ -17,17 +19,21 @@ type FontLayout struct {
cachedFontFace font.Face cachedFontFace font.Face
} }
func (fl *FontLayout) GetFontFace() font.Face { func (fl *FontLayout) GetFontFace(scale int) font.Face {
if fl.cachedFontFace != nil { if fl.cachedFontFace != nil {
return fl.cachedFontFace return fl.cachedFontFace
} }
file, _ := ioutil.ReadFile(fl.Path) file, _ := ioutil.ReadFile(fl.Path)
true_type, _ := opentype.Parse(file) true_type, _ := opentype.Parse(file)
font_face, _ := opentype.NewFace(true_type, &opentype.FaceOptions{ font_face, _ := opentype.NewFace(true_type, &opentype.FaceOptions{
Size: fl.DPI, Size: fl.Size * float64(scale),
DPI: fl.Size, DPI: fl.DPI,
Hinting: font.HintingVertical, Hinting: font.HintingVertical,
}) })
fl.cachedFontFace = font_face fl.cachedFontFace = font_face
return 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/domain"
"game/konstructor" "game/konstructor"
"game/konstructor/engines/easy_ebitengine" "game/konstructor/engines/easy_ebitengine"
"image/color"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
) )
@@ -18,14 +19,26 @@ func main() {
ActiveDialog: domain.DialogTest, ActiveDialog: domain.DialogTest,
CurrentLevel: 0, CurrentLevel: 0,
CurrentPlayground: 0, CurrentPlayground: 0,
Multiplayer: false,
LiveCount: 3,
}, },
Config: &konstructor.Config{ Config: &konstructor.Config{
Name: "Game", Name: "Teletype Adventure",
Screen: &konstructor.ScreenConfig{ Screen: &konstructor.ScreenConfig{
Width: 640, Width: 640,
Height: 480, Height: 480,
Scale: 2, Scale: 2,
}, },
Header: &konstructor.HeaderConfig{
BackgroundColor: color.RGBA{
R: 0,
G: 255,
B: 0,
A: 200,
},
Height: 20,
FontLayout: domain.GetHeaderFontLayout(),
},
KeyMap: konstructor.KeyMap{ KeyMap: konstructor.KeyMap{
Up: ebiten.KeyUp, Up: ebiten.KeyUp,
Down: ebiten.KeyDown, Down: ebiten.KeyDown,