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)
}
Пример #2
0
func main() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	screen := sdl.SetVideoMode(400, 300, 32, 0)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("Template", "")

	ticker := time.NewTicker(1e9 / 2 /*2 Hz*/)

loop:
	for {
		select {
		case <-ticker.C:
			// Note: For better efficiency, use UpdateRects instead of Flip
			screen.FillRect(nil /*color*/, rand.Uint32())
			//screen.Blit(&sdl.Rect{x,y, 0, 0}, image, nil)
			screen.Flip()

		case event := <-sdl.Events:
			fmt.Printf("%#v\n", event)

			switch event.(type) {
			case sdl.QuitEvent:
				break loop
			}
		}
	}
}
Пример #3
0
func main() {
	if sdl.Init(sdl.INIT_VIDEO) < 0 {
		fmt.Printf(
			"Video initialization failed: %s\n",
			sdl.GetError())
		quit_tutorial(1)
	}
	info := sdl.GetVideoInfo()
	if info == nil {
		fmt.Printf(
			"Video query failed: %s\n",
			sdl.GetError())
		quit_tutorial(1)
	}
	width := 640
	height := 480
	bpp := int(info.Vfmt.BitsPerPixel)
	sdl.GL_SetAttribute(sdl.GL_RED_SIZE, 5)
	sdl.GL_SetAttribute(sdl.GL_GREEN_SIZE, 5)
	sdl.GL_SetAttribute(sdl.GL_BLUE_SIZE, 5)
	sdl.GL_SetAttribute(sdl.GL_DEPTH_SIZE, 16)
	sdl.GL_SetAttribute(sdl.GL_DOUBLEBUFFER, 1)
	flags := uint32(sdl.OPENGL | sdl.FULLSCREEN)
	if sdl.SetVideoMode(width, height, bpp, flags) == nil {
		fmt.Printf("Video mode set failed: %s\n", sdl.GetError())
		quit_tutorial(1)
	}
	setup_opengl(width, height)
	for {
		process_events()
		draw_screen()
	}
	os.Exit(0)
}
Пример #4
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	screen := sdl.SetVideoMode(640, 480, 32, sdl.SWSURFACE)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("Hello World", "test.png")

	message := load_image("hello.bmp")
	background := load_image("background.bmp")

	apply_surface(0, 0, background, screen)
	apply_surface(320, 0, background, screen)
	apply_surface(0, 240, background, screen)
	apply_surface(320, 240, background, screen)

	apply_surface(180, 140, message, screen)

	message.Free()
	background.Free()

	screen.Flip()
	sdl.Delay(2000)

}
Пример #5
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}
}
Пример #6
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")
}
Пример #7
0
func LoadImage(name string) *sdl.Surface {
	image := sdl.Load(name)
	if image == nil {
		panic(sdl.GetError())
	}
	return image
}
Пример #8
0
func (v *Video) Init(t <-chan []int, d <-chan []int, n string) {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}

	v.screen = sdl.SetVideoMode(512, 480, 32, 0)

	if v.screen == nil {
		log.Fatal(sdl.GetError())
	}

	sdl.WM_SetCaption(fmt.Sprintf("Fergulator - %s", n), "")

	v.tick = t
	v.debug = d
}
Пример #9
0
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
}
Пример #10
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)
}
Пример #11
0
func StartGraphics(id uint32, update chan *State, move chan MoveMsg, quit chan bool) error {
	var err error

	rc := sdl.Init(sdl.INIT_VIDEO)
	if rc == -1 {
		return errors.New(sdl.GetError())
	}
	disp := sdl.SetVideoMode(800, 600, 32, sdl.OPENGL)
	if disp == nil {
		return errors.New(sdl.GetError())
	}
	winscale = gl.Mul4(gl.Translate(-1, 1, 0), gl.Scale(1./400, -1./300, 1))
	gl.Init()
	gl.ClearColor(1, 1, 1, 1)
	s := new(State)
	ls := time.Now()
	quadBuf = gl.NewBuffer(gl.ARRAY_BUFFER, quadData, gl.STATIC_DRAW)
	prog, err = gl.MakeProgram([]string{vertexShader}, []string{fragmentShader})
	if err != nil {
		log.Fatal(err)
	}
	AddTex("glenda", "glenda.png")
	AddTex("tux", "tux.png")
	i := image.NewRGBA(image.Rect(0, 0, 800, 600))
	texs["black"] = gl.NewTexture2D(i, 0)

	tick := time.Tick(time.Second / 50)
	squit := make(chan bool)
	go ProcessInput(move, quit, squit)
	for {
		select {
		case s = <-update:
			ls = time.Now()
		case <-tick:
			s.Advance(time.Now().Sub(ls))
			ls = time.Now()
			gl.Clear(gl.COLOR_BUFFER_BIT)
			RenderScene(s)
			sdl.GL_SwapBuffers()
		case <-squit:
			return nil
		}
	}
	return nil
}
Пример #12
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}
}
Пример #13
0
func sdlInit(width, height int32) *sdl.Surface {

	var surface = sdl.SetVideoMode(int(width), int(height), 32, sdl.RESIZABLE)

	if surface == nil {
		panic(sdl.GetError())
	}
	return surface
}
Пример #14
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	screen := sdl.SetVideoMode(640, 480, 32, sdl.SWSURFACE)
	if screen == nil {
		panic(sdl.GetError())
	}

	hello := sdl.Load("hello.bmp")

	screen.Blit(nil, hello, nil)
	screen.Flip()

	sdl.Delay(2000)
	hello.Free()
}
Пример #15
0
func load_image(filename string) *sdl.Surface {
	loadedImage := sdl.Load(filename)
	if loadedImage == nil {
		panic(sdl.GetError())
	}

	optimizedImage := loadedImage.DisplayFormat()
	loadedImage.Free()

	return optimizedImage
}
Пример #16
0
func NewRacer(levelDir string) (*Racer, error) {
	r := &Racer{cars: make([]*Car, 0)}

	var err error
	if r.obstaclemap, err = LoadImageGray(filepath.Join(levelDir, "velocity.png")); err != nil {
		return nil, err
	}
	if r.heightmap, err = LoadImageGray(filepath.Join(levelDir, "z.png")); err != nil {
		return nil, err
	}

	carSize := 0.02 * float32(screenWidth)
	if r.spriteCarFG, err = NewSprite("data/cars/car1/fg.png", carSize, carSize); err != nil {
		return nil, err
	}
	if r.spriteCarBG, err = NewSprite("data/cars/car1/bg.png", carSize, carSize); err != nil {
		return nil, err
	}

	r.carSize = carSize

	if r.spriteForeground, err = NewSprite(filepath.Join(levelDir, "foreground.png"), screenWidth, screenHeight); err != nil {
		return nil, err
	}
	if r.spriteBackground, err = NewSprite(filepath.Join(levelDir, "background.png"), screenWidth, screenHeight); err != nil {
		return nil, err
	}

	if r.music = mixer.LoadMUS("data/music.ogg"); r.music == nil {
		return nil, errors.New(sdl.GetError())
	}

	if r.font = ttf.OpenFont("data/font.otf", 32); r.font == nil {
		return nil, errors.New(sdl.GetError())
	}

	textWaiting := ttf.RenderUTF8_Blended(r.font, "Waiting for other players. Press space to start....", sdl.Color{0, 0, 255, 0})
	r.spriteWaiting = NewSpriteFromSurface(textWaiting)

	return r, nil
}
Пример #17
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}
}
Пример #18
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,
	}
}
Пример #19
0
func main() {

	iWidth := int32(WIDTH)
	iHeight := int32(HEIGHT)

	var screen = sdl.SetVideoMode(int(iWidth), int(iHeight), 32, sdl.RESIZABLE)

	if screen == nil {
		log.Fatal(sdl.GetError())
	}

	iIterationNr := 0
	iIterations := int(math.Min(float64(333333), float64((WIDTH)*(HEIGHT))))
	iStartNS := time.Now().UnixNano()

	//mainloop:
	for {

		var iX int32
		var iY int32
		var iColor uint32
		for i := 0; i < iIterations; i++ {
			iX = rand.Int31() % (iWidth)
			iY = rand.Int31() % (iHeight)
			iColor = rand.Uint32()
			iColor &= 0x00ffffff
			iColor += 0x88000000
			draw_point(iX, iY, iColor, screen)
			//break mainloop;
		}

		screen.Flip()

		iIterationNr++
		if iIterationNr%99 == 0 {
			iIterationNS := time.Now().UnixNano()
			iDeltaNS := iIterationNS - iStartNS
			nDeltaS := float64(iDeltaNS) / float64(1000000000)
			nFPS := float64(iIterationNr) / nDeltaS
			fmt.Printf("fps: %v\n", nFPS)
			fmt.Printf("%v x %v\n", screen.W, screen.H)
		}

	}
	base.Dump("")

}
Пример #20
0
Файл: main.go Проект: Sas0ri/sms
func main() {
	verbose := flag.Bool("verbose", false, "verbose mode")
	debug := flag.Bool("debug", false, "debug mode")
	fullScreen := flag.Bool("fullscreen", false, "go fullscreen")
	help := flag.Bool("help", false, "Show usage")
	flag.Usage = usage
	flag.Parse()

	if *help {
		usage()
		return
	}

	application.Verbose = *verbose
	application.Debug = *debug

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}

	screen := newSDL2xScreen(*fullScreen)
	sdlLoop := newSDLLoop(screen)
	emulatorLoop := newEmulatorLoop(sdlLoop)
	if emulatorLoop == nil {
		usage()
		return
	}
	commandLoop := newCommandLoop(emulatorLoop, sdlLoop)
	inputLoop := newInputLoop(emulatorLoop.sms)

	application.Register("Emulator loop", emulatorLoop)
	application.Register("Command loop", commandLoop)
	application.Register("SDL render loop", sdlLoop)
	application.Register("SDL input loop", inputLoop)

	exitCh := make(chan bool)
	application.Run(exitCh)
	<-exitCh
	sdl.Quit()
}
Пример #21
0
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
		}

	}
}
Пример #22
0
Файл: k270em.go Проект: iand/go
// Function main is the main entry point in the program.
func main() {
	flag.Parse()

	if flag.NArg() < 1 {
		fmt.Fprintf(os.Stderr, "Not enough arguments\nusage: %s file.hex\n", os.Args[0])
		os.Exit(2)
	}

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

	defer sdl.Quit()

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

	defer ttf.Quit()

	rm := resourcemanager.NewResourceManager("github.com/kierdavis/go/k270em")

	font := ttf.OpenFont(rm.GetFilename("FreeMono.ttf"), 12)
	if font == nil {
		panic(sdl.GetError())
	}

	defer font.Close()

	_, _, _, _, fontWidth, errID := font.GlyphMetrics('m')
	if errID != 0 {
		panic(sdl.GetError())
	}

	fontHeight := font.Height()

	fmt.Printf("h: %d, w: %d\n", fontHeight, fontWidth)

	screenHeight := fontHeight * 48
	screenWidth := fontWidth * 128

	screen := sdl.SetVideoMode(screenWidth, screenHeight, 32, sdl.RESIZABLE)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("K270 Emulator Display", "")

	f, err := os.Open(flag.Arg(0))
	die(err)
	defer f.Close()

	reader := bufio.NewReader(f)
	ix, err := ihex.ReadIHex(reader)
	die(err)
	program := ix.ExtractDataToEnd(0)

	em := k270emlib.NewEmulator()
	em.SetTraceFile(os.Stdout)
	em.SetMemory(program)

	running := true
	stopRequest := make(chan bool)
	vmem := em.GetVideoMemory()

	go em.Run()
	go func() {
		scanTicker := time.NewTicker(time.Second / 24.0) // 24 hz
		color := sdl.Color{255, 255, 255, 0}

		for {
			if !running {
				break
			}

			select {
			case <-scanTicker.C:
				address := uint16(0)
				for y := 0; y < 48; y++ {
					chars := make([]byte, 128)

					for x := 0; x < 128; x++ {
						c := vmem[address].Char
						if c == 0 {
							c = ' '
						}
						chars[x] = c
						address++
					}

					surf := ttf.RenderText_Solid(font, string(chars), color)
					screen.Blit(&sdl.Rect{0, int16(y * fontHeight), 0, 0}, surf, nil)
				}

			case <-stopRequest:
				return
			}
		}
	}()

	ticker := time.NewTicker(time.Second / 24.0) // 24 fps

	for running {
		select {
		case <-ticker.C:
			screen.Flip()

		case ev := <-sdl.Events:
			switch e := ev.(type) {
			case sdl.QuitEvent:
				running = false

			case sdl.KeyboardEvent:
				if e.Keysym.Sym == sdl.K_ESCAPE {
					running = false
				}

			case sdl.ResizeEvent:
				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)
				if screen == nil {
					panic(sdl.GetError())
				}
			}
		}
	}

	stopRequest <- true

	//fmt.Println("Locking...")
	em.Mutex.Lock()
	//fmt.Println("Locked!")
	em.SetRunning(false)
	//fmt.Println("Unlocking...")
	em.Mutex.Unlock()
	//fmt.Println("Unlocked!")
}
Пример #23
0
func main() {
	log.SetFlags(0)

	var resourcePath string
	{
		GOPATH := os.Getenv("GOPATH")
		if GOPATH == "" {
			log.Fatal("No such environment variable: GOPATH")
		}
		for _, gopath := range strings.Split(GOPATH, ":") {
			a := gopath + "/src/github.com/0xe2-0x9a-0x9b/Go-SDL/sdl-test"
			_, err := os.Stat(a)
			if err == nil {
				resourcePath = a
				break
			}
		}
		if resourcePath == "" {
			log.Fatal("Failed to find resource directory")
		}
	}

	var joy *sdl.Joystick
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}

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

	if sdl.NumJoysticks() > 0 {
		// Open joystick
		joy = sdl.JoystickOpen(0)

		if joy != nil {
			println("Opened Joystick 0")
			println("Name: ", sdl.JoystickName(0))
			println("Number of Axes: ", joy.NumAxes())
			println("Number of Buttons: ", joy.NumButtons())
			println("Number of Balls: ", joy.NumBalls())
		} else {
			println("Couldn't open Joystick!")
		}
	}

	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		log.Fatal(sdl.GetError())
	}

	var screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)

	if screen == nil {
		log.Fatal(sdl.GetError())
	}

	var video_info = sdl.GetVideoInfo()

	println("HW_available = ", video_info.HW_available)
	println("WM_available = ", video_info.WM_available)
	println("Video_mem = ", video_info.Video_mem, "kb")

	sdl.EnableUNICODE(1)

	sdl.WM_SetCaption("Go-SDL SDL Test", "")

	image := sdl.Load(resourcePath + "/test.png")

	if image == nil {
		log.Fatal(sdl.GetError())
	}

	sdl.WM_SetIcon(image, nil)

	running := true

	font := ttf.OpenFont(resourcePath+"/Fontin Sans.otf", 72)

	if font == nil {
		log.Fatal(sdl.GetError())
	}

	font.SetStyle(ttf.STYLE_UNDERLINE)
	white := sdl.Color{255, 255, 255, 0}
	text := ttf.RenderText_Blended(font, "Test (with music)", white)
	music := mixer.LoadMUS(resourcePath + "/test.ogg")
	sound := mixer.LoadWAV(resourcePath + "/sound.ogg")

	if music == nil || sound == nil {
		log.Fatal(sdl.GetError())
	}

	music.PlayMusic(-1)

	if sdl.GetKeyName(270) != "[+]" {
		log.Fatal("GetKeyName broken")
	}

	worm_in := make(chan Point)
	draw := make(chan Point, 64)

	var out chan Point
	var in chan Point

	out = worm_in

	in = out
	out = make(chan Point)
	go worm(in, out, draw)

	ticker := time.NewTicker(time.Second / 50) // 50 Hz

	// Note: The following SDL code is highly ineffective.
	//       It is eating too much CPU. If you intend to use Go-SDL,
	//       you should to do better than this.

	for running {
		select {
		case <-ticker.C:
			screen.FillRect(nil, 0x302019)
			screen.Blit(&sdl.Rect{0, 0, 0, 0}, text, nil)

		loop:
			for {
				select {
				case p := <-draw:
					screen.Blit(&sdl.Rect{int16(p.x), int16(p.y), 0, 0}, image, nil)

				case <-out:
				default:
					break loop
				}
			}

			var p Point
			sdl.GetMouseState(&p.x, &p.y)
			worm_in <- p

			screen.Flip()

		case _event := <-sdl.Events:
			switch e := _event.(type) {
			case sdl.QuitEvent:
				running = false

			case sdl.KeyboardEvent:
				println("")
				println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))

				if e.Keysym.Sym == sdl.K_ESCAPE {
					running = false
				}

				fmt.Printf("%04x ", e.Type)

				for i := 0; i < len(e.Pad0); i++ {
					fmt.Printf("%02x ", e.Pad0[i])
				}
				println()

				fmt.Printf("Type: %02x Which: %02x State: %02x Pad: %02x\n", e.Type, e.Which, e.State, e.Pad0[0])
				fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", e.Keysym.Scancode, e.Keysym.Sym, e.Keysym.Mod, e.Keysym.Unicode)

			case sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					println("Click:", e.X, e.Y)
					in = out
					out = make(chan Point)
					go worm(in, out, draw)
					sound.PlayChannel(-1, 0)
				}

			case sdl.JoyAxisEvent:
				println("Joystick Axis Event ->", "Type", e.Type, "Axis:", e.Axis, " Value:", e.Value, "Which:", e.Which)

			case sdl.JoyButtonEvent:
				println("Joystick Button Event ->", e.Button)
				println("State of button", e.Button, "->", joy.GetButton(int(e.Button)))

			case sdl.ResizeEvent:
				println("resize screen ", e.W, e.H)

				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)

				if screen == nil {
					log.Fatal(sdl.GetError())
				}
			}
		}
	}

	// Close if opened
	if sdl.JoystickOpened(0) > 0 {
		joy.Close()
	}

	image.Free()
	music.Free()
	font.Close()

	ttf.Quit()
	sdl.Quit()
}
Пример #24
0
func main() {
	runtime.LockOSThread()

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal(sdl.GetError())
	}
	var screen = sdl.SetVideoMode(screenWidth, screenHeight, 32, sdl.OPENGL|sdl.HWSURFACE|sdl.GL_DOUBLEBUFFER|sdl.FULLSCREEN)
	if screen == nil {
		log.Fatal(sdl.GetError())
	}
	sdl.WM_SetCaption("Lecture Hall Games", "")
	sdl.EnableUNICODE(1)
	if gl.Init() != 0 {
		log.Fatal("could not initialize OpenGL")
	}
	gl.Viewport(0, 0, int(screen.W), int(screen.H))
	gl.ClearColor(1, 1, 1, 0)
	gl.Clear(gl.COLOR_BUFFER_BIT)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, float64(screen.W), float64(screen.H), 0, -1.0, 1.0)
	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.DEPTH_TEST)
	gl.TexEnvi(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE)

	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		log.Fatal(sdl.GetError())
	}

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

	if p, err := build.Default.Import(basePkg, "", build.FindOnly); err == nil {
		os.Chdir(p.Dir)
	}

	var err error

	rand.Seed(time.Now().UnixNano())
	levelDir := fmt.Sprintf("data/levels/demolevel%d", 3+rand.Intn(numberLevels))
	//carsDir := fmt.Sprintf(" data/cars/car%d/", 1+rand.Intn(numberCars))
	if game, err = NewRacer(levelDir); err != nil {
		log.Fatal(err)
	}

	go func() {
		listen, err := net.Listen("tcp", ":8001")
		if err != nil {
			log.Fatal(err)
		}
		for {
			conn, err := listen.Accept()
			if err != nil {
				log.Println(err)
				continue
			}
			go handleConnection(conn)
		}
	}()

	running := true
	last := time.Now()
	for running {
		select {
		case event := <-sdl.Events:
			switch e := event.(type) {
			case sdl.QuitEvent:
				running = false
			case sdl.ResizeEvent:
				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)
			case sdl.KeyboardEvent:
				if e.Type == sdl.KEYDOWN {
					if e.Keysym.Sym == sdl.K_ESCAPE {
						running = false
					} else {
						game.KeyPressed(e.Keysym)
					}
				}
			}
		default:
		}

		current := time.Now()
		t := current.Sub(last)
		last = current

		mu.Lock()
		game.Update(t)
		game.Render(screen)
		mu.Unlock()

		sdl.GL_SwapBuffers()
	}

	sdl.Quit()
}
Пример #25
0
func main() {
	var p Playfield = NewPlayfield(StandardWidth, StandardHeight)

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

	var screen = sdl.SetVideoMode(p.width*PixPerBrick,
		p.height*PixPerBrick, 32, sdl.RESIZABLE)

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

	//var video_info = sdl.GetVideoInfo()

	sdl.EnableUNICODE(1)

	sdl.WM_SetCaption("YATI:Go", "")

	dropTicker := time.NewTicker(time.Second / 2.0)
	var curPiece *Piece = nil
	score := 0
mainloop:
	for {
		select {
		case <-dropTicker.C:
			if curPiece == nil {
				curPiece = makeNewTet()
				curPiece.pos.x = p.width/2 - 2
				if !curPiece.move(&p, 0, 0) {
					fmt.Println("Failed placement test")
					break mainloop
				}
			} else {
				// advance piece
				if !curPiece.move(&p, 0, 1) {
					p.place(curPiece)
					// remove lines
					score += p.removeCompletedLines()
					fmt.Println(score)
					timeDiv := 2.0 + float64(score/10)/4.0
					duration := float64(time.Second) / timeDiv
					dropTicker = time.NewTicker(time.Duration(duration))
					curPiece = nil
				}
			}

		case _event := <-sdl.Events:
			switch e := _event.(type) {
			case sdl.QuitEvent:
				break mainloop
			case sdl.KeyboardEvent:
				//key := sdl.GetKeyName(sdl.Key(e.Keysym.Sym))
				//println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))
				if e.Type == sdl.KEYDOWN {
					switch e.Keysym.Sym {
					case sdl.K_ESCAPE:
						break mainloop
					case sdl.K_w:
						curPiece.rotate(&p, -1)
					case sdl.K_a:
						curPiece.move(&p, -1, 0)
					case sdl.K_d:
						curPiece.move(&p, 1, 0)
					case sdl.K_s:
						//curPiece.rotate(&p,1)
						fallthrough
					case sdl.K_SPACE:
						// turn on drop mode
						dropTicker = time.NewTicker(time.Second / 60.0)
					}

				}
			}
		}
		p.render(screen)
		curPiece.render(screen)
		screen.Flip()
	}
	//fmt.Println("----")
	//p.removeLine(5)
	//p.print()
}
Пример #26
0
func main() {

	sdl.Init(sdl.INIT_VIDEO)

	screen = sdl.SetVideoMode(300, 300, 16, sdl.OPENGL|sdl.RESIZABLE)
	if !screen {
		fprintf(stderr, "Couldn't set 300x300 GL video mode: %s\n", sdl.GetError())
		sdl.Quit()
		exit(2)
	}
	sdl.WM_SetCaption("Gears", "gears")

	init(argc, argv)
	reshape(screen.w, screen.h)
	done := false
	for !done {
		var event sdl.Event

		idle()
		for sdl.PollEvent(&event) {
			switch event.Type {
			case sdl.VIDEORESIZE:
				screen = sdl.SetVideoMode(event.resize.w, event.resize.h, 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen {
					reshape(screen.w, screen.h)
				} else {
					/* Uh oh, we couldn't set the new video mode?? */
				}
				break

			case sdl.QUIT:
				done = true
				break
			}
		}
		keys = sdl.GetKeyState(NULL)

		if keys[SDLK_ESCAPE] {
			done = true
		}
		if keys[SDLK_UP] {
			view_rotx += 5.0
		}
		if keys[SDLK_DOWN] {
			view_rotx -= 5.0
		}
		if keys[SDLK_LEFT] {
			view_roty += 5.0
		}
		if keys[SDLK_RIGHT] {
			view_roty -= 5.0
		}
		if keys[SDLK_z] {
			if sdl.GetModState() & KMOD_SHIFT {
				view_rotz -= 5.0
			} else {
				view_rotz += 5.0
			}
		}

		draw()
	}
	sdl.Quit()
	return 0 /* ANSI C requires main to return int. */
}