當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Renderer.CreateTextureFromSurface方法代碼示例

本文整理匯總了Golang中github.com/veandco/go-sdl2/sdl.Renderer.CreateTextureFromSurface方法的典型用法代碼示例。如果您正苦於以下問題:Golang Renderer.CreateTextureFromSurface方法的具體用法?Golang Renderer.CreateTextureFromSurface怎麽用?Golang Renderer.CreateTextureFromSurface使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/veandco/go-sdl2/sdl.Renderer的用法示例。


在下文中一共展示了Renderer.CreateTextureFromSurface方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GenerateMenuShellOneColumn

//TODO Make sdl_tff work!
func (menuGeneratorInfo *MenuGeneratorInfo) GenerateMenuShellOneColumn(renderer *sdl.Renderer, x, y, dx, dy int32) Menu {
	background, err := renderer.CreateTextureFromSurface(menuGeneratorInfo.menuBackground)
	if err != nil {
		panic(err)
	}

	buttonCount := len(menuGeneratorInfo.buttonTexts)

	menuItems := make([]*MenuItem, buttonCount)
	highlightedBackground, notHighlightedBackground, srcRects, FrameLength := menuGeneratorInfo.menuItemAnimation.Generate(renderer)

	TEXTSURFACE := sdl.Texture{}
	TEXTRECT := sdl.Rect{0, 0, 0, 0}

	for i := 0; i < buttonCount; i++ {
		currentRect := sdl.Rect{x, y + dy*int32(i), dx, dy}
		currentRects := make([]*sdl.Rect, len(srcRects))
		for j := 0; j < len(srcRects); j++ {
			currentRects[j] = &currentRect
		}
		previous := (i - 1 + buttonCount) % buttonCount
		next := (i + 1) % buttonCount
		menuAction := MenuAction{nil, func(menuInfo *MenuInfo, menuItem *MenuItem) int { return previous }, func(menuInfo *MenuInfo, menuItem *MenuItem) int { return next }, nil, nil}
		menuItems[i] = &MenuItem{notHighlightedBackground, highlightedBackground, srcRects, currentRects, &TEXTSURFACE, &TEXTRECT, menuAction, FrameLength}
	}
	return Menu{background, menuItems, nil}
}
開發者ID:ITR13,項目名稱:campusFighterI,代碼行數:28,代碼來源:MenuGenerator.go

示例2: DrawItem

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)
	}
}
開發者ID:kageurufu,項目名稱:platformr,代碼行數:28,代碼來源:menu.go

示例3: Bake

func (t *TextEl) Bake(renderer *sdl.Renderer) (*sdl.Texture, int32, int32) {
	surface, _ := t.Font.RenderUTF8_Solid(t.Content, t.Color)
	defer surface.Free()

	txtr, _ := renderer.CreateTextureFromSurface(surface)
	//defer txtr.Destroy()

	return txtr, surface.W, surface.H
}
開發者ID:billyninja,項目名稱:wander,代碼行數:9,代碼來源:gui.go

示例4: LoadTexture

func LoadTexture(renderer *sdl.Renderer, path string) *sdl.Texture {
	bmp, err := img.Load(path)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to load image %s! SDL Error: %s\n", path, img.GetError())
	} else {
		texture, err := renderer.CreateTextureFromSurface(bmp)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to create texture %s! SDL Error: %s\n", path, sdl.GetError())
		}
		bmp.Free()
		return texture
	}
	return nil
}
開發者ID:EvanTheB,項目名稱:SDLGame,代碼行數:14,代碼來源:graphics.go

示例5: LoadText

func LoadText(renderer *sdl.Renderer, font *ttf.Font, text string, textColor sdl.Color) *sdl.Texture {
	bmp := font.RenderText_Solid(text, textColor)
	if bmp == nil {
		fmt.Fprintf(os.Stderr, "Unable to load text %s! SDL Error: %s\n", ttf.GetError())
	} else {
		texture, err := renderer.CreateTextureFromSurface(bmp)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to create texture %s! SDL Error: %s\n", sdl.GetError())
		}
		bmp.Free()
		return texture
	}
	return nil
}
開發者ID:EvanTheB,項目名稱:SDLGame,代碼行數:14,代碼來源:font.go

示例6: run

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
}
開發者ID:emlai,項目名稱:go-sdl2,代碼行數:49,代碼來源:texture_png.go

示例7: main

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()
}
開發者ID:JalfResi,項目名稱:go-sdl2,代碼行數:47,代碼來源:texture_png.go

示例8: main

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)
}
開發者ID:flazz,項目名稱:go-sdl2,代碼行數:46,代碼來源:texture.go

示例9: LoadTextureTransparent

func LoadTextureTransparent(renderer *sdl.Renderer, path string, r, g, b uint8) *sdl.Texture {
	bmp, err := img.Load(path)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to load image %s! SDL Error: %s\n", path, img.GetError())
	} else {
		if bmp.SetColorKey(1, sdl.MapRGB(bmp.Format, r, g, b)) != 0 {
			fmt.Fprintf(os.Stderr, "Unable to set Color Key %s! SDL Error: %s\n", path, sdl.GetError())
		}
		texture, err := renderer.CreateTextureFromSurface(bmp)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to create texture %s! SDL Error: %s\n", path, sdl.GetError())
		}
		bmp.Free()
		return texture
	}
	return nil
}
開發者ID:EvanTheB,項目名稱:SDLGame,代碼行數:17,代碼來源:graphics.go

示例10: Load

// Load loads the image into a Texture
func Load(renderer *sdl.Renderer, fileName, id string) error {
	tempSurface, err := img.Load(fileName)

	if err != nil {
		return err
	}

	defer tempSurface.Free()

	texture, err := renderer.CreateTextureFromSurface(tempSurface)

	if err != nil {
		return err
	}

	textureMap[id] = texture

	return nil
}
開發者ID:yukiisbored,項目名稱:Kaori,代碼行數:20,代碼來源:texture.go

示例11: Draw

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()
}
開發者ID:stephenbinns,項目名稱:ein,代碼行數:19,代碼來源:label.go

示例12: DrawTitle

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)
}
開發者ID:kageurufu,項目名稱:platformr,代碼行數:23,代碼來源:menu.go

示例13: Generate

func (animation *MenuItemAnimation) Generate(renderer *sdl.Renderer) (*sdl.Texture, *sdl.Texture, []*sdl.Rect, uint64) {
	highlightedTexture, err := renderer.CreateTextureFromSurface(animation.highlightedAnimation)
	if err != nil {
		panic(err)
	}
	notHighlightedTexture, err := renderer.CreateTextureFromSurface(animation.notHighlightedAnimation)
	rows := animation.highlightedAnimation.W / animation.dx
	columns := animation.highlightedAnimation.H / animation.dy
	rects := make([]*sdl.Rect, rows*columns)
	xMult := columns * int32((animation.AnimationDirection>>YX)%2)
	if xMult == 0 {
		xMult = 1
	}
	yMult := rows * int32(1-(animation.AnimationDirection>>YX)%2)
	if yMult == 0 {
		yMult = 1
	}
	xDir := int32(1)
	xPluss := int32(0)
	yDir := int32(1)
	yPluss := int32(0)
	if (animation.AnimationDirection>>LeftRight)%2 == 0 {
		xDir = -1
		xPluss = rows
	}
	if (animation.AnimationDirection>>UpDown)%2 == 0 {
		yDir = -1
		yPluss = columns
	}
	for y := int32(0); y < columns; y++ {
		for x := int32(0); x < rows; x++ {
			currentRect := sdl.Rect{animation.dx * x, animation.dy * y, animation.dx, animation.dy}
			rects[((xPluss+x*xDir)*xMult + (yPluss+y*yDir)*yMult)] = &currentRect
		}
	}
	return highlightedTexture, notHighlightedTexture, rects, animation.FrameLength
}
開發者ID:ITR13,項目名稱:campusFighterI,代碼行數:37,代碼來源:MenuGenerator.go

示例14: Render

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)
//.........這裏部分代碼省略.........
開發者ID:billyninja,項目名稱:wander,代碼行數:101,代碼來源:scene.go

示例15: WriteOnScreen

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})
}
開發者ID:kageurufu,項目名稱:platformr,代碼行數:5,代碼來源:test_state.go


注:本文中的github.com/veandco/go-sdl2/sdl.Renderer.CreateTextureFromSurface方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。