Skip to content

Commit

Permalink
Moved game loop examples from README into the examples repository
Browse files Browse the repository at this point in the history
Signed-off-by: Lilis Iskandar <[email protected]>
  • Loading branch information
veeableful committed Mar 26, 2024
1 parent 1814a4f commit 74189f8
Showing 1 changed file with 1 addition and 188 deletions.
189 changes: 1 addition & 188 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,194 +199,7 @@ func main() {
}
```

There are two ways a game might be running: one that updates on user input and one that updates regardless of user input. The following example updates on user input:
```go
package main

import "github.com/veandco/go-sdl2/sdl"

const (
TITLE = "README"
WIDTH = 800
HEIGHT = 600
)

var (
playerX, playerY = int32(WIDTH / 2), int32(HEIGHT / 2)
)

func main() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
defer sdl.Quit()

window, err := sdl.CreateWindow(TITLE, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer window.Destroy()

surface, err := window.GetSurface()
if err != nil {
panic(err)
}

// Draw initial frame
draw(window, surface)

for event := sdl.WaitEvent(); event != nil; event = sdl.WaitEvent() {
switch t := event.(type) {
case sdl.QuitEvent: // NOTE: Please use `*sdl.QuitEvent` for `v0.4.x` (current version).
println("Quitting..")
return
case sdl.KeyboardEvent:
if t.State == sdl.RELEASED {
if t.Keysym.Sym == sdl.K_LEFT {
playerX -= 4
} else if t.Keysym.Sym == sdl.K_RIGHT {
playerX += 4
}
if t.Keysym.Sym == sdl.K_UP {
playerY -= 4
} else if t.Keysym.Sym == sdl.K_DOWN {
playerY += 4
}
if playerX < 0 {
playerX = WIDTH
} else if playerX > WIDTH {
playerX = 0
}
if playerY < 0 {
playerY = HEIGHT
} else if playerY > HEIGHT {
playerY = 0
}
draw(window, surface)
}
break
}
}
}

func draw(window *sdl.Window, surface *sdl.Surface) {
// Clear surface
surface.FillRect(nil, 0)

// Draw on the surface
rect := sdl.Rect{playerX, playerY, 4, 4}
colour := sdl.Color{R: 255, G: 0, B: 255, A: 255} // purple
pixel := sdl.MapRGBA(surface.Format, colour.R, colour.G, colour.B, colour.A)
surface.FillRect(&rect, pixel)

window.UpdateSurface()
}
```

And this one updates many times per second regardless of user input according to the desired framerate:
```go
package main

import "github.com/veandco/go-sdl2/sdl"

const (
TITLE = "README"
WIDTH = 800
HEIGHT = 600
FRAMERATE = 60
)

var (
playerX, playerY = int32(WIDTH / 2), int32(HEIGHT / 2)
playerVX, playerVY = int32(0), int32(0)
running = true
)

func main() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
defer sdl.Quit()

window, err := sdl.CreateWindow(TITLE, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer window.Destroy()

surface, err := window.GetSurface()
if err != nil {
panic(err)
}

for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
handleEvent(event)
}

loopTime := loop(surface)
window.UpdateSurface()

delay := (1000 / FRAMERATE) - loopTime
sdl.Delay(delay)
}
}

func handleEvent(event sdl.Event) {
switch t := event.(type) {
case sdl.QuitEvent: // NOTE: Please use `*sdl.QuitEvent` for `v0.4.x` (current version).
println("Quitting..")
running = false
break
case sdl.KeyboardEvent:
if t.State == sdl.RELEASED {
if t.Keysym.Sym == sdl.K_LEFT {
playerVX -= 1
} else if t.Keysym.Sym == sdl.K_RIGHT {
playerVX += 1
}
if t.Keysym.Sym == sdl.K_UP {
playerVY -= 1
} else if t.Keysym.Sym == sdl.K_DOWN {
playerVY += 1
}
}
break
}
}

func loop(surface *sdl.Surface) (loopTime uint32) {
// Get time at the start of the function
startTime := sdl.GetTicks()

// Update player position
playerX += playerVX
playerY += playerVY
if playerX < 0 {
playerX = WIDTH
} else if playerX > WIDTH {
playerX = 0
}
if playerY < 0 {
playerY = HEIGHT
} else if playerY > HEIGHT {
playerY = 0
}

// Clear surface
surface.FillRect(nil, 0)

// Draw on the surface
rect := sdl.Rect{playerX, playerY, 4, 4}
colour := sdl.Color{R: 255, G: 0, B: 255, A: 255} // purple
pixel := sdl.MapRGBA(surface.Format, colour.R, colour.G, colour.B, colour.A)
surface.FillRect(&rect, pixel)

// Calculate time passed since start of the function
endTime := sdl.GetTicks()
return endTime - startTime
}
```
There are two ways a game might be running: one that updates on user input using `sdl.WaitEvent()` and one that updates regardless of user input using `sdl.PollEvent()`. You can check the examples of those two loops [here](https://github.com/veandco/go-sdl2-examples/tree/master/examples/gameloop).

For more runnable examples, see https://github.com/veandco/go-sdl2-examples. You can run any of the `.go` files with `go run`.

Expand Down

0 comments on commit 74189f8

Please sign in to comment.