예제 #1
0
func main() {
	if len(os.Args) != 2 {
		fmt.Printf("Usage: %s imagefile\n", os.Args[0])
		return
	}
	f, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()
	m, _, err := image.Decode(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	w, err := x11.NewWindow()
	if err != nil {
		fmt.Println(err)
		return
	}
	draw.Draw(w.Screen(), w.Screen().Bounds(), m, image.ZP, draw.Src)
	w.FlushImage()
	for e := range w.EventChan() {
		switch e := e.(type) {
		case ui.KeyEvent:
			if e.Key == ' ' {
				return
			}
		}
	}
}
예제 #2
0
파일: draw.go 프로젝트: rboulton/gomand
func openDisplay() (window ui.Window) {
	window, err := x11.NewWindow()
	if err != nil {
		panic(fmt.Sprintf("%s", err))
	}
	return window
}
예제 #3
0
파일: draw.go 프로젝트: taysom/va
func main() {
	Display = make(chan []Point)
	go morph(Display)
	pic := makePicture(400, 600, 20)
	v := <-Display
	pic.Plot(v)
	w, err := x11.NewWindow()
	if err != nil {
		fmt.Println(err)
		return
	}
	for {
		draw.Draw(w.Screen(), w.Screen().Bounds(), pic, image.ZP, draw.Src)
		w.FlushImage()
		time.Sleep(5 * time.Millisecond)
		/*
		   next_picture:
		   		for e := range w.EventChan() {
		   			switch e := e.(type) {
		   			case ui.KeyEvent:
		   				if e.Key == ' ' {
		   					break next_picture
		   				}
		   			}
		   		}
		*/
		v := <-Display
		pic.Clear()
		//		pic := makePicture(400, 600, 20)
		pic.Plot(v)
	}
}
예제 #4
0
func main() {
	tc := runtime.NumCPU()
	runtime.GOMAXPROCS(tc)

	// Initiate a new x11 screen to print images onto
	win, err := x11.NewWindow()
	if err != nil {
		log.Fatalln(err)
	}
	defer win.Close()
	screen := win.Screen()
	_, ok := screen.(*image.RGBA)
	if !ok {
		log.Fatalln("screen isn't an RGBA image.")
	}

	//Create lookup table for every combination of 16 black/white pixels
	var i, j uint
	for i = 0; i < 65536; i++ {
		for j = 0; j < 16; j++ {
			if i&(1<<j) > 0 {
				bw[i][j*4+0] = 0xFF
				bw[i][j*4+1] = 0xFF
				bw[i][j*4+2] = 0xFF
			}
		}
	}
	// Start fps counter in a new goroutin
	go fps()
	// Start goroutines
	for i := 0; i < tc; i++ {
		go createNoise(win, screen)
	}
	createNoise(win, screen)
}
예제 #5
0
func do_worldgen2() {
	win, err := x11.NewWindow()
	Panic(err)
	defer win.Close()
	eventchan := win.EventChan()
	scr := win.Screen()

	w := worldgen2.NewWorld(17, 5)
	translate := noise.NewTranslate(float64(scr.Bounds().Max.X), float64(scr.Bounds().Max.Y))
	translate.Zoom(0, 0, 1, 1)

	drawworld2(translate, w, scr)

	selecting := false
	selectx := uint32(0)
	selecty := uint32(0)

	stop := false
	for !stop {
		win.FlushImage()
		tlevent := <-eventchan
		switch event := tlevent.(type) {
		case nil:
			fmt.Printf("Window closed\n")
			stop = true
		//case draw.KeyEvent:
		case ui.MouseEvent:
			if event.Buttons&1 == 1 {
				if !selecting {
					selecting = true
					selectx = uint32(event.Loc.X)
					selecty = uint32(event.Loc.Y)
				} else {
					selecting = false
					translate.ZoomBox(float64(selectx), float64(selecty), float64(event.Loc.X), float64(event.Loc.Y))
					drawworld2(translate, w, scr)
				}
			} else {
				posx, posy := translate.Into(float64(event.Loc.X), float64(event.Loc.Y))
				per := w.GetHeight_Island(posx, posy)
				fmt.Printf("%f, %f = %f\n", posx, posy, per)
			}
		case ui.ConfigEvent:
			fmt.Printf("CONFIG\n")
			scr = win.Screen()
			translate.Resize(float64(scr.Bounds().Max.X), float64(scr.Bounds().Max.Y))
			drawworld2(translate, w, scr)
		case ui.ErrEvent:
			fmt.Printf("ERR\n")
			Panic(event.Err)
		}
	}
}
예제 #6
0
func (me *Canvas) ensureHasWindow() error {
	if me.window != nil {
		return nil
	}

	window, err := x11.NewWindow()
	if err != nil {
		return err
	}

	me.window = window

	return nil
}
func main() {
	win, err := x11.NewWindow()
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}
	defer win.Close()

	for ev := range win.EventChan() {
		switch e := ev.(type) {
		case ui.ErrEvent:
			log.Fatalf("Error: %v\n", e.Err)
		}
	}
}
예제 #8
0
파일: main.go 프로젝트: vivounicorn/eaburns
func main() {
	ps[0].plot = linesPlot()
	ps[1].plot = histPlot()

	var err error
	font, err = vg.MakeFont("Times-Roman", vg.Points(12))
	if err != nil {
		panic(err)
	}

	win, err := x11.NewWindow()
	if err != nil {
		panic(err)
	}

	drawPlots(win.Screen())
	win.FlushImage()

	if cpuProfile != "" {
		f, err := os.Create(cpuProfile)
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	if memProfile != "" {
		f, err := os.Create(memProfile)
		if err != nil {
			panic(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
	}

	events := win.EventChan()
	for ev := range events {
		if m, ok := ev.(ui.MouseEvent); ok && m.Buttons == 1 {
			winHeight := 600 // hard-coded for ui/x11…
			p, x, y := dataCoord(m.Loc.X, winHeight-m.Loc.Y)
			if p >= 0 {
				str := fmt.Sprintf("plot: %d, coord: %g, %g\n", p, x, y)
				crosshair(win.Screen(), m.Loc.X, winHeight-m.Loc.Y, str)
				win.FlushImage()
			}
		}
	}
}
예제 #9
0
func main() {
	// Ticker that keeps the world ticking
	ticker = time.NewTicker(time.Nanosecond * 40000)
	// Initialize screen
	win, err := x11.NewWindow()
	if win == nil {
		log.Fatalf("no window: %v", err)
	}
	screen := win.Screen()

	bg := canvas.NewBackground(screen.(*image.RGBA), image.White, nil)
	cvs = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(cvs)
	bbox := cvs.Bbox()
	height = uint32(bbox.Dy())
	width = uint32(bbox.Dx())

	// Set even handler for ui events
	ec := win.EventChan()
	go readKeyboardInput(ec)

	var last time.Time
	var now time.Time
	last = time.Now()

	// Parse scene
	sceneparser.ParseJsonFile(world, defaultScenePath)

	// Clear output buffer
	clear()

	renderer.Render(world, width, height, output)
	paint()
	win.FlushImage()

	// Main loop
	for {
		now = <-ticker.C
		paint()
		win.FlushImage()

		dt := now.Nanosecond() - last.Nanosecond()
		last = now
		dt = dt
	}
}
예제 #10
0
func main() {
	win, err := x11.NewWindow()
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}
	defer win.Close()

	evchan := win.EventChan()
	for ev := range evchan {
		switch e := ev.(type) {
		case ui.ErrEvent:
			fmt.Printf("Error: %v\n", e.Err)
			os.Exit(1)
		}
	}
}
예제 #11
0
func main() {
	win, err := x11.NewWindow()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer win.Close()

	go func() {
		upfps := fps()
		screen := win.Screen()

		for {
			gennoise(screen)

			win.FlushImage()

			upfps <- true
		}
	}()

	for _ = range win.EventChan() {
	}
}
예제 #12
0
func main() {
	flag.Parse()

	var rocket image.Image
	rfile, err := os.Open("rocket.png")
	if err == nil {
		rocket, err = png.Decode(rfile)
	}
	if err != nil {
		log.Fatal("Cannot open rocket.png:", err)
	}
	rfile.Close()
	rocketHeight := rocket.Bounds().Dy()

	var flame image.Image
	ffile, err := os.Open("flame.png")
	if err == nil {
		flame, err = png.Decode(ffile)
	}
	if err != nil {
		log.Fatal("Cannot open flame.png:", err)
	}
	ffile.Close()

	urls := make(chan string, 4)
	imgReady := make(chan *image.Image, 4)

	go imagePuller(urls, imgReady)
	go urlGen(urls)

	xdisp, err := x11.NewWindow()
	if err != nil {
		log.Fatal("X11 error: ", err)
	}
	screen := xdisp.Screen()

	//2010/11/29 16:23:17 one tile is sized (0,0)-(256,256)
	//2010/11/29 16:23:17 the screen is sized (0,0)-(800,600)

	//	log.Print("one tile is sized ", img.Bounds())
	//	log.Print("the screen is sized ", screen.Bounds())

	numTiles := screen.Bounds().Dx()/tileWidth + 2
	tileStrip := image.NewRGBA(image.Rect(0, 0, numTiles*tileWidth, tileHeight))

	// pre-load the tile strip
	for i := 0; i < numTiles; i++ {
		iptr := <-imgReady
		img := *iptr
		draw.Draw(tileStrip, image.Rect(i*tileWidth, 0, i*tileWidth+tileWidth, tileHeight), img, image.ZP, draw.Over)
	}

	rocketLimit = tileHeight/2 - rocketHeight/2
	topBlack := (screen.Bounds().Dy() - tileHeight) / 2
	origin = image.Pt(10, topBlack+tileHeight/2-rocketHeight/2)
	for {
		for x := 0; x < tileWidth; x += 7 {
			then := time.Now()
			draw.Draw(screen, image.Rect(0, topBlack, screen.Bounds().Dx(), topBlack+tileHeight), tileStrip, image.Pt(x, 0), draw.Over)
			if *game {
				draw.Draw(screen, image.Rect(10, topBlack+tileHeight/2-rocketHeight/2+rocketPos, screen.Bounds().Dx(), topBlack+tileHeight), rocket, image.ZP, draw.Over)
				for i := 0; i < 10; i++ {
					if flames[i].active {
						flames[i].where.X += 40
						if flames[i].where.X > screen.Bounds().Dx() {
							flames[i].active = false
						}
						draw.Draw(screen, image.Rectangle{flames[i].where.Add(origin).Add(image.Pt(0, rocketHeight/2)), screen.Bounds().Size()}, flame, image.Pt(0, 0), draw.Over)
					}
				}
			}

			now := time.Now()
			frameTime := now.Sub(then)

			// a flush is just a write on a channel, so it takes negligible time
			xdisp.FlushImage()

			toSleep := 0.1*1e9 - frameTime
			//			log.Print("Took ", frameTime, " ns to draw, will sleep ", toSleep, " ns")
			time.Sleep(toSleep)
			processEvent(xdisp.EventChan())
		}

		// shift tiles in tileStrip and put in new last one
		draw.Draw(tileStrip, image.Rect(0, 0, (numTiles-1)*tileWidth, tileHeight), tileStrip, image.Pt(tileWidth, 0), draw.Over)
		iptr := <-imgReady
		img := *iptr
		draw.Draw(tileStrip, image.Rect((numTiles-1)*tileWidth, 0, numTiles*tileWidth, tileHeight), img, image.ZP, draw.Over)
	}
}
예제 #13
0
파일: bounce.go 프로젝트: zxpbenson/rog-go
func main() {
	rand.Seed(0)
	ctxt, err := x11.NewWindow()
	if ctxt == nil {
		log.Fatalf("no window: %v", err)
	}
	screen := ctxt.Screen()
	bg := canvas.NewBackground(screen.(*image.RGBA), image.White, flushFunc(ctxt))
	window = canvas.NewCanvas(nil, bg.Rect())
	bg.SetItem(window)
	nballs := 0
	ctxt.FlushImage()

	csz := window.Rect().Max

	// add edges of window
	addLine(image.Pt(-1, -1), image.Pt(csz.X, -1))
	addLine(image.Pt(csz.X, -1), image.Pt(csz.X, csz.Y))
	addLine(image.Pt(csz.X, csz.Y), image.Pt(-1, csz.Y))
	addLine(image.Pt(-1, csz.Y), image.Pt(-1, -1))

	go sliderProc()

	makeRect(image.Rect(30, 30, 200, 100), setAlpha(red, 128))
	makeRect(image.Rect(150, 90, 230, 230), setAlpha(blue, 128))

	window.Flush()

	mkball := make(chan ball)
	delball := make(chan bool)
	pause := make(chan bool)

	go monitor(mkball, delball, pause)

	for i := 0; i < nballs; i++ {
		mkball <- randBall()
	}
	ecc := make(chan (<-chan interface{}))
	ec := ctxt.EventChan()
	for {
		select {
		case e, ok := <-ec:
			if !ok {
				return
			}
			switch e := e.(type) {
			case ui.MouseEvent:
				if e.Buttons == 0 {
					break
				}
				if window.HandleMouse(window, e, ec) {
					break
				}
				switch {
				case e.Buttons&1 != 0:
					go handleMouse(e, ec, ecc, lineMaker)
					ec = nil
				case e.Buttons&2 != 0:
					go handleMouse(e, ec, ecc, func(m ui.MouseEvent, ec <-chan interface{}) {
						ballMaker(e, ec, mkball)
					})
					ec = nil
				case e.Buttons&4 != 0:
					delball <- true
				}
			case ui.KeyEvent:
				fmt.Printf("got key %c (%d)\n", e.Key, e.Key)
				switch e.Key {
				case ' ':
					pause <- true
				case 'd':
					delball <- true
				}
			default:
				fmt.Printf("unknown event %v\n", e)
			}
		case ec = <-ecc:
			break
		}
	}
}