Exemplo n.º 1
0
// GetNextEvent returns the next sdl.Event depending on the eventMode
func (g *GameManager) GetNextEvent() sdl.Event {
	switch g.eventMode {
	case GameManagerEventDriven:
		return sdl.WaitEvent()
	case GameManagerEventTimeoutDriven:
		return sdl.WaitEventTimeout(g.EventTimeout)
	case GameManagerPollDriven:
		return sdl.PollEvent()
	}

	panic(fmt.Sprintf("GetNextEvent: unknown event mode: %d", g.eventMode))
}
Exemplo n.º 2
0
func (gui *Gui) handleInput() {
	for true {
		ev := sdl.WaitEvent()
		switch ev := ev.(type) {
		case *sdl.MouseButtonEvent:
			if ev.Type == sdl.MOUSEBUTTONDOWN {
				gui.handleClick(ev)
			}
		case *sdl.KeyDownEvent:
			gui.handleKeyDown(ev)
		}
	}
}
Exemplo n.º 3
0
func run() int {
	font, charWidth, charHeight := initFont()

	cellSize := int32(30)
	cellPadding := int32(6)
	startX := int32(20)
	startY := int32(20)

	winSize := int(
		((cellSize + cellPadding) * 10) +
			(cellPadding * 2), // extra padding separating squares
	)

	menuHeight := 30

	window := createWindow("Sugoku", winSize, winSize+menuHeight)
	renderer := createRenderer(window)
	board := initBoard(cellSize, startX, startY, cellPadding)
	ctx := Ctx{
		window, renderer, &board,
		cellSize, cellPadding,
		font, charWidth, charHeight,
	}

	drawBoard(&ctx)

	running := true
	for running {
		if event := sdl.WaitEvent(); event != nil {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseButtonEvent:
				if t.State == 1 {
					clickButton(&ctx, t.X, t.Y)
				}
			case *sdl.KeyDownEvent:
				handleKey(&ctx, t.Keysym)
			}
		}
	}

	close(&ctx)

	return 0
}
Exemplo n.º 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
	for running {
		event = sdl.WaitEvent() // wait here until an event is in the event queue
		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()
}