Skip to content

Commit

Permalink
stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
temidaradev committed Nov 13, 2024
1 parent 3a13b3e commit d4b402a
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 31 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/temidaradev/esset.svg)](https://pkg.go.dev/github.com/temidaradev/esset)

# Esset

Esset is a basic asset implementer for ebitengine.

## Usage
# Usage

First `go get github.com/temidaradev/esset` and create an assets folder if you don't have. After creating folder put .png assets into that folder and create `assets.go`. After this add

Expand All @@ -12,4 +13,31 @@ First `go get github.com/temidaradev/esset` and create an assets folder if you d
var assets embed.FS
```

this embed statement after import part. Now you can use esset as you asset implementer, It wants 2 parameters from you firstly embed statement and then your `"asset.png"`. Here is an example: `var Idle = esset.GetSingleAsset(assets, "path/to/your/asset.png")`
this embed statement after import part. Now you can use esset as you asset implementer. It wants 2 parameters from you firstly embed statement and then your `"asset.png"`.

## GetAsset

Here is an example: `var Idle = esset.GetAsset(assets, "path/to/your/asset.png")`

## GetMultipleAssets

Important thing is create a folder and put every single tile item (.png) like this ![Resource](./resources/image.png) and then you can use that function easily like this: `var Tile = esset.GetMultipleAssets(assets, "path/to/your/*.png")` Because of you are selecting more than 1 image our `*ebiten.Image` is a slice you can select by index like this: `TileComponent := assets.Tile[0]` or if you need to get random asset from that folder you can do like this: `TileRandom := assets.Tile[rand.Intn(len(assets.Tile))]`

## UseFont

For fonts you have to embed fonts seperataly like this:

```
//go:embed font/OpenSans-Medium.ttf
var MyFont []byte
```

After that you should create a `&text.DrawOptions{}` in your `Draw()` func like this:

```
opF := &text.DrawOptions{}
opF.GeoM.Translate(x, y)
opF.ColorScale.ScaleWithColor(color.White)
```

After that you can use `esset.UseFont` func like this: `esset.UseFont(screen, assets.MyFont, "wassup", 24, opF)`
30 changes: 13 additions & 17 deletions esset.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package esset

import (
"bytes"
"embed"
"image"
_ "image/png"
"io/fs"
"log"

"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)

func GetAsset(efs embed.FS, path string) *ebiten.Image {
Expand Down Expand Up @@ -40,25 +40,21 @@ func GetMultiAssets(efs embed.FS, path string) []*ebiten.Image {
return images
}

func GetFont(efs embed.FS, name string) font.Face {
f, err := efs.ReadFile(name)
if err != nil {
panic(err)
}
var (
fontFaceSource *text.GoTextFaceSource
)

tt, err := opentype.Parse(f)
func UseFont(screen *ebiten.Image, data []byte, str string, fontSize int, op *text.DrawOptions) {
s, err := text.NewGoTextFaceSource(bytes.NewReader(data))
if err != nil {
panic(err)
log.Fatal(err)
}
fontFaceSource = s

face, err := opentype.NewFace(tt, &opentype.FaceOptions{
Size: 48,
DPI: 72,
Hinting: font.HintingVertical,
})
if err != nil {
panic(err)
ffs := &text.GoTextFace{
Source: s,
Size: float64(fontSize),
}

return face
text.Draw(screen, str, ffs, op)
}
5 changes: 5 additions & 0 deletions example/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
//go:embed *
var assets embed.FS

//go:embed font/OpenSans-Medium.ttf
var MyFont []byte

var Idle = esset.GetAsset(assets, "mainchar.png")
var Left = esset.GetAsset(assets, "left.png")
var Right = esset.GetAsset(assets, "right.png")

var GopherTile = esset.GetMultiAssets(assets, "gopherTile/*.png")
Binary file added example/assets/font/OpenSans-Medium.ttf
Binary file not shown.
Binary file added example/assets/gopherTile/leftTile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/assets/gopherTile/maincharTile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/assets/gopherTile/rightTile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 29 additions & 8 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/temidaradev/esset"
"github.com/temidaradev/esset/example/assets"
)

Expand Down Expand Up @@ -94,6 +96,33 @@ var (

func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(LightBlue)

opF := &text.DrawOptions{}
opF.GeoM.Translate(245, 75)
opF.ColorScale.ScaleWithColor(color.White)
esset.UseFont(screen, assets.MyFont, "ESSET", 48, opF)

op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(0.3, 0.3)
op.GeoM.Translate(0, 0)

gTileLeft := assets.GopherTile[0]
screen.DrawImage(gTileLeft, op)

op1 := &ebiten.DrawImageOptions{}
op1.GeoM.Scale(0.3, 0.3)
op1.GeoM.Translate(50, 0)

gTileMain := assets.GopherTile[1]
screen.DrawImage(gTileMain, op1)

op2 := &ebiten.DrawImageOptions{}
op2.GeoM.Scale(0.3, 0.3)
op2.GeoM.Translate(100, 0)

gTileRight := assets.GopherTile[2]
screen.DrawImage(gTileRight, op2)

g.player.Draw(screen, *g)

}
Expand All @@ -107,14 +136,6 @@ func (g *Game) Update() error {
ebiten.SetVsyncEnabled(!vsync)
}

if inpututil.IsKeyJustPressed(ebiten.KeyW) {
g.player.player.x = 0
}

if inpututil.IsKeyJustPressed(ebiten.KeyW) {
g.player.player.x = 0
}

return nil
}

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ module github.com/temidaradev/esset

go 1.23.2

require (
github.com/hajimehoshi/ebiten/v2 v2.8.4
golang.org/x/image v0.20.0
)
require github.com/hajimehoshi/ebiten/v2 v2.8.4

require (
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325 // indirect
github.com/ebitengine/hideconsole v1.0.0 // indirect
github.com/ebitengine/purego v0.8.0 // indirect
github.com/go-text/typesetting v0.2.0 // indirect
github.com/jezek/xgb v1.1.1 // indirect
golang.org/x/image v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE=
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/go-text/typesetting v0.2.0 h1:fbzsgbmk04KiWtE+c3ZD4W2nmCRzBqrqQOvYlwAOdho=
github.com/go-text/typesetting v0.2.0/go.mod h1:2+owI/sxa73XA581LAzVuEBZ3WEEV2pXeDswCH/3i1I=
github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66 h1:GUrm65PQPlhFSKjLPGOZNPNxLCybjzjYBzjfoBGaDUY=
github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
github.com/hajimehoshi/bitmapfont/v3 v3.2.0 h1:0DISQM/rseKIJhdF29AkhvdzIULqNIIlXAGWit4ez1Q=
github.com/hajimehoshi/bitmapfont/v3 v3.2.0/go.mod h1:8gLqGatKVu0pwcNCJguW3Igg9WQqVXF0zg/RvrGQWyg=
github.com/hajimehoshi/ebiten/v2 v2.8.4 h1:BzXkcyYX046SRZFkzF2KaCaHiBjwCaufUPCAOK59JSw=
github.com/hajimehoshi/ebiten/v2 v2.8.4/go.mod h1:SXx/whkvpfsavGo6lvZykprerakl+8Uo1X8d2U5aAnA=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
Expand Down
Binary file added resources/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d4b402a

Please sign in to comment.