Exemplo n.º 1
0
func main() {

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(640, 480, 32, sdl.OPENGL)

	if screen == nil {
		panic("sdl error")
	}

	if gl.Init() != 0 {
		panic("gl error")
	}

	pen := Pen{}

	gl.MatrixMode(gl.PROJECTION)

	gl.Viewport(0, 0, int(screen.W), int(screen.H))
	gl.LoadIdentity()
	gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0)

	gl.ClearColor(1, 1, 1, 0)
	gl.Clear(gl.COLOR_BUFFER_BIT)

	var running = true

	for running {

		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch ev := e.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.KeyboardEvent:
				if ev.Keysym.Sym == sdl.K_ESCAPE {
					running = false
				}
			case *sdl.MouseMotionEvent:
				if ev.State != 0 {
					pen.lineTo(Point{int(ev.X), int(ev.Y)})
				} else {
					pen.moveTo(Point{int(ev.X), int(ev.Y)})
				}
			}
		}

		sdl.GL_SwapBuffers()
		sdl.Delay(25)
	}

	sdl.Quit()

}
Exemplo n.º 2
0
func main() {

	flag.Parse()

	var done bool
	var keys []uint8

	sdl.Init(sdl.INIT_VIDEO)

	var screen = sdl.SetVideoMode(300, 300, 16, sdl.OPENGL|sdl.RESIZABLE)

	if screen == nil {
		sdl.Quit()
		panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
	}

	if gl.Init() != 0 {
		panic("gl error")
	}

	sdl.WM_SetCaption("Gears", "gears")

	init_()
	reshape(int(screen.W), int(screen.H))
	done = false
	for !done {

		idle()
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.ResizeEvent:
				re := e.(*sdl.ResizeEvent)
				screen = sdl.SetVideoMode(int(re.W), int(re.H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					reshape(int(screen.W), int(screen.H))
				} else {
					panic("we couldn't set the new video mode??")
				}
				break

			case *sdl.QuitEvent:
				done = true
				break
			}
		}
		keys = sdl.GetKeyState()

		if keys[sdl.K_ESCAPE] != 0 {
			done = true
		}
		if keys[sdl.K_UP] != 0 {
			view_rotx += 5.0
		}
		if keys[sdl.K_DOWN] != 0 {
			view_rotx -= 5.0
		}
		if keys[sdl.K_LEFT] != 0 {
			view_roty += 5.0
		}
		if keys[sdl.K_RIGHT] != 0 {
			view_roty -= 5.0
		}
		if keys[sdl.K_z] != 0 {
			if (sdl.GetModState() & sdl.KMOD_RSHIFT) != 0 {
				view_rotz -= 5.0
			} else {
				view_rotz += 5.0
			}
		}

		draw()
	}
	sdl.Quit()
	return

}
Exemplo n.º 3
0
func Loop(onKeyDown, onKeyUp func(*sdl.KeyboardEvent)) {
	var nowTime = time.Now()
	var lastSecond, lastTime = nowTime, nowTime
	var nowNano int64 = 0
	var durSec time.Duration
	var sdlEvt sdl.Event
	var winw, winh int
	var retro int = int(Canvas0.RetroFactor)
	var canvas0RetroFactor = 1 / Canvas0.RetroFactor
	gl.Enable(gl.FRAMEBUFFER_SRGB)
	for Looping {
		nowTime = time.Now()
		pipeline.TimeLast, pipeline.TimeNow = lastTime, nowTime
		pipeline.TimeSecsElapsed = nowTime.Sub(lastTime).Seconds()
		lastTime = nowTime
		nowNano = nowTime.UnixNano()
		Canvas0.TimeElapsed = nowNano - StartTime
		durSec = nowTime.Sub(lastSecond)
		if sdlEvt = sdl.PollEvent(); sdlEvt != nil {
			switch event := sdlEvt.(type) {
			case *sdl.ResizeEvent:
				winw, winh = int(event.W), int(event.H)
				if retro > 0 {
					for (winw % retro) != 0 {
						winw--
					}
					for (winh % retro) != 0 {
						winh--
					}
					if (winw / retro) < 32 {
						winw = 32 * retro
					}
					if (winh / retro) < 32 {
						winh = 32 * retro
					}
				}
				ReinitVideo(winw, winh, true, true)
				RefreshWindowCaption()
			case *sdl.QuitEvent:
				Looping = false
				return
			case *sdl.KeyboardEvent:
				if event.Type == sdl.KEYUP {
					KeysPressed[event.Keysym.Sym] = false
					if event.Keysym.Sym == KeySymTmpTexUp {
						LoadTmpTex(TmpTexIndex + 1)
					}
					if event.Keysym.Sym == KeySymTmpTexDn {
						LoadTmpTex(TmpTexIndex - 1)
					}
					// if event.Keysym.Sym == KeySymOctreeLevelUp { softpipeline.OctreeMaxLevel++ }
					// if event.Keysym.Sym == KeySymOctreeLevelDown { softpipeline.OctreeMaxLevel-- }
					onKeyUp(event)
				} else if event.Type == sdl.KEYDOWN {
					KeysPressed[event.Keysym.Sym] = true
					onKeyDown(event)
				}
			case *sdl.MouseMotionEvent:
				MouseX, MouseY = float64(event.X)*canvas0RetroFactor, float64(event.Y)*canvas0RetroFactor
				Canvas0.MouseX, Canvas0.MouseY = gl.Float(MouseX*Canvas0.InvWidth), gl.Float(MouseY*Canvas0.InvHeight)
			}
		}
		pipeline.CamMove, pipeline.CamTurn = false, false
		if KeysPressed[sdl.K_UP] {
			pipeline.CamMove, pipeline.CamMoveFwd = true, 1
		} else {
			pipeline.CamMoveFwd = 0
		}
		if KeysPressed[sdl.K_DOWN] {
			pipeline.CamMove, pipeline.CamMoveBack = true, 1
		} else {
			pipeline.CamMoveBack = 0
		}
		if KeysPressed[sdl.K_LEFT] {
			pipeline.CamTurn, pipeline.CamTurnLeft = true, 1
		} else {
			pipeline.CamTurnLeft = 0
		}
		if KeysPressed[sdl.K_RIGHT] {
			pipeline.CamTurn, pipeline.CamTurnRight = true, 1
		} else {
			pipeline.CamTurnRight = 0
		}
		if KeysPressed[sdl.K_PAGEUP] {
			pipeline.CamTurn, pipeline.CamTurnUp = true, 1
		} else {
			pipeline.CamTurnUp = 0
		}
		if KeysPressed[sdl.K_PAGEDOWN] {
			pipeline.CamTurn, pipeline.CamTurnDown = true, 1
		} else {
			pipeline.CamTurnDown = 0
		}
		// if KeysPressed[sdl.K_RSHIFT] { pipeline.SpeedUp = 10 } else { pipeline.SpeedUp = 1 }
		if KeysPressed[sdl.K_a] {
			pipeline.CamMove, pipeline.CamMoveLeft = true, 1
		} else {
			pipeline.CamMoveLeft = 0
		}
		if KeysPressed[sdl.K_d] {
			pipeline.CamMove, pipeline.CamMoveRight = true, 1
		} else {
			pipeline.CamMoveRight = 0
		}
		if KeysPressed[sdl.K_w] {
			pipeline.CamMove, pipeline.CamMoveUp = true, 1
		} else {
			pipeline.CamMoveUp = 0
		}
		if KeysPressed[sdl.K_s] {
			pipeline.CamMove, pipeline.CamMoveDown = true, 1
		} else {
			pipeline.CamMoveDown = 0
		}
		if durSec.Seconds() < 1 {
			Fps++
		} else {
			RefreshWindowCaption()
			Fps = 0
			lastSecond = nowTime
		}

		pipeline.PreRender()
		// gl.Disable(gl.FRAMEBUFFER_SRGB)
		Canvas0.RenderContent()
		// Canvas0.RenderPostFx()
		// gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
		// gl.Viewport(0, 0, gl.Sizei(Screen.W), gl.Sizei(Screen.H))
		// Canvas0.RenderSelf()

		sdl.GL_SwapBuffers()
		glutil.PanicIfErrors("Post Render Loop")
	}
}
Exemplo n.º 4
0
func main() {
	runtime.LockOSThread()
	flag.Parse()
	buildPalette()
	sdl.Init(sdl.INIT_VIDEO)
	defer sdl.Quit()

	sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)

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

	if gl.Init() != 0 {
		panic("gl error")
	}

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

	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 dndStart Point
	var dndEnd Point
	var tex gl.Texture
	var tc TexCoords
	var lastProgress int
	initialRect := Rect{-1.5, -1.5, 3, 3}
	rect := initialRect

	rc := new(MandelbrotRequest)
	rc.MakeRequest(512, 512, rect)
	rc.WaitFor(Small, &tex, &tc)

	running := true
	for running {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseButtonEvent:
				mbe := e.(*sdl.MouseButtonEvent)
				if mbe.Type == sdl.MOUSEBUTTONDOWN {
					dndDragging = true
					sdl.GetMouseState(&dndStart.X, &dndStart.Y)
					dndEnd = dndStart
				} else {
					dndDragging = false
					sdl.GetMouseState(&dndEnd.X, &dndEnd.Y)
					if mbe.Which == 3 {
						rect = initialRect
					} else {
						rect = rectFromSelection(dndStart, dndEnd, 512, 512, rect)
						tc = texCoordsFromSelection(dndStart, dndEnd, 512, 512, tc)
					}

					// make request
					rc.MakeRequest(512, 512, rect)
				}
			case *sdl.MouseMotionEvent:
				if dndDragging {
					sdl.GetMouseState(&dndEnd.X, &dndEnd.Y)
				}
			}
		}

		// if we're waiting for a result, check if it's ready
		p := rc.Update(&tex, &tc)
		if p != -1 {
			lastProgress = p
		}

		gl.Clear(gl.COLOR_BUFFER_BIT)
		tex.Bind(gl.TEXTURE_2D)
		drawQuad(0, 0, 512, 512, tc.TX, tc.TY, tc.TX2, tc.TY2)
		gl.BindTexture(gl.TEXTURE_2D, 0)
		if dndDragging {
			drawSelection(dndStart, dndEnd)
		}
		drawProgress(512, 512, lastProgress, rc.Pending)
		sdl.GL_SwapBuffers()
	}
}
Exemplo n.º 5
0
func main() {

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		panic(sdl.GetError())
	}

	var screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)

	if screen == nil {
		panic(sdl.GetError())
	}

	var video_info = sdl.GetVideoInfo()

	println("HW_available = ", video_info.HW_available)
	println("WM_available = ", video_info.WM_available)
	println("Video_mem = ", video_info.Video_mem, "kb")

	sdl.EnableUNICODE(1)

	sdl.WM_SetCaption("Go-SDL SDL Test", "")

	image := sdl.Load("test.png")

	if image == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetIcon(image, nil)

	running := true

	font := ttf.OpenFont("Fontin Sans.otf", 72)

	if font == nil {
		panic(sdl.GetError())
	}

	font.SetStyle(ttf.STYLE_UNDERLINE)
	white := sdl.Color{255, 255, 255, 0}
	text := ttf.RenderText_Blended(font, "Test (with music)", white)
	music := mixer.LoadMUS("test.ogg")
	sound := mixer.LoadWAV("sound.ogg")

	if music == nil || sound == nil {
		panic(sdl.GetError())
	}

	music.PlayMusic(-1)

	if sdl.GetKeyName(270) != "[+]" {
		panic("GetKeyName broken")
	}

	worm_in := make(chan Point)
	draw := make(chan Point, 64)

	var out chan Point
	var in chan Point

	out = worm_in

	in = out
	out = make(chan Point)
	go worm(in, out, draw)

	for running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyboardEvent:
				println("")
				println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))

				if e.Keysym.Sym == 27 {
					running = false
				}

				fmt.Printf("%04x ", e.Type)

				for i := 0; i < len(e.Pad0); i++ {
					fmt.Printf("%02x ", e.Pad0[i])
				}
				println()

				fmt.Printf("Type: %02x Which: %02x State: %02x Pad: %02x\n", e.Type, e.Which, e.State, e.Pad0[0])
				fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", e.Keysym.Scancode, e.Keysym.Sym, e.Keysym.Mod, e.Keysym.Unicode)
			case *sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					println("Click:", e.X, e.Y)
					in = out
					out = make(chan Point)
					go worm(in, out, draw)
					sound.PlayChannel(-1, 0)
				}
			case *sdl.ResizeEvent:
				println("resize screen ", e.W, e.H)

				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)

				if screen == nil {
					panic(sdl.GetError())
				}
			}
		}

		screen.FillRect(nil, 0x302019)
		screen.Blit(&sdl.Rect{0, 0, 0, 0}, text, nil)

		loop := true

		for loop {

			select {
			case p := <-draw:
				screen.Blit(&sdl.Rect{int16(p.x), int16(p.y), 0, 0}, image, nil)
			case <-out:
			default:
				loop = false
			}

		}

		var p Point
		sdl.GetMouseState(&p.x, &p.y)
		worm_in <- p

		screen.Flip()
		sdl.Delay(25)
	}

	image.Free()
	music.Free()
	font.Close()

	ttf.Quit()
	sdl.Quit()
}
Exemplo n.º 6
0
func main() {
	var now = time.Now()
	initialWidth, initialHeight := 1280, 720
	runtime.LockOSThread()
	if sdlInit := sdl.Init(sdl.INIT_VIDEO); sdlInit != 0 {
		panic("SDL init error")
	}
	reinitScreen(initialWidth, initialHeight)
	defer cleanExit(true, false)
	if err := gl.Init(); err != nil {
		panic(err)
	}
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Enable(gl.DEPTH)
	sdl.WM_SetCaption("Loading volume...", "")
	loadVolume()
	defer cleanExit(false, true)
	sdl.WM_SetCaption("Compiling shaders...", "")
	glSetupShaderProg(&shaderTextureCreator)
	glSetupShaderProg(&shaderTextureDisplay)
	glFillBuffer(rttVerts, &rttVertBuf)
	glFillBuffer(dispVerts, &dispVertBuf)
	gl.GenTextures(1, &rttFrameTex)
	gl.BindTexture(gl.TEXTURE_2D, rttFrameTex)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glTexFilter)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, glTexClamp)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, glTexClamp)
	if doRtt && !noRtt {
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texWidth, texHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
		gl.GenFramebuffers(1, &rttFrameBuf)
		gl.BindFramebuffer(gl.FRAMEBUFFER, rttFrameBuf)
		gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttFrameTex, 0)
		gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
	} else {
		if noRtt {
			rttFrameBuf = 0
		} else {
			glFillTextureFromImageFile("texture.jpg")
		}
	}
	gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.ClearColor(0.3, 0.2, 0.1, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	var looping = true
	var fps = 0
	var durSec time.Duration
	startTime, lastLoopTime, lastSecond = now, now, now
	sdl.WM_SetCaption("Up & running!", "")
	for looping {
		now = time.Now()
		if durSec = now.Sub(lastSecond); durSec.Seconds() >= 1 {
			sdl.WM_SetCaption(fmt.Sprintf("%v×%v @ %vfps", sdlScreen.W, sdlScreen.H, fps), "")
			fps = 0
			lastSecond = now
		} else {
			fps++
		}
		if evt := sdl.PollEvent(); evt != nil {
			switch event := evt.(type) {
			case *sdl.ResizeEvent:
				reinitScreen(int(event.W), int(event.H))
			case *sdl.QuitEvent:
				looping = false
			}
		}
		if doRtt || noRtt {
			renderToTexture()
		}
		if !noRtt {
			renderToScreen()
		}
		sdl.GL_SwapBuffers()
		lastLoopTime = now
	}
	sdl.Quit()
}