Example #1
0
func NewGLRenderer(view *View, title string) *GLRenderer {
	r := new(GLRenderer)
	r.View = view
	GL <- func() {
		if sdl.Init(sdl.INIT_VIDEO) < 0 {
			panic(sdl.GetError())
		}
		sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
		sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)
		r.Screen = sdl.SetVideoMode(view.Width, view.Height, 32, sdl.OPENGL)
		if r.Screen == nil {
			panic(sdl.GetError())
		}
		sdl.WM_SetCaption(title, title)
		err := gl33.Init()
		if err != nil {
			panic(err)
		}
		gl33.Enable(gl33.BLEND)
		gl33.Enable(gl33.CULL_FACE)
		gl33.Enable(gl33.DEPTH_TEST)
		gl33.ClearColor(1.0, 1.0, 1.0, 1.0)
		gl33.BlendFunc(gl33.SRC_ALPHA, gl33.ONE_MINUS_SRC_ALPHA)
		gl33.DepthFunc(gl33.LEQUAL)
		gl33.Viewport(0, 0, gl33.Sizei(view.Width), gl33.Sizei(view.Height))
	}
	r.PerspectiveMatrix = s3dm.PerspectiveMatrix(view.Fovy, view.Aspect, view.Near, view.Far)
	r.OrthographicMatrix = s3dm.OrthographicMatrix(float64(view.Width), float64(view.Height), 0, 1)
	return r
}
Example #2
0
func main() {
	runtime.LockOSThread()
	flag.Parse()
	if *workers <= 0 {
		*workers = 1
	}
	runtime.GOMAXPROCS(*workers + 1)
	buildPalette()
	sdl.Init(sdl.INIT_VIDEO)
	defer sdl.Quit()

	if !*noVSync {
		sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)
	}

	if sdl.SetVideoMode(512, 512, 32, sdl.OPENGL) == nil {
		panic("sdl error")
	}

	sdl.WM_SetCaption("Gomandel", "Gomandel")

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

	gl.Enable(gl.TEXTURE_2D)
	gl.Viewport(0, 0, 512, 512)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, 512, 512, 0, -1, 1)

	gl.ClearColor(0, 0, 0, 0)

	//-----------------------------------------------------------------------------
	var dndDragging bool = false
	var dnd3 bool = false
	var dndStart Point
	var dndEnd Point
	initialRect := Rect{-1.5, -1.5, 3, 3}
	rect := initialRect

	tm := NewTileManager(512, 512)
	tm.ZoomRequest(&rect)

	running := true
	for running {
		for {
			var event interface{}
			select {
			case event = <-sdl.Events:
			default:
			}

			if event == nil {
				break
			}

			switch e := event.(type) {
			case sdl.QuitEvent:
				running = false
			case sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					dndDragging = true
					dndStart.X = gl.Int(e.X)
					dndStart.Y = gl.Int(e.Y)
					dndEnd = dndStart
					if e.Button == 3 {
						dnd3 = true
					} else {
						dndDragging = true
					}
				} else {
					dndDragging = false
					dndEnd.X = gl.Int(e.X)
					dndEnd.Y = gl.Int(e.Y)

					switch e.Button {
					case 1:
						rect = rectFromSelection(dndStart, dndEnd, 512, 512, rect)
						tm.ZoomRequest(&rect)
					case 2:
						rect = initialRect
						tm.ZoomRequest(&rect)
					case 3:
						dnd3 = false
					}
				}
			case sdl.MouseMotionEvent:
				if dnd3 {
					dndEnd.X = gl.Int(e.X)
					dndEnd.Y = gl.Int(e.Y)
					rect = moveRectBy(rect, dndStart, dndEnd, 512, 512)
					tm.MoveRequest(rect)
					dndStart = dndEnd
				} else if dndDragging {
					dndEnd.X = gl.Int(e.X)
					dndEnd.Y = gl.Int(e.Y)
				}
			}
		}
		tm.Update()
		gl.Clear(gl.COLOR_BUFFER_BIT)
		tm.Draw()
		gl.BindTexture(gl.TEXTURE_2D, 0)
		if dndDragging {
			drawSelection(dndStart, dndEnd)
		}
		sdl.GL_SwapBuffers()
	}
}