sdlkit

package module
v0.0.0-...-2c3a8a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2022 License: BSD-3-Clause Imports: 14 Imported by: 0

README

SDL2 toolkit for Go

Latest release Build status Go Report Card Documentation

Package sdlkit contains basic building blocks when using SDL2 with go. It uses github.com/veandco/go-sdl2 at its base and builds upon it. This package is not necessarily a game or physics engine, but does provide enough common structs and functions to easily make your ideas become a reality.

go get github.com/roeldev/go-sdl2-experiments/pkg/sdlkit
import "github.com/roeldev/go-sdl2-experiments/pkg/sdlkit"

SDL2 installation on Windows

  • install a gcc compiler (for example tdm-gcc from https://jmeubank.github.io/tdm-gcc/download/)
  • make sure all gcc related executables are available through your PATH environment variable, test this by running gcc -v
  • run go get -v github.com/veandco/go-sdl2/sdl@master
  • run go mod vendor to create/sync vendor directory
  • run git submodule add https://github.com/veandco/go-sdl2-libs.git .go-sdl2-libs
  • copy/symlink .go-sdl2-libs to vendor/github.com/veandco/go-sdl12/.go-sdl2-libs
  • build/run your project with the -tags static build flag
  • optionally add the -v -x build flags to output the files that are compiled

Documentation

Additional detailed documentation is available at pkg.go.dev

Examples

Several examples can be found at https://github.com/roeldev/go-sdl2-experiments.

Created with

License

Copyright © 2020-2021 Roel Schut. All rights reserved.

This project is governed by a BSD-style license that can be found in the LICENSE file.

Documentation

Index

Constants

View Source
const (
	DefaultFps       uint8   = 60
	DefaultTimeScale float64 = 1.0
)
View Source
const QUIT quit = "QUIT"

Variables

View Source
var DefaultOptions = Options{

	PosX:           sdl.WINDOWPOS_CENTERED,
	PosY:           sdl.WINDOWPOS_CENTERED,
	WindowFlags:    sdl.WINDOW_SHOWN | sdl.WINDOW_INPUT_FOCUS,
	FullscreenMode: 1,

	RendererIndex: -1,
	RendererFlags: sdl.RENDERER_ACCELERATED | sdl.RENDERER_PRESENTVSYNC,
	BgColor:       color.RGBA{},

	TargetFps: DefaultFps,
}
View Source
var ErrInvalidTexture = stderrors.New("Invalid texture")
View Source
var FatalErrorTitle = "fatal error"

Functions

func Draw

func Draw(renderer *sdl.Renderer, draw ...Drawable) error

func FailOnErr

func FailOnErr(possibleErr error)

FailOnErr shows a simple message box and exits the program when it receives a non-nil error.

func GetClosestDisplayModeRatio

func GetClosestDisplayModeRatio(displayIndex int, mode sdl.DisplayMode) (*sdl.DisplayMode, error)

func LoadTextureFromMem

func LoadTextureFromMem(ren *sdl.Renderer, data []byte) (*sdl.Texture, error)

func OpenFontFromMem

func OpenFontFromMem(data []byte, size int) (*sdlttf.Font, error)

func OpenFontIndexFromMem

func OpenFontIndexFromMem(data []byte, size, index int) (*sdlttf.Font, error)

func RNG

func RNG() *rand.Rand

RNG returns a new rand.Rand with the current unix time as source.

func RunLoop

func RunLoop(stage *Stage) error

Types

type AssetsLoader

type AssetsLoader struct {

	// Surfaces SurfacesMap
	Textures TexturesMap
	Fonts    *FontsMap
	// contains filtered or unexported fields
}

func NewAssetsLoader

func NewAssetsLoader(fs fs.ReadFileFS, r *sdl.Renderer) *AssetsLoader

func (*AssetsLoader) Font

func (l *AssetsLoader) Font(file string, size int, index uint) (font *sdlttf.Font, err error)

func (*AssetsLoader) Read

func (l *AssetsLoader) Read(file string) ([]byte, error)

func (*AssetsLoader) ReadRW

func (l *AssetsLoader) ReadRW(file string) (*sdl.RWops, error)

func (*AssetsLoader) Surface

func (l *AssetsLoader) Surface(file string) (*sdl.Surface, error)

func (*AssetsLoader) Texture

func (l *AssetsLoader) Texture(file string) (*sdl.Texture, error)

func (*AssetsLoader) TextureAtlas

func (l *AssetsLoader) TextureAtlas(file string, locations map[string]sdl.Rect) (*TextureAtlas, error)

func (*AssetsLoader) TextureAtlasXml

func (l *AssetsLoader) TextureAtlasXml(file string) (*TextureAtlas, error)

func (*AssetsLoader) UniformTextureAtlas

func (l *AssetsLoader) UniformTextureAtlas(file string, w, h int32, total uint8) (*TextureAtlas, error)

type Clock

type Clock struct {

	// TimeScale affects the speed of time. Its default value is 1.0.
	// When TimeScale < 1, time slows down. Time speeds up when TimeScale > 1.
	TimeScale float64

	// Delta64 returns the current delta time value multiplied by TimeScale.
	Delta64 float64

	// Delta32 is a float32 version of Delta64.
	Delta32 float32
	// contains filtered or unexported fields
}

func NewClock

func NewClock() *Clock

func (*Clock) Pause

func (c *Clock) Pause()

func (*Clock) Time

func (c *Clock) Time() *Time

func (*Clock) Unpause

func (c *Clock) Unpause()

type Drawable

type Drawable interface {
	Draw(r *sdl.Renderer) error
}

type DrawableFunc

type DrawableFunc func(r *sdl.Renderer) error

func (DrawableFunc) Draw

func (d DrawableFunc) Draw(r *sdl.Renderer) error

type FontsMap

type FontsMap map[string]*sdlttf.Font

type Layer

type Layer []Drawable

func (*Layer) Append

func (l *Layer) Append(d ...Drawable)

func (Layer) Draw

func (l Layer) Draw(r *sdl.Renderer) error

func (*Layer) Prepend

func (l *Layer) Prepend(d ...Drawable)

type Options

type Options struct {
	Context context.Context

	// sdl.Window options
	// see https://wiki.libsdl.org/SDL_CreateWindow
	PosX, PosY     int32
	WindowFlags    uint32
	FullscreenMode uint32 // https://wiki.libsdl.org/SDL_SetWindowFullscreen

	// sdl.DisplayMode options
	// (https://wiki.libsdl.org/SDL_DisplayMode)
	DisplayMode sdl.DisplayMode

	// sdl.Renderer options
	// see https://wiki.libsdl.org/SDL_CreateRenderer
	RendererIndex int
	RendererFlags uint32

	BgColor color.Color // see https://wiki.libsdl.org/SDL_RenderClear

	// timer
	TargetFps      uint8 // todo: DisplayMode.RefreshRate
	LimitFps       bool
	WindowTitleFps bool
}

type Scene

type Scene interface {
	SceneName() string
	Process() error
	Update(dt float64)
	Render(r *sdl.Renderer) error
}

type SceneActivater

type SceneActivater interface {
	Scene
	Activate() error
}

type SceneDeactivater

type SceneDeactivater interface {
	Scene
	Deactivate() error
}

type SceneDestroyer

type SceneDestroyer interface {
	Scene
	Destroy() error
}

type SceneManager

type SceneManager struct {
	// contains filtered or unexported fields
}

func NewSceneManager

func NewSceneManager() *SceneManager

func (*SceneManager) Activate

func (sm *SceneManager) Activate(name string) (Scene, error)

func (*SceneManager) ActivationScheduled

func (sm *SceneManager) ActivationScheduled() bool

func (*SceneManager) ActiveSceneName

func (sm *SceneManager) ActiveSceneName() string

func (*SceneManager) Add

func (sm *SceneManager) Add(scene Scene)

func (*SceneManager) Destroy

func (sm *SceneManager) Destroy() error

func (*SceneManager) Get

func (sm *SceneManager) Get(name string) Scene

func (*SceneManager) Has

func (sm *SceneManager) Has(name string) bool

func (*SceneManager) Remove

func (sm *SceneManager) Remove(name string, destroy bool) (bool, error)

func (*SceneManager) ScheduleActivation

func (sm *SceneManager) ScheduleActivation(name string) error

func (*SceneManager) UpdateActiveScene

func (sm *SceneManager) UpdateActiveScene(scenePtr *Scene) bool

type SplashScreen

type SplashScreen struct {
	// contains filtered or unexported fields
}

func MustNewSplashScreen

func MustNewSplashScreen(w, h int32) *SplashScreen

func NewSplashScreen

func NewSplashScreen(w, h int32) (*SplashScreen, error)

func (*SplashScreen) Destroy

func (s *SplashScreen) Destroy() error

func (*SplashScreen) DisplayImage

func (s *SplashScreen) DisplayImage(data []byte) error

func (*SplashScreen) Window

func (s *SplashScreen) Window() *sdl.Window

Window returns the sdl.Window where the SplashScreen is shown in.

type Stage

type Stage struct {
	BgColor color.RGBA
	// contains filtered or unexported fields
}

func MustNewStage

func MustNewStage(title string, width, height int32, opts Options) *Stage

MustNewStage creates a new Stage using NewStage and returns it on success. Any returned errors from NewStage are passed to FailOnErr and result in a fatal exit of the program.

func NewStage

func NewStage(title string, w, h int32, opts Options) (*Stage, error)

NewStage creates a new Stage by first creating a new sdl.Window and sdl.Renderer. These are configured with the provided Options.

func (*Stage) AddScene

func (s *Stage) AddScene(scene Scene) error

AddScene adds a new Scene to the SceneManager of the Stage. It also activates the Scene when no other Scene is currently active.

func (*Stage) ClearScreen

func (s *Stage) ClearScreen() error

func (*Stage) Clock

func (s *Stage) Clock() *Clock

func (*Stage) Context

func (s *Stage) Context() context.Context

func (*Stage) Destroy

func (s *Stage) Destroy()

func (*Stage) FHeight

func (s *Stage) FHeight() float64

Height returns the height of the logical size of the Stage as a float64.

func (*Stage) FWidth

func (s *Stage) FWidth() float64

Width returns the width of the logical size of the Stage as a float64.

func (*Stage) HandleKeyUpEvent

func (s *Stage) HandleKeyUpEvent(e *sdl.KeyboardEvent) error

func (*Stage) HandleWindowSizeChangedEvent

func (s *Stage) HandleWindowSizeChangedEvent(e *sdl.WindowEvent) error

func (*Stage) Height

func (s *Stage) Height() int32

Height returns the height of the logical size of the Stage.

func (*Stage) MustAddScene

func (s *Stage) MustAddScene(scene Scene, possibleErr error)

MustAddScene adds a Scene to the SceneManager, the same way AddScene does. Any errors are passed to FailOnErr.

func (*Stage) PresentScreen

func (s *Stage) PresentScreen()

func (*Stage) Renderer

func (s *Stage) Renderer() *sdl.Renderer

Renderer returns the sdl.Renderer that's attached to the window.

func (*Stage) Scene

func (s *Stage) Scene() Scene

Scene returns the current active scene from the SceneManager.

func (*Stage) SceneManager

func (s *Stage) SceneManager() *SceneManager

SceneManager returns the SceneManager instance that handles switching of scenes for the Stage.

func (*Stage) SetWindowIcon

func (s *Stage) SetWindowIcon(icon []byte) error

https://wiki.libsdl.org/SDL_SetWindowIcon

func (*Stage) Size

func (s *Stage) Size() sdl.Rect

Size returns the current logical size of the Stage.

func (*Stage) Time

func (s *Stage) Time() *Time

Time returns the Time that keeps track of time and framerate.

func (*Stage) ToggleFullscreen

func (s *Stage) ToggleFullscreen() (err error)

func (*Stage) ToggleWindowTitleFps

func (s *Stage) ToggleWindowTitleFps()

func (*Stage) Width

func (s *Stage) Width() int32

Width returns the width of the logical size of the Stage.

func (*Stage) Window

func (s *Stage) Window() *sdl.Window

Window returns the sdl.Window in which the Stage is set.

type TextureAtlas

type TextureAtlas struct {
	// contains filtered or unexported fields
}

func NewTextureAtlas

func NewTextureAtlas(tx *sdl.Texture, locations map[string]sdl.Rect) *TextureAtlas

func NewUniformTextureAtlas

func NewUniformTextureAtlas(tx *sdl.Texture, cellW, cellH int32, total uint8) (*TextureAtlas, error)

func (*TextureAtlas) Destroy

func (ta *TextureAtlas) Destroy() error

func (*TextureAtlas) GetFomIndex

func (ta *TextureAtlas) GetFomIndex(i int) (TextureClip, error)

func (*TextureAtlas) GetFromName

func (ta *TextureAtlas) GetFromName(name string) (TextureClip, error)

func (*TextureAtlas) HasIndex

func (ta *TextureAtlas) HasIndex(i int) bool

func (*TextureAtlas) HasName

func (ta *TextureAtlas) HasName(name string) bool

func (*TextureAtlas) IsUniform

func (ta *TextureAtlas) IsUniform() bool

func (*TextureAtlas) Len

func (ta *TextureAtlas) Len() int

func (*TextureAtlas) Names

func (ta *TextureAtlas) Names() []string

func (*TextureAtlas) Texture

func (ta *TextureAtlas) Texture() *sdl.Texture

type TextureClip

type TextureClip struct {
	Texture  *sdl.Texture
	Location sdl.Rect
}

func (TextureClip) Size

func (tc TextureClip) Size() (float64, float64)

type TexturesMap

type TexturesMap map[string]*sdl.Texture

func (TexturesMap) Destroy

func (t TexturesMap) Destroy(name string) error

func (TexturesMap) DestroyAll

func (t TexturesMap) DestroyAll() error

Destroy destroys all sdl.Textures within the TexturesMap.

type Time

type Time struct {
	LimitFps bool
	// contains filtered or unexported fields
}

func NewTime

func NewTime(targetFps uint8, clock ...*Clock) *Time

func (*Time) AvgFps

func (t *Time) AvgFps() float32

AvgFps returns the average FPS of the last 30 seconds.

func (*Time) ConvTicks

func (t *Time) ConvTicks(ticks uint32) time.Time

ConvTicks coverts a ticks value from sdl.GetTicks to a time.Time value. The result may be a few microseconds off but is well below a millisecond.

func (*Time) Elapsed

func (t *Time) Elapsed() time.Duration

func (*Time) Fps

func (t *Time) Fps() float32

Fps returns the average FPS of the last 500 milliseconds.

func (*Time) Init

func (t *Time) Init() *Time

func (*Time) RegisterClock

func (t *Time) RegisterClock(clock *Clock)

func (*Time) SetTargetFps

func (t *Time) SetTargetFps(targetFps uint8) *Time

func (*Time) String

func (t *Time) String() string

func (*Time) Tick

func (t *Time) Tick() float64

Directories

Path Synopsis
debug
Package event defines interfaces for sdl.Event handling.
Package event defines interfaces for sdl.Event handling.
Package geom defines a two-dimensional coordinate system.
Package geom defines a two-dimensional coordinate system.
xform
Package xform supplies additional transform related features.
Package xform supplies additional transform related features.
Package math provides additional game math related functions.
Package math provides additional game math related functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL