Ejemplo n.º 1
0
func handlekeypress(key sdl.Keysym, str string) string {
	fmt.Println(string(key.Sym), "    ", string(key.Scancode), "    ", string(key.Unicode), key.Unicode)
	if key.Sym >= 97 && key.Sym <= 122 {
		return str + sdl.GetKeyName(sdl.Key(key.Sym))
	}
	switch key.Sym {
	case 8:
		return str[:len(str)-1]
	case 230:
		return "å"
	}
	return sdl.GetKeyName(sdl.Key(key.Sym)) + "   " + string(key.Sym)
}
Ejemplo n.º 2
0
func (world *World) HandleEvents() {
	for world.running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				world.running = false
			case *sdl.KeyboardEvent:
				switch sdl.GetKeyName(sdl.Key(e.Keysym.Sym)) {
				case "p":
					world.pause = !world.pause
				case "j":
					world.Paddle.Go(0, world.Paddle.vector.Y+world.Paddle.speed)
				case "k":
					world.Paddle.Go(0, world.Paddle.vector.Y-world.Paddle.speed)
				case "q":
					world.running = false
				}
			case *sdl.MouseMotionEvent:
				world.Paddle.Go(float64(e.X), float64(e.Y))
			}
		}

		sdl.Delay(25)
	}
}
Ejemplo n.º 3
0
func gameloop(screen *sdl.Surface) {
	gc := &GameContext{screen, 320, 16}

	for {
		e := sdl.WaitEvent()

		screen.FillRect(nil, 0x0)

		gc.drawFloor()
		gc.drawPlayer()
		gc.drawObjects()

		switch re := e.(type) {
		case *sdl.QuitEvent:
			return

		case *sdl.MouseMotionEvent:

			screen.FillRect(&sdl.Rect{
				int16(re.X),
				int16(re.Y),
				50, 50}, 0xffffff)

			screen.Blit(&sdl.Rect{
				int16(re.X),
				int16(re.Y),
				0, 0}, playerTexture, nil)

			fmt.Println(re.X, re.Y)

		case *sdl.KeyboardEvent:
			if re.Type == sdl.KEYDOWN {
				keyname := sdl.GetKeyName(sdl.Key(re.Keysym.Sym))
				fmt.Println("pressed:", keyname)

				switch keyname {
				case "right":
					gc.moveRight()
				case "left":
					gc.moveLeft()
				case "q":
					return
				}
			} else if re.Type == sdl.KEYUP {
				gc.resetPlayerSpeed()
			}
		default:
			//fmt.Println("What the heck?!")
		}

		gc.Dump()

		screen.Flip()
	}
}
Ejemplo n.º 4
0
func main() {

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

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

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

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

	if screen == nil {
		panic(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("test.png")

	if image == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetIcon(image, nil)

	running := true

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

	if font == nil {
		panic(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("test.ogg")
	sound := mixer.LoadWAV("sound.ogg")

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

	music.PlayMusic(-1)

	if sdl.GetKeyName(270) != "[+]" {
		panic("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)

	for running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyboardEvent:
				println("")
				println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))

				if e.Keysym.Sym == 27 {
					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.ResizeEvent:
				println("resize screen ", e.W, e.H)

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

				if screen == nil {
					panic(sdl.GetError())
				}
			}
		}

		screen.FillRect(nil, 0x302019)
		screen.Blit(&sdl.Rect{0, 0, 0, 0}, text, nil)

		loop := true

		for loop {

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

		}

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

		screen.Flip()
		sdl.Delay(25)
	}

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

	ttf.Quit()
	sdl.Quit()
}
Ejemplo n.º 5
0
func handleEvents(new_attractor, redraw *bool, npoints *int) bool {
	for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
		switch e := ev.(type) {
		case *sdl.QuitEvent:
			return false
		case *sdl.KeyboardEvent:
			//fmt.Println(sdl.Key(e.Keysym.Sym), ":", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))
			switch e.Type {
			case sdl.KEYUP:
				switch sdl.Key(e.Keysym.Sym) {
				case sdl.K_LCTRL:
					mod = sdl.KMOD_NONE
				case sdl.K_UP, sdl.K_DOWN:
					yvel = 0
				case sdl.K_LEFT, sdl.K_RIGHT:
					xvel = 0
					svel = 0
				case sdl.K_KP4, sdl.K_KP6:
					yrotvel = 0
				case sdl.K_KP8, sdl.K_KP2:
					xrotvel = 0
				case sdl.K_KP7, sdl.K_KP9:
					zrotvel = 0
				}
			case sdl.KEYDOWN:
				switch mod {
				case sdl.KMOD_NONE:
					switch sdl.Key(e.Keysym.Sym) {
					case sdl.K_LCTRL:
						mod = sdl.KMOD_LCTRL
					case sdl.K_ESCAPE:
						return false
					case sdl.K_UP:
						yvel = -1 / scale
					case sdl.K_DOWN:
						yvel = 1 / scale
					case sdl.K_LEFT:
						xvel = 1 / scale
					case sdl.K_RIGHT:
						xvel = -1 / scale
					case sdl.K_KP4:
						yrotvel = -1
					case sdl.K_KP6:
						yrotvel = +1
					case sdl.K_KP8:
						xrotvel = -1
					case sdl.K_KP2:
						xrotvel = +1
					case sdl.K_KP7:
						zrotvel = -1
					case sdl.K_KP9:
						zrotvel = +1
					case sdl.K_n:
						*new_attractor = true
					case sdl.K_z:
						xoff, yoff, zoff, xrot, yrot, zrot = 0, 0, 0, 0, 0, 0
						scale = 0.5
					case sdl.K_COMMA:
						*npoints -= 1e5
						*redraw = true
					case sdl.K_PERIOD:
						*npoints += 1e5
						*redraw = true
					default:
					}
				case sdl.KMOD_LCTRL:
					switch sdl.Key(e.Keysym.Sym) {
					case sdl.K_LEFT:
						svel -= 1
					case sdl.K_RIGHT:
						svel += 1
					default:
					}
				}
			}
		default:
		}
	}
	return true
}