Last active
April 18, 2019 00:39
-
-
Save fopina/1a592dcac57e67cf3f5eec0e3a205768 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"fmt" | |
"math/rand" | |
"github.com/veandco/go-sdl2/sdl" | |
) | |
const ( | |
WindowTitle = "test" | |
WindowWidth = 800 | |
WindowHeight = 600 | |
) | |
func run() int { | |
var window *sdl.Window | |
var renderer *sdl.Renderer | |
var err error | |
r := rand.New(rand.NewSource(99)) | |
sdl.Do(func() { | |
window, err = sdl.CreateWindow( | |
WindowTitle, | |
0, 0, | |
WindowWidth, WindowHeight, | |
sdl.WINDOW_FULLSCREEN, | |
) | |
}) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) | |
return 1 | |
} | |
defer func() { | |
sdl.Do(func() { | |
window.Destroy() | |
}) | |
}() | |
sdl.Do(func() { | |
renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) | |
}) | |
if err != nil { | |
fmt.Fprint(os.Stderr, "Failed to create renderer: %s\n", err) | |
return 2 | |
} | |
defer func() { | |
sdl.Do(func() { | |
renderer.Destroy() | |
}) | |
}() | |
running := true | |
for running { | |
sdl.Do(func() { | |
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { | |
switch event.(type) { | |
case *sdl.KeyboardEvent: | |
running = false | |
} | |
} | |
renderer.Clear() | |
renderer.SetDrawColor(uint8(r.Int31n(255)), uint8(r.Int31n(255)), uint8(r.Int31n(255)), 0x80) | |
renderer.FillRect(&sdl.Rect{0, 0, WindowWidth, WindowHeight}) | |
renderer.Present() | |
}) | |
} | |
return 0 | |
} | |
func main() { | |
var exitcode int | |
sdl.Main(func() { | |
exitcode = run() | |
}) | |
os.Exit(exitcode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment