Esempio n. 1
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)
	}
}
Esempio n. 2
0
func (c *Screen) DisplayScript(script *subtitle.Script) {
	if script == nil {
		return
	}

	c.currScript = script

	log.Printf("display %d.%s", script.Idx, script.TextWithoutMarkup())

	c.surface.FillRect(&sdl.Rect{0, int16(c.debugLineHeight), c.w, c.h}, 0 /* BG_COLOR */)
	offsetY := c.debugLineHeight

	for _, line := range strings.Split(script.TextWithoutMarkup(), "\n") {
		if strings.TrimSpace(line) == "" {
			offsetY += c.lineHeight
			continue
		}
		runeLine := []rune(line)
		runeLineLen := len(runeLine)
		runeLineStart := 0

		for runeLineStart != runeLineLen {
			/* log.Println("start =", runeLineStart, "len =", runeLineLen) */
			runeSubLine := runeLine[runeLineStart:]
			runeSubLineLen := len(runeSubLine)
			i := sort.Search(runeSubLineLen, func(i int) bool {
				w, _, _ := c.font.SizeUTF8(string(runeSubLine[:i]))
				return w+20 >= int(c.w)
			})
			/* log.Printf("runeSubLine=%s, i=%d\n", string(runeSubLine), i) */

			if i > runeSubLineLen {
				i = runeSubLineLen
			}

			w, _, _ := c.font.SizeUTF8(string(runeSubLine[:i]))
			for w > int(c.w) {
				i -= 1
				w, _, _ = c.font.SizeUTF8(string(runeSubLine[:i]))
			}

			subline := string(runeLine[runeLineStart : runeLineStart+i])
			subline = strings.TrimSpace(subline)
			/* log.Println("subline=", subline) */
			runeLineStart += i
			if runeLineStart > runeLineLen {
				runeLineStart = runeLineLen
			}

			if subline == "" {
				continue
			}

			offsetX := 10
			if opt.alignCenter {
				w, _, err := c.font.SizeUTF8(subline)
				if err != 0 {
					log.Fatal("Failed to get size of the font")
				}
				offsetX = (int(c.w) - w) / 2
			}

			glypse := ttf.RenderUTF8_Blended(c.font, subline, TEXT_COLOR)
			if glypse == nil {
				continue
			}
			lt := sdl.Rect{int16(offsetX), int16(offsetY), 0, 0}
			c.surface.Blit(&lt, glypse, nil)
			offsetY += c.lineHeight
		}
	}
	c.updateC <- 1
}
Esempio n. 3
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
}