func SetDrawColor(r *sdl.Renderer, color [4]uint8) {
	r.SetDrawColor(
		color[0],
		color[1],
		color[2],
		color[3])
}
Example #2
0
//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}
}
Example #3
0
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
}
Example #4
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var event sdl.Event
	var running bool

	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)
	}

	var peepArray []sdl.Event = make([]sdl.Event, 5)

	running = true
	for running {
		sdl.PumpEvents()
		numEventsRetrieved := sdl.PeepEvents(peepArray, sdl.PEEKEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT)
		if numEventsRetrieved < 0 {
			fmt.Printf("PeepEvents error: %s\n", sdl.GetError())
		} else {
			for i := 0; i < numEventsRetrieved; i++ {
				fmt.Printf("Event Peeked Value: %v\n", peepArray[i]) // primitive printing of event
			}
		}

		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseMotionEvent:
				fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
			case *sdl.MouseButtonEvent:
				fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
			case *sdl.MouseWheelEvent:
				fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y)
			case *sdl.KeyUpEvent:
				fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
					t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
			}
		}
		sdl.Delay(1000 / 30)
	}

	renderer.Destroy()
	window.Destroy()
}
Example #5
0
func (d *drawable) Draw(r *sdl.Renderer) {
	// Fill
	if d.fill {
		SetDrawColor(r, d.fill_color)
		r.FillRect(&sdl.Rect{d.X, d.Y, d.W, d.H})
	}

	// Draw bounds
	SetDrawColor(r, d.border_color)
	r.DrawRect(&sdl.Rect{d.X, d.Y, d.W, d.H})
}
Example #6
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)
}
func (d *RectDrawer) Draw(r *sdl.Renderer, offsetX, offsetY int32) error {
	if err := r.SetDrawColor(d.r, d.g, d.b, d.a); err != nil {
		return err
	}
	rect := &sdl.Rect{
		X: d.x + offsetX,
		Y: d.y + offsetY,
		W: d.w,
		H: d.h,
	}
	return r.FillRect(rect)
}
Example #8
0
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
}
Example #9
0
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
}
Example #10
0
func Draw(r *sdl.Renderer, g *grid.Grid) {
	g.EachRoom(func(room *grid.Room) {
		x := int32(room.X) * pixelSize * roomSize
		y := int32(room.Y) * pixelSize * roomSize
		width := roomSize * pixelSize
		height := roomSize * pixelSize
		rect := &sdl.Rect{x, y, width, height}

		if room.Blocked {
			r.FillRect(rect)
		} else {
			r.DrawRect(rect)
		}
	})
}
Example #11
0
func (g gameObject) Draw(r *sdl.Renderer) {
	tGeometry := make([]sdl.Rect, cap(g.geometry))
	copy(tGeometry, g.geometry)
	for index := range g.geometry {
		tGeometry[index].X += int32(g.location.X) * roomSize
		tGeometry[index].Y += int32(g.location.Y) * roomSize
	}
	for index := range tGeometry {
		tGeometry[index].X *= pixelSize
		tGeometry[index].Y *= pixelSize
		tGeometry[index].H *= pixelSize
		tGeometry[index].W *= pixelSize
	}
	r.FillRects(tGeometry)
}
Example #12
0
func DrawPath(r *sdl.Renderer, nodes []graph.Node) {
	for index, node := range nodes {
		if index != 0 {
			previousNode := nodes[index-1]
			startRoom := previousNode.(*grid.Room)
			endRoom := node.(*grid.Room)
			x1 := int32(startRoom.X) * pixelSize * roomSize
			y1 := int32(startRoom.Y) * pixelSize * roomSize
			x2 := int32(endRoom.X) * roomSize * pixelSize
			y2 := int32(endRoom.Y) * roomSize * pixelSize

			r.DrawLine(int(x1), int(y1), int(x2), int(y2))
		}
	}
}
Example #13
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var event sdl.Event
	var running bool

	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)
	}

	running = true
	for running {
		event = sdl.WaitEventTimeout(1000) // wait here until an event is in the event queue
		if event == nil {
			fmt.Println("WaitEventTimeout timed out")
			continue
		}

		switch t := event.(type) {
		case *sdl.QuitEvent:
			running = false
		case *sdl.MouseMotionEvent:
			fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
				t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
		case *sdl.MouseButtonEvent:
			fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
				t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
		case *sdl.MouseWheelEvent:
			fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
				t.Timestamp, t.Type, t.Which, t.X, t.Y)
		case *sdl.KeyUpEvent:
			fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
				t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
		}
	}

	renderer.Destroy()
	window.Destroy()
}
Example #14
0
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
}
Example #15
0
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var event sdl.Event
	var running bool
	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()

	running = true
	for running {
		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseMotionEvent:
				fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
			case *sdl.MouseButtonEvent:
				fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
			case *sdl.MouseWheelEvent:
				fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y)
			case *sdl.KeyUpEvent:
				fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
					t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
			}
		}
	}

	return 0
}
Example #16
0
// 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
}
Example #17
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()
}
Example #18
0
func DrawGraph(r *sdl.Renderer, g *graph.Graph) {
	nodesDrawn := make(map[graph.Node]struct{})
	g.EachAdjacencyList(func(n graph.Node, l []graph.Node) {
		for _, d := range l {
			_, drawn := nodesDrawn[d]

			if !drawn {
				startRoom := n.(*grid.Room)
				endRoom := d.(*grid.Room)
				x1 := int32(startRoom.X) * pixelSize * roomSize
				y1 := int32(startRoom.Y) * pixelSize * roomSize
				x2 := int32(endRoom.X) * roomSize * pixelSize
				y2 := int32(endRoom.Y) * roomSize * pixelSize

				r.DrawLine(int(x1), int(y1), int(x2), int(y2))
			}
		}

		nodesDrawn[n] = struct{}{}
	})
}
Example #19
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)
	}
}
Example #20
0
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
}
Example #21
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer

	sdl.Init(sdl.INIT_EVERYTHING)

	window = createWindow(winTitle, winHeight, winWidth)
	defer window.Destroy()

	renderer = createRenderer(window)
	defer renderer.Destroy()

	var secTickChan = time.Tick(time.Second)
	var events = &Events{}
	var fps = 0
	var view = NewGameView()

	for {
		// Pump events
		events.GetEvents()

		// Pass events and renderer to view
		view.Render(renderer, events)

		// This structure logs the fps
		select {
		case <-secTickChan:
			log.Println("fps:", fps)
			fps = 0
		default:
			fps++
		}

		// Delay the next frame rendering to free up CPU
		sdl.Delay(13)
	}
}
Example #22
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)
}
Example #23
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var image *sdl.Surface
	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 = sdl.LoadBMP(imageName)
	if image == nil {
		fmt.Fprintf(os.Stderr, "Failed to load BMP: %s", 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.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)

	image.Free()
	texture.Destroy()
	renderer.Destroy()
	window.Destroy()
}
Example #24
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var points []sdl.Point
	var rect sdl.Rect
	var rects []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)
	}

	renderer.SetDrawColor(255, 255, 255, 255)
	renderer.DrawPoint(150, 300)

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

	points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
	renderer.SetDrawColor(255, 255, 0, 255)
	renderer.DrawLines(points)

	rect = sdl.Rect{300, 0, 200, 200}
	renderer.SetDrawColor(255, 0, 0, 255)
	renderer.DrawRect(&rect)

	rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
	renderer.SetDrawColor(0, 255, 255, 255)
	renderer.DrawRects(rects)

	rect = sdl.Rect{250, 250, 200, 200}
	renderer.SetDrawColor(0, 255, 0, 255)
	renderer.FillRect(&rect)

	rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}}
	renderer.SetDrawColor(255, 0, 255, 255)
	renderer.FillRects(rects)

	renderer.Present()

	sdl.Delay(2000)

	renderer.Destroy()
	window.Destroy()
}
Example #25
0
func run() int {
	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)
		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 = sdl.LoadBMP(imageName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load BMP: %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.Copy(texture, &src, &dst)
	renderer.Present()

	sdl.Delay(2000)

	return 0
}
Example #26
0
func (t *TitleState) Draw(renderer *sdl.Renderer) {
	renderer.Copy(t.currentTexture(), &t.src, &t.dest)
}
Example #27
0
func (t *Rect) Draw(renderer *sdl.Renderer) {
	renderer.SetDrawColor(t.r, t.g, t.b, t.a)
	renderer.FillRect(t.Rect)
	renderer.DrawRect(t.Rect)
}
Example #28
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
}
Example #29
0
func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var event sdl.Event
	var running bool

	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)
	}

	var peepArray []sdl.Event = make([]sdl.Event, 2)
	peepArray[0] = &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 1331, nil, nil}
	peepArray[1] = &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 10101, nil, nil}

	running = true
	lastPushTime := sdl.GetTicks()
	for running {
		if lastPushTime+pushTime < sdl.GetTicks() {
			lastPushTime = sdl.GetTicks()
			sdl.PumpEvents()
			numEventsHandled := sdl.PeepEvents(peepArray, sdl.ADDEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT)
			if numEventsHandled < 0 {
				fmt.Printf("PeepEvents error: %s\n", sdl.GetError())
			} else {
				fmt.Printf("Successful push of %d events\n", numEventsHandled)
			}
		}

		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseMotionEvent:
				fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
			case *sdl.MouseButtonEvent:
				fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
			case *sdl.MouseWheelEvent:
				fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
					t.Timestamp, t.Type, t.Which, t.X, t.Y)
			case *sdl.KeyUpEvent:
				fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
					t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
			case *sdl.UserEvent:
				fmt.Printf("[%d ms] UserEvent\tcode:%d\n", t.Timestamp, t.Code)
			}
		}
		sdl.Delay(1000 / 30)
	}

	renderer.Destroy()
	window.Destroy()
}
Example #30
0
func main() {
	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)
		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 := img.Load(imageName)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load PNG: %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.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)
}