Ejemplo n.º 1
0
func MainLoop(gs *gamestate.GameState, renderer *rendering.WorldRenderer) {
	var frames int
	time := float32(sdl.GetTicks()) / 1000
	window := gs.Window
	running := true

	for running {
		currentTime := float32(sdl.GetTicks()) / 1000
		if currentTime > time+1 {
			gs.Fps = float32(frames)
			frames = 0
			time = currentTime
		}
		frames += 1
		running = Input(gs, renderer)

		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.Disable(gl.BLEND)

		simulation.Simulate(gs, renderer.ParticleSystem)
		renderer.View = gs.Camera.View()
		renderer.Render(gs.World, &gs.Options, window)

		tw.Draw()
		sdl.GL_SwapWindow(window)

		helpers.UpdateManagers()
	}
}
Ejemplo n.º 2
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)
	}
}
Ejemplo n.º 3
0
func windowLoop(window *sdl.Window) {
	data := &renderData{}

	data.renderSections = true
	data.zoom = -250
	data.nodes = loadTree("cmd/packer/test.priv.oct")
	data.box = genBox()
	defer gl.DeleteLists(data.box, 1)

	buttonDown := false
	t := float64(sdl.GetTicks())
	for {
		ticks := float64(sdl.GetTicks()) * 0.001
		dt := ticks - t
		t = ticks

		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				return
			case *sdl.KeyDownEvent:
				switch t.Keysym.Sym {
				case sdl.K_ESCAPE:
					return
				case sdl.K_SPACE:
					data.renderSections = !data.renderSections
				case sdl.K_PLUS:
					data.minNodeSize += 1.0
				case sdl.K_MINUS:
					data.minNodeSize -= 1.0
				}
			case *sdl.MouseButtonEvent:
				if t.State == 1 {
					buttonDown = true
				} else {
					buttonDown = false
				}
			case *sdl.MouseMotionEvent:
				if buttonDown {
					data.xrot += dt * float64(t.YRel) * rotSpeed
					data.yrot += dt * float64(t.XRel) * rotSpeed
				}
			case *sdl.MouseWheelEvent:
				data.zoom += dt * float64(t.Y) * rotSpeed
			}
		}

		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		renderTree(data, &data.nodes[0], point3d{0, 0, 0}, 100)

		gl.Disable(gl.DEPTH_TEST)
		drawAxis(data)
		gl.Enable(gl.DEPTH_TEST)

		sdl.GL_SwapWindow(window)

		if glErr := gl.GetError(); glErr != gl.NO_ERROR {
			panic(fmt.Errorf("GL error: %x", glErr))
		}
	}
}
Ejemplo n.º 4
0
// SwapBuffers swaps the current frame buffer in the window. This is generally
// only used by the engine but you could use it to roll your own game loop.
func SwapBuffers() {
	sdl.GL_SwapWindow(getCurrent().sdl_window)
}
Ejemplo n.º 5
0
func (w Window) Refresh() {
	sdl.Delay(1)
	sdl.GL_SwapWindow(w.windowSDL)

}
Ejemplo n.º 6
0
func main() {
	var window *sdl.Window
	var context sdl.GLContext
	var event sdl.Event
	var running bool
	var err error
	runtime.LockOSThread()
	if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
		panic(err)
	}
	defer sdl.Quit()
	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.Init()
	gl.Viewport(0, 0, gl.Sizei(winWidth), gl.Sizei(winHeight))
	// OPENGL FLAGS
	gl.ClearColor(0.0, 0.1, 0.0, 1.0)
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)

	// VERTEX BUFFER
	var vertexbuffer gl.Uint
	gl.GenBuffers(1, &vertexbuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_vertices)*4), gl.Pointer(&triangle_vertices[0]), gl.STATIC_DRAW)

	// COLOUR BUFFER
	var colourbuffer gl.Uint
	gl.GenBuffers(1, &colourbuffer)
	gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangle_colours)*4), gl.Pointer(&triangle_colours[0]), gl.STATIC_DRAW)

	// GUESS WHAT
	program := createprogram()

	// VERTEX ARRAY
	var VertexArrayID gl.Uint
	gl.GenVertexArrays(1, &VertexArrayID)
	gl.BindVertexArray(VertexArrayID)
	gl.EnableVertexAttribArray(0)
	gl.BindBuffer(gl.ARRAY_BUFFER, vertexbuffer)
	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 0, nil)

	// VERTEX ARRAY HOOK COLOURS
	gl.EnableVertexAttribArray(1)
	gl.BindBuffer(gl.ARRAY_BUFFER, colourbuffer)
	gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 0, nil)

	//UNIFORM HOOK
	unistring := gl.GLString("scaleMove")
	UniScale = gl.GetUniformLocation(program, unistring)
	fmt.Printf("Uniform Link: %v\n", UniScale+1)

	gl.UseProgram(program)

	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:

				xrot = float32(t.Y) / 2
				yrot = float32(t.X) / 2
				fmt.Printf("[%dms]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)
	}
}
Ejemplo n.º 7
0
Archivo: window.go Proyecto: fmd/gogol
// Swaps the OpenGL window.
// Call this after everything has been rendered.
func (w *Window) Swap() {
	sdl.GL_SwapWindow(w.Window)
}
Ejemplo n.º 8
0
Archivo: window.go Proyecto: FSX/nanovg
func StartGame() {
	window := initSDL()
	context := initGL(window)
	nvg := initNanovg()

	ww, wh := window.GetSize()
	gw := ww / 5
	gh := wh / 5

	life := NewLife(gw, gh)

	refgrid := make([][]bool, gh)
	for i := range refgrid {
		refgrid[i] = make([]bool, gw)
	}

	// Run the game at 15fps. This gives us about 66ms to calculate
	// and render everything. 30ms is not possible yet.
	minms := uint32(1000 / 15)
	running := true

	gl.ClearColor(0.3, 0.3, 0.32, 1.0)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)

	// Draw all squares first.
	tw, th := float32(life.w), float32(life.h)
	for y := float32(0); y < th; y++ {
		for x := float32(0); x < tw; x++ {
			nvg.BeginPath()
			nvg.Rect(x*5+1, y*5+1, 3, 3)
			nvg.FillColor(nanovg.RGBA(100, 100, 100, 255))
			nvg.Fill()
			nvg.ClosePath()
		}
	}

	// Loop!
	for running {
		lt := sdl.GetTicks()

		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.KeyUpEvent:
				if t.Keysym.Sym == sdl.K_ESCAPE {
					running = false
				}
			}
		}

		life.Step()

		ww, wh := window.GetSize()
		gl.Viewport(0, 0, ww, wh)

		nvg.BeginFrame(ww, wh, 1.0)
		drawGrid(life, nvg, refgrid)
		nvg.EndFrame()

		if d := sdl.GetTicks() - lt; d < minms {
			sdl.Delay(minms - d)
		}

		sdl.GL_SwapWindow(window)
	}

	// Cleanup.
	window.Destroy()
	sdl.GL_DeleteContext(context)
	nvg.Close()
	sdl.Quit()
}
Ejemplo n.º 9
0
func windowLoop(window *sdl.Window, cameraMatrixUniform int32) {
	var (
		yrot       float32
		xrot       float32
		buttonDown bool
	)

	numFrame := 0
	count := 0
	t := time.Now()

	for {
		c := time.Now()
		dt := c.Sub(t)
		t = c

		if numFrame > count {
			fmt.Println("\rDeltaTime:", dt)
			count += 5
		}
		numFrame++

		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				return
			case *sdl.KeyDownEvent:
				if t.Keysym.Sym == sdl.K_ESCAPE {
					return
				}
			case *sdl.MouseButtonEvent:
				if t.State == 1 {
					buttonDown = true
				} else {
					buttonDown = false
				}
			case *sdl.MouseMotionEvent:
				if buttonDown {
					xrot += float32(t.YRel) * 0.001
					yrot += float32(t.XRel) * 0.001
				}
			}
		}

		gl.Clear(gl.COLOR_BUFFER_BIT)

		xq := mgl32.QuatRotate(xrot, mgl32.Vec3{1, 0, 0})
		yq := mgl32.QuatRotate(yrot, mgl32.Vec3{0, 1, 0})

		mat := xq.Mul(yq).Mat4()
		//mat = mat.Mul4(mgl32.Translate3D(0,0,-0.5))

		gl.UniformMatrix4fv(cameraMatrixUniform, 1, false, &mat[0])

		gl.DrawArrays(gl.TRIANGLES, 0, 6)

		sdl.GL_SwapWindow(window)
		if glErr := gl.GetError(); glErr != gl.NO_ERROR {
			panic(fmt.Errorf("GL error, swap: %x", glErr))
		}
	}
}