golang调用sdl2,键盘和鼠标事件
win10 x64下测试成功,其他操作系统下不保证成功。
采用的是syscall方式,不是cgo方式。
[见地址](https://github.com/moonfdd/sdl2-go/blob/main/examples/a03-sdl-event/main.go)
代码用golang编写。代码如下:
代码语言:javascript复制package main
import (
"fmt"
"github.com/moonfdd/sdl2-go/sdl"
"github.com/moonfdd/sdl2-go/sdlcommon"
)
func main() {
sdlcommon.SetSDL2Path("SDL2.dll")
sdl.SDL_Init(sdl.SDL_INIT_VIDEO)
window := sdl.SDL_CreateWindow("微信公众号:福大大架构师每日一题", sdl.SDL_WINDOWPOS_UNDEFINED, sdl.SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl.SDL_WINDOW_OPENGL|sdl.SDL_WINDOW_RESIZABLE)
if window == nil {
fmt.Printf("Can't create window, err:%s", sdl.SDL_GetError())
return
}
renderer := window.SDL_CreateRenderer(-1, 0)
if renderer == nil {
return
}
renderer.SDL_SetRenderDrawColor(255, 0, 0, 255)
renderer.SDL_RenderClear()
renderer.SDL_RenderPresent()
var event sdl.SDL_Event
for {
event.SDL_WaitEvent()
if event.Type == sdl.SDL_KEYDOWN {
fmt.Println("键盘按下事件", event.ToSDL_KeyboardEvent().Keysym.Sym)
} else if event.Type == sdl.SDL_MOUSEBUTTONDOWN {
fmt.Println("鼠标按下事件")
} else if event.Type == sdl.SDL_QUIT {
fmt.Println("退出事件")
break
}
}
if renderer != nil {
renderer.SDL_DestroyRenderer()
}
window.SDL_DestroyWindow()
sdl.SDL_Quit()
}
执行结果如下: