示例#1
0
// DestroyGraphic destroys a Graphic that has been previously loaded by
// LoadGraphic. It must be called once for each graphic that has been loaded
// before the program exists.
//
// DestroyGraphic will panic if:
//
// 1. The Graphic, g, does not contain a Graphic type.
//
// 2. The toolbox has not been initialised.
func DestroyGraphic(g Graphic) {
	if !initialised {
		// this stops execution here, so ne need for an else after the if
		panic(notInitialisedMessage)
	}
	if g == nil {
		panic(nilGraphicMessage)
	}
	var t *sdl.Texture
	t = g
	t.Destroy()
}
示例#2
0
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var texture *sdl.Texture
	var src, dst sdl.Rect
	var err error

	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 1
	}
	defer window.Destroy()

	renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
		return 2
	}
	defer renderer.Destroy()

	image, err := img.Load(imageName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err)
		return 3
	}
	defer image.Free()

	texture, err = renderer.CreateTextureFromSurface(image)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
		return 4
	}
	defer texture.Destroy()

	src = sdl.Rect{0, 0, 512, 512}
	dst = sdl.Rect{100, 50, 512, 512}

	renderer.Clear()
	renderer.SetDrawColor(255, 0, 0, 255)
	renderer.FillRect(&sdl.Rect{0, 0, int32(winWidth), int32(winHeight)})
	renderer.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)

	return 0
}
示例#3
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var texture *sdl.Texture
	var src, dst sdl.Rect

	window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_SHOWN)
	if window == nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError())
		os.Exit(1)
	}

	renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if renderer == nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError())
		os.Exit(2)
	}

	image := img.Load(imageName)
	if image == nil {
		fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", sdl.GetError())
		os.Exit(3)
	}

	texture = renderer.CreateTextureFromSurface(image)
	if texture == nil {
		fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", sdl.GetError())
		os.Exit(4)
	}

	src = sdl.Rect{0, 0, 512, 512}
	dst = sdl.Rect{100, 50, 512, 512}

	renderer.Clear()
	renderer.SetDrawColor(255, 0, 0, 255)
	renderer.FillRect(&sdl.Rect{0, 0, int32(winWidth), int32(winHeight)})
	renderer.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)

	image.Free()
	texture.Destroy()
	renderer.Destroy()
	window.Destroy()
}
示例#4
0
文件: texture.go 项目: flazz/go-sdl2
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var image *sdl.Surface
	var texture *sdl.Texture
	var src, dst sdl.Rect
	var err error

	window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		os.Exit(1)
	}
	defer window.Destroy()

	renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
		os.Exit(2)
	}
	defer renderer.Destroy()

	image, err = sdl.LoadBMP(imageName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load BMP: %s\n", err)
		os.Exit(3)
	}
	defer image.Free()

	texture, err = renderer.CreateTextureFromSurface(image)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
		os.Exit(4)
	}
	defer texture.Destroy()

	src = sdl.Rect{0, 0, 512, 512}
	dst = sdl.Rect{100, 50, 512, 512}

	renderer.Clear()
	renderer.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)
}
示例#5
0
文件: tmx.go 项目: billyninja/wander
func LoadTMX(mapname string, renderer *sdl.Renderer) ([][]Space, []*Object) {

	f, _ := os.Open(mapname)
	output, _ := ioutil.ReadAll(f)
	_ = f.Close()

	tmx := &TMX{}

	err := xml.Unmarshal(output, tmx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error parsing the tmx map: %s\n", err)
		os.Exit(11)
	}

	var tilesetTxt *sdl.Texture

	for i := 0; i < len(tmx.Tilesets); i++ {

		tmx.Tilesets[i].TTMap = make(map[int][4]*Terrain)

		tilesetImg, err := img.Load("assets/" + tmx.Tilesets[i].Image.Src)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load PNG: %s\n", err)
			os.Exit(3)
		}
		defer tilesetImg.Free()

		tilesetTxt, err = renderer.CreateTextureFromSurface(tilesetImg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
			os.Exit(4)
		}
		defer tilesetTxt.Destroy()

		for _, tt := range tmx.Tilesets[i].TerrainTiles {

			var terrList [4]*Terrain

			// Spliting and converting into integer so that it
			// can be used as array idx
			for ti, terr := range strings.Split(tt.TerStr, ",") {
				ttype, _ := strconv.Atoi(terr)
				terrList[ti] = &tmx.Tilesets[i].TerrTypes[ttype]
			}
			tmx.Tilesets[i].TTMap[tt.Id] = terrList
		}

		tmx.Tilesets[i].Txtr = tilesetTxt
	}

	world := make([][]Space, tmx.HeightTiles)
	for i := 0; i < tmx.Layers[0].Height; i++ {
		world[i] = make([]Space, tmx.WidthTiles)
	}

	ts := tmx.Tilesets[0]

	for li, layer := range tmx.Layers {
		for i := 0; i < layer.Height; i++ {
			for j := 0; j < layer.Width; j++ {

				tile := layer.Tiles[(i*layer.Height)+j]

				// RENDER ORDER: RIGHT-DOWN
				world[j][i].Terrains = ts.TTMap[tile.Gid]
				world[j][i].Gfxs[li] = &Gfx{
					Txtr:   ts.Txtr,
					Source: ts.GetGIDRect(tile.Gid),
				}

				for _, terr := range world[i][j].Terrains {
					if terr == nil {
						continue
					}

					for _, prop := range terr.PropList {
						switch prop.Name {
						case "COLL":
							world[i][j].Coll = (prop.Value == "1")
							break
						case "WARP":
							world[i][j].Warp = prop.Value
							break
						}
					}
				}
			}
		}
	}

	// Now loading the Tiled Objects
	var objects []*Object

	for _, g := range tmx.ObjGroups {
		for _, o := range g.Objects {
			obj := &Object{
				Pos:  sdl.Rect{o.X, o.Y, o.W, o.H},
				Gfx:  nil,
				Type: TRIG,
			}
			objects = append(objects, obj)
		}
	}

	//tmx = nil
	return world, objects
}
示例#6
0
文件: main.go 项目: 32bitkid/gtsp
func play(seq *video.VideoSequence) {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var texture *sdl.Texture
	var err error

	window, err = sdl.CreateWindow(winTitle,
		sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight,
		sdl.WINDOW_SHOWN)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		os.Exit(1)
	}
	defer window.Destroy()

	renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
		os.Exit(2)
	}
	defer renderer.Destroy()

	seq.AlignTo(video.SequenceHeaderStartCode)

	var pointer unsafe.Pointer
	var pitch int
	var yLen int
	var cLen int

	sdl.Delay(1000)
	renderer.Present()

	for {
		img, err := seq.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			fmt.Fprintf(os.Stderr, "Decoding error: %s\n", err)
			os.Exit(3)
		}

		if texture == nil {
			w, h := seq.Size()
			yLen = w * h
			cLen = (w * h) >> 2
			texture, err = renderer.CreateTexture(sdl.PIXELFORMAT_IYUV, sdl.TEXTUREACCESS_STREAMING, w, h)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
				os.Exit(4)
			}
			defer texture.Destroy()
		}

		{
			texture.Lock(nil, &pointer, &pitch)

			// Convert pointer to []uint8
			pixels := *(*[]uint8)(unsafe.Pointer(&reflect.SliceHeader{
				Data: uintptr(pointer),
				Len:  yLen + 2*cLen,
				Cap:  yLen + 2*cLen,
			}))

			// Select color planes
			y := pixels[0:yLen]
			cb := pixels[yLen : yLen+cLen]
			cr := pixels[yLen+cLen : yLen+cLen+cLen]

			// Copy image data into texture
			copy(y, img.Y)
			copy(cb, img.Cb)
			copy(cr, img.Cr)

			texture.Unlock()
		}

		err = renderer.Copy(texture, nil, nil)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Copy failed: %s\n", err)
			os.Exit(5)
		}
		renderer.Present()
	}
}