Example #1
0
// NewRenderer creates a new Renderer with the given window size and title.
func NewRenderer(window *Window) (*Renderer, error) {
	logger.Debug("Initializing GLEW")

	// Init glew
	if gl.Init() != 0 {
		return nil, errors.New("Could not initialise glew.")
	}
	gl.GetError()

	if window.Settings.ClearColor != nil {
		color := window.Settings.ClearColor
		gl.ClearColor(
			gl.GLclampf(color.R()),
			gl.GLclampf(color.G()),
			gl.GLclampf(color.B()),
			0.,
		)
	}

	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)

	gl.Enable(gl.CULL_FACE)

	// Vertex buffers
	vertexArray := gl.GenVertexArray()
	vertexArray.Bind()

	renderer := Renderer{
		vertexArray: vertexArray,
		window:      window,
	}
	return &renderer, nil
}
Example #2
0
func fill_background(args List, context *Context) Data {
	red := args.First().(Float)
	green := args.Second().(Float)
	blue := args.Third().(Float)
	alpha := args.Get(3).(Float)

	graphicsQueue.Enqueue(func() {
		gl.ClearColor(gl.GLclampf(red.Value), gl.GLclampf(green.Value),
			gl.GLclampf(blue.Value), gl.GLclampf(alpha.Value))
	})

	return Nothing{}
}
Example #3
0
func animate() {
	now := float64(time.Since(seconds)) / float64(time.Second)

	offset := [4]float32{
		float32(math.Sin(now)),
		float32(math.Cos(now)),
		0.0, 0.0}
	attr.Attrib4fv(&offset)

	red := gl.GLclampf(math.Sin(now)*0.25 + 0.75)
	blue := gl.GLclampf(math.Cos(now)*0.25 + 0.75)
	green := gl.GLclampf(time.Since(seconds))
	_ = green

	gl.ClearColor(red, blue, 0.2, 0.0)
}
Example #4
0
func initWindow() (window *glfw.Window, err error) {
	monitor, err := glfw.GetPrimaryMonitor()
	if err != nil {
		return nil, err
	}
	videomode, err := monitor.GetVideoMode()
	if err != nil {
		return nil, err
	}
	if videomode.Height < 480 || videomode.Width < 640 {
		return nil, errors.New("unsupported resolution!")
	}

	ratio := float64(videomode.Width) / float64(videomode.Height)

	if fullscreen {
		glfw.WindowHint(glfw.Decorated, 0)
		window, err = glfw.CreateWindow(videomode.Width, videomode.Height, "Golang Asteroids!", nil, nil)
		if err != nil {
			return nil, err
		}
		window.SetPosition(0, 0)
	} else {
		glfw.WindowHint(glfw.Decorated, 1)
		window, err = glfw.CreateWindow(int(ratio*480), 480, "Golang Asteroids!", nil, nil)
		if err != nil {
			return nil, err
		}
		window.SetPosition(videomode.Width/2-320, videomode.Height/2-240)
	}

	window.SetKeyCallback(keyCallback)
	window.SetFramebufferSizeCallback(reshapeWindow)
	window.MakeContextCurrent()

	gl.Init()
	gl.ClearColor(gl.GLclampf(Colorize(0)), gl.GLclampf(Colorize(0)), gl.GLclampf(Colorize(0)), 0.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	width, height := window.GetFramebufferSize()
	reshapeWindow(window, width, height)

	return window, nil
}
Example #5
0
// Clamps a value to a max.
func (c *Controller) clamp(i int, max int) gl.GLclampf {
	return gl.GLclampf(float64(i) / float64(max))
}
Example #6
0
func (c *Camera) Clear() {
	gl.ClearColor(gl.GLclampf(c.clearColor.R), gl.GLclampf(c.clearColor.G), gl.GLclampf(c.clearColor.B), gl.GLclampf(c.clearColor.A))
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
}
Example #7
0
func (d *DisplayEngine) drawFrame() {
	toDraw := make([][]*resources.Bitmap, d.config.MapW*d.config.MapH)
	drawPasses := 0
	for x := 0; x < d.config.MapW; x++ {
		for y := 0; y < d.config.MapH; y++ {
			toDraw[x*d.config.MapW+y] = (*d.gameEngine).GetTile(x, y)
			length := len(toDraw[x*d.config.MapW+y])
			if length > drawPasses {
				drawPasses = length
			}
		}
	}

	viewport := d.viewport
	font := allegro.CreateBuiltinFont()

	// Don't want anyone changing the viewport mid frame or any such highjinks
	d.Display.SetTargetBackbuffer()

	allegro.RunInThread(func() {
		r, g, b, a := d.config.BGColor.GetRGBA()
		gl.ClearColor(
			gl.GLclampf(r)/255.0,
			gl.GLclampf(g)/255.0,
			gl.GLclampf(b)/255.0,
			gl.GLclampf(a)/255.0)

		gl.Clear(gl.COLOR_BUFFER_BIT)

		viewport.SetupTransform()

		for p := 0; p < drawPasses; p++ {
			m := d.config.MapW
			n := d.config.MapH
			for s := 0; s < m+n; s++ {
				for x := 0; x < s; x++ {
					y := s - x - 1
					if x >= m || y < 0 || y >= n {
						continue
					}
					if len(toDraw[x*d.config.MapW+y]) < p {
						continue
					}

					// Coordinates in terms of pixels
					px := (y - x) * d.config.TileW / 2
					py := (x + y) * d.config.TileH / 2
					bmp := toDraw[x*d.config.MapW+y][p]
					/*					ox := bmp.OffX
										oy := bmp.OffY*/
					bw, bh := bmp.W, bmp.H
					if viewport.OnScreen(px, py, bw, bh) {
						gl.Begin(gl.QUADS)
						bmp.Tex.Bind(gl.TEXTURE_2D)
						gl.TexCoord2f(0, 0)
						gl.Vertex3i(px, py, 0)
						gl.TexCoord2f(0, 1)
						gl.Vertex3i(px, py+bw, 0)
						gl.TexCoord2f(1, 1)
						gl.Vertex3i(px+bh, py+bw, 0)
						gl.TexCoord2f(1, 0)
						gl.Vertex3i(px+bh, py, 0)
						gl.End()
					}
				}
			}
		}

		gl.Flush()
	})

	var trans allegro.Transform
	trans.Identity()
	trans.Use()

	font.Draw(allegro.CreateColor(0, 255, 0, 255), 0, 0, 0, fmt.Sprint(int(d.fps)))

	allegro.Flip()

	d.frameDrawing.Unlock()
}
Example #8
0
func (r *RenderTarget) Clear(color Color) {
	gl.ClearColor(gl.GLclampf(color.R/255), gl.GLclampf(color.G/255), gl.GLclampf(color.B/255), gl.GLclampf(color.A/255))
	gl.Clear(gl.COLOR_BUFFER_BIT)
}
Example #9
0
func switchColors() {
	colorsInverted = !colorsInverted
	gl.ClearColor(gl.GLclampf(Colorize(0)), gl.GLclampf(Colorize(0)), gl.GLclampf(Colorize(0)), 0.0)
	gl.Clear(gl.COLOR_BUFFER_BIT)
}
Example #10
0
func (r *RenderTarget) Clear(color Color) {
	gl.ClearColor(gl.GLclampf(float32(color.R)/255), gl.GLclampf(float32(color.G)/255),
		gl.GLclampf(float32(color.B)/255), gl.GLclampf(float32(color.A)/255))
	gl.Clear(gl.COLOR_BUFFER_BIT)
}