Ejemplo n.º 1
0
func init() {
	runtime.LockOSThread()
	sys = system.Make(gos.GetSystemInterface())

	datadir = filepath.Join(os.Args[0], "..", "..")
	base.SetDatadir(datadir)
	base.Log().Printf("Setting datadir: %s", datadir)
	wdx = 1024
	wdy = 768

	// var key_binds base.KeyBinds
	// base.LoadJson(filepath.Join(datadir, "key_binds.json"), &key_binds)
	// key_map = key_binds.MakeKeyMap()
	// base.SetDefaultKeyMap(key_map)
}
Ejemplo n.º 2
0
func handleLoadRequest(req loadRequest) {
	f, _ := os.Open(req.path)
	im, _, err := image.Decode(f)
	f.Close()
	if err != nil {
		base.Error().Printf("Unable to open %s: %v", req.path, err)
		return
	}
	gray := true
	dx := im.Bounds().Dx()
	dy := im.Bounds().Dy()
	for i := 0; i < dx; i++ {
		for j := 0; j < dy; j++ {
			r, g, b, _ := im.At(i, j).RGBA()
			if r != g || g != b {
				gray = false
				break
			}
		}
		if !gray {
			break
		}
	}
	var canvas draw.Image
	var pix []byte
	base.Log().Printf("Is Gray: %t", gray)
	if gray {
		ga := NewGrayAlpha(im.Bounds())
		pix = ga.Pix
		canvas = ga
	} else {
		pix = memory.GetBlock(4 * req.data.dx * req.data.dy)
		canvas = &image.RGBA{pix, 4 * req.data.dx, im.Bounds()}
	}
	draw.Draw(canvas, im.Bounds(), im, image.Point{}, draw.Src)
	load_mutex.Lock()
	load_count += len(pix)
	manual_unlock := false
	// This prevents us from trying to send too much to opengl in a single
	// frame.  If we go over the threshold then we hold the lock until we're
	// done sending data to opengl, then other requests will be free to
	// queue up and they will run on the next frame.
	if load_count < load_threshold {
		load_mutex.Unlock()
	} else {
		manual_unlock = true
	}
	render.Queue(func() {
		{
			gl.Enable(gl.TEXTURE_2D)
			gl.GenTextures(1, (*gl.Uint)(&req.data.texture))
			gl.BindTexture(gl.TEXTURE_2D, req.data.texture)
			gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
			gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
			gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
			gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
			gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
		}
		if gray {
			gl.TexImage2D(
				gl.TEXTURE_2D,
				0,
				gl.ALPHA,
				gl.Sizei(req.data.dx),
				gl.Sizei(req.data.dy),
				0,
				gl.ALPHA,
				gl.UNSIGNED_BYTE,
				gl.Pointer(&pix[0]))
		} else {
			gl.TexImage2D(
				gl.TEXTURE_2D,
				0,
				gl.RGBA,
				gl.Sizei(req.data.dx),
				gl.Sizei(req.data.dy),
				0,
				gl.RGBA,
				gl.UNSIGNED_BYTE,
				gl.Pointer(&pix[0]))
			// gl.TexImage2D(target, level, internalformat, width, height, border, format, type_, pixels)
			// glu.Build2DMipmaps(gl.TEXTURE_2D, gl.RGBA, req.data.dx, req.data.dy, gl.RGBA, pix)
		}
		memory.FreeBlock(pix)
		if manual_unlock {
			load_count = 0
			load_mutex.Unlock()
		}
	})
}
Ejemplo n.º 3
0
// This routine waits for a filename and a data object, then loads the texture
// in that file into that object.  This is so that only one texture is being
// loaded at a time, it prevents us from hammering the filesystem and also
// makes sure we aren't using up a ton of memory all at once.
func loadTextureRoutine(pipe chan loadRequest) {
	for req := range pipe {
		base.Log().Printf("Load: %v", req)
		handleLoadRequest(req)
	}
}
Ejemplo n.º 4
0
func main() {
	{
		f, err := os.Create("/Users/jwills/code/src/github.com/runningwild/shadertest/log.err")
		if err != nil {
			panic("shoot")
		}
		os.Stderr = f
		f, err = os.Create("/Users/jwills/code/src/github.com/runningwild/shadertest/log.out")
		if err != nil {
			panic("shoot")
		}
		os.Stdout = f
	}
	sys.Startup()
	err := gl.Init()
	if err != nil {
		panic(err)
	}
	fmt.Printf("RAWR!!!\n")
	render.Init()
	render.Queue(func() {
		sys.CreateWindow(10, 10, wdx, wdy)
		sys.EnableVSync(true)
		err := gl.Init()
		if err != nil {
			panic(err)
		}
	})
	base.InitShaders()
	runtime.GOMAXPROCS(2)
	ui, err = gui.Make(gin.In(), gui.Dims{wdx, wdy}, filepath.Join(datadir, "fonts", "skia.ttf"))
	if err != nil {
		panic(err)
	}

	anchor := gui.MakeAnchorBox(gui.Dims{wdx, wdy})
	ui.AddChild(anchor)
	var v float64
	// var profile_output *os.File
	// var num_mem_profiles int
	// ui.AddChild(base.MakeConsole())
	size := 19.0
	base.InitShaders()
	x := gl.Double(0.0)
	// y := 0.0
	// tex := texture.LoadFromPath(filepath.Join(base.GetDataDir(), "test/out.dff.small.png"))
	fmt.Printf("RAWR!\n")
	listener := Listener{}
	gin.In().RegisterEventListener(&listener)
	button := gin.In().GetKeyFlat(gin.ControllerButton0+6, gin.DeviceTypeController, gin.DeviceIndexAny)
	fmt.Printf("RAWR!\n")
	for button.FramePressCount() == 0 {
		sys.Think()
		// dsize := gin.In().GetKey(gin.MouseWheelVertical).FramePressAmt()
		// size += dsize
		// x -= float64(tex.Dx()) * dsize / 2
		// y -= float64(tex.Dy()) * dsize / 2
		// if gin.In().GetKey(gin.Down).FramePressAmt() > 0 {
		// 	y += 10
		// }
		// if gin.In().GetKey(gin.Up).FramePressAmt() > 0 {
		// 	y -= 10
		// }
		// if gin.In().GetKey(gin.Left).FramePressAmt() > 0 {
		// 	x += 10
		// }
		// if gin.In().GetKey(gin.Right).FramePressAmt() > 0 {
		// 	x -= 10
		// }
		render.Queue(func() {
			ui.Draw()
			gl.Enable(gl.BLEND)
			gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
			gl.Disable(gl.TEXTURE_2D)
			gl.Color4ub(255, 0, 0, 255)
			gl.Begin(gl.QUADS)
			gl.Vertex2d(100+x, 20)
			gl.Vertex2d(100+x, gl.Double(size+20))
			gl.Vertex2d(200+x, gl.Double(size+20))
			gl.Vertex2d(200+x, 20)
			x += 1
			gl.End()
			gl.Enable(gl.TEXTURE_2D)
			gl.Color4ub(255, 255, 255, 255)
			// // str := "!@#$%^&*"
			// diff := 5.0 / (math.Log(size) + math.Pow(size, 0.7))

			// // Works for 1200
			// diff = 50 * math.Pow(base.GetDictionary("skia").Scale(), 2) / math.Pow(size, 1.0)

			// // Works for 3000
			// diff = 50 * math.Pow(base.GetDictionary("skia").Scale(), 1.5) / math.Pow(size, 0.8)
			// //0.340637
			// //0.159241
			// diff = 75 * math.Pow(base.GetDictionary("skia").Scale(), 1.0) / math.Pow(size, 1.0)
			// diff = 10 / math.Pow(size, 1.0)
			// diff = 20/math.Pow(size, 1.0) + 5*math.Pow(base.GetDictionary("skia").Scale(), 1.0)/math.Pow(size, 1.0)
			// if diff > 0.45 {
			//   diff = 0.45
			// }
			// base.EnableShader("distance_field")
			// base.SetUniformF("distance_field", "dist_min", float32(0.5-diff))
			// base.SetUniformF("distance_field", "dist_max", float32(0.5+diff))
			// base.GetDictionary("skia").RenderString(str, 100, 20, 0, dy, gui.Left)
			// base.GetDictionary("skia").RenderString(str, 100, 20+2*dy, 0, dy/4, gui.Left)
			// base.GetDictionary("skia").RenderString(str, 100, 20, 0, size, gui.Left)
			lorem := "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
			kk := gin.In().GetKeyFlat(gin.ControllerAxis0Positive+1, gin.DeviceTypeController, device_index)
			kl := gin.In().GetKeyFlat(gin.ControllerAxis0Positive+1, gin.DeviceTypeController, gin.DeviceIndexAny)
			s := fmt.Sprintf("%1.2f %1.2f - %1.2f %1.2f", kk.FramePressAvg(), kk.FramePressAmt(), kl.FramePressAvg(), kl.FramePressAmt())
			devices := sys.GetActiveDevices()
			y := 500.0
			for _, t := range []gin.DeviceType{gin.DeviceTypeController, gin.DeviceTypeKeyboard, gin.DeviceTypeMouse} {
				for _, d := range devices[t] {
					var s string
					switch t {
					case gin.DeviceTypeController:
						s = "controller"
					case gin.DeviceTypeKeyboard:
						s = "keyboard"
					case gin.DeviceTypeMouse:
						s = "mouse"
					}
					base.GetDictionary("skia").RenderString(fmt.Sprintf("%s: %d", s, d), 100, y, 0, 45, gui.Left)
					y -= 50
				}
			}
			base.GetDictionary("luxisr").RenderString(s, 50, 50, 0, size, gui.Left)
			// base.GetDictionary("luxisr").RenderString(lorem, 50, 50+size, 0, size, gui.Left)
			base.GetDictionary("skia").RenderString(lorem, 50, 50+2*size, 0, size, gui.Left)
			base.Log().Printf("Foo")
			//      base.EnableShader("")
			// gl.Enable(gl.ALPHA_TEST)
			// gl.AlphaFunc(gl.GREATER, 0.5)
			// tex := texture.LoadFromPath(filepath.Join(base.GetDataDir(), "ships/ship.png"))
			// tex.Bind()
			// tex.RenderAdvanced(x, y, float64(tex.Dx())*size, float64(tex.Dy())*size, 0, true)
			// tex.RenderNatural(300, 100)
			// gl.Disable(gl.ALPHA_TEST)
		})
		render.Queue(func() {
			sys.SwapBuffers()
		})
		render.Purge()

		// if key_map["cpu profile"].FramePressCount() > 0 {
		// 	if profile_output == nil {
		// 		profile_output, err = os.Create(filepath.Join(datadir, "cpu.prof"))
		// 		if err == nil {
		// 			err = pprof.StartCPUProfile(profile_output)
		// 			if err != nil {
		// 				fmt.Printf("Unable to start CPU profile: %v\n", err)
		// 				profile_output.Close()
		// 				profile_output = nil
		// 			}
		// 			fmt.Printf("profout: %v\n", profile_output)
		// 		} else {
		// 			fmt.Printf("Unable to start CPU profile: %v\n", err)
		// 		}
		// 	} else {
		// 		pprof.StopCPUProfile()
		// 		profile_output.Close()
		// 		profile_output = nil
		// 	}
		// }

		// if key_map["mem profile"].FramePressCount() > 0 {
		// 	f, err := os.Create(filepath.Join(datadir, fmt.Sprintf("mem.%d.prof", num_mem_profiles)))
		// 	if err != nil {
		// 		base.Error().Printf("Unable to write mem profile: %v", err)
		// 	}
		// 	pprof.WriteHeapProfile(f)
		// 	f.Close()
		// 	num_mem_profiles++
		// }

		v += 0.01
	}
}