示例#1
0
// Loads resources
func (e *Engine) Load() {
	assetDir := filepath.Join("..", "android", "assets")
	if runtime.GOOS == "android" {
		assetDir = ""
	}

	err := e.LoadSprite(filepath.Join(assetDir, "images", "sprite.png"))
	if err != nil {
		sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadSprite: %s\n", err)
	}

	err = e.LoadFont(filepath.Join(assetDir, "fonts", "universalfruitcake.ttf"), 24)
	if err != nil {
		sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadTexture: %s\n", err)
	}

	err = e.LoadMusic(filepath.Join(assetDir, "music", "frantic-gameplay.ogg"))
	if err != nil {
		sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadMusic: %s\n", err)
	}

	err = e.LoadSound(filepath.Join(assetDir, "sounds", "click.wav"))
	if err != nil {
		sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "LoadSound: %s\n", err)
	}

	e.StateText = map[int]*Text{}
	for k, v := range stateText {
		t, _ := e.RenderText(v, sdl.Color{0, 0, 0, 0})
		_, _, tW, tH, _ := t.Query()
		e.StateText[k] = &Text{tW, tH, t}
	}
}
示例#2
0
func run() {
	runtime.LockOSThread()
	e := NewEngine()

	// Initialize SDL
	err := e.Init()
	if err != nil {
		sdl.LogError(sdl.LOG_CATEGORY_APPLICATION, "Init: %s\n", err)
	}
	defer e.Destroy()

	// Sprite size
	const n = 128

	// Sprite rects
	var rects []*sdl.Rect
	for x := 0; x < 6; x++ {
		rect := &sdl.Rect{int32(n * x), 0, n, n}
		rects = append(rects, rect)
	}

	// Load resources
	e.Load()

	// Play music
	e.Music.Play(-1)

	var frame int = 0
	var alpha uint8 = 255
	var showText bool = true

	var text *Text = e.StateText[StateRun]

	for e.Running() {

		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				e.Quit()

			case *sdl.MouseButtonEvent:
				e.Sound.Play(2, 0)
				if t.Type == sdl.MOUSEBUTTONDOWN && t.Button == sdl.BUTTON_LEFT {
					alpha = 255
					showText = true

					if e.State == StateRun {
						text = e.StateText[StateFlap]
						e.State = StateFlap
					} else if e.State == StateFlap {
						text = e.StateText[StateDead]
						e.State = StateDead
					} else if e.State == StateDead {
						text = e.StateText[StateRun]
						e.State = StateRun
					}
				}

			case *sdl.KeyDownEvent:
				if t.Keysym.Scancode == sdl.SCANCODE_ESCAPE || t.Keysym.Scancode == sdl.SCANCODE_AC_BACK {
					e.Quit()
				}
			}
		}

		e.Renderer.Clear()

		var clips []*sdl.Rect

		w, h := e.Window.GetSize()
		x, y := int32(w/2), int32(h/2)

		switch e.State {
		case StateRun:
			e.Renderer.SetDrawColor(168, 235, 254, 255)
			clips = rects[0:2]

		case StateFlap:
			e.Renderer.SetDrawColor(251, 231, 240, 255)
			clips = rects[2:4]

		case StateDead:
			e.Renderer.SetDrawColor(255, 250, 205, 255)
			clips = rects[4:6]
		}

		clip := clips[frame/2]

		e.Renderer.FillRect(nil)
		e.Renderer.Copy(e.Sprite, clip, &sdl.Rect{x - (n / 2), y - (n / 2), n, n})

		if showText {
			text.Texture.SetAlphaMod(alpha)
			e.Renderer.Copy(text.Texture, nil, &sdl.Rect{x - (text.Width / 2), y - n*1.5, text.Width, text.Height})
		}

		e.Renderer.Present()
		sdl.Delay(50)

		frame += 1
		if frame/2 >= 2 {
			frame = 0
		}

		alpha -= 10
		if alpha <= 10 {
			alpha = 255
			showText = false
		}
	}
}