Esempio n. 1
0
func LoadImage(path string) *Image {
	var retval *Image
	var texture *C.SDL_Texture

	i := C.IMG_Load(C.CString(path))
	if i == nil {
		errlog.Println("Surface for", path, "loaded nil")
		return nil
	}

	if renderer == nil {
		errlog.Println("Cannot load image because renderer is nil")
		return nil
	}

	runMainOp(func() {
		texture = C.SDL_CreateTextureFromSurface(renderer, i)
	})

	if texture == nil {
		errlog.Println("Texture for", path, "loaded nil")
		return nil
	}
	retval = new(Image)
	retval.surface = texture
	C.SDL_FreeSurface(i)
	return retval
}
Esempio n. 2
0
func LoadPNG(path, name string) error {
	// Make sure this name isn't already in use
	if _, ok := Textures[name]; ok {
		return errors.New("Texture name already in use")
	}

	// Convert the path to a c-string
	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))

	// Load the surface
	surface := C.IMG_Load(cpath)
	if surface == nil {
		return errors.New("SDL_LoadBMP")
	}
	defer C.SDL_FreeSurface(surface)

	// Create the texture
	tex := C.SDL_CreateTextureFromSurface(renderer, surface)
	if tex == nil {
		return errors.New("SDL_CreateTextureFromSurface")
	}

	Textures[name] = Texture{tex: tex}

	return nil
}
Esempio n. 3
0
//Closes the window.
func CloseDisplay() {
	if screen != nil {
		close(kill)
		C.SDL_FreeSurface(screen)
		screen = nil
		C.SDL_Quit()
	}
}
Esempio n. 4
0
func NewPaletteSurface(w, h int) (s *Surface) {
	mutex.Lock()
	defer mutex.Unlock()

	ptr := C.SDL_CreateRGBSurface(0, C.int(w), C.int(h), 8, 0, 0, 0, 0)
	s = &Surface{ptr}
	runtime.SetFinalizer(s, func(s *Surface) { C.SDL_FreeSurface(s.ptr) })
	return
}
Esempio n. 5
0
func resize(img *C.SDL_Surface, width, height int) *C.SDL_Surface {
	if img.w == 0 || img.h == 0 {
		return nil
	}
	xstretch := C.double(float64(width) / float64(img.w))
	ystretch := C.double(float64(height) / float64(img.h))
	retval := C.zoomSurface(img, xstretch, ystretch, 1)
	C.SDL_FreeSurface(img)
	return retval
}
Esempio n. 6
0
// Frees (deletes) a Surface
func (screen *Surface) Free() {
	GlobalMutex.Lock()
	screen.mutex.Lock()

	C.SDL_FreeSurface(screen.cSurface)

	screen.destroy()
	if screen == currentVideoSurface {
		currentVideoSurface = nil
	}

	screen.mutex.Unlock()
	GlobalMutex.Unlock()
}
Esempio n. 7
0
func NewSurface(w, h int) (s *Surface) {
	mutex.Lock()
	defer mutex.Unlock()

	video := C.SDL_GetVideoSurface()
	ptr := C.SDL_CreateRGBSurface(
		0, C.int(w), C.int(h), C.int(video.format.BitsPerPixel),
		video.format.Rmask,
		video.format.Gmask,
		video.format.Bmask,
		video.format.Amask)
	s = &Surface{ptr}
	runtime.SetFinalizer(s, func(s *Surface) { C.SDL_FreeSurface(s.ptr) })
	return
}
Esempio n. 8
0
	path   string
	width  int
	height int
}

func (me *imageKey) String() string {
	return me.path + strconv.Itoa(me.width) + strconv.Itoa(me.height)
}

//Resizes images from imageFiles and caches them.
var images = newFlyweight(
	func(path key) interface{} {
		key := path.(*imageKey)
		tmp := C.IMG_Load(C.CString(key.path))
		i := C.SDL_DisplayFormatAlpha(tmp)
		C.SDL_FreeSurface(tmp)
		if key.width != -1 && key.height != -1 {
			if (i != nil) && (int(i.w) != key.width || int(i.h) != key.height) {
				i = resize(i, key.width, key.height)
			}
		}
		return i
	},
	func(path key, img interface{}) {
		i := img.(*C.SDL_Surface)
		C.SDL_FreeSurface(i)
	})

type Image struct {
	img *C.SDL_Surface
	key imageKey
Esempio n. 9
0
func (surface *Surface) Free() {
	C.SDL_FreeSurface(surface.cptr())
}
Esempio n. 10
0
// Frees (deletes) a Surface
func (screen *Surface) Free() { C.SDL_FreeSurface((*C.SDL_Surface)(cast(screen))) }
Esempio n. 11
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)
			}
		}
	}
}
Esempio n. 12
0
// Free frees a surface.
func (surface *Surface) Free() {
	C.SDL_FreeSurface(surface.cSurface)
}
Esempio n. 13
0
func (me *Text) Free() {
	C.SDL_FreeSurface(me.text)
}
Esempio n. 14
0
func (surface *Surface) Free() {
	_surface := (*C.SDL_Surface)(unsafe.Pointer(surface))
	C.SDL_FreeSurface(_surface)
}
Esempio n. 15
0
func (s *Surface) Release() {
	C.SDL_FreeSurface(s.Get())
}
Esempio n. 16
0
// Frees (deletes) a Surface
func (screen *Surface) Free() {
	C.SDL_FreeSurface(screen.cSurface)

	screen.destroy()
}
Esempio n. 17
0
// Frees the memory associated with the surface
func freeSurface(surface *C.SDL_Surface) {
	C.SDL_FreeSurface(surface)
}
Esempio n. 18
0
File: sdl.go Progetto: rwcarlsen/sdl
func freesurf(s *Surface) {
	C.SDL_FreeSurface(s.surf)
	C.SDL_FreeFormat(s.pixfmt)
}
Esempio n. 19
0
func (s *Surface) Free() {
	C.SDL_FreeSurface(s.c())
}
Esempio n. 20
0
func (this Surface) Free() {
	c_surface := (*C.SDL_Surface)(this.Ptr)
	C.SDL_FreeSurface(c_surface)
}