func InitGL(width, height, msaa int, fullscreen bool) { C.SDL_Init(C.SDL_INIT_VIDEO) C.SDL_VideoInit( /*nil*/ C.SDL_GetVideoDriver(0)) C.SDL_GL_SetAttribute(C.SDL_GL_CONTEXT_MAJOR_VERSION, 3) C.SDL_GL_SetAttribute(C.SDL_GL_CONTEXT_MINOR_VERSION, 2) if msaa != 0 { C.SDL_GL_SetAttribute(C.SDL_GL_MULTISAMPLEBUFFERS, 1) C.SDL_GL_SetAttribute(C.SDL_GL_MULTISAMPLESAMPLES, C.int(msaa)) } C.SDL_GL_SetAttribute(C.SDL_GL_DEPTH_SIZE, 16) //win = C.SDL_CreateWindow(nil, C.SDL_WINDOWPOS_CENTERED, C.SDL_WINDOWPOS_CENTERED,C.int(width), C.int(height), C.SDL_WINDOW_OPENGL) if fullscreen { win = C.SDL_CreateWindow(nil, C.SDL_WINDOWPOS_CENTERED, C.SDL_WINDOWPOS_CENTERED, C.int(width), C.int(height), C.SDL_WINDOW_OPENGL|C.SDL_WINDOW_FULLSCREEN_DESKTOP) } else { win = C.SDL_CreateWindow(nil, C.SDL_WINDOWPOS_CENTERED, C.SDL_WINDOWPOS_CENTERED, C.int(width), C.int(height), C.SDL_WINDOW_OPENGL) } C.SDL_ShowWindow(win) wat := C.SDL_GL_CreateContext(win) fmt.Println(C.GoString(C.SDL_GetVideoDriver(0))) C.SDL_GL_MakeCurrent(win, wat) //C.SDL_GL_SetSwapInterval(1) C.glEnable(C.GL_DEPTH_TEST) C.glDepthFunc(C.GL_LEQUAL) C.glClearColor(0.3, 0.5, 1, 0) C.glClear(C.GL_COLOR_BUFFER_BIT | C.GL_DEPTH_BUFFER_BIT) printerr("failed to initialize openGL") }
func CreateWindow(title string, x int, y int, w int, h int, flags uint32) *Window { _title := C.CString(title) defer C.free(unsafe.Pointer(_title)) var window = C.SDL_CreateWindow(_title, C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) return (*Window)(unsafe.Pointer(window)) }
// NewWindow returns a new window. func NewWindow(title string, w, h int) *Window { win := &Window{ events: make(chan interface{}, eventChanSize), imgs: make(map[string]texture), } do(func() { ctitle := C.CString(title) defer C.free(unsafe.Pointer(ctitle)) x, y := C.SDL_WINDOWPOS_UNDEFINED, C.SDL_WINDOWPOS_UNDEFINED flags := C.SDL_WINDOW_SHOWN | C.SDL_WINDOW_OPENGL win.win = C.SDL_CreateWindow(ctitle, C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) if win.win == nil { panic(sdlError()) } win.rend = C.SDL_CreateRenderer(win.win, 0, C.SDL_RENDERER_ACCELERATED) if win.rend == nil { panic(sdlError()) } if C.SDL_SetRenderDrawBlendMode(win.rend, C.SDL_BLENDMODE_BLEND) < 0 { panic(sdlError()) } win.id = windowID(C.SDL_GetWindowID(win.win)) windows[win.id] = win }) return win }
// CreateWindow (https://wiki.libsdl.org/SDL_CreateWindow) func CreateWindow(title string, x int, y int, w int, h int, flags uint32) (*Window, error) { _title := C.CString(title) var _window = C.SDL_CreateWindow(_title, C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) if _window == nil { return nil, GetError() } return (*Window)(unsafe.Pointer(_window)), nil }
// CreateWindow creates a window with the specified position, dimensions, and // flags. // // Note: Destroy must be called when finished using the window. func CreateWindow(title string, x, y WindowPos, w, h int, flags WindowFlag) (win *Window, err error) { win = new(Window) win.cWin = C.SDL_CreateWindow(C.CString(title), C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) if win.cWin == nil { return nil, getError() } return win, nil }
func CreateWindow(title string, x, y int, h, w int, flags uint32) *Window { GlobalMutex.Lock() defer GlobalMutex.Unlock() window := C.SDL_CreateWindow(C.CString(title), C.int(x), C.int(y), C.int(h), C.int(w), C.Uint32(flags)) return wrapWindow(window) }
func CreateWindow(_title string, _x int, _y int, _width int, _height int, _flags uint32) (*Window, error) { ctitle := C.CString(_title) var window *C.SDL_Window = C.SDL_CreateWindow(ctitle, C.int(_x), C.int(_y), C.int(_width), C.int(_height), C.Uint32(_flags)) C.free(unsafe.Pointer(ctitle)) if window == nil { return nil, errors.New(GetError()) } return &Window{window}, nil }
func CreateWindow(_title string, _width int, _height int) (*Window, string) { ctitle := C.CString(_title) var window *C.SDL_Window = C.SDL_CreateWindow(ctitle, C.SDL_WINDOWPOS_CENTERED, C.SDL_WINDOWPOS_CENTERED, C.int(_width), C.int(_height), C.SDL_WINDOW_SHOWN|C.SDL_WINDOW_OPENGL) C.free(unsafe.Pointer(ctitle)) if window == nil { return nil, GetError() } return &Window{window}, "" }
func CreateWindow(title string, x, y, w, h int, flags WindowFlags) (*Window, error) { ctitle := C.CString(title) defer C.free(unsafe.Pointer(ctitle)) win := C.SDL_CreateWindow( ctitle, C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags), ) if win == nil { return nil, getError() } return &Window{win}, nil }
func NewWindow(title string, xpos, ypos, w, h int) (*Window, error) { cs := C.CString(title) defer C.free(unsafe.Pointer(cs)) ww := C.SDL_CreateWindow(cs, C.int(xpos), C.int(ypos), C.int(w), C.int(h), 0) if ww == nil { return nil, sdlerr() } r := C.SDL_CreateRenderer(ww, -1, C.SDL_RENDERER_ACCELERATED|C.SDL_RENDERER_PRESENTVSYNC) if r == nil { return nil, sdlerr() } win := &Window{ww, r} runtime.SetFinalizer(win, freewin) return win, nil }
func LaunchGame(title string, width, height int) error { // Convert the title to a c-string ctitle := C.CString(title) defer C.free(unsafe.Pointer(ctitle)) // Initialize SDL if C.SDL_Init(C.SDL_INIT_VIDEO) < 0 { return errors.New("SDL_Init") } defer C.SDL_Quit() // Create the window window = C.SDL_CreateWindow(ctitle, C.SDL_WINDOWPOS_UNDEFINED, C.SDL_WINDOWPOS_UNDEFINED, C.int(width), C.int(height), C.SDL_WINDOW_SHOWN) if window == nil { return errors.New("SDL_CreateWindow") } defer C.SDL_DestroyWindow(window) // Create the renderer renderer = C.SDL_CreateRenderer(window, C.int(-1), C.SDL_RENDERER_ACCELERATED|C.SDL_RENDERER_PRESENTVSYNC) if renderer == nil { return errors.New("SDL_CreateRenderer") } defer C.SDL_DestroyRenderer(renderer) // Initialize image support if success := int(C.initIMG()); success == 0 { return errors.New("IMG_Init") } EventInit() C.gameLoop() destroyTextures() return nil }
func main() { runtime.GOMAXPROCS(32) // sdl C.SDL_Init(C.SDL_INIT_AUDIO | C.SDL_INIT_VIDEO | C.SDL_INIT_TIMER) defer C.SDL_Quit() runtime.LockOSThread() window := C.SDL_CreateWindow(C.CString("play"), 0, 0, 1680, 1050, C.SDL_WINDOW_BORDERLESS|C.SDL_WINDOW_RESIZABLE|C.SDL_WINDOW_MAXIMIZED|C.SDL_WINDOW_OPENGL) if window == nil { fatalSDLError() } defer C.SDL_DestroyWindow(window) C.SDL_DisableScreenSaver() renderer := C.SDL_CreateRenderer(window, -1, C.SDL_RENDERER_ACCELERATED) if renderer == nil { fatalSDLError() } defer C.SDL_DestroyRenderer(renderer) var width, height C.int C.SDL_GetWindowSize(window, &width, &height) texture := C.SDL_CreateTexture(renderer, C.SDL_PIXELFORMAT_YV12, C.SDL_TEXTUREACCESS_STREAMING, width, height) if texture == nil { fatalSDLError() } defer C.SDL_DestroyTexture(texture) // sdl ttf if C.TTF_Init() == C.int(-1) { log.Fatal("sdl ttf init failed") } defer C.TTF_Quit() font := C.TTF_OpenFont(C.CString("/home/reus/font.ttf"), 32) if font == nil { fatalTTFError() } defer C.TTF_CloseFont(font) // open decoder decoder, err := NewDecoder(os.Args[1]) if err != nil { log.Fatal(err) } if len(decoder.AudioStreams) == 0 || len(decoder.VideoStreams) == 0 { log.Fatal("no video or audio") } defer decoder.Close() // audio aCodecCtx := decoder.AudioStreams[0].codec audioStream := setupAudioOutput(int(aCodecCtx.sample_rate), int(aCodecCtx.channels), decoder) // call closure in sdl thread callEventCode := C.SDL_RegisterEvents(1) callbacks := make(chan func(Env), 1024) call := func(f func(env Env)) { var event C.SDL_Event var userevent C.SDL_UserEvent userevent._type = C.SDL_USEREVENT userevent.code = C.Sint32(callEventCode) C.set_userevent(&event, userevent) callbacks <- f C.SDL_PushEvent(&event) } // show fps nFrames := 0 var fpsTexture *C.SDL_Texture var fpsColor C.SDL_Color var fpsSrc, fpsDst C.SDL_Rect fpsColor.r = 255 fpsColor.g = 255 fpsColor.b = 255 fpsColor.a = 0 go func() { for _ = range time.NewTicker(time.Second * 1).C { call(func(env Env) { cText := C.CString(fmt.Sprintf("%d", nFrames)) sur := C.TTF_RenderUTF8_Blended(font, cText, fpsColor) fpsSrc.w = sur.w fpsSrc.h = sur.h fpsDst.w = sur.w fpsDst.h = sur.h C.SDL_DestroyTexture(fpsTexture) fpsTexture = C.SDL_CreateTextureFromSurface(env.renderer, sur) C.SDL_FreeSurface(sur) C.free(unsafe.Pointer(cText)) nFrames = 0 }) } }() // render go func() { for { frame := <-decoder.timedFrames nFrames++ call(func(env Env) { C.SDL_UpdateYUVTexture(env.texture, nil, (*C.Uint8)(unsafe.Pointer(frame.data[0])), frame.linesize[0], (*C.Uint8)(unsafe.Pointer(frame.data[1])), frame.linesize[1], (*C.Uint8)(unsafe.Pointer(frame.data[2])), frame.linesize[2]) C.SDL_RenderCopy(env.renderer, env.texture, nil, nil) C.SDL_RenderCopy(env.renderer, fpsTexture, &fpsSrc, &fpsDst) C.SDL_RenderPresent(env.renderer) decoder.RecycleFrame(frame) }) } }() // start decode decoder.Start(decoder.VideoStreams[0], decoder.AudioStreams[0], width, height) ret := C.Pa_StartStream(audioStream) if ret != C.paNoError { fatalPAError(ret) } // main loop var ev C.SDL_Event env := Env{ window: window, renderer: renderer, texture: texture, } for { if C.SDL_WaitEvent(&ev) == C.int(0) { fatalSDLError() } switch C.get_event_type(&ev) { case C.SDL_QUIT: os.Exit(0) case C.SDL_KEYDOWN: key := C.get_event_key(&ev) switch key.keysym.sym { case C.SDLK_q: // quit os.Exit(0) case C.SDLK_f: // forward decoder.Seek(time.Second * 60) } case C.SDL_USEREVENT: if C.get_userevent_code(&ev) == callEventCode { (<-callbacks)(env) } } } }
func CreateWindow(title string, x, y int, w, h int, flags uint32) *Window { window := C.SDL_CreateWindow(C.CString(title), C.int(x), C.int(y), C.int(w), C.int(h), C.Uint32(flags)) return wrapWindow(window) }