コード例 #1
0
ファイル: testlib.go プロジェクト: remogatto/gltext
func newWorld(width, height int) *world {
	// Load the font
	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/freesans.ttf", responseCh)
	response := <-responseCh
	fontBuffer := response.Buffer
	err := response.Error
	if err != nil {
		panic(err)
	}

	w := &world{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), -float32(height/2), float32(height/2)),
		viewMatrix: mathgl.Ident4f(),
	}

	sans, err := gltext.LoadTruetype(bytes.NewBuffer(fontBuffer), w, 40, 32, 127, gltext.LeftToRight)
	if err != nil {
		panic(err)
	}

	w.font = sans

	return w
}
コード例 #2
0
ファイル: svg.go プロジェクト: remogatto/mandala-examples
func (w *World) CreateFromSvg(filename string) {
	var svg svgFile

	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource(filename, responseCh)
	response := <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	buf := response.Buffer

	err := xml.Unmarshal(buf, &svg)
	if err != nil {
		mandala.Fatalf(err.Error())
	}

	scaleX := float32(w.width) / svg.Width
	scaleY := float32(w.height) / svg.Height

	for _, group := range svg.Groups {
		for _, rect := range group.Rects {
			rX := (rect.X + rect.Width/2) * scaleX
			rY := float32(w.height) - (rect.Y+rect.Height/2)*scaleY
			rW := rect.Width * scaleX
			rH := rect.Height * scaleY
			box := newBox(w, rW, rH)
			pos := vect.Vect{
				vect.Float(rX),
				vect.Float(rY),
			}

			box.physicsBody.SetPosition(pos)

			if rect.Transform != "" {
				var a, b, c float32
				_, err = fmt.Sscanf(rect.Transform, "rotate(%f %f,%f)", &a, &b, &c)
				if err != nil {
					mandala.Fatalf(err.Error())
				}
				box.physicsBody.SetAngle(90 / chipmunk.DegreeConst)
			}

			box.openglShape.SetColor(colorful.HappyColor())
			w.addBox(box)
		}
	}
	line := svg.Groups[0].Line
	w.setGround(newGround(
		w,
		0,
		float32(w.height)-float32(line.Y1*scaleY),
		float32(w.width),
		float32(w.height)-float32(line.Y2*scaleY),
	))
}
コード例 #3
0
ファイル: world.go プロジェクト: TimothyKlim/mandala-examples
func newWorld(width, height int) *world {
	world := &world{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), 0, float32(height)),
		viewMatrix: mathgl.Ident4f(),
		space:      chipmunk.NewSpace(),
	}

	world.space.Gravity = vect.Vect{0, Gravity}

	// Initialize the audio player
	var err error
	world.explosionPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	// Read the PCM audio samples

	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/explosion.pcm", responseCh)
	response := <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.explosionBuffer = response.Buffer

	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/impact.pcm", responseCh)
	response = <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.impactBuffer = response.Buffer

	return world
}
コード例 #4
0
ファイル: tests.go プロジェクト: leonardyp/mandala
func (t *TestSuite) TestResourceManager() {

	request := mandala.LoadResourceRequest{
		Filename: "drawable/gopher.png",
		Response: make(chan mandala.LoadResourceResponse),
	}

	mandala.ResourceManager() <- request

	response := <-request.Response
	buffer := response.Buffer

	t.True(response.Error == nil, "An error occured during resource opening")

	_, err := png.Decode(bytes.NewBuffer(buffer))
	t.True(err == nil, "An error occured during png decoding")

	// Load a non existent resource
	request = mandala.LoadResourceRequest{
		Filename: "res/doesntexist",
		Response: make(chan mandala.LoadResourceResponse),
	}

	mandala.ResourceManager() <- request

	response = <-request.Response
	buffer = response.Buffer

	t.True(buffer == nil)
	t.True(response.Error != nil)

	// Use the helper API for loading resources
	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("drawable/gopher.png", responseCh)
	response = <-responseCh

	buffer = response.Buffer
	t.True(buffer != nil)
	t.True(response.Error == nil, "An error occured during resource opening")

	_, err = png.Decode(bytes.NewBuffer(buffer))
	t.True(err == nil, "An error occured during png decoding")
}
コード例 #5
0
ファイル: tests.go プロジェクト: leonardyp/mandala
func (t *TestSuite) TestAudio() {
	// Open an audio file from resources
	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/android.raw", responseCh)
	response := <-responseCh
	buffer := response.Buffer
	t.True(response.Error == nil, "An error occured during resource opening")

	// Create the audio player
	player, err := mandala.NewAudioPlayer()
	t.True(err == nil)

	// Hum...this seems to fail for now
	max, err := player.GetMaxVolumeLevel()
	t.True(err == nil, "Error in getting the max volume level")
	t.True(max > 0, fmt.Sprintf("Max volume level is %d", max))

	if player != nil {
		player.Play(buffer, nil)
	}
}
コード例 #6
0
ファイル: world.go プロジェクト: remogatto/mandala-examples
func NewWorld(width, height int) *World {
	world := &World{
		width:      width,
		height:     height,
		projMatrix: mathgl.Ortho2D(0, float32(width), 0, float32(height)),
		viewMatrix: mathgl.Ident4f(),
		space:      chipmunk.NewSpace(),
	}

	world.space.Gravity = vect.Vect{0, Gravity}

	// Initialize the audio players
	var err error
	world.explosionPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	world.impactPlayer, err = mandala.NewAudioPlayer()
	if err != nil {
		mandala.Fatalf("%s\n", err.Error())
	}

	// Read the PCM audio samples

	responseCh := make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/explosion.pcm", responseCh)
	response := <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.explosionBuffer = response.Buffer

	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/impact.pcm", responseCh)
	response = <-responseCh

	if response.Error != nil {
		mandala.Fatalf(response.Error.Error())
	}
	world.impactBuffer = response.Buffer

	// Compile the shaders

	world.boxProgramShader = shaders.NewProgram(shapes.DefaultBoxFS, shapes.DefaultBoxVS)
	world.segmentProgramShader = shaders.NewProgram(shapes.DefaultSegmentFS, shapes.DefaultSegmentVS)

	// Load the font
	responseCh = make(chan mandala.LoadResourceResponse)
	mandala.ReadResource("raw/freesans.ttf", responseCh)
	response = <-responseCh
	fontBuffer := response.Buffer
	err = response.Error
	if err != nil {
		panic(err)
	}

	world.font, err = gltext.LoadTruetype(bytes.NewBuffer(fontBuffer), world, 12, 32, 127, gltext.LeftToRight)
	if err != nil {
		panic(err)
	}

	return world
}