Ejemplo n.º 1
0
func (m *Menu) DrawItem(renderer *sdl.Renderer, index int) {
	var text_surface *sdl.Surface
	var text_texture *sdl.Texture
	var text_width, text_height int32
	var text_rect sdl.Rect
	var screen_width, _, _ int = renderer.GetRendererOutputSize()

	for index, menu_item := range m.menu_items {
		if index == m.selected_item {
			text_surface = m.font.RenderText_Solid(menu_item.text, *m.selected_color)
		} else {
			text_surface = m.font.RenderText_Solid(menu_item.text, *m.unselected_color)
		}
		text_width = text_surface.W
		text_height = text_surface.H

		text_texture = renderer.CreateTextureFromSurface(text_surface)

		text_rect = sdl.Rect{
			int32(screen_width) - 10 - text_width, //X position
			(m.font_size_32 * int32(index)),
			text_width,
			text_height,
		}

		renderer.Copy(text_texture, nil, &text_rect)
	}
}
Ejemplo n.º 2
0
func (gb *GUIBlock) Bake(renderer *sdl.Renderer) *sdl.Texture {

	finalTxtr, _ := renderer.CreateTexture(sdl.PIXELFORMAT_RGBA8888, sdl.TEXTUREACCESS_TARGET, 800, 600)
	originalTarget := renderer.GetRenderTarget()
	renderer.SetRenderTarget(finalTxtr)
	defer renderer.SetRenderTarget(originalTarget)

	renderer.SetDrawColor(1, 1, 1, 0)
	renderer.FillRect(gb.Pos)

	for _, el := range gb.Elements {
		println("Baking: ")
		println(el.Layer, el.Pos.X, el.Pos.Y)

		renderer.SetDrawColor(el.BGColor.R, el.BGColor.G, el.BGColor.B, el.BGColor.A)
		renderer.FillRect(el.Pos)

		for _, txt := range el.Texts {
			texture, W, H := txt.Bake(renderer)
			renderer.Copy(
				texture,
				&sdl.Rect{0, 0, W, H},
				&sdl.Rect{el.Pos.X + txt.RelPos.X, el.Pos.Y + txt.RelPos.Y, W, H})
		}
	}

	finalTxtr.SetBlendMode(sdl.BLENDMODE_BLEND)
	finalTxtr.SetAlphaMod(216)

	return finalTxtr
}
Ejemplo n.º 3
0
func (s *AnimatedSprite) Draw(renderer *sdl.Renderer) {
	src_rect := s.Src_rects[s.current_frame]
	dest_rect := &sdl.Rect{
		int32(s.X),
		int32(s.Y),
		src_rect.W,
		src_rect.H,
	}

	renderer.Copy(s.Texture, src_rect, dest_rect)
}
Ejemplo n.º 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
}
Ejemplo n.º 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()
}
Ejemplo n.º 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)
}
Ejemplo n.º 7
0
func (t *Label) Draw(renderer *sdl.Renderer) {
	surface, err := t.font.RenderUTF8_Solid(t.text, sdl.Color{255, 255, 255, 255})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create surface: %s\n", err)
		os.Exit(1)
	}

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

	src := &sdl.Rect{0, 0, surface.W, surface.H}
	dst := &sdl.Rect{t.rect.X, t.rect.Y, src.W, src.H}

	renderer.Copy(texture, src, dst)
	surface.Free()
}
Ejemplo n.º 8
0
func (m *Menu) DrawTitle(renderer *sdl.Renderer) {
	var text_surface *sdl.Surface
	var text_texture *sdl.Texture
	var text_width, text_height int32
	var title_rect sdl.Rect
	var _, screen_height, _ int = renderer.GetRendererOutputSize()

	text_surface = m.title_font.RenderText_Solid(m.menu_title, *m.title_color)

	text_width = text_surface.W
	text_height = text_surface.H

	text_texture = renderer.CreateTextureFromSurface(text_surface)

	title_rect = sdl.Rect{
		20, //X position
		int32(screen_height) - 20 - text_height, //Y position
		text_width,
		text_height,
	}

	renderer.Copy(text_texture, nil, &title_rect)
}
Ejemplo n.º 9
0
func (t *TitleState) Draw(renderer *sdl.Renderer) {
	renderer.Copy(t.currentTexture(), &t.src, &t.dest)
}
Ejemplo n.º 10
0
func (p *Player) Draw(renderer *sdl.Renderer) {
	renderer.Copy(p.Texture, p.Src_rect, &sdl.Rect{int32(p.X), int32(p.Y), p.Src_rect.W, p.Src_rect.H})
}
Ejemplo n.º 11
0
func (t *TestState) WriteOnScreen(renderer *sdl.Renderer, text string, X int32, Y int32) {
	text_surface := t.font.RenderText_Solid(text, sdl.Color{255, 255, 255, 255})
	text_texture := renderer.CreateTextureFromSurface(text_surface)
	renderer.Copy(text_texture, nil, &sdl.Rect{X, Y, text_surface.W, text_surface.H})
}
Ejemplo n.º 12
0
func (s *Sprite) Draw(renderer *sdl.Renderer) {
	renderer.Copy(s.Texture, s.Src_rect, &sdl.Rect{s.X, s.Y, s.Src_rect.W, s.Src_rect.H})
}
Ejemplo n.º 13
0
func (s *Scene) Render(renderer *sdl.Renderer) {

	// Empty CullM
	s.CullM.Zero()

	var init int32 = 0
	var Source *sdl.Rect

	var ofX, ofY int32 = tileSize, tileSize

	renderer.SetDrawColor(0, 0, 0, 255)

	// Rendering the map
	for sh := init; sh < s.Window.Height; sh += ofY {

		for sw := init; sw < s.Window.Width; sw += ofX {

			ofX = (tileSize - ((s.Cam.WX + sw) % tileSize))
			ofY = (tileSize - ((s.Cam.WY + sh) % tileSize))

			var worldCellX uint16 = uint16((s.Cam.WX + sw) / tileSize)
			var worldCellY uint16 = uint16((s.Cam.WY + sh) / tileSize)

			// Draw black box for out of bounds areas
			if worldCellX < 0 || worldCellX > s.WidthCells || worldCellY < 0 || worldCellY > s.HeightCells {
				renderer.FillRect(&sdl.Rect{sw, sh, ofX, ofY})
				continue
			}

			rect := Object{
				Pos: sdl.Rect{sw, sh, ofX, ofY},
			}

			for _, gfx := range s.World[worldCellX][worldCellY].Gfxs {

				if gfx != nil {

					if gfx.Txtr == nil {
						continue
					}

					if ofX != int32(tileSize) || ofY != int32(tileSize) {
						Source = &sdl.Rect{gfx.Source.X + (tileSize - ofX), gfx.Source.Y + (tileSize - ofY), ofX, ofY}
					} else {
						Source = gfx.Source
					}

					renderer.Copy(s.TsTxt, Source, &rect.Pos)

				}
			}

			// Updating CullM with SOLID/COLLIDABLE terrain types
			if s.World[worldCellX][worldCellY].Coll {

				// fabricate fake SOL obj
				tempO := &Object{
					Pos:  rect.Pos,
					Gfx:  nil,
					Type: SOL,
				}
				s.CullM.Add(tempO, s)
			}
		}
	}

	// Rendering the enemies
	for _, e := range s.Enemies {
		in, e_pos := s.CullM.Add(e, s)
		if in {
			renderer.Copy(s.SsTxt, e.GetPose(), &e_pos)
		}
	}

	// Rendering the player character
	pos := WorldToScreen(s.PC.Pos, s.Cam)
	renderer.Copy(s.SsTxt, s.PC.GetPose(), &pos)

	// Rendering FRAME RATE COUNTER
	fps := fmt.Sprintf("%v", s.GetFPS())
	surface, _ := s.Font.RenderUTF8_Solid(fps, sdl.Color{255, 255, 255, 255})
	defer surface.Free()

	txtr, _ := renderer.CreateTextureFromSurface(surface)
	defer txtr.Destroy()
	renderer.Copy(txtr, &sdl.Rect{0, 0, surface.W, surface.H}, &sdl.Rect{0, 0, surface.W, surface.H})

	// Rendering Game Objects
	for _, obj := range s.Objects {

		in, obj_pos := s.CullM.Add(obj, s)
		if in {
			renderer.SetDrawColor(255, 0, 0, 125)
			renderer.FillRect(&obj_pos)
		}
	}

	// Rendering GUI Blocks
	for _, gb := range s.GUIBlocks {
		renderer.Copy(gb.Baked, gb.Pos, gb.Pos)
	}

	renderer.Present()
}
Ejemplo n.º 14
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()
	}
}