Exemple #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
}
func writeSDLstr(newStr string) {

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}
	var font = ttf.OpenFont("FontinSans.otf", int(fontsize))

	blue := sdl.Color{185, 230, 255, 0}
	white := sdl.Color{255, 255, 255, 0}
	text0 := ttf.RenderText_Blended(font, previousStr, blue)
	font.SetStyle(ttf.STYLE_UNDERLINE)
	text1 := ttf.RenderText_Blended(font, newStr, white)

	introw = introw - fontsize - fontsize/2
	screen.FillRect(&sdl.Rect{3, introw, uint16(winWidth - 3), uint16(fontsize + fontsize/2)}, bgcolor)
	screen.Blit(&sdl.Rect{3, introw, 0, 0}, text0, nil)
	introw = introw + fontsize + fontsize/2
	screen.Blit(&sdl.Rect{3, introw, 0, 0}, text1, nil)
	introw = introw + fontsize + fontsize/2
	if introw > (int16(winHeight) - fontsize) {
		introw = 0
	}
	screen.FillRect(&sdl.Rect{3, introw, uint16(winWidth - 3), uint16(fontsize + fontsize/2)}, bgcolor)

	screen.Flip()
	previousStr = newStr

}
Exemple #3
0
func NewFont(name string, size int) (font *ttf.Font) {
	font = ttf.OpenFont("/usr/share/fonts/TTF/"+name, size)
	if font == nil {
		panic(sdl.GetError())
	}
	return font
}
Exemple #4
0
func main() {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

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

	var screen = sdl.SetVideoMode(1400, 300, 32, 0)
	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)

	font := ttf.OpenFont("euler.otf", 24)

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

	black := sdl.Color{0, 0, 0, 0}
	text := ttf.RenderUTF8_Blended(font, "!!", black)
	fmt.Println(text.Bounds())
	sdl.WM_SetCaption("Go-SDL SDL Test", "")
	running := true
	num := NewNumberShower("b+c", "", "euler.otf", 24, 0)
	denom := NewNumberShower("a", "", "euler.otf", 24, 0)
	testy := NewFractionShower(num, denom, "euler.otf", 24, 0)
	testy.CursorPosition = 3
	testy.RenderEmptyIndextxt = true
	str := ""
	for running {
		switch e := sdl.PollEvent().(type) {
		case *sdl.QuitEvent:
			running = false
			break
		case *sdl.KeyboardEvent:

			if e.State == 1 {
				str = handlekeypress2(e.Keysym, str)
				fmt.Println("rendering string", str)
				text = ttf.RenderUTF8_Blended(font, str, black)
			}
		}
		screen.FillRect(nil, 0xFFFFFF)
		screen.Blit(&sdl.Rect{23, 0, 0, 0}, testy.Render(), nil)
		screen.Flip()
		sdl.Delay(5)
	}
}
Exemple #5
0
func NewScreen(w, h int) *Screen {
	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		log.Fatal("failed to init sdl", sdl.GetError())
		return nil
	}

	if ttf.Init() != 0 {
		log.Fatal("failed to init ttf", sdl.GetError())
		return nil
	}

	var ctx Screen
	if err := ctx.setSurface(w, h); err != nil {
		log.Fatal(err)
	}

	var vInfo = sdl.GetVideoInfo()
	log.Println("HW_available = ", vInfo.HW_available)
	log.Println("WM_available = ", vInfo.WM_available)
	log.Println("Video_mem = ", vInfo.Video_mem, "kb")

	/* title := "Subtitle Player" */
	title := os.Args[0]
	icon := "" // path/to/icon
	sdl.WM_SetCaption(title, icon)

	sdl.EnableUNICODE(1)

	ctx.fps = opt.fps

	err := ctx.setFont(opt.fontPath, opt.fontSize)
	if err != nil {
		log.Fatal("failed to set default font")
		return nil
	}

	ctx.debugFont = ttf.OpenFont(DFLT_FONT_PATH, 20)
	if ctx.debugFont == nil {
		errMsg := fmt.Sprintf("failed to open debug font: %s",
			sdl.GetError())
		/* return errors.New(errMsg) */
		log.Fatal(errMsg)
	}
	ctx.debugLineHeight = uint16(ctx.debugFont.LineSkip())

	ctx.updateC = make(chan int)

	return &ctx
}
Exemple #6
0
func NewFont(filename string, size int, c color.Color) *Font {

	var font Font
	font.textures = make(map[rune]gl.Texture)
	font.widths = make(map[rune]int)

	extfont := ttf.OpenFont(filename, size)
	if extfont == nil {
		panic("Could not load font")
	}
	defer extfont.Close()

	font.height = extfont.LineSkip()

	for _, ch := range "abcdefghijklmnopqrdstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :;@'<>,.?/~#{}[]!£$%^&*()_-+=\"\\|" {
		_, _, _, _, advance, err := extfont.GlyphMetrics(uint16(ch))
		if err != 0 {
			panic("Could not get glyph metrics")
		}

		// Create a bitmap with width=advance, height=font.height
		surface := sdl.CreateRGBSurface(sdl.SWSURFACE|sdl.SRCALPHA, advance, font.height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
		//surface.FillRect(&sdl.Rect{0,0, uint16(advance), uint16(font.height)}, 0x0)
		// rect := sdl.Rect{0,0, uint16(advance), uint16(ascent)}
		// rect := sdl.Rect{int16(minx), int16(ascent)-int16(maxy), 0, 0}

		fontSurface := ttf.RenderText_Blended(extfont, string(ch), sdl.ColorFromGoColor(c))
		fontSurface.Blit(nil, surface, nil)

		rgba := image.NewRGBA(image.Rect(0, 0, advance, font.height))
		for x := 0; x < advance; x++ {
			for y := 0; y < font.height; y++ {
				rgba.Set(x, y, fontSurface.At(x, font.height-y))
			}
		}

		font.widths[ch] = advance
		font.textures[ch] = gl.GenTexture()
		font.textures[ch].Bind(gl.TEXTURE_2D)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
		gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
		gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, advance, font.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, &rgba.Pix[0])
		font.textures[ch].Unbind(gl.TEXTURE_2D)
	}
	return &font
}
Exemple #7
0
func (c *Screen) setFont(path string, size int) error {
	if c.font != nil {
		c.font.Close()
	}

	if size < 10 {
		size = 10
	}
	c.font = ttf.OpenFont(path, size)
	if c.font == nil {
		errMsg := fmt.Sprintf("failed to open font from %s: %s",
			path, sdl.GetError())
		return errors.New(errMsg)
	}
	/* c.font.SetStyle(ttf.STYLE_UNDERLINE) */

	c.fontSize = size
	c.fontPath = path
	c.lineHeight = uint16(c.font.LineSkip())

	log.Printf("fontsize=%d lineheight=%d\n", c.fontSize, c.lineHeight)
	return nil
}
Exemple #8
0
func main() {

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

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

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

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

	if screen == nil {
		panic(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("test.png")

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

	sdl.WM_SetIcon(image, nil)

	running := true

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

	if font == nil {
		panic(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("test.ogg")
	sound := mixer.LoadWAV("sound.ogg")

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

	music.PlayMusic(-1)

	if sdl.GetKeyName(270) != "[+]" {
		panic("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)

	for running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyboardEvent:
				println("")
				println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))

				if e.Keysym.Sym == 27 {
					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.ResizeEvent:
				println("resize screen ", e.W, e.H)

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

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

		screen.FillRect(nil, 0x302019)
		screen.Blit(&sdl.Rect{0, 0, 0, 0}, text, nil)

		loop := true

		for loop {

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

		}

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

		screen.Flip()
		sdl.Delay(25)
	}

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

	ttf.Quit()
	sdl.Quit()
}
Exemple #9
0
func (self MathShower) Render() *sdl.Surface {
	switch self.Subtype {
	case "Number":
		font1 := ttf.OpenFont(self.Fontname, self.Fontsize())
		defer font1.Close()
		txt := ttf.RenderUTF8_Blended(font1, self.Txt, self.Color)
		if self.Indextxt == "" && !self.RenderEmptyIndextxt {
			mainsurface := sdl.CreateRGBSurface(0, txt.Bounds().Max.X,
				txt.Bounds().Max.Y, 32, 0, 0, 0, 0)
			mainsurface.FillRect(nil, 0xFFFFFF)
			mainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, txt, nil)
			if self.CursorPosition != -1 && self.CursorPosition < len(self.Txt) {
				curssurf := RenderCursorSurface(font1)
				if self.CursorPosition == 0 {
					mainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, curssurf, nil)
				} else if self.CursorPosition == len(self.Txt) {
					mainsurface.Blit(&sdl.Rect{int16(mainsurface.Bounds().Max.X - curssurf.Bounds().Max.X), 0, 0, 0},
						curssurf, nil)
				} else {
					inputwhere, _, _ := font1.SizeUTF8(self.Txt[:self.CursorPosition])
					mainsurface.Blit(&sdl.Rect{int16(inputwhere), 0, 0, 0}, curssurf, nil)
				}
			}
			return mainsurface
		} else if self.RenderEmptyIndextxt {
			font2 := ttf.OpenFont(self.Fontname, self.Fontsize()-int(float64(self.Fontsize())*0.5))
			defer font2.Close()
			if font2 == nil {
				panic(sdl.GetError())
			}
			subtext := ttf.RenderUTF8_Blended(font2, self.Indextxt, self.Color)
			mainsurface := sdl.CreateRGBSurface(0, txt.Bounds().Max.X+subtext.Bounds().Max.X,
				txt.Bounds().Max.Y+subtext.Bounds().Max.Y, 32, 0, 0, 0, 0)
			mainsurface.FillRect(nil, 0xFFFFFF)
			mainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, txt, nil)
			overmainsurface := sdl.CreateRGBSurface(0, txt.Bounds().Max.X, txt.Bounds().Max.Y, 32,
				0, 0, 0, 0)
			overmainsurface.FillRect(nil, 0xFFFFFF)
			overmainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, txt, nil)
			if self.CursorPosition != -1 && self.CursorPosition < len(self.Txt) {
				curssurf := RenderCursorSurface(font1)
				if self.CursorPosition == 0 {
					overmainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, curssurf, nil)
				} else if self.CursorPosition == len(self.Txt) {
					overmainsurface.Blit(&sdl.Rect{int16(overmainsurface.Bounds().Max.X - curssurf.Bounds().Max.X), 0, 0, 0},
						curssurf, nil)
				} else {
					inputwhere, _, _ := font1.SizeUTF8(self.Txt[:self.CursorPosition])
					overmainsurface.Blit(&sdl.Rect{int16(inputwhere), 0, 0, 0}, curssurf, nil)
				}
			}
			mainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, overmainsurface, nil)
			submainsurface := sdl.CreateRGBSurface(0, font2.Height()+2+2, font2.Height()+2+2,
				32, 0, 0, 0, 0)
			subsubmainsurface := sdl.CreateRGBSurface(0, font2.Height()+2, font2.Height()+2,
				32, 0, 0, 0, 0)
			submainsurface.FillRect(nil, 0x0)
			subsubmainsurface.FillRect(nil, 0xFFFFFF)
			if self.CursorPosition == len(self.Txt) {
				curssurf := RenderCursorSurface(font2)
				subsubmainsurface.Blit(&sdl.Rect{2, 1, 0, 0}, curssurf, nil)
			}
			submainsurface.Blit(&sdl.Rect{1, 1, 0, 0}, subsubmainsurface, nil)
			mainsurface.Blit(&sdl.Rect{int16(txt.Bounds().Max.X), int16(font1.Ascent()), 0, 0},
				submainsurface, nil)

			return mainsurface
		}

		font2 := ttf.OpenFont(self.Fontname, self.Fontsize()-int(float64(self.Fontsize())*0.5))
		defer font2.Close()
		if font2 == nil {
			panic(sdl.GetError())
		}
		subtext := ttf.RenderUTF8_Blended(font2, self.Indextxt, self.Color)
		mainsurface := sdl.CreateRGBSurface(0, txt.Bounds().Max.X+subtext.Bounds().Max.X,
			txt.Bounds().Max.Y+subtext.Bounds().Max.Y, 32, 0, 0, 0, 0)
		mainsurface.FillRect(nil, 0xFFFFFF)
		mainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, txt, nil)
		overmainsurface := sdl.CreateRGBSurface(0, txt.Bounds().Max.X, txt.Bounds().Max.Y, 32,
			0, 0, 0, 0)
		overmainsurface.FillRect(nil, 0xFFFFFF)
		overmainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, txt, nil)
		if self.CursorPosition != -1 && self.CursorPosition < len(self.Txt) {
			curssurf := RenderCursorSurface(font1)
			if self.CursorPosition == 0 {
				overmainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, curssurf, nil)
			} else if self.CursorPosition == len(self.Txt) {
				overmainsurface.Blit(&sdl.Rect{int16(overmainsurface.Bounds().Max.X - curssurf.Bounds().Max.X), 0, 0, 0},
					curssurf, nil)
			} else {
				inputwhere, _, _ := font1.SizeUTF8(self.Txt[:self.CursorPosition])
				overmainsurface.Blit(&sdl.Rect{int16(inputwhere), 0, 0, 0}, curssurf, nil)
			}
		}
		mainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, overmainsurface, nil)
		submainsurface := sdl.CreateRGBSurface(0, subtext.Bounds().Max.X, subtext.Bounds().Max.Y,
			32, 0, 0, 0, 0)
		submainsurface.FillRect(nil, 0xFFFFFF)
		submainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, subtext, nil)
		if self.CursorPosition >= len(self.Txt) {
			Subcurspos := self.CursorPosition - len(self.Txt)
			curssurf := RenderCursorSurface(font2)
			if Subcurspos == 0 {
				submainsurface.Blit(&sdl.Rect{0, 0, 0, 0}, curssurf, nil)
			} else {
				inputwhere, _, _ := font2.SizeUTF8(self.Indextxt[:Subcurspos])
				submainsurface.Blit(&sdl.Rect{int16(inputwhere), 0, 0, 0}, curssurf, nil)
			}
		}
		mainsurface.Blit(&sdl.Rect{int16(txt.Bounds().Max.X), int16(font1.Ascent()), 0, 0},
			submainsurface, nil)

		return mainsurface
	case "Fraction":
		numeratorsurf := (*self.Parts)[0].Render()
		Denominatorsurf := (*self.Parts)[1].Render()
		ysum := numeratorsurf.Bounds().Max.Y + Denominatorsurf.Bounds().Max.Y
		maxx := numeratorsurf.Bounds().Max.X
		if Denominatorsurf.Bounds().Max.X > maxx {
			maxx = Denominatorsurf.Bounds().Max.X
		}
		mainsurface := sdl.CreateRGBSurface(0, maxx+6, ysum, 32,
			0, 0, 0, 0)
		mainsurface.FillRect(nil, 0xFFFFFF)
		line := sdl.CreateRGBSurface(0, maxx+6, 1, 32, 0, 0, 0, 0)
		line.FillRect(nil, 0x000000)
		mainsurface.Blit(&sdl.Rect{int16(float64(maxx-numeratorsurf.Bounds().Max.X) / 2),
			0, 0, 0}, numeratorsurf, nil)
		mainsurface.Blit(&sdl.Rect{int16(float64(maxx-Denominatorsurf.Bounds().Max.X) / 2),
			int16(numeratorsurf.Bounds().Max.Y), 0, 0}, Denominatorsurf, nil)
		mainsurface.Blit(&sdl.Rect{0, int16(numeratorsurf.Bounds().Max.Y), 0, 0}, line, nil)

		return mainsurface
	}
	var k *sdl.Surface
	return k
}