code.witches.io/go/sdl2@v0.1.1/error.go (about)

     1  package sdl
     2  
     3  // #include <SDL2/SDL_error.h>
     4  //
     5  // int setError(char *s) {
     6  //   return SDL_SetError("%s", s);
     7  // }
     8  import "C"
     9  import (
    10  	"fmt"
    11  	"unsafe"
    12  )
    13  
    14  func ClearError() {
    15  	C.SDL_ClearError()
    16  }
    17  
    18  func GetError() error {
    19  	ptr := C.SDL_GetError()
    20  	if ptr == nil {
    21  		return nil
    22  	}
    23  	err := C.GoString(ptr)
    24  	if len(err) == 0 {
    25  		return nil
    26  	}
    27  	return fmt.Errorf("sdl: %s", err)
    28  }
    29  
    30  func SetError(format string, a ...interface{}) {
    31  	nativeError := C.CString(fmt.Sprintf(format, a...))
    32  	defer C.free(unsafe.Pointer(nativeError))
    33  
    34  	C.setError(nativeError)
    35  }