Beispiel #1
0
func Draw_Init() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		fmt.Println(sdl.GetError())
	}

	//screen = sdl.SetVideoMode(sw, sh, 32, sdl.OPENGL)
	screen = sdl.SetVideoMode(sw, sh, 32, sdl.RESIZABLE)
	if screen == nil {
		return
	}
	var video_info = sdl.GetVideoInfo()

	fmt.Println("HW_available = ", video_info.HW_available)
	fmt.Println("WM_available = ", video_info.WM_available)
	fmt.Println("Video_mem = ", video_info.Video_mem, "kiB")

	surface = sdl.CreateRGBSurface(sdl.HWSURFACE, 500, 500, 32, rmask,
		gmask, bmask, amask)

	surface.FillRect(nil, 0xff00ff00)

	surfaceb = sdl.CreateRGBSurface(sdl.HWSURFACE, 500, 500, 32, rmask,
		gmask, bmask, amask)

	surfaceb.FillRect(nil, 0xffff0000)
}
Beispiel #2
0
func newSDL2xScreen(fullScreen bool) *sdl2xScreen {
	sdlMode := uint32(sdl.SWSURFACE)
	if fullScreen {
		application.Logf("%s", "Activate fullscreen mode")
		sdlMode = sdl.FULLSCREEN
		sdl.ShowCursor(sdl.DISABLE)
	}
	screenSurface := &sdlSurface{sdl.SetVideoMode(SCREEN_WIDTH*2, SCREEN_HEIGHT*2, 32, sdlMode)}
	if screenSurface.surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	borderSurface := &sdlSurface{sdl.CreateRGBSurface(sdl.SWSURFACE, SCREEN_WIDTH*2, SCREEN_HEIGHT*2, 32, 0, 0, 0, 0)}
	if borderSurface.surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	displaySurface := &sdlSurface{sdl.CreateRGBSurface(sdl.SWSURFACE, DISPLAY_WIDTH*2, DISPLAY_HEIGHT*2, 32, 0, 0, 0, 0)}
	if displaySurface.surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	return &sdl2xScreen{screenSurface, borderSurface, displaySurface}
}
func NewSDLRenderer(surface *sdl.Surface, font *ttf.Font) *SDLRenderer {
	renderer := &SDLRenderer{
		Color:          sdl.Color{255, 255, 255, 0},
		visibleSurface: surface,
		Font:           font,
		eventCh:        make(chan interface{}),
		updatedRectsCh: make(chan []sdl.Rect),
		updatedRects:   make([]sdl.Rect, 0),
		width:          uint(surface.W),
		height:         uint(surface.H),
	}

	fontWidth, fontHeight, err := font.SizeText("A")
	if err != 0 {
		panic("failed to determine font dimensions: " + sdl.GetError())
	}

	renderer.fontWidth = uint(fontWidth)
	renderer.fontHeight = uint(fontHeight)

	renderer.lastVisibleLine = (renderer.height / renderer.fontHeight) * MAX_INTERNAL_SIZE_FACTOR

	renderer.internalSurfaceHeight = uint(renderer.fontHeight)
	renderer.internalSurfaceMaxHeight = renderer.height * MAX_INTERNAL_SIZE_FACTOR
	renderer.internalSurface = sdl.CreateRGBSurface(sdl.SWSURFACE,
		int(renderer.width), int(renderer.internalSurfaceHeight), 32, 0, 0, 0, 0)

	renderer.calcCommandLineRect()

	renderer.updatedRects = append(renderer.updatedRects, sdl.Rect{0, 0, uint16(renderer.width), uint16(renderer.height)})

	go renderer.loop()

	return renderer
}
func initSDL() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	font := ttf.OpenFont("testdata/VeraMono.ttf", 20)

	if font == nil {
		panic(sdl.GetError())
	}

	appSurface = sdl.SetVideoMode(640, 480, 32, 0)
	sdlrenderer = NewSDLRenderer(sdl.CreateRGBSurface(sdl.SRCALPHA, 640, 480, 32, 0, 0, 0, 0), font)
	sdlrenderer.GetSurface().SetAlpha(sdl.SRCALPHA, 0xaa)
	console = NewConsole(nil)

	go func() {
		for {
			select {
			case rects := <-sdlrenderer.UpdatedRectsCh():
				render(rects)
			}

		}
	}()

	render(nil)
}
Beispiel #5
0
func initTest() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	font = ttf.OpenFont("../testdata/VeraMono.ttf", 12)

	if font == nil {
		panic(sdl.GetError())
	}

	appSurface = sdl.SetVideoMode(appSurfaceW, appSurfaceH, 32, 0)
	gopher = sdl.Load("../testdata/gopher.jpg")

	sdlrenderer := clingon.NewSDLRenderer(sdl.CreateRGBSurface(sdl.SRCALPHA, int(consoleW), int(consoleH), 32, 0, 0, 0, 0), font)
	sdlrenderer.GetSurface().SetAlpha(sdl.SRCALPHA, 0xaa)

	newRenderingLoop(sdlrenderer)

	console = clingon.NewConsole(&Echoer{})
	console.SetRenderer(sdlrenderer)
	console.Print("Welcome to the CLIngon shell!\n\n")
}
Beispiel #6
0
func newSDLSurface(w, h int) *sdlSurface {
	surface := sdl.CreateRGBSurface(sdl.SWSURFACE, w, h, 32, 0, 0, 0, 0)
	if surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	return &sdlSurface{surface}
}
Beispiel #7
0
func newSDLUnscaledScreen() *sdlUnscaledScreen {
	screenSurface := &sdlSurface{sdl.SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, sdl.SWSURFACE)}
	if screenSurface.surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	borderSurface := &sdlSurface{sdl.CreateRGBSurface(sdl.SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0, 0, 0, 0)}
	if borderSurface.surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	displaySurface := &sdlSurface{sdl.CreateRGBSurface(sdl.SWSURFACE, DISPLAY_WIDTH, DISPLAY_HEIGHT, 32, 0, 0, 0, 0)}
	if displaySurface.surface == nil {
		log.Printf("%s", sdl.GetError())
		application.Exit()
		return nil
	}
	return &sdlUnscaledScreen{screenSurface, borderSurface, displaySurface}
}
func (renderer *SDLRenderer) resizeInternalSurface(console *Console) {
	h := uint(len(console.lines)+1) * renderer.fontHeight
	if h > renderer.internalSurfaceMaxHeight {
		h = renderer.internalSurfaceMaxHeight
	}

	if renderer.internalSurfaceHeight != h {
		renderer.internalSurface.Free()

		renderer.internalSurface = sdl.CreateRGBSurface(sdl.SWSURFACE, int(renderer.width), int(h), 32, 0, 0, 0, 0)
		renderer.calcCommandLineRect()
		renderer.cursorY = int(renderer.commandLineRect.Y)
		renderer.viewportY = int(renderer.internalSurface.H - renderer.visibleSurface.H)
		renderer.internalSurfaceHeight = h
		renderer.internalSurface.SetClipRect(&sdl.Rect{0, 0, uint16(renderer.width), uint16(h)})
	}
}
Beispiel #9
0
// Initialization boilerplate
func initialize(config *configuration) *renderer {
	var bgImage, appSurface *sdl.Surface

	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	font := ttf.OpenFont(flag.Arg(0), 12)

	if font == nil {
		panic(sdl.GetError())
	}

	sdl.EnableUNICODE(1)

	if config.fullscreen {
		flags := sdl.FULLSCREEN
		appSurface = sdl.SetVideoMode(640, 480, 32, uint32(flags))
		sdl.ShowCursor(sdl.DISABLE)
	} else {
		appSurface = sdl.SetVideoMode(640, 480, 32, 0)
	}
	if config.bgImage != "" {
		bgImage = sdl.Load(config.bgImage)
	}

	sdlrenderer = clingon.NewSDLRenderer(sdl.CreateRGBSurface(sdl.SRCALPHA, int(config.consoleW), int(config.consoleH), 32, 0, 0, 0, 0), font)
	sdlrenderer.GetSurface().SetAlpha(sdl.SRCALPHA, 0xaa)

	return &renderer{
		config:         config,
		appSurface:     appSurface,
		sdlRenderer:    sdlrenderer,
		bgImageSurface: bgImage,
	}
}
func main() {
	// Use all processors
	runtime.GOMAXPROCS(runtime.NumCPU())

	// SDL voodoo
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}
	defer sdl.Quit()

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}
	defer ttf.Quit()

	screen := sdl.SetVideoMode(640, 480, 32, sdl.ANYFORMAT)
	if screen == nil {
		panic(sdl.GetError())
	}
	screen.SetAlpha(sdl.SRCALPHA, 255)

	sdl.WM_SetCaption("Connect Four", "")

	ticker := time.NewTicker(time.Second / 60 /*60 Hz*/)

	// Make some pipes for communicating with the game logic
	moveReady := make(chan int)
	newState := make(chan c4.State)
	nextMove := make(chan int)
	gameResults := make(chan c4.Piece)
	var winner c4.Piece
	var game c4.State
	waitingForMove := false
	gameOver := false

	// Get ready to write text
	font := ttf.OpenFont("DroidSans.ttf", 36)
	var line1, line2 *sdl.Surface
	showMessage := false

	// Start a game
	startGame := func() {
		c4.RunGame(
			SDLHuman{moveReady, nextMove},
			c4.AlphaBetaAI{
				c4.Black,
				8,
				func(game c4.State, p c4.Piece) float64 {
					// Evolved solution:
					// return c4.EvalFactors{
					// 		0.2502943943301069,
					// 		-0.4952316649483701,
					// 		0.3932539700819625,
					// 		-0.2742452616759889,
					// 		0.4746881137884282,
					// 		0.2091091127191147}.Eval(game, p)
					// Least mean squares solution after 2 iterations against evolved solution:
					return c4.EvalFactors{
						0.32386133725050104, 0.5490470895311659, 0.3932539698522742, -0.27424526114286796, 0.4746881136468789, 0.2091091126568151}.Eval(game, p)
				},
				func(game c4.State) bool {
					return game.GetWinner() != c4.None
				},
			},
			NewUpdater(newState),
			func(err error) {
				fmt.Println(err)
			},
			func(winner c4.Piece) {
				if winner == c4.Red {
					gameResults <- c4.Red
				} else if winner == c4.Black {
					gameResults <- c4.Black
				} else {
					gameResults <- c4.None
				}
			})
	}
	go startGame()

loop:
	for {
		select {
		case <-ticker.C:
			screen.FillRect(
				&sdl.Rect{0, 0, SCREEN_WIDTH, SCREEN_HEIGHT},
				BOARD_COLOR)
			for col := 0; col < c4.MaxColumns; col++ {
				for row := 0; row < c4.MaxRows; row++ {
					drawPiece(screen, col, row, game.GetPiece(col, row))
				}
			}
			if showMessage {
				screen.Blit(
					&sdl.Rect{
						int16(SCREEN_WIDTH/2 - line1.W/2),
						int16(SCREEN_HEIGHT/2 - line1.H),
						0,
						0},
					line1,
					nil)
				screen.Blit(
					&sdl.Rect{
						int16(SCREEN_WIDTH/2 - line2.W/2),
						int16(SCREEN_HEIGHT / 2),
						0,
						0},
					line2,
					nil)
			}
			screen.Flip()

		case event := <-sdl.Events:
			switch e := event.(type) {
			case sdl.MouseButtonEvent:
				if waitingForMove &&
					e.Type == sdl.MOUSEBUTTONUP &&
					e.Button == sdl.BUTTON_LEFT {
					waitingForMove = false
					nextMove <- int(e.X * c4.MaxColumns / SCREEN_WIDTH)

					// Tell user that the AI is thinking now
					line1 =
						sdl.CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, 0)
					line2 =
						ttf.RenderText_Blended(font,
							"Thinking...",
							sdl.Color{255, 255, 255, 0})
					showMessage = true
				} else if gameOver &&
					e.Type == sdl.MOUSEBUTTONUP &&
					e.Button == sdl.BUTTON_LEFT {
					gameOver = false
					showMessage = false
					go startGame()
				}
			case sdl.QuitEvent:
				break loop
			}

		case game = <-newState:
			// We did the assignment; there's nothing else to do

		case <-moveReady:
			waitingForMove = true
			showMessage = false

		case winner = <-gameResults:
			gameOver = true
			var message string
			if winner == c4.Red {
				message = "You win!"
			} else if winner == c4.Black {
				message = "You lose."
			} else {
				message = "Draw."
			}
			line1 =
				ttf.RenderText_Blended(font,
					message,
					sdl.Color{255, 255, 255, 0})

			line2 =
				ttf.RenderText_Blended(font,
					"Click to play again...",
					sdl.Color{255, 255, 255, 0})

			showMessage = true
		}

	}
}