// Renderer (https://wiki.libsdl.org/SDL_RenderCopy) func (renderer *Renderer) Copy(texture *Texture, src, dst *Rect) error { _ret := C.SDL_RenderCopy(renderer.cptr(), texture.cptr(), src.cptr(), dst.cptr()) if _ret < 0 { return GetError() } return nil }
func (r *Renderer) Copy(t *Texture, src, dst *Rect) { GlobalMutex.Lock() defer GlobalMutex.Unlock() C.SDL_RenderCopy(r.cRenderer, t.cTexture, (*C.SDL_Rect)(cast(src)), (*C.SDL_Rect)(cast(dst))) }
func (renderer *Renderer) Copy(texture *Texture, srcrect, dstrect *Rect) int { _renderer := (*C.SDL_Renderer)(unsafe.Pointer(renderer)) _texture := (*C.SDL_Texture)(unsafe.Pointer(texture)) _srcrect := (*C.SDL_Rect)(unsafe.Pointer(srcrect)) _dstrect := (*C.SDL_Rect)(unsafe.Pointer(dstrect)) return (int)(C.SDL_RenderCopy(_renderer, _texture, _srcrect, _dstrect)) }
func (r *Renderer) Copy(t *Texture, sr *Rect, dr *Rect) error { if C.SDL_RenderCopy(r.c, t.c, sr.c(), dr.c()) != 0 { return getError() } return nil }
// FillString fills a string of text in the current font and draw color. X and y specify the // upper-left corner of the bounding box of the text, and the width and height of the // bounding box is returned. func (c Canvas) FillString(s string, x, y int) (width, height int) { img := c.font.draw(s, c.color()) tex := texFromImage(c.win.rend, img) defer C.SDL_DestroyTexture(tex) b := img.Bounds() w, h := b.Dx(), b.Dy() dst := image.Rect(x, y, x+w, y+h) if C.SDL_RenderCopy(c.win.rend, tex, nil, sdlRect(&dst)) < 0 { panic(sdlError()) } return w, h }
//Draws the image at the given coordinates. func DrawImage(img *Image, destX, destY, srcX, srcY, srcW, srcH int) { var src, dest C.SDL_Rect dest.x = C.int(destX) dest.y = C.int(destY) dest.w = C.int(img.Width) dest.h = C.int(img.Height) src.x = C.int(srcX) src.y = C.int(srcY) src.w = C.int(srcW) src.h = C.int(srcH) C.SDL_SetTextureAlphaMod(img.surface, 255) C.SDL_RenderCopy(renderer, img.surface, &src, &dest) }
// DrawPNG draws the image loaded from a PNG file to the canvas. // The image is drawn with the upper-left corner located at x, y. func (c Canvas) DrawPNG(path string, x, y int) { tex, ok := c.win.imgs[path] if !ok { img := loadPNG(path) tex = texture{ tex: texFromImage(c.win.rend, img), width: img.Bounds().Dx(), height: img.Bounds().Dy(), } c.win.imgs[path] = tex } dst := image.Rect(x, y, x+tex.width, y+tex.height) if C.SDL_RenderCopy(c.win.rend, tex.tex, nil, sdlRect(&dst)) < 0 { panic(sdlError()) } }
func (w *Window) Copy(tex *Texture, src, dst *Rect) error { csrc := sdlRect(src) if src != nil { defer C.free(unsafe.Pointer(csrc)) } cdst := sdlRect(dst) if dst != nil { defer C.free(unsafe.Pointer(cdst)) } status := C.SDL_RenderCopy(w.ren, tex.tex, csrc, cdst) if status != 0 { return sdlerr() } return nil }
func (t *Texture) RenderCopy(_renderer *Renderer, _srcrect *Rect, _dstrect *Rect) { src := (*C.SDL_Rect)(cast(_srcrect)) dst := (*C.SDL_Rect)(cast(_dstrect)) C.SDL_RenderCopy(_renderer.Get(), t.Get(), src, dst) }
func (r *Renderer) Copy(t *Texture, src, dst *Rect) bool { ret := C.SDL_RenderCopy(r.cRenderer, t.cTexture, (*C.SDL_Rect)(cast(src)), (*C.SDL_Rect)(cast(dst))) return int(ret) == 0 }
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 DrawTexture(tex Texture, x, y, w, h int) { dest := C.struct_SDL_Rect{x: C.int(x), y: C.int(y), w: C.int(w), h: C.int(h)} C.SDL_RenderCopy(renderer, tex.tex, nil, &dest) }
func (renderer *Renderer) Copy(texture *Texture, src, dst *Rect) int { return int(C.SDL_RenderCopy(renderer.cptr(), texture.cptr(), src.cptr(), dst.cptr())) }