示例#1
0
func main() {

	sdl.Init(sdl.INIT_VIDEO)

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

	if screen == nil {
		panic("sdl error")
	}

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

	pen := Pen{}

	gl.MatrixMode(gl.PROJECTION)

	gl.Viewport(0, 0, gl.GLsizei(screen.W), gl.GLsizei(screen.H))
	gl.LoadIdentity()
	gl.Ortho(0, gl.GLdouble(screen.W), gl.GLdouble(screen.H), 0, -1.0, 1.0)

	gl.ClearColor(1, 1, 1, 0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	var running = true

	for running {

		e := &sdl.Event{}

		for e.Poll() {
			switch e.Type {
			case sdl.QUIT:
				running = false
				break
			case sdl.KEYDOWN:
				running = false
				break
			case sdl.MOUSEMOTION:
				me := e.MouseMotion()
				if me.State != 0 {
					pen.lineTo(Point{int(me.X), int(me.Y)})
				} else {
					pen.moveTo(Point{int(me.X), int(me.Y)})
				}
				break
			}
		}

		sdl.GL_SwapBuffers()
		sdl.Delay(25)
	}

	sdl.Quit()

}
示例#2
0
func main() {

	sdl.Init(sdl.INIT_VIDEO)

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

	if screen == nil {
		panic("sdl error")
	}

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

	pen := Pen{}

	gl.MatrixMode(gl.PROJECTION)

	gl.Viewport(0, 0, int(screen.W), int(screen.H))
	gl.LoadIdentity()
	gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0)

	gl.ClearColor(1, 1, 1, 0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	var running = true

	for running {

		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch ev := e.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.KeyboardEvent:
				if ev.Keysym.Sym == sdl.K_ESCAPE {
					running = false
				}
			case *sdl.MouseMotionEvent:
				if ev.State != 0 {
					pen.lineTo(Point{int(ev.X), int(ev.Y)})
				} else {
					pen.moveTo(Point{int(ev.X), int(ev.Y)})
				}
			}
		}

		sdl.GL_SwapBuffers()
		sdl.Delay(25)
	}

	sdl.Quit()

}
示例#3
0
func (p *PU_GameProtocol) Receive_Warp(_message *punet.Message) {
	data := _message.Warp
	g_game.state = GAMESTATE_LOADING
	sdl.Delay(10)

	if g_game.self != nil {
		g_game.self.CancelWalk()
		g_game.self.SetPosition(data.X, data.Y)
	}

	//request the tiles of the new area we just arrived at
	p.Send_RequestTileRefresh()
}
示例#4
0
文件: game.go 项目: WaylandGod/Go2D
//Game loop
func (game *Game) Run() {
	defer game.Exit()

	if game.initFun == nil {
		fmt.Println("Go2D Warning: No init function set!")
	}

	if game.updateFun == nil {
		fmt.Println("Go2D Warning: No update function set!")
	}

	if game.drawFun == nil {
		fmt.Println("Go2D Warning: No draw function set!")
	}

	//Initialize the game
	game.initialize()

	var dt, old_t, now_t uint32 = 0, 0, 0

	for g_running {
		//Check for events and handle them
		for {
			event, present := sdl.PollEvent()
			if present {
				EventHandler(event)
			} else {
				break
			}
		}

		//Calculate time delta
		now_t = sdl.GetTicks()
		dt = now_t - old_t
		old_t = now_t

		//Update
		game.update(dt)

		//Draw
		game.draw()

		//Give the CPU some time to do other stuff
		sdl.Delay(1)
	}
}
示例#5
0
func (manager *FPSmanager) FramerateDelay() {
	var current_ticks, target_ticks, the_delay uint32

	// next frame
	manager.framecount++

	// get/calc ticks
	current_ticks = sdl.GetTicks()
	target_ticks = manager.lastticks + uint32(float(manager.framecount)*manager.rateticks)

	if current_ticks <= target_ticks {
		the_delay = target_ticks - current_ticks
		sdl.Delay(the_delay)
	} else {
		manager.framecount = 0
		manager.lastticks = sdl.GetTicks()
	}
}
示例#6
0
func main() {

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

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

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

	sdl.WM_SetCaption("Template", "")

	running := true

	for running {

		e := &sdl.Event{}

		for e.Poll() {
			switch e.Type {
			case sdl.QUIT:
				running = false
				break
			default:
			}
		}

		screen.FillRect(nil, 0x000000)

		//screen.Blit(&sdl.Rect{x,y, 0, 0}, image, nil)

		screen.Flip()
		sdl.Delay(25)

	}

	sdl.Quit()

}
示例#7
0
func main() {
	fmt.Println("loaded", len(blokus.Tiles), "shapes")
	blokus.Board[2][4] = blokus.Red
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}
	resize(640, 480)
	sdl.WM_SetCaption("Blokus", "")

	for running {
		e := &sdl.Event{}

		for e.Poll() {
			switch e.Type {
			case sdl.QUIT:
				running = false
			case sdl.KEYDOWN:
				keyDown(e.Keyboard())
			case sdl.KEYUP:
				keyUp(e.Keyboard())
			case sdl.MOUSEBUTTONDOWN:
				mouseDown(e.MouseButton())
			case sdl.VIDEORESIZE:
				r := e.Resize()
				resize(int(r.W), int(r.H))
			default:
			}
			if !running {
				break
			}
		}

		draw()

		sdl.Delay(25)
	}

	sdl.Quit()
}
示例#8
0
文件: test.go 项目: GunioRobot/Go-SDL
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()
}
示例#9
0
文件: main.go 项目: jcd/go-gamelib
func main() {
	sch = sched.NewScheduler(0)

	flag.Parse()

	cmd := selectCommand(CmdNum)

	SetupVideo()

	// Let the selected command initialize
	cmd.OnSetup()

	// Extract sleep time between frames for this command
	sleeptime := cmd.SleepTime()

	var frames uint32 = 0
	// var t0 uint32 = sdl.GetTicks()
	// 	gt := sdl.GetTicks

	if ShowFPS {
		sch.AddInterval(1.0, 2, func(tc sched.TaskChan, sc *sched.Scheduler) {

			val := <-tc
			if val != sched.RUNNING {
				sc.C <- sched.TaskResult{val, tc}
				return
			}
			sc.C <- sched.TaskResult{sched.COMPLETED, tc}

			// seconds := (t - t0) / 1000.0
			// fps := float(frames) / float(1.0)
			// os.Stdout.WriteString("Fdafsda")
			//sch.LOG <- "FPS"
			// fmt.Println("FPS")
			return
			// t0 = t
			frames = 0

		})
	}

	var running = true

	for running {

		e := &sdl.Event{}

		for e.Poll() {
			switch e.Type {
			case sdl.QUIT:
				running = false
				break
			case sdl.KEYDOWN:
				kb := e.Keyboard()
				KeySym = kb.Keysym.Sym
				if KeySym == sdl.K_ESCAPE {
					running = false
				}
				break
			case sdl.MOUSEBUTTONUP, sdl.MOUSEBUTTONDOWN:
				me := e.MouseButton()
				MouseState = me.State
				break
			case sdl.MOUSEMOTION:
				me := e.MouseMotion()
				MouseX = int(me.X)
				MouseY = int(me.Y)
				MouseState = me.State
				break
			case sdl.VIDEORESIZE:
				me := (*sdl.ResizeEvent)(cast(e))
				sdl.SetVideoMode(int(me.W), int(me.H), 32, SDL_FLAGS)
				ResizeWindow(me.W, me.H)
			}
		}

		// sch.Update()
		cmd.OnUpdate()
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		cmd.OnDraw()
		sdl.GL_SwapBuffers()

		if sleeptime != 0 {
			sdl.Delay(uint32(sleeptime))
		}
		frames++
	}

	sdl.Quit()

}
示例#10
0
文件: tools.go 项目: WaylandGod/Go2D
func Sleep(_ticks uint32) {
	sdl.Delay(_ticks)
}
示例#11
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
}
示例#12
0
func main() {
	//Make sure that resources get released
	defer g_engine.Exit()

	//Permission to run on 2 CPU cores
	runtime.GOMAXPROCS(2)

	//Initialize SDL
	err := sdl.Init()
	if err != "" {
		fmt.Printf("Error in Init: %v", err)
		return
	}

	//Initialize the engine
	g_engine.Init()

	//Load data
	g_game.LoadFonts()
	Draw() //Draw the "please wait" text after loading the fonts
	g_game.LoadGuiImages()
	g_game.LoadTileImages()
	g_game.LoadCreatureImages()
	g_game.SetState(GAMESTATE_LOGIN)

	g_loginControls.Show()

	lastTime := sdl.GetTicks()

	frameTime := sdl.GetTicks()
	frameCount := 0

	//Handle events 
	for g_running {
		for {
			event, present := sdl.PollEvent()
			if present {
				EventHandler(event)
			} else {
				break
			}
		}

		//Render everything on screen
		Draw()

		//Give the CPU some time to do other stuff
		sdl.Delay(1)

		//Handle a network message
		g_conn.HandleMessage()

		//Handle a battle event
		if g_game.state == GAMESTATE_BATTLE {
			g_game.battle.ProcessEvents()
		}

		//Update frame time 
		g_frameTime = sdl.GetTicks() - lastTime
		lastTime = sdl.GetTicks()

		//Update FPS
		frameCount++
		if sdl.GetTicks()-frameTime >= 1000 {
			g_FPS = frameCount
			frameCount = 0
			frameTime = sdl.GetTicks()
		}
	}
	sdl.Quit()
}
示例#13
0
func MainLoop() {
	outcode := -1
	option := 0
	lvl := 0

	Refresh(screen)
	screen.Blit(&sdl.Rect{0, 0, 0, 0}, IM.BG[0], nil)
	screen.Flip()

	//MU.Music[0].PlayMusic(-1)

	for running := true; running; {
		for ev := sdl.WaitEvent(); ev != nil; ev = sdl.WaitEvent() {
			Refresh(screen)
			screen.Blit(&sdl.Rect{0, 0, 0, 0}, IM.BG[0], nil)
			if option == 0 {
				screen.FillRect(&sdl.Rect{320, 212, 10, 10}, 0xFF00FF)
			} else {
				screen.FillRect(&sdl.Rect{320, 233, 10, 10}, 0xFF00FF)
			}

			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				return
			case *sdl.KeyboardEvent:
				if e.Type != sdl.KEYDOWN {
					break
				}
				switch e.Keysym.Sym {
				case sdl.K_SPACE:
					if option == 0 {
						goto game
					} else {
						screen.FillRect(&sdl.Rect{100, 200, 100, 10}, 0xFFFFFF)
						screen.FillRect(&sdl.Rect{100, 220, 100, 10}, 0xFFFFFF)
						screen.FillRect(&sdl.Rect{100, 200, 10, 20}, 0xFFFFFF)
						screen.FillRect(&sdl.Rect{200, 200, 10, 30}, 0xFFFFFF)
						screen.Flip()
						var ok bool
						lvl, ok = Password()
						if ok {
							goto game
						}
					}
				case sdl.K_UP:
					option = 0
				case sdl.K_DOWN:
					option = 1
				}
			}
			screen.Flip()
		}

		continue

		//Gameing
	game:
		outcode = MainGame(lvl)

		if outcode == G_EXITPROGRAM { //Exit Program
			running = false
			return
		}

		if outcode == G_NEXTLEVEL { //NextLevel
			screen.Blit(&sdl.Rect{350, 250, 0, 0}, IM.Misc[2], nil)
			screen.Flip()
			sdl.Delay(1000)
			for PressKey() != sdl.K_SPACE {
			}
			lvl++
			if lvl%4 == 0 {
				checkpoint = lvl
			}
			goto game

		}

		if outcode == G_GAMEOVER { //Gameover
			screen.Blit(&sdl.Rect{350, 250, 0, 0}, IM.Misc[1], nil)
			screen.Flip()
			for PressKey() != sdl.K_SPACE {
			}
			player = nil
			lvl = checkpoint
			goto game

		}

		if outcode == G_ENDGAME { //Game Completed
			screen.Blit(&sdl.Rect{350, 250, 0, 0}, IM.Misc[3], nil)
			screen.Flip()
			for PressKey() != sdl.K_SPACE {
			}
			lvl = 0
		}
	}
}