Esempio n. 1
0
func (d *LEM1802) LoadFontFromTTF(filename string) (font *Font, err error) {
	if filename == "" {
		filename = resourcemanager.NewResourceManager("github.com/kierdavis/go/dcpuem/lem1802").GetFilename("FreeMono.ttf")
	}

	f := ttf.OpenFont(filename, 8)
	if f == nil {
		return nil, ErrCouldNotLoadFont
	}

	defer f.Close()

	font = new(Font)

	for ch := uint16(0); ch < 128; ch++ {
		_, _, _, _, advance, e := f.GlyphMetrics(ch)
		if e != 0 {
			return nil, ErrCouldNotLoadFont
		}

		surf := sdl.CreateRGBSurface(sdl.SWSURFACE|sdl.SRCALPHA, advance, 8, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)

		fontSurf := ttf.RenderText_Blended(f, string(ch), sdl.Color{255, 255, 255, 0})
		fontSurf.Blit(nil, surf, nil)

		font[ch] = surf
	}

	return font, nil
}
Esempio n. 2
0
func main() {
	ResourceManager = resourcemanager.NewResourceManager("github.com/kierdavis/go/amberfell/cellulose/test_cellulose")

	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT, mixer.DEFAULT_CHANNELS, 4096) != 0 {
		panic(sdl.GetError())
	}
	defer mixer.CloseAudio()

	soundFiles, err := filepath.Glob(ResourceManager.GetFilename("sound/out/*.wav"))
	if err != nil {
		panic(err)
	}

	RowSounds = make([]*mixer.Chunk, 0)
	ColSounds = make([]*mixer.Chunk, 0)

	l2 := len(soundFiles) / 2

	for _, filename := range soundFiles[:l2] {
		RowSounds = append(RowSounds, mixer.LoadWAV(filename))
	}

	for _, filename := range soundFiles[l2:] {
		ColSounds = append(ColSounds, mixer.LoadWAV(filename))
	}

	err = termbox.Init()
	if err != nil {
		panic(err)
	}
	defer termbox.Close()

	go runSeq()

loop:
	for {
		switch ev := termbox.PollEvent(); ev.Type {
		case termbox.EventKey:
			switch ev.Key {
			case termbox.KeyEsc:
				break loop
			}
		}
	}
}
Esempio n. 3
0
File: k270em.go Progetto: 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!")
}