本文整理汇总了Golang中github.com/veandco/go-sdl2/sdl.Delay函数的典型用法代码示例。如果您正苦于以下问题:Golang Delay函数的具体用法?Golang Delay怎么用?Golang Delay使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Delay函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
if err := sdl.Init(sdl.INIT_AUDIO); err != nil {
log.Println(err)
return
}
if err := mix.Init(mix.INIT_MP3); err != nil {
log.Println(err)
return
}
defer sdl.Quit()
defer mix.Quit()
defer mix.CloseAudio()
if err := mix.OpenAudio(22050, mix.DEFAULT_FORMAT, 2, 4096); err != nil {
log.Println(err)
return
}
if music, err := mix.LoadMUS("test.mp3"); err != nil {
log.Println(err)
} else if err = music.Play(1); err != nil {
log.Println(err)
} else {
sdl.Delay(5000)
music.Free()
}
}
示例2: DelayToNextFrame
// DelayToNextFrame waits until it's time to do the next event/render loop
func (g *GameManager) DelayToNextFrame() {
curTime := sdl.GetTicks()
if g.prevFrameTime == 0 {
if curTime >= g.FrameDelay {
g.prevFrameTime = curTime - g.FrameDelay
}
}
diff := curTime - g.prevFrameTime
if g.FrameDelay > diff {
frameDelayUnder := g.FrameDelay - diff
// we have not yet exceeded one frame, so we need to sleep
//fmt.Printf("Under: %d %d %d %d\n", curTime, g.prevFrameTime, diff, frameDelayUnder)
sdl.Delay(frameDelayUnder)
} else {
//frameDelayOver := diff - g.FrameDelay
//fmt.Printf("Over: %d %d %d %d\n", curTime, g.prevFrameTime, diff, frameDelayOver)
// we have exceeded one frame, so no sleep
// TODO sleep less in the future to make up for it?
}
g.prevFrameTime = curTime
}
示例3: StateMachine
func StateMachine(coreFunction Update, updateFunctionsToStartWith []Update) {
UpdateFunctions = updateFunctionsToStartWith
functionCount := len(updateFunctionsToStartWith)
states := make([]int, functionCount)
for i := 0; i < functionCount; i++ {
states[i] = 0
}
coreState := coreFunction(0)
for coreState != -1 && functionCount != 0 {
for i := 0; i < functionCount; i++ {
states[i] = UpdateFunctions[i](states[i])
}
newCount := len(UpdateFunctions)
states = append(states, make([]int, newCount-functionCount)...)
for i := functionCount; i < newCount; i++ {
states[i] = 0
}
for i := functionCount - 1; i >= 0; i-- {
if states[i] == -1 {
UpdateFunctions = append(UpdateFunctions[:i], UpdateFunctions[i+1:]...)
states = append(states[:i], states[i+1:]...)
}
}
functionCount = len(UpdateFunctions)
sdl.Delay(15)
coreState = coreFunction(coreState)
}
}
示例4: render
// Render updates the screen, based on the new positions of the bats and the ball.
func render() {
var fps uint32
fps = 60
var delay uint32
delay = 1000 / fps
var frameStart uint32
frameStart = sdl.GetTicks()
renderer.Clear()
renderMyBat()
renderComputersBat()
renderScore()
// if the game is over render the gameOver graphic
if gameOver == true {
renderGameOver()
} else {
// otherwise we need to draw the ball
renderBall()
}
// Show the game window window.
renderer.Present()
var frameTime uint32
frameTime = sdl.GetTicks() - frameStart
if frameTime < delay {
sdl.Delay(delay - frameTime)
}
}
示例5: Update
// Update is
func (T *TextWidget) Update() {
w := T.Geometry.Width
// h := T.Geometry.Height
var r sdl.Rect
T.Clear()
for i, line := range T.Content {
r = sdl.Rect{
X: T.Padding.Left,
Y: T.Padding.Top + int32(i*T.LineHeight),
W: int32(w),
H: int32(T.LineHeight),
}
T.DrawColoredText(line.Content,
&r, "foreground", "default",
line.Rules,
)
}
T.App.DrawMode()
T.drawCursor()
// T.Renderer.Clear()
// T.Renderer.Present()
sdl.Delay(5)
T.App.Window.UpdateSurface()
}
示例6: main
func main() {
rom, err := LoadROM(os.Args[1])
if err != nil {
panic(err)
}
frameDelay := 1000 / frameRate
window := sdl.CreateWindow(
"Go NES",
sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED,
256,
240,
sdl.WINDOW_SHOWN,
)
if window == nil {
panic(sdl.GetError())
}
defer window.Destroy()
controller := NewController()
ppu := NewPPU(window, NewVRAM(rom.Mapper()))
cpu := NewCPU(NewMMU(rom.Mapper(), ppu, controller), ppu)
ticksPerFrame := uint32(341 * 262)
for {
expectedTicks := sdl.GetTicks() + frameDelay
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event := event.(type) {
case *sdl.KeyDownEvent:
controller.SetKeyState(event.Keysym.Sym, true)
case *sdl.KeyUpEvent:
controller.SetKeyState(event.Keysym.Sym, false)
case *sdl.QuitEvent:
return
}
}
ppuTicks := uint32(0)
for ppuTicks < ticksPerFrame {
ppuTicks += ppu.Tick(cpu.Tick())
}
actualTicks := sdl.GetTicks()
if actualTicks < expectedTicks {
sdl.Delay(expectedTicks - actualTicks)
}
}
}
示例7: main
func main() {
var window *sdl.Window
var renderer *sdl.Renderer
var event sdl.Event
var running bool
window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
winWidth, winHeight, sdl.WINDOW_SHOWN)
if window == nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError())
os.Exit(1)
}
renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if renderer == nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError())
os.Exit(2)
}
var peepArray []sdl.Event = make([]sdl.Event, 5)
running = true
for running {
sdl.PumpEvents()
numEventsRetrieved := sdl.PeepEvents(peepArray, sdl.PEEKEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT)
if numEventsRetrieved < 0 {
fmt.Printf("PeepEvents error: %s\n", sdl.GetError())
} else {
for i := 0; i < numEventsRetrieved; i++ {
fmt.Printf("Event Peeked Value: %v\n", peepArray[i]) // primitive printing of event
}
}
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
running = false
case *sdl.MouseMotionEvent:
fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
case *sdl.MouseButtonEvent:
fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
case *sdl.MouseWheelEvent:
fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y)
case *sdl.KeyUpEvent:
fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
}
}
sdl.Delay(1000 / 30)
}
renderer.Destroy()
window.Destroy()
}
示例8: run
func run() int {
var window *sdl.Window
var renderer *sdl.Renderer
var points []sdl.Point
var rect sdl.Rect
var rects []sdl.Rect
window, err := sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
winWidth, winHeight, sdl.WINDOW_SHOWN)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
return 1
}
defer window.Destroy()
renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
return 2
}
defer renderer.Destroy()
renderer.Clear()
renderer.SetDrawColor(255, 255, 255, 255)
renderer.DrawPoint(150, 300)
renderer.SetDrawColor(0, 0, 255, 255)
renderer.DrawLine(0, 0, 200, 200)
points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
renderer.SetDrawColor(255, 255, 0, 255)
renderer.DrawLines(points)
rect = sdl.Rect{300, 0, 200, 200}
renderer.SetDrawColor(255, 0, 0, 255)
renderer.DrawRect(&rect)
rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
renderer.SetDrawColor(0, 255, 255, 255)
renderer.DrawRects(rects)
rect = sdl.Rect{250, 250, 200, 200}
renderer.SetDrawColor(0, 255, 0, 255)
renderer.FillRect(&rect)
rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}}
renderer.SetDrawColor(255, 0, 255, 255)
renderer.FillRects(rects)
renderer.Present()
sdl.Delay(2000)
return 0
}
示例9: doesMyCodeWork2
func doesMyCodeWork2() {
startTime := time.Now()
for i := 0; i != -1; i++ {
fmt.Print(i)
fmt.Print(": ")
timeDiff := time.Now().Second() + 60*time.Now().Minute() - startTime.Second() - startTime.Minute()*60
fmt.Println(timeDiff)
sdl.Delay(1000)
}
}
示例10: tick
func (g *Game) tick() {
//var ticks uint32
g.defaultSleep = 300 // this param will control the speed somehow TODO Fix this
sdl.Delay(1)
if sdl.GetTicks()%g.defaultSleep == 0 {
g.heartbeat(sdl.GetTicks())
}
}
示例11: main
func main() {
for game.loop {
get_event()
logic()
renderer.Clear()
render()
renderer.Present()
sdl.Delay(delay)
}
}
示例12: Pause
// Pause pauses execution of the game for the specified number of ticks.
// During the pause period nothing will happen. All input will be ignored
// and the window will not redraw.
//
// The duration of the pause must be specified in ticks in a variable
// of type int64
//
// Pause will panic if:
//
// 1. The toolbox has not been initialised.
//
// 2. The numnber of ticks is negative.
func Pause(numberOfTicks int64) {
if !initialised {
// this stops execution here, so ne need for an else after the if
panic(notInitialisedMessage)
}
if numberOfTicks < 0 {
panic("Cannot pause. The numberOfTicks is negative.")
}
sdl.Delay(uint32(numberOfTicks))
}
示例13: DispatchKeys
//DispatchKeys interface method
func (sw *Switcher) DispatchKeys(t *sdl.KeyDownEvent) int {
app := sw.App
T := app.Widget
key := sdl.GetScancodeName(t.Keysym.Scancode)
if (key == "N" && t.Keysym.Mod == 64) || key == "Down" {
T.SetLine(sw.Selected, sw.getLine(sw.Selected, sw.Clients[sw.Selected], false))
if sw.Selected < len(sw.Clients)-1 {
sw.Selected++
} else {
sw.Selected = 0
}
T.SetLine(sw.Selected, sw.getLine(sw.Selected, sw.Clients[sw.Selected], true))
return 1
}
if (key == "P" && t.Keysym.Mod == 64) || key == "Up" {
T.SetLine(sw.Selected, sw.getLine(sw.Selected, sw.Clients[sw.Selected], false))
if sw.Selected > 0 {
sw.Selected--
} else {
sw.Selected = len(sw.Clients) - 1
}
T.SetLine(sw.Selected, sw.getLine(sw.Selected, sw.Clients[sw.Selected], true))
return 1
}
if key == "X" && t.Keysym.Mod == 64 {
wid := sw.Clients[sw.Selected].WID
ewmh.CloseWindow(X, wid)
sdl.Delay(500)
sw.Clients = GetClients()
sw.Draw()
return 1
}
if (key == "J" && t.Keysym.Mod == 64) || key == "Return" {
wid := sw.Clients[sw.Selected].WID
ewmh.ActiveWindowReq(X, wid)
return 0
}
if strings.Index("0123456789", key) > -1 {
i, err := strconv.Atoi(key)
if err == nil && len(sw.Clients) > i {
sw.Selected = i
if t.Keysym.Mod == 64 {
wid := sw.Clients[sw.Selected].WID
ewmh.ActiveWindowReq(X, wid)
return 0
}
sw.Draw()
return 1
}
}
if t.Keysym.Sym == sdl.K_ESCAPE || t.Keysym.Sym == sdl.K_CAPSLOCK {
return 0
}
return 1
}
示例14: main
func main() {
var window *sdl.Window
var renderer *sdl.Renderer
var points []sdl.Point
var rect sdl.Rect
var rects []sdl.Rect
window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
winWidth, winHeight, sdl.WINDOW_SHOWN)
if window == nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError())
os.Exit(1)
}
renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if renderer == nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError())
os.Exit(2)
}
renderer.SetDrawColor(255, 255, 255, 255)
renderer.DrawPoint(150, 300)
renderer.SetDrawColor(0, 0, 255, 255)
renderer.DrawLine(0, 0, 200, 200)
points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
renderer.SetDrawColor(255, 255, 0, 255)
renderer.DrawLines(points)
rect = sdl.Rect{300, 0, 200, 200}
renderer.SetDrawColor(255, 0, 0, 255)
renderer.DrawRect(&rect)
rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
renderer.SetDrawColor(0, 255, 255, 255)
renderer.DrawRects(rects)
rect = sdl.Rect{250, 250, 200, 200}
renderer.SetDrawColor(0, 255, 0, 255)
renderer.FillRect(&rect)
rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}}
renderer.SetDrawColor(255, 0, 255, 255)
renderer.FillRects(rects)
renderer.Present()
sdl.Delay(2000)
renderer.Destroy()
window.Destroy()
}
示例15: main
func main() {
js, err := GetJs()
if err != nil {
panic(err)
}
for {
axes := GetAxes(js)
sdl.Delay(100)
log.Println(axes)
}
}