Example #1
0
func TestZeroLengthEntryCanGoBetweenTwoEntries(t *testing.T) {
	b := blob.New()
	buffer := bytes.NewBuffer(nil)

	b.Append("1", []byte{1})
	b.Append("_", []byte{})
	b.Append("2", []byte{2})
	err := b.Write(buffer)

	if err != nil {
		t.Fatal(err)
	}
	checkBytes(t, buffer.Bytes(), []byte{
		33, 0, 0, 0,
		1, 0, // "1" is 1 byte long
		byte('1'),
		1, 0, 0, 0, 0, 0, 0, 0, // data length
		1, 0, // "_" is 1 byte long
		byte('_'),
		0, 0, 0, 0, 0, 0, 0, 0, // data length
		1, 0, // "2" is 1 byte long
		byte('2'),
		1, 0, 0, 0, 0, 0, 0, 0, // data length
		1, 2, // data
	})
}
Example #2
0
func TestAccessFunctions(t *testing.T) {
	b := blob.New()
	b.Append("one", []byte{1, 2, 3})
	b.Append("two", []byte{4, 5})

	if b.ItemCount() != 2 {
		t.Error("item count was", b.ItemCount())
	}

	one, found := b.GetByID("one")
	if !found {
		t.Error("one not found")
	}
	checkBytes(t, one, []byte{1, 2, 3})

	two, found := b.GetByIndex(1)
	if !found {
		t.Error("two not found by index")
	}
	checkBytes(t, two, []byte{4, 5})

	if id := b.GetIDAtIndex(0); id != "one" {
		t.Error("expected id is one but was", id)
	}
	if id := b.GetIDAtIndex(1); id != "two" {
		t.Error("expected id is two but was", id)
	}
}
Example #3
0
func TestEmptyBlobJustWritesZeroHeaderLength(t *testing.T) {
	b := blob.New()
	buffer := bytes.NewBuffer(nil)

	err := b.Write(buffer)

	if err != nil {
		t.Fatal(err)
	}
	checkBytes(t, buffer.Bytes(), []byte{0, 0, 0, 0})
}
Example #4
0
func TestZeroLengthEntryIsStillRepresentedInHeader(t *testing.T) {
	b := blob.New()
	buffer := bytes.NewBuffer(nil)

	b.Append("id", []byte{})
	err := b.Write(buffer)

	if err != nil {
		t.Fatal(err)
	}
	checkBytes(t, buffer.Bytes(), []byte{
		12, 0, 0, 0,
		2, 0, // "id" is 2 bytes long
		byte('i'), byte('d'),
		0, 0, 0, 0, 0, 0, 0, 0, // data length
		// there is no data, the slice is empty
	})
}
Example #5
0
func TestOneResourceMakesOneHeaderEntry(t *testing.T) {
	b := blob.New()
	buffer := bytes.NewBuffer(nil)

	b.Append("id", []byte{1, 2, 3})
	err := b.Write(buffer)

	if err != nil {
		t.Fatal(err)
	}
	checkBytes(t, buffer.Bytes(), []byte{
		12, 0, 0, 0,
		2, 0, // "id" is 2 bytes long
		byte('i'), byte('d'),
		3, 0, 0, 0, 0, 0, 0, 0, // data length
		1, 2, 3, // actual data
	})
}
Example #6
0
func TestTwoResourcesMakeTwoEntries(t *testing.T) {
	b := blob.New()
	buffer := bytes.NewBuffer(nil)

	b.Append("id", []byte{1, 2, 3})
	b.Append("2nd", []byte{4, 5})
	err := b.Write(buffer)

	if err != nil {
		t.Fatal(err)
	}
	checkBytes(t, buffer.Bytes(), []byte{
		25, 0, 0, 0,
		2, 0, // "id" is 2 bytes long
		byte('i'), byte('d'),
		3, 0, 0, 0, 0, 0, 0, 0, // data length
		3, 0, // "2nd" is 3 bytes long
		byte('2'), byte('n'), byte('d'),
		2, 0, 0, 0, 0, 0, 0, 0, // data length
		1, 2, 3, // data for "id"
		4, 5, // data for "2nd"
	})
}
Example #7
0
func main() {
	resources := blob.New()
	textureAtlas := atlas.New(2048)

	gophette, err := xcf.LoadFromFile("./gophette.xcf")
	check(err)
	barney, err := xcf.LoadFromFile("./barney.xcf")
	check(err)

	// create the collision information for Gophette and Barney
	addCollisionInfo := func(canvas xcf.Canvas, id string) {
		collision := canvas.GetLayerByName("collision")
		left, top := findTopLeftNonTransparentPixel(collision)
		right, bottom := findBottomRightNonTransparentPixel(collision)
		// scale the collision rect just like the images
		left = int(0.5 + scale*float64(left))
		top = int(0.5 + scale*float64(top))
		right = int(0.5 + scale*float64(right))
		bottom = int(0.5 + scale*float64(bottom))
		width, height := right-left+1, bottom-top+1
		r := rect{int32(left), int32(top), int32(width), int32(height)}
		buffer := bytes.NewBuffer(nil)
		check(binary.Write(buffer, byteOrder, &r))
		resources.Append(id, buffer.Bytes())
	}
	addCollisionInfo(gophette, "hero collision")
	addCollisionInfo(barney, "barney collision")

	addImage := func(img image.Image, id string) {
		_, err := textureAtlas.Add(id, img)
		check(err)
	}

	// create the image resources
	for _, layer := range []string{
		"jump",
		"run1",
		"run2",
		"run3",
	} {
		small := scaleImage(gophette.GetLayerByName(layer))
		addImage(small, "gophette_left_"+layer)
		addImage(imaging.FlipH(small), "gophette_right_"+layer)
	}

	for _, layer := range []string{
		"stand",
		"jump",
		"run1",
		"run2",
		"run3",
		"run4",
		"run5",
		"run6",
	} {
		smallLeft := scaleImage(barney.GetLayerByName("left_" + layer))
		smallRight := scaleImage(barney.GetLayerByName("right_" + layer))
		addImage(smallLeft, "barney_left_"+layer)
		addImage(smallRight, "barney_right_"+layer)
	}

	grass, err := xcf.LoadFromFile("./grass.xcf")
	check(err)
	for _, layer := range []string{
		"grass left",
		"grass right",
		"grass center 1",
		"grass center 2",
		"grass center 3",
	} {
		addImage(grass.GetLayerByName(layer), layer)
	}

	grassLong, err := xcf.LoadFromFile("./grass_long.xcf")
	check(err)
	for _, layer := range []string{
		"grass long 1",
		"grass long 2",
		"grass long 3",
	} {
		addImage(grassLong.GetLayerByName(layer), layer)
	}

	ground, err := xcf.LoadFromFile("./ground.xcf")
	check(err)
	for _, layer := range []string{
		"ground left",
		"ground right",
		"ground center 1",
		"ground center 2",
		"ground center 3",
	} {
		addImage(ground.GetLayerByName(layer), layer)
	}

	groundLong, err := xcf.LoadFromFile("./ground_long.xcf")
	check(err)
	for _, layer := range []string{
		"ground long 1",
		"ground long 2",
	} {
		addImage(groundLong.GetLayerByName(layer), layer)
	}

	rock, err := xcf.LoadFromFile("./rock.xcf")
	check(err)
	addImage(scaleImage(rock.GetLayerByName("rock")), "square rock")

	tree, err := xcf.LoadFromFile("./tree.xcf")
	check(err)
	smallTree := scaleImage(tree.GetLayerByName("small"))
	addImage(smallTree, "small tree")

	tree, err = xcf.LoadFromFile("./tree_big.xcf")
	check(err)
	bigTree := scaleImage(tree.GetLayerByName("big"))
	addImage(bigTree, "big tree")

	tree, err = xcf.LoadFromFile("./tree_huge.xcf")
	check(err)
	hugeTree := scaleImage(tree.GetLayerByName("huge"))
	addImage(hugeTree, "huge tree")

	cave, err := xcf.LoadFromFile("./cave.xcf")
	check(err)
	addImage(scaleImage(cave.GetLayerByName("cave back")), "cave back")
	addImage(scaleImage(cave.GetLayerByName("cave front")), "cave front")

	intro, err := xcf.LoadFromFile("./intro.xcf")
	check(err)
	addImage(scaleImageToFactor(intro.GetLayerByName("pc 1"), 0.67), "intro pc 1")
	addImage(scaleImageToFactor(intro.GetLayerByName("pc 2"), 0.67), "intro pc 2")
	addImage(scaleImageToFactor(intro.GetLayerByName("gophette"), 0.67), "intro gophette")

	{
		music, err := ioutil.ReadFile("./background_music.ogg")
		check(err)
		resources.Append("music", music)
	}

	{
		music, err := ioutil.ReadFile("./background_music.wav")
		check(err)
		resources.Append("music_wav", music)
	}

	for _, sound := range []string{
		"win",
		"lose",
		"fall",
		"barney wins",
		"barney intro text",
		"whistle",
		"instructions",
	} {
		data, err := ioutil.ReadFile(sound + ".wav")
		check(err)
		resources.Append(sound, data)
	}

	resources.Append("atlas", imageToBytes(textureAtlas))
	for _, sub := range textureAtlas.SubImages {
		resources.Append(
			sub.ID,
			toRectData(sub.Bounds().Sub(textureAtlas.Bounds().Min)),
		)
	}

	resourceFile, err := os.Create("../resource/resources.blob")
	check(err)
	defer resourceFile.Close()
	resources.Write(resourceFile)
}