Example #1
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}
	defer sdl.Quit()

	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		log.Fatal(sdl.GetError())
	}

	window, rend := sdl.CreateWindowAndRenderer(640, 480, sdl.WINDOW_SHOWN)

	if window == nil {
		log.Println("nil window")
		log.Fatal(sdl.GetError())
	}
	defer window.Destroy()

	if rend == nil {
		log.Println("nil rend")
		log.Fatal(sdl.GetError())
	}
	defer rend.Destroy()

	if ttf.Init() != 0 {
		log.Fatal(sdl.GetError())
	}
	defer ttf.Quit()

	window.SetTitle("First SDL2 Window")

	image := sdl.Load("./test.png")
	defer image.Free()

	if image == nil {
		log.Println("nil image")
		log.Fatal(sdl.GetError())
	}

	window.SetIcon(image)

	tex := rend.CreateTextureFromSurface(image)
	defer tex.Destroy()

	font := ttf.OpenFont("./Fontin Sans.otf", 16)
	defer font.Close()
	txt_tex, _, _ := RenderTextToTexture(rend, font, "This is a test", sdl.Color{0x7F, 0xFF, 0x10, 0xFF})
	defer txt_tex.Destroy()

	running := true

	worm_in := make(chan Point)
	draw := make(chan Point, 64)

	var out chan Point
	var in chan Point

	out = worm_in

	in = out
	out = make(chan Point)
	go worm(in, out, draw)

	// ticker := time.NewTicker(time.Second / 50) // 50 Hz

	window.ShowSimpleMessageBox(sdl.MESSAGEBOX_INFORMATION, "Test Message", "SDL2 supports message boxes!")
	event := &sdl.Event{}

	for running {
		//		select {
		/*
					case <-ticker.C:
						rend.SetDrawColor(0x30, 0x20, 0x19, 0xFF)
						rend.FillRect(nil)
			            if sdl.GetError() != "" {
			                log.Fatalf(sdl.GetError())
			            }

					loop:
						for {
							select {
							case p := <-draw:
								rend.Clear()
								rend.Copy(tex, &sdl.Rect{int16(p.x), int16(p.y), 0, 0}, nil)

							case <-out:
							default:
								break loop
							}
						}

						var p Point
						sdl.GetMouseState(&p.x, &p.y)
						worm_in <- p

						rend.Present()
		*/
		for event.Poll() {
			switch e := event.Get().(type) {
			case sdl.QuitEvent:
				running = false

			case sdl.KeyboardEvent:
				println("")
				println(e.Keysym.Keycode, ": ", sdl.GetKeyName(e.Keysym.Keycode))

				if e.Keysym.Keycode == sdl.K_ESCAPE {
					running = false
				}

				fmt.Printf("%04x ", e.Type)

				println()

				fmt.Printf("Type: %02x State: %02x\n", e.Type, e.State)
				fmt.Printf("Scancode: %02x Keycode: %02x Mod: %04x\n",
					e.Keysym.Scancode,
					e.Keysym.Keycode,
					e.Keysym.Mod)
			case sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					println("Click:", e.X, e.Y)
					in = out
					out = make(chan Point)
					go worm(in, out, draw)
				}
			}
		}
		rend.SetDrawColor(sdl.Color{0x30, 0x20, 0x19, 0xFF})
		rend.FillRect(nil)
		rend.Copy(tex, nil, nil)

		rend.Copy(txt_tex, nil, nil)

		rend.Present()
	}
}
Example #2
0
func main() {
	runtime.LockOSThread()

	// Parse command line
	flag.Parse()
	tracks := []string{}
	if flag.NArg() > 0 {
		tracks = flag.Args()[0:]
	}
	log.Println("Tracks: ", tracks)

	// Init libSoX and SDL
	if !sox.Init() {
		log.Fatal("Failed to init sox")
	}
	defer sox.Quit()

	einfo := sox.GetEncodingsInfo()
	log.Println("Supported encodings:")
	for _, e := range einfo {
		log.Printf("%s: %s (%x)\n", e.Name, e.Desc, e.Flags)
	}

	effects := sox.GetEffectHandlers()
	log.Println("Supported effects:")
	for _, e := range effects {
		log.Printf("%s: %s (%x)\n", e.Name(), e.Usage(), e.Flags())
	}

	if sdl.Init(sdl.INIT_NOPARACHUTE|sdl.INIT_VIDEO|sdl.INIT_EVENTS) != 0 {
		log.Fatal(sdl.GetError())
	}
	defer sdl.Quit()

	gtk.Init(nil)

	//sdl.GL_SetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2)
	//sdl.GL_SetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 1)
	//sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
	//sdl.GL_SetAttribute(sdl.GL_DEPTH_SIZE, 24)

	window, renderer := sdl.CreateWindowAndRenderer(640, 480,
		sdl.WINDOW_SHOWN|sdl.WINDOW_OPENGL|sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
	if (window == nil) || (renderer == nil) {
		log.Fatal(sdl.GetError())
	}
	//window.GL_CreateContext()

	if ttf.Init() != 0 {
		log.Fatal(sdl.GetError())
	}

	defer window.Destroy()
	defer renderer.Destroy()
	defer ttf.Quit()
	window.SetTitle("Podcast Studio")
	log.Println("Video Driver:", sdl.GetCurrentVideoDriver())
	// Jump to studio.go
	//run_studio(window, renderer, tracks)
	screen := studioSetup(window, renderer, tracks)
	defer screen.rsc.Free()
	defer screen.Destroy()

	loop := glib.NewMainLoop(nil, false)

	glib.IdleAdd(func() bool {
		ret := studioUpdate(window, renderer, screen)
		if !ret {
			loop.Quit()
			return false
		}
		return true
	})

	loop.Run()
}