sdl2

package
v0.0.0-...-66ba032 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2018 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

動作橋段

孤 使用 sdl2 封裝的一個 2d 遊戲引擎

Index

Constants

View Source
const (
	//鼠標 單擊
	UI_EVT_LBTN_CLICK = "k-lbtn_click"
	UI_EVT_RBTN_CLICK = "k-rbtn_click"
	UI_EVT_MBTN_CLICK = "k-mbtn_click"
	//鼠標 雙擊
	UI_EVT_LBTN_DCLICK = "k-lbtn_dclick"
	UI_EVT_RBTN_DCLICK = "k-rbtn_dclick"
	UI_EVT_MBTN_DCLICK = "k-mbtn_dclick"

	//鍵盤 按鍵
	UI_EVT_KEY_DOWM = "k-key-down"
	UI_EVT_KEY_UP   = "k-key-up"

	//焦點
	UI_EVT_FOCUS_IN  = "k-foucs-in"
	UI_EVT_FOCUS_OUT = "k-foucs-out"
)
View Source
const (
	R_MASK = 0x000000ff
	G_MASK = 0x0000ff00
	B_MASK = 0x00ff0000
	A_MASK = 0xff000000
)
View Source
const (
	FLIP_NONE       = sdl.FLIP_NONE
	FLIP_HORIZONTAL = sdl.FLIP_HORIZONTAL
	FLIP_VERTICAL   = sdl.FLIP_VERTICAL
)
View Source
const (
	FONT_DEFAULT_FILE = "NotoSansCJKtc-Regular.otf"
	FONT_DEFAULT_SIZE = 16
)
View Source
const (
	BUFFER_INIT_COUNT = 10
)

Variables

This section is empty.

Functions

func CreateWindow

func CreateWindow(title string, w, h, fps int) error

創建一個 遊戲窗口

func DestoryEngine

func DestoryEngine()

銷毀 引擎資源

func DestoryWindow

func DestoryWindow()

銷毀 遊戲 窗口

func GetWindow

func GetWindow() *sdl.Window

返回 遊戲 窗口

func InitEngine

func InitEngine() error

初始化 引擎

func LoadTexture

func LoadTexture(file string) (*sdl.Texture, error)

func LoadTextureMem

func LoadTextureMem(b []byte) (*sdl.Texture, error)

func PushEvent

func PushEvent(evt interface{})

壓入一個 事件

func PushScene

func PushScene(scene IObject)

壓入 場景

func Run

func Run(r, g, b, a uint8, show bool)

渲染遊戲 運行邏輯

func SetFocus

func SetFocus(obj IObject)

設置獲取焦點元素

Types

type ActionAlphaTo

type ActionAlphaTo struct {
	ActionBase
	// contains filtered or unexported fields
}

勻速 改變 Alpha 到指定值

func NewActionAlphaTo

func NewActionAlphaTo(alpha float64, duration time.Duration) *ActionAlphaTo

alpha 目標alpha duration 花費時間

func (*ActionAlphaTo) Clone

func (a *ActionAlphaTo) Clone() IAction

返回一個動作副本

func (*ActionAlphaTo) Destory

func (a *ActionAlphaTo) Destory()

釋放 動作

func (*ActionAlphaTo) DoAction

func (a *ActionAlphaTo) DoAction(node IObject, duration time.Duration)

執行動作

type ActionBase

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

func (*ActionBase) Destroy

func (a *ActionBase) Destroy()

釋放 動作

func (*ActionBase) GetAutoDestroy

func (a *ActionBase) GetAutoDestroy() bool

是否自動 釋放 返回 true 移除action時 自動調用 a.Destroy()

func (*ActionBase) GetCallBack

func (a *ActionBase) GetCallBack() (ActionCallBack, interface{})

返回 action 完成 通知

func (*ActionBase) GetLoop

func (a *ActionBase) GetLoop() bool

返回 是否 循環執行

func (*ActionBase) SetAutoDestory

func (a *ActionBase) SetAutoDestory(yes bool)

func (*ActionBase) SetCallBack

func (a *ActionBase) SetCallBack(callback ActionCallBack, params interface{})

func (*ActionBase) SetLoop

func (a *ActionBase) SetLoop(yes bool)

type ActionCallBack

type ActionCallBack func(node IObject, a IAction, params interface{})

動作執行完一個週期後的 回調

type ActionLinear

type ActionLinear struct {
	ActionBase
	// contains filtered or unexported fields
}

將多個 action 線性執行 對於 ActionLinear 中的 Action GetLoop() 的返回值 會被忽略直接以 false 處理

func NewActionLinear

func NewActionLinear(as ...IAction) *ActionLinear

創建線性動作

func (*ActionLinear) Clone

func (a *ActionLinear) Clone() IAction

返回一個動作副本

func (*ActionLinear) Destory

func (a *ActionLinear) Destory()

釋放 動作

func (*ActionLinear) DoAction

func (a *ActionLinear) DoAction(node IObject, duration time.Duration)

執行動作

type ActionMoveTo

type ActionMoveTo struct {
	ActionBase
	// contains filtered or unexported fields
}

勻速 移動到 指定 坐標

func NewActionMoveTo

func NewActionMoveTo(x, y float64, duration time.Duration) *ActionMoveTo

x,y 目標坐標 duration 花費時間

func (*ActionMoveTo) Clone

func (a *ActionMoveTo) Clone() IAction

返回一個動作副本

func (*ActionMoveTo) Destory

func (a *ActionMoveTo) Destory()

釋放 動作

func (*ActionMoveTo) DoAction

func (a *ActionMoveTo) DoAction(node IObject, duration time.Duration)

執行動作

type ActionMovie

type ActionMovie struct {
	ActionBase
	// contains filtered or unexported fields
}

播放動畫

func NewActionMovie

func NewActionMovie(n int, duration time.Duration) *ActionMovie

n 預計紋理數量 duration 花費時間

func (*ActionMovie) Clone

func (a *ActionMovie) Clone() IAction

返回一個動作副本

func (*ActionMovie) Destory

func (a *ActionMovie) Destory()

釋放 動作

func (*ActionMovie) DoAction

func (a *ActionMovie) DoAction(node IObject, duration time.Duration)

執行動作

func (*ActionMovie) PushFrame

func (a *ActionMovie) PushFrame(texture *sdl.Texture)

增加一個 動畫幀

type ActionScaleTo

type ActionScaleTo struct {
	ActionBase
	// contains filtered or unexported fields
}

勻速 改變 scale x y 到指定值

func NewActionScaleTo

func NewActionScaleTo(scaleX, scaleY float64, duration time.Duration) *ActionScaleTo

scaleX scaleY 目標scale duration 花費時間

func (*ActionScaleTo) Clone

func (a *ActionScaleTo) Clone() IAction

返回一個動作副本

func (*ActionScaleTo) Destory

func (a *ActionScaleTo) Destory()

釋放 動作

func (*ActionScaleTo) DoAction

func (a *ActionScaleTo) DoAction(node IObject, duration time.Duration)

執行動作

type ActionTogether

type ActionTogether struct {
	ActionBase
	// contains filtered or unexported fields
}

將多個 action 並行執行 ActionTogether 的 callback 會在 所有 action 完成後被 調用

func NewActionTogether

func NewActionTogether(as ...IAction) *ActionTogether

創建並行動作

func (*ActionTogether) Clone

func (a *ActionTogether) Clone() IAction

返回一個動作副本

func (*ActionTogether) Destory

func (a *ActionTogether) Destory()

釋放 動作

func (*ActionTogether) DoAction

func (a *ActionTogether) DoAction(node IObject, duration time.Duration)

執行動作

type Director

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

導演 保存了遊戲 環境 運行狀況

type FocusInEvent

type FocusInEvent struct {
	//得到焦點元素
	Obj IObject
}

獲取到焦點

type FocusOutEvent

type FocusOutEvent struct {
	//失去焦點元素
	Obj IObject
}

失去焦點

type IAction

type IAction interface {
	//執行動作
	DoAction(node IObject, duration time.Duration)
	//釋放 動作
	Destroy()
	//是否自動 釋放
	//返回 true 移除action時 自動調用 a.Destory()
	GetAutoDestroy() bool
	SetAutoDestory(yes bool)
	//返回一個動作副本
	Clone() IAction

	//設置 action 完成 通知
	SetCallBack(callback ActionCallBack, params interface{})
	//返回 action 完成 通知
	GetCallBack() (ActionCallBack, interface{})

	//返回 是否 循環執行
	GetLoop() bool
	//設置 是否 循環執行
	SetLoop(yes bool)
}

施加到 演員的 動作定義

type IObject

type IObject interface {
	//繪製自己
	Draw(renderer *sdl.Renderer, duration time.Duration)

	//執行動作
	OnAction(duration time.Duration)

	//處理 事件 返回 true 停止事件傳遞
	OnEvent(evt sdl.Event) bool

	//是否可見
	IsVisible() bool
	//設置是否可見
	SetVisible(yes bool)

	//返回 坐標 (相對父節點)
	GetPos() (float64, float64)
	//坐標轉 屏幕坐標
	ToScreenPos(x, y float64) (float64, float64)
	//坐標轉 相對坐標
	ToPos(x, y float64) (float64, float64)

	//設置 坐標
	SetPos(x float64, y float64)
	//返回 大小
	GetSize() (int, int)
	//設置 大小
	SetSize(w int, h int)

	//返回繪製坐標
	GetDrawPos() (float64, float64)
	//返回繪製 大小
	GetDrawSize() (float64, float64)

	//返回 z 坐標
	GetZ() int
	//返回 z 坐標
	SetZ(z int)
	//返回 子節點中的 最大 z 坐標
	GetMaxZ() int
	//返回 子節點中的 最小 z 坐標
	GetMinZ() int

	//銷毀 元素
	Destroy()

	//設置元素 id
	SetId(id string)
	//設置元素 Tag
	SetTag(tag string)
	//返回元素 id
	GetId() string
	//返回元素 Tag
	GetTag() string

	//增加一個 子元素
	Add(obj IObject)

	//刪除一個 子元素
	Remove(obj IObject)
	//刪除一個 指定id的 子元素
	RemoveById(id string)
	//刪除 指定 tag 的元素
	RemoveByTag(tag string)

	//返回 子元素 數量
	LenChilds() int

	//返回 父節點
	GetParent() IObject
	//設置 父節點
	SetParent(parent IObject)

	//綁定一個 動作 多次 bind 的 動作 同時被執行
	BindAction(a IAction)
	//移除一個 動作
	RemoveAction(a IAction)

	//設置紋理
	SetTexture(texture *sdl.Texture)
	//返回當前 紋理
	GetTexture() *sdl.Texture

	//返回 錨點
	GetAnchor() (anchorX, anchorY float64)
	//設置 錨點
	SetAnchor(anchorX, anchorY float64)
	SetAnchorX(anchor float64)
	SetAnchorY(anchor float64)

	//設置 透明度
	SetAlpha(a float64)
	//返回 透明度
	GetAlpha() float64

	//設置 縮放
	SetScale(scaleX, scaleY float64)
	SetScaleX(scaleX float64)
	SetScaleY(scaleY float64)

	//返回 縮放
	GetScale() (scaleX float64, scaleY float64)
	GetScaleX() float64
	GetScaleY() float64
}

基礎接口定義

func GetScene

func GetScene() IObject

返回當前 場景

func PopScene

func PopScene() IObject

彈出 場景

func Replace

func Replace(scene IObject) IObject

替換當前 場景

type IUi

type IUi interface {
	IObject
	//設置 事件回調
	SetEventCallback(evt string, callback UiCallBack)
	//返回 事件回調 函數
	GetEventCallback(evt string) UiCallBack
}

type Node

type Node struct {
	//紋理
	Texture *sdl.Texture

	//坐標 大小
	X, Y float64

	//放大縮小 [0,+...)
	ScaleX, ScaleY float64
	Width, Height  int
	//z坐標 值越小 越先繪製 越後響應事件
	Z int

	//錨點 [0,1]
	AnchorX, AnchorY float64

	//旋轉角度
	Angle float64
	//翻轉
	Flip sdl.RendererFlip

	//透明
	Alpha float64
	// contains filtered or unexported fields
}

基礎 接口 實現

func NewNode

func NewNode(x, y float64, z, w, h int, texture *sdl.Texture) *Node

func (*Node) Add

func (n *Node) Add(obj IObject)

增加一個 子元素

func (*Node) BindAction

func (n *Node) BindAction(a IAction)

綁定一個 動作 多次 bind 的 動作 同時被執行

func (*Node) Destroy

func (n *Node) Destroy()

銷毀 元素

func (*Node) Draw

func (n *Node) Draw(renderer *sdl.Renderer, duration time.Duration)

繪製自己

func (*Node) GetAlpha

func (n *Node) GetAlpha() float64

返回 透明度

func (*Node) GetAnchor

func (n *Node) GetAnchor() (anchorX, anchorY float64)

返回 錨點

func (*Node) GetDrawPos

func (n *Node) GetDrawPos() (float64, float64)

返回繪製坐標

func (*Node) GetDrawScale

func (n *Node) GetDrawScale() (float64, float64)

func (*Node) GetDrawSize

func (n *Node) GetDrawSize() (float64, float64)

返回繪製 大小

func (*Node) GetId

func (n *Node) GetId() string

返回元素 id

func (*Node) GetMaxZ

func (n *Node) GetMaxZ() int

返回 子節點中的 最大 z 坐標

func (*Node) GetMinZ

func (n *Node) GetMinZ() int

返回 子節點中的 最小 z 坐標

func (*Node) GetParent

func (n *Node) GetParent() IObject

返回 父節點

func (*Node) GetPos

func (n *Node) GetPos() (float64, float64)

返回 坐標

func (*Node) GetScale

func (n *Node) GetScale() (float64, float64)

返回 縮放

func (*Node) GetScaleX

func (n *Node) GetScaleX() float64

func (*Node) GetScaleY

func (n *Node) GetScaleY() float64

func (*Node) GetSize

func (n *Node) GetSize() (int, int)

返回 大小

func (*Node) GetTag

func (n *Node) GetTag() string

返回元素 Tag

func (*Node) GetTexture

func (n *Node) GetTexture() *sdl.Texture

返回當前 紋理

func (*Node) GetZ

func (n *Node) GetZ() int

返回 z 坐標

func (*Node) IsVisible

func (n *Node) IsVisible() bool

是否可見

func (*Node) LenChilds

func (n *Node) LenChilds() int

返回 子節點數量

func (*Node) OnAction

func (n *Node) OnAction(duration time.Duration)

func (*Node) OnEvent

func (n *Node) OnEvent(evt sdl.Event) bool

處理 事件 返回 true 停止事件傳遞

func (*Node) Remove

func (n *Node) Remove(obj IObject)

刪除一個 子元素

func (*Node) RemoveAction

func (n *Node) RemoveAction(a IAction)

移除一個 動作

func (*Node) RemoveById

func (n *Node) RemoveById(id string)

刪除一個 指定id的 子元素

func (*Node) RemoveByTag

func (n *Node) RemoveByTag(tag string)

刪除 指定 tag 的元素

func (*Node) SetAlpha

func (n *Node) SetAlpha(a float64)

設置 透明度

func (*Node) SetAnchor

func (n *Node) SetAnchor(anchorX, anchorY float64)

設置 錨點

func (*Node) SetAnchorX

func (n *Node) SetAnchorX(anchor float64)

func (*Node) SetAnchorY

func (n *Node) SetAnchorY(anchor float64)

func (*Node) SetId

func (n *Node) SetId(id string)

設置元素 id

func (*Node) SetParent

func (n *Node) SetParent(obj IObject)

設置 父節點 不要調用 此函數 除非你的確知道在做什麼 要 添加子節點 調用 父節點的 Add 方法

func (*Node) SetPos

func (n *Node) SetPos(x float64, y float64)

設置 坐標

func (*Node) SetScale

func (n *Node) SetScale(scaleX, scaleY float64)

設置 縮放

func (*Node) SetScaleX

func (n *Node) SetScaleX(scale float64)

func (*Node) SetScaleY

func (n *Node) SetScaleY(scale float64)

func (*Node) SetSize

func (n *Node) SetSize(w int, h int)

設置 大小

func (*Node) SetTag

func (n *Node) SetTag(tag string)

設置元素 Tag

func (*Node) SetTexture

func (n *Node) SetTexture(texture *sdl.Texture)

設置紋理

func (*Node) SetVisible

func (n *Node) SetVisible(ok bool)

設置是否可見

func (*Node) SetZ

func (n *Node) SetZ(z int)

返回 z 坐標

func (*Node) ToPos

func (n *Node) ToPos(x, y float64) (float64, float64)

坐標轉 相對坐標

func (*Node) ToScreenPos

func (n *Node) ToScreenPos(x, y float64) (float64, float64)

坐標轉 屏幕坐標

type RendererFlip

type RendererFlip sdl.RendererFlip

type UiBase

type UiBase struct {
	Node
	// contains filtered or unexported fields
}

func (*UiBase) GetEventCallback

func (u *UiBase) GetEventCallback(evt string) UiCallBack

返回 事件回調 函數

func (*UiBase) SetEventCallback

func (u *UiBase) SetEventCallback(evt string, callback UiCallBack)

設置 事件回調

type UiBtuuon

type UiBtuuon struct {
	UiBase
	// contains filtered or unexported fields
}

按鈕

func NewUiBtuuon

func NewUiBtuuon(normal, down *sdl.Texture) *UiBtuuon

func (*UiBtuuon) OnEvent

func (u *UiBtuuon) OnEvent(evt sdl.Event) bool

type UiCallBack

type UiCallBack func(ui IUi, info interface{})

ui事件 回調

type UiDialog

type UiDialog struct {
	UiBase
	// contains filtered or unexported fields
}

對話框 對於模式對話框 需要自己保證 其 z 值爲最大

func NewUiDialog

func NewUiDialog(move bool, model bool) *UiDialog

func (*UiDialog) IsModel

func (u *UiDialog) IsModel() bool

func (*UiDialog) IsMove

func (u *UiDialog) IsMove() bool

func (*UiDialog) OnEvent

func (u *UiDialog) OnEvent(evt sdl.Event) bool

func (*UiDialog) SetModel

func (u *UiDialog) SetModel(yes bool)

func (*UiDialog) SetMove

func (u *UiDialog) SetMove(yes bool)

type UiLabel

type UiLabel struct {
	UiBase
	// contains filtered or unexported fields
}

靜態文本框

func NewUiLabel

func NewUiLabel() (*UiLabel, error)

func NewUiLabelFont

func NewUiLabelFont(fFile string, fSize int) (*UiLabel, error)

func (*UiLabel) Destroy

func (u *UiLabel) Destroy()

func (*UiLabel) Draw

func (u *UiLabel) Draw(renderer *sdl.Renderer, duration time.Duration)

func (*UiLabel) GetColor

func (u *UiLabel) GetColor() sdl.Color

func (*UiLabel) GetPadding

func (u *UiLabel) GetPadding() int32

func (*UiLabel) GetValue

func (u *UiLabel) GetValue() string

func (*UiLabel) SetColor

func (u *UiLabel) SetColor(color sdl.Color)

func (*UiLabel) SetPadding

func (u *UiLabel) SetPadding(pading int32)

func (*UiLabel) SetValue

func (u *UiLabel) SetValue(val string)

type UiLineEdit

type UiLineEdit struct {
	UiBase
	// contains filtered or unexported fields
}

單行編輯框

func NewUiLineEdit

func NewUiLineEdit() (*UiLineEdit, error)

func NewUiLineEditFont

func NewUiLineEditFont(fFile string, fSize int) (*UiLineEdit, error)

func (*UiLineEdit) Destroy

func (u *UiLineEdit) Destroy()

func (*UiLineEdit) Draw

func (u *UiLineEdit) Draw(renderer *sdl.Renderer, duration time.Duration)

func (*UiLineEdit) GetBackgroundColor

func (u *UiLineEdit) GetBackgroundColor() sdl.Color

func (*UiLineEdit) GetChartRGB

func (u *UiLineEdit) GetChartRGB() (r uint8, g uint8, b uint8)

func (*UiLineEdit) GetColor

func (u *UiLineEdit) GetColor() sdl.Color

func (*UiLineEdit) GetMax

func (u *UiLineEdit) GetMax() int

設置 允許輸入的最大字符數

func (*UiLineEdit) GetPwdChar

func (u *UiLineEdit) GetPwdChar() string

返回 密碼框 顯示 文本

func (*UiLineEdit) GetValue

func (u *UiLineEdit) GetValue() string

func (*UiLineEdit) IsPwd

func (u *UiLineEdit) IsPwd() bool

返回 是否 是密碼框

func (*UiLineEdit) OnEvent

func (u *UiLineEdit) OnEvent(evt sdl.Event) bool

func (*UiLineEdit) ReplaceRune

func (u *UiLineEdit) ReplaceRune(arrs []rune) error

將 選中項 以 []rune 替換

func (*UiLineEdit) ReplaceStr

func (u *UiLineEdit) ReplaceStr(str string) error

將 選中項 以 字符串 替換

func (*UiLineEdit) Select

func (u *UiLineEdit) Select(begin, end int)

光標 選擇

func (*UiLineEdit) SelectAll

func (u *UiLineEdit) SelectAll()

光標 全選

func (*UiLineEdit) SetBackgroundColor

func (u *UiLineEdit) SetBackgroundColor(color sdl.Color)

func (*UiLineEdit) SetChartRGB

func (u *UiLineEdit) SetChartRGB(r, g, b uint8)

func (*UiLineEdit) SetColor

func (u *UiLineEdit) SetColor(color sdl.Color)

func (*UiLineEdit) SetMax

func (u *UiLineEdit) SetMax(max int)

返回 允許輸入的最大字符數

func (*UiLineEdit) SetPwd

func (u *UiLineEdit) SetPwd(yes bool)

設置 是否 是密碼框

func (*UiLineEdit) SetPwdChar

func (u *UiLineEdit) SetPwdChar(c string)

設置 密碼框 顯示 文本

func (*UiLineEdit) SetValue

func (u *UiLineEdit) SetValue(text string)

Jump to

Keyboard shortcuts

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