Esempio n. 1
2
func main() {
	// Handle options
	help := flag.Bool("help", false, "Show usage")
	verbose := flag.Bool("verbose", false, "Verbose output")
	fullscreen := flag.Bool("fullscreen", false, "Go fullscreen!")
	bgImage := flag.String("bg-image", "", "Background image file")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "shell - A system shell based on CLIngon (Command Line INterface for Go Nerds\n\n")
		fmt.Fprintf(os.Stderr, "Usage:\n\n")
		fmt.Fprintf(os.Stderr, "\tshell [options] <fontfile> \n\n")
		fmt.Fprintf(os.Stderr, "Options are:\n\n")
		flag.PrintDefaults()
	}

	flag.Parse()

	if *help == true {
		flag.Usage()
		return
	}

	if len(flag.Args()) < 1 {
		flag.Usage()
		return
	}

	config := configuration{
		verbose:         *verbose,
		fullscreen:      *fullscreen,
		bgImage:         *bgImage,
		consoleX:        40,
		consoleY:        40,
		consoleW:        560,
		consoleH:        400,
		animationLength: ANIMATION_LENGTH,
	}

	r := initialize(&config)

	y_channel := make(chan int16)
	running := true

	// Have to start this *before* printing to the console,
	// because the printing will cause some messages to be sent to 'sdlrenderer.UpdatedRectsCh()'
	go func() {
		var y int16 = config.consoleY
		for running {
			select {
			case newY := <-y_channel:
				y = newY
				r.render(nil, y)
			case rects := <-sdlrenderer.UpdatedRectsCh():
				r.render(rects, y)
			}
		}
	}()

	var console *clingon.Console = clingon.NewConsole(&ShellEvaluator{})
	console.SetRenderer(r.sdlRenderer)
	y_channel <- config.consoleY
	console.Print(greetingText)
	console.SetPrompt("shell:$ ")

	var anim *clingon.Animation = nil
	var animValueCh <-chan float64 = nil
	toggleAnimation := false

	for running {

		select {
		case event := <-sdl.Events:
			switch e := event.(type) {
			case sdl.QuitEvent:
				running = false
			case sdl.MouseMotionEvent:
				var y int
				state := sdl.GetRelativeMouseState(nil, &y)
				if state == 1 {
					if y < 0 {
						sdlrenderer.EventCh() <- clingon.Cmd_Scroll{clingon.SCROLL_UP}
					} else if y > 0 {
						sdlrenderer.EventCh() <- clingon.Cmd_Scroll{clingon.SCROLL_DOWN}

					}
				}
			case sdl.KeyboardEvent:
				keyName := sdl.GetKeyName(sdl.Key(e.Keysym.Sym))
				if config.verbose {
					fmt.Printf("\n")
					fmt.Printf("%v: %v", e.Keysym.Sym, ": ", keyName)

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

					for i := 0; i < len(e.Pad0); i++ {
						fmt.Printf("%02x ", e.Pad0[i])
					}
					fmt.Printf("\n")

					fmt.Printf("Type: %02x Which: %02x State: %02x Pad: %02x\n", e.Type, e.Which, e.State, e.Pad0[0])
					fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", e.Keysym.Scancode, e.Keysym.Sym, e.Keysym.Mod, e.Keysym.Unicode)
				}
				if (keyName == "escape") && (e.Type == sdl.KEYDOWN) {
					running = false
				} else if (keyName == "f10") && (e.Type == sdl.KEYDOWN) {
					if anim != nil {
						anim.Terminate()
						<-anim.FinishedCh()
						anim = nil
					}

					toggleAnimation = !toggleAnimation

					slidingDistance := int16(r.appSurface.H) - config.consoleY
					anim = clingon.NewSliderAnimation(config.animationLength, float64(slidingDistance))

					animValueCh = anim.ValueCh()
					anim.Start()

				} else if (keyName == "page up") && (e.Type == sdl.KEYDOWN) {
					sdlrenderer.EventCh() <- clingon.Cmd_Scroll{clingon.SCROLL_UP}
				} else if (keyName == "page down") && (e.Type == sdl.KEYDOWN) {
					sdlrenderer.EventCh() <- clingon.Cmd_Scroll{clingon.SCROLL_DOWN}
				} else if (keyName == "up") && (e.Type == sdl.KEYDOWN) {
					console.PutReadline(clingon.HISTORY_PREV)
				} else if (keyName == "down") && (e.Type == sdl.KEYDOWN) {
					console.PutReadline(clingon.HISTORY_NEXT)
				} else if (keyName == "left") && (e.Type == sdl.KEYDOWN) {
					console.PutReadline(clingon.CURSOR_LEFT)
				} else if (keyName == "right") && (e.Type == sdl.KEYDOWN) {
					console.PutReadline(clingon.CURSOR_RIGHT)
				} else {
					unicode := e.Keysym.Unicode
					if unicode > 0 {
						console.PutUnicode(unicode)
					}
				}
			}

		case value := <-animValueCh:
			var y float64
			if toggleAnimation {
				y = 40 + value
			} else {
				y = float64(r.appSurface.H) - value
			}

			y_channel <- int16(y)
		}

	} // End of "for running"

	sdl.Quit()
}
Esempio n. 2
0
func (l *inputLoop) Run() {
	for {
		select {
		case <-l.pause:
			l.pause <- 0

		case <-l.terminate:
			l.terminate <- 0

		case _event := <-sdl.Events:
			switch e := _event.(type) {
			case sdl.QuitEvent:
				application.Exit()
			case sdl.KeyboardEvent:
				keyName := sdl.GetKeyName(sdl.Key(e.Keysym.Sym))
				application.Logf("%d: %s\n", e.Keysym.Sym, keyName)
				if e.Type == sdl.KEYDOWN {
					l.sms.command <- cmdJoypadEvent{keyMap[keyName], JOYPAD_DOWN}
				} else if e.Type == sdl.KEYUP {
					l.sms.command <- cmdJoypadEvent{keyMap[keyName], JOYPAD_UP}
				}
				if e.Type == sdl.KEYDOWN && keyName == "p" {
					paused := make(chan bool)
					l.sms.paused = !l.sms.paused
					l.sms.command <- cmdPauseEmulation{paused}
					<-paused
				}
				if e.Type == sdl.KEYDOWN && keyName == "d" {
					l.sms.paused = true
					paused := make(chan bool)
					l.sms.command <- cmdPauseEmulation{paused}
					<-paused
					l.sms.command <- cmdShowCurrentInstruction{}
				}
				if e.Keysym.Sym == sdl.K_ESCAPE {
					application.Exit()
				}

			}
		}
	}
}
Esempio n. 3
0
func main() {
	log.SetFlags(0)

	var resourcePath string
	{
		GOPATH := os.Getenv("GOPATH")
		if GOPATH == "" {
			log.Fatal("No such environment variable: GOPATH")
		}
		for _, gopath := range strings.Split(GOPATH, ":") {
			a := gopath + "/src/github.com/0xe2-0x9a-0x9b/Go-SDL/sdl-test"
			_, err := os.Stat(a)
			if err == nil {
				resourcePath = a
				break
			}
		}
		if resourcePath == "" {
			log.Fatal("Failed to find resource directory")
		}
	}

	var joy *sdl.Joystick
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}

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

	if sdl.NumJoysticks() > 0 {
		// Open joystick
		joy = sdl.JoystickOpen(0)

		if joy != nil {
			println("Opened Joystick 0")
			println("Name: ", sdl.JoystickName(0))
			println("Number of Axes: ", joy.NumAxes())
			println("Number of Buttons: ", joy.NumButtons())
			println("Number of Balls: ", joy.NumBalls())
		} else {
			println("Couldn't open Joystick!")
		}
	}

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

	var screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)

	if screen == nil {
		log.Fatal(sdl.GetError())
	}

	var video_info = sdl.GetVideoInfo()

	println("HW_available = ", video_info.HW_available)
	println("WM_available = ", video_info.WM_available)
	println("Video_mem = ", video_info.Video_mem, "kb")

	sdl.EnableUNICODE(1)

	sdl.WM_SetCaption("Go-SDL SDL Test", "")

	image := sdl.Load(resourcePath + "/test.png")

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

	sdl.WM_SetIcon(image, nil)

	running := true

	font := ttf.OpenFont(resourcePath+"/Fontin Sans.otf", 72)

	if font == nil {
		log.Fatal(sdl.GetError())
	}

	font.SetStyle(ttf.STYLE_UNDERLINE)
	white := sdl.Color{255, 255, 255, 0}
	text := ttf.RenderText_Blended(font, "Test (with music)", white)
	music := mixer.LoadMUS(resourcePath + "/test.ogg")
	sound := mixer.LoadWAV(resourcePath + "/sound.ogg")

	if music == nil || sound == nil {
		log.Fatal(sdl.GetError())
	}

	music.PlayMusic(-1)

	if sdl.GetKeyName(270) != "[+]" {
		log.Fatal("GetKeyName broken")
	}

	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

	// Note: The following SDL code is highly ineffective.
	//       It is eating too much CPU. If you intend to use Go-SDL,
	//       you should to do better than this.

	for running {
		select {
		case <-ticker.C:
			screen.FillRect(nil, 0x302019)
			screen.Blit(&sdl.Rect{0, 0, 0, 0}, text, nil)

		loop:
			for {
				select {
				case p := <-draw:
					screen.Blit(&sdl.Rect{int16(p.x), int16(p.y), 0, 0}, image, nil)

				case <-out:
				default:
					break loop
				}
			}

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

			screen.Flip()

		case _event := <-sdl.Events:
			switch e := _event.(type) {
			case sdl.QuitEvent:
				running = false

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

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

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

				for i := 0; i < len(e.Pad0); i++ {
					fmt.Printf("%02x ", e.Pad0[i])
				}
				println()

				fmt.Printf("Type: %02x Which: %02x State: %02x Pad: %02x\n", e.Type, e.Which, e.State, e.Pad0[0])
				fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", e.Keysym.Scancode, e.Keysym.Sym, e.Keysym.Mod, e.Keysym.Unicode)

			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)
					sound.PlayChannel(-1, 0)
				}

			case sdl.JoyAxisEvent:
				println("Joystick Axis Event ->", "Type", e.Type, "Axis:", e.Axis, " Value:", e.Value, "Which:", e.Which)

			case sdl.JoyButtonEvent:
				println("Joystick Button Event ->", e.Button)
				println("State of button", e.Button, "->", joy.GetButton(int(e.Button)))

			case sdl.ResizeEvent:
				println("resize screen ", e.W, e.H)

				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)

				if screen == nil {
					log.Fatal(sdl.GetError())
				}
			}
		}
	}

	// Close if opened
	if sdl.JoystickOpened(0) > 0 {
		joy.Close()
	}

	image.Free()
	music.Free()
	font.Close()

	ttf.Quit()
	sdl.Quit()
}