Beispiel #1
0
func MainLoop(renderer *sdl.Renderer, font *ttf.Font) {
	lastTick := sdl.GetTicks()
	lastSecond := lastTick
	frames := 0
	fpsText := gfx.CreateText(renderer, font, "FPS: ... "+StatusCallback(), sdl.Color{255, 255, 255, 255})
	framesPerSecond := 0

	for running {

		tick := sdl.GetTicks()
		if tick < lastTick+10 {
			sdl.Delay(1)

			continue
		}

		delta := float64(tick-lastTick) / 1000.0
		lastTick = tick
		fpsText.Destroy()
		fpsText = gfx.CreateText(renderer, font, fmt.Sprintf("FPS: %3d ", framesPerSecond)+StatusCallback(), sdl.Color{255, 255, 255, 255})

		if tick > (lastSecond + 1000) {
			lastSecond = tick
			framesPerSecond = frames
			frames = 0
		} else {
			frames++
		}

		handleEvents()

		UpdateCallback(delta)

		renderer.SetDrawColor(0, 0, 0, 0)
		renderer.Clear()

		RenderCallback(renderer)

		gfx.RenderTexture(renderer, fpsText, 0, 0)

		renderer.Present()

	}

	fpsText.Destroy()
}
Beispiel #2
0
func main() {

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		error("Error initializing SDL")
		return
	}

	defer sdl.Quit()

	ttf.Init()

	window := sdl.CreateWindow("Hello World!", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
		sdl.WINDOW_SHOWN)

	if window == nil {
		error("Error opening window")
		return
	}

	defer window.Destroy()

	renderer := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)

	if renderer == nil {
		error("Error creating renderer")
		return
	}

	defer renderer.Destroy()

	texture := img.LoadTexture(renderer, "assets/images/exampletexture_2.png")

	if texture == nil {
		error("Error creating texture")
		return
	}

	defer texture.Destroy()

	font, err := ttf.OpenFont("assets/fonts/ComicNeue-Regular.ttf", 18)

	if err != nil {
		log.Printf("Error when loading font: %v", error)
		return
	}

	defer font.Close()

	ok := mix.OpenAudio(22050, mix.DEFAULT_FORMAT, 2, 1024)
	if !ok {
		error("Can't open audio")
		return
	}

	defer mix.CloseAudio()

	mix.AllocateChannels(2)

	// Might have to convert sound effects to wav to load them as audio
	// and not music?
	shoot := mix.LoadMUS("assets/audio/shoot.ogg")
	if shoot == nil {
		error("Can't load ogg sound")
		return
	}

	defer shoot.Free()

	explode := mix.LoadMUS("assets/audio/invaderkilled.ogg")
	if explode == nil {
		error("Can't load ogg sound")
		return
	}

	defer explode.Free()

	game.RenderCallback = func(r *sdl.Renderer) {

		drawShip(r)
		drawMissiles(r)
		drawObstacles(r)
	}

	lastObstacleTick := sdl.GetTicks()

	game.UpdateCallback = func(delta float64) {

		energy += EnergyRegainedPerSecond * delta
		if energy > 100. {
			energy = 100.
		}

		moveSpeed := delta * 100

		keystate := sdl.GetKeyboardState()

		updateMissiles(moveSpeed * 3)
		updateObstacles(delta, explode)

		if sdl.GetTicks() > lastObstacleTick+500 && rand.Intn(20) == 0 {
			lastObstacleTick = sdl.GetTicks()
			size := rand.Float64()*20.0 + 10.0
			y := rand.Float64() * SCREEN_HEIGHT
			x := SCREEN_WIDTH + size
			speed := rand.Float64()*80.0 + 50.0
			obstacles = append(obstacles, Obstacle{x, y, speed, size, 0})
		}

		if keystate[sdl.SCANCODE_W] != 0 {
			y -= moveSpeed
		}
		if keystate[sdl.SCANCODE_S] != 0 {
			y += moveSpeed
		}
		if keystate[sdl.SCANCODE_A] != 0 {
			x -= moveSpeed
		}
		if keystate[sdl.SCANCODE_D] != 0 {
			x += moveSpeed
		}
		if keystate[sdl.SCANCODE_RSHIFT] != 0 {
			currentTick := sdl.GetTicks()
			if currentTick > lastFire+300 && energy > EnergyCostPerShot {
				lastFire = currentTick
				missiles = append(missiles, FloatPoint{x + 32, y})
				energy -= EnergyCostPerShot
				shoot.Play(1)
			}
		}
	}

	game.StatusCallback = func() string {
		return fmt.Sprintf("Energy: %3d   Score: %d", int(energy), score)
	}

	game.MainLoop(renderer, font)

}
Beispiel #3
0
		}
	}

	game.StatusCallback = func() string {
		return fmt.Sprintf("Energy: %3d   Score: %d", int(energy), score)
	}

	game.MainLoop(renderer, font)

}

type FloatPoint struct {
	X, Y float64
}

var lastFire = sdl.GetTicks()

var shipPoints = [...]sdl.Point{sdl.Point{30, 0}, sdl.Point{-20, -15}, sdl.Point{-20, 15}}

func drawShip(r *sdl.Renderer) {
	ship := make([]sdl.Point, 4)
	for i := 0; i < 4; i++ {
		p := shipPoints[i%3]
		ship[i] = sdl.Point{p.X + int32(x), p.Y + int32(y)}
	}

	r.SetDrawColor(255, 255, 255, 255)
	err := r.DrawLines(ship)
	if err != 0 {
		error("Draw error")
	}
Beispiel #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)
	}

	running = true
	lastPushTime := sdl.GetTicks()
	for running {

		// Push a UserEvent every second
		if lastPushTime+pushTime < sdl.GetTicks() {
			lastPushTime = sdl.GetTicks()
			pEvent := &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 1331, nil, nil}

			retVal := sdl.PushEvent(pEvent) // Here's where the event is actually pushed

			switch retVal {
			case 1:
				fmt.Println("PushEvent returned success")
			case 0:
				fmt.Println("PushEvent returned filtered")
			case -1:
				fmt.Printf("PushEvent returned error: %s\n", sdl.GetError)
			}
		}

		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()
}
Beispiel #5
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()
}