Created
January 15, 2018 15:05
-
-
Save jackmott/064e670a546abc62b633bf70efce64b5 to your computer and use it in GitHub Desktop.
Games With Go EP06 OSX fix
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 | |
// Experiment! draw some crazy stuff! | |
// Gist it next week and I'll show it off on stream | |
import ( | |
"fmt" | |
"github.com/veandco/go-sdl2/sdl" | |
) | |
const winWidth, winHeight int = 800, 600 | |
type color struct { | |
r, g, b byte | |
} | |
func setPixel(x, y int, c color, pixels []byte) { | |
index := (y*winWidth + x) * 4 | |
if index < len(pixels)-4 && index >= 0 { | |
pixels[index] = c.r | |
pixels[index+1] = c.g | |
pixels[index+2] = c.b | |
} | |
} | |
func main() { | |
// Added after EP06 to address macosx issues | |
err := sdl.Init(sdl.INIT_EVERYTHING) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer sdl.Quit() | |
window, err := sdl.CreateWindow("Testing SDL2", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, | |
int32(winWidth), int32(winHeight), sdl.WINDOW_SHOWN) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer window.Destroy() | |
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer renderer.Destroy() | |
tex, err := renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, int32(winWidth), int32(winHeight)) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer tex.Destroy() | |
pixels := make([]byte, winWidth*winHeight*4) | |
for y := 0; y < winHeight; y++ { | |
for x := 0; x < winWidth; x++ { | |
setPixel(x, y, color{byte(x % 255), byte(y % 255), 0}, pixels) | |
} | |
} | |
tex.Update(nil, pixels, winWidth*4) | |
renderer.Copy(tex, nil, nil) | |
renderer.Present() | |
// Changd after EP 06 to address MacOSX | |
// OSX requires that you consume events for windows to open and work properly | |
for { | |
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { | |
switch event.(type) { | |
case *sdl.QuitEvent: | |
return | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment