veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.17k stars 219 forks source link

Hello-World example on a ReadMe page #563

Closed vault-thirteen closed 1 year ago

vault-thirteen commented 1 year ago

After struggling several hours with colour bug, I have a suggestion to make the "Hello-World" code example on the main page (ReadMe file) more user-friendly. By that I mean, colours should not be hard-coded as it is now (0xffff0000) because it is platform-dependent and does not produce same results on all computers. Instead, it would be cool to set colours via functions.

I have written my Hello-World while I was solving the previous isssue. Some parts of it may be useful.

package main

import (
    "github.com/vault-thirteen/errorz"
    "github.com/veandco/go-sdl2/sdl"

    "fmt"
    "log"
)

func main() {
    var err error
    err = work()
    if err != nil {
        log.Fatal(err)
    }
}

func work() (err error) {
    err = sdl.Init(sdl.INIT_EVERYTHING)
    if err != nil {
        return err
    }
    defer sdl.Quit()

    var window *sdl.Window
    window, err = sdl.CreateWindow("SDL Test", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, 640, 480, sdl.WINDOW_SHOWN)
    if err != nil {
        return err
    }
    defer func() {
        derr := window.Destroy()
        if derr != nil {
            err = errorz.Combine(err, derr)
        }
    }()

    var surface *sdl.Surface
    surface, err = window.GetSurface()
    if err != nil {
        return err
    }

    colour := sdl.Color{R: 255, G: 0, B: 255, A: 255}
    pixel := sdl.MapRGBA(surface.Format, colour.R, colour.G, colour.B, colour.A)

    err = surface.FillRect(nil, pixel)
    if err != nil {
        return err
    }

    err = window.UpdateSurface()
    if err != nil {
        return err
    }

    running := true
    var event sdl.Event
    for running {
        event = sdl.WaitEvent()

        switch event.(type) {
        case *sdl.QuitEvent:
            fmt.Println("QuitEvent")
            running = false
            break
        }
    }

    return nil
}
  1. Here, colour is set via sdl.Color object and sdl.MapRGBA function. This is the official SDL way which works everywhere.

  2. We use WaitEvent function which does not create idle load loops on CPU and is good for beginners like me. When I used PollEvent in a loop, even with a delay, one of the CPU cores was loaded to 100% by doing nothing :)

You can throw away my pedantic error checks and combining errors.

Thank you.

veeableful commented 1 year ago

Hi @vault-thirteen, thank you for the input! I agree that the colour should be generated using the SDL2 function in the example so I have updated the README with updated example. I will leave PollEvent() as the default because it's easier to write game loop with it in my opinion. The user has choice to use WaitEvent() if that's not the case.

vault-thirteen commented 1 year ago

Thank you, @veeableful !