Example #1
0
func (t *Text) draw_texture(texture *sdl.Texture, x, y int32, center_x, center_y bool) {
	_, _, t.w, t.h, _ = texture.Query()
	if center_x {
		x = int32(win_size/2 - t.w/2)
	}
	if center_y {
		y = int32(win_size/2 - t.h/2)
	}
	dst = sdl.Rect{x, y, int32(t.w), int32(t.h)}
	renderer.Copy(texture, &src, &dst)
}
Example #2
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()
}
Example #3
0
func lockTexture(tex *sdl.Texture, width int, height int) (pixels []Pixel, pitch int) {
	var data unsafe.Pointer
	err := tex.Lock(nil, &data, &pitch)
	if err != nil {
		fmt.Fprintf(os.Stderr, "tex.Lock: %v", err)
		panic(err)
	}
	// Convert pitch units from byte to pixels
	pitch /= 4
	pixels = sliceFromPixelPtr(data, width*height)
	return
}
Example #4
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
}
Example #5
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()
}
Example #6
0
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)
}
Example #7
0
func (t *Texture) MarkSubTexture(r splish.Rectangle) interface{} {
	si := t.Src.SubImage(r.ToImage()).(*image.RGBA)

	size := r.Size()
	var subTex *sdl.Texture
	var err error
	subTex, err = t.Win.renderer.CreateTexture(
		sdl.PIXELFORMAT_RGBA8888,
		sdl.TEXTUREACCESS_STATIC,
		int(size.X),
		int(size.Y),
	)
	if err == nil {
		err = subTex.Update(nil, unsafe.Pointer(&si.Pix), t.Src.Stride)
	}
	if err != nil {
		return 0
	}
	t.Subs = append(t.Subs, subTex)

	return len(t.Subs) - 1
}
Example #8
0
// GetSizeOfGraphic returns the width and heigth of the Graphic, g.
// GetSizeOfGraphic returns two numbers of type int. The first numnber,
// marked as 'width', is the width of the Graphic, g, in pixels. The second number,
// marked as 'height' is the height of the Graphic, g, in pixels.
//
// GetSizeOfGraphic will panic if:
//
// 1. The Graphic, g, does not contain a Graphic type.
//
// 2. The toolbox has not been initialised.
func GetSizeOfGraphic(g Graphic) (width, height int) {
	if !initialised {
		// this stops execution here, so ne need for an else after the if
		panic(notInitialisedMessage)
	}
	if g == nil {
		panic(nilGraphicMessage)
	}
	var w, h int32
	var err error
	var t *sdl.Texture
	t = g
	_, _, w, h, err = t.Query()
	if err != nil {
		fmt.Print("Failed to query texture: ")
		fmt.Println(err)
		panic(err)
	}
	width = int(w)
	height = int(h)
	// return is implicit - using named return paramaters
	return
}
Example #9
0
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
}
Example #10
0
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()
	}
}