examples: add shaped-window example

Signed-off-by: Lilis Iskandar <lilis@veand.co>
This commit is contained in:
Lilis Iskandar 2023-02-04 11:51:51 +08:00
parent 9f8a6a76d8
commit 7120350bf7
No known key found for this signature in database
GPG Key ID: 7EE2F0C4E86662C6
3 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,7 @@
module shaped-window
go 1.20
require github.com/veandco/go-sdl2 v0.4.30
replace github.com/veandco/go-sdl2 v0.4.30 => github.com/TooManySugar/go-sdl2 v0.0.0-20230201111233-54995b38d528

View File

@ -0,0 +1,2 @@
github.com/TooManySugar/go-sdl2 v0.0.0-20230201111233-54995b38d528 h1:0h3DcRpKTWP9WtAPhNKEt1TSi3Ra3vCDEHDIA8n3rzw=
github.com/TooManySugar/go-sdl2 v0.0.0-20230201111233-54995b38d528/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=

View File

@ -0,0 +1,120 @@
package main
import (
"fmt"
"image"
"image/png"
"os"
"github.com/veandco/go-sdl2/sdl"
)
func fatal(msgs ...interface{}) {
fmt.Fprint(os.Stderr, msgs...)
os.Exit(1)
}
func main() {
reader, err := os.Open("../../assets/test.png")
if err != nil {
fatal(err)
}
defer reader.Close()
img, err := png.Decode(reader)
if err != nil {
fatal(err)
}
imgNRGBA, ok := img.(*image.NRGBA)
if !ok {
fatal("failed to cast img to image.NRGBA")
}
bounds := imgNRGBA.Bounds()
var width, height int32
width = int32(bounds.Max.X - bounds.Min.X)
height = int32(bounds.Max.Y - bounds.Min.Y)
surf, err := sdl.CreateRGBSurfaceWithFormat(0, width, height, 32, uint32(sdl.PIXELFORMAT_ABGR8888))
if err != nil {
fatal(err)
}
defer surf.Free()
surf.Lock()
if len(surf.Pixels()) != len(imgNRGBA.Pix) {
fatal("surface and image had different pixel size")
}
copy(surf.Pixels(), imgNRGBA.Pix)
surf.Unlock()
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
fatal(err)
}
defer sdl.Quit()
window, err := sdl.CreateShapedWindow(
"gopher",
uint32(sdl.WINDOWPOS_CENTERED), uint32(sdl.WINDOWPOS_CENTERED),
uint32(width), uint32(height),
sdl.WINDOW_SHOWN)
if err != nil {
fatal(err)
}
defer window.Destroy()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_SOFTWARE)
if err != nil {
fatal(err)
}
defer renderer.Destroy()
shmp := sdl.ShapeModeBinarizeAlpha{1 << 6}
window.SetShape(surf, shmp)
texture, err := renderer.CreateTextureFromSurface(surf)
if err != nil {
fatal(err)
}
defer texture.Destroy()
renderer.Copy(texture, nil, nil)
renderer.Present()
eventLoop:
for {
event := sdl.WaitEvent()
if event == nil {
break eventLoop
}
switch e := event.(type) {
case *sdl.QuitEvent:
fmt.Println("Close")
break eventLoop
case *sdl.MouseButtonEvent:
if e.Button == sdl.BUTTON_LEFT && e.State == sdl.PRESSED {
fmt.Println("LMB Click")
break eventLoop
}
if e.Button == sdl.BUTTON_RIGHT && e.State == sdl.PRESSED {
fmt.Println("RMB Click")
fmt.Println("window.IsShaped():", window.IsShaped())
wsm, ierr := window.GetShapeMode()
fmt.Println("window.GetShapeMode():", wsm, ierr)
}
}
}
fmt.Println("Quit")
}