示例#1
0
文件: gui.go 项目: billyninja/wander
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
}
func SetDrawColor(r *sdl.Renderer, color [4]uint8) {
	r.SetDrawColor(
		color[0],
		color[1],
		color[2],
		color[3])
}
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)
}
示例#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
}
示例#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()
}
示例#6
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()
}
示例#7
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)
}
示例#8
0
func run(serverStr, name string) int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var running bool
	var event sdl.Event
	var inputState InputState

	url := "ws://" + serverStr
	origin := "http://" + serverStr
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}

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

	renderer.Clear()

	websocket.Message.Send(ws, joinMessage(name))

	running = true

	var data map[string]map[string][]map[string]interface{}
	decoder := msgpack.NewDecoder(ws)

	renderer.SetDrawColor(1, 1, 1, 255)
	renderer.Clear()

	for running {
		renderer.SetDrawColor(1, 1, 1, 255)
		renderer.Clear()
		newInputState := inputState

		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				running = false

			case *sdl.KeyDownEvent:
				switch event.(*sdl.KeyDownEvent).Keysym.Sym {
				case sdl.K_ESCAPE:
					running = false
				case sdl.K_LEFT:
					newInputState.Left = true
				case sdl.K_RIGHT:
					newInputState.Right = true
				case sdl.K_UP:
					newInputState.Thrust = true
				}

			case *sdl.KeyUpEvent:
				switch event.(*sdl.KeyUpEvent).Keysym.Sym {
				case sdl.K_LEFT:
					newInputState.Left = false
				case sdl.K_RIGHT:
					newInputState.Right = false
				case sdl.K_UP:
					newInputState.Thrust = false
				}
			}
		}

		if !newInputState.IsSameAs(inputState) {
			websocket.Message.Send(ws, newInputState.Message())
			inputState = newInputState
		}

		decoder.Decode(&data)

		for _, ship := range data["state"]["ships"] {
			colourhex, _ := hex.DecodeString(ship["colour"].(string)[1:7])
			rot, _ := ship["rotation"].(float64)
			s := Ship{
				Position: V2{float64(ship["x"].(int64)), float64(ship["y"].(int64))},
				Size:     30,
				Rotation: rot,
				Doge:     ship["doge"].(bool),
			}
			renderer.SetDrawColor(uint8(colourhex[0]), uint8(colourhex[1]), uint8(colourhex[2]), 255)
			renderer.DrawLines(s.Points())
		}

		renderer.Present()
	}

	return 0
}
示例#9
0
func (view *gameView) Render(renderer *sdl.Renderer, events *Events) {
	view.player.calculateMovement(events)

	// start each new frame by filling the frame black
	renderer.SetDrawColor(0, 0, 0, 0)
	renderer.Clear()

	// draw the player
	renderer.SetDrawColor(255, 0, 0, 255)
	playerSDLRect := view.player.rect.GetSDLRect()
	renderer.DrawRect(playerSDLRect)
	renderer.FillRect(playerSDLRect)

	// draw the players spinning weapon
	playerWeaponSDLRect := view.player.weapon.rect.GetSDLRect()
	renderer.DrawRect(playerWeaponSDLRect)
	renderer.FillRect(playerWeaponSDLRect)

	// destroyedMeteors is used to remove destroyed meteors outside
	// of looping through view.meteors to avoid out of range errors
	var destroyedMeteors []int

	// loop through the frames active meteors
	for i, meteor := range view.meteors {
		// move and draw and then reinsert the moved meteor
		meteor.move()
		enemySDLRect := meteor.rect.GetSDLRect()
		renderer.SetDrawColor(0, 255, 0, 255)
		renderer.DrawRect(enemySDLRect)
		renderer.FillRect(enemySDLRect)
		view.meteors[i] = meteor

		// if the player and meteor collide, end the game
		if view.player.rect.Colision(meteor.rect) {
			renderer.Present()
			log.Println("You died!")
			log.Println("Score:", view.score)
			sdl.Delay(1000)
			os.Exit(0)
		}

		// if the weapon and meteor collide, destroy it and update score
		if meteor.rect.Colision(view.player.weapon.rect) {
			destroyedMeteors = append(destroyedMeteors, i)
			view.score += 10
		}

		// if the meteor is off screen, destroy it and update score
		if meteor.isOffScreen() {
			destroyedMeteors = append(destroyedMeteors, i)
			view.score++
		}
	}

	// for each destroyed meteor, remove it from the meteors slice
	for _, i := range destroyedMeteors {
		view.meteors = append(view.meteors[:i], view.meteors[i+1:]...)
	}

	// present the updated frame to the player
	renderer.Present()

	// add new meteors depending on the difficulty and ticks since
	// last meteor was added
	if view.meteorTimer > 100 {
		newMeteor := spawnMeteor()
		view.meteors = append(view.meteors, newMeteor)
		view.meteorTimer = rand.Intn(30) + view.difficulty
		if view.difficulty < 60 {
			view.difficulty += 3
		}
	}
	view.meteorTimer++

	return
}
示例#10
0
文件: render.go 项目: emlai/go-sdl2
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var points []sdl.Point
	var rect sdl.Rect
	var rects []sdl.Rect

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

	renderer.Clear()

	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)

	return 0
}
示例#11
0
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var points []sdl.Point
	var rect sdl.Rect
	var rects []sdl.Rect
	var err error

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

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

	sdl.Do(func() {
		renderer.Clear()
	})
	defer func() {
		sdl.Do(func() {
			renderer.Destroy()
		})
	}()

	go func() {
		println("goroutine: A")
	}()

	sdl.Do(func() {
		renderer.SetDrawColor(255, 255, 255, 255)
		renderer.DrawPoint(150, 300)
		println("queue: A")
	})

	go func() {
		println("goroutine: B")
	}()

	sdl.Do(func() {
		renderer.SetDrawColor(0, 0, 255, 255)
		renderer.DrawLine(0, 0, 200, 200)
		println("queue: B")
	})

	go func() {
		println("goroutine: C")
	}()

	sdl.Do(func() {
		points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
		renderer.SetDrawColor(255, 255, 0, 255)
		renderer.DrawLines(points)
		println("queue: C")
	})

	go func() {
		println("goroutine: D")
	}()

	sdl.Do(func() {
		rect = sdl.Rect{300, 0, 200, 200}
		renderer.SetDrawColor(255, 0, 0, 255)
		renderer.DrawRect(&rect)
		println("queue: D")
	})

	go func() {
		println("goroutine: E")
	}()

	sdl.Do(func() {
		rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
		renderer.SetDrawColor(0, 255, 255, 255)
		renderer.DrawRects(rects)
		println("queue: E")
	})

	go func() {
		println("goroutine: F")
	}()

	sdl.Do(func() {
		rect = sdl.Rect{250, 250, 200, 200}
		renderer.SetDrawColor(0, 255, 0, 255)
		renderer.FillRect(&rect)
		println("queue: F")
	})

	go func() {
		println("goroutine: G")
	}()

	sdl.Do(func() {
		rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}}
		renderer.SetDrawColor(255, 0, 255, 255)
		renderer.FillRects(rects)
		println("queue: G")
	})

	go func() {
		println("goroutine: H")
	}()

	sdl.Do(func() {
		renderer.Present()
		println("queue: H")
	})

	sdl.Do(func() {
		sdl.Delay(2000)
	})

	return 0
}
示例#12
0
func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var err error

	sdl.Do(func() {
		window, err = sdl.CreateWindow(WindowTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WindowWidth, WindowHeight, sdl.WINDOW_OPENGL)
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 1
	}
	defer func() {
		sdl.Do(func() {
			window.Destroy()
		})
	}()

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

	sdl.Do(func() {
		renderer.Clear()
	})

	for i := range rects {
		rects[i] = sdl.Rect{
			X: int32(rand.Int() % WindowWidth),
			Y: int32(i * WindowHeight / len(rects)),
			W: RectWidth,
			H: RectHeight,
		}
	}

	running := true
	for running {
		sdl.Do(func() {
			for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
				switch event.(type) {
				case *sdl.QuitEvent:
					runningMutex.Lock()
					running = false
					runningMutex.Unlock()
				}
			}

			renderer.Clear()
			renderer.SetDrawColor(0, 0, 0, 0x20)
			renderer.FillRect(&sdl.Rect{0, 0, WindowWidth, WindowHeight})
		})

		// Do expensive stuff using goroutines
		wg := sync.WaitGroup{}
		for i := range rects {
			wg.Add(1)
			go func(i int) {
				rects[i].X = (rects[i].X + 10) % WindowWidth
				sdl.Do(func() {
					renderer.SetDrawColor(0xff, 0xff, 0xff, 0xff)
					renderer.DrawRect(&rects[i])
				})
				wg.Done()
			}(i)
		}
		wg.Wait()

		sdl.Do(func() {
			renderer.Present()
			sdl.Delay(1000 / FrameRate)
		})
	}

	return 0
}
示例#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()
}