예제 #1
0
파일: raptgo.go 프로젝트: manveru/raptgo
func HandleDrawing() {
	for Running {
		for Background == nil {
			sdl.Delay(100)
		}
		DrawBackground()

		for _, drawable := range Drawables {
			drawable.Draw()
		}

		Player.Draw() // separate so it's always on top
		DrawScore()
		DrawShieldIcons()

		if GameOver {
			DrawGameOver()
		} else if Paused {
			DrawPaused()
		} else if GameWin {
			DrawWin()
		}

		Screen.Flip()
		sdl.Delay(24)
		FrameTicks++
	}
}
예제 #2
0
파일: life.go 프로젝트: manveru/go.life
func RefreshCells(ch chan func()) {
	var targetp *[][]bool
	var target [][]bool
	var l []bool
	var alive bool
	var x, y, count int

	for Running && !Paused {
		targetp = MakeCells()
		target = *targetp

		for x, l = range *Cells {
			for y, alive = range l {
				DrawCell(x, y, alive)

				count = Count(x, y)
				target[x][y] = rule[alive][count]
			}
		}

		select {
		case fun := <-ch:
			fun()
		default:
			break
		}

		Screen.Flip()
		sdl.Delay(Delay)
		Cells = targetp
	}
}
예제 #3
0
파일: life.go 프로젝트: manveru/go.life
func HandleEvents() {
	ch := make(chan func(), 10)

	for Running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				Running = false
			case *sdl.MouseButtonEvent:
				if e.State == 1 {
					x, y := int(e.X), int(e.Y)
					switch e.Button {
					case 1:
						queue(ch, func() { ToggleCell(x/Scale, y/Scale) })
					case 2:
						queue(ch, func() { AddAcorn(x/Scale, y/Scale) })
					case 3:
						queue(ch, func() { AddGlider(x/Scale, y/Scale) })
					}
				}
			case *sdl.KeyboardEvent:
				if e.State == 1 {
					switch e.Keysym.Sym {
					case sdl.K_ESCAPE:
						Running = false
					case sdl.K_SPACE, sdl.K_p:
						TogglePause(ch)
					}
				}
			}
		}

		sdl.Delay(25)
	}
}
예제 #4
0
파일: pong.go 프로젝트: manveru/go-pong
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)
	}
}
예제 #5
0
파일: pong.go 프로젝트: manveru/go-pong
func (world *World) Run() {
	for world.running {
		if !world.pause {
			world.Update()
			world.Draw()
		}
		sdl.Delay(25)
	}
}
예제 #6
0
파일: main.go 프로젝트: Englebabz/CasGO
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

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

	var screen = sdl.SetVideoMode(1400, 300, 32, 0)
	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)

	font := ttf.OpenFont("euler.otf", 24)

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

	black := sdl.Color{0, 0, 0, 0}
	text := ttf.RenderUTF8_Blended(font, "!!", black)
	fmt.Println(text.Bounds())
	sdl.WM_SetCaption("Go-SDL SDL Test", "")
	running := true
	num := NewNumberShower("b+c", "", "euler.otf", 24, 0)
	denom := NewNumberShower("a", "", "euler.otf", 24, 0)
	testy := NewFractionShower(num, denom, "euler.otf", 24, 0)
	testy.CursorPosition = 3
	testy.RenderEmptyIndextxt = true
	str := ""
	for running {
		switch e := sdl.PollEvent().(type) {
		case *sdl.QuitEvent:
			running = false
			break
		case *sdl.KeyboardEvent:

			if e.State == 1 {
				str = handlekeypress2(e.Keysym, str)
				fmt.Println("rendering string", str)
				text = ttf.RenderUTF8_Blended(font, str, black)
			}
		}
		screen.FillRect(nil, 0xFFFFFF)
		screen.Blit(&sdl.Rect{23, 0, 0, 0}, testy.Render(), nil)
		screen.Flip()
		sdl.Delay(5)
	}
}
예제 #7
0
파일: raptgo.go 프로젝트: manveru/raptgo
func HandleActors() {
	for Running {
		if !GameWin && !Paused && !GameOver {
			ActorTicks++

			for _, actor := range Actors {
				actor.Act()
			}
		}

		sdl.Delay(25)
	}
}
예제 #8
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(float32(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()
	}
}
예제 #9
0
func main() {

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

	defer sdl.Quit()

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

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

	sdl.WM_SetCaption("Template", "")

	for true {

		e := &sdl.Event{}

		for e.Poll() {
			switch e.Type {
			case sdl.QUIT:
				return
			default:
			}
		}

		screen.FillRect(nil, 0x000000)

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

		screen.Flip()
		sdl.Delay(25)

	}

}
예제 #10
0
파일: test.go 프로젝트: dmbreaker/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()
}
예제 #11
0
파일: tess.go 프로젝트: 0xfaded/glu
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")
	}

	gl.MatrixMode(gl.PROJECTION)

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

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

	gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)

	tess := glu.NewTess()

	tess.SetBeginCallback(tessBeginHandler)
	tess.SetVertexCallback(tessVertexHandler)
	tess.SetEndCallback(tessEndHandler)
	tess.SetErrorCallback(tessErrorHandler)
	tess.SetEdgeFlagCallback(tessEdgeFlagHandler)
	tess.SetCombineCallback(tessCombineHandler)

	tess.Normal(0, 0, 1)

	var running = true

	for running {

		gl.MatrixMode(gl.MODELVIEW)
		gl.LoadIdentity()
		gl.Translated(-2.5, 0, 0)

		tess.BeginPolygon(nil)
		tess.BeginContour()

		for v := range OuterContour {
			tess.Vertex(OuterContour[v], &OuterContour[v])
		}

		tess.EndContour()
		tess.BeginContour()

		for v := range InnerContour {
			tess.Vertex(InnerContour[v], &InnerContour[v])
		}

		tess.EndContour()
		tess.EndPolygon()

		gl.Translated(5, 0, 0)

		tess.BeginPolygon(nil)
		tess.BeginContour()

		for v := range StarContour {
			tess.Vertex(StarContour[v], &StarContour[v])
		}

		tess.EndContour()
		tess.EndPolygon()

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

	tess.Delete()
	sdl.Quit()

}
예제 #12
0
파일: main.go 프로젝트: kse/aStar-demo
func main() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	v_info := sdl.GetVideoInfo()

	var screen = sdl.SetVideoMode(
		int(v_info.Current_w),
		int(v_info.Current_h),
		32,
		sdl.HWSURFACE|sdl.DOUBLEBUF)

	rows = v_info.Current_w / SIZE
	columns = v_info.Current_h / SIZE

	if v_info.Current_w%SIZE != 0 {
		rows += 1
	}

	if v_info.Current_h%SIZE != 0 {
		columns += 1
	}

	// Initialize our world
	world = make([][]Field, rows)
	for i := range world {
		world[i] = make([]Field, columns)
		for j := range world[i] {
			world[i][j].X = i
			world[i][j].Y = j
			world[i][j].T = OPEN
			world[i][j].lsize = 0
			world[i][j].rsize = 0
			world[i][j].o = false
			world[i][j].c = false
		}
	}

	// Once we're done, free screen object and quit sdl.
	defer sdl.Quit()
	defer screen.Free()

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

	// Set window title
	sdl.WM_SetCaption("A* algorithm demo", "")

	// Give the screen an initially and update display
	screen.FillRect(nil, OPEN)
	screen.Flip()

	/* Draw the grid on our display */
	drawGrid(screen)

	/* Create the different channels we need */
	paint_chan = make(chan *Paint, 2000)
	read_field = make(chan *Field)
	field_chan = make(chan *Field)

	// Fillbox runs asynchronously, start it here
	go initFillBox(screen)

	// Getneighbours process runs all the time, starts here.
	go GetNeighbours(world, read_field)

	for true {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {

			switch e := ev.(type) {
			case *sdl.QuitEvent:
				return
			case *sdl.KeyboardEvent:
				if e.Keysym.Sym == sdl.K_ESCAPE {
					/* Quit when escape is pressed */
					return
				} else if e.Keysym.Sym == sdl.K_r &&
					(e.Keysym.Mod&sdl.KMOD_LCTRL) != 0 &&
					(e.Keysym.Mod&sdl.KMOD_LSHIFT) != 0 {
					resetComplete()
				} else if e.Keysym.Sym == sdl.K_r && (e.Keysym.Mod&sdl.KMOD_LCTRL) != 0 {
					resetAllPaths()
				} else if e.Keysym.Sym == sdl.K_r {
					resetPaths()
				} else if e.Keysym.Sym == sdl.K_RETURN {
					/* If RETURN is pressed, run pathfinding */
					if start != nil && goal != nil {
						//resetPaths();
						go aStar(world, screen)
					}
				}
			case *sdl.MouseMotionEvent:
				if e.State == sdl.BUTTON_LEFT || e.State == sdl.BUTTON_WHEELUP {
					drawMouseMotion(world, screen, e)
				}
			case *sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					state := sdl.GetKeyState()
					if e.Button == sdl.BUTTON_LEFT {
						r := getRect(e)

						if state[sdl.K_s] == 1 {
							// Left mouse button with s, set new start point
							if start == nil {
								start = &world[int(r.X)/SIZE][int(r.Y)/SIZE]

								fillBox(start, START)
							} else {
								fillBox(start, OPEN)

								start = &world[int(r.X)/SIZE][int(r.Y)/SIZE]
								fillBox(start, START)
							}
						} else if state[sdl.K_g] == 1 {
							// Left mouse button with g, set new goal point
							if goal == nil {
								goal = &world[int(r.X)/SIZE][int(r.Y)/SIZE]

								fillBox(goal, GOAL)
							} else {
								fillBox(goal, OPEN)

								goal = &world[int(r.X)/SIZE][int(r.Y)/SIZE]
								fillBox(goal, GOAL)
							}
						} else {
							// No relevant modifiers were pressed, color the field.
							var f *Field = &world[(r.X / SIZE)][(r.Y / SIZE)]
							//fmt.Println("Click on", f);

							if f.T == OPEN {
								fillBox(f, WALL)
							} else {
								fillBox(f, OPEN)
							}
						}
					}
				}
			default:
			}
		}

		// Delay for 15 milliseconds
		sdl.Delay(30)
		screen.Flip()
	}

	fmt.Println("Exiting")
}
예제 #13
0
파일: main.go 프로젝트: salviati/go-opencl
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}
	defer sdl.Quit()

	image0 := sdl.Load(*file)
	if image0 == nil {
		panic(sdl.GetError())
	}

	factorx := float32(1)
	factory := float32(1)

	dstW := uint32(float32(image0.W) * factorx)
	dstH := uint32(float32(image0.H) * factory)

	screen := sdl.SetVideoMode(int(dstW), int(dstH), 32, sdl.DOUBLEBUF)

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

	image := sdl.DisplayFormat(image0)
	format := image.Format

	err := initAndPrepCL()
	check(err)

	order := cl.RGBA
	elemSize := 4

	src, err := c.NewImage2D(cl.MEM_READ_ONLY|cl.MEM_USE_HOST_PTR, order, cl.UNSIGNED_INT8,
		uint32(image.W), uint32(image.H), uint32(image.Pitch), image.Pixels)
	check(err)

	dst, err := c.NewImage2D(cl.MEM_WRITE_ONLY, order, cl.UNSIGNED_INT8,
		dstW, dstH, 0, nil)
	check(err)

	angle := float32(0)
	for running := true; running; angle += 0.001 {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch ev := e.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyboardEvent:
				if ev.Keysym.Sym == sdl.K_ESCAPE {
					running = false
					break
				}
			}
		}

		//pixels, err := imageCall(dstW, dstH, "image_recscale", src, dst, 1/factorx, 1/factory)

		//pixels, err := imageCall(dstW, dstH, "image_rotate", src, dst, angle)
		m := mul(S(1.2, 1.2), R(angle))
		off := []float32{float32(dstW / 2), float32(dstH / 2)}
		pixels, err := imageCall(dstW, dstH, "image_affine2", src, dst,
			[]float32(m[0:2]), []float32(m[2:4]),
			off, off)
		check(err)

		news := sdl.CreateRGBSurfaceFrom(pixels,
			int(dstW), int(dstH), int(elemSize*8), int(elemSize)*int(dstW),
			format.Rmask, format.Gmask, format.Bmask, format.Amask,
		)

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

		screen.FillRect(nil, 0)
		screen.Blit(&sdl.Rect{0, 0, 0, 0}, news, nil)
		screen.Flip()
		sdl.Delay(25)
	}
}
예제 #14
0
파일: raptgo.go 프로젝트: manveru/raptgo
func sleep(delay uint32) { sdl.Delay(delay) }
예제 #15
0
파일: raptgo.go 프로젝트: manveru/raptgo
func HandleEvents() {
	for Running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				Running = false
			case *sdl.MouseMotionEvent:
				MouseMotion(e)
			case *sdl.MouseButtonEvent:
				switch e.Type {
				case sdl.MOUSEBUTTONDOWN:
					MouseDown(e)
				case sdl.MOUSEBUTTONUP:
					MouseUp(e)
				}
			}
		}

		keys := sdl.GetKeyState()
		for n, i := range keys {
			if i == 1 {
				if Paused {
					switch n {
					case sdl.K_RETURN, sdl.K_SPACE:
						Paused = false
					case sdl.K_q:
						Running = false
					}
				} else if GameOver || GameWin {
					switch n {
					case sdl.K_RETURN, sdl.K_SPACE:
						Player.Reset()
					case sdl.K_q:
						Running = false
					}
				} else {
					switch n {
					case sdl.K_LSHIFT, sdl.K_RSHIFT, sdl.K_LCTRL, sdl.K_RCTRL:
						Player.FireWeapon()
					case sdl.K_0:
						Player.weapon = Player.weapons['0']
					case sdl.K_1:
						Player.weapon = Player.weapons['1']
					case sdl.K_2:
						Player.weapon = Player.weapons['2']
					case sdl.K_3:
						Player.weapon = Player.weapons['3']
					case sdl.K_5:
						Player.weapon = Player.weapons['5']
					case sdl.K_9:
						Player.weapon = Player.weapons['9']
					case sdl.K_MINUS:
						Player.weapon = Player.weapons['-']
					case sdl.K_a, sdl.K_LEFT:
						Player.GoLeft()
					case sdl.K_d, sdl.K_RIGHT:
						Player.GoRight()
					case sdl.K_s, sdl.K_DOWN:
						Player.GoDown()
					case sdl.K_w, sdl.K_UP:
						Player.GoUp()
					case sdl.K_ESCAPE:
						Running = false
					case sdl.K_p:
						Paused = true
					}
				}
			}
		}

		sdl.Delay(25)
	}
}