From c29aa61c8c067c022b8595d0fb6f02e4ee87731a Mon Sep 17 00:00:00 2001 From: Lilis Iskandar Date: Sun, 15 Dec 2019 20:13:20 +0800 Subject: [PATCH] examples: rewrite text example as drawing-text Signed-off-by: Lilis Iskandar --- examples/drawing-text/drawing-text.go | 81 +++++++++++++++++++++++++++ examples/text/text.go | 66 ---------------------- 2 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 examples/drawing-text/drawing-text.go delete mode 100644 examples/text/text.go diff --git a/examples/drawing-text/drawing-text.go b/examples/drawing-text/drawing-text.go new file mode 100644 index 0000000..eabc24f --- /dev/null +++ b/examples/drawing-text/drawing-text.go @@ -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) + } +} diff --git a/examples/text/text.go b/examples/text/text.go deleted file mode 100644 index a69f349..0000000 --- a/examples/text/text.go +++ /dev/null @@ -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()) -}