예제 #1
0
파일: template.go 프로젝트: jonhanks/Go-SDL
func main() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	screen := sdl.SetVideoMode(400, 300, 32, 0)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("Template", "")

	ticker := time.NewTicker(1e9 / 2 /*2 Hz*/)

loop:
	for {
		select {
		case <-ticker.C:
			// Note: For better efficiency, use UpdateRects instead of Flip
			screen.FillRect(nil /*color*/, rand.Uint32())
			//screen.Blit(&sdl.Rect{x,y, 0, 0}, image, nil)
			screen.Flip()

		case event := <-sdl.Events:
			fmt.Printf("%#v\n", event)

			switch event.(type) {
			case sdl.QuitEvent:
				break loop
			}
		}
	}
}
예제 #2
0
func main() {
	//runtime.GOMAXPROCS(runtime.NumCPU()*2)

	var err error
	os.Setenv("SDL_VIDEODRIVER", "x11")

	rand.Seed(time.Now().Unix())

	GOALS := []rune(GOALS_SRC)

	runtime.GOMAXPROCS(1)
	//f, _ := os.Create("prof.dat")
	//pprof.StartCPUProfile(f)
	//defer pprof.StopCPUProfile()

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		fmt.Println(sdl.GetError())
		return
	}
	defer sdl.Quit()

	// load the font system and a font
	if err = ttf.Init(); err != nil {
		fmt.Println(err)
		return
	}
	defer ttf.Quit()
	var fnt *ttf.Font
	if fnt, err = ttf.OpenFont("font.ttf", 60); err != nil {
		fmt.Println(sdl.GetError())
		return
	}
	defer fnt.Close()

	// build the goals
	goals := make([]*Goal, len(GOALS))
	for i, ch := range GOALS {
		goals[i] = NewGoal(fnt, ch, i)
		goals[i].X = goals[i].W/2 + rand.Intn(WIDTH-goals[i].W)
		goals[i].Y = goals[i].H/2 + rand.Intn(HEIGHT-goals[i].H)
		goals[i].Hidden = false
	}

	stickCount := sdl.NumJoysticks()
	if stickCount == 0 {
		panic("No joysticks available")
	}
	markers := make([]Marker, stickCount)
	fmt.Println("Found ", stickCount, " joysticks:")

	colors := [3]uint32{uint32(0x00aa0000), uint32(0x00009900), uint32(0x00000099)}

	for i := 0; i < stickCount; i++ {
		fmt.Println(i+1, " ", sdl.JoystickName(i))
		markers[i] = Marker{Joystick: sdl.JoystickOpen(i), X: WIDTH / 2, Y: HEIGHT / 2, Color: colors[i%len(colors)]}
		defer markers[i].Close()
	}

	var screen = sdl.SetVideoMode(WIDTH, HEIGHT, 32, 0 /*sdl.RESIZABLE*/)

	if screen == nil {
		fmt.Println(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 Joystick Test", "")

	if sdl.GetKeyName(270) != "[+]" {
		fmt.Println("GetKeyName broken")
		return
	}
	mainLoop(screen, markers, goals)
}
예제 #3
0
파일: test.go 프로젝트: jonhanks/Go-SDL
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")

	if music == 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)
				}

			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()
}
예제 #4
0
func main() {
	WIDTH := 640
	HEIGHT := 480
	size := Size{Width: WIDTH, Height: HEIGHT}

	runtime.GOMAXPROCS(runtime.NumCPU() * 2)

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

	var screen = sdl.SetVideoMode(WIDTH, HEIGHT, 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", "")

	running := true
	redraw := true
	requestRedraw := false
	redrawFlag := &redraw

	fullState := NewFractalData(WIDTH, HEIGHT, 2.0, 200)
	fullState.Center = complex(-0.5, 0.0)

	state := fullState.FractalState
	state.DebugRender = true
	state.DebugMerge = true
	pal := NewPalette()

	keymap := make(map[string]bool)

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

	// 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.

	fractalReady := make(chan bool)

	for running {
		if redraw {
			fmt.Println("Parsing keypresses")
			if keymap["+"] {
				state.Scale *= 0.8
			}
			if keymap["-"] {
				state.Scale *= 1.2
			}
			if keymap["left"] {
				state.Center -= complex(state.Scale*0.1, 0.0)
			}
			if keymap["right"] {
				state.Center += complex(state.Scale*0.1, 0.0)
			}
			if keymap["up"] {
				state.Center += complex(0.0, state.Scale*0.1)
			}
			if keymap["down"] {
				state.Center -= complex(0.0, state.Scale*0.1)
			}
			if keymap["iterup"] {
				state.MaxIterations += 25
				if state.MaxIterations > 2000 {
					state.MaxIterations = 2000
				}
			}
			if keymap["iterdown"] {
				state.MaxIterations -= 25
				if state.MaxIterations < 100 {
					state.MaxIterations = 100
				}
			}
			if keymap["overlay"] {
				state.DebugOverlay = !state.DebugOverlay
			}
			keymap = make(map[string]bool)
			fmt.Println("Queue fractal")
			screen.FillRect(nil, 0)
			if size.Width == WIDTH && size.Height == HEIGHT {
				fullState.Merge(state)
			} else {
				// resize event
				screen = sdl.SetVideoMode(size.Width, size.Height, 32, sdl.RESIZABLE)

				if screen == nil {
					log.Fatal(sdl.GetError())
				}
				WIDTH = size.Width
				HEIGHT = size.Height
				state.Width = WIDTH
				state.Height = HEIGHT
				fullState = NewFractalData(WIDTH, HEIGHT, state.Scale, state.MaxIterations)
				fullState.FractalState = state
			}
			fmt.Println("Iterations: ", fullState.MaxIterations)
			go doFractal(fullState, screen, pal, fractalReady)
			redraw = false
			requestRedraw = false
			redrawFlag = &requestRedraw
		}
		select {

		case _ = <-fractalReady:
			fmt.Println("Fractal reports ready")
			screen.Flip()
			redraw = requestRedraw
			requestRedraw = false
			redrawFlag = &redraw
		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 || e.Keysym.Sym == sdl.K_q {
					running = false
				}
				if e.Type == sdl.KEYDOWN {
					if e.Keysym.Sym == sdl.K_KP_PLUS || e.Keysym.Sym == sdl.K_EQUALS {
						fmt.Println("zoom in")
						keymap["+"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_KP_MINUS || e.Keysym.Sym == sdl.K_MINUS {
						fmt.Println("zoom out")
						keymap["-"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_LEFT {
						keymap["left"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_RIGHT {
						keymap["right"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_UP {
						keymap["up"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_DOWN {
						keymap["down"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_LEFTBRACKET {
						keymap["iterup"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_RIGHTBRACKET {
						keymap["iterdown"] = true
						*redrawFlag = true
					} else if e.Keysym.Sym == sdl.K_o {
						keymap["overlay"] = true
						*redrawFlag = true
					}
				}
				/*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 fullState: %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)
				}

			case sdl.ResizeEvent:
				println("resize screen ", e.W, e.H)
				//panic("Resize not supported yet")

				size.Width = int(e.W)
				size.Height = int(e.H)
				*redrawFlag = true

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

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