Ejemplo n.º 1
0
// handle key press events
func handleKeyPress(keysym []uint8) {
	keys := sdl.GetKeyState()

	if keys[sdl.K_RIGHT] == 1 {
		yrot -= 3.5
	}

	if keys[sdl.K_LEFT] == 1 {
		yrot += 3.5
	}

	if keys[sdl.K_UP] == 1 {
		xpos -= float32(math.Sin(float64(yrot)*PiOver100)) * 0.5
		zpos -= float32(math.Cos(float64(yrot)*PiOver100)) * 0.5
		if walkbiasangle >= 359.0 {
			walkbiasangle = 0.0
		} else {
			walkbiasangle += 10.0
		}
		walkbias = float32(math.Sin(float64(walkbiasangle)*PiOver100)) / 20.0
	}

	if keys[sdl.K_DOWN] == 1 {
		xpos += float32(math.Sin(float64(yrot)*PiOver100)) * 0.5
		zpos += float32(math.Cos(float64(yrot)*PiOver100)) * 0.5
		if walkbiasangle <= 1.0 {
			walkbiasangle = 359.0
		} else {
			walkbiasangle -= 10.0
		}
		walkbias = float32(math.Sin(float64(walkbiasangle)*PiOver100)) / 20.0
	}
}
Ejemplo n.º 2
0
func main() {

	flag.Parse()

	var done bool
	var keys []uint8

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(640, 480, 16, sdl.OPENGL|sdl.RESIZABLE)

	if screen == nil {
		sdl.Quit()
		panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
	}

	sdl.WM_SetCaption("Gears", "gears")

	init_()
	reshape(int(screen.W), int(screen.H))
	done = false
	for !done {
		var event sdl.Event

		idle()
		for event.Poll() {
			switch event.Type {
			case sdl.VIDEORESIZE:
				screen = sdl.SetVideoMode(int(event.Resize().W), int(event.Resize().H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					reshape(int(screen.W), int(screen.H))
				} else {
					panic("we couldn't set the new video mode??")
				}
				break

			case sdl.QUIT:
				done = true
				break
			}
		}
		keys = sdl.GetKeyState()

		handleKeyPress(keys)
		if keys[sdl.K_ESCAPE] != 0 {
			done = true
		}

		draw()
	}
	sdl.Quit()
	return

}
Ejemplo n.º 3
0
func (b *Player) Events() {
	key := sdl.GetKeyState()
	kright := key[sdl.K_RIGHT]
	kleft := key[sdl.K_LEFT]

	//Set Maximums
	b.Topspeed = PLAYER_TOPSPEED / fps()
	b.GravityMaxVelocity = PLAYER_MAXGRAVITY / fps()
	b.Force = Vector64{PLAYER_TOPSPEED * 2 / fps2(), PLAYER_TOPSPEED * 2 / fps2()}

	//Apply Force
	if key[sdl.K_UP] != 0 && b.InAir == false {
		b.Velocity.Y = -math.Sqrt(210 * gravity.Y)
		b.InAir = true
	}
	if !(kright != 0 && kleft != 0) {
		if kright != 0 && b.Velocity.X < b.Topspeed {
			b.Velocity.X += b.Force.X
			b.Direction = true
		}

		if kleft != 0 && b.Velocity.X > -b.Topspeed {
			b.Velocity.X -= b.Force.X
			b.Direction = false
			b.SetAnimation(1)
		}
	}

	if b.Velocity.Y < b.GravityMaxVelocity {
		b.Velocity.Y += gravity.Y
	}
	if b.Velocity.Y > b.GravityMaxVelocity {
		b.Velocity.Y = b.GravityMaxVelocity
	}

	/*DEBUG
	if key[sdl.K_UP] != 0 && b.Velocity.Y > -b.Topspeed {
		b.Velocity.Y -= b.Force.Y
	}
	if key[sdl.K_DOWN] != 0 && b.Velocity.Y < b.Topspeed {
		b.Velocity.Y += b.Force.Y
	}*/

	b.InAir = true

	//Animation
	if (kleft != 0 || kright != 0) && !(kright != 0 && kleft != 0) {
		b.SetAnimation(1)
	} else {
		b.SetAnimation(0)
	}
}
// handle key press events
func handleKeyPress(keysym sdl.Keysym) {
	keys := sdl.GetKeyState()

	if keys[sdl.K_ESCAPE] == 1 {
		Quit(0)
	}

	if keys[sdl.K_F1] == 1 {
		sdl.WM_ToggleFullScreen(surface)
	}

	if keys[sdl.K_f] == 1 {
		filter = (filter + 1) % 3
	}

	if keys[sdl.K_RIGHT] == 1 {
		yrot -= 1.5
	}

	if keys[sdl.K_LEFT] == 1 {
		yrot += 1.5
	}

	if keys[sdl.K_UP] == 1 {
		xpos -= math.Sin(yrot*PiOver100) * 0.05
		zpos -= math.Cos(yrot*PiOver100) * 0.05
		if walkbiasangle >= 359.0 {
			walkbiasangle = 0.0
		} else {
			walkbiasangle += 10.0
		}
		walkbias = math.Sin(walkbiasangle*PiOver100) / 20.0
	}

	if keys[sdl.K_DOWN] == 1 {
		xpos += math.Sin(yrot*PiOver100) * 0.05
		zpos += math.Cos(yrot*PiOver100) * 0.05
		if walkbiasangle <= 1.0 {
			walkbiasangle = 359.0
		} else {
			walkbiasangle -= 10.0
		}
		walkbias = math.Sin(walkbiasangle*PiOver100) / 20.0
	}
}
Ejemplo n.º 5
0
func main() {

	flag.Parse()

	var done bool
	var keys []uint8

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(300, 300, 16, sdl.OPENGL|sdl.RESIZABLE)

	if screen == nil {
		sdl.Quit()
		panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
	}

	sdl.WM_SetCaption("Gears", "gears")

	init_()
	reshape(int(screen.W), int(screen.H))
	done = false
	for !done {
		var event sdl.Event

		idle()
		for event.Poll() {
			switch event.Type {
			case sdl.VIDEORESIZE:
				screen = sdl.SetVideoMode(int(event.Resize().W), int(event.Resize().H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					reshape(int(screen.W), int(screen.H))
				} else {
					panic("we couldn't set the new video mode??")
				}
				break

			case sdl.QUIT:
				done = true
				break
			}
		}
		keys = sdl.GetKeyState()

		if keys[sdl.K_ESCAPE] != 0 {
			done = true
		}
		if keys[sdl.K_UP] != 0 {
			view_rotx += 5.0
		}
		if keys[sdl.K_DOWN] != 0 {
			view_rotx -= 5.0
		}
		if keys[sdl.K_LEFT] != 0 {
			view_roty += 5.0
		}
		if keys[sdl.K_RIGHT] != 0 {
			view_roty -= 5.0
		}
		if keys[sdl.K_z] != 0 {
			if (sdl.GetModState() & sdl.KMOD_RSHIFT) != 0 {
				view_rotz -= 5.0
			} else {
				view_rotz += 5.0
			}
		}

		draw()
	}
	sdl.Quit()
	return

}
Ejemplo n.º 6
0
func main() {

	filename = flag.String("file", "testbed/data/dude.dat", "enter filename path")
	cx = flag.Int("cx", 300, "enter x-coordinate center")
	cy = flag.Int("cy", 500, "enter y-coordinate center")
	zoom = flag.Float64("zoom", 2, "enter zoom")

	flag.Parse()

	fmt.Println("opening...", *filename)
	f, err := os.Open(*filename)
	if f == nil {
		fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", *filename, err)
		os.Exit(1)
	}

	d, _ := ioutil.ReadAll(f)
	line := strings.SplitAfter(string(d), "\n")

	j := 0
	for i := 0; i < len(line); i++ {
		if len(line[i]) <= 2 {
			break
		}
		j++
	}

	var polyline = make(p2t.PointArray, j)
	for i := 0; i < j; i++ {
		line[i] = strings.TrimRight(line[i], "\r\n")
		num := strings.Split(line[i], " ")
		n1, err1 := strconv.Atof64(num[0])
		n2, err2 := strconv.Atof64(num[1])
		if err1 != nil || err2 != nil {
			fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", *filename, err)
			os.Exit(1)
		}
		polyline[i] = &p2t.Point{X: n1, Y: n2}
	}

	f.Close()

	left = -Width / float64(*zoom)
	right = Width / float64(*zoom)
	bottom = -Height / float64(*zoom)
	top = Height / float64(*zoom)

	last_time := time.Nanoseconds()

	p2t.Init(polyline)
	var triangles p2t.TriArray = p2t.Triangulate()

	dt := time.Nanoseconds() - last_time
	fmt.Printf("Elapsed time : %f ms\n", float64(dt)*1e-6)

	//var mesh p2t.TriArray = p2t.Mesh()

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(Width, Height, 16, sdl.OPENGL|sdl.RESIZABLE)

	if screen == nil {
		sdl.Quit()
		panic("Couldn't set GL video mode: " + sdl.GetError() + "\n")
	}

	sdl.WM_SetCaption("Pol2tri - testbed", "poly2tri")

	if gl.Init() != 0 {
		panic("gl error")
	}

	initGL()

	done := false
	for !done {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.ResizeEvent:
				re := e.(*sdl.ResizeEvent)
				screen = sdl.SetVideoMode(int(re.W), int(re.H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					reshape(int(screen.W), int(screen.H))
				} else {
					panic("we couldn't set the new video mode??")
				}
				break

			case *sdl.QuitEvent:
				done = true
				break
			}
		}
		keys := sdl.GetKeyState()

		if keys[sdl.K_ESCAPE] != 0 {
			done = true
		}
		resetZoom()
		draw(triangles)
	}
	sdl.Quit()
	return

}
Ejemplo n.º 7
0
func MainGame(lvl int) int {
	//=================================
	//Init Stuff
	//=================================
	if player == nil {
		player = NewPlayer()
	}
	currentLvl = MapList[lvl]()
	currentLvl.CreateSurface(screen)
	lasers := Lasers{}
	elasers := Lasers{}
	timer := GameTime{now(), now() + int64(currentLvl.TimeLimit*1e9)}
	var WinDelay int64

	DeltaTime = now() - 50e6
	for running := true; running; {
		Refresh(screen)
		//=================================
		//Events
		//=================================
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				return G_EXITPROGRAM
				/*
				   case *sdl.KeyboardEvent:
				   	if e.Type == sdl.KEYDOWN && e.Keysym.Sym == sdl.K_p {
				   		d := difftime(DeltaTime)
				   		for PressKey() != sdl.K_p {
				   		}
				   		DeltaTime = now()-d
				   	}*/
			}
		}

		if fps() < 20 {
			DeltaTime = now()
			continue
		}

		//=================================
		//Object Events
		//=================================
		gravity.Y = GRAVITYCONSTANT / fps2()

		player.Events()
		lasers = lasers.Clean()
		elasers = elasers.Clean()
		currentLvl.Objs = currentLvl.Objs.Clean()

		if sdl.GetKeyState()[sdl.K_SPACE] != 0 {
			t := player.Shoot()
			if t != nil {
				lasers = append(lasers, t)
			}
		}

		for _, v := range currentLvl.Monst.Attack() {
			elasers = append(elasers, v)
		}

		//=================================
		//Moving Objects
		//=================================
		player.Move()
		currentLvl.Monst.Move()
		currentLvl.Objs.Move()
		lasers.Move()
		elasers.Move()

		//=================================
		//Collission
		//=================================
		for im := 0; im < len(currentLvl.Monst); im++ {
			mon := currentLvl.Monst[im]
			for il := 0; il < len(lasers); il++ {
				las := lasers[il]
				if HitTest(mon, las) {
					if mon.Damage(las.Damage()) { //if dies
						//Spoils
						spoil := NewSpoils(mon.GetRect().X, mon.GetRect().Y)
						if !spoil.IsDead() {
							currentLvl.Objs = append(currentLvl.Objs, spoil)
						}
						//Removing
						currentLvl.Monst = currentLvl.Monst.Remove(im)
						im--

					}
					lasers = lasers.Remove(il)
					il--
				}
			}
		}

		if !player.IsImmortal() {
			for _, v := range currentLvl.Monst {
				if HitTest(player, v) {
					player.WasHit()
				}
			}
			for _, v := range elasers {
				if HitTest(player, v) {
					player.WasHit()
				}
			}
		}

		lasers = lasers.HitWall(currentLvl)

		currentLvl.Objs.Hit(player)

		DeltaTime = now()
		//=================================
		//Blitting
		//=================================
		currentLvl.Blit(screen)
		player.Blit(screen)
		currentLvl.Monst.Blit(screen)
		currentLvl.Objs.Blit(screen)
		lasers.Blit(screen)
		elasers.Blit(screen)
		BlitHeartsAndTimer(screen, player.Lives, timer)

		//=================================
		//Win Lose Conditions
		//=================================
		if WinDelay == 0 {
			if player.Lives == 0 || difftime(timer.End) > 0 {
				if len(currentLvl.Monst) != 0 {
					return G_GAMEOVER
				}
			}
			if len(currentLvl.Monst) == 0 {
				WinDelay = now() + 2e9
			}
		} else if now()-WinDelay > 0 {
			if lvl == len(MapList)-1 {
				return G_ENDGAME
			}
			return G_NEXTLEVEL
		}
		//=================================
		//FrameSetting
		//=================================
		screen.Flip()
		f := now() - DeltaTime
		if f/1e6 < 30 {
			sdl.Delay(uint32(30 - (f / 1e6)))
		}
	}
	return G_NEXTLEVEL
}