switch to using gopackagebase

main
Sean Hickey 2022-11-09 18:04:36 -08:00
parent 4882cdedf5
commit d86124b735
17 changed files with 70 additions and 215 deletions

View File

@ -29,6 +29,7 @@ release_static: prebuild
CGO_LDFLAGS="-Wl,-rpath -L${HOME}/pkg/lib -L/usr/lib -L/usr/lib/x86_64-linux-gnu" \
${GO} ${GO_BUILD_RELEASE_STATIC}
lint: linter
linter:
golangci-lint run

View File

@ -21,7 +21,7 @@ You can use the Git Bash terminal to do this too. Open up `Git Bash`. This is a
```sh
mkdir projects
cd projects
git clone ssh://git@gitea.wisellama.rocks:222/Project-Ely/project-ely.git
git clone ssh://git@git.wisellama.rocks:222/Project-Ely/project-ely.git
```
## Install Go

View File

@ -1,6 +0,0 @@
game.title = "Project Ely"
game.framerate = 60.0
log.utcTime = false
log.file = "output.log"
log.writeToFile = false

5
go.mod
View File

@ -1,8 +1,9 @@
module gitea.wisellama.rocks/Project-Ely/project-ely
module git.wisellama.rocks/Project-Ely/project-ely
go 1.18
require (
gitea.wisellama.rocks/Wisellama/gosimpleconf v0.0.4
git.wisellama.rocks/Wisellama/gopackagebase v0.0.4
git.wisellama.rocks/Wisellama/gosimpleconf v0.1.0
github.com/veandco/go-sdl2 v0.4.25
)

6
go.sum
View File

@ -1,4 +1,6 @@
gitea.wisellama.rocks/Wisellama/gosimpleconf v0.0.4 h1:xFjG/dGPUoh1L7/PG9xQRr0GQZwlh1EGI9RnG8Ja8R8=
gitea.wisellama.rocks/Wisellama/gosimpleconf v0.0.4/go.mod h1:kY9gQL8laVTe+tW0ue5bYb6QThw78d7mx6AHwQ5CIzc=
git.wisellama.rocks/Wisellama/gopackagebase v0.0.4 h1:EUj/GqcSLJVm4aedSFaleudXlJNwJqRuSrTE0LhcA1M=
git.wisellama.rocks/Wisellama/gopackagebase v0.0.4/go.mod h1:0xUyJkT61TTIpekmeApB8U0mVwNqX/6Iz85RKUZQYyU=
git.wisellama.rocks/Wisellama/gosimpleconf v0.1.0 h1:Z2FAzARct8ShV4NSueC/y+PyuSQVcyo4WnW7GoZ9L10=
git.wisellama.rocks/Wisellama/gosimpleconf v0.1.0/go.mod h1:Gg1vUTBRZD7qcXvdF8L50PsnL9coLt/XbWa5BwSDN/M=
github.com/veandco/go-sdl2 v0.4.25 h1:J5ac3KKOccp/0xGJA1PaNYKPUcZm19IxhDGs8lJofPI=
github.com/veandco/go-sdl2 v0.4.25/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=

View File

@ -1,36 +0,0 @@
package config
import (
"os"
"gitea.wisellama.rocks/Wisellama/gosimpleconf"
)
var defaultConfig gosimpleconf.ConfigMap = gosimpleconf.ConfigMap{
"game.title": "Project Ely",
"game.framerate": "60.0",
"log.utcTime": "false",
"log.writeToFile": "false",
}
func Configure(filename string) (gosimpleconf.ConfigMap, error) {
var err error
_, err = os.Stat(filename)
if os.IsNotExist(err) {
// Config file does not exist, return a default configMap
return defaultConfig, nil
} else if err != nil {
return nil, err
}
configMap, err := gosimpleconf.Load(filename)
if err != nil {
return nil, err
}
flagMap := gosimpleconf.SetupFlagOverrides(configMap)
configMap = gosimpleconf.ParseFlags(configMap, flagMap)
return configMap, nil
}

View File

@ -1,49 +0,0 @@
package config
import (
"fmt"
"log"
"os"
"time"
)
type logWriter struct {
writeToFile bool
utcTime bool
logFile *os.File
}
func (w *logWriter) Write(bytes []byte) (int, error) {
t := time.Now()
if w.utcTime {
t = t.UTC()
}
format := t.Format(time.RFC3339)
return fmt.Fprintf(w.logFile, "%v %v", format, string(bytes))
}
func (w *logWriter) Cleanup() {
defer w.logFile.Close()
}
func SetupLogging(writeToFile bool, utcTime bool, logFilename string) (*logWriter, error) {
var err error
log.SetFlags(0)
logFile := os.Stdout
if writeToFile {
logFile, err = os.OpenFile(logFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
}
writer := &logWriter{
writeToFile: writeToFile,
utcTime: utcTime,
logFile: logFile,
}
log.SetOutput(writer)
return writer, nil
}

View File

@ -1,24 +0,0 @@
package config
import (
crypto_rand "crypto/rand"
"encoding/binary"
"fmt"
math_rand "math/rand"
)
// InitRNG will grab some cryptographically secure bytes to use as the
// seed for the non-crypto random number generator. Suggested on
// StackOverflow to avoid time-based seeds:
// https://stackoverflow.com/a/54491783
func InitRNG() error {
var b [8]byte
_, err := crypto_rand.Read(b[:])
if err != nil {
err = fmt.Errorf("error seeding random number generator: %w", err)
return err
}
math_rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
return nil
}

View File

@ -1,7 +1,7 @@
package animation
import (
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/sprite"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/sprite"
"github.com/veandco/go-sdl2/sdl"
)

View File

@ -6,7 +6,7 @@ import (
"sync"
"time"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/channels"
"git.wisellama.rocks/Project-Ely/project-ely/internal/channels"
)
type Entity interface {

View File

@ -1,7 +1,7 @@
package types
import (
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"git.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"github.com/veandco/go-sdl2/sdl"
)

View File

@ -5,8 +5,8 @@ import (
"math"
"sync"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/animation"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/animation"
"git.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"github.com/veandco/go-sdl2/sdl"
)

View File

@ -8,14 +8,14 @@ import (
"strconv"
"sync"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/animation"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/command"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/types"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/player"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/sprite"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/window"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"gitea.wisellama.rocks/Wisellama/gosimpleconf"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/animation"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/command"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/types"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/player"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/sprite"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/window"
"git.wisellama.rocks/Project-Ely/project-ely/internal/vector"
"git.wisellama.rocks/Wisellama/gosimpleconf"
"github.com/veandco/go-sdl2/sdl"
)

View File

@ -4,7 +4,7 @@ import (
"context"
"time"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/command"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game/entity/command"
"github.com/veandco/go-sdl2/sdl"
)

View File

@ -0,0 +1,17 @@
package sprite
const (
PENGUIN = "assets/images/penguin.png"
PERCYWALKING = "assets/images/percywalking.png"
PLATFORMER_FOREST_CHARACTERS = "assets/images/a-platformer-in-the-forest/characters.png"
DELILAHWALKING = "assets/images/Delilah_walking.png"
KINGWALKING = "assets/images/King_walking.png"
)
var SPRITE_FILE_LIST []string = []string{
PENGUIN,
PERCYWALKING,
PLATFORMER_FOREST_CHARACTERS,
DELILAHWALKING,
KINGWALKING,
}

View File

@ -6,22 +6,6 @@ import (
"github.com/veandco/go-sdl2/sdl"
)
const (
PENGUIN = "assets/images/penguin.png"
PERCYWALKING = "assets/images/percywalking.png"
PLATFORMER_FOREST_CHARACTERS = "assets/images/a-platformer-in-the-forest/characters.png"
DELILAHWALKING = "assets/images/Delilah_walking.png"
KINGWALKING = "assets/images/King_walking.png"
)
var fileList []string = []string{
PENGUIN,
PERCYWALKING,
PLATFORMER_FOREST_CHARACTERS,
DELILAHWALKING,
KINGWALKING,
}
var (
spriteCache map[string]*spritesheet
defaultSprite *spritesheet
@ -30,7 +14,7 @@ var (
func InitSpriteCache(renderer *sdl.Renderer) error {
spriteCache = make(map[string]*spritesheet)
for _, filename := range fileList {
for _, filename := range SPRITE_FILE_LIST {
s, err := NewSprite(renderer, filename)
if err != nil {
log.Printf("error creating sprite %v, using DefaultSprite: %v", filename, err)

93
main.go
View File

@ -1,44 +1,44 @@
package main
import (
"context"
"log"
"os"
"os/signal"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/config"
"gitea.wisellama.rocks/Project-Ely/project-ely/internal/game"
"gitea.wisellama.rocks/Wisellama/gosimpleconf"
"git.wisellama.rocks/Project-Ely/project-ely/internal/game"
"git.wisellama.rocks/Wisellama/gopackagebase"
"git.wisellama.rocks/Wisellama/gosimpleconf"
"github.com/veandco/go-sdl2/sdl"
)
const (
CONFIG_FILE = "project-ely.conf"
)
var defaultConfig gosimpleconf.ConfigMap = gosimpleconf.ConfigMap{
"game.title": "Project Ely",
"game.framerate": "60.0",
"log.utcTime": "false",
"log.writeToFile": "false",
}
func main() {
// Setup some initial context for gracefully killing
// the program with system interrupt signals like ctrl+c
// https://pace.dev/blog/2020/02/17/repond-to-ctrl-c-interrupt-signals-gracefully-with-context-in-golang-by-mat-ryer.html
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
defer func() {
signal.Stop(signalChan)
cancel()
}()
go func() {
select {
case <-signalChan:
// Graceful exit on ctrl+c
log.Printf("Attempting to exit gracefully...\n")
cancel()
case <-ctx.Done():
}
<-signalChan // Hard exit on second ctrl+c
log.Printf("Hard kill\n")
os.Exit(2)
}()
var err error
baseConfig, err := gopackagebase.Initialize(CONFIG_FILE, defaultConfig)
if err != nil {
log.Fatalf("error initializing: %v", err)
}
if baseConfig == nil {
log.Fatalf("baseConfig was nil")
}
defer baseConfig.Cancel()
// Run the program
err := run(ctx)
// Start everything with the SDL goroutine context
log.Printf("=== Starting %v ===", baseConfig.ConfigMap["game.title"])
sdl.Main(func() {
err = game.Run(baseConfig.Ctx, baseConfig.ConfigMap)
})
if err != nil {
log.Printf("%v\n", err)
os.Exit(1)
@ -46,38 +46,3 @@ func main() {
log.Printf("Exited gracefully!\n")
}
func run(ctx context.Context) error {
var err error
// Read configuration
configMap, err := config.Configure("game.conf")
if err != nil {
log.Fatalf("error during configure: %v\n", err)
}
// Setup logging
writeToFile := gosimpleconf.Bool(configMap["log.writeToFile"])
utcTime := gosimpleconf.Bool(configMap["log.utcTime"])
logFilename := configMap["log.file"]
logWriter, err := config.SetupLogging(writeToFile, utcTime, logFilename)
if err != nil {
log.Fatalf("error setting up logging: %v\n", err)
}
defer logWriter.Cleanup()
// Setup Random Number Generator seed
config.InitRNG()
// Start everything with the SDL goroutine context
log.Printf("=== Starting %v ===", configMap["game.title"])
sdl.Main(func() {
err = game.Run(ctx, configMap)
})
if err != nil {
return err
}
return nil
}