Example #1
0
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
}
Example #2
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
}