1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-12-25 19:46:50 -05:00

Cleanup error handling (#303)

This commit is contained in:
Alex Yatskov 2020-02-09 11:12:04 -08:00 committed by GitHub
parent 9478e2c2be
commit 1983ec395d
18 changed files with 311 additions and 431 deletions

View File

@ -83,7 +83,7 @@ func createAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, tran
}
}
err, image := d2render.NewSurface(frameWidth, frameHeight, d2render.FilterNearest)
image, err := d2render.NewSurface(frameWidth, frameHeight, d2render.FilterNearest)
if err != nil {
return nil, err
}
@ -118,7 +118,7 @@ func createAnimationFromDC6(dc6 *d2dc6.DC6File) (*Animation, error) {
}
for frameIndex, dc6Frame := range dc6.Frames {
err, image := d2render.NewSurface(int(dc6Frame.Width), int(dc6Frame.Height), d2render.FilterNearest)
image, err := d2render.NewSurface(int(dc6Frame.Width), int(dc6Frame.Height), d2render.FilterNearest)
if err != nil {
return nil, err
}

View File

@ -10,8 +10,8 @@ import (
)
var (
ErrHasInit = errors.New("asset system is already initialized")
ErrNoInit = errors.New("asset system is not initialized")
ErrWasInit = errors.New("asset system is already initialized")
ErrNotInit = errors.New("asset system is not initialized")
)
type assetManager struct {
@ -22,10 +22,7 @@ type assetManager struct {
}
func loadPalette(palettePath string) (*d2datadict.PaletteRec, error) {
if singleton == nil {
return nil, ErrNoInit
}
verifyWasInit()
return singleton.paletteManager.loadPalette(palettePath)
}

View File

@ -13,16 +13,10 @@ import (
var singleton *assetManager
func Initialize() error {
if singleton != nil {
return ErrHasInit
}
config, err := d2config.Get()
if err != nil {
return err
}
verifyNotInit()
var (
config = d2config.Get()
archiveManager = createArchiveManager(config)
fileManager = createFileManager(config, archiveManager)
paletteManager = createPaletteManager()
@ -71,17 +65,12 @@ func Shutdown() {
}
func LoadArchive(archivePath string) (*d2mpq.MPQ, error) {
if singleton == nil {
return nil, ErrNoInit
}
verifyWasInit()
return singleton.archiveManager.loadArchive(archivePath)
}
func LoadFile(filePath string) ([]byte, error) {
if singleton == nil {
return nil, ErrNoInit
}
verifyWasInit()
data, err := singleton.fileManager.loadFile(filePath)
if err != nil {
@ -92,10 +81,7 @@ func LoadFile(filePath string) ([]byte, error) {
}
func FileExists(filePath string) (bool, error) {
if singleton == nil {
return false, ErrNoInit
}
verifyWasInit()
return singleton.fileManager.fileExists(filePath)
}
@ -104,13 +90,22 @@ func LoadAnimation(animationPath, palettePath string) (*Animation, error) {
}
func LoadAnimationWithTransparency(animationPath, palettePath string, transparency int) (*Animation, error) {
if singleton == nil {
return nil, ErrNoInit
}
verifyWasInit()
return singleton.animationManager.loadAnimation(animationPath, palettePath, transparency)
}
func LoadComposite(object *d2datadict.ObjectLookupRecord, palettePath string) (*Composite, error) {
return CreateComposite(object, palettePath), nil
}
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}

View File

@ -7,7 +7,7 @@ import (
var singleton AudioProvider
var (
ErrHasInit = errors.New("audio system is already initialized")
ErrWasInit = errors.New("audio system is already initialized")
ErrNotInit = errors.New("audio system has not been initialized")
)
@ -24,33 +24,36 @@ type AudioProvider interface {
// CreateManager creates a sound provider
func Initialize(audioProvider AudioProvider) error {
if singleton != nil {
return ErrHasInit
}
verifyNotInit()
singleton = audioProvider
return nil
}
// PlayBGM plays an infinitely looping background track
func PlayBGM(song string) error {
if singleton == nil {
return ErrNotInit
}
verifyWasInit()
singleton.PlayBGM(song)
return nil
}
func LoadSoundEffect(sfx string) (SoundEffect, error) {
if singleton == nil {
return nil, ErrNotInit
}
verifyWasInit()
return singleton.LoadSoundEffect(sfx)
}
func SetVolumes(bgmVolume, sfxVolume float64) error {
if singleton == nil {
return ErrNotInit
}
func SetVolumes(bgmVolume, sfxVolume float64) {
verifyWasInit()
singleton.SetVolumes(bgmVolume, sfxVolume)
return nil
}
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}

View File

@ -11,7 +11,7 @@ import (
var (
ErrNotInit = errors.New("configuration is not initialized")
ErrHasInit = errors.New("configuration has already been initialized")
ErrWasInit = errors.New("configuration has already been initialized")
)
// Configuration defines the configuration for the engine, loaded from config.json
@ -32,9 +32,8 @@ type Configuration struct {
var singleton *Configuration
func Initialize() error {
if singleton != nil {
return ErrHasInit
}
verifyNotInit()
configDir, err := os.UserConfigDir()
if err != nil {
singleton = getDefaultConfiguration()
@ -72,9 +71,8 @@ func Initialize() error {
}
func Save() error {
if singleton == nil {
return ErrNotInit
}
verifyWasInit()
configDir, err := os.UserConfigDir()
if err != nil {
return err
@ -101,9 +99,19 @@ func Save() error {
return nil
}
func Get() (*Configuration, error) {
if singleton == nil {
return nil, ErrNotInit
}
return singleton, nil
func Get() *Configuration {
verifyWasInit()
return singleton
}
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}

View File

@ -14,7 +14,7 @@ var (
var singleton *guiManager
func Initialize() error {
assertNotInit()
verifyNotInit()
var err error
if singleton, err = createGuiManager(); err != nil {
@ -24,52 +24,48 @@ func Initialize() error {
return nil
}
func Shutdown() {
singleton = nil
}
func Render(target d2render.Surface) error {
assertWasInit()
verifyWasInit()
return singleton.render(target)
}
func Advance(elapsed float64) error {
assertWasInit()
verifyWasInit()
return singleton.advance(elapsed)
}
func Clear() {
assertWasInit()
verifyWasInit()
singleton.clear()
}
func ShowLoadScreen(progress float64) {
assertWasInit()
verifyWasInit()
singleton.showLoadScreen(progress)
}
func HideLoadScreen() {
assertWasInit()
verifyWasInit()
singleton.hideLoadScreen()
}
func ShowCursor() {
assertWasInit()
verifyWasInit()
singleton.showCursor()
}
func HideCursor() {
assertWasInit()
verifyWasInit()
singleton.hideCursor()
}
func assertWasInit() {
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func assertNotInit() {
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}

View File

@ -7,10 +7,8 @@ import (
)
var (
ErrHasInit = errors.New("input system is already initialized")
ErrNotInit = errors.New("input system is not initialized")
ErrHasReg = errors.New("input system already has provided handler")
ErrNotReg = errors.New("input system does not have provided handler")
ErrHasReg = errors.New("input system already has provided handler")
ErrNotReg = errors.New("input system does not have provided handler")
)
type Priority int
@ -25,106 +23,106 @@ type Key int
//noinspection GoUnusedConst
const (
Key0 = Key(ebiten.Key0)
Key1 = Key(ebiten.Key1)
Key2 = Key(ebiten.Key2)
Key3 = Key(ebiten.Key3)
Key4 = Key(ebiten.Key4)
Key5 = Key(ebiten.Key5)
Key6 = Key(ebiten.Key6)
Key7 = Key(ebiten.Key7)
Key8 = Key(ebiten.Key8)
Key9 = Key(ebiten.Key9)
KeyA = Key(ebiten.KeyA)
KeyB = Key(ebiten.KeyB)
KeyC = Key(ebiten.KeyC)
KeyD = Key(ebiten.KeyD)
KeyE = Key(ebiten.KeyE)
KeyF = Key(ebiten.KeyF)
KeyG = Key(ebiten.KeyG)
KeyH = Key(ebiten.KeyH)
KeyI = Key(ebiten.KeyI)
KeyJ = Key(ebiten.KeyJ)
KeyK = Key(ebiten.KeyK)
KeyL = Key(ebiten.KeyL)
KeyM = Key(ebiten.KeyM)
KeyN = Key(ebiten.KeyN)
KeyO = Key(ebiten.KeyO)
KeyP = Key(ebiten.KeyP)
KeyQ = Key(ebiten.KeyQ)
KeyR = Key(ebiten.KeyR)
KeyS = Key(ebiten.KeyS)
KeyT = Key(ebiten.KeyT)
KeyU = Key(ebiten.KeyU)
KeyV = Key(ebiten.KeyV)
KeyW = Key(ebiten.KeyW)
KeyX = Key(ebiten.KeyX)
KeyY = Key(ebiten.KeyY)
KeyZ = Key(ebiten.KeyZ)
KeyApostrophe = Key(ebiten.KeyApostrophe)
KeyBackslash = Key(ebiten.KeyBackslash)
KeyBackspace = Key(ebiten.KeyBackspace)
KeyCapsLock = Key(ebiten.KeyCapsLock)
KeyComma = Key(ebiten.KeyComma)
KeyDelete = Key(ebiten.KeyDelete)
KeyDown = Key(ebiten.KeyDown)
KeyEnd = Key(ebiten.KeyEnd)
KeyEnter = Key(ebiten.KeyEnter)
KeyEqual = Key(ebiten.KeyEqual)
KeyEscape = Key(ebiten.KeyEscape)
KeyF1 = Key(ebiten.KeyF1)
KeyF2 = Key(ebiten.KeyF2)
KeyF3 = Key(ebiten.KeyF3)
KeyF4 = Key(ebiten.KeyF4)
KeyF5 = Key(ebiten.KeyF5)
KeyF6 = Key(ebiten.KeyF6)
KeyF7 = Key(ebiten.KeyF7)
KeyF8 = Key(ebiten.KeyF8)
KeyF9 = Key(ebiten.KeyF9)
KeyF10 = Key(ebiten.KeyF10)
KeyF11 = Key(ebiten.KeyF11)
KeyF12 = Key(ebiten.KeyF12)
KeyGraveAccent = Key(ebiten.KeyGraveAccent)
KeyHome = Key(ebiten.KeyHome)
KeyInsert = Key(ebiten.KeyInsert)
KeyKP0 = Key(ebiten.KeyKP0)
KeyKP1 = Key(ebiten.KeyKP1)
KeyKP2 = Key(ebiten.KeyKP2)
KeyKP3 = Key(ebiten.KeyKP3)
KeyKP4 = Key(ebiten.KeyKP4)
KeyKP5 = Key(ebiten.KeyKP5)
KeyKP6 = Key(ebiten.KeyKP6)
KeyKP7 = Key(ebiten.KeyKP7)
KeyKP8 = Key(ebiten.KeyKP8)
KeyKP9 = Key(ebiten.KeyKP9)
KeyKPAdd = Key(ebiten.KeyKPAdd)
KeyKPDecimal = Key(ebiten.KeyKPDecimal)
KeyKPDivide = Key(ebiten.KeyKPDivide)
KeyKPEnter = Key(ebiten.KeyKPEnter)
KeyKPEqual = Key(ebiten.KeyKPEqual)
KeyKPMultiply = Key(ebiten.KeyKPMultiply)
KeyKPSubtract = Key(ebiten.KeyKPSubtract)
KeyLeft = Key(ebiten.KeyLeft)
KeyLeftBracket = Key(ebiten.KeyLeftBracket)
KeyMenu = Key(ebiten.KeyMenu)
KeyMinus = Key(ebiten.KeyMinus)
KeyNumLock = Key(ebiten.KeyNumLock)
KeyPageDown = Key(ebiten.KeyPageDown)
KeyPageUp = Key(ebiten.KeyPageUp)
KeyPause = Key(ebiten.KeyPause)
KeyPeriod = Key(ebiten.KeyPeriod)
KeyPrintScreen = Key(ebiten.KeyPrintScreen)
KeyRight = Key(ebiten.KeyRight)
KeyRightBracket = Key(ebiten.KeyRightBracket)
KeyScrollLock = Key(ebiten.KeyScrollLock)
KeySemicolon = Key(ebiten.KeySemicolon)
KeySlash = Key(ebiten.KeySlash)
KeySpace = Key(ebiten.KeySpace)
KeyTab = Key(ebiten.KeyTab)
KeyUp = Key(ebiten.KeyUp)
KeyAlt = Key(ebiten.KeyAlt)
KeyControl = Key(ebiten.KeyControl)
KeyShift = Key(ebiten.KeyShift)
Key0 = Key(ebiten.Key0)
Key1 = Key(ebiten.Key1)
Key2 = Key(ebiten.Key2)
Key3 = Key(ebiten.Key3)
Key4 = Key(ebiten.Key4)
Key5 = Key(ebiten.Key5)
Key6 = Key(ebiten.Key6)
Key7 = Key(ebiten.Key7)
Key8 = Key(ebiten.Key8)
Key9 = Key(ebiten.Key9)
KeyA = Key(ebiten.KeyA)
KeyB = Key(ebiten.KeyB)
KeyC = Key(ebiten.KeyC)
KeyD = Key(ebiten.KeyD)
KeyE = Key(ebiten.KeyE)
KeyF = Key(ebiten.KeyF)
KeyG = Key(ebiten.KeyG)
KeyH = Key(ebiten.KeyH)
KeyI = Key(ebiten.KeyI)
KeyJ = Key(ebiten.KeyJ)
KeyK = Key(ebiten.KeyK)
KeyL = Key(ebiten.KeyL)
KeyM = Key(ebiten.KeyM)
KeyN = Key(ebiten.KeyN)
KeyO = Key(ebiten.KeyO)
KeyP = Key(ebiten.KeyP)
KeyQ = Key(ebiten.KeyQ)
KeyR = Key(ebiten.KeyR)
KeyS = Key(ebiten.KeyS)
KeyT = Key(ebiten.KeyT)
KeyU = Key(ebiten.KeyU)
KeyV = Key(ebiten.KeyV)
KeyW = Key(ebiten.KeyW)
KeyX = Key(ebiten.KeyX)
KeyY = Key(ebiten.KeyY)
KeyZ = Key(ebiten.KeyZ)
KeyApostrophe = Key(ebiten.KeyApostrophe)
KeyBackslash = Key(ebiten.KeyBackslash)
KeyBackspace = Key(ebiten.KeyBackspace)
KeyCapsLock = Key(ebiten.KeyCapsLock)
KeyComma = Key(ebiten.KeyComma)
KeyDelete = Key(ebiten.KeyDelete)
KeyDown = Key(ebiten.KeyDown)
KeyEnd = Key(ebiten.KeyEnd)
KeyEnter = Key(ebiten.KeyEnter)
KeyEqual = Key(ebiten.KeyEqual)
KeyEscape = Key(ebiten.KeyEscape)
KeyF1 = Key(ebiten.KeyF1)
KeyF2 = Key(ebiten.KeyF2)
KeyF3 = Key(ebiten.KeyF3)
KeyF4 = Key(ebiten.KeyF4)
KeyF5 = Key(ebiten.KeyF5)
KeyF6 = Key(ebiten.KeyF6)
KeyF7 = Key(ebiten.KeyF7)
KeyF8 = Key(ebiten.KeyF8)
KeyF9 = Key(ebiten.KeyF9)
KeyF10 = Key(ebiten.KeyF10)
KeyF11 = Key(ebiten.KeyF11)
KeyF12 = Key(ebiten.KeyF12)
KeyGraveAccent = Key(ebiten.KeyGraveAccent)
KeyHome = Key(ebiten.KeyHome)
KeyInsert = Key(ebiten.KeyInsert)
KeyKP0 = Key(ebiten.KeyKP0)
KeyKP1 = Key(ebiten.KeyKP1)
KeyKP2 = Key(ebiten.KeyKP2)
KeyKP3 = Key(ebiten.KeyKP3)
KeyKP4 = Key(ebiten.KeyKP4)
KeyKP5 = Key(ebiten.KeyKP5)
KeyKP6 = Key(ebiten.KeyKP6)
KeyKP7 = Key(ebiten.KeyKP7)
KeyKP8 = Key(ebiten.KeyKP8)
KeyKP9 = Key(ebiten.KeyKP9)
KeyKPAdd = Key(ebiten.KeyKPAdd)
KeyKPDecimal = Key(ebiten.KeyKPDecimal)
KeyKPDivide = Key(ebiten.KeyKPDivide)
KeyKPEnter = Key(ebiten.KeyKPEnter)
KeyKPEqual = Key(ebiten.KeyKPEqual)
KeyKPMultiply = Key(ebiten.KeyKPMultiply)
KeyKPSubtract = Key(ebiten.KeyKPSubtract)
KeyLeft = Key(ebiten.KeyLeft)
KeyLeftBracket = Key(ebiten.KeyLeftBracket)
KeyMenu = Key(ebiten.KeyMenu)
KeyMinus = Key(ebiten.KeyMinus)
KeyNumLock = Key(ebiten.KeyNumLock)
KeyPageDown = Key(ebiten.KeyPageDown)
KeyPageUp = Key(ebiten.KeyPageUp)
KeyPause = Key(ebiten.KeyPause)
KeyPeriod = Key(ebiten.KeyPeriod)
KeyPrintScreen = Key(ebiten.KeyPrintScreen)
KeyRight = Key(ebiten.KeyRight)
KeyRightBracket = Key(ebiten.KeyRightBracket)
KeyScrollLock = Key(ebiten.KeyScrollLock)
KeySemicolon = Key(ebiten.KeySemicolon)
KeySlash = Key(ebiten.KeySlash)
KeySpace = Key(ebiten.KeySpace)
KeyTab = Key(ebiten.KeyTab)
KeyUp = Key(ebiten.KeyUp)
KeyAlt = Key(ebiten.KeyAlt)
KeyControl = Key(ebiten.KeyControl)
KeyShift = Key(ebiten.KeyShift)
)
type KeyMod int
@ -207,34 +205,13 @@ type MouseMoveHandler interface {
OnMouseMove(event MouseMoveEvent) bool
}
var singleton *inputManager
func Initialize() error {
if singleton != nil {
return ErrHasInit
}
singleton = &inputManager{}
return nil
}
func Shutdown() {
singleton = nil
}
var singleton inputManager
func Advance(elapsed float64) error {
if singleton == nil {
return ErrNotInit
}
return singleton.advance(elapsed)
}
func BindHandlerWithPriority(handler Handler, priority Priority) error {
if singleton == nil {
return ErrNotInit
}
return singleton.bindHandler(handler, priority)
}
@ -243,9 +220,5 @@ func BindHandler(handler Handler) error {
}
func UnbindHandler(handler Handler) error {
if singleton == nil {
return ErrNotInit
}
return singleton.unbindHandler(handler)
}

View File

@ -154,7 +154,7 @@ func loadRegion(seed int64, tileOffsetX, tileOffsetY int, levelType d2enum.Regio
func (mr *MapRegion) generateWalkableMatrix() {
mr.walkableArea = make([][]PathTile, mr.tileRect.Height*5)
for y := 0; y < (mr.tileRect.Height - 1)*5; y++ {
for y := 0; y < (mr.tileRect.Height-1)*5; y++ {
mr.walkableArea[y] = make([]PathTile, mr.tileRect.Width*5)
ty := int(float64(y) / 5.0)
for x := 0; x < (mr.tileRect.Width-1)*5; x++ {
@ -648,7 +648,7 @@ func (mr *MapRegion) generateFloorCache(tile *d2ds1.FloorShadowRecord, tileX, ti
}
tileYOffset := d2common.AbsInt32(tileYMinimum)
tileHeight := d2common.AbsInt32(tileData[i].Height)
_, image := d2render.NewSurface(int(tileData[i].Width), int(tileHeight), d2render.FilterNearest)
image, _ := d2render.NewSurface(int(tileData[i].Width), int(tileHeight), d2render.FilterNearest)
pixels := make([]byte, 4*tileData[i].Width*tileHeight)
mr.decodeTileGfxData(tileData[i].Blocks, &pixels, tileYOffset, tileData[i].Width)
image.ReplacePixels(pixels)
@ -683,7 +683,7 @@ func (mr *MapRegion) generateShadowCache(tile *d2ds1.FloorShadowRecord, tileX, t
return
}
_, image := d2render.NewSurface(int(tileData.Width), tileHeight, d2render.FilterNearest)
image, _ := d2render.NewSurface(int(tileData.Width), tileHeight, d2render.FilterNearest)
pixels := make([]byte, 4*tileData.Width*int32(tileHeight))
mr.decodeTileGfxData(tileData.Blocks, &pixels, tileYOffset, tileData.Width)
image.ReplacePixels(pixels)
@ -744,7 +744,7 @@ func (mr *MapRegion) generateWallCache(tile *d2ds1.WallRecord, tileX, tileY int)
return
}
_, image := d2render.NewSurface(160, int(realHeight), d2render.FilterNearest)
image, _ := d2render.NewSurface(160, int(realHeight), d2render.FilterNearest)
pixels := make([]byte, 4*160*realHeight)
mr.decodeTileGfxData(tileData.Blocks, &pixels, tileYOffset, 160)

View File

@ -3,12 +3,10 @@ package d2render
import (
"errors"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2input"
)
var (
ErrHasInit = errors.New("rendering system is already initialized")
ErrWasInit = errors.New("rendering system is already initialized")
ErrNotInit = errors.New("rendering system has not been initialized")
ErrInvalidRenderer = errors.New("invalid rendering system specified")
)
@ -16,89 +14,76 @@ var (
var singleton Renderer
func Initialize(rend Renderer) error {
if singleton != nil {
return d2input.ErrHasInit
}
verifyNotInit()
singleton = rend
log.Printf("Initialized the %s renderer...", singleton.GetRendererName())
return nil
}
func SetWindowIcon(fileName string) error {
if singleton == nil {
return ErrNotInit
}
func SetWindowIcon(fileName string) {
verifyWasInit()
singleton.SetWindowIcon(fileName)
return nil
}
func Run(f func(Surface) error, width, height int, title string) error {
if singleton == nil {
return ErrNotInit
}
verifyWasInit()
singleton.Run(f, width, height, title)
return nil
}
func IsDrawingSkipped() (error, bool) {
if singleton == nil {
return ErrNotInit, true
}
return nil, singleton.IsDrawingSkipped()
func IsDrawingSkipped() bool {
verifyWasInit()
return singleton.IsDrawingSkipped()
}
func CreateSurface(surface Surface) (error, Surface) {
if singleton == nil {
return ErrNotInit, nil
}
func CreateSurface(surface Surface) (Surface, error) {
verifyWasInit()
return singleton.CreateSurface(surface)
}
func NewSurface(width, height int, filter Filter) (error, Surface) {
if singleton == nil {
return ErrNotInit, nil
}
func NewSurface(width, height int, filter Filter) (Surface, error) {
verifyWasInit()
return singleton.NewSurface(width, height, filter)
}
func IsFullScreen() (bool, error) {
if singleton == nil {
return false, ErrNotInit
}
func IsFullScreen() bool {
verifyWasInit()
return singleton.IsFullScreen()
}
func SetFullScreen(fullScreen bool) error {
if singleton == nil {
return ErrNotInit
}
return singleton.SetFullScreen(fullScreen)
func SetFullScreen(fullScreen bool) {
verifyWasInit()
singleton.SetFullScreen(fullScreen)
}
func SetVSyncEnabled(vsync bool) error {
if singleton == nil {
return ErrNotInit
}
return singleton.SetVSyncEnabled(vsync)
func SetVSyncEnabled(vsync bool) {
verifyWasInit()
singleton.SetVSyncEnabled(vsync)
}
func GetVSyncEnabled() (bool, error) {
if singleton == nil {
return false, ErrNotInit
}
func GetVSyncEnabled() bool {
verifyWasInit()
return singleton.GetVSyncEnabled()
}
func GetCursorPos() (int, int, error) {
if singleton == nil {
return 0, 0, ErrNotInit
}
func GetCursorPos() (int, int) {
verifyWasInit()
return singleton.GetCursorPos()
}
func CurrentFPS() (float64, error) {
if singleton == nil {
return 0, ErrNotInit
}
return singleton.CurrentFPS(), nil
func CurrentFPS() float64 {
verifyWasInit()
return singleton.CurrentFPS()
}
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}

View File

@ -2,7 +2,6 @@ package ebiten
import (
"image"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
@ -11,17 +10,12 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)
type Renderer struct {
}
type Renderer struct{}
func CreateRenderer() (*Renderer, error) {
result := &Renderer{}
config, err := d2config.Get()
if err != nil {
log.Fatal(err)
return nil, err
}
config := d2config.Get()
ebiten.SetCursorVisible(false)
ebiten.SetFullscreen(config.FullScreen)
@ -41,7 +35,6 @@ func (*Renderer) SetWindowIcon(fileName string) {
if err == nil {
ebiten.SetWindowIcon([]image.Image{iconImage})
}
}
func (r *Renderer) IsDrawingSkipped() bool {
@ -49,11 +42,7 @@ func (r *Renderer) IsDrawingSkipped() bool {
}
func (r *Renderer) Run(f func(surface d2render.Surface) error, width, height int, title string) error {
config, err := d2config.Get()
if err != nil {
log.Fatal(err)
return err
}
config := d2config.Get()
return ebiten.Run(func(img *ebiten.Image) error {
err := f(&ebitenSurface{image: img})
@ -64,7 +53,7 @@ func (r *Renderer) Run(f func(surface d2render.Surface) error, width, height int
}, width, height, config.Scale, title)
}
func (r *Renderer) CreateSurface(surface d2render.Surface) (error, d2render.Surface) {
func (r *Renderer) CreateSurface(surface d2render.Surface) (d2render.Surface, error) {
result := &ebitenSurface{
image: surface.(*ebitenSurface).image,
stateCurrent: surfaceState{
@ -72,42 +61,37 @@ func (r *Renderer) CreateSurface(surface d2render.Surface) (error, d2render.Surf
mode: ebiten.CompositeModeSourceOver,
},
}
return nil, result
return result, nil
}
func (r *Renderer) NewSurface(width, height int, filter d2render.Filter) (error, d2render.Surface) {
func (r *Renderer) NewSurface(width, height int, filter d2render.Filter) (d2render.Surface, error) {
ebitenFilter := d2ToEbitenFilter(filter)
img, err := ebiten.NewImage(width, height, ebitenFilter)
if err != nil {
return err, nil
return nil, err
}
result := &ebitenSurface{
image: img,
}
return nil, result
result := &ebitenSurface{image: img}
return result, nil
}
func (r *Renderer) IsFullScreen() (bool, error) {
return ebiten.IsFullscreen(), nil
func (r *Renderer) IsFullScreen() bool {
return ebiten.IsFullscreen()
}
func (r *Renderer) SetFullScreen(fullScreen bool) error {
func (r *Renderer) SetFullScreen(fullScreen bool) {
ebiten.SetFullscreen(fullScreen)
return nil
}
func (r *Renderer) SetVSyncEnabled(vsync bool) error {
func (r *Renderer) SetVSyncEnabled(vsync bool) {
ebiten.SetVsyncEnabled(vsync)
return nil
}
func (r *Renderer) GetVSyncEnabled() (bool, error) {
return ebiten.IsVsyncEnabled(), nil
func (r *Renderer) GetVSyncEnabled() bool {
return ebiten.IsVsyncEnabled()
}
func (r *Renderer) GetCursorPos() (int, int, error) {
cx, cy := ebiten.CursorPosition()
return cx, cy, nil
func (r *Renderer) GetCursorPos() (int, int) {
return ebiten.CursorPosition()
}
func (r *Renderer) CurrentFPS() float64 {

View File

@ -5,12 +5,12 @@ type Renderer interface {
SetWindowIcon(fileName string)
Run(f func(Surface) error, width, height int, title string) error
IsDrawingSkipped() bool
CreateSurface(surface Surface) (error, Surface)
NewSurface(width, height int, filter Filter) (error, Surface)
IsFullScreen() (bool, error)
SetFullScreen(fullScreen bool) error
SetVSyncEnabled(vsync bool) error
GetVSyncEnabled() (bool, error)
GetCursorPos() (int, int, error)
CreateSurface(surface Surface) (Surface, error)
NewSurface(width, height int, filter Filter) (Surface, error)
IsFullScreen() bool
SetFullScreen(fullScreen bool)
SetVSyncEnabled(vsync bool)
GetVSyncEnabled() bool
GetCursorPos() (int, int)
CurrentFPS() float64
}

View File

@ -9,16 +9,14 @@ import (
)
var (
ErrHasInit = errors.New("terminal system is already initialized")
ErrWasInit = errors.New("terminal system is already initialized")
ErrNotInit = errors.New("terminal system is not initialized")
)
var singleton *terminal
func Initialize() error {
if singleton != nil {
return ErrHasInit
}
verifyNotInit()
terminal, err := createTerminal()
if err != nil {
@ -26,7 +24,6 @@ func Initialize() error {
}
if err := d2input.BindHandlerWithPriority(terminal, d2input.PriorityHigh); err != nil {
log.Println(err)
return err
}
@ -34,81 +31,58 @@ func Initialize() error {
return nil
}
func Shutdown() {
if singleton != nil {
d2input.UnbindHandler(singleton)
singleton = nil
}
}
func Advance(elapsed float64) error {
if singleton == nil {
return ErrNotInit
}
if singleton != nil {
return singleton.advance(elapsed)
}
return ErrNotInit
verifyWasInit()
return singleton.advance(elapsed)
}
func Output(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.output(format, params...)
func Output(format string, params ...interface{}) {
verifyWasInit()
singleton.output(format, params...)
}
func OutputInfo(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.outputInfo(format, params...)
func OutputInfo(format string, params ...interface{}) {
verifyWasInit()
singleton.outputInfo(format, params...)
}
func OutputWarning(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.outputWarning(format, params...)
func OutputWarning(format string, params ...interface{}) {
verifyWasInit()
singleton.outputWarning(format, params...)
}
func OutputError(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.outputError(format, params...)
func OutputError(format string, params ...interface{}) {
verifyWasInit()
singleton.outputError(format, params...)
}
func BindAction(name, description string, action interface{}) error {
if singleton == nil {
return ErrNotInit
}
verifyWasInit()
return singleton.bindAction(name, description, action)
}
func UnbindAction(name string) error {
if singleton == nil {
return ErrNotInit
}
verifyWasInit()
return singleton.unbindAction(name)
}
func Render(surface d2render.Surface) error {
if singleton == nil {
return ErrNotInit
}
verifyWasInit()
return singleton.render(surface)
}
func BindLogger() {
log.SetOutput(&terminalLogger{writer: log.Writer()})
}
func verifyWasInit() {
if singleton == nil {
panic(ErrNotInit)
}
}
func verifyNotInit() {
if singleton != nil {
panic(ErrWasInit)
}
}

View File

@ -358,7 +358,7 @@ func (t *terminal) execute(command string) error {
return nil
}
func (t *terminal) outputRaw(text string, category termCategory) error {
func (t *terminal) outputRaw(text string, category termCategory) {
var line string
for _, word := range strings.Split(text, " ") {
if len(line) > 0 {
@ -377,23 +377,22 @@ func (t *terminal) outputRaw(text string, category termCategory) error {
}
t.outputHistory = append(t.outputHistory, termHistroyEntry{line, category})
return nil
}
func (t *terminal) output(format string, params ...interface{}) error {
return t.outputRaw(fmt.Sprintf(format, params...), termCategoryNone)
func (t *terminal) output(format string, params ...interface{}) {
t.outputRaw(fmt.Sprintf(format, params...), termCategoryNone)
}
func (t *terminal) outputInfo(format string, params ...interface{}) error {
return t.outputRaw(fmt.Sprintf(format, params...), termCategoryInfo)
func (t *terminal) outputInfo(format string, params ...interface{}) {
t.outputRaw(fmt.Sprintf(format, params...), termCategoryInfo)
}
func (t *terminal) outputWarning(format string, params ...interface{}) error {
return t.outputRaw(fmt.Sprintf(format, params...), termCategoryWarning)
func (t *terminal) outputWarning(format string, params ...interface{}) {
t.outputRaw(fmt.Sprintf(format, params...), termCategoryWarning)
}
func (t *terminal) outputError(format string, params ...interface{}) error {
return t.outputRaw(fmt.Sprintf(format, params...), termCategoryError)
func (t *terminal) outputError(format string, params ...interface{}) {
t.outputRaw(fmt.Sprintf(format, params...), termCategoryError)
}
func (t *terminal) outputClear() {

View File

@ -126,7 +126,7 @@ func CreateButton(buttonType ButtonType, text string) Button {
result.height += h
}
_, result.normalSurface = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.normalSurface, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
_, fontHeight := font.GetTextMetrics(text)
textY := (result.height / 2) - (fontHeight / 2) + buttonLayout.TextOffset
@ -136,22 +136,22 @@ func CreateButton(buttonType ButtonType, text string) Button {
font.Render(0, textY, text, color.RGBA{R: 100, G: 100, B: 100, A: 255}, result.normalSurface)
if buttonLayout.AllowFrameChange {
if totalButtonTypes > 1 {
_, result.pressedSurface = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.pressedSurface, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
buttonSprite.RenderSegmented(result.pressedSurface, buttonLayout.XSegments, buttonLayout.YSegments, buttonLayout.BaseFrame+1)
font.Render(-2, textY+2, text, color.RGBA{R: 100, G: 100, B: 100, A: 255}, result.pressedSurface)
}
if totalButtonTypes > 2 {
_, result.toggledSurface = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.toggledSurface, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
buttonSprite.RenderSegmented(result.toggledSurface, buttonLayout.XSegments, buttonLayout.YSegments, buttonLayout.BaseFrame+2)
font.Render(0, textY, text, color.RGBA{R: 100, G: 100, B: 100, A: 255}, result.toggledSurface)
}
if totalButtonTypes > 3 {
_, result.pressedToggledSurface = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.pressedToggledSurface, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
buttonSprite.RenderSegmented(result.pressedToggledSurface, buttonLayout.XSegments, buttonLayout.YSegments, buttonLayout.BaseFrame+3)
font.Render(0, textY, text, color.RGBA{R: 100, G: 100, B: 100, A: 255}, result.pressedToggledSurface)
}
if buttonLayout.DisabledFrame != -1 {
_, result.disabledSurface = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.disabledSurface, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
buttonSprite.RenderSegmented(result.disabledSurface, buttonLayout.XSegments, buttonLayout.YSegments, buttonLayout.DisabledFrame)
font.Render(0, textY, text, color.RGBA{R: 100, G: 100, B: 100, A: 255}, result.disabledSurface)
}

View File

@ -31,10 +31,10 @@ func CreateCheckbox(checkState bool) Checkbox {
result.width, result.height, _ = checkboxSprite.GetFrameSize(0)
checkboxSprite.SetPosition(0, 0)
_, result.Image = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.Image, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
checkboxSprite.RenderSegmented(result.Image, 1, 1, 0)
_, result.checkedImage = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
result.checkedImage, _ = d2render.NewSurface(result.width, result.height, d2render.FilterNearest)
checkboxSprite.RenderSegmented(result.checkedImage, 1, 1, 1)
return result
}

View File

@ -80,8 +80,8 @@ func (v *Label) cacheImage() {
width, height := v.font.GetTextMetrics(v.text)
v.Width = width
v.Height = height
_, v.imageData = d2render.NewSurface(width, height, d2render.FilterNearest)
_, surface := d2render.CreateSurface(v.imageData)
v.imageData, _ = d2render.NewSurface(width, height, d2render.FilterNearest)
surface, _ := d2render.CreateSurface(v.imageData)
v.font.Render(0, 0, v.text, v.Color, surface)
}

View File

@ -152,7 +152,7 @@ func (met *MapEngineTest) OnUnload() error {
func (met *MapEngineTest) Render(screen d2render.Surface) error {
met.mapEngine.Render(screen)
screenX, screenY, _ := d2render.GetCursorPos()
screenX, screenY := d2render.GetCursorPos()
worldX, worldY := met.mapEngine.ScreenToWorld(screenX, screenY)
//subtileX := int(math.Ceil(math.Mod(worldX*10, 10))) / 2
//subtileY := int(math.Ceil(math.Mod(worldY*10, 10))) / 2

74
main.go
View File

@ -80,10 +80,7 @@ func initialize() error {
return err
}
config, err := d2config.Get()
if err != nil {
return err
}
config := d2config.Get()
d2resource.LanguageCode = config.Language
renderer, err := ebiten.CreateRenderer()
@ -94,22 +91,7 @@ func initialize() error {
if err := d2render.Initialize(renderer); err != nil {
return err
}
if err := d2render.SetWindowIcon("d2logo.png"); err != nil {
return err
}
if err := d2asset.Initialize(); err != nil {
return err
}
if err := d2input.Initialize(); err != nil {
return err
}
if err := d2gui.Initialize(); err != nil {
return err
}
d2render.SetWindowIcon("d2logo.png")
if err := d2term.Initialize(); err != nil {
return err
@ -117,24 +99,14 @@ func initialize() error {
d2term.BindLogger()
d2term.BindAction("fullscreen", "toggles fullscreen", func() {
fullscreen, err := d2render.IsFullScreen()
if err == nil {
fullscreen = !fullscreen
d2render.SetFullScreen(fullscreen)
d2term.OutputInfo("fullscreen is now: %v", fullscreen)
} else {
d2term.OutputError(err.Error())
}
fullscreen := !d2render.IsFullScreen()
d2render.SetFullScreen(fullscreen)
d2term.OutputInfo("fullscreen is now: %v", fullscreen)
})
d2term.BindAction("vsync", "toggles vsync", func() {
vsync, err := d2render.GetVSyncEnabled()
if err == nil {
vsync = !vsync
d2render.SetVSyncEnabled(vsync)
d2term.OutputInfo("vsync is now: %v", vsync)
} else {
d2term.OutputError(err.Error())
}
vsync := !d2render.GetVSyncEnabled()
d2render.SetVSyncEnabled(vsync)
d2term.OutputInfo("vsync is now: %v", vsync)
})
d2term.BindAction("fps", "toggle fps counter", func() {
singleton.showFPS = !singleton.showFPS
@ -152,6 +124,14 @@ func initialize() error {
os.Exit(0)
})
if err := d2asset.Initialize(); err != nil {
return err
}
if err := d2gui.Initialize(); err != nil {
return err
}
audioProvider, err := ebiten2.CreateAudio()
if err != nil {
return err
@ -160,10 +140,7 @@ func initialize() error {
if err := d2audio.Initialize(audioProvider); err != nil {
return err
}
if err := d2audio.SetVolumes(config.BgmVolume, config.SfxVolume); err != nil {
return err
}
d2audio.SetVolumes(config.BgmVolume, config.SfxVolume)
if err := loadDataDict(); err != nil {
return err
@ -248,25 +225,14 @@ func render(target d2render.Surface) error {
func renderDebug(target d2render.Surface) error {
if singleton.showFPS {
vsyncEnabled, err := d2render.GetVSyncEnabled()
if err != nil {
return err
}
fps, err := d2render.CurrentFPS()
if err != nil {
return err
}
vsyncEnabled := d2render.GetVSyncEnabled()
fps := d2render.CurrentFPS()
cx, cy := d2render.GetCursorPos()
target.PushTranslation(5, 565)
target.DrawText("vsync:" + strconv.FormatBool(vsyncEnabled) + "\nFPS:" + strconv.Itoa(int(fps)))
target.Pop()
cx, cy, err := d2render.GetCursorPos()
if err != nil {
return err
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
target.PushTranslation(680, 0)