Esempio n. 1
0
func main() {
	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)
	defer cleanExit(false, true)
	glSetupShaderProg(&shaderTextureCreator, false)
	glSetupShaderProg(&shaderTextureDisplay, true)
	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, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
	if doRtt {
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, texSize, texSize, 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 {
		glFillTextureFromImageFile("texture.jpg")
	}
	gl.BindTexture(gl.TEXTURE_2D, 0)
	gl.ClearColor(0.3, 0.6, 0.9, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.ActiveTexture(gl.TEXTURE0)
	for {
		if evt := sdl.PollEvent(); evt != nil {
			switch event := evt.(type) {
			case *sdl.ResizeEvent:
				reinitScreen(int(event.W), int(event.H))
			case *sdl.QuitEvent:
				return
			}
		} else {
			if doRtt {
				renderToTexture()
			}
			renderToScreen()
			sdl.GL_SwapBuffers()
		}
	}

	sdl.Quit()
}
Esempio n. 2
0
File: cube.go Progetto: uriel/gl
func main() {
	sdl.Init(sdl.INIT_VIDEO)
	sdl.SetVideoMode(800, 600, 32, sdl.OPENGL)
	gl.Init()
	gl.Enable(gl.DEPTH_TEST)
	gl.Viewport(0, 0, 800, 600)
	tick := time.Tick(time.Second / 50)
	timer := 0.0
	posbuf := gl.NewBuffer(gl.ARRAY_BUFFER, Vertices, gl.STATIC_DRAW)
	prog, err := gl.MakeProgram([]string{vertexShader}, []string{fragmentShader})
	if err != nil {
		fmt.Println(err)
		return
	}
	f, err := os.Open("glenda.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	img, _, err := image.Decode(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	tex := gl.NewTexture2D(img, 0)
	for {
		select {
		case ev := <-sdl.Events:
			if _, ok := ev.(sdl.QuitEvent); ok {
				return
			}
		case <-tick:
			gl.ClearColor(0, 0, 0, 1)
			gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

			prog.Use()
			mat := gl.Mul4(gl.Frustum(45, 800./600, 0.01, 100), gl.Translate(0, 0, -8), gl.RotX(timer), gl.RotY(2*timer), gl.RotZ(3*timer))
			prog.EnableAttrib("position", posbuf, 0, 3, 5, false)
			prog.EnableAttrib("texcoord", posbuf, 3, 2, 5, false)
			prog.SetUniform("tex", 0)
			prog.SetUniform("matrix", mat)
			tex.Enable(0, gl.TEXTURE_2D)
			gl.DrawArrays(gl.TRIANGLE_STRIP, 0, len(Vertices)/5)
			prog.DisableAttrib("position")
			prog.Unuse()

			sdl.GL_SwapBuffers()
			timer += 1
		}
	}
}
Esempio n. 3
0
func StartGraphics(id uint32, update chan *State, move chan MoveMsg, quit chan bool) error {
	var err error

	rc := sdl.Init(sdl.INIT_VIDEO)
	if rc == -1 {
		return errors.New(sdl.GetError())
	}
	disp := sdl.SetVideoMode(800, 600, 32, sdl.OPENGL)
	if disp == nil {
		return errors.New(sdl.GetError())
	}
	winscale = gl.Mul4(gl.Translate(-1, 1, 0), gl.Scale(1./400, -1./300, 1))
	gl.Init()
	gl.ClearColor(1, 1, 1, 1)
	s := new(State)
	ls := time.Now()
	quadBuf = gl.NewBuffer(gl.ARRAY_BUFFER, quadData, gl.STATIC_DRAW)
	prog, err = gl.MakeProgram([]string{vertexShader}, []string{fragmentShader})
	if err != nil {
		log.Fatal(err)
	}
	AddTex("glenda", "glenda.png")
	AddTex("tux", "tux.png")
	i := image.NewRGBA(image.Rect(0, 0, 800, 600))
	texs["black"] = gl.NewTexture2D(i, 0)

	tick := time.Tick(time.Second / 50)
	squit := make(chan bool)
	go ProcessInput(move, quit, squit)
	for {
		select {
		case s = <-update:
			ls = time.Now()
		case <-tick:
			s.Advance(time.Now().Sub(ls))
			ls = time.Now()
			gl.Clear(gl.COLOR_BUFFER_BIT)
			RenderScene(s)
			sdl.GL_SwapBuffers()
		case <-squit:
			return nil
		}
	}
	return nil
}
Esempio n. 4
0
func draw() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

	glPushMatrix()
	glRotatef(view_rotx, 1.0, 0.0, 0.0)
	glRotatef(view_roty, 0.0, 1.0, 0.0)
	glRotatef(view_rotz, 0.0, 0.0, 1.0)

	glPushMatrix()
	glTranslatef(-3.0, -2.0, 0.0)
	glRotatef(angle, 0.0, 0.0, 1.0)
	glCallList(gear1)
	glPopMatrix()

	glPushMatrix()
	glTranslatef(3.1, -2.0, 0.0)
	glRotatef(-2.0*angle-9.0, 0.0, 0.0, 1.0)
	glCallList(gear2)
	glPopMatrix()

	glPushMatrix()
	glTranslatef(-3.1, 4.2, 0.0)
	glRotatef(-2.0*angle-25.0, 0.0, 0.0, 1.0)
	glCallList(gear3)
	glPopMatrix()

	glPopMatrix()

	sdl.GL_SwapBuffers()

	frames++
	{
		t := sdl.GetTicks()
		if t-t0 >= 5000 {
			seconds := (t - t0) / 1000.0
			fps := frames / seconds
			printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps)
			t0 = t
			frames = 0
		}
	}
}
Esempio n. 5
0
File: gotris.go Progetto: nsf/gotris
func main() {
	runtime.LockOSThread()
	flag.Parse()
	sdl.Init(sdl.INIT_VIDEO)
	defer sdl.Quit()

	sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)

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

	sdl.WM_SetCaption("Gotris", "Gotris")
	sdl.EnableKeyRepeat(250, 45)

	gl.Enable(gl.TEXTURE_2D)
	gl.Enable(gl.BLEND)
	gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
	gl.Viewport(0, 0, 640, 480)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, 640, 480, 0, -1, 1)

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

	//-----------------------------------------------------------------------------

	font, err := LoadFontFromFile("dejavu.font")
	if err != nil {
		panic(err)
	}

	rand.Seed(int64(sdl.GetTicks()))

	gs := NewGameSession(*initLevel, font)
	lastTime := sdl.GetTicks()
	ticker := time.NewTicker(10 * time.Millisecond)

	stop := make(chan byte, 1)
	go func() {
		for {
			switch e := (<-sdl.Events).(type) {
			case sdl.QuitEvent:
				stop <- 0
			case sdl.KeyboardEvent:
				if e.Type == sdl.KEYDOWN {
					running := gs.HandleKey(e.Keysym.Sym)
					if !running {
						stop <- 0
					}
					gs.update <- 0
				}
			}
		}
	}()
loop:
	for {
		select {
		case <-ticker.C:
			gs.update <- 0

		case <-gs.update:
			now := sdl.GetTicks()
			delta := now - lastTime
			lastTime = now

			gs.Update(delta)

			gl.Clear(gl.COLOR_BUFFER_BIT)
			font.Draw(5, 5, fmt.Sprintf("Level: %d | Score: %d", gs.Level, gs.Score))
			gs.Draw()
			gl.Color3ub(255, 255, 255)
			sdl.GL_SwapBuffers()

		case <-stop:
			break loop
		}
	}
}
Esempio n. 6
0
func draw_screen() {
	angle := float32(0.0)
	v := [][]float32{
		{-1.0, -1.0, 1.0},
		{1.0, -1.0, 1.0},
		{1.0, 1.0, 1.0},
		{-1.0, 1.0, 1.0},
		{-1.0, -1.0, -1.0},
		{1.0, -1.0, -1.0},
		{1.0, 1.0, -1.0},
		{-1.0, 1.0, -1.0}}
	red := []byte{255, 0, 0, 255}
	green := []byte{0, 255, 0, 255}
	blue := []byte{0, 0, 255, 255}
	white := []byte{255, 255, 255, 255}
	yellow := []byte{0, 255, 255, 255}
	black := []byte{0, 0, 0, 255}
	orange := []byte{255, 255, 0, 255}
	purple := []byte{255, 0, 255, 0}

	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.MatrixMode(gl.MODELVIEW)
	gl.LoadIdentity()
	gl.Translatef(0.0, 0.0, -5.0)
	gl.Rotatef(angle, 0.0, 1.0, 0.0)
	if should_rotate {
		angle++
		if angle > 360.0 {
			angle = 0.0
		}
	}
	gl.Begin(gl.TRIANGLES)
	gl.Color4ubv(red)
	gl.Vertex3fv(v[0])
	gl.Color4ubv(green)
	gl.Vertex3fv(v[1])
	gl.Color4ubv(blue)
	gl.Vertex3fv(v[2])
	gl.Color4ubv(red)
	gl.Vertex3fv(v[0])
	gl.Color4ubv(blue)
	gl.Vertex3fv(v[2])
	gl.Color4ubv(white)
	gl.Vertex3fv(v[3])
	gl.Color4ubv(green)
	gl.Vertex3fv(v[1])
	gl.Color4ubv(black)
	gl.Vertex3fv(v[5])
	gl.Color4ubv(orange)
	gl.Vertex3fv(v[6])
	gl.Color4ubv(green)
	gl.Vertex3fv(v[1])
	gl.Color4ubv(orange)
	gl.Vertex3fv(v[6])
	gl.Color4ubv(blue)
	gl.Vertex3fv(v[2])
	gl.Color4ubv(black)
	gl.Vertex3fv(v[5])
	gl.Color4ubv(yellow)
	gl.Vertex3fv(v[4])
	gl.Color4ubv(purple)
	gl.Vertex3fv(v[7])
	gl.Color4ubv(black)
	gl.Vertex3fv(v[5])
	gl.Color4ubv(purple)
	gl.Vertex3fv(v[7])
	gl.Color4ubv(orange)
	gl.Vertex3fv(v[6])
	gl.Color4ubv(yellow)
	gl.Vertex3fv(v[4])
	gl.Color4ubv(red)
	gl.Vertex3fv(v[0])
	gl.Color4ubv(white)
	gl.Vertex3fv(v[3])
	gl.Color4ubv(yellow)
	gl.Vertex3fv(v[4])
	gl.Color4ubv(white)
	gl.Vertex3fv(v[3])
	gl.Color4ubv(purple)
	gl.Vertex3fv(v[7])
	gl.Color4ubv(white)
	gl.Vertex3fv(v[3])
	gl.Color4ubv(blue)
	gl.Vertex3fv(v[2])
	gl.Color4ubv(orange)
	gl.Vertex3fv(v[6])
	gl.Color4ubv(white)
	gl.Vertex3fv(v[3])
	gl.Color4ubv(orange)
	gl.Vertex3fv(v[6])
	gl.Color4ubv(purple)
	gl.Vertex3fv(v[7])
	gl.Color4ubv(green)
	gl.Vertex3fv(v[1])
	gl.Color4ubv(red)
	gl.Vertex3fv(v[0])
	gl.Color4ubv(yellow)
	gl.Vertex3fv(v[4])
	gl.Color4ubv(green)
	gl.Vertex3fv(v[1])
	gl.Color4ubv(yellow)
	gl.Vertex3fv(v[4])
	gl.Color4ubv(black)
	gl.Vertex3fv(v[5])
	gl.End()
	sdl.GL_SwapBuffers()
}
Esempio n. 7
0
func main() {
	runtime.LockOSThread()

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}
	var screen = sdl.SetVideoMode(screenWidth, screenHeight, 32, sdl.OPENGL|sdl.HWSURFACE|sdl.GL_DOUBLEBUFFER|sdl.FULLSCREEN)
	if screen == nil {
		log.Fatal(sdl.GetError())
	}
	sdl.WM_SetCaption("Lecture Hall Games", "")
	sdl.EnableUNICODE(1)
	if gl.Init() != 0 {
		log.Fatal("could not initialize OpenGL")
	}
	gl.Viewport(0, 0, int(screen.W), int(screen.H))
	gl.ClearColor(1, 1, 1, 0)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0)
	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.DEPTH_TEST)
	gl.TexEnvi(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)

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

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

	if p, err := build.Default.Import(basePkg, "", build.FindOnly); err == nil {
		os.Chdir(p.Dir)
	}

	var err error

	rand.Seed(time.Now().UnixNano())
	levelDir := fmt.Sprintf("data/levels/demolevel%d", 3+rand.Intn(numberLevels))
	//carsDir := fmt.Sprintf(" data/cars/car%d/", 1+rand.Intn(numberCars))
	if game, err = NewRacer(levelDir); err != nil {
		log.Fatal(err)
	}

	go func() {
		listen, err := net.Listen("tcp", ":8001")
		if err != nil {
			log.Fatal(err)
		}
		for {
			conn, err := listen.Accept()
			if err != nil {
				log.Println(err)
				continue
			}
			go handleConnection(conn)
		}
	}()

	running := true
	last := time.Now()
	for running {
		select {
		case event := <-sdl.Events:
			switch e := event.(type) {
			case sdl.QuitEvent:
				running = false
			case sdl.ResizeEvent:
				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)
			case sdl.KeyboardEvent:
				if e.Type == sdl.KEYDOWN {
					if e.Keysym.Sym == sdl.K_ESCAPE {
						running = false
					} else {
						game.KeyPressed(e.Keysym)
					}
				}
			}
		default:
		}

		current := time.Now()
		t := current.Sub(last)
		last = current

		mu.Lock()
		game.Update(t)
		game.Render(screen)
		mu.Unlock()

		sdl.GL_SwapBuffers()
	}

	sdl.Quit()
}