examples: rewrite text example as drawing-text
Signed-off-by: Lilis Iskandar <lilis@veand.co>
This commit is contained in:
parent
144230c86f
commit
c29aa61c8c
|
@ -0,0 +1,81 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/veandco/go-sdl2/ttf"
|
||||
)
|
||||
|
||||
const (
|
||||
fontPath = "../../assets/test.ttf"
|
||||
fontSize = 32
|
||||
)
|
||||
|
||||
func run() (err error) {
|
||||
var window *sdl.Window
|
||||
var font *ttf.Font
|
||||
var surface *sdl.Surface
|
||||
var text *sdl.Surface
|
||||
|
||||
if err = ttf.Init(); err != nil {
|
||||
return
|
||||
}
|
||||
defer ttf.Quit()
|
||||
|
||||
if err = sdl.Init(sdl.INIT_VIDEO); err != nil {
|
||||
return
|
||||
}
|
||||
defer sdl.Quit()
|
||||
|
||||
// Create a window for us to draw the text on
|
||||
if window, err = sdl.CreateWindow("Drawing text", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN); err != nil {
|
||||
return
|
||||
}
|
||||
defer window.Destroy()
|
||||
|
||||
if surface, err = window.GetSurface(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Load the font for our text
|
||||
if font, err = ttf.OpenFont(fontPath, fontSize); err != nil {
|
||||
return
|
||||
}
|
||||
defer font.Close()
|
||||
|
||||
// Create a red text with the font
|
||||
if text, err = font.RenderUTF8Blended("Hello, World!", sdl.Color{R: 255, G: 0, B: 0, A: 255}); err != nil {
|
||||
return
|
||||
}
|
||||
defer text.Free()
|
||||
|
||||
// Draw the text around the center of the window
|
||||
if err = text.Blit(nil, surface, &sdl.Rect{X: 400 - (text.W / 2), Y: 300 - (text.H / 2), W: 0, H: 0}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Update the window surface with what we have drawn
|
||||
window.UpdateSurface()
|
||||
|
||||
// Run infinite loop until user closes the window
|
||||
running := true
|
||||
for running {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
running = false
|
||||
}
|
||||
}
|
||||
|
||||
sdl.Delay(16)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"github.com/veandco/go-sdl2/ttf"
|
||||
)
|
||||
|
||||
var winTitle string = "Text"
|
||||
var winWidth, winHeight int32 = 800, 600
|
||||
|
||||
func run() int {
|
||||
var window *sdl.Window
|
||||
var font *ttf.Font
|
||||
var surface *sdl.Surface
|
||||
var solid *sdl.Surface
|
||||
var err error
|
||||
|
||||
sdl.Init(sdl.INIT_VIDEO)
|
||||
|
||||
if err := ttf.Init(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to initialize TTF: %s\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
|
||||
return 2
|
||||
}
|
||||
defer window.Destroy()
|
||||
|
||||
if font, err = ttf.OpenFont("../../assets/test.ttf", 32); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to open font: %s\n", err)
|
||||
return 4
|
||||
}
|
||||
defer font.Close()
|
||||
|
||||
if solid, err = font.RenderUTF8Solid("Hello, World!", sdl.Color{255, 0, 0, 255}); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to render text: %s\n", err)
|
||||
return 5
|
||||
}
|
||||
defer solid.Free()
|
||||
|
||||
if surface, err = window.GetSurface(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to get window surface: %s\n", err)
|
||||
return 6
|
||||
}
|
||||
|
||||
if err = solid.Blit(nil, surface, nil); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to put text on window surface: %s\n", err)
|
||||
return 7
|
||||
}
|
||||
|
||||
// Show the pixels for a while
|
||||
window.UpdateSurface()
|
||||
sdl.PollEvent()
|
||||
sdl.Delay(3000)
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
os.Exit(run())
|
||||
}
|
Loading…
Reference in New Issue