Ejemplo n.º 1
0
func CreateMenus(window *sdl.Window, surface *sdl.Surface, renderer *sdl.Renderer, controlManager *inputManager.ControlManager) inputManager.Update {
	progress := float64(0)
	mutex := sync.Mutex{}
	dx := 4 * surface.W / 8
	startRect := sdl.Rect{surface.W / 4, 4 * surface.H / 7, 0, 2 * surface.H / 7}
	midBackgroundRect := sdl.Rect{surface.W / 4, 4 * surface.H / 7, dx, 2 * surface.H / 7}
	backgroundRect := sdl.Rect{surface.W/4 - 5, 4*surface.H/7 - 5, dx + 10, 2*surface.H/7 + 10}
	var mainMenu *Menu
	go asyncMenuCreator(renderer, &mutex, &progress, &mainMenu, surface.W, surface.H, window, controlManager)
	return func(state int) int {
		if controlManager.Player1.BF || controlManager.Player2.BF {
			controlManager.Running = false
			return -1
		}
		mutex.Lock()
		startRect.W = int32(float64(dx) * progress)
		surface.FillRect(&backgroundRect, 0xffff0000)
		surface.FillRect(&midBackgroundRect, 0xff000000)
		surface.FillRect(&startRect, 0xffff0000)
		window.UpdateSurface()
		if progress == 1 {
			menuInfo := MenuInfo{0, controlManager, renderer, &sdl.Rect{0, 0, surface.W, surface.H}}
			inputManager.UpdateFunctions = append(inputManager.UpdateFunctions, mainMenu.Open(0, &menuInfo))
			return -1
		}
		mutex.Unlock()
		return 0
	}
}
Ejemplo n.º 2
0
func run() int {
	var window *sdl.Window
	var font *ttf.Font
	var surface *sdl.Surface
	var solid *sdl.Surface
	var err error

	sdl.Init(sdl.INIT_VIDEO)

	if err := ttf.Init(); err != nil {
		fmt.Fprintf(os.Stderr, "Failed to initialize TTF: %s\n", err)
		return 1
	}

	if window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN); err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 2
	}
	defer window.Destroy()

	if font, err = ttf.OpenFont("../../assets/test.ttf", 32); err != nil {
		fmt.Fprint(os.Stderr, "Failed to open font: %s\n", err)
		return 4
	}
	defer font.Close()

	if solid, err = font.RenderUTF8_Solid("Hello, World!", sdl.Color{255, 0, 0, 255}); err != nil {
		fmt.Fprint(os.Stderr, "Failed to render text: %s\n", err)
		return 5
	}
	defer solid.Free()

	if surface, err = window.GetSurface(); err != nil {
		fmt.Fprint(os.Stderr, "Failed to get window surface: %s\n", err)
		return 6
	}

	if err = solid.Blit(nil, surface, nil); err != nil {
		fmt.Fprint(os.Stderr, "Failed to put text on window surface: %s\n", err)
		return 7
	}

	// Show the pixels for a while
	window.UpdateSurface()
	sdl.Delay(3000)

	return 0
}
Ejemplo n.º 3
0
func Draw(window *sdl.Window, parsedInput *ParsedInput, loop *bool) inputManager.Update {
	surface, err := window.GetSurface()
	if err != nil {
		panic(err)
	}
	surface.FillRect(&sdl.Rect{0, 0, surface.W, surface.H}, 0xff000000)

	var player2Rects [6]sdl.Rect
	for i := 0; i < 6; i++ {
		player2Rects[i] = sdl.Rect{(256 + player1Rects[i].X), (player1Rects[i].Y), (player1Rects[i].W), (player1Rects[i].H)}
	}

	return func(state int) int {
		if *loop {
			for i := 0; i < 6; i++ {
				if parsedInput[i] {
					surface.FillRect(&player1Rects[i], 0xffff0000)
				} else {
					surface.FillRect(&player1Rects[i], 0xffffff00)
				}
				if parsedInput[i+6] {
					surface.FillRect(&player2Rects[i], 0xffff0000)
				} else {
					surface.FillRect(&player2Rects[i], 0xffffff00)
				}
			}
			if parsedInput[12] {
				surface.FillRect(&sdl.Rect{192, 160, 64, 64}, 0xffff0000)
			} else {
				surface.FillRect(&sdl.Rect{192, 160, 64, 64}, 0xffffff00)
			}
			if parsedInput[13] {
				surface.FillRect(&sdl.Rect{192, 224, 64, 64}, 0xffff0000)
			} else {
				surface.FillRect(&sdl.Rect{192, 224, 64, 64}, 0xffffff00)
			}
			err := window.UpdateSurface()
			if err != nil {
				panic(err)
			}
		} else {
			return -1
		}
		return 0
	}
}