예제 #1
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)
	}
}
예제 #2
0
func NewScreen(w, h int) *Screen {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal("failed to init sdl", sdl.GetError())
		return nil
	}

	if ttf.Init() != 0 {
		log.Fatal("failed to init ttf", sdl.GetError())
		return nil
	}

	var ctx Screen
	if err := ctx.setSurface(w, h); err != nil {
		log.Fatal(err)
	}

	var vInfo = sdl.GetVideoInfo()
	log.Println("HW_available = ", vInfo.HW_available)
	log.Println("WM_available = ", vInfo.WM_available)
	log.Println("Video_mem = ", vInfo.Video_mem, "kb")

	/* title := "Subtitle Player" */
	title := os.Args[0]
	icon := "" // path/to/icon
	sdl.WM_SetCaption(title, icon)

	sdl.EnableUNICODE(1)

	ctx.fps = opt.fps

	err := ctx.setFont(opt.fontPath, opt.fontSize)
	if err != nil {
		log.Fatal("failed to set default font")
		return nil
	}

	ctx.debugFont = ttf.OpenFont(DFLT_FONT_PATH, 20)
	if ctx.debugFont == nil {
		errMsg := fmt.Sprintf("failed to open debug font: %s",
			sdl.GetError())
		/* return errors.New(errMsg) */
		log.Fatal(errMsg)
	}
	ctx.debugLineHeight = uint16(ctx.debugFont.LineSkip())

	ctx.updateC = make(chan int)

	return &ctx
}
예제 #3
0
파일: drawing.go 프로젝트: kse/aStar-demo
/*
 * Draw a grid on the display and return info about the Tile
 */
func drawGrid(screen *sdl.Surface) {
	/*{{{*/
	vid := sdl.GetVideoInfo()

	// First the vertical
	for i := 0; i < int(vid.Current_w); i += SIZE {
		screen.FillRect(
			&sdl.Rect{int16(i), int16(0), 1, uint16(vid.Current_h)},
			0x000000)
		screen.UpdateRect(int32(i), 0, 1, uint32(vid.Current_h))
	}

	// Then the horizontal
	for i := 0; i < int(vid.Current_h); i += SIZE {
		screen.FillRect(
			&sdl.Rect{0, int16(i), uint16(vid.Current_w), 1},
			0x000000)
		screen.UpdateRect(0, int32(i), uint32(vid.Current_w), 1)
	}

	return
	/*}}}*/
}
예제 #4
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()
}
예제 #5
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")
}