Exemple #1
0
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),
	))
}
Exemple #2
0
func newBox(width, height float32) *box {
	var err error

	box := new(box)

	// Sound player

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

	// Chipmunk body

	box.physicsShape = chipmunk.NewBox(
		vect.Vect{0, 0},
		vect.Float(width),
		vect.Float(height),
	)

	box.physicsShape.SetElasticity(BoxElasticity)
	box.physicsBody = chipmunk.NewBody(vect.Float(BoxMass), box.physicsShape.Moment(float32(BoxMass)))
	box.physicsBody.AddShape(box.physicsShape)
	box.physicsBody.CallbackHandler = callbacks{}

	// OpenGL shape

	box.openglShape = shapes.NewBox(width, height)

	return box
}
Exemple #3
0
func (t *TestSuite) TestDraw() {
	filename := GOPHER_PNG
	distance, _, _, err := TestImage(filename, <-t.testDraw, imagetest.Center)
	if err != nil {
		mandala.Fatalf(err.Error())
	}
	t.True(distance < distanceThreshold, fmt.Sprintf("Image differs by distance %f", distance))
}
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
}
Exemple #5
0
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
}