Clean resources after import+error handling

This commit is contained in:
bloeys
2021-11-19 08:35:56 +04:00
parent 8c752a0bda
commit 8bf9dc9121
2 changed files with 17 additions and 5 deletions

View File

@ -10,10 +10,12 @@ package asig
//Functions
struct aiScene* aiImportFile(const char* pFile, unsigned int pFlags);
void aiReleaseImport(const struct aiScene* pScene);
const char* aiGetErrorString();
*/
import "C"
import (
"errors"
"unsafe"
"github.com/bloeys/gglm/gglm"
@ -50,15 +52,22 @@ type Scene struct {
Cameras []*Camera
}
func ImportFile(file string, postProcessFlags uint) *Scene {
func ImportFile(file string, postProcessFlags uint) (*Scene, error) {
cstr := C.CString(file)
defer C.free(unsafe.Pointer(cstr))
cs := C.aiImportFile(cstr, C.uint(postProcessFlags))
scene := parseScene(cs)
if cs == nil {
return nil, getAiErr()
}
defer C.aiReleaseImport(cs)
return scene
return parseScene(cs), nil
}
func getAiErr() error {
return errors.New("asig error: " + C.GoString(C.aiGetErrorString()))
}
func parseScene(cs *C.struct_aiScene) *Scene {