Example #1
0
func (l *sdlAssetLoader) loadResources() error {
	resourceData, err := payload.Read()
	if err != nil {
		return err
	}
	l.resources, err = blob.Read(bytes.NewBuffer(resourceData))
	if err != nil {
		return err
	}

	// load the texture atlas
	atlas, found := l.resources.GetByID("atlas")
	if !found {
		panic("texture atlas not found in resources")
	}
	rwOps := sdl.RWFromMem(unsafe.Pointer(&atlas[0]), len(atlas))
	surface, err := img.Load_RW(rwOps, false)
	check(err)
	defer surface.Free()
	texture, err := l.renderer.CreateTextureFromSurface(surface)
	check(err)
	l.textureAtlas = texture

	return err
}
Example #2
0
func TestReadingTwoEntryBlob(t *testing.T) {
	buffer := bytes.NewBuffer([]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"
	})

	b, err := blob.Read(buffer)

	if err != nil {
		t.Fatal(err)
	}
	if b.ItemCount() != 2 {
		t.Fatal("item count was", b.ItemCount())
	}
	// item 1
	data, found := b.GetByID("id")
	if !found {
		t.Fatal("id not found")
	}
	checkBytes(t, data, []byte{1, 2, 3})
	// item 2
	data, found = b.GetByID("2nd")
	if !found {
		t.Fatal("2nd not found")
	}
	checkBytes(t, data, []byte{4, 5})
}
Example #3
0
func TestReadingEmptyBlobReturnsZeroItems(t *testing.T) {
	buffer := bytes.NewBuffer([]byte{
		0, 0, 0, 0, // empty header, 0 length
	})

	b, err := blob.Read(buffer)

	if err != nil {
		t.Fatal(err)
	}
	if b.ItemCount() != 0 {
		t.Fatal("item count was", b.ItemCount())
	}
}
Example #4
0
func (l *windowsAssetloader) loadResources() {
	resourceData, err := payload.Read()
	check(err)
	l.resources, err = blob.Read(bytes.NewBuffer(resourceData))

	// load the texture atlas
	atlas, found := l.resources.GetByID("atlas")
	if !found {
		panic("texture atlas not found in resources")
	}

	ping, err := png.Decode(bytes.NewReader(atlas))
	check(err)

	var nrgba *image.NRGBA
	if asNRGBA, ok := ping.(*image.NRGBA); ok {
		nrgba = asNRGBA
	} else {
		nrgba = image.NewNRGBA(ping.Bounds())
		draw.Draw(nrgba, nrgba.Bounds(), ping, image.ZP, draw.Src)
	}

	texture, err := l.device.CreateTexture(
		uint(nrgba.Bounds().Dx()),
		uint(nrgba.Bounds().Dy()),
		1,
		d3d9.USAGE_SOFTWAREPROCESSING,
		d3d9.FMT_A8R8G8B8,
		d3d9.POOL_MANAGED,
		nil,
	)
	check(err)
	lockedRect, err := texture.LockRect(0, nil, d3d9.LOCK_DISCARD)
	check(err)
	lockedRect.SetAllBytes(nrgba.Pix, nrgba.Stride)
	check(texture.UnlockRect(0))

	l.textureAtlas = texture
	l.textureAtlasBounds = nrgba.Bounds()
}
Example #5
0
func TestReadingZeroLengthDataEntry(t *testing.T) {
	buffer := bytes.NewBuffer([]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
		// no data, length is 0
	})

	b, err := blob.Read(buffer)

	if err != nil {
		t.Fatal(err)
	}
	if b.ItemCount() != 1 {
		t.Fatal("item count was", b.ItemCount())
	}
	// item 1
	data, found := b.GetByID("id")
	if !found {
		t.Fatal("id not found")
	}
	checkBytes(t, data, []byte{})
}
Example #6
0
func TestReadingOneEntryBlob(t *testing.T) {
	buffer := bytes.NewBuffer([]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
	})

	b, err := blob.Read(buffer)

	if err != nil {
		t.Fatal(err)
	}
	if b.ItemCount() != 1 {
		t.Fatal("item count was", b.ItemCount())
	}
	// item 1
	data, found := b.GetByID("id")
	if !found {
		t.Fatal("id not found")
	}
	checkBytes(t, data, []byte{1, 2, 3})
}
Example #7
0
func main() {
	func() {
		file, err := os.Open("../resource/resources.blob")
		check(err)
		defer file.Close()
		resources, err = blob.Read(file)
		check(err)
	}()

	sdl.SetHint(sdl.HINT_RENDER_VSYNC, "1")

	check(sdl.Init(sdl.INIT_EVERYTHING))
	defer sdl.Quit()

	window, r, err := sdl.CreateWindowAndRenderer(
		640, 480,
		sdl.WINDOW_RESIZABLE,
	)
	check(err)
	renderer = r
	defer renderer.Destroy()
	defer window.Destroy()
	window.SetTitle("Gophette's Adventures - Level Editor")
	window.SetPosition(50, 50)
	window.SetSize(1800, 900)
	window.SetFullscreen(sdl.WINDOW_FULLSCREEN_DESKTOP)
	renderer.SetDrawBlendMode(sdl.BLENDMODE_BLEND)

	if len(LevelImages) == 0 {
		for i, id := range []string{
			"grass left",
			"grass right",
			"grass center 1",
			"grass center 2",
			"grass center 3",
			"small tree",
		} {
			images = append(images, image{id, loadImage(id), 0, i * 50})
		}
	} else {
		for i := range LevelImages {
			id := LevelImages[i].ID
			x, y := LevelImages[i].X, LevelImages[i].Y
			img := loadImage(id)
			images = append(images, image{id, img, x, y})
		}
	}

	leftDown := false
	middleDown := false
	rightDown := false
	selectedImage := -1
	selectedObject := -1
	var lastX, lastY int

	moveImage := func(dx, dy int) {
		if selectedImage != -1 {
			images[selectedImage].x += dx
			images[selectedImage].y += dy
		}
	}

	stretchObject := func(dx, dy int) {
		if selectedObject != -1 {
			if sdl.GetKeyboardState()[sdl.SCANCODE_LCTRL] != 0 {
				dx *= 20
				dy *= 20
			}
			obj := &LevelObjects[selectedObject]
			obj.W += dx
			obj.H += dy
		}
	}

	running := true
	for running {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch event := e.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseButtonEvent:
				if event.Button == sdl.BUTTON_LEFT {
					leftDown = event.State == sdl.PRESSED
					if !leftDown {
						draggingImage = false
						draggingObject = false
					} else {
						selectedObject = -1
						selectedImage = -1
						for i := range images {
							if images[i].contains(
								int(event.X)-cameraX,
								int(event.Y)-cameraY,
							) {
								draggingImage = true
								selectedImage = i
							}
						}

						if selectedImage == -1 {
							for i := range LevelObjects {
								if contains(LevelObjects[i],
									int(event.X)-cameraX,
									int(event.Y)-cameraY,
								) {
									draggingObject = true
									selectedObject = i
								}
							}
						}
					}
				}
				if event.Button == sdl.BUTTON_MIDDLE {
					middleDown = event.State == sdl.PRESSED
				}
				if event.Button == sdl.BUTTON_RIGHT {
					rightDown = event.State == sdl.PRESSED
					LevelObjects = append(LevelObjects, LevelObject{
						int(event.X) - cameraX,
						int(event.Y) - cameraY,
						0,
						0,
						true,
					})
					selectedObject = -1
				}
			case *sdl.MouseMotionEvent:
				dx, dy := int(event.X)-lastX, int(event.Y)-lastY
				if selectedImage != -1 && draggingImage {
					img := &images[selectedImage]
					img.x += dx
					img.y += dy
				}
				if selectedObject != -1 && draggingObject {
					obj := &LevelObjects[selectedObject]
					obj.X += dx
					obj.Y += dy
				}
				lastX, lastY = int(event.X), int(event.Y)

				if middleDown {
					cameraX += dx
					cameraY += dy
				}

				if rightDown {
					last := &LevelObjects[len(LevelObjects)-1]
					last.W += dx
					last.H += dy
				}
			case *sdl.KeyDownEvent:
				switch event.Keysym.Sym {
				case sdl.K_ESCAPE:
					running = false
				case sdl.K_LEFT:
					cameraX += 100
				case sdl.K_RIGHT:
					cameraX -= 100
				case sdl.K_UP:
					cameraY += 100
				case sdl.K_DOWN:
					cameraY -= 100
				case sdl.K_a:
					moveImage(-1, 0)
				case sdl.K_d:
					moveImage(1, 0)
				case sdl.K_w:
					moveImage(0, -1)
				case sdl.K_s:
					moveImage(0, 1)
				case sdl.K_j:
					stretchObject(-1, 0)
				case sdl.K_l:
					stretchObject(1, 0)
				case sdl.K_i:
					stretchObject(0, -1)
				case sdl.K_k:
					stretchObject(0, 1)
				case sdl.K_MINUS:
					if selectedImage != -1 && selectedImage != 0 {
						images = append(append(
							[]image{images[selectedImage]},
							images[0:selectedImage]...),
							images[selectedImage+1:]...,
						)
						selectedImage = 0
					}
				case sdl.K_PLUS:
					last := len(images) - 1
					fmt.Println("last", last, len(images))
					fmt.Println("selectedImage", selectedImage)
					if selectedImage != -1 && selectedImage != last {
						images = append(append(
							images[0:selectedImage],
							images[selectedImage+1:]...),
							images[selectedImage],
						)
						selectedImage = last
						fmt.Println("selectedImage after", selectedImage)
					}
				case sdl.K_SPACE:
					if selectedObject != -1 {
						obj := &LevelObjects[selectedObject]
						obj.Solid = !obj.Solid
					}
				case sdl.K_c:
					if selectedImage != -1 {
						copy := images[selectedImage]
						copy.x += 10
						copy.y += 10
						images = append(images, copy)
					}
				case sdl.K_DELETE:
					if selectedImage != -1 {
						images = append(images[:selectedImage], images[selectedImage+1:]...)
						selectedImage = -1
					} else if selectedObject != -1 {
						LevelObjects = append(
							LevelObjects[:selectedObject],
							LevelObjects[selectedObject+1:]...,
						)
						selectedObject = -1
					}
				case sdl.K_F3:
					saveLevel()
				}
			}
		}

		renderer.SetDrawColor(backColor[0], backColor[1], backColor[2], 255)
		renderer.Clear()

		for i, img := range images {
			img.render(i == selectedImage)
		}

		for i, obj := range LevelObjects {
			var g uint8 = 0
			var a uint8 = 100
			if i == selectedObject {
				g = 255
			}
			renderer.SetDrawColor(0, g, 0, a)
			if obj.Solid {
				renderer.SetDrawColor(0, g, 255, a)
			}
			obj.X += cameraX
			obj.Y += cameraY
			r := sdl.Rect{int32(obj.X), int32(obj.Y), int32(obj.W), int32(obj.H)}
			renderer.FillRect(&r)
		}

		renderer.Present()
	}
}