Пример #1
0
func main() {

	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.False)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(800, 600, "Create Window Example", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}

	window.SetKeyCallback(keypress)
	window.SetMouseButtonCallback(mousepress)

	gl.Viewport(0, 0, 800, 600)

	for !window.ShouldClose() {
		draw()
		window.SwapBuffers()
		glfw.PollEvents()
	}

}
Пример #2
0
func main() {
	err := glfw.Init()
	if err != nil {
		panic(err)
	}
	defer glfw.Terminate()
	fp, err := os.Open("example.tmx")
	if err != nil {
		panic(err)
	}

	m, err := tmx.NewMap(fp)
	if err != nil {
		panic(err)
	}

	var monitor *glfw.Monitor
	window, err := glfw.CreateWindow(screenWidth, screenHeight, "Map Renderer", monitor, nil)
	if err != nil {
		panic(err)
	}

	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}

	width, height := window.GetFramebufferSize()
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.ClearColor(1.0, 1.0, 1.0, 1.0)
	gl.Viewport(0, 0, int32(width), int32(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(width), float64(height), 0, -1, 1)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	canvas := newOpenGLCanvas(width, height, float32(width)/float32(screenWidth), float32(height)/float32(screenHeight))
	renderer := tmx.NewRenderer(*m, canvas)
	fps := 0
	startTime := time.Now().UnixNano()
	timer := tmx.CreateTimer()
	timer.Start()
	for !window.ShouldClose() {
		elapsed := float64(timer.GetElapsedTime()) / (1000 * 1000)
		renderer.Render(int64(math.Ceil(elapsed)))
		fps++
		if time.Now().UnixNano()-startTime > 1000*1000*1000 {
			log.Println(fps)
			startTime = time.Now().UnixNano()
			fps = 0
		}

		window.SwapBuffers()
		glfw.PollEvents()
		timer.UpdateTime()
	}
}
Пример #3
0
func drawScene(w *glfw.Window) {
	width, height := w.GetFramebufferSize()
	ratio := float32(width) / float32(height)
	var x1, x2, y1, y2 float32
	if ratio > 1 {
		x1, x2, y1, y2 = -ratio, ratio, -1, 1
	} else {
		x1, x2, y1, y2 = -1, 1, -1/ratio, 1/ratio
	}

	gl.Viewport(0, 0, int32(width), int32(height))
	gl.Clear(gl.COLOR_BUFFER_BIT)

	// Applies subsequent matrix operations to the projection matrix stack
	gl.MatrixMode(gl.PROJECTION)

	gl.LoadIdentity()                                                   // replace the current matrix with the identity matrix
	gl.Ortho(float64(x1), float64(x2), float64(y1), float64(y2), 1, -1) // multiply the current matrix with an orthographic matrix

	// Applies subsequent matrix operations to the modelview matrix stack
	gl.MatrixMode(gl.MODELVIEW)

	gl.LoadIdentity()

	gl.LineWidth(1)
	gl.Begin(gl.LINE)   // delimit the vertices of a primitive or a group of like primitives
	gl.Color3f(0, 0, 0) // set the current color
	gl.Vertex3f(0, y1, 0)
	gl.Vertex3f(0, y2, 0)
	gl.Vertex3f(x1, 0, 0)
	gl.Vertex3f(x2, 0, 0)
	gl.End()

	gl.Rotatef(float32(glfw.GetTime()*50), 0, 0, 1) // multiply the current matrix by a rotation matrix

	s := float32(.95)

	gl.Begin(gl.TRIANGLES)
	gl.Color3f(1, 0, 0)  // set the current color
	gl.Vertex3f(0, s, 0) // specify a vertex
	gl.Color3f(0, 1, 0)
	gl.Vertex3f(s*.866, s*-0.5, 0)
	gl.Color3f(0, 0, 1)
	gl.Vertex3f(s*-.866, s*-0.5, 0)
	gl.End()

	gl.LineWidth(5)
	gl.Begin(gl.LINE_LOOP)
	for i := float64(0); i < 2*math.Pi; i += .05 {
		r, g, b := hsb2rgb(float32(i/(2*math.Pi)), 1, 1)
		gl.Color3f(r, g, b)
		gl.Vertex3f(s*float32(math.Sin(i)), s*float32(math.Cos(i)), 0)
	}
	gl.End()

}
Пример #4
0
/* new window size or exposure */
func reshape(width int32, height int32) {
	h := float64(height) / float64(width)
	gl.Viewport(0, 0, width, height)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Frustum(-1.0, 1.0, -h, h, 5.0, 600.0)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(0.0, 0.0, -100)
}
Пример #5
0
// onResize sets up a simple 2d ortho context based on the window size
func onResize(window *glfw.Window, w, h int) {
	w, h = window.GetSize() // query window to get screen pixels
	width, height := window.GetFramebufferSize()
	gl.Viewport(0, 0, int32(width), int32(height))
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(w), 0, float64(h), -1, 1)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.ClearColor(1, 1, 1, 1)
}
Пример #6
0
func (c *Context) SetViewport(f Framebuffer, width, height int) error {
	gl.Flush()
	gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))
	if err := gl.CheckFramebufferStatus(gl.FRAMEBUFFER); err != gl.FRAMEBUFFER_COMPLETE {
		if e := gl.GetError(); e != 0 {
			return errors.New(fmt.Sprintf("glBindFramebuffer failed: %d", e))
		}
		return errors.New("glBindFramebuffer failed: the context is different?")
	}
	gl.Viewport(0, 0, int32(width), int32(height))
	return nil
}
Пример #7
0
func (state *State) Reset(window *glfw.Window) {
	gl.ClearColor(1, 1, 1, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()

	gl.Disable(gl.DEPTH)
	gl.Enable(gl.FRAMEBUFFER_SRGB)

	width, height := window.GetSize()
	gl.Viewport(0, 0, int32(width), int32(height))
	gl.Ortho(0, float64(width), float64(height), 0, 30, -30)
}
Пример #8
0
func reshape(window *glfw.Window, w, h int) {
	gl.ClearColor(1, 1, 1, 1)
	//fmt.Println(gl.GetString(gl.EXTENSIONS))
	gl.Viewport(0, 0, int32(w), int32(h))         /* Establish viewing area to cover entire window. */
	gl.MatrixMode(gl.PROJECTION)                  /* Start modifying the projection matrix. */
	gl.LoadIdentity()                             /* Reset project matrix. */
	gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */
	gl.Scalef(1, -1, 1)                           /* Invert Y axis so increasing Y goes down. */
	gl.Translatef(0, float32(-h), 0)              /* Shift origin up to upper-left corner. */
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Disable(gl.DEPTH_TEST)
	width, height = w, h
}
Пример #9
0
func reshape(w int, h int) {
	/* Because Gil specified "screen coordinates" (presumably with an
	   upper-left origin), this short bit of code sets up the coordinate
	   system to correspond to actual window coodrinates.  This code
	   wouldn't be required if you chose a (more typical in 3D) abstract
	   coordinate system. */

	gl.Viewport(0, 0, int32(w), int32(h))         /* Establish viewing area to cover entire window. */
	gl.MatrixMode(gl.PROJECTION)                  /* Start modifying the projection matrix. */
	gl.LoadIdentity()                             /* Reset project matrix. */
	gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */
	gl.Scalef(1, -1, 1)                           /* Invert Y axis so increasing Y goes down. */
	gl.Translatef(0, float32(-h), 0)              /* Shift origin up to upper-left corner. */
}
Пример #10
0
func main() {
	var winTitle string = "Go-SDL2 + Go-GL"
	var winWidth, winHeight int = 800, 600
	var window *sdl.Window
	var context sdl.GLContext
	var event sdl.Event
	var running bool
	var err error

	if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
		panic(err)
	}
	defer sdl.Quit()

	if err = gl.Init(); err != nil {
		panic(err)
	}

	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_OPENGL)
	if err != nil {
		panic(err)
	}
	defer window.Destroy()
	context, err = sdl.GL_CreateContext(window)
	if err != nil {
		panic(err)
	}
	defer sdl.GL_DeleteContext(context)

	gl.Enable(gl.DEPTH_TEST)
	gl.ClearColor(0.2, 0.2, 0.3, 1.0)
	gl.ClearDepth(1)
	gl.DepthFunc(gl.LEQUAL)
	gl.Viewport(0, 0, int32(winWidth), int32(winHeight))

	running = true
	for running {
		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseMotionEvent:
				fmt.Printf("[%d ms] MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel)
			}
		}
		drawgl()
		sdl.GL_SwapWindow(window)
	}
}
Пример #11
0
func (v *Video) Reshape(width int32, height int32) {
	var x_offset int32 = 0
	var y_offset int32 = 0

	r := ((float64)(height)) / ((float64)(width))

	if r > 0.9375 { // Height taller than ratio
		h := (int32)(math.Floor((float64)(0.9375 * (float64)(width))))
		y_offset = (height - h) / 2
		height = h
	} else if r < 0.9375 { // Width wider
		w := (int32)(math.Floor((float64)((256.0 / 240.0) * (float64)(height))))
		x_offset = (width - w) / 2
		width = w
	}

	v.width = width
	v.height = height

	gl.Viewport(x_offset, y_offset, width, height)
}
Пример #12
0
func main() {
	if err := glfw.Init(nopContextWatcher{}); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Samples, 8) // Anti-aliasing.

	window, err := glfw.CreateWindow(400, 400, "", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}

	glfw.SwapInterval(1) // Vsync.

	InitFont()
	defer DeinitFont()

	framebufferSizeCallback := func(w *glfw.Window, framebufferSize0, framebufferSize1 int) {
		gl.Viewport(0, 0, int32(framebufferSize0), int32(framebufferSize1))

		var windowSize [2]int
		windowSize[0], windowSize[1] = w.GetSize()

		// Update the projection matrix.
		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		gl.Ortho(0, float64(windowSize[0]), float64(windowSize[1]), 0, -1, 1)
		gl.MatrixMode(gl.MODELVIEW)
	}
	{
		var framebufferSize [2]int
		framebufferSize[0], framebufferSize[1] = window.GetFramebufferSize()
		framebufferSizeCallback(window, framebufferSize[0], framebufferSize[1])
	}
	window.SetFramebufferSizeCallback(framebufferSizeCallback)

	var inputEventQueue []events.InputEvent
	mousePointer = &events.Pointer{VirtualCategory: events.POINTING}

	window.SetMouseMovementCallback(func(w *glfw.Window, xpos, ypos, xdelta, ydelta float64) {
		inputEvent := events.InputEvent{
			Pointer:    mousePointer,
			EventTypes: map[events.EventType]struct{}{events.SLIDER_EVENT: {}},
			InputId:    0,
			Buttons:    nil,
			Sliders:    []float64{xdelta, ydelta},
		}
		if w.GetInputMode(glfw.CursorMode) != glfw.CursorDisabled {
			inputEvent.EventTypes[events.AXIS_EVENT] = struct{}{}
			inputEvent.Axes = []float64{xpos, ypos}
		}
		inputEventQueue = events.EnqueueInputEvent(inputEventQueue, inputEvent)
	})

	window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
		inputEvent := events.InputEvent{
			Pointer:     mousePointer,
			EventTypes:  map[events.EventType]struct{}{events.BUTTON_EVENT: {}},
			InputId:     uint16(button),
			Buttons:     []bool{action != glfw.Release},
			Sliders:     nil,
			Axes:        nil,
			ModifierKey: uint8(mods),
		}
		inputEventQueue = events.EnqueueInputEvent(inputEventQueue, inputEvent)
	})

	go func() {
		<-time.After(5 * time.Second)
		log.Println("trigger!")
		boxUpdated = true

		glfw.PostEmptyEvent()
	}()

	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // For font.

	gl.ClearColor(247.0/255, 247.0/255, 247.0/255, 1)

	var spinner int

	var widgets []events.Widgeter

	widgets = append(widgets, NewButtonWidget(mgl64.Vec2{50, 200}, func() { fmt.Println("button triggered") }))

	for !window.ShouldClose() && glfw.Press != window.GetKey(glfw.KeyEscape) {
		glfw.WaitEvents()

		// Process Input.
		inputEventQueue = events.ProcessInputEventQueue(inputEventQueue, widgets[0])

		gl.Clear(gl.COLOR_BUFFER_BIT)
		gl.LoadIdentity()

		for _, widget := range widgets {
			widget.Render()
		}

		drawSpinner(spinner)
		spinner++

		drawBox()

		gl.Color3d(1, 0, 0)
		NewOpenGlStream(mgl64.Vec2{50, 300}).PrintText(` !"#$%&'()*+,-./
0123456789:;<=>?
@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_
` + "`" + `abcdefghijklmno
pqrstuvwxyz{|}~`)

		window.SwapBuffers()
		log.Println("swapped buffers")

		//runtime.Gosched()
	}
}
Пример #13
0
func (c *Context) setViewportImpl(width, height int) error {
	return c.runOnContextThread(func() error {
		gl.Viewport(0, 0, int32(width), int32(height))
		return nil
	})
}
Пример #14
0
func Run(charset string, charwidth, charheight int, eh EventHandler) {
	mousetrans := mouseTranslator{}
	keytrans := keyTranslator{}
	defer eh.Fin(true)
	width := 800
	height := 600
	// Initialize glfw and create window
	err := glfw.Init()
	Panic(err)
	defer glfw.Terminate()
	glfw.WindowHint(glfw.Resizable, glfw.True)
	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	window, err := glfw.CreateWindow(width, height, "Roguelike", nil, nil)
	defer window.Destroy()
	Panic(err)
	window.MakeContextCurrent()

	// Initialize opengl
	err = gl.Init()
	Panic(err)

	// Create shaders and program
	program, err := gli.NewProgram(vertexShaderText, fragmentShaderText)
	Panic(err)
	defer program.Delete()

	// Load and initialize texture
	img, err := gli.LoadImage(charset)
	Panic(err)
	texture, err := gli.NewTexture(img,
		gli.TextureFilter(gli.LINEAR, gli.LINEAR),
		gli.TextureWrap(gli.CLAMP_TO_EDGE, gli.CLAMP_TO_EDGE))
	Panic(err)
	defer texture.Delete()

	// Create Vertex ArrayObject
	vao, err := gli.NewVAO()
	Panic(err)
	defer vao.Delete()

	// Create grid
	grid, err := NewGrid(charwidth, charheight, texture.Size().X, texture.Size().Y)
	Panic(err)
	grid.Resize(width, height)
	vCoords, vIndex, vData := grid.Buffers()

	// Create grid buffers
	posvbo, err := gli.NewBuffer(vCoords)
	Panic(err)
	defer posvbo.Delete()
	idxvbo, err := gli.NewBuffer(vIndex, gli.BufferElementArray())
	Panic(err)
	defer idxvbo.Delete()
	vbo, err := gli.NewBuffer(vData, gli.BufferAccessFrequency(gli.DYNAMIC))
	Panic(err)
	defer vbo.Delete()

	window.SetSizeCallback(func(win *glfw.Window, w, h int) {
		//fmt.Printf("resize\n")
		width = w
		height = h
		gl.Viewport(0, 0, int32(width), int32(height))
		grid.Resize(width, height)
		vCoords, vIndex, vData := grid.Buffers()
		posvbo.Upload(vCoords)
		idxvbo.Upload(vIndex)
		vbo.Upload(vData)
	})

	window.SetKeyCallback(func(win *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
		//fmt.Printf("key=%v code=%d, action=%v, mods=%v\n", key, scancode, action, mods)
		e, ok := keytrans.Key(key, action, mods)
		if ok {
			eh.Key(e)
		}
	})

	window.SetCharCallback(func(win *glfw.Window, key rune) {
		eh.Char(key)
		//fmt.Printf("char=%d(%c)\n", key, key)
	})

	window.SetCursorPosCallback(func(win *glfw.Window, x float64, y float64) {
		e, ok := mousetrans.Pos(x, y)
		if ok {
			eh.Mouse(e)
		}
	})

	window.SetMouseButtonCallback(func(win *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
		e, ok := mousetrans.Button(button, action, mods)
		if ok {
			eh.Mouse(e)
		}
	})

	// Set up VAO
	vao.Enable(2, posvbo, program.Attrib("position"))
	vao.Enable(2, vbo, program.Attrib("texCoord"),
		gli.VAOStride(4))
	vao.Enable(1, vbo, program.Attrib("foreColor"),
		gli.VAOStride(4), gli.VAOOffset(2))
	vao.Enable(1, vbo, program.Attrib("backColor"),
		gli.VAOStride(4), gli.VAOOffset(3))

	// Set uniforms
	program.Uniform("tex").SetSampler(1)
	program.Uniform("colorData[0]").SetFloat(colorData...)
	program.Uniform("runeSize").SetFloat(float32(grid.RuneSize().X), float32(grid.RuneSize().Y))

	gl.ClearColor(0.0, 0.0, 0.0, 1.0)

	for !window.ShouldClose() && !eh.Fin(false) {
		//fmt.Printf("draw\n")

		// Render scene
		grid.clearData()
		eh.Draw(grid)
		_, _, vData = grid.Buffers()
		vbo.Update(0, vData)

		gl.Clear(gl.COLOR_BUFFER_BIT)

		// Draw scene
		program.Use()
		vao.Use()
		texture.Use(1)
		idxvbo.Use()
		gl.DrawElements(gl.TRIANGLES, grid.Vertices(), gl.UNSIGNED_INT, gl.PtrOffset(0))

		window.SwapBuffers()
		glfw.WaitEvents()
	}
}
Пример #15
0
func (ctx *DrawContext) drawScreen(o *orrery.Orrery) {
	fullscreen := false

	gl.Viewport(0, 0, int32(ctx.width), int32(ctx.height))
	gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)

	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(0, 0, 0)

	t_delta := time.Duration(0)
	frametime := time.Second / 24
	log.Printf(`ft: %v`, frametime)

	frametimes := time.Duration(0)
	nsamples := 0
	slowest_frame := time.Duration(0)
	frames_over_deadline := 0

	defer func() {
		log.Printf(`average frame time: %v, #sample: %d`, frametimes/time.Duration(nsamples), nsamples)
		log.Printf(`slowest frame: %v, # of frames over %v: %d`, slowest_frame, frametime, frames_over_deadline)
	}()

	for {
		t_start := time.Now()

		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		ctx.cam.Update()
		ctx.drawGrid()
		ctx.drawParticles(o)
		ctx.drawHud(o, t_delta)
		ctx.win.SwapBuffers()

		glfw.PollEvents()

		select {
		case cmd := <-ctx.cmd:
			switch cmd {
			case DRAW_QUIT:
				return
			case DRAW_FULLSCREEN:
				/*
					if fullscreen {
						ctx.win.SetFullscreen(0)
					} else {
						ctx.win.SetFullscreen(sdl.WINDOW_FULLSCREEN)
					}
				*/
				fullscreen = !fullscreen
			case DRAW_TOGGLE_WIREFRAME:
				ctx.wireframe = !ctx.wireframe
			case DRAW_TOGGLE_VERBOSE:
				ctx.verbose = !ctx.verbose
			}
		default:
			/* ignore */
		}

		t_delta = time.Since(t_start)
		frametimes += t_delta
		nsamples++
		if t_delta > slowest_frame {
			slowest_frame = t_delta
		}
		if t_delta > frametime {
			frames_over_deadline++
		}
		t_sleep := frametime.Nanoseconds() - t_delta.Nanoseconds()
		if t_sleep > 0 {
			time.Sleep(time.Duration(t_sleep) * time.Nanosecond)
		}
	}
}
Пример #16
0
func main() {
	if err := glfw.Init(); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Samples, 8) // Anti-aliasing.

	window, err := glfw.CreateWindow(400, 400, "", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		panic(err)
	}

	glfw.SwapInterval(1)
	//window.SetPos(50, 600)
	//window.SetPos(1600, 600)
	//window.SetPos(1275, 300)
	//window.SetPos(1200, 300)

	framebufferSizeCallback := func(w *glfw.Window, framebufferSize0, framebufferSize1 int) {
		gl.Viewport(0, 0, int32(framebufferSize0), int32(framebufferSize1))

		var windowSize [2]int
		windowSize[0], windowSize[1] = w.GetSize()

		// Update the projection matrix.
		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		gl.Ortho(0, float64(windowSize[0]), float64(windowSize[1]), 0, -1, 1)
		gl.MatrixMode(gl.MODELVIEW)
	}
	{
		var framebufferSize [2]int
		framebufferSize[0], framebufferSize[1] = window.GetFramebufferSize()
		framebufferSizeCallback(window, framebufferSize[0], framebufferSize[1])
	}
	window.SetFramebufferSizeCallback(framebufferSizeCallback)

	go func() {
		<-time.After(5 * time.Second)
		log.Println("trigger!")
		boxUpdated = true

		glfw.PostEmptyEvent()
	}()

	//gl.ClearColor(0.8, 0.3, 0.01, 1)
	gl.ClearColor(247.0/255, 247.0/255, 247.0/255, 1)

	var spinner int

	for !window.ShouldClose() && glfw.Press != window.GetKey(glfw.KeyEscape) {
		glfw.WaitEvents()
		//glfw.PollEvents()

		gl.Clear(gl.COLOR_BUFFER_BIT)

		drawSpinner(spinner)
		spinner++

		drawBox()

		window.SwapBuffers()
		log.Println("swapped buffers")

		//runtime.Gosched()
	}
}
Пример #17
0
func main() {
	if err := glfw.Init(); err != nil {
		fmt.Println("glfw.Init():", err)
		return
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Decorated, glfw.True)
	glfw.WindowHint(glfw.ContextVersionMajor, 1)
	glfw.WindowHint(glfw.ContextVersionMinor, 0)
	glfw.WindowHint(glfw.Resizable, glfw.True)

	window, err := glfw.CreateWindow(10, 10, "Settlers", nil, nil)
	if err != nil {
		fmt.Println("glfw.CreateWindow():", err)
		return
	}
	window.MakeContextCurrent()

	if err := gl.Init(); err != nil {
		fmt.Println("gl.Init():", err)
		return
	}

	stash := fontstash.New(512, 512)
	fontID, err := stash.AddFont(resourcePath("MorrisRoman-Black.ttf"))
	if err != nil {
		fmt.Println(err)
		return
	}
	stash.SetYInverted(true)
	font := &font{stash, fontID, 35}

	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()

	rand.Seed(time.Now().UnixNano())
	g := game.New([]game.Color{game.Red, game.White, game.Blue}, rand.Int())

	var lines []string
	window.SetCharCallback(func(_ *glfw.Window, r rune) {
		if len(lines) == 0 {
			lines = []string{""}
		}
		lines[len(lines)-1] += string(r)
	})
	window.SetCharModsCallback(func(_ *glfw.Window, r rune, _ glfw.ModifierKey) {
	})
	window.SetKeyCallback(func(_ *glfw.Window, key glfw.Key, _ int, action glfw.Action, _ glfw.ModifierKey) {
		if action == glfw.Release {
			return
		}
		if key == glfw.KeyEscape {
			window.SetShouldClose(true)
		}
		if key == glfw.Key1 {
			g.CurrentPlayer = 0
		}
		if key == glfw.Key2 {
			g.CurrentPlayer = 1
		}
		if key == glfw.Key3 {
			g.CurrentPlayer = 2
		}
		if key == glfw.Key4 {
			g.CurrentPlayer = 3
		}
		if key == glfw.KeyR {
			state = buildingRoad
		}
		if key == glfw.KeyS {
			state = buildingSettlement
		}
		if key == glfw.KeyKP2 {
			g.DealResources(2)
		}
		if key == glfw.KeyKP3 {
			g.DealResources(3)
		}
		if key == glfw.KeyKP4 {
			g.DealResources(4)
		}
		if key == glfw.KeyKP5 {
			g.DealResources(5)
		}
		if key == glfw.KeyKP6 {
			g.DealResources(6)
		}
		if key == glfw.KeyKP8 {
			g.DealResources(8)
		}
		if key == glfw.KeyKP9 {
			g.DealResources(9)
		}
		if key == glfw.KeyKP0 {
			g.DealResources(10)
		}
		if key == glfw.KeyKP1 {
			g.DealResources(11)
		}
		if key == glfw.KeyKP7 {
			g.DealResources(12)
		}
		if len(lines) > 0 && key == glfw.KeyBackspace && (action == glfw.Press || action == glfw.Repeat) {
			if len(lines[len(lines)-1]) == 0 {
				lines = lines[:len(lines)-1]
			} else {
				lines[len(lines)-1] = removeLastRune(lines[len(lines)-1])
			}
		}
		if (action == glfw.Press || action == glfw.Repeat) &&
			(key == glfw.KeyEnter || key == glfw.KeyKPEnter) {
			lines = append(lines, "")
		}
	})
	window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)
	window.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
	window.SetSizeCallback(func(_ *glfw.Window, w, h int) {
		windowW, windowH = w, h
		cam.WindowWidth, cam.WindowHeight = w, h
		gl.Viewport(0, 0, int32(w), int32(h))
	})

	if err := loadImages(); err != nil {
		fmt.Println(err)
		return
	}

	var background image.Image
	var backImg *glImage
	go func() {
		gameW, gameH := 7*200, 7*tileYOffset+tileSlopeHeight
		back := image.NewRGBA(image.Rect(0, 0, gameW, gameH))
		clearToTransparent(back)
		drawGameBackgroundIntoImage(back, g)
		background = back
	}()

	gameColorToString := func(c game.Color) string {
		switch c {
		case game.Red:
			return "red"
		case game.Orange:
			return "orange"
		case game.Blue:
			return "blue"
		default:
			return "white"
		}
	}

	roadImage := func(pos game.TileEdge, c game.Color) *glImage {
		color := gameColorToString(c)
		dir := "up"
		if isEdgeVertical(pos) {
			dir = "vertical"
		}
		if isEdgeGoingDown(pos) {
			dir = "down"
		}
		return glImages["road_"+color+"_"+dir]
	}

	settlementImage := func(c game.Color) *glImage {
		return glImages["settlement_"+gameColorToString(c)]
	}

	cityImage := func(c game.Color) *glImage {
		return glImages["city_"+gameColorToString(c)]
	}

	// TODO this is for showing the road/settlement being built currently
	var movingSettlementCorner game.TileCorner
	var movingSettlementVisible bool
	var movingRoadEdge game.TileEdge
	var movingRoadVisible bool
	window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
		mouseX, mouseY = x, y
		gameX, gameY := cam.screenToGame(x, y)
		movingSettlementCorner, movingSettlementVisible = screenToCorner(gameX, gameY)
		movingRoadEdge, movingRoadVisible = screenToEdge(gameX, gameY)
	})
	window.SetCursorEnterCallback(func(_ *glfw.Window, entered bool) {
		mouseIn = entered
		if entered == false {
			movingSettlementVisible = false
			movingRoadVisible = true
		}
	})
	//////////////////////////////////////////////////////////////////

	drawGame := func() {
		if backImg == nil && background != nil {
			backImg, _ = NewGLImageFromImage(background)
		}
		if backImg != nil {
			backImg.DrawAtXY(0, 0)

			for _, p := range g.GetPlayers() {
				for _, r := range p.GetBuiltRoads() {
					x, y := edgeToScreen(r.Position)
					img := roadImage(r.Position, p.Color)
					img.DrawAtXY(x-img.Width/2, y-img.Height/2)
				}
				for _, s := range p.GetBuiltSettlements() {
					x, y := cornerToScreen(s.Position)
					img := settlementImage(p.Color)
					img.DrawAtXY(x-img.Width/2, y-(5*img.Height/8))
				}
				for _, c := range p.GetBuiltCities() {
					x, y := cornerToScreen(c.Position)
					img := cityImage(p.Color)
					img.DrawAtXY(x-img.Width/2, y-(5*img.Height/8))
				}
			}

			if state == buildingSettlement && movingSettlementVisible && mouseIn {
				color := [4]float32{1, 1, 1, 1}
				x, y := cornerToScreen(movingSettlementCorner)
				if !movingSettlementVisible || !g.CanBuildSettlementAt(movingSettlementCorner) {
					color = [4]float32{0.7, 0.7, 0.7, 0.7}
					x, y = cam.screenToGame(mouseX, mouseY)
				}
				img := settlementImage(g.GetCurrentPlayer().Color)
				// TODO duplicate code:
				img.DrawColoredAtXY(x-img.Width/2, y-(5*img.Height/8), color)
			}
			if state == buildingRoad && movingRoadVisible && mouseIn {
				color := [4]float32{1, 1, 1, 1}
				x, y := edgeToScreen(movingRoadEdge)
				if !(!movingRoadVisible || !g.CanBuildRoadAt(movingRoadEdge)) {
					img := roadImage(movingRoadEdge, g.GetCurrentPlayer().Color)
					// TODO duplicate code:
					img.DrawColoredAtXY(x-img.Width/2, y-(5*img.Height/8), color)
				}
			}

			x, y, w, h := tileToScreen(g.Robber.Position)
			robber := glImages["robber"]
			robber.DrawAtXY(x+(w-robber.Width)/2, y+(h-robber.Height)/2)
		}
	}

	buildMenu := &menu{color{0.5, 0.4, 0.8, 0.9}, rect{0, 500, 800, 250}}

	showCursor := 0
	start := time.Now()
	frames := 0

	window.SetSize(windowW, windowH)

	for !window.ShouldClose() {
		glfw.PollEvents()

		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		const controlsHeight = 0 // TODO reserve are for stats and menus
		gameW, gameH := 7.0*200, 7.0*tileYOffset+tileSlopeHeight+controlsHeight
		gameRatio := gameW / gameH
		windowRatio := float64(windowW) / float64(windowH)
		var left, right, bottom, top float64
		if windowRatio > gameRatio {
			// window is wider than game => borders left and right
			border := (windowRatio*gameH - gameW) / 2
			left, right = -border, border+gameW
			bottom, top = gameH, 0
		} else {
			// window is higher than game => borders on top and bottom
			border := (gameW/windowRatio - gameH) / 2
			left, right = 0, gameW
			bottom, top = gameH+border, -border
		}
		gl.Ortho(left, right, bottom, top, -1, 1)
		cam.Left, cam.Right, cam.Bottom, cam.Top = left, right, bottom, top
		gl.ClearColor(0, 0, 1, 1)
		gl.Clear(gl.COLOR_BUFFER_BIT)

		drawGame()

		lines = make([]string, g.PlayerCount*6)
		for i, player := range g.GetPlayers() {
			lines[i*6+0] = fmt.Sprintf("player %v has %v ore", i+1, player.Resources[game.Ore])
			lines[i*6+1] = fmt.Sprintf("player %v has %v grain", i+1, player.Resources[game.Grain])
			lines[i*6+2] = fmt.Sprintf("player %v has %v lumber", i+1, player.Resources[game.Lumber])
			lines[i*6+3] = fmt.Sprintf("player %v has %v wool", i+1, player.Resources[game.Wool])
			lines[i*6+4] = fmt.Sprintf("player %v has %v brick", i+1, player.Resources[game.Brick])
		}

		if len(lines) > 0 {
			stash.BeginDraw()
			const fontSize = 35
			const cursorText = "" //"_"
			cursor := ""
			if showCursor > 60 {
				cursor = cursorText
			}
			for i, line := range lines {
				output := line
				if i == len(lines)-1 {
					output += cursor
				}
				font.Write(output, 0, float64(i+1)*fontSize)
			}
			if len(lines) == 0 {
				font.Write(cursor, 0, fontSize)
			}
			stash.EndDraw()
		}

		buildMenu.draw()

		window.SwapBuffers()

		showCursor = (showCursor + 1) % 120
		frames++
		if time.Now().Sub(start).Seconds() >= 1.0 {
			fmt.Println(frames, "fps")
			frames = 0
			start = time.Now()
		}
	}
}
Пример #18
0
func (c *Context) Viewport(x, y, width, height int) {
	gl.Viewport(int32(x), int32(y), int32(width), int32(height))
}
Пример #19
0
func setViewport(width int, height int) {
	gl.Viewport(0, 0, int32(width), int32(height))

	gl.Ortho(-float64(width)/2, float64(width)/2,
		-float64(height)/2, float64(height)/2, -1.0, 1.0)
}
Пример #20
0
func main() {
	if err := glfw.Init(nopContextWatcher{}); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	window, err := glfw.CreateWindow(1536, 960, "", nil, nil)
	if err != nil {
		panic(err)
	}
	globalWindow = window
	window.MakeContextCurrent()

	window.SetInputMode(glfw.CursorMode, glfw.CursorHidden)

	if err := gl.Init(); nil != err {
		panic(err)
	}

	glfw.SwapInterval(1) // Vsync.

	framebufferSizeCallback := func(w *glfw.Window, framebufferSize0, framebufferSize1 int) {
		gl.Viewport(0, 0, int32(framebufferSize0), int32(framebufferSize1))

		var windowSize [2]int
		windowSize[0], windowSize[1] = w.GetSize()

		// Update the projection matrix
		gl.MatrixMode(gl.PROJECTION)
		gl.LoadIdentity()
		gl.Ortho(0, float64(windowSize[0]), float64(windowSize[1]), 0, -1, 1)
		gl.MatrixMode(gl.MODELVIEW)
	}
	{
		var framebufferSize [2]int
		framebufferSize[0], framebufferSize[1] = window.GetFramebufferSize()
		framebufferSizeCallback(window, framebufferSize[0], framebufferSize[1])
	}
	window.SetFramebufferSizeCallback(framebufferSizeCallback)

	var inputEventQueue []InputEvent
	mousePointer = &Pointer{VirtualCategory: POINTING}

	var lastMousePos mgl64.Vec2
	lastMousePos[0], lastMousePos[1] = window.GetCursorPos()
	MousePos := func(w *glfw.Window, x, y float64) {
		//fmt.Println("MousePos:", x, y)

		inputEvent := InputEvent{
			Pointer:    mousePointer,
			EventTypes: map[EventType]bool{SLIDER_EVENT: true, AXIS_EVENT: true},
			InputId:    0,
			Buttons:    nil,
			Sliders:    []float64{x - lastMousePos[0], y - lastMousePos[1]}, // TODO: Do this in a pointer general way?
			Axes:       []float64{x, y},
		}
		lastMousePos[0] = x
		lastMousePos[1] = y
		inputEventQueue = EnqueueInputEvent(inputEvent, inputEventQueue)
	}
	window.SetCursorPosCallback(MousePos)
	MousePos(window, lastMousePos[0], lastMousePos[1])

	gl.ClearColor(0.85, 0.85, 0.85, 1)

	rand.Seed(4)
	var widget = newMultitouchTestBoxWidget(mgl64.Vec2{600, 300}, rand.Intn(6))
	var widget2 = newMultitouchTestBoxWidget(mgl64.Vec2{600 + 210, 300 + 210}, rand.Intn(6))
	var widget3 = newMultitouchTestBoxWidget(mgl64.Vec2{600 + 210, 300}, rand.Intn(6))
	var widget4 = newMultitouchTestBoxWidget(mgl64.Vec2{600, 300 + 210}, rand.Intn(6))

	go func() {
		<-time.After(5 * time.Second)
		log.Println("trigger!")
		widget.color++ // HACK: Racy.

		glfw.PostEmptyEvent()
	}()

	for !window.ShouldClose() {
		//glfw.PollEvents()
		glfw.WaitEvents()

		// Process Input.
		inputEventQueue = ProcessInputEventQueue(inputEventQueue)

		gl.Clear(gl.COLOR_BUFFER_BIT)

		widget.Render()
		widget2.Render()
		widget3.Render()
		widget4.Render()

		mousePointer.Render()

		window.SwapBuffers()
		log.Println("swapped buffers")
		runtime.Gosched()
	}
}
Пример #21
0
func main() {
	flag.Parse()

	go func() {
		http.ListenAndServe("localhost:6060", nil)
	}()

	go func() {
		for {
			runtime.GC()
			time.Sleep(1)
		}
	}()

	if err := glfw.Init(); err != nil {
		log.Fatalln("failed to initialize glfw:", err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, glfw.True)
	glfw.WindowHint(glfw.Visible, glfw.False) // do not steal focus
	glfw.WindowHint(glfw.Samples, 4)

	glfw.WindowHint(glfw.ContextVersionMajor, 2)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)

	window, err := glfw.CreateWindow(800, 600, "Spector", nil, nil)
	if err != nil {
		panic(err)
	}
	window.MakeContextCurrent()
	window.Restore()
	window.SetPos(32, 64)

	if err := gl.Init(); err != nil {
		panic(err)
	}

	if err := gl.GetError(); err != 0 {
		fmt.Println("INIT", err)
	}

	startnano := time.Now().UnixNano()

	DrawList := draw.NewList()
	for !window.ShouldClose() {
		start := qpc.Now()
		if window.GetKey(glfw.KeyEscape) == glfw.Press {
			return
		}

		now := float64(time.Now().UnixNano()-startnano) / 1e9
		width, height := window.GetSize()

		{ // reset window
			gl.MatrixMode(gl.MODELVIEW)
			gl.LoadIdentity()

			gl.Viewport(0, 0, int32(width), int32(height))
			gl.Ortho(0, float64(width), float64(height), 0, 30, -30)
			gl.ClearColor(1, 1, 1, 1)
			gl.Clear(gl.COLOR_BUFFER_BIT)
		}

		DrawList.Reset()

		DrawList.AddRectFill(&draw.Rectangle{
			draw.Vector{10, 10},
			draw.Vector{50, 50},
		}, draw.Red)

		CircleRadius := float32(50.0 * math.Sin(now*1.3))
		DrawList.AddCircle(
			draw.Vector{100, 100}, CircleRadius, draw.Red)
		DrawList.AddArc(
			draw.Vector{200, 100}, CircleRadius/2+50,
			float32(now),
			float32(math.Sin(now)*10),
			draw.ColorHSL(float32(math.Sin(now*0.3)), 0.8, 0.5))

		LineWidth := float32(math.Sin(now*2.1)*5 + 5)

		LineCount := int(width / 8)
		line := make([]draw.Vector, LineCount)
		for i := range line {
			r := float64(i) / float64(LineCount-1)
			line[i].X = float32(r) * float32(width)
			line[i].Y = float32(height)*0.5 + float32(math.Sin(r*11.8+now)*100)
		}
		DrawList.AddLine(line[:], LineWidth,
			draw.ColorHSL(float32(math.Sin(now*0.3)), 0.6, 0.6))

		CircleCount := int(width / 8)
		circle := make([]draw.Vector, CircleCount)
		for i := range circle {
			p := float64(i) / float64(CircleCount)
			a := now + p*math.Pi*2
			w := math.Sin(p*62)*20.0 + 100.0
			circle[i].X = float32(width)*0.5 + float32(math.Cos(a)*w)
			circle[i].Y = float32(height)*0.5 + float32(math.Sin(a)*w)
		}

		// DrawList.PushClip(draw.Rect(0, 0, float32(width)/2, float32(height)/2))
		DrawList.AddClosedLine(circle[:], LineWidth, draw.Green)
		// DrawList.PopClip()

		render.List(width, height, DrawList)
		if err := gl.GetError(); err != 0 {
			fmt.Println(err)
		}
		stop := qpc.Now()

		window.SwapBuffers()
		runtime.GC()
		glfw.PollEvents()

		fmt.Printf("%-10.3f\n", stop.Sub(start).Duration().Seconds()*1000)
	}

}
Пример #22
0
// Viewport sets the viewport, an affine transformation that
// normalizes device coordinates to window coordinates.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glViewport.xhtml
func Viewport(x, y, width, height int) {
	gl.Viewport(int32(x), int32(y), int32(width), int32(height))
}
Пример #23
0
func (cam *camera) windowSizeChangedTo(width, height int) {
	gl.Viewport(0, 0, int32(width), int32(height))
	cam.WindowWidth, cam.WindowHeight = width, height
	cam.recalcOrthoBorders()
}