screen2d

package module
v0.0.0-...-17138db Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: MIT Imports: 4 Imported by: 1

README

screen2d

2D library in Go using SDL2

Documentation

Index

Constants

View Source
const (
	// KEYUP represents the Key Up state
	KEYUP uint8 = 0
	// KEYDOWN represents the Key Down or pressed state
	KEYDOWN uint8 = 1
)

Variables

This section is empty.

Functions

func CheckBoxHit

func CheckBoxHit(entity1, entity2 Hitter) bool

CheckBoxHit checks if any part of the two EntitState boxes overlap

func CheckBoxHitDebug

func CheckBoxHitDebug(entity1, entity2 Hitter) bool

CheckBoxHitDebug checks if any part of the two EntitState boxes overlap

func CheckPixelHit

func CheckPixelHit(entity1, entity2 Hitter) bool

CheckPixelHit checks if any pixels in the two EntityStates overlap

func RGBAPixels2Mask

func RGBAPixels2Mask(rgbaData []int, w, h int32) ([]bool, error)

RGBAPixels2Mask takes an array of RGBA pixel data and returns a collision mask

func RGBAPixels2Surface

func RGBAPixels2Surface(rgbaData []int, w, h int32) (*sdl.Surface, error)

RGBAPixels2Surface takes an array of RGBA pixel data and returns a Surface

func RGBAPixels2Texture

func RGBAPixels2Texture(rend *sdl.Renderer, rgbaData []int, w, h int32) (*sdl.Texture, error)

RGBAPixels2Texture takes an array of RGBA pixel data and returns a Texture

func SetScalingQuality

func SetScalingQuality(quality ScreenScalingQuality) func(*Screen)

SetScalingQuality sets the scaling quality of the screen

func SetVSync

func SetVSync(enabled bool) func(*Screen)

SetVSync enables or disables virtical sync

func Surface2Texture

func Surface2Texture(rend *sdl.Renderer, surf *sdl.Surface) (*sdl.Texture, error)

Surface2Texture takes a Surface returnes a Texxture

Types

type Box

type Box struct {
	X1, Y1 int32
	X2, Y2 int32
	W, H   int32
}

Box discribes the bounding corners of a Box

type Color

type Color struct {
	R, G, B, A uint32
}

Color meets the color.Color interface required for CreateRGBSurface

func HexColorToRGBA

func HexColorToRGBA(color int) *Color

HexColorToRGBA converts a colour stored in an int to RGBA values

func (Color) RGBA

func (c Color) RGBA() (r, g, b, a uint32)

RGBA meets the color.Color interface required for CreateRGBSurface

type Counter

type Counter struct {
	FPS int

	FrameElapsed float32
	// contains filtered or unexported fields
}

Counter hold various runtie countners

func (*Counter) FrameEnd

func (c *Counter) FrameEnd()

FrameEnd indicates the rendering frame has ended

func (*Counter) FrameStart

func (c *Counter) FrameStart()

FrameStart indicates a rendering frame as started

func (*Counter) Start

func (c *Counter) Start()

Start the counters running

type DrawFunc

type DrawFunc func()

DrawFunc is the function that will be called during the draw phase

type Drawer

type Drawer interface {
	Draw()
}

Drawer represents an item that can be drawn on the screen

type Entity

type Entity struct {
	// Virtaul game position
	X, Y, Z float32
	Scale   float32
	Sprite  *Sprite
	// ProjectXYFunc ProjectXYFunc
	Visible bool
	// contains filtered or unexported fields
}

Entity is a item that can be displayed on the screen and tested for collision

func (*Entity) ClearProjectXYFunc

func (e *Entity) ClearProjectXYFunc()

ClearProjectXYFunc restores the default virutal game to screen coord calculation

func (*Entity) Draw

func (e *Entity) Draw()

Draw is the default draw method

func (*Entity) GetBox

func (e *Entity) GetBox() Box

GetBox returns the Emntity Box

func (*Entity) GetMask

func (e *Entity) GetMask() []bool

GetMask returns the collision mask for the underlying sprite

func (*Entity) SetPos

func (e *Entity) SetPos(x, y, z int32)

SetPos sets the position of the Entity

func (*Entity) SetProjectXYFunc

func (e *Entity) SetProjectXYFunc(f ProjectXYFunc)

SetProjectXYFunc overrides the default virutal game to screen coord calculation

func (*Entity) SetSprite

func (e *Entity) SetSprite(sprite *Sprite)

SetSprite sets the Sprite the Entity will display

type EntityService

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

EntityService builds new Entities with preset default values

func NewEntityService

func NewEntityService() *EntityService

NewEntityService returns a new EntityService

func (*EntityService) NewEntity

func (es *EntityService) NewEntity() *Entity

NewEntity returns a new Entity with the preset defaults

func (*EntityService) NewTextEntity

func (es *EntityService) NewTextEntity() *TextEntity

NewTextEntity returns a new TextEntity with the preset defaults

func (*EntityService) SetScale

func (es *EntityService) SetScale(scale float32)

SetScale sets the default Scale

func (*EntityService) SetXYProjection

func (es *EntityService) SetXYProjection(projectXY ProjectXYFunc)

SetXYProjection sets the default XY projection

type EntityStrip

type EntityStrip struct {
	// Virtaul game position
	X, Y, Z float32
	Scale   float32
	Sprites []*Sprite
	// contains filtered or unexported fields
}

EntityStrip is a strip of items that can be displayed on the screen and tested for collision

func (*EntityStrip) ClearProjectXYFunc

func (e *EntityStrip) ClearProjectXYFunc()

ClearProjectXYFunc restores the default virutal game to screen coord calculation

func (*EntityStrip) Draw

func (e *EntityStrip) Draw()

Draw is the default draw method

func (*EntityStrip) GetBox

func (e *EntityStrip) GetBox() Box

GetBox returns the Entity Box

func (*EntityStrip) SetPos

func (e *EntityStrip) SetPos(x, y, z int32)

SetPos sets the position of the Entity

func (*EntityStrip) SetProjectXYFunc

func (e *EntityStrip) SetProjectXYFunc(f ProjectXYFunc)

SetProjectXYFunc overrides the default virutal game to screen coord calculation

func (*EntityStrip) SetSprite

func (e *EntityStrip) SetSprite(index int, sprite *Sprite)

SetSprite sets the Sprite the Entity will display

type Hitter

type Hitter interface {
	GetBox() Box
	GetMask() []bool
}

Hitter represents an item that can be checked to see if it has hit another item

type KBState

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

KBState represents the state of the keyboard

func NewKBState

func NewKBState() *KBState

NewKBState fsceney

func (*KBState) IsKeyDown

func (kb *KBState) IsKeyDown(key uint8) bool

IsKeyDown returns true if the key state is down

func (*KBState) IsKeyUp

func (kb *KBState) IsKeyUp(key uint8) bool

IsKeyUp returns true if the key state is up

func (*KBState) OnKeyDown

func (kb *KBState) OnKeyDown(key uint8) bool

OnKeyDown returns true when the key state changes from up to down

func (*KBState) OnKeyUp

func (kb *KBState) OnKeyUp(key uint8) bool

OnKeyUp returns true when the key state changes from up to down

func (*KBState) Refresh

func (kb *KBState) Refresh()

Refresh the keyboard state

type KeyboardEventFunc

type KeyboardEventFunc func(e *sdl.KeyboardEvent)

KeyboardEventFunc is the function that will be called when a keyboard event occurs

type ProjectXYFunc

type ProjectXYFunc func(x, y, scale float32) (tX, tY int32)

ProjectXYFunc definces a function that projects of virtaul Game X/Y coords to Screen coords

type Screen

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

Screen represents a 2d window as shown on the screen

func NewScreen

func NewScreen(width, height int, title string, hints ...ScreenHint) (*Screen, error)

NewScreen returns a newly initialisd screen in Windowed mode

func (*Screen) ClearDrawFunc

func (s *Screen) ClearDrawFunc()

ClearDrawFunc clears the function that will be called during the draw phase

func (*Screen) ClearFuncs

func (s *Screen) ClearFuncs()

ClearFuncs clears all update functions

func (*Screen) ClearKeyDownFunc

func (s *Screen) ClearKeyDownFunc()

ClearKeyDownFunc clears the function that will be called whena key is pressed

func (*Screen) ClearKeyUpFunc

func (s *Screen) ClearKeyUpFunc()

ClearKeyUpFunc clears the function that will be called when a key is released

func (*Screen) ClearUpdateFunc

func (s *Screen) ClearUpdateFunc()

ClearUpdateFunc clears the function that will be called during the update phase

func (*Screen) Close

func (s *Screen) Close()

Close causes the Screen to close

func (*Screen) Destroy

func (s *Screen) Destroy()

Destroy cleans up resources

func (*Screen) GetKBState

func (s *Screen) GetKBState() *KBState

GetKBState allows access to the current state of the keyboard

func (*Screen) Rend

func (s *Screen) Rend() *sdl.Renderer

Rend returns the SDL Renderer

func (*Screen) Run

func (s *Screen) Run()

Run starts the main event loop

func (*Screen) SetBackgroundColor

func (s *Screen) SetBackgroundColor(r, b, g, a uint8)

SetBackgroundColor sets the background color drawn on the screen

func (*Screen) SetDrawFunc

func (s *Screen) SetDrawFunc(f DrawFunc)

SetDrawFunc sets the function that will be called during the draw phase

func (*Screen) SetKeyDownFunc

func (s *Screen) SetKeyDownFunc(f KeyboardEventFunc)

SetKeyDownFunc sets the function that will be called whena key is pressed

func (*Screen) SetKeyUpFunc

func (s *Screen) SetKeyUpFunc(f KeyboardEventFunc)

SetKeyUpFunc sets the function that will be called when a key is released

func (*Screen) SetUpdateFunc

func (s *Screen) SetUpdateFunc(f UpdateFunc)

SetUpdateFunc sets the function that will be called during the update phase

type ScreenHint

type ScreenHint func(*Screen)

ScreenHint represtents configuration options when creating a new screen

type ScreenScalingQuality

type ScreenScalingQuality int

ScreenScalingQuality represent the scaling method

const (
	// ScreenScalingNearestPixel - nearest pixel sampling
	ScreenScalingNearestPixel ScreenScalingQuality = iota
	// ScreenScalingLinear - linear filtering (supported by OpenGL and Direct3D)
	ScreenScalingLinear
	// ScreenScalingAnistropic - anisotropic filtering (supported by Direct3D)
	ScreenScalingAnistropic
)

type Sprite

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

Sprite represents a single object on the screen

func NewSprite

func NewSprite(rend *sdl.Renderer) *Sprite

NewSprite returns a new Sprite

func (*Sprite) DrawAt

func (s *Sprite) DrawAt(x, y int32, scale float32)

DrawAt the Sprite onto the screen

func (*Sprite) GetPitch

func (s *Sprite) GetPitch() int32

GetPitch returns the Sprite Pitch

func (*Sprite) GetPixels

func (s *Sprite) GetPixels() []byte

GetPixels returns the Sprite Pixels

func (*Sprite) LoadRGBAPixels

func (s *Sprite) LoadRGBAPixels(pixels []int, pitch int32) error

LoadRGBAPixels loads the Sprite using an array of 32-bit pixels containing RGBA values

type SpriteAtlas

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

SpriteAtlas comprises multiple images tiled into a larger image. These images can be retrieved via their coordinates

func NewSpriteAtlas

func NewSpriteAtlas(rend *sdl.Renderer) *SpriteAtlas

NewSpriteAtlas returns a new SpriteAtlas

func (*SpriteAtlas) DrawTileAt

func (sa *SpriteAtlas) DrawTileAt(tileX, tileY, screenX, screenY int32, scale float32)

DrawTileAt draws a specific tile at the given location

func (*SpriteAtlas) LoadRGBAPixels

func (sa *SpriteAtlas) LoadRGBAPixels(pixels []int, pixelsPitch, tilePitch, tileHeight int32) error

LoadRGBAPixels loads the Sprite using an array of 32-bit pixels containing RGBA values

type SpriteMap

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

SpriteMap contains a map of individual Sprites that can be retrieved using an SpriteMapKey

func NewSpriteMap

func NewSpriteMap() *SpriteMap

NewSpriteMap returns an empty SpriteMap

func (*SpriteMap) AddSprite

func (a *SpriteMap) AddSprite(key SpriteMapKey, sprite *Sprite)

AddSprite adds a Sprite to the SpriteMap against the specified SpriteMapKey

func (*SpriteMap) GetSprite

func (a *SpriteMap) GetSprite(key SpriteMapKey) *Sprite

GetSprite gets the Sprite at the SpriteMap position specified by the SpriteMapKey

type SpriteMapKey

type SpriteMapKey int

SpriteMapKey is used to retrieve a specific Sprite from the SpriteMap

type State

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

State ..

type StripDirection

type StripDirection int

StripDirection represents the direction in which the Sprites will be drawn.

const (
	// Left2RightStrip draws from left to right
	Left2RightStrip StripDirection = iota
	// Top2BottomStrip draws from top to bootm
	Top2BottomStrip
)

type TextEntity

type TextEntity struct {
	// Virtaul game position
	X, Y, Z float32
	Scale   float32

	// ProjectXYFunc ProjectXYFunc
	Visible bool
	// contains filtered or unexported fields
}

TextEntity is a item that can be displayed on the screen and tested for collision

func (*TextEntity) ClearProjectXYFunc

func (e *TextEntity) ClearProjectXYFunc()

ClearProjectXYFunc restores the default virutal game to screen coord calculation

func (*TextEntity) Draw

func (e *TextEntity) Draw()

Draw is the default draw method

func (*TextEntity) LoadAtlas

func (e *TextEntity) LoadAtlas(atlas *SpriteAtlas, keys map[rune]int32, fontWidth int32)

LoadAtlas sets the Sprite the Entity will display

func (*TextEntity) SetPos

func (e *TextEntity) SetPos(x, y, z int32)

SetPos sets the position of the Entity

func (*TextEntity) SetProjectXYFunc

func (e *TextEntity) SetProjectXYFunc(f ProjectXYFunc)

SetProjectXYFunc overrides the default virutal game to screen coord calculation

func (*TextEntity) SetText

func (e *TextEntity) SetText(value string)

SetText sets the text that will be displayed

type UpdateFunc

type UpdateFunc func(ticks uint32, elapsed float32)

UpdateFunc is the function that will be called during the update phase

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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