Example #1
0
// Initializes SDL.
func Init(flags uint32) int {
	status := int(C.SDL_Init(C.Uint32(flags)))
	if (status != 0) && (runtime.GOOS == "darwin") && (flags&INIT_VIDEO != 0) {
		if os.Getenv("SDL_VIDEODRIVER") == "" {
			os.Setenv("SDL_VIDEODRIVER", "x11")
			status = int(C.SDL_Init(C.Uint32(flags)))
			if status != 0 {
				os.Setenv("SDL_VIDEODRIVER", "")
			}
		}
	}
	return status
}
Example #2
0
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")

}
Example #3
0
File: sdl.go Project: willemvds/sdl
func Init(flags uint32) error {
	if C.SDL_Init(C.Uint32(flags)) != 0 {
		return getError()
	}

	return nil
}
Example #4
0
func mainLoop(width, height int) {
	// SDL window must be created in the same thread where the events are
	// polled. Hence this stuff must be in a separate goroutine along with the
	// event loop.

	initFlags := int64(C.SDL_INIT_VIDEO) | int64(C.SDL_INIT_AUDIO)
	screenFlags := 0

	if C.SDL_Init(C.Uint32(initFlags)) == C.int(-1) {
		panic(getError())
	}

	screen := C.SDL_SetVideoMode(
		C.int(width), C.int(height), 32, C.Uint32(screenFlags))
	if screen == nil {
		panic(getError())
	}
	C.SDL_EnableUNICODE(1)
	C.SDL_EnableKeyRepeat(C.SDL_DEFAULT_REPEAT_DELAY, C.SDL_DEFAULT_REPEAT_INTERVAL)

	initAudio()

	// Synchronize with Run function.
	coord <- true

	eventLoop()
	C.SDL_Quit()

	// Synchronize with Stop function.
	coord <- true
	runLevel = off
}
Example #5
0
File: sdl.go Project: rwcarlsen/sdl
func init() {
	log.SetFlags(0)
	status := C.SDL_Init(C.SDL_INIT_EVERYTHING)
	if status != 0 {
		log.Fatal(sdlerr())
	}
}
Example #6
0
func Init() (error string) {
	flags := int64(C.SDL_INIT_VIDEO)
	if C.SDL_Init(C.Uint32(flags)) != 0 {
		error = C.GoString(C.SDL_GetError())
		return
	}
	return ""
}
Example #7
0
// New returns a newly created Screen
func New(width int, height int, fullscreen bool, FSAA int, name string) *Screen {
	window := &Screen{}

	C.SDL_Init(C.SDL_INIT_VIDEO)
	C.setGlContextAttributes()

	C.SDL_GL_SetAttribute(C.SDL_GL_DOUBLEBUFFER, 1)

	// Force hardware accel
	C.SDL_GL_SetAttribute(C.SDL_GL_ACCELERATED_VISUAL, 1)

	if FSAA > 0 {
		// FSAA (Fullscreen antialiasing)
		C.SDL_GL_SetAttribute(C.SDL_GL_MULTISAMPLEBUFFERS, 1)
		C.SDL_GL_SetAttribute(C.SDL_GL_MULTISAMPLESAMPLES, C.int(FSAA)) // 2, 4, 8
	}

	flags := C.SDL_WINDOW_OPENGL | C.SDL_RENDERER_ACCELERATED
	if fullscreen {
		flags = flags | C.SDL_WINDOW_FULLSCREEN
	}

	C.SDL_CreateWindowAndRenderer(C.int(width), C.int(height), C.Uint32(flags), &window.sdlWindow, &window.renderer)
	C.SDL_SetWindowTitle(window.sdlWindow, C.CString(name))
	C.SDL_GL_CreateContext(window.sdlWindow)

	if err := gl.Init(); err != nil {
		panic(err)
	}
	version := gl.GoStr(gl.GetString(gl.VERSION))
	fmt.Println("OpenGL version", version)

	// Configure global settings
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	gl.ClearColor(0.0, 0.0, 0.0, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	window.Width = width
	window.Height = height
	window.name = name
	window.shouldClose = false
	C.SDL_GL_SwapWindow(window.sdlWindow)

	window.startTime = time.Now()
	window.frameTime = time.Now()
	C.SDL_GL_SetSwapInterval(1)
	window.vsync = true

	return window
}
Example #8
0
File: ui.go Project: velour/ui
// Start starts the user interface.  It must be called by the main go routine, and it
// never returns. The function f is called in a new go routine as the new "main"
// function.  Rate is the rate at which the user interface should poll for events.
func Start(f func(), rate time.Duration) {
	if C.SDL_Init(C.SDL_INIT_EVERYTHING) < 0 {
		panic(sdlError())
	}

	initAudio()

	go func() {
		f()
		os.Exit(0)
	}()

	tick := time.NewTicker(rate)
	for {
		select {
		case f := <-doChan:
			f()
		case <-tick.C:
			pollEvents()
		}
	}
}
Example #9
0
//Opens a window.
//Returns an indicator of success.
func OpenDisplay(width, height int, fullscreen bool) bool {
	if C.SDL_Init(C.SDL_INIT_VIDEO) != 0 {
		return false
	}
	C.TTF_Init()
	var flags C.Uint32 = C.SDL_DOUBLEBUF
	flags |= C.SDL_SWSURFACE
	flags |= C.SDL_HWACCEL

	if fullscreen {
		screen = C.openDisplayFullscreen(C.int(width), C.int(height))
	} else {
		screen = C.openDisplay(C.int(width), C.int(height))
	}
	if screen == nil {
		return false
	}
	C.SDL_WM_SetCaption(C.CString(displayTitle), C.CString(""))
	C.SDL_GL_SetAttribute(C.SDL_GL_SWAP_CONTROL, 1)

	return true
}
Example #10
0
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
}
Example #11
0
func Init(initSDL, stereo bool, rate, nchannels, nbuffers int) {
	if initSDL {
		GSDLWasInitHere = true
		if C.SDL_Init(C.SDL_INIT_AUDIO) != 0 {
			panic(fmt.Sprintf("Unable to initialize SDL: %v\n", util.GetSdlError()))

		}
	}

	//initFlags := C.MIX_INIT_FLAC | C.MIX_INIT_MP3 | C.MIX_INIT_OGG
	//C.Mix_Init(initFlags)

	audio_format := C.AUDIO_S16SYS
	nstereo := 1
	if stereo {
		nstereo = 2
	}
	if C.Mix_OpenAudio(C.int(rate), C.Uint16(audio_format),
		C.int(nstereo), C.int(nbuffers)) != 0 {
		panic(fmt.Sprintf("Unable to initialize audio: %v\n", util.GetMixError()))
	}
	channel.Allocate(nchannels)
}
Example #12
0
// Initializes SDL.
func Init(flags uint32) int {
	GlobalMutex.Lock()
	status := int(C.SDL_Init(C.Uint32(flags)))
	GlobalMutex.Unlock()
	return status
}
Example #13
0
func Init() int {
	x := C.SDL_Init(C.SDL_INIT_VIDEO)

	return int(x)
}
Example #14
0
func Init(flags uint32) (err error) {
	if C.SDL_Init(C.Uint32(flags)) != 0 {
		return errors.New(C.GoString(C.SDL_GetError()))
	}
	return nil
}
Example #15
0
func Init(flags uint32) int {
	return int(C.SDL_Init(C.Uint32(flags)))
}
Example #16
0
File: init.go Project: salihdb/sdl
// Init initializes the subsystems specified by flags.
//
// Note: Init must be called before using any other SDL function.
//
// Note: Quit must be called when finished using the SDL library.
//
// Note: Unless the InitNoParachute flag is set, it will install cleanup signal
// handlers for some commonly ignored fatal signals (like SIGSEGV).
func Init(flags InitFlag) (err error) {
	if C.SDL_Init(C.Uint32(flags)) != 0 {
		return getError()
	}
	return nil
}
Example #17
0
func init() {
	C.SDL_Init(C.SDL_INIT_VIDEO | C.SDL_INIT_AUDIO | C.SDL_INIT_TIMER)
}
Example #18
0
func InitSDL() {
	if ok := C.SDL_Init(C.SDL_INIT_AUDIO); ok < 0 {
		panic(ok)
	}
}
Example #19
0
func Init(flags uint32) int {
	_flags := (C.Uint32)(flags)
	return (int)(C.SDL_Init(_flags))
}
Example #20
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)
			}
		}
	}
}
Example #21
0
File: sdl.go Project: beoran/fungo
// This function loads the SDL dynamically linked library and initializes
// the subsystems specified by 'flags' (and those satisfying dependencies)
// Unless the INIT_NOPARACHUTE flag is set, it will install cleanup
// signal handlers for some commonly ignored fatal signals (like SIGSEGV)
func Init(flags uint32) uint32 {
	return uint32(C.SDL_Init(C.Uint32(flags)))
}