func CreateRGBSurface(width, height int) *Surface { var rmask, gmask, bmask, amask uint32 byteorder := 0 bigendian := 0 if byteorder == bigendian { rmask = 0xff000000 gmask = 0x00ff0000 bmask = 0x0000ff00 amask = 0x000000ff } else { rmask = 0x000000ff gmask = 0x0000ff00 bmask = 0x00ff0000 amask = 0xff000000 } inner := sdl.CreateRGBSurface( sdl.SRCALPHA, // flags width, height, 32, // depth rmask, gmask, bmask, amask, ) if inner == nil { panic(sdl.GetError()) } return &Surface{inner} }
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 RenderCursorSurface(font *ttf.Font) *sdl.Surface { height := font.Height() width := 1 width = int(0.1 * float64(height)) if width < 1 { width = 1 } return sdl.CreateRGBSurface(0, width, height, 32, 0, 0, 0, 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 }
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 }