Esempio n. 1
0
File: k270em.go Progetto: iand/go
// Function main is the main entry point in the program.
func main() {
	flag.Parse()

	if flag.NArg() < 1 {
		fmt.Fprintf(os.Stderr, "Not enough arguments\nusage: %s file.hex\n", os.Args[0])
		os.Exit(2)
	}

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

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

	defer ttf.Quit()

	rm := resourcemanager.NewResourceManager("github.com/kierdavis/go/k270em")

	font := ttf.OpenFont(rm.GetFilename("FreeMono.ttf"), 12)
	if font == nil {
		panic(sdl.GetError())
	}

	defer font.Close()

	_, _, _, _, fontWidth, errID := font.GlyphMetrics('m')
	if errID != 0 {
		panic(sdl.GetError())
	}

	fontHeight := font.Height()

	fmt.Printf("h: %d, w: %d\n", fontHeight, fontWidth)

	screenHeight := fontHeight * 48
	screenWidth := fontWidth * 128

	screen := sdl.SetVideoMode(screenWidth, screenHeight, 32, sdl.RESIZABLE)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("K270 Emulator Display", "")

	f, err := os.Open(flag.Arg(0))
	die(err)
	defer f.Close()

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	die(err)
	program := ix.ExtractDataToEnd(0)

	em := k270emlib.NewEmulator()
	em.SetTraceFile(os.Stdout)
	em.SetMemory(program)

	running := true
	stopRequest := make(chan bool)
	vmem := em.GetVideoMemory()

	go em.Run()
	go func() {
		scanTicker := time.NewTicker(time.Second / 24.0) // 24 hz
		color := sdl.Color{255, 255, 255, 0}

		for {
			if !running {
				break
			}

			select {
			case <-scanTicker.C:
				address := uint16(0)
				for y := 0; y < 48; y++ {
					chars := make([]byte, 128)

					for x := 0; x < 128; x++ {
						c := vmem[address].Char
						if c == 0 {
							c = ' '
						}
						chars[x] = c
						address++
					}

					surf := ttf.RenderText_Solid(font, string(chars), color)
					screen.Blit(&sdl.Rect{0, int16(y * fontHeight), 0, 0}, surf, nil)
				}

			case <-stopRequest:
				return
			}
		}
	}()

	ticker := time.NewTicker(time.Second / 24.0) // 24 fps

	for running {
		select {
		case <-ticker.C:
			screen.Flip()

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

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

			case sdl.ResizeEvent:
				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)
				if screen == nil {
					panic(sdl.GetError())
				}
			}
		}
	}

	stopRequest <- true

	//fmt.Println("Locking...")
	em.Mutex.Lock()
	//fmt.Println("Locked!")
	em.SetRunning(false)
	//fmt.Println("Unlocking...")
	em.Mutex.Unlock()
	//fmt.Println("Unlocked!")
}
Esempio n. 2
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()
}
func main() {
	// Use all processors
	runtime.GOMAXPROCS(runtime.NumCPU())

	// SDL voodoo
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}
	defer sdl.Quit()

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

	screen := sdl.SetVideoMode(640, 480, 32, sdl.ANYFORMAT)
	if screen == nil {
		panic(sdl.GetError())
	}
	screen.SetAlpha(sdl.SRCALPHA, 255)

	sdl.WM_SetCaption("Connect Four", "")

	ticker := time.NewTicker(time.Second / 60 /*60 Hz*/)

	// Make some pipes for communicating with the game logic
	moveReady := make(chan int)
	newState := make(chan c4.State)
	nextMove := make(chan int)
	gameResults := make(chan c4.Piece)
	var winner c4.Piece
	var game c4.State
	waitingForMove := false
	gameOver := false

	// Get ready to write text
	font := ttf.OpenFont("DroidSans.ttf", 36)
	var line1, line2 *sdl.Surface
	showMessage := false

	// Start a game
	startGame := func() {
		c4.RunGame(
			SDLHuman{moveReady, nextMove},
			c4.AlphaBetaAI{
				c4.Black,
				8,
				func(game c4.State, p c4.Piece) float64 {
					// Evolved solution:
					// return c4.EvalFactors{
					// 		0.2502943943301069,
					// 		-0.4952316649483701,
					// 		0.3932539700819625,
					// 		-0.2742452616759889,
					// 		0.4746881137884282,
					// 		0.2091091127191147}.Eval(game, p)
					// Least mean squares solution after 2 iterations against evolved solution:
					return c4.EvalFactors{
						0.32386133725050104, 0.5490470895311659, 0.3932539698522742, -0.27424526114286796, 0.4746881136468789, 0.2091091126568151}.Eval(game, p)
				},
				func(game c4.State) bool {
					return game.GetWinner() != c4.None
				},
			},
			NewUpdater(newState),
			func(err error) {
				fmt.Println(err)
			},
			func(winner c4.Piece) {
				if winner == c4.Red {
					gameResults <- c4.Red
				} else if winner == c4.Black {
					gameResults <- c4.Black
				} else {
					gameResults <- c4.None
				}
			})
	}
	go startGame()

loop:
	for {
		select {
		case <-ticker.C:
			screen.FillRect(
				&sdl.Rect{0, 0, SCREEN_WIDTH, SCREEN_HEIGHT},
				BOARD_COLOR)
			for col := 0; col < c4.MaxColumns; col++ {
				for row := 0; row < c4.MaxRows; row++ {
					drawPiece(screen, col, row, game.GetPiece(col, row))
				}
			}
			if showMessage {
				screen.Blit(
					&sdl.Rect{
						int16(SCREEN_WIDTH/2 - line1.W/2),
						int16(SCREEN_HEIGHT/2 - line1.H),
						0,
						0},
					line1,
					nil)
				screen.Blit(
					&sdl.Rect{
						int16(SCREEN_WIDTH/2 - line2.W/2),
						int16(SCREEN_HEIGHT / 2),
						0,
						0},
					line2,
					nil)
			}
			screen.Flip()

		case event := <-sdl.Events:
			switch e := event.(type) {
			case sdl.MouseButtonEvent:
				if waitingForMove &&
					e.Type == sdl.MOUSEBUTTONUP &&
					e.Button == sdl.BUTTON_LEFT {
					waitingForMove = false
					nextMove <- int(e.X * c4.MaxColumns / SCREEN_WIDTH)

					// Tell user that the AI is thinking now
					line1 =
						sdl.CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, 0)
					line2 =
						ttf.RenderText_Blended(font,
							"Thinking...",
							sdl.Color{255, 255, 255, 0})
					showMessage = true
				} else if gameOver &&
					e.Type == sdl.MOUSEBUTTONUP &&
					e.Button == sdl.BUTTON_LEFT {
					gameOver = false
					showMessage = false
					go startGame()
				}
			case sdl.QuitEvent:
				break loop
			}

		case game = <-newState:
			// We did the assignment; there's nothing else to do

		case <-moveReady:
			waitingForMove = true
			showMessage = false

		case winner = <-gameResults:
			gameOver = true
			var message string
			if winner == c4.Red {
				message = "You win!"
			} else if winner == c4.Black {
				message = "You lose."
			} else {
				message = "Draw."
			}
			line1 =
				ttf.RenderText_Blended(font,
					message,
					sdl.Color{255, 255, 255, 0})

			line2 =
				ttf.RenderText_Blended(font,
					"Click to play again...",
					sdl.Color{255, 255, 255, 0})

			showMessage = true
		}

	}
}