Example #1
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.Debugf("%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()
				}

			}
		}
	}
}
Example #2
0
func Run() {
	// {
	// 	GOPATH := os.Getenv("GOPATH")
	// 	if GOPATH == "" {
	// 		log.Fatal("No such environment variable: GOPATH")
	// 	}
	// 	for _, gopath := range strings.Split(GOPATH, ":") {
	// 		a := gopath + "/src/binpix/gosdl_test"
	// 		_, err := os.Stat(a)
	// 		if err == nil {
	// 			resourcePath = a
	// 			break
	// 		}
	// 	}
	// 	if resourcePath == "" {
	// 		log.Fatal("Failed to find resource directory")
	// 	}
	// }

	// renderManager := NewRenderManager()
	// renderManager.Init()
	// var i int16 = 0
	// image := sdl.Load(resourcePath + "/test.png")

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

	// sdl.WM_SetIcon(image, nil)

	/////////////////////////////////////////////

	// Init managers
	renderManager := NewRenderManager()
	sceneManager := NewSceneManager()

	// Create an object
	obj1 := NewObject(1)
	// Create a render component
	spr := renderManager.CreateRenderComponent("walk_face2")
	// spr := renderManager.CreateRenderComponent("walk_face")
	// Add it to the object
	obj1.AddComponent(spr)

	// Add object to the scene
	rootNode := sceneManager.GetRootNode()
	rootNode.AttachObject(obj1)

	// scene.AddObject(obj1)

	msg := NewSetPositionMessage(1, Vector2d{23, 69})
	sceneManager.HandleMessage(msg)

	/////////////////////////////////////////////

	running := true

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

	ticker := time.NewTicker(time.Second / 50) // 50 Hz
	for running {
		select {
		case <-ticker.C:
			renderManager.Update(sceneManager)
		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.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())
				// 	}
				// }
			}
		}
		// image.Free()
	}
}
Example #3
0
// A Go routine for processing SDL events.
func sdlEventLoop(app *spectrum.Application, speccy *spectrum.Spectrum48k, verboseInput bool) {
	evtLoop := app.NewEventLoop()

	consoleIsVisible := false

	shutdown.Add(1)
	for {
		select {
		case <-evtLoop.Pause:
			evtLoop.Pause <- 0

		case <-evtLoop.Terminate:
			// Terminate this Go routine
			if app.Verbose {
				app.PrintfMsg("SDL event loop: exit")
			}
			evtLoop.Terminate <- 0
			shutdown.Done()
			return

		case event := <-sdl.Events:
			switch e := event.(type) {
			case sdl.QuitEvent:
				if app.Verbose {
					app.PrintfMsg("SDL quit -> request[exit the application]")
				}
				app.RequestExit()

			case sdl.JoyAxisEvent:
				if verboseInput {
					app.PrintfMsg("[Joystick] Axis: %d, Value: %d", e.Axis, e.Value)
				}
				if e.Axis == 0 {
					if e.Value > 0 {
						speccy.Joystick.KempstonDown(spectrum.KEMPSTON_RIGHT)
					} else if e.Value < 0 {
						speccy.Joystick.KempstonDown(spectrum.KEMPSTON_LEFT)
					} else {
						speccy.Joystick.KempstonUp(spectrum.KEMPSTON_RIGHT)
						speccy.Joystick.KempstonUp(spectrum.KEMPSTON_LEFT)
					}
				} else if e.Axis == 1 {
					if e.Value > 0 {
						speccy.Joystick.KempstonDown(spectrum.KEMPSTON_UP)
					} else if e.Value < 0 {
						speccy.Joystick.KempstonDown(spectrum.KEMPSTON_DOWN)
					} else {
						speccy.Joystick.KempstonUp(spectrum.KEMPSTON_UP)
						speccy.Joystick.KempstonUp(spectrum.KEMPSTON_DOWN)
					}
				}

			case sdl.JoyButtonEvent:
				if verboseInput {
					app.PrintfMsg("[Joystick] Button: %d, State: %d", e.Button, e.State)
				}
				if e.Button == 0 {
					if e.State > 0 {
						speccy.Joystick.KempstonDown(spectrum.KEMPSTON_FIRE)
					} else {
						speccy.Joystick.KempstonUp(spectrum.KEMPSTON_FIRE)
					}
				}

			case sdl.KeyboardEvent:
				keyName := sdl.GetKeyName(sdl.Key(e.Keysym.Sym))

				if verboseInput {
					app.PrintfMsg("\n")
					app.PrintfMsg("%v: %v", e.Keysym.Sym, keyName)
					app.PrintfMsg("Type: %02x Which: %02x State: %02x\n", e.Type, e.Which, e.State)
					app.PrintfMsg("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) {
					if app.Verbose {
						app.PrintfMsg("escape key -> request[exit the application]")
					}
					app.RequestExit()

				} else if (keyName == "f10") && (e.Type == sdl.KEYDOWN) {
					//if app.Verbose {
					//	app.PrintfMsg("f10 key -> toggle console")
					//}
					if !r.toggling {
						r.toggling = true

						if r.cliSurface_orNil == nil {
							done := make(chan bool)
							r.cliSurfaceCh <- cmd_newCliSurface{newCLISurface(r.scale2x, r.fullscreen), done}
							<-done
						}

						anim := clingon.NewSliderAnimation(0.500, 1.0)

						var targetState int
						if consoleIsVisible {
							targetState = HIDE
						} else {
							targetState = SHOW
						}

						r.sliderCh <- cmd_newSlider{anim, targetState}
						anim.Start()

						consoleIsVisible = !consoleIsVisible
					}
				} else {
					if r.cliSurface_orNil != nil {
						cliSurface := r.cliSurface_orNil

						if (keyName == "page up") && (e.Type == sdl.KEYDOWN) {
							cliSurface.EventCh() <- clingon.Cmd_Scroll{clingon.SCROLL_UP}
						} else if (keyName == "page down") && (e.Type == sdl.KEYDOWN) {
							cliSurface.EventCh() <- clingon.Cmd_Scroll{clingon.SCROLL_DOWN}
						} else if (keyName == "up") && (e.Type == sdl.KEYDOWN) {
							cli.PutReadline(clingon.HISTORY_PREV)
						} else if (keyName == "down") && (e.Type == sdl.KEYDOWN) {
							cli.PutReadline(clingon.HISTORY_NEXT)
						} else if (keyName == "left") && (e.Type == sdl.KEYDOWN) {
							cli.PutReadline(clingon.CURSOR_LEFT)
						} else if (keyName == "right") && (e.Type == sdl.KEYDOWN) {
							cli.PutReadline(clingon.CURSOR_RIGHT)
						} else {
							unicode := e.Keysym.Unicode
							if unicode > 0 {
								cli.PutUnicode(unicode)
							}
						}
					} else {
						sequence, haveMapping := spectrum.SDL_KeyMap[keyName]

						if haveMapping {
							switch e.Type {
							case sdl.KEYDOWN:
								// Normal order
								for i := 0; i < len(sequence); i++ {
									speccy.Keyboard.KeyDown(sequence[i])
								}
							case sdl.KEYUP:
								// Reverse order
								for i := len(sequence) - 1; i >= 0; i-- {
									speccy.Keyboard.KeyUp(sequence[i])
								}
							}
						}
					}
				}
			}
		}
	}
}